FirmwareUpdateUtility.cc
Go to the documentation of this file.
1 
37 #include <signal.h>
38 #include <limits.h>
39 #include <iostream>
40 #include <stdio.h>
41 #include <chrono>
42 #include <thread>
43 #include <atomic>
44 
45 #include <getopt/getopt.h>
46 
47 #include "Messages.hh"
48 #include "Updater.hh"
49 #include "Util.hh"
50 
51 static void usage() {
52  std::cout << "Usage\n";
53  std::cout << "\n";
54  std::cout << "FirmwareUpdateUtility\n";
55  std::cout << "\n";
56  std::cout << "FirmwareUpdateUtility <options>\n";
57  std::cout << "-a [address] Ip Address of camera (Default 10.66.171.21)\n";
58  std::cout << "-f [filepath] Path of firmware update (Default update.img)\n";
59  std::cout << "-v Print verbose file upload status (default false)\n";
60  std::cout << "-h Print this menu and exit.\n\n";
61 }
62 
63 static void signal_handler(int signal);
64 std::atomic<int> ExitReceived(0);
65 
66 #ifndef PATH_MAX
67 #define PATH_MAX MAX_PATH
68 #endif
69 
70 
71 int main(int argc, char *argv[]) {
72 
73  bool verbose_progress = false;
74  std::string ipAddress = "10.66.171.21";
75  std::string filePath;
76  bool UpdateComplete = false;
77  int ret = 0;
78  int opt;
79 
80  while ((opt = getopt(argc, argv, "a:f:vh")) != -1) {
81  switch (opt) {
82  case 'a':
83  ipAddress = std::string(optarg);
84  break;
85  case 'f':
86  filePath = std::string(optarg);
87  break;
88  case 'v':
89  verbose_progress = true;
90  break;
91  case 'h':
92  default: /* '?' */
93  usage();
94  exit(1);
95  }
96  }
97 
98  signal(SIGINT, signal_handler);
99 
100  if (ipAddress.empty()) {
101  std::cerr << "Missing IP address\n";
102  exit(-1);
103  }
104 
105  if (filePath.empty())
106  {
107  std::cerr << "Please specify a file path\n";
108  exit(-1);
109  }
110 
111  if (!Util::FileExists(filePath.c_str())) {
112  std::cerr << "Error: File " << filePath << " Does not exist\n";
113  usage();
114  return -1;
115  }
116 
117  Ip i;
118  if (i.Setup(ipAddress.c_str()) < 0) {
119  std::cerr << "Error Failed to setup Network\n";
120  return -1;
121  }
122 
123  if (i.Bind() < 0) {
124  std::cerr << "Error Failed to bind to camera server\n";
125  return -1;
126  }
127 
128  Updater u(&i);
129 
130  if (u.SendFile(filePath, verbose_progress) < 0) {
131  std::cerr << "Error failed to send the file " << filePath << " to the camera!\n";
132  return -1;
133  }
134 
135  while (!UpdateComplete || !ExitReceived)
136  {
137  Messages::MessageUpdateStatus UpdateStatus;
138  long int MsgLen = 0;
139 
140  ret = u.Receive((uint8_t *)&UpdateStatus, sizeof(UpdateStatus), &MsgLen);
141  if (ret < 0)
142  {
143  if (ret == -CRL_EAGAIN)
144  {
145  usleep(100);
146  continue;
147  }
148  std::cerr << "Error: Failed to get message from camera\n";
149  return -1;
150  }
151  else if (ret == 0)
152  {
153  std::cerr << "Error: Connection closed by camera\n";
154  return -1;
155  }
156 
157  if (UpdateStatus.State & Messages::Status_Error)
158  {
159  std::cerr << "Error: Received an error from Camera\n";
160  }
161  else
162  {
163  Messages::PrintStatus(UpdateStatus.State);
164  if (UpdateStatus.State & Messages::Status_UpdateComplete)
165  {
166  std::cout << "Notice: Update is complete, camera will power cycle!\n";
167  UpdateStatus.Id = Messages::UpdateStatusId_ACK;
168  UpdateStatus.Status = Messages::UpdateStatus_Done;
169  ret = u.Send((uint8_t*)&UpdateStatus, sizeof(UpdateStatus));
170  break;
171  }
172  else
173  {
174  UpdateStatus.Id = Messages::UpdateStatusId_ACK;
175  UpdateStatus.Status = Messages::UpdateStatus_Ok;
176  ret = u.Send((uint8_t *)&UpdateStatus, sizeof(UpdateStatus));
177  if (ret < 0)
178  {
179  std::cerr << "Error Failed to send ack to camera\n";
180  }
181 
182  }
183 
184  }
185  }
186 
187  return ret;
188 }
189 
190 static void signal_handler(int signal)
191 {
192  (void) signal;
193  std::cout << "Exit Signal Received Cancelling Update\n";
194  ExitReceived = 1;
195 }
usage
static void usage()
Definition: FirmwareUpdateUtility.cc:51
Util.hh
Updater::SendFile
int SendFile(std::string &FilePath, bool verbose)
Definition: Updater.cc:115
Updater::Receive
int Receive(uint8_t *buf, const size_t len, long int *rxlen)
Definition: Updater.cc:45
Util::FileExists
bool FileExists(const char *FilePath)
Definition: Util.hh:49
Ip
Definition: Ip.hh:75
main
int main(int argc, char *argv[])
Definition: FirmwareUpdateUtility.cc:71
getopt.h
getopt
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:31
Messages::PrintStatus
void PrintStatus(StatusMessage s)
Definition: Messages.hh:119
Messages::MessageUpdateStatus
Definition: Messages.hh:189
Updater::Send
int Send(uint8_t *buf, const size_t len)
Definition: Updater.cc:82
Ip::Setup
int Setup(std::string _IpAddress)
Definition: Ip.cc:113
Messages.hh
Updater
Definition: Updater.hh:45
CRL_EAGAIN
#define CRL_EAGAIN
Definition: Legacy/include/MultiSense/details/utility/Portability.hh:102
Messages::Status_UpdateComplete
static CRL_CONSTEXPR StatusMessage Status_UpdateComplete
Definition: Messages.hh:85
Messages::UpdateStatus_Done
static CRL_CONSTEXPR UpdateStatusMessage UpdateStatus_Done
Definition: Messages.hh:135
Messages::Status_Error
static CRL_CONSTEXPR StatusMessage Status_Error
Definition: Messages.hh:88
ExitReceived
std::atomic< int > ExitReceived(0)
Messages::UpdateStatusId_ACK
static CRL_CONSTEXPR UpdateStatusId UpdateStatusId_ACK
Definition: Messages.hh:139
Updater.hh
signal_handler
static void signal_handler(int signal)
Definition: FirmwareUpdateUtility.cc:190
optarg
char * optarg
Definition: getopt.c:29
Messages::UpdateStatus_Ok
static CRL_CONSTEXPR UpdateStatusMessage UpdateStatus_Ok
Definition: Messages.hh:134
Ip::Bind
int Bind()
Definition: Ip.cc:43


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