FlashUtility.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 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string>
52 
53 #include <fstream>
54 
56 
57 namespace { // anonymous
58 
59 void usage(const char *programNameP)
60 {
61  fprintf(stderr, "USAGE: %s <options>\n", programNameP);
62  fprintf(stderr, "Where <options> are:\n");
63  fprintf(stderr, "\t-a <ip_address> : IP address of device (default=10.66.171.21)\n");
64  fprintf(stderr, "\t-p : Perform flash operation\n");
65  fprintf(stderr, "\t-v : Perform verify operation\n");
66  fprintf(stderr, "\t-b <bitstream_file> : The bitstream (.bin) file\n");
67  fprintf(stderr, "\t-f <firmware_file> : The firmware (.srec) file\n");
68 }
69 
70 bool verifyFileWithExtension(const std::string& description,
71  const std::string& fileName,
72  const std::string& extension)
73 {
74  try {
75  std::ifstream file(fileName.c_str(),
76  std::ios::in | std::ios::binary);
77 
78  if (false == file.good()) {
79  fprintf(stderr, "Cannot open %s file \"%s\" for reading, aborting.\n",
80  description.c_str(), fileName.c_str());
81  return false;
82  }
83 
84  } catch (const std::exception& e) {
85  fprintf(stderr, "Exception accessing %s file \"%s\" for reading: %s.\n",
86  description.c_str(), fileName.c_str(), e.what());
87  return false;
88  }
89 
90  std::string::size_type idx = fileName.rfind('.');
91 
92  if (std::string::npos != idx &&
93  fileName.substr(idx+1) == extension)
94  return true;
95 
96  fprintf(stderr, "%s file \"%s\" is not a \".%s\" file, aborting.\n",
97  description.c_str(), fileName.c_str(), extension.c_str());
98 
99  return false;
100 }
101 
102 }; // anonymous
103 
104 using namespace crl::multisense;
105 
106 int main(int argc,
107  char **argvPP)
108 {
109  std::string ipAddress = "10.66.171.21";
110  bool programOp = false;
111  bool verifyOp = false;
112  int returnCode = 0;
113  std::string bitstreamFile;
114  std::string firmwareFile;
115 
116  //
117  // Parse args
118 
119  int c;
120 
121  while(-1 != (c = getopt(argc, argvPP, "a:epvb:f:")))
122  switch(c) {
123  case 'a': ipAddress = std::string(optarg); break;
124  case 'p': programOp = true; break;
125  case 'v': verifyOp = true; break;
126  case 'b': bitstreamFile = std::string(optarg); break;
127  case 'f': firmwareFile = std::string(optarg); break;
128  default: usage(*argvPP); exit(-1); break;
129  }
130 
131  //
132  // Verify that the bitstream/firmware filenames are sane, and that the files exist
133 
134  if (false == bitstreamFile.empty() &&
135  false == verifyFileWithExtension("Bitstream", bitstreamFile, "bin"))
136  exit(-2);
137  if (false == firmwareFile.empty() &&
138  false == verifyFileWithExtension("Firmware", firmwareFile, "srec"))
139  exit(-3);
140 
141  //
142  // Initialize communications.
143 
144  Channel *channelP = Channel::Create(ipAddress);
145  if (NULL == channelP) {
146  fprintf(stderr, "Failed to establish communications with \"%s\"\n",
147  ipAddress.c_str());
148  exit(-4);
149  }
150 
151  //
152  // Perform any programming operations first
153 
154  Status status=Status_Ok;
155 
156  if (programOp) {
157 
158  if (!bitstreamFile.empty()) {
159 
160  fprintf(stderr, "Programming bitstream: %s\n",
161  bitstreamFile.c_str());
162 
163  status = channelP->flashBitstream(bitstreamFile);
164  if (Status_Ok != status) {
165  fprintf(stderr, "Programming bitstream failed: %s\n",
166  Channel::statusString(status));
167  returnCode = -6;
168  goto clean_out;
169  }
170  }
171 
172  if (!firmwareFile.empty()) {
173 
174  fprintf(stderr, "Programming firmware: %s\n",
175  firmwareFile.c_str());
176 
177  status = channelP->flashFirmware(firmwareFile);
178  if (Status_Ok != status) {
179  fprintf(stderr, "Programming firmware failed: %s\n",
180  Channel::statusString(status));
181  returnCode = -7;
182  goto clean_out;
183  }
184  }
185  }
186 
187  //
188  // Perform any verification operations
189 
190  if (verifyOp) {
191 
192  if (!bitstreamFile.empty()) {
193 
194  fprintf(stderr, "Verifying bitstream: %s\n",
195  bitstreamFile.c_str());
196 
197  status = channelP->verifyBitstream(bitstreamFile);
198  if (Status_Ok != status) {
199  fprintf(stderr, "Verify bitstream failed: %s\n",
200  Channel::statusString(status));
201  returnCode = -8;
202  goto clean_out;
203  }
204  }
205 
206  if (!firmwareFile.empty()) {
207 
208  fprintf(stderr, "Verifying firmware: %s\n",
209  firmwareFile.c_str());
210 
211  status = channelP->verifyFirmware(firmwareFile);
212  if (Status_Ok != status) {
213  fprintf(stderr, "Verify firmware failed: %s\n",
214  Channel::statusString(status));
215  returnCode = -9;
216  goto clean_out;
217  }
218  }
219  }
220 
221 clean_out:
222 
223  Channel::Destroy(channelP);
224  return returnCode;
225 }
static const char * statusString(Status status)
Definition: channel.cc:609
virtual Status verifyFirmware(const std::string &file)=0
static Channel * Create(const std::string &sensorAddress)
Definition: channel.cc:583
int main(int argc, char **argvPP)
virtual Status verifyBitstream(const std::string &file)=0
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:34
static CRL_CONSTEXPR Status Status_Ok
def usage(progname)
virtual Status flashBitstream(const std::string &file)=0
static void Destroy(Channel *instanceP)
Definition: channel.cc:596
virtual Status flashFirmware(const std::string &file)=0
char * optarg
Definition: getopt.c:32


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