fp@922: /***************************************************************************** fp@922: * fp@922: * $Id$ fp@922: * fp@922: ****************************************************************************/ fp@922: fp@922: #include 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@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@966: static bool quiet = false; fp@968: string dataTypeStr; fp@922: fp@922: /*****************************************************************************/ fp@922: fp@922: void printUsage() fp@922: { fp@922: cerr fp@922: << "Usage: ethercat [OPTIONS]" << endl fp@922: << "Commands:" << endl fp@949: << " data Output binary domain process data." << endl fp@956: << " debug Set the master debug level." << endl fp@948: << " domain Show domain information." << endl fp@922: << " list (ls, slaves) List all slaves (former 'lsec')." << endl fp@957: << " master Show master information." << endl fp@965: << " pdos List Pdo mapping." << endl fp@965: << " sdos List Sdo dictionaries." << endl fp@968: << " sdo_upload (su) Read Sdo entries." << endl fp@965: << " state Request slave states." << endl fp@965: << " 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@966: << " --quiet -q Show less output." << endl fp@922: << " --help -h Show this help." << 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@922: //name, has_arg, flag, val fp@922: {"master", required_argument, NULL, 'm'}, fp@922: {"slave", required_argument, NULL, 's'}, fp@948: {"domain", required_argument, NULL, 'd'}, fp@968: {"type", required_argument, NULL, 't'}, fp@966: {"quiet", no_argument, NULL, 'q'}, fp@922: {"help", no_argument, NULL, 'h'}, fp@922: {} fp@922: }; fp@922: fp@922: do { fp@968: c = getopt_long(argc, argv, "m:s:d:t:qh", 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@966: case 'q': fp@966: quiet = true; fp@966: break; fp@966: fp@922: case 'h': fp@922: case '?': fp@922: printUsage(); fp@922: exit(0); 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@922: if (!argCount) { fp@922: cerr << "Please specify a command!" << endl; fp@922: printUsage(); fp@922: exit(1); 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@922: fp@922: getOptions(argc, argv); fp@922: fp@935: try { fp@935: master.open(masterIndex); fp@922: fp@949: 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@948: } else if (command == "list" || command == "ls" || command == "slaves") { fp@935: master.listSlaves(); fp@957: } else if (command == "master") { fp@957: master.showMaster(); fp@935: } else if (command == "pdos") { fp@966: master.listPdos(slavePosition, quiet); fp@965: } else if (command == "sdos") { fp@966: master.listSdos(slavePosition, quiet); fp@968: } else if (command == "sdo_upload" || command == "su") { fp@968: master.sdoUpload(slavePosition, dataTypeStr, 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: /****************************************************************************/