|
1 /***************************************************************************** |
|
2 * |
|
3 * $Id$ |
|
4 * |
|
5 ****************************************************************************/ |
|
6 |
|
7 #include <getopt.h> |
|
8 #include <libgen.h> // basename() |
|
9 |
|
10 #include <iostream> |
|
11 #include <string> |
|
12 #include <vector> |
|
13 using namespace std; |
|
14 |
|
15 #include "Master.h" |
|
16 |
|
17 /*****************************************************************************/ |
|
18 |
|
19 #define DEFAULT_MASTER 0 |
|
20 |
|
21 static string cmdName; |
|
22 static unsigned int masterIndex = DEFAULT_MASTER; |
|
23 static int slavePosition = -1; |
|
24 static int domainIndex = -1; |
|
25 static string command; |
|
26 vector<string> commandArgs; |
|
27 static Master::Verbosity verbosity = Master::Normal; |
|
28 string dataTypeStr; |
|
29 bool force = false; |
|
30 bool helpWanted = false; |
|
31 |
|
32 /*****************************************************************************/ |
|
33 |
|
34 void printUsage() |
|
35 { |
|
36 cerr |
|
37 << "Usage: " << cmdName << " <COMMAND> [OPTIONS]" << endl |
|
38 << "Commands:" << endl |
|
39 << " alias Write alias addresses." << endl |
|
40 << " config Show bus configuration." << endl |
|
41 << " data Output binary domain process data." << endl |
|
42 << " debug Set the master's debug level." << endl |
|
43 << " domain Show domain information." << endl |
|
44 << " master Show master information." << endl |
|
45 << " pdos List Pdo assignment/mapping." << endl |
|
46 << " sdo_download Write an Sdo entry." << endl |
|
47 << " sdos List Sdo dictionaries." << endl |
|
48 << " sdo_upload Read an Sdo entry." << endl |
|
49 << " sii_read Output a slave's SII contents." << endl |
|
50 << " sii_write Write slave's SII contents." << endl |
|
51 << " slaves Show slaves." << endl |
|
52 << " state Request slave states." << endl |
|
53 << " xml Generate slave information xmls." << endl |
|
54 << "Global options:" << endl |
|
55 << " --master -m <master> Index of the master to use. Default: " |
|
56 << DEFAULT_MASTER << endl |
|
57 << " --slave -s <index> Positive numerical ring position," |
|
58 << endl |
|
59 << " or 'all' for all slaves (default)." |
|
60 << endl |
|
61 << " --domain -d <index> Positive numerical index," |
|
62 << endl |
|
63 << " or 'all' for all domains (default)." |
|
64 << endl |
|
65 << " --type -t <type> Forced Sdo data type." << endl |
|
66 << " --force -f Force action." << endl |
|
67 << " --quiet -q Output less information." << endl |
|
68 << " --verbose -v Output more information." << endl |
|
69 << " --help -h Show this help." << endl |
|
70 << "Call '" << cmdName << " <COMMAND> --help' for command-specific " |
|
71 << "help." << endl |
|
72 << "Send bug reports to " << PACKAGE_BUGREPORT << "." << endl; |
|
73 } |
|
74 |
|
75 /*****************************************************************************/ |
|
76 |
|
77 void getOptions(int argc, char **argv) |
|
78 { |
|
79 int c, argCount, optionIndex, number; |
|
80 char *remainder; |
|
81 |
|
82 static struct option longOptions[] = { |
|
83 //name, has_arg, flag, val |
|
84 {"master", required_argument, NULL, 'm'}, |
|
85 {"slave", required_argument, NULL, 's'}, |
|
86 {"domain", required_argument, NULL, 'd'}, |
|
87 {"type", required_argument, NULL, 't'}, |
|
88 {"force", no_argument, NULL, 'f'}, |
|
89 {"quiet", no_argument, NULL, 'q'}, |
|
90 {"verbose", no_argument, NULL, 'v'}, |
|
91 {"help", no_argument, NULL, 'h'}, |
|
92 {} |
|
93 }; |
|
94 |
|
95 do { |
|
96 c = getopt_long(argc, argv, "m:s:d:t:fqvh", longOptions, &optionIndex); |
|
97 |
|
98 switch (c) { |
|
99 case 'm': |
|
100 number = strtoul(optarg, &remainder, 0); |
|
101 if (remainder == optarg || *remainder || number < 0) { |
|
102 cerr << "Invalid master number " << optarg << "!" << endl; |
|
103 printUsage(); |
|
104 exit(1); |
|
105 } |
|
106 masterIndex = number; |
|
107 break; |
|
108 |
|
109 case 's': |
|
110 if (!strcmp(optarg, "all")) { |
|
111 slavePosition = -1; |
|
112 } else { |
|
113 number = strtoul(optarg, &remainder, 0); |
|
114 if (remainder == optarg || *remainder |
|
115 || number < 0 || number > 0xFFFF) { |
|
116 cerr << "Invalid slave position " |
|
117 << optarg << "!" << endl; |
|
118 printUsage(); |
|
119 exit(1); |
|
120 } |
|
121 slavePosition = number; |
|
122 } |
|
123 break; |
|
124 |
|
125 case 'd': |
|
126 if (!strcmp(optarg, "all")) { |
|
127 domainIndex = -1; |
|
128 } else { |
|
129 number = strtoul(optarg, &remainder, 0); |
|
130 if (remainder == optarg || *remainder || number < 0) { |
|
131 cerr << "Invalid domain index " |
|
132 << optarg << "!" << endl; |
|
133 printUsage(); |
|
134 exit(1); |
|
135 } |
|
136 domainIndex = number; |
|
137 } |
|
138 break; |
|
139 |
|
140 case 't': |
|
141 dataTypeStr = optarg; |
|
142 break; |
|
143 |
|
144 case 'f': |
|
145 force = true; |
|
146 break; |
|
147 |
|
148 case 'q': |
|
149 verbosity = Master::Quiet; |
|
150 break; |
|
151 |
|
152 case 'v': |
|
153 verbosity = Master::Verbose; |
|
154 break; |
|
155 |
|
156 case 'h': |
|
157 helpWanted = true; |
|
158 break; |
|
159 |
|
160 case '?': |
|
161 printUsage(); |
|
162 exit(1); |
|
163 |
|
164 default: |
|
165 break; |
|
166 } |
|
167 } |
|
168 while (c != -1); |
|
169 |
|
170 argCount = argc - optind; |
|
171 |
|
172 if (!argCount) { |
|
173 if (!helpWanted) { |
|
174 cerr << "Please specify a command!" << endl; |
|
175 } |
|
176 printUsage(); |
|
177 exit(!helpWanted); |
|
178 } |
|
179 |
|
180 command = argv[optind]; |
|
181 while (++optind < argc) |
|
182 commandArgs.push_back(string(argv[optind])); |
|
183 } |
|
184 |
|
185 /****************************************************************************/ |
|
186 |
|
187 int main(int argc, char **argv) |
|
188 { |
|
189 Master master; |
|
190 |
|
191 cmdName = basename(argv[0]); |
|
192 |
|
193 getOptions(argc, argv); |
|
194 |
|
195 master.setIndex(masterIndex); |
|
196 master.setVerbosity(verbosity); |
|
197 |
|
198 try { |
|
199 if (command == "alias") { |
|
200 master.writeAlias(slavePosition, force, commandArgs); |
|
201 } else if (command == "config") { |
|
202 master.showConfigs(); |
|
203 } else if (command == "data") { |
|
204 master.outputData(domainIndex); |
|
205 } else if (command == "debug") { |
|
206 master.setDebug(commandArgs); |
|
207 } else if (command == "domain") { |
|
208 master.showDomains(domainIndex); |
|
209 } else if (command == "master") { |
|
210 master.showMaster(); |
|
211 } else if (command == "pdos") { |
|
212 master.listPdos(slavePosition); |
|
213 } else if (command == "sdos") { |
|
214 master.listSdos(slavePosition); |
|
215 } else if (command == "sdo_download" || command == "sd") { |
|
216 master.sdoDownload(slavePosition, dataTypeStr, commandArgs); |
|
217 } else if (command == "sdo_upload" || command == "su") { |
|
218 master.sdoUpload(slavePosition, dataTypeStr, commandArgs); |
|
219 } else if (command == "slave" || command == "slaves" |
|
220 || command == "list" || command == "ls") { |
|
221 master.showSlaves(slavePosition); |
|
222 } else if (command == "sii_read" || command == "sr") { |
|
223 master.siiRead(slavePosition); |
|
224 } else if (command == "sii_write" || command == "sw") { |
|
225 master.siiWrite(slavePosition, force, commandArgs); |
|
226 } else if (command == "state") { |
|
227 master.requestStates(slavePosition, commandArgs); |
|
228 } else if (command == "xml") { |
|
229 master.generateXml(slavePosition); |
|
230 } else { |
|
231 cerr << "Unknown command " << command << "!" << endl; |
|
232 printUsage(); |
|
233 exit(1); |
|
234 } |
|
235 } catch (MasterException &e) { |
|
236 cerr << e.what() << endl; |
|
237 exit(1); |
|
238 } |
|
239 |
|
240 return 0; |
|
241 } |
|
242 |
|
243 /****************************************************************************/ |