FlashUtility.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 #include <stdio.h>
00049 #include <stdlib.h>
00050 #include <string>
00051 #include <LibMultiSense/MultiSenseChannel.hh>
00052 
00053 #include <fstream>
00054 
00055 #include <Utilities/portability/getopt/getopt.h>
00056 
00057 namespace {  // anonymous
00058 
00059 void usage(const char *programNameP) 
00060 {
00061     fprintf(stderr, "USAGE: %s <options>\n", programNameP);
00062     fprintf(stderr, "Where <options> are:\n");
00063     fprintf(stderr, "\t-a <ip_address>        : IP address of device (default=10.66.171.21)\n");
00064     fprintf(stderr, "\t-p                     : Perform flash operation\n");
00065     fprintf(stderr, "\t-v                     : Perform verify operation\n");
00066     fprintf(stderr, "\t-b <bitstream_file>    : The bitstream (.bin) file\n");
00067     fprintf(stderr, "\t-f <firmware_file>     : The firmware (.srec) file\n");
00068 }
00069 
00070 bool verifyFileWithExtension(const std::string& description,
00071                              const std::string& fileName,
00072                              const std::string& extension)
00073 {
00074     try {
00075         std::ifstream file(fileName.c_str(),
00076                            std::ios::in | std::ios::binary);
00077 
00078         if (false == file.good()) {
00079             fprintf(stderr, "Cannot open %s file \"%s\" for reading, aborting.\n",
00080                     description.c_str(), fileName.c_str());
00081             return false;
00082         }
00083 
00084     } catch (const std::exception& e) {
00085         fprintf(stderr, "Exception accessing %s file \"%s\" for reading: %s.\n",
00086                 description.c_str(), fileName.c_str(), e.what());
00087         return false;
00088     }
00089 
00090     std::string::size_type idx = fileName.rfind('.');
00091 
00092     if (std::string::npos != idx &&
00093         fileName.substr(idx+1) == extension)
00094         return true;
00095 
00096     fprintf(stderr, "%s file \"%s\" is not a \".%s\" file, aborting.\n",
00097             description.c_str(), fileName.c_str(), extension.c_str());
00098 
00099     return false;
00100 }
00101 
00102 }; // anonymous
00103 
00104 using namespace crl::multisense;
00105 
00106 int main(int    argc, 
00107          char **argvPP)
00108 {
00109     std::string ipAddress  = "10.66.171.21";
00110     bool        programOp  = false;
00111     bool        verifyOp   = false;
00112     int         returnCode = 0;
00113     std::string bitstreamFile;
00114     std::string firmwareFile;
00115 
00116     //
00117     // Parse args
00118 
00119     int c;
00120 
00121     while(-1 != (c = getopt(argc, argvPP, "a:epvb:f:")))
00122         switch(c) {
00123         case 'a': ipAddress     = std::string(optarg);    break;
00124         case 'p': programOp     = true;                   break;
00125         case 'v': verifyOp      = true;                   break;
00126         case 'b': bitstreamFile = std::string(optarg);    break;
00127         case 'f': firmwareFile  = std::string(optarg);    break;
00128         default: usage(*argvPP); exit(-1);                break;
00129         }
00130 
00131     //
00132     // Verify that the bitstream/firmware filenames are sane, and that the files exist
00133 
00134     if (false == bitstreamFile.empty() && 
00135         false == verifyFileWithExtension("Bitstream", bitstreamFile, "bin"))
00136         exit(-2);
00137     if (false == firmwareFile.empty() && 
00138         false == verifyFileWithExtension("Firmware", firmwareFile, "srec"))
00139         exit(-3);
00140 
00141     //
00142     // Initialize communications.
00143 
00144     Channel *channelP = Channel::Create(ipAddress);
00145     if (NULL == channelP) {
00146         fprintf(stderr, "Failed to establish communications with \"%s\"\n",
00147                 ipAddress.c_str());
00148         exit(-4);
00149     }
00150     
00151     //
00152     // Perform any programming operations first
00153 
00154     Status status=Status_Ok;
00155 
00156     if (programOp) {
00157 
00158         if (!bitstreamFile.empty()) {
00159             
00160             fprintf(stderr, "Programming bitstream: %s\n",
00161                     bitstreamFile.c_str());
00162 
00163             status = channelP->flashBitstream(bitstreamFile); 
00164             if (Status_Ok != status) {
00165                 fprintf(stderr, "Programming bitstream failed: %s\n",
00166                         Channel::statusString(status));
00167                 returnCode = -6; 
00168                 goto clean_out;
00169             }
00170         }
00171 
00172         if (!firmwareFile.empty()) {
00173             
00174             fprintf(stderr, "Programming firmware: %s\n",
00175                     firmwareFile.c_str());
00176 
00177             status = channelP->flashFirmware(firmwareFile);
00178             if (Status_Ok != status) {
00179                 fprintf(stderr, "Programming firmware failed: %s\n",
00180                         Channel::statusString(status));
00181                 returnCode = -7;
00182                 goto clean_out;
00183             }
00184         }
00185     }
00186 
00187     //
00188     // Perform any verification operations
00189 
00190     if (verifyOp) {
00191 
00192         if (!bitstreamFile.empty()) {
00193             
00194             fprintf(stderr, "Verifying bitstream: %s\n",
00195                     bitstreamFile.c_str());
00196 
00197             status = channelP->verifyBitstream(bitstreamFile);
00198             if (Status_Ok != status) {
00199                 fprintf(stderr, "Verify bitstream failed: %s\n",
00200                         Channel::statusString(status));
00201                 returnCode = -8;
00202                 goto clean_out;
00203             }
00204         }
00205 
00206         if (!firmwareFile.empty()) {
00207             
00208             fprintf(stderr, "Verifying firmware: %s\n",
00209                     firmwareFile.c_str());
00210 
00211             status = channelP->verifyFirmware(firmwareFile);
00212             if (Status_Ok != status) {
00213                 fprintf(stderr, "Verify firmware failed: %s\n",
00214                         Channel::statusString(status));
00215                 returnCode = -9;
00216                 goto clean_out;
00217             }
00218         }
00219     }
00220 
00221 clean_out:
00222 
00223     Channel::Destroy(channelP);
00224     return returnCode;
00225 }


multisense_lib
Author(s):
autogenerated on Mon Oct 9 2017 03:06:21