fp@922: /***************************************************************************** fp@922: * fp@922: * $Id$ fp@922: * fp@922: ****************************************************************************/ fp@922: fp@922: #include fp@1118: #include // basename() fp@922: fp@922: #include fp@922: #include fp@956: #include fp@922: using namespace std; fp@922: fp@922: #include "Master.h" fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: #define DEFAULT_MASTER 0 fp@922: fp@1118: static string cmdName; fp@922: static unsigned int masterIndex = DEFAULT_MASTER; fp@968: static int slavePosition = -1; fp@968: static int domainIndex = -1; fp@968: static string command; fp@956: vector commandArgs; fp@1099: static Master::Verbosity verbosity = Master::Normal; fp@968: string dataTypeStr; fp@980: bool force = false; fp@1118: bool helpWanted = false; fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: void printUsage() fp@922: { fp@922: cerr fp@1118: << "Usage: " << cmdName << " [OPTIONS]" << endl fp@922: << "Commands:" << endl fp@1065: << " alias Write alias addresses." << endl fp@1065: << " config Show bus configuration." << endl fp@1065: << " data Output binary domain process data." << endl fp@1065: << " debug Set the master's debug level." << endl fp@1065: << " domain Show domain information." << endl fp@1065: << " master Show master information." << endl fp@1065: << " pdos List Pdo assignment/mapping." << endl fp@1065: << " sdo_download Write an Sdo entry." << endl fp@1065: << " sdos List Sdo dictionaries." << endl fp@1065: << " sdo_upload Read an Sdo entry." << endl fp@1065: << " sii_read Output a slave's SII contents." << endl fp@1065: << " sii_write Write slave's SII contents." << endl fp@1065: << " slaves Show slaves." << endl fp@1065: << " state Request slave states." << endl fp@1065: << " xml Generate slave information xmls." << endl fp@922: << "Global options:" << endl fp@922: << " --master -m Index of the master to use. Default: " fp@953: << DEFAULT_MASTER << endl fp@948: << " --slave -s Positive numerical ring position," fp@936: << endl fp@953: << " or 'all' for all slaves (default)." fp@936: << endl fp@948: << " --domain -d Positive numerical index," fp@948: << endl fp@953: << " or 'all' for all domains (default)." fp@953: << endl fp@968: << " --type -t Forced Sdo data type." << endl fp@980: << " --force -f Force action." << endl fp@1065: << " --quiet -q Output less information." << endl fp@1065: << " --verbose -v Output more information." << endl fp@1118: << " --help -h Show this help." << endl fp@1118: << "Call '" << cmdName << " --help' for command-specific " fp@1118: << "help." << endl fp@1118: << "Send bug reports to " << PACKAGE_BUGREPORT << "." << endl; fp@922: } fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: void getOptions(int argc, char **argv) fp@922: { fp@922: int c, argCount, optionIndex, number; fp@922: char *remainder; fp@922: fp@922: static struct option longOptions[] = { fp@1065: //name, has_arg, flag, val fp@1065: {"master", required_argument, NULL, 'm'}, fp@1065: {"slave", required_argument, NULL, 's'}, fp@1065: {"domain", required_argument, NULL, 'd'}, fp@1065: {"type", required_argument, NULL, 't'}, fp@1065: {"force", no_argument, NULL, 'f'}, fp@1065: {"quiet", no_argument, NULL, 'q'}, fp@1065: {"verbose", no_argument, NULL, 'v'}, fp@1065: {"help", no_argument, NULL, 'h'}, fp@922: {} fp@922: }; fp@922: fp@922: do { fp@1065: c = getopt_long(argc, argv, "m:s:d:t:fqvh", longOptions, &optionIndex); fp@922: fp@922: switch (c) { fp@922: case 'm': fp@922: number = strtoul(optarg, &remainder, 0); fp@922: if (remainder == optarg || *remainder || number < 0) { fp@922: cerr << "Invalid master number " << optarg << "!" << endl; fp@922: printUsage(); fp@922: exit(1); fp@922: } fp@922: masterIndex = number; fp@922: break; fp@922: fp@922: case 's': fp@936: if (!strcmp(optarg, "all")) { fp@936: slavePosition = -1; fp@936: } else { fp@936: number = strtoul(optarg, &remainder, 0); fp@936: if (remainder == optarg || *remainder fp@936: || number < 0 || number > 0xFFFF) { fp@936: cerr << "Invalid slave position " fp@936: << optarg << "!" << endl; fp@936: printUsage(); fp@936: exit(1); fp@936: } fp@936: slavePosition = number; fp@935: } fp@922: break; fp@922: fp@948: case 'd': fp@948: if (!strcmp(optarg, "all")) { fp@948: domainIndex = -1; fp@948: } else { fp@948: number = strtoul(optarg, &remainder, 0); fp@948: if (remainder == optarg || *remainder || number < 0) { fp@948: cerr << "Invalid domain index " fp@948: << optarg << "!" << endl; fp@948: printUsage(); fp@948: exit(1); fp@948: } fp@948: domainIndex = number; fp@948: } fp@948: break; fp@948: fp@968: case 't': fp@968: dataTypeStr = optarg; fp@968: break; fp@968: fp@980: case 'f': fp@980: force = true; fp@980: break; fp@980: fp@966: case 'q': fp@1099: verbosity = Master::Quiet; fp@1065: break; fp@1065: fp@1065: case 'v': fp@1099: verbosity = Master::Verbose; fp@966: break; fp@966: fp@922: case 'h': fp@1118: helpWanted = true; fp@1118: break; fp@1118: fp@922: case '?': fp@922: printUsage(); fp@1118: exit(1); fp@922: fp@922: default: fp@922: break; fp@922: } fp@922: } fp@922: while (c != -1); fp@922: fp@922: argCount = argc - optind; fp@922: fp@1118: if (!argCount) { fp@1118: if (!helpWanted) { fp@1118: cerr << "Please specify a command!" << endl; fp@1118: } fp@1118: printUsage(); fp@1118: exit(!helpWanted); fp@922: } fp@922: fp@922: command = argv[optind]; fp@956: while (++optind < argc) fp@956: commandArgs.push_back(string(argv[optind])); fp@922: } fp@922: fp@922: /****************************************************************************/ fp@922: fp@922: int main(int argc, char **argv) fp@922: { fp@922: Master master; fp@1118: fp@1118: cmdName = basename(argv[0]); fp@922: fp@922: getOptions(argc, argv); fp@922: fp@1099: master.setIndex(masterIndex); fp@1099: master.setVerbosity(verbosity); fp@1099: fp@935: try { fp@980: if (command == "alias") { fp@980: master.writeAlias(slavePosition, force, commandArgs); fp@990: } else if (command == "config") { fp@1099: master.showConfigs(); fp@980: } else if (command == "data") { fp@949: master.outputData(domainIndex); fp@956: } else if (command == "debug") { fp@956: master.setDebug(commandArgs); fp@949: } else if (command == "domain") { fp@948: master.showDomains(domainIndex); fp@957: } else if (command == "master") { fp@957: master.showMaster(); fp@935: } else if (command == "pdos") { fp@1099: master.listPdos(slavePosition); fp@965: } else if (command == "sdos") { fp@1099: master.listSdos(slavePosition); fp@974: } else if (command == "sdo_download" || command == "sd") { fp@974: master.sdoDownload(slavePosition, dataTypeStr, commandArgs); fp@968: } else if (command == "sdo_upload" || command == "su") { fp@968: master.sdoUpload(slavePosition, dataTypeStr, commandArgs); fp@1065: } else if (command == "slave" || command == "slaves" fp@1065: || command == "list" || command == "ls") { fp@1099: master.showSlaves(slavePosition); fp@978: } else if (command == "sii_read" || command == "sr") { fp@978: master.siiRead(slavePosition); fp@980: } else if (command == "sii_write" || command == "sw") { fp@980: master.siiWrite(slavePosition, force, commandArgs); fp@960: } else if (command == "state") { fp@960: master.requestStates(slavePosition, commandArgs); fp@938: } else if (command == "xml") { fp@938: master.generateXml(slavePosition); fp@935: } else { fp@935: cerr << "Unknown command " << command << "!" << endl; fp@935: printUsage(); fp@935: exit(1); fp@935: } fp@935: } catch (MasterException &e) { fp@935: cerr << e.what() << endl; fp@922: exit(1); fp@922: } fp@922: fp@922: return 0; fp@922: } fp@922: fp@922: /****************************************************************************/