DeviceInfoUtility.cc
Go to the documentation of this file.
1 
37 #ifdef WIN32
38 #ifndef WIN32_LEAN_AND_MEAN
39 #define WIN32_LEAN_AND_MEAN 1
40 #endif
41 
42 #include <windows.h>
43 #include <winsock2.h>
44 #else
45 #include <unistd.h>
46 #endif
47 
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <errno.h>
52 #include <string.h>
53 
55 
57 
58 using namespace crl::multisense;
59 
60 namespace { // anonymous
61 
62 void usage(const char *programNameP)
63 {
64  fprintf(stderr, "USAGE: %s [<options>]\n", programNameP);
65  fprintf(stderr, "Where <options> are:\n");
66  fprintf(stderr, "\t-a <ip_address> : ip address (default=10.66.171.21)\n");
67  fprintf(stderr, "\t-k <passphrase> : passphrase for setting device info\n");
68  fprintf(stderr, "\t-s <file_name> : set device info from file\n");
69  fprintf(stderr, "\t-q : query device info (default)\n");
70  fprintf(stderr, "\t-y : disable confirmation prompt\n");
71 
72  exit(-1);
73 }
74 
75 //
76 // Pass over any whitespace
77 
78 const char *skipSpace(const char *strP)
79 {
80  if (strP)
81  while (isspace(*strP))
82  strP++;
83  return strP;
84 }
85 
86 //
87 // Dump device info contents to a file in format that can
88 // be re-parsed by this utility
89 
90 void printDeviceInfo(const system::DeviceInfo& info,
91  FILE* fP=stdout)
92 {
93  fprintf(fP, "deviceName: %s\n", info.name.c_str());
94  fprintf(fP, "buildDate: %s\n", info.buildDate.c_str());
95  fprintf(fP, "serialNumber: %s\n", info.serialNumber.c_str());
96  fprintf(fP, "hardwareRevision: %d\n\n", info.hardwareRevision);
97 
98  fprintf(fP, "imagerName: %s\n", info.imagerName.c_str());
99  fprintf(fP, "imagerType: %d\n", info.imagerType);
100  fprintf(fP, "imagerWidth: %d\n", info.imagerWidth);
101  fprintf(fP, "imagerHeight: %d\n\n", info.imagerHeight);
102 
103  fprintf(fP, "lensName: %s\n", info.lensName.c_str());
104  fprintf(fP, "lensType: %d\n", info.lensType);
105  fprintf(fP, "nominalBaseline: %f\n", info.nominalBaseline);
106  fprintf(fP, "nominalFocalLength: %f\n", info.nominalFocalLength);
107  fprintf(fP, "nominalRelativeAperture: %f\n\n", info.nominalRelativeAperture);
108 
109  fprintf(fP, "lightingType: %d\n", info.lightingType);
110  fprintf(fP, "numberOfLights: %d\n\n", info.numberOfLights);
111 
112  fprintf(fP, "laserName: %s\n", info.laserName.c_str());
113  fprintf(fP, "laserType: %d\n\n", info.laserType);
114 
115  fprintf(fP, "motorName: %s\n", info.motorName.c_str());
116  fprintf(fP, "motorType: %d\n", info.motorType);
117  fprintf(fP, "motorGearReduction: %f\n\n", info.motorGearReduction);
118 
119  for(uint32_t i=0; i<info.pcbs.size(); i++)
120  fprintf(fP, "pcb: %d %s\n", info.pcbs[i].revision,
121  info.pcbs[i].name.c_str());
122 }
123 
124 //
125 // Parse a file with device information
126 
127 bool parseFile(const std::string& fileName,
128  system::DeviceInfo& info)
129 {
130  FILE *fP = fopen(fileName.c_str(), "r");
131  if (NULL == fP) {
132  fprintf(stderr, "fopen(\"%s\") failed: %s",
133  fileName.c_str(), strerror(errno));
134  return false;
135  }
136 
137  while (!feof(fP)) {
138 
139  char lineP[512] = {0};
140  char tempP[512] = {0};
141  int tempi;
142  float tempf;
143 
144  if (NULL == fgets(lineP, 512, fP))
145  break;
146 
147  const char *s = skipSpace(lineP);
148  if (!s || '#' == *s || '\0' == *s)
149  continue;
150 
151 #ifdef WIN32
152 #define strncasecmp _strnicmp
153 #endif
154 
155 #define CASE_STR(str_,x_) \
156  if (0 == strncasecmp(s, str_, strlen(str_))) { \
157  if (1 != sscanf(s, str_"%[^\n]", tempP)) { \
158  fprintf(stderr, "malformed " str_ " %s\n",s); \
159  return false; \
160  } else { \
161  x_ = std::string(tempP); \
162  continue; \
163  }} \
164 
165 #define CASE_INT(str_,x_) \
166  if (0 == strncasecmp(s, str_, strlen(str_))) { \
167  if (1 != sscanf(s, str_"%d\n", &tempi)) { \
168  fprintf(stderr, "malformed " str_ " %s\n",s); \
169  return false; \
170  } else { \
171  x_ = tempi; \
172  continue; \
173  }} \
174 
175 #define CASE_FLT(str_,x_) \
176  if (0 == strncasecmp(s, str_, strlen(str_))) { \
177  if (1 != sscanf(s, str_"%f\n", &tempf)) { \
178  fprintf(stderr, "malformed " str_ " %s\n",s); \
179  return false; \
180  } else { \
181  x_ = tempf; \
182  continue; \
183  }} \
184 
185 #define CASE_PCB(str_) \
186  if (0 == strncasecmp(s, str_, strlen(str_))) { \
187  if (2 != sscanf(s, str_"%d %[^\n]", &tempi, tempP)) { \
188  fprintf(stderr, "malformed " str_ " %s\n",s); \
189  return false; \
190  } else \
191 
192 
193  CASE_STR("deviceName: ", info.name);
194  CASE_STR("buildDate: ", info.buildDate);
195  CASE_STR("serialNumber: ", info.serialNumber);
196  CASE_INT("hardwareRevision: ", info.hardwareRevision);
197  CASE_STR("imagerName: ", info.imagerName);
198  CASE_INT("imagerType: ", info.imagerType);
199  CASE_INT("imagerWidth: ", info.imagerWidth);
200  CASE_INT("imagerHeight: ", info.imagerHeight);
201  CASE_STR("lensName: ", info.lensName);
202  CASE_INT("lensType: ", info.lensType);
203  CASE_FLT("nominalBaseline: ", info.nominalBaseline);
204  CASE_FLT("nominalFocalLength: ", info.nominalFocalLength);
205  CASE_FLT("nominalRelativeAperture: ", info.nominalRelativeAperture);
206  CASE_INT("lightingType: ", info.lightingType);
207  CASE_INT("numberOfLights: ", info.numberOfLights);
208  CASE_STR("laserName: ", info.laserName);
209  CASE_INT("laserType: ", info.laserType);
210  CASE_STR("motorName: ", info.motorName);
211  CASE_INT("motorType: ", info.motorType);
212  CASE_FLT("motorGearReduction: ", info.motorGearReduction);
213  CASE_PCB("pcb: ") {
214  if (system::DeviceInfo::MAX_PCBS == info.pcbs.size()) {
215  fprintf(stderr, "no room for pcb: %s %d\n", tempP, tempi);
216  return false;
217  } else {
218  system::PcbInfo pcb;
219  pcb.name = std::string(tempP);
220  pcb.revision = tempi;
221  info.pcbs.push_back(pcb);
222  }
223  continue;
224  }}
225 
226  fprintf(stderr, "malformed line: \"%s\"\n", s);
227  return false;
228  }
229 
230  fclose(fP);
231  return true;
232 }
233 
234 }; // anonymous
235 
236 int main(int argc,
237  char **argvPP)
238 {
239  std::string ipAddress = "10.66.171.21";
240  std::string key;
241  std::string fileName;
242  bool query = false;
243  bool prompt = true;
244 
245  //
246  // Parse args
247 
248  int c;
249 
250  while(-1 != (c = getopt(argc, argvPP, "a:k:s:qy")))
251  switch(c) {
252  case 'a': ipAddress = std::string(optarg); break;
253  case 'k': key = std::string(optarg); break;
254  case 's': fileName = std::string(optarg); break;
255  case 'q': query = true; break;
256  case 'y': prompt = false; break;
257  default: usage(*argvPP); break;
258  }
259 
260  if (!fileName.empty() && key.empty()) {
261  fprintf(stderr,
262  "To program device info, please also specify the device's key with '-k'\n");
263  usage(*argvPP);
264  }
265 
266  if (fileName.empty())
267  query = true;
268 
269  //
270  // Initialize communications.
271 
272  Channel *channelP = Channel::Create(ipAddress);
273  if (NULL == channelP) {
274  fprintf(stderr, "Failed to establish communications with \"%s\"\n",
275  ipAddress.c_str());
276  return -1;
277  }
278 
279  //
280  // Query version
281 
282  Status status;
283  VersionType version;
284 
285  status = channelP->getSensorVersion(version);
286  if (Status_Ok != status) {
287  fprintf(stderr, "Failed to query sensor version: %s\n",
288  Channel::statusString(status));
289  goto clean_out;
290  }
291 
292  //
293  // Parse and send device info, if requested
294 
295  if (!fileName.empty()) {
296 
297  system::DeviceInfo info;
298 
299  if (false == parseFile(fileName, info))
300  goto clean_out;
301  else {
302 
303  if (prompt) {
304  printDeviceInfo(info);
305  fprintf(stdout, "\nReally update device information? (y/n): ");
306  fflush(stdout);
307  int c = getchar();
308 
309  if ('Y' != c && 'y' != c) {
310  fprintf(stdout, "Aborting\n");
311  goto clean_out;
312  }
313  }
314 
315  status = channelP->setDeviceInfo(key, info);
316  if (Status_Ok != status) {
317  fprintf(stderr, "Failed to set the device info: %s\n",
318  Channel::statusString(status));
319  goto clean_out;
320  } else
321  fprintf(stdout, "Device info updated successfully\n");
322  }
323  }
324 
325  //
326  // Query the current device information, if requested
327 
328  if (query) {
329 
330  system::DeviceInfo info;
331 
332  status = channelP->getDeviceInfo(info);
333  if (Status_Ok != status)
334  fprintf(stderr, "Failed to query device info: %s\n",
335  Channel::statusString(status));
336  else
337  printDeviceInfo(info);
338  }
339 
340 clean_out:
341 
342  Channel::Destroy(channelP);
343  return 0;
344 }
virtual Status setDeviceInfo(const std::string &key, const system::DeviceInfo &i)=0
static const char * statusString(Status status)
Definition: channel.cc:609
int main(int argc, char **argvPP)
static Channel * Create(const std::string &sensorAddress)
Definition: channel.cc:583
#define CASE_INT(str_, x_)
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:34
#define CASE_STR(str_, x_)
static CRL_CONSTEXPR uint32_t MAX_PCBS
static CRL_CONSTEXPR Status Status_Ok
#define CASE_PCB(str_)
def usage(progname)
virtual Status getDeviceInfo(system::DeviceInfo &info)=0
static void Destroy(Channel *instanceP)
Definition: channel.cc:596
char * optarg
Definition: getopt.c:32
#define CASE_FLT(str_, x_)
virtual Status getSensorVersion(VersionType &version)=0


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46