|
1 /***************************************************************************** |
|
2 * |
|
3 * $Id$ |
|
4 * |
|
5 ****************************************************************************/ |
|
6 |
|
7 #include <getopt.h> |
|
8 |
|
9 #include <iostream> |
|
10 #include <string> |
|
11 using namespace std; |
|
12 |
|
13 #include "Master.h" |
|
14 |
|
15 /*****************************************************************************/ |
|
16 |
|
17 #define DEFAULT_MASTER 0 |
|
18 #define DEFAULT_COMMAND "slaves" |
|
19 #define DEFAULT_SLAVESPEC "" |
|
20 |
|
21 static unsigned int masterIndex = DEFAULT_MASTER; |
|
22 static string slaveSpec = DEFAULT_SLAVESPEC; |
|
23 static string command = DEFAULT_COMMAND; |
|
24 |
|
25 /*****************************************************************************/ |
|
26 |
|
27 void printUsage() |
|
28 { |
|
29 cerr |
|
30 << "Usage: ethercat <COMMAND> [OPTIONS]" << endl |
|
31 << "Commands:" << endl |
|
32 << " list (ls, slaves) List all slaves (former 'lsec')." << endl |
|
33 << "Global options:" << endl |
|
34 << " --master -m <master> Index of the master to use. Default: " |
|
35 << DEFAULT_MASTER << endl |
|
36 << " --slave -s <slave> Slave specification. Default: All " |
|
37 "slaves." << endl |
|
38 << " --help -h Show this help." << endl; |
|
39 } |
|
40 |
|
41 /*****************************************************************************/ |
|
42 |
|
43 void getOptions(int argc, char **argv) |
|
44 { |
|
45 int c, argCount, optionIndex, number; |
|
46 char *remainder; |
|
47 |
|
48 static struct option longOptions[] = { |
|
49 //name, has_arg, flag, val |
|
50 {"master", required_argument, NULL, 'm'}, |
|
51 {"slave", required_argument, NULL, 's'}, |
|
52 {"help", no_argument, NULL, 'h'}, |
|
53 {} |
|
54 }; |
|
55 |
|
56 do { |
|
57 c = getopt_long(argc, argv, "m:s:h", longOptions, &optionIndex); |
|
58 |
|
59 switch (c) { |
|
60 case 'm': |
|
61 number = strtoul(optarg, &remainder, 0); |
|
62 if (remainder == optarg || *remainder || number < 0) { |
|
63 cerr << "Invalid master number " << optarg << "!" << endl; |
|
64 printUsage(); |
|
65 exit(1); |
|
66 } |
|
67 masterIndex = number; |
|
68 break; |
|
69 |
|
70 case 's': |
|
71 slaveSpec = optarg; |
|
72 break; |
|
73 |
|
74 case 'h': |
|
75 case '?': |
|
76 printUsage(); |
|
77 exit(0); |
|
78 |
|
79 default: |
|
80 break; |
|
81 } |
|
82 } |
|
83 while (c != -1); |
|
84 |
|
85 argCount = argc - optind; |
|
86 |
|
87 if (!argCount) { |
|
88 cerr << "Please specify a command!" << endl; |
|
89 printUsage(); |
|
90 exit(1); |
|
91 } |
|
92 |
|
93 command = argv[optind]; |
|
94 } |
|
95 |
|
96 /****************************************************************************/ |
|
97 |
|
98 int main(int argc, char **argv) |
|
99 { |
|
100 Master master; |
|
101 |
|
102 getOptions(argc, argv); |
|
103 |
|
104 master.open(masterIndex); |
|
105 |
|
106 if (command == "list" || command == "ls" || command == "slaves") { |
|
107 master.listSlaves(); |
|
108 } else { |
|
109 cerr << "Unknown command " << command << "!" << endl; |
|
110 printUsage(); |
|
111 exit(1); |
|
112 } |
|
113 |
|
114 return 0; |
|
115 } |
|
116 |
|
117 /****************************************************************************/ |