Legacy/DeviceInfoUtility/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 
54 #include <MultiSense/MultiSenseChannel.hh>
55 
56 #include <getopt/getopt.h>
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-r : remote head channel (options: VPB, 0, 1, 2, 3)\n");
70  fprintf(stderr, "\t-q : query device info (default)\n");
71  fprintf(stderr, "\t-y : disable confirmation prompt\n");
72 
73  exit(-1);
74 }
75 
76 static int getRemoteHeadIdFromString(const std::string &head_str, RemoteHeadChannel & rh)
77 {
78  int err = 0;
79 
80  if (head_str == "VPB")
81  rh = Remote_Head_VPB;
82  else if (head_str == "0")
83  rh = Remote_Head_0;
84  else if (head_str == "1")
85  rh = Remote_Head_1;
86  else if (head_str == "2")
87  rh = Remote_Head_2;
88  else if (head_str == "3")
89  rh = Remote_Head_3;
90  else {
91  fprintf(stderr, "Error: Unrecognized remote head\n");
92  fprintf(stderr, "Please use one of the following:\n");
93  fprintf(stderr, "\tVPB\n");
94  fprintf(stderr, "\t0'\n");
95  fprintf(stderr, "\t1\n");
96  fprintf(stderr, "\t2\n");
97  fprintf(stderr, "\t3\n");
98  err = -1;
99  }
100 
101  return err;
102 }
103 
104 //
105 // Pass over any whitespace
106 
107 const char *skipSpace(const char *strP)
108 {
109  if (strP)
110  while (isspace(*strP))
111  strP++;
112  return strP;
113 }
114 
115 //
116 // Dump device info contents to a file in format that can
117 // be re-parsed by this utility
118 
119 void printDeviceInfo(const system::DeviceInfo& info,
120  FILE* fP=stdout)
121 {
122  fprintf(fP, "deviceName: %s\n", info.name.c_str());
123  fprintf(fP, "buildDate: %s\n", info.buildDate.c_str());
124  fprintf(fP, "serialNumber: %s\n", info.serialNumber.c_str());
125  fprintf(fP, "hardwareRevision: %d\n\n", info.hardwareRevision);
126 
127  fprintf(fP, "imagerName: %s\n", info.imagerName.c_str());
128  fprintf(fP, "imagerType: %d\n", info.imagerType);
129  fprintf(fP, "imagerWidth: %d\n", info.imagerWidth);
130  fprintf(fP, "imagerHeight: %d\n\n", info.imagerHeight);
131 
132  fprintf(fP, "lensName: %s\n", info.lensName.c_str());
133  fprintf(fP, "lensType: %d\n", info.lensType);
134  fprintf(fP, "nominalBaseline: %f\n", info.nominalBaseline);
135  fprintf(fP, "nominalFocalLength: %f\n", info.nominalFocalLength);
136  fprintf(fP, "nominalRelativeAperture: %f\n\n", info.nominalRelativeAperture);
137 
138  fprintf(fP, "lightingType: %d\n", info.lightingType);
139  fprintf(fP, "numberOfLights: %d\n\n", info.numberOfLights);
140 
141  fprintf(fP, "laserName: %s\n", info.laserName.c_str());
142  fprintf(fP, "laserType: %d\n\n", info.laserType);
143 
144  fprintf(fP, "motorName: %s\n", info.motorName.c_str());
145  fprintf(fP, "motorType: %d\n", info.motorType);
146  fprintf(fP, "motorGearReduction: %f\n\n", info.motorGearReduction);
147 
148  for(uint32_t i=0; i<info.pcbs.size(); i++)
149  fprintf(fP, "pcb: %d %s\n", info.pcbs[i].revision,
150  info.pcbs[i].name.c_str());
151 }
152 
153 //
154 // Parse a file with device information
155 
156 bool parseFile(const std::string& fileName,
157  system::DeviceInfo& info)
158 {
159  FILE *fP = fopen(fileName.c_str(), "r");
160  if (NULL == fP) {
161  fprintf(stderr, "fopen(\"%s\") failed: %s",
162  fileName.c_str(), strerror(errno));
163  return false;
164  }
165 
166  while (!feof(fP)) {
167 
168  char lineP[513] = {0};
169  char tempP[513] = {0};
170  int tempi;
171  float tempf;
172 
173  if (NULL == fgets(lineP, 512, fP))
174  break;
175 
176  const char *s = skipSpace(lineP);
177  if (!s || '#' == *s || '\0' == *s)
178  continue;
179 
180 #ifdef WIN32
181 #define strncasecmp _strnicmp
182 #endif
183 
184 #define CASE_STR(str_,x_) \
185  if (0 == strncasecmp(s, str_, strlen(str_))) { \
186  if (1 != sscanf(s, str_"%512[^\n]", tempP)) { \
187  fprintf(stderr, "malformed " str_ " %s\n",s); \
188  return false; \
189  } else { \
190  x_ = std::string(tempP); \
191  continue; \
192  }} \
193 
194 #define CASE_INT(str_,x_) \
195  if (0 == strncasecmp(s, str_, strlen(str_))) { \
196  if (1 != sscanf(s, str_"%d\n", &tempi)) { \
197  fprintf(stderr, "malformed " str_ " %s\n",s); \
198  return false; \
199  } else { \
200  x_ = tempi; \
201  continue; \
202  }} \
203 
204 #define CASE_FLT(str_,x_) \
205  if (0 == strncasecmp(s, str_, strlen(str_))) { \
206  if (1 != sscanf(s, str_"%f\n", &tempf)) { \
207  fprintf(stderr, "malformed " str_ " %s\n",s); \
208  return false; \
209  } else { \
210  x_ = tempf; \
211  continue; \
212  }} \
213 
214 #define CASE_PCB(str_) \
215  if (0 == strncasecmp(s, str_, strlen(str_))) { \
216  if (2 != sscanf(s, str_"%d %512[^\n]", &tempi, tempP)) { \
217  fprintf(stderr, "malformed " str_ " %s\n",s); \
218  return false; \
219  } else \
220 
221 
222  CASE_STR("deviceName: ", info.name);
223  CASE_STR("buildDate: ", info.buildDate);
224  CASE_STR("serialNumber: ", info.serialNumber);
225  CASE_INT("hardwareRevision: ", info.hardwareRevision);
226  CASE_STR("imagerName: ", info.imagerName);
227  CASE_INT("imagerType: ", info.imagerType);
228  CASE_INT("imagerWidth: ", info.imagerWidth);
229  CASE_INT("imagerHeight: ", info.imagerHeight);
230  CASE_STR("lensName: ", info.lensName);
231  CASE_INT("lensType: ", info.lensType);
232  CASE_FLT("nominalBaseline: ", info.nominalBaseline);
233  CASE_FLT("nominalFocalLength: ", info.nominalFocalLength);
234  CASE_FLT("nominalRelativeAperture: ", info.nominalRelativeAperture);
235  CASE_INT("lightingType: ", info.lightingType);
236  CASE_INT("numberOfLights: ", info.numberOfLights);
237  CASE_STR("laserName: ", info.laserName);
238  CASE_INT("laserType: ", info.laserType);
239  CASE_STR("motorName: ", info.motorName);
240  CASE_INT("motorType: ", info.motorType);
241  CASE_FLT("motorGearReduction: ", info.motorGearReduction);
242  CASE_PCB("pcb: ") {
243  if (system::DeviceInfo::MAX_PCBS == info.pcbs.size()) {
244  fprintf(stderr, "no room for pcb: %s %d\n", tempP, tempi);
245  return false;
246  } else {
247  system::PcbInfo pcb;
248  pcb.name = std::string(tempP);
249  pcb.revision = tempi;
250  info.pcbs.push_back(pcb);
251  }
252  continue;
253  }}
254 
255  fprintf(stderr, "malformed line: \"%s\"\n", s);
256  return false;
257  }
258 
259  fclose(fP);
260  return true;
261 }
262 
263 } // anonymous
264 
265 int main(int argc,
266  char **argvPP)
267 {
268  std::string ipAddress = "10.66.171.21";
269  std::string key;
270  std::string fileName;
271  std::string remoteHeadChannelId;
272  bool cameraRemoteHead=false;
273  bool query = false;
274  bool prompt = true;
275 
276  //
277  // Parse args
278 
279  int c;
280 
281  while(-1 != (c = getopt(argc, argvPP, "a:k:s:r:qy")))
282  switch(c) {
283  case 'a': ipAddress = std::string(optarg); break;
284  case 'k': key = std::string(optarg); break;
285  case 's': fileName = std::string(optarg); break;
286  case 'r': {
287  remoteHeadChannelId = std::string(optarg);
288  cameraRemoteHead = true;
289  break;
290  }
291  case 'q': query = true; break;
292  case 'y': prompt = false; break;
293  default: usage(*argvPP); break;
294  }
295 
296  if (!fileName.empty() && key.empty()) {
297  fprintf(stderr,
298  "To program device info, please also specify the device's key with '-k'\n");
299  usage(*argvPP);
300  }
301 
302  if (fileName.empty())
303  query = true;
304 
305  //
306  // Initialize communications.
307 
308  Channel *channelP = NULL;
309  if (cameraRemoteHead) {
310  RemoteHeadChannel rch;
311  if (getRemoteHeadIdFromString(remoteHeadChannelId, rch) < 0) {
312  return -1;
313  }
314  channelP = Channel::Create(ipAddress, rch);
315  }
316  else {
317  channelP = Channel::Create(ipAddress);
318  }
319 
320  if (NULL == channelP) {
321  fprintf(stderr, "Failed to establish communications with \"%s\"\n",
322  ipAddress.c_str());
323  return -1;
324  }
325 
326  //
327  // Query version
328 
329  Status status;
330  VersionType version;
331 
332  status = channelP->getSensorVersion(version);
333  if (Status_Ok != status) {
334  fprintf(stderr, "Failed to query sensor version: %s\n",
335  Channel::statusString(status));
336  goto clean_out;
337  }
338 
339  //
340  // Parse and send device info, if requested
341 
342  if (!fileName.empty()) {
343 
344  system::DeviceInfo info;
345 
346  if (false == parseFile(fileName, info))
347  goto clean_out;
348  else {
349 
350  if (prompt) {
351  printDeviceInfo(info);
352  fprintf(stdout, "\nReally update device information? (y/n): ");
353  fflush(stdout);
354  int reply = getchar();
355 
356  if ('Y' != reply && 'y' != reply) {
357  fprintf(stdout, "Aborting\n");
358  goto clean_out;
359  }
360  }
361 
362  status = channelP->setDeviceInfo(key, info);
363  if (Status_Ok != status) {
364  fprintf(stderr, "Failed to set the device info: %s\n",
365  Channel::statusString(status));
366  goto clean_out;
367  } else
368  fprintf(stdout, "Device info updated successfully\n");
369  }
370  }
371 
372  //
373  // Query the current device information, if requested
374 
375  if (query) {
376 
377  system::DeviceInfo info;
378 
379  status = channelP->getDeviceInfo(info);
380  if (Status_Ok != status)
381  fprintf(stderr, "Failed to query device info: %s\n",
382  Channel::statusString(status));
383  else
384  printDeviceInfo(info);
385  }
386 
387 clean_out:
388 
389  Channel::Destroy(channelP);
390  return 0;
391 }
CASE_STR
#define CASE_STR(str_, x_)
usage
static void usage()
Definition: FirmwareUpdateUtility.cc:51
crl::multisense::system::DeviceInfo::name
std::string name
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3291
CASE_PCB
#define CASE_PCB(str_)
crl::multisense::Status_Ok
static CRL_CONSTEXPR Status Status_Ok
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:99
crl::multisense::system::DeviceInfo::laserType
uint32_t laserType
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3330
crl::multisense::Channel::getDeviceInfo
virtual Status getDeviceInfo(system::DeviceInfo &info)=0
crl::multisense::system::DeviceInfo::pcbs
std::vector< PcbInfo > pcbs
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3300
crl::multisense::system::DeviceInfo::lensName
std::string lensName
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3312
crl::multisense::Remote_Head_0
static CRL_CONSTEXPR RemoteHeadChannel Remote_Head_0
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:214
crl::multisense::VersionType
uint32_t VersionType
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:88
getopt.h
getopt
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:31
crl::multisense::system::DeviceInfo::lightingType
uint32_t lightingType
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3323
main
int main(int argc, char **argvPP)
Definition: Legacy/DeviceInfoUtility/DeviceInfoUtility.cc:265
crl::multisense::Remote_Head_VPB
static CRL_CONSTEXPR RemoteHeadChannel Remote_Head_VPB
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:212
crl::multisense::system::DeviceInfo::nominalBaseline
float nominalBaseline
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3316
crl::multisense::Channel::getSensorVersion
virtual Status getSensorVersion(VersionType &version)=0
crl::multisense::Remote_Head_3
static CRL_CONSTEXPR RemoteHeadChannel Remote_Head_3
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:220
crl::multisense::Remote_Head_1
static CRL_CONSTEXPR RemoteHeadChannel Remote_Head_1
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:216
crl::multisense::system::DeviceInfo::imagerWidth
uint32_t imagerWidth
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3307
crl::multisense::system::PcbInfo::revision
uint32_t revision
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3200
crl::multisense::system::DeviceInfo::serialNumber
std::string serialNumber
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3295
crl::multisense::system::DeviceInfo
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3245
CASE_INT
#define CASE_INT(str_, x_)
crl::multisense::system::DeviceInfo::hardwareRevision
uint32_t hardwareRevision
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3297
CASE_FLT
#define CASE_FLT(str_, x_)
crl::multisense::system::DeviceInfo::buildDate
std::string buildDate
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3293
crl::multisense::system::DeviceInfo::laserName
std::string laserName
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3328
crl::multisense::system::PcbInfo::name
std::string name
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3198
crl::multisense::system::DeviceInfo::motorType
uint32_t motorType
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3335
crl::multisense::Remote_Head_2
static CRL_CONSTEXPR RemoteHeadChannel Remote_Head_2
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:218
crl::multisense::system::DeviceInfo::motorName
std::string motorName
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3333
crl::multisense::Status
int32_t Status
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:94
crl::multisense::Channel
Definition: Legacy/include/MultiSense/MultiSenseChannel.hh:69
crl::multisense::system::DeviceInfo::lensType
uint32_t lensType
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3314
crl::multisense::system::DeviceInfo::nominalFocalLength
float nominalFocalLength
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3318
crl::multisense::system::DeviceInfo::numberOfLights
uint32_t numberOfLights
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3325
crl::multisense
Definition: Legacy/details/channel.cc:62
crl::multisense::system::DeviceInfo::imagerName
std::string imagerName
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3303
crl::multisense::system::DeviceInfo::MAX_PCBS
static CRL_CONSTEXPR uint8_t MAX_PCBS
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3249
crl::multisense::system::DeviceInfo::nominalRelativeAperture
float nominalRelativeAperture
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3320
crl::multisense::system::DeviceInfo::imagerType
uint32_t imagerType
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3305
crl::multisense::system::DeviceInfo::motorGearReduction
float motorGearReduction
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3337
crl::multisense::Channel::setDeviceInfo
virtual Status setDeviceInfo(const std::string &key, const system::DeviceInfo &i)=0
optarg
char * optarg
Definition: getopt.c:29
crl::multisense::system::PcbInfo
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3194
crl::multisense::system::DeviceInfo::imagerHeight
uint32_t imagerHeight
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:3309
crl::multisense::RemoteHeadChannel
int16_t RemoteHeadChannel
Definition: Legacy/include/MultiSense/MultiSenseTypes.hh:210


multisense_lib
Author(s):
autogenerated on Thu Apr 17 2025 02:49:08