DeviceInfoUtility.cc
Go to the documentation of this file.
00001 
00037 #ifdef WIN32
00038 #ifndef WIN32_LEAN_AND_MEAN
00039 #define WIN32_LEAN_AND_MEAN 1
00040 #endif
00041 
00042 #include <windows.h>
00043 #include <winsock2.h>
00044 #else
00045 #include <unistd.h>
00046 #endif
00047 
00048 
00049 #include <stdio.h>
00050 #include <stdlib.h>
00051 #include <errno.h>
00052 #include <string.h>
00053 
00054 #include <LibMultiSense/MultiSenseChannel.hh>
00055 
00056 #include <Utilities/portability/getopt/getopt.h>
00057 
00058 using namespace crl::multisense;
00059 
00060 namespace {  // anonymous
00061 
00062 void usage(const char *programNameP) 
00063 {
00064     fprintf(stderr, "USAGE: %s [<options>]\n", programNameP);
00065     fprintf(stderr, "Where <options> are:\n");
00066     fprintf(stderr, "\t-a <ip_address>    : ip address (default=10.66.171.21)\n");
00067     fprintf(stderr, "\t-k <passphrase>    : passphrase for setting device info\n");
00068     fprintf(stderr, "\t-s <file_name>     : set device info from file\n");
00069     fprintf(stderr, "\t-q                 : query device info (default)\n");
00070     fprintf(stderr, "\t-y                 : disable confirmation prompt\n");
00071     
00072     exit(-1);
00073 }
00074 
00075 //
00076 // Pass over any whitespace
00077 
00078 const char *skipSpace(const char *strP)
00079 {
00080     if (strP)
00081         while (isspace(*strP))
00082             strP++;
00083     return strP;
00084 }
00085 
00086 //
00087 // Dump device info contents to a file in format that can
00088 // be re-parsed by this utility
00089 
00090 void printDeviceInfo(const system::DeviceInfo& info,
00091                      FILE*                     fP=stdout)
00092 {
00093      fprintf(fP, "deviceName: %s\n", info.name.c_str());
00094      fprintf(fP, "buildDate: %s\n", info.buildDate.c_str());
00095      fprintf(fP, "serialNumber: %s\n", info.serialNumber.c_str());
00096      fprintf(fP, "hardwareRevision: %d\n\n", info.hardwareRevision);
00097           
00098      fprintf(fP, "imagerName: %s\n", info.imagerName.c_str());
00099      fprintf(fP, "imagerType: %d\n", info.imagerType);
00100      fprintf(fP, "imagerWidth: %d\n", info.imagerWidth);
00101      fprintf(fP, "imagerHeight: %d\n\n", info.imagerHeight);
00102      
00103      fprintf(fP, "lensName: %s\n", info.lensName.c_str());
00104      fprintf(fP, "lensType: %d\n", info.lensType);
00105      fprintf(fP, "nominalBaseline: %f\n", info.nominalBaseline);
00106      fprintf(fP, "nominalFocalLength: %f\n", info.nominalFocalLength);
00107      fprintf(fP, "nominalRelativeAperture: %f\n\n", info.nominalRelativeAperture);
00108      
00109      fprintf(fP, "lightingType: %d\n", info.lightingType);
00110      fprintf(fP, "numberOfLights: %d\n\n", info.numberOfLights);
00111      
00112      fprintf(fP, "laserName: %s\n", info.laserName.c_str());
00113      fprintf(fP, "laserType: %d\n\n", info.laserType);
00114      
00115      fprintf(fP, "motorName: %s\n", info.motorName.c_str());
00116      fprintf(fP, "motorType: %d\n", info.motorType);
00117      fprintf(fP, "motorGearReduction: %f\n\n", info.motorGearReduction);
00118 
00119      for(uint32_t i=0; i<info.pcbs.size(); i++)
00120          fprintf(fP, "pcb: %d %s\n", info.pcbs[i].revision,
00121                  info.pcbs[i].name.c_str());
00122 }
00123 
00124 //
00125 // Parse a file with device information
00126 
00127 bool parseFile(const std::string& fileName,
00128                system::DeviceInfo& info)
00129 {
00130     FILE *fP = fopen(fileName.c_str(), "r");
00131     if (NULL == fP) {
00132         fprintf(stderr, "fopen(\"%s\") failed: %s",
00133                 fileName.c_str(), strerror(errno));
00134         return false;
00135     }
00136 
00137     while (!feof(fP)) {
00138 
00139         char  lineP[512] = {0};
00140         char  tempP[512] = {0};
00141         int   tempi;
00142         float tempf;
00143 
00144         if (NULL == fgets(lineP, 512, fP))
00145             break;
00146 
00147         const char *s = skipSpace(lineP);
00148         if (!s || '#' == *s || '\0' == *s)
00149             continue;
00150 
00151 #ifdef WIN32
00152 #define strncasecmp _strnicmp
00153 #endif
00154 
00155 #define CASE_STR(str_,x_)                                               \
00156             if (0 == strncasecmp(s, str_, strlen(str_))) {              \
00157                 if (1 != sscanf(s, str_"%[^\n]", tempP)) {              \
00158                     fprintf(stderr, "malformed " str_ " %s\n",s);       \
00159                     return false;                                       \
00160                 } else {                                                \
00161                     x_ = std::string(tempP);                            \
00162                     continue;                                           \
00163                 }}                                                      \
00164             
00165 #define CASE_INT(str_,x_)                                               \
00166             if (0 == strncasecmp(s, str_, strlen(str_))) {              \
00167                 if (1 != sscanf(s, str_"%d\n", &tempi)) {               \
00168                     fprintf(stderr, "malformed " str_ " %s\n",s);       \
00169                     return false;                                       \
00170                 } else {                                                \
00171                     x_ = tempi;                                         \
00172                     continue;                                           \
00173                 }}                                                      \
00174             
00175 #define CASE_FLT(str_,x_)                                               \
00176             if (0 == strncasecmp(s, str_, strlen(str_))) {              \
00177                 if (1 != sscanf(s, str_"%f\n", &tempf)) {               \
00178                     fprintf(stderr, "malformed " str_ " %s\n",s);       \
00179                     return false;                                       \
00180                 } else {                                                \
00181                     x_ = tempf;                                         \
00182                     continue;                                           \
00183                 }}                                                      \
00184 
00185 #define CASE_PCB(str_)                                                  \
00186         if (0 == strncasecmp(s, str_, strlen(str_))) {                  \
00187             if (2 != sscanf(s, str_"%d %[^\n]", &tempi, tempP)) {       \
00188                 fprintf(stderr, "malformed " str_ " %s\n",s);           \
00189                 return false;                                           \
00190             } else                                                      \
00191                 
00192 
00193         CASE_STR("deviceName: ",              info.name);
00194         CASE_STR("buildDate: ",               info.buildDate);
00195         CASE_STR("serialNumber: ",            info.serialNumber);
00196         CASE_INT("hardwareRevision: ",        info.hardwareRevision);
00197         CASE_STR("imagerName: ",              info.imagerName);
00198         CASE_INT("imagerType: ",              info.imagerType);
00199         CASE_INT("imagerWidth: ",             info.imagerWidth);
00200         CASE_INT("imagerHeight: ",            info.imagerHeight);
00201         CASE_STR("lensName: ",                info.lensName);
00202         CASE_INT("lensType: ",                info.lensType);
00203         CASE_FLT("nominalBaseline: ",         info.nominalBaseline);
00204         CASE_FLT("nominalFocalLength: ",      info.nominalFocalLength);
00205         CASE_FLT("nominalRelativeAperture: ", info.nominalRelativeAperture);
00206         CASE_INT("lightingType: ",            info.lightingType);
00207         CASE_INT("numberOfLights: ",          info.numberOfLights);
00208         CASE_STR("laserName: ",               info.laserName);
00209         CASE_INT("laserType: ",               info.laserType);
00210         CASE_STR("motorName: ",               info.motorName);
00211         CASE_INT("motorType: ",               info.motorType);
00212         CASE_FLT("motorGearReduction: ",      info.motorGearReduction);
00213         CASE_PCB("pcb: ") {
00214             if (system::DeviceInfo::MAX_PCBS == info.pcbs.size()) {
00215                 fprintf(stderr, "no room for pcb: %s %d\n", tempP, tempi);
00216                 return false;
00217             } else {
00218                 system::PcbInfo pcb;
00219                 pcb.name     = std::string(tempP);
00220                 pcb.revision = tempi;
00221                 info.pcbs.push_back(pcb);
00222             }
00223             continue;
00224         }}
00225         
00226         fprintf(stderr, "malformed line: \"%s\"\n", s);
00227         return false;
00228     }
00229 
00230     fclose(fP);
00231     return true;
00232 }
00233 
00234 }; // anonymous
00235 
00236 int main(int    argc, 
00237          char **argvPP)
00238 {
00239     std::string ipAddress  = "10.66.171.21";
00240     std::string key;
00241     std::string fileName;
00242     bool        query  = false;
00243     bool        prompt = true;
00244 
00245     //
00246     // Parse args
00247 
00248     int c;
00249 
00250     while(-1 != (c = getopt(argc, argvPP, "a:k:s:qy")))
00251         switch(c) {
00252         case 'a': ipAddress = std::string(optarg);    break;
00253         case 'k': key       = std::string(optarg);    break;
00254         case 's': fileName  = std::string(optarg);    break;
00255         case 'q': query     = true;                   break;
00256         case 'y': prompt    = false;                  break;
00257         default: usage(*argvPP);                      break;
00258         }
00259 
00260     if (!fileName.empty() && key.empty()) {
00261         fprintf(stderr, 
00262                 "To program device info, please also specify the device's key with '-k'\n");
00263         usage(*argvPP);
00264     }
00265 
00266     if (fileName.empty())
00267         query = true;
00268 
00269     //
00270     // Initialize communications.
00271 
00272     Channel *channelP = Channel::Create(ipAddress);
00273     if (NULL == channelP) {
00274         fprintf(stderr, "Failed to establish communications with \"%s\"\n",
00275                 ipAddress.c_str());
00276         return -1;
00277     }
00278 
00279     //
00280     // Query version
00281 
00282     Status      status;
00283     VersionType version;
00284 
00285     status = channelP->getSensorVersion(version);
00286     if (Status_Ok != status) {
00287         fprintf(stderr, "Failed to query sensor version: %s\n", 
00288                 Channel::statusString(status));
00289         goto clean_out;
00290     }
00291 
00292     //
00293     // Parse and send device info, if requested
00294 
00295     if (!fileName.empty()) {
00296         
00297         system::DeviceInfo info;
00298         
00299         if (false == parseFile(fileName, info))
00300             goto clean_out;
00301         else {
00302 
00303             if (prompt) {
00304                 printDeviceInfo(info);
00305                 fprintf(stdout, "\nReally update device information? (y/n): ");
00306                 fflush(stdout);
00307                 int c = getchar();
00308                 
00309                 if ('Y' != c && 'y' != c) {
00310                     fprintf(stdout, "Aborting\n");
00311                     goto clean_out;
00312                 }
00313             }
00314 
00315             status = channelP->setDeviceInfo(key, info);
00316             if (Status_Ok != status) {
00317                 fprintf(stderr, "Failed to set the device info: %s\n", 
00318                         Channel::statusString(status));
00319                 goto clean_out;
00320             } else
00321                 fprintf(stdout, "Device info updated successfully\n");
00322         }
00323     }
00324 
00325     //
00326     // Query the current device information, if requested
00327     
00328     if (query) {
00329 
00330         system::DeviceInfo info;
00331 
00332         status = channelP->getDeviceInfo(info);
00333         if (Status_Ok != status)
00334             fprintf(stderr, "Failed to query device info: %s\n", 
00335                     Channel::statusString(status));
00336         else
00337             printDeviceInfo(info);
00338     }
00339 
00340 clean_out:
00341 
00342     Channel::Destroy(channelP);
00343     return 0;
00344 }


multisense_lib
Author(s):
autogenerated on Fri Apr 5 2019 02:28:24