iCub-main
firmwareupdatercore.cpp
Go to the documentation of this file.
1 #include "firmwareupdatercore.h"
2 #include <qdebug.h>
3 #include <EoUpdaterProtocol.h>
4 
5 FirmwareUpdaterCore *self = NULL;
6 
7 
8 static void updateProgressCallback(float fraction)
9 {
10  self->updateProgress(fraction);
11 }
12 
13 // 09/2020 davide.tome@iit.it - address and port configurable in firmwareupdater.ini
14 bool FirmwareUpdaterCore::isValidIpAddress(QString addr)
15 {
16  QStringList ipFields;
17 
18  ipFields = addr.split(".");
19  if(ipFields.count() == 4 && ipFields[3].contains(":"))
20  {
21  QString address_,port_;
22  int ipv0, ipv1, ipv2, ipv3;
23 
24  address_ = addr.split(":")[0];
25  port_ = ipFields[3].split(":")[1];
26 
27  QRegExp re("\\d*"); // a digit (\d), zero or more times (*)
28  if (!re.exactMatch(port_)) return false;
29  for(int k=0; k<4; k++)
30  {
31  if (!re.exactMatch(address_.split(".")[k])) return false;
32  }
33 
34  ipv0 = address_.split(".")[0].toInt();
35  ipv1 = address_.split(".")[1].toInt();
36  ipv2 = address_.split(".")[2].toInt();
37  ipv3 = address_.split(".")[3].toInt();
38 
39  if(ipv0 == 10 && 0 < ipv1 < 255 && 0 < ipv2 < 255 && 0 < ipv3 < 255 && 0 < port_.toInt() < 255)
40  {
41  hostIPaddress = EO_COMMON_IPV4ADDR(ipv0, ipv1, ipv2, ipv3);
42  return true;
43  }
44  else return false;
45  }
46  else return false;
47 }
48 
49 FirmwareUpdaterCore::FirmwareUpdaterCore(QObject *parent) : QObject(parent), mutex(QMutex::Recursive)
50 {
51  self = this;
52 }
53 
54 bool FirmwareUpdaterCore::init(Searchable& config, int port, QString address, int VerbositY)
55 {
56  verbosity = VerbositY;
57 
58  mutex.lock();
59 
60  setVerbosity(verbosity);
61 
62  Bottle sensorSetConfig=config.findGroup("DRIVERS").tail();
63 
64  for (int t=0; t<sensorSetConfig.size(); ++t){
65  yarp::os::Bottle sensorConfig(sensorSetConfig.get(t).toString());
66 
67  QString type = QString("%1").arg(sensorConfig.get(0).asString().c_str());
68  QString line = QString("%1").arg(sensorConfig.get(1).asString().c_str());
69  if(verbosity>0) qDebug() << type << "-" << line;
70 
71  bool ok;
72  int num = QString("%1").arg(line).toInt(&ok);
73  if(ok){
74  devices.append(QPair<QString,QVariant>(type,num));
75  }else{
76  devices.append(QPair<QString,QVariant>(type,line));
77  }
78 
79 
80  // 09/2020 davide.tome@iit.it - address and port configurable in firmwareupdater.ini
81  if(type == "ETH") {
82 
83  if(isValidIpAddress(line))
84  {
85  port = line.split(":")[1].toInt();
86  qDebug() << "IP address FOUND in .ini file, Using :" << line.split(":")[0];
87  qDebug() << "Port Number FOUND in .ini file, Using :" << line.split(":")[1];
88  }
89  else
90  {
91  int ipv0, ipv1, ipv2, ipv3;
92 
93  ipv0 = address.split(".")[0].toInt();
94  ipv1 = address.split(".")[1].toInt();
95  ipv2 = address.split(".")[2].toInt();
96  ipv3 = address.split(".")[3].toInt();
97 
98  hostIPaddress = EO_COMMON_IPV4ADDR(ipv0, ipv1, ipv2, ipv3);
99 
100  qDebug() << "Missing or invalid IP address found in .ini file (format is 10.0.X.Y:Z , 0 < X,Y < 255 , Z port number)";
101  qDebug() << "Skipped, using defaults:";
102  if(verbosity >= 1)
103  {
104  qInfo() << " - IP address:" << DEFAULT_IP_ADDRESS;
105  qInfo() << " - IP port: " << DEFAULT_IP_PORT;
106  }
107 
108  }
109  }
110 
111  }
112 
113  if(!gMNT.open(hostIPaddress, port))
114  {
115  if(verbosity>0) qDebug("Can't open socket, aborting.");
116  mutex.unlock();
117  return false;
118  }
119 
120  mutex.unlock();
121  return true;
122 }
123 
125 {
126  verbosity = verb;
127  bool lowers_are_verbose = (verb >= 1) ? true : false;
128  gMNT.verbose(lowers_are_verbose);
129  downloader.set_verbose(lowers_are_verbose);
130 
131  return true;
132 }
133 
135 {
136  mutex.lock();
137  QStringList names;
138  for(int i=0;i<devices.count();i++){
139  QPair<QString,QVariant> p = devices.at(i);
140  names.append(p.first);
141  }
142  mutex.unlock();
143 
144  return names;
145 }
146 
148 {
149  mutex.lock();
150  bool ret = gMNT.go2maintenance(EthMaintainer::ipv4OfAllSelected, true, 5, 1.0);;
151  mutex.unlock();
152  return ret;
153 }
154 
156 {
157  return gMNT.go2application(EthMaintainer::ipv4OfAllSelected, true, 10, true);
158 }
159 
161 {
163 }
164 
165 
166 
168 {
169  mutex.lock();
171  mutex.unlock();
172  return ret;
173 }
174 
175 QList<QPair<QString,QVariant> > FirmwareUpdaterCore::getDevices()
176 {
177  return devices;
178 }
179 
180 void FirmwareUpdaterCore::disconnectFrom(QString device, QString id)
181 {
182  // if(!device.isEmpty() && !id.isEmpty() && device.contains("ETH")){
183  // gMNT.boards_get().clear();
184  // yDebug() << "empty eth devices";
185  // qDebug() << "empty eth devices";
186  // }
187  // //connectTo(device,id);
188 }
189 
190 int FirmwareUpdaterCore::connectTo(QString device, QString id)
191 {
192  mutex.lock();
193  if(!device.isEmpty() && !id.isEmpty()){
194  if(device.contains("ETH")){
195  int num = gMNT.discover(true, 2, 1.0).size();
196  if(verbosity>0)
197  {
198  yDebug() << "FirmwareUpdaterCore::connectTo() has found " << num << " ETH boards";
199  }
200  mutex.unlock();
201  return num;
202  }else{
203  QString result;
204  QList<sBoard> s = getCanBoardsFromDriver(device,id.toInt(),&result,true);
205  mutex.unlock();
206  return s.count();
207  }
208  }
209  mutex.unlock();
210  return 0;
211 }
212 
214 {
215  for(int i=0;i<gMNT.boards_get().size();i++){
216  QString boardIp = QString("%1").arg(gMNT.boards_get()[i].getIPV4string().c_str());
217  if(boardIp == ip){
218  return gMNT.boards_get()[i].isInMaintenance();
219  }
220  }
221  return false;
222 }
223 
225 {
226  return gMNT.boards_get();
227 }
228 
229 void FirmwareUpdaterCore::setSelectedEthBoard(int index,bool selected)
230 {
231  mutex.lock();
232  if(gMNT.boards_get().size() > index){
233  gMNT.boards_get()[index].setSelected(selected);
234  }
235  mutex.unlock();
236  selectedEnded();
237 }
238 
239 void FirmwareUpdaterCore::setSelectedEthBoard(QString boardIp,bool selected)
240 {
241  mutex.lock();
242  for(int i=0;i<gMNT.boards_get().size();i++){
243  if(QString("%1").arg(gMNT.boards_get()[i].getIPV4string().c_str()) == boardIp){
244  gMNT.boards_get()[i].setSelected(selected);
245  break;
246  }
247  }
248  mutex.unlock();
249  selectedEnded();
250 }
251 
252 void FirmwareUpdaterCore::setSelectedCanBoards(QList <sBoard> selectedBoards,QString address, int deviceId)
253 {
254  mutex.lock();
255  QString res;
256  if(!address.isEmpty() && deviceId == -1){
257  getCanBoardsFromEth(address,&res);
258  }else{
259  getCanBoardsFromDriver(address,deviceId,&res);
260  }
261 
262  this->canBoards = selectedBoards;
263 
264  foreach (sBoard b, selectedBoards) {
265  for(int i=0;i<downloader.board_list_size;i++){
266  if(downloader.board_list[i].bus == b.bus &&
267  downloader.board_list[i].pid == b.pid){
268  downloader.board_list[i].selected = b.selected;
269  downloader.board_list[i].eeprom = b.eeprom;
270  }
271  }
272  }
273 
274  mutex.unlock();
275  selectedEnded();
276 }
277 
279 {
280 
281 }
282 
283 void FirmwareUpdaterCore::setSelectedCanBoard(int index,bool selected,QString address, int deviceId)
284 {
285  mutex.lock();
286 // if(!ethAddress.isEmpty()){
287 // if(currentAddress != ethAddress){
288 // if(downloader.connected){
289 // downloader.stopdriver();
290 // }
291 // QString res;
292 // getCanBoardsFromEth(ethAddress,&res);
293 // }
294 // }
295  QString res;
296  if(!address.isEmpty() && deviceId == -1){
297  getCanBoardsFromEth(address,&res);
298  }else{
299  getCanBoardsFromDriver(address,deviceId,&res);
300  }
301  downloader.board_list[index].selected=selected;
302  mutex.unlock();
303  selectedEnded();
304 }
305 
306 
307 boardInfo2_t FirmwareUpdaterCore::getMoreDetails(int boardNum,QString *infoString,eOipv4addr_t *address)
308 {
309  mutex.lock();
310  boardInfo2_t info = gMNT.boards_get()[boardNum].getInfo();
311  if(address){
312  *address = gMNT.boards_get()[boardNum].getIPV4();
313  if(infoString && info.protversion == 0){
314  *infoString = QString("%1").arg(gMNT.moreinformation(*address, false).c_str());
315  }
316  }
317  mutex.unlock();
318 
319  return info;
320 
321 }
322 
323 QString FirmwareUpdaterCore::getProcessFromUint(uint8_t id, bool isMultiCore)
324 {
325  switch (id) {
326  case uprot_proc_Loader:
327  return "eLoader";
328  case uprot_proc_Updater:
329  return "eUpdater";
330  case uprot_proc_Application00:
331  return isMultiCore ? "eApplication_core_0" : "eApplication";
332  case uprot_proc_Application01:
333  return "eApplication_core_1";
334  case uprot_proc_ApplPROGupdater:
335  return "eApplPROGupdater";
336  default:
337  return "None";
338  break;
339  }
340 }
341 
342 QList<sBoard> FirmwareUpdaterCore::getCanBoardsFromDriver(QString driver, int networkId, QString *retString, bool force)
343 {
344  mutex.lock();
345 
346  if(force){
347  downloader.stopdriver();
348  }else{
349  if(downloader.connected && (!currentAddress.isEmpty() || (currentDriver != driver || currentId != networkId ))){
350  downloader.stopdriver();
351  }
352  if(currentDriver == driver && currentId == networkId){
353  mutex.unlock();
354  return canBoards;
355  }
356  }
357 
358  canBoards.clear();
359  yarp::os::Property params;
360  QString networkType;
361  if(driver.contains("CFW2",Qt::CaseInsensitive)){
362  networkType="cfw2can";
363  } else if(driver.contains("ECAN",Qt::CaseInsensitive)){
364  networkType = "ecan";
365  } else if(driver.contains("PCAN",Qt::CaseInsensitive)){
366  networkType = "pcan";
367  } else if(driver.contains("SOCKET",Qt::CaseInsensitive)){
368  networkType="socketcan";
369  }
370  params.put("device", networkType.toLatin1().data());
371  params.put("canDeviceNum", networkId);
372  params.put("canTxQueue", 64);
373  params.put("canRxQueue", 64);
374  params.put("canTxTimeout", 2000);
375  params.put("canRxTimeout", 2000);
376 
377  //try to connect to the driver
378  int ret = downloader.initdriver(params, (verbosity>1) ? true : false);
379 
380  if (0 != ret){
381  if(-2 == ret){
382  if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromDriver(): Init ETH driver - The ETH board has just jumped to eUpdater\n Connect again";
383  *retString = "FirmwareUpdaterCore::getCanBoardsFromDriver(): Init ETH driver - The ETH board has just jumped to eUpdater\n Connect again";
384  // TODO DIALOG
385  } else {
386  if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromDriver(): Init driver failed - Hardware busy or not connected?!";
387  *retString = "Cannot init driver " + driver + "<" + QString::number(networkId) + "> ... HW is busy or not connected";
388  // TODO DIALOG
389  }
390  mutex.unlock();
391  return canBoards;
392  }
393 
394 
395  ret = downloader.initschede();
396 
397  if (ret == -1)
398  {
399  if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromDriver(): No answer received from CAN boards after a successful driver init.";
400  *retString = "No CAN boards found beneath " + driver + "<" + QString::number(networkId) + ">";
401  downloader.stopdriver();
402  currentAddress = "";
403  //not_connected_status();
404  mutex.unlock();
405  return canBoards;
406  }
407 
408  for(int i=0; i<downloader.board_list_size;i++){
409  canBoards.append(downloader.board_list[i]);
410  }
411  currentAddress = "";
412  currentDriver = driver;
413  currentId = networkId;
414 
415  //downloader.stopdriver();
416  mutex.unlock();
417  return canBoards;
418 
419 }
420 
421 QList<sBoard > FirmwareUpdaterCore::getCanBoardsFromEth(QString address, QString *retString, int canID, bool force)
422 {
423  mutex.lock();
424 
425  if(force){
426  downloader.stopdriver();
427  }else{
428  if(downloader.connected && address != currentAddress && !currentAddress.isEmpty() || (currentAddress.isEmpty() && !currentDriver.isEmpty())){
429  downloader.stopdriver();
430  }
431 
432  if(currentAddress == address){
433  mutex.unlock();
434  return canBoards;
435  }
436  }
437 
438 
439  canBoards.clear();
440  unsigned int remoteAddr;
441  unsigned int localAddr;
442 
443 
444 
445  if (!compile_ip_addresses(address.toLatin1().data(),&remoteAddr,&localAddr)){
446  if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromEth(): Init driver failed - Could not find network interface";
447  // TODO DIALOG
448  *retString = "Init driver failed - Could not find network interface";
449  address = "";
450  mutex.unlock();
451  return canBoards;
452  }
453 
454 
455  yarp::os::Property params;
456  params.put("device", "ETH");
457  params.put("local", int( localAddr));
458  params.put("remote",int(remoteAddr));
459  params.put("canid",canID);
460 
461 
462  //try to connect to the driver
463  int ret = downloader.initdriver(params, (verbosity>1) ? true : false);
464 
465  if (0 != ret){
466  if(-2 == ret){
467  if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromEth((): Init ETH driver - The ETH board has just jumped to eUpdater\n Connect again";
468  *retString = "FirmwareUpdaterCore::getCanBoardsFromEth((): Init ETH driver - The ETH board has just jumped to eUpdater\n Connect again";
469  // TODO DIALOG
470  } else {
471  if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromEth((): Init driver failed - Hardware busy or not connected?!";
472  *retString = "FirmwareUpdaterCore::getCanBoardsFromEth(): Init driver failed - Hardware busy or not connected?!";
473  // TODO DIALOG
474  }
475  mutex.unlock();
476  return canBoards;
477  }
478 
479 
480  ret = downloader.initschede();
481 
482  if (ret == -1)
483  {
484  if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromEth(): No CAN boards found beneath " << address << " after a successful driver init.";
485  *retString = "No CAN boards found beneath " + address;
486  downloader.stopdriver();
487  address = "";
488  //not_connected_status();
489  mutex.unlock();
490  return canBoards;
491  }
492 
493  for(int i=0; i<downloader.board_list_size;i++){
494  canBoards.append(downloader.board_list[i]);
495  }
496  currentAddress = address;
497 
498  //downloader.stopdriver();
499  mutex.unlock();
500  return canBoards;
501 
502 }
503 
504 
505 
506 bool FirmwareUpdaterCore::compile_ip_addresses(const char* addr,unsigned int *remoteAddr,unsigned int *localAddr)
507 {
508  ACE_UINT32 ip1,ip2,ip3,ip4;
509  sscanf(addr,"%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4);
510  *remoteAddr=(ip1<<24)|(ip2<<16)|(ip3<<8)|ip4;
511 
512  size_t count=0;
513  ACE_INET_Addr* addr_array=NULL;
514  int ret=ACE::get_ip_interfaces(count,addr_array);
515 
516  if (ret || count<=0)
517  {
518  mutex.unlock();
519  return false;
520  }
521 
522  *localAddr=addr_array[0].get_ip_address();
523 
524  for (unsigned int a=1; a<count; ++a)
525  {
526  if ((*remoteAddr & 0xFFFF0000)==(addr_array[a].get_ip_address() & 0xFFFF0000))
527  {
528  *localAddr=addr_array[a].get_ip_address();
529  break;
530  }
531  }
532 
533  return true;
534 }
535 
537 {
538  mutex.lock();
540  mutex.unlock();
541 }
542 
544 {
545  mutex.lock();
546  QString ret = QString("%1").arg(gMNT.boards_get()[index].getInfoOnEEPROM().c_str());
547  mutex.unlock();
548  return ret;
549 }
550 
552 {
553  mutex.lock();
554  char board_ipaddr[16];
555  ACE_UINT32 ip = ipv4toace(gMNT.boards_get()[index].getIPV4());
556  sprintf(board_ipaddr,"%d.%d.%d.%d",(ip>>24)&0xFF,(ip>>16)&0xFF,(ip>>8)&0xFF,ip&0xFF);
557  mutex.unlock();
558  return QString("%1").arg(board_ipaddr);
559 }
560 
561 bool FirmwareUpdaterCore::setEthBoardInfo(int index, QString newInfo)
562 {
563  mutex.lock();
564  eOipv4addr_t address = gMNT.boards_get()[index].getIPV4();
565  bool ret = gMNT.command_info32_set(address, newInfo.toLatin1().data());
566  if(!ret){
567  if(verbosity>0) qDebug() << "setEthBoardInfo failed";
568  }
569 
570 
571  vector<string> vv = gMNT.command_info32_get(address);
572  foreach (string v, vv) {
573  if(verbosity>0) qDebug() << v.c_str();
574  }
575  // TODO chiedere
576  // it already sets it internally to commandInfo32Get()
577  if(vv.size() > 0){
578  if(verbosity>0) qDebug() << gMNT.boards_get()[index].getInfoOnEEPROM().c_str();
579  }
580  mutex.unlock();
581  return true;
582 }
583 
584 void FirmwareUpdaterCore::setCanBoardInfo(int bus, int id, QString newInfo,QString address, int deviceId,QString *resultString)
585 {
586  mutex.lock();
587  QString res;
588  if(!address.isEmpty() && deviceId == -1){
589  getCanBoardsFromEth(address,&res);
590  }else{
591  getCanBoardsFromDriver(address,deviceId,&res);
592  }
593  downloader.change_board_info(bus, id, newInfo.toLatin1().data());
594 
595 // if(!ethAddress.isEmpty()){
596 // if(currentAddress != ethAddress){
597 // if(downloader.connected){
598 // downloader.stopdriver();
599 // }
600 // getCanBoardsFromEth(ethAddress,resultString);
601 // }
602 // downloader.change_board_info(bus, id, newInfo.toLatin1().data());
603 // }
604  mutex.unlock();
605 }
606 
607 bool FirmwareUpdaterCore::setCanBoardAddress(int bus, int id, int canType,QString newAddress,QString address,int deviceId,QString *resultString)
608 {
609  mutex.lock();
610  QString res;
611  if(!address.isEmpty() && deviceId == -1){
612  getCanBoardsFromEth(address,&res);
613  }else{
614  getCanBoardsFromDriver(address,deviceId,&res);
615  }
616 
617  int new_val = newAddress.toInt();
618  if (new_val <=0 || new_val> 15){
619  if(verbosity>0) qDebug() << "Error, new address out of range 0 - 15";
620  return false;
621  }
622 
623  if (new_val == id){
624  if(verbosity>0) qDebug() << "Error, same address set";
625  return false;
626  }
627 
628  downloader.change_card_address(bus, id, new_val,canType);
629 
630 // if(!ethAddress.isEmpty()){
631 // if(currentAddress != ethAddress){
632 // if(downloader.connected){
633 // downloader.stopdriver();
634 // }
635 // getCanBoardsFromEth(ethAddress,resultString);
636 // }
637 
638 // int new_val = newAddress.toInt();
639 // if (new_val <=0 || new_val> 15){
640 // if(verbosity>0) qDebug() << "Error, new address out of range 0 - 15";
641 // return false;
642 // }
643 
644 // if (new_val == id){
645 // if(verbosity>0) qDebug() << "Error, same address set";
646 // return false;
647 // }
648 
649 // downloader.change_card_address(bus, id, new_val,canType);
650 // }
651  mutex.unlock();
652 
653  return true;
654 }
655 
656 bool FirmwareUpdaterCore::setEthBoardAddress(int index, QString newAddress)
657 {
658 
659  mutex.lock();
660  int ip1,ip2,ip3,ip4;
661  sscanf(newAddress.toLatin1().data(),"%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4);
662  if (ip1<0 || ip1>255 || ip2<0 || ip2>255 || ip3<0 || ip3>255 || ip4<0 || ip4>255){
663  mutex.unlock();
664  return false;
665  }
666  ACE_UINT32 iNewAddress=(ip1<<24)|(ip2<<16)|(ip3<<8)|ip4;
667 
668 
669  ACE_UINT32 address = ipv4toace(gMNT.boards_get()[index].getIPV4());
670  ACE_UINT32 mask = 0xFFFFFF00;
671 
672  if(iNewAddress == (iNewAddress & mask)){ // checks new ip address is not a network address . For example x.y.z.w/24 x.y.z.0
673  if(verbosity>0) qDebug() << "Error Setting address";
674  mutex.unlock();
675  return false;
676  }
677 
678  if((~mask) == (iNewAddress & (~mask))){ // checks new ip address is not a broadcast address . For example x.y.z.w/24 x.y.z.255
679  if(verbosity>0) qDebug() << "Error Setting address";
680  mutex.unlock();
681  return false;
682  }
683 
684  if (iNewAddress == address){
685  if(verbosity>0) qDebug() << "Error, same address set";
686  mutex.unlock();
687  return false;
688  }
689  char old_addr[16];
690  sprintf(old_addr,"%d.%d.%d.%d",(address>>24)&0xFF,(address>>16)&0xFF,(address>>8)&0xFF,address&0xFF);
691 
692  bool ret = gMNT.command_changeaddress(acetoipv4(address), acetoipv4(iNewAddress), true, true, true, true);
693  mutex.unlock();
694  return ret;
695 
696 
697 
698 }
699 
700 
701 bool FirmwareUpdaterCore::uploadLoader(QString filename,QString *resultString)
702 {
703  mutex.lock();
704  FILE *programFile=fopen(filename.toLatin1().data(),"r");
705  if (!programFile){
706  //TODO ERROR
707  if(verbosity>0) qDebug() << "Error opening the selected file!";
708  mutex.unlock();
709  return false;
710  }
711  eOipv4addr_t ipv4 = 0; // all selected
712  eObrd_ethtype_t type = eobrd_ethtype_none;
713  eOversion_t ver;
714  ver.major = 0;
715  ver.minor = 0;
716  std::string result;
717  bool ok = gMNT.program(ipv4, type, eLoader, ver, programFile, false, updateProgressCallback, false);
718 
719  fclose(programFile);
720 
721  *resultString = QString("%1").arg(result.c_str());
722  if(ok){
723  mutex.unlock();
724  return true;
725  }
726  mutex.unlock();
727  return false;
728 }
729 
730 
731 bool FirmwareUpdaterCore::uploadUpdater(QString filename,QString *resultString)
732 {
733  mutex.lock();
734  FILE *programFile=fopen(filename.toLatin1().data(),"r");
735  if (!programFile){
736  //TODO ERROR
737  if(verbosity>0) qDebug() << "Error opening the selected file!";
738  mutex.unlock();
739  return false;
740  }
741  eOipv4addr_t ipv4 = 0; // all selected
742  eObrd_ethtype_t type = eobrd_ethtype_none;
743  eOversion_t ver;
744  ver.major = 0;
745  ver.minor = 0;
746  std::string result;
747  bool ok = gMNT.program(ipv4, type, eUpdater, ver, programFile, false, updateProgressCallback, false);
748 
749  fclose(programFile);
750 
751  *resultString = QString("%1").arg(result.c_str());
752  if(ok){
753  mutex.unlock();
754  return true;
755  }
756  mutex.unlock();
757  return false;
758 }
759 
760 
761 #ifdef SERIALMETHOD
762 bool FirmwareUpdaterCore::uploadCanApplication(QString filename,QString *resultString, QString ethAddress)
763 {
764  mutex.lock();
765  if(!ethAddress.isEmpty()){
766  if(currentAddress != ethAddress){
767  if(downloader.connected){
768  downloader.stopdriver();
769  }
770  getCanBoardsFromEth(ethAddress,resultString);
771  }
772 
773  }
774  double timer_start =0;
775  double timer_end =0;
776 
777  if (downloader.connected == false){
778  *resultString ="Driver not running";
779  mutex.unlock();
780  return false;
781  }
782 
783  //check if at least one board was selected
784  bool at_least_one_board_selected = false;
785 
786  for (int i=0; i<downloader.board_list_size; i++){
787  if (downloader.board_list[i].status==BOARD_RUNNING &&
788  downloader.board_list[i].selected==true)
789  at_least_one_board_selected = true;
790  }
791 
792  if (!at_least_one_board_selected){
793  *resultString = "No Boards selected! - Select one or more boards to update the firmware";
794  mutex.unlock();
795  return false;
796  }
797 
798  QMap<int,int> canDevices;
799  for (int i=0; i<downloader.board_list_size; i++)
800  {
801  if (downloader.board_list[i].selected==true)
802  {
803  canDevices.insertMulti(downloader.board_list[i].bus,i);
804  downloader.board_list[i].selected = false;
805  if(verbosity>0) qDebug() << "FOUND SELECTED SCHEDA " << i << " ON BUS " << downloader.board_list[i].bus;
806  }
807  }
808 
809  int ret = 0;
810  int finished = 0;
811  int busCount = canDevices.uniqueKeys().count();
812  for(int k=0;k<busCount;k++){
813 
814  int bus = canDevices.uniqueKeys().at(k);
815 
816  if (downloader.open_file(filename.toLatin1().data())!=0){
817  *resultString = "Error opening the selected file!";
818  mutex.unlock();
819  return false;
820  }
821  if(verbosity>0) qDebug() << "FILE " << filename << " OPENED";
822  //TODO
823  //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
824  // if (strstr (buffer, "calibrationDataSN") != 0)
825  // {
826  // load_calibration (buffer);
827  // return ALL_OK;
828  // }
829  //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
830 
831  // Get an identification of the firmware fot the file that you have selected
832  // int firmware_board_type=0;
833  // int firmware_version=0;
834  // int firmware_revision=0;
835 
836  //indentify download type from the type of the selected boards
837  int download_type = icubCanProto_boardType__unknown;
838  bool download_eeprom =false;
839 
840 
841  QList<int> indexes = canDevices.values(bus);
842 // foreach (int index, indexes) {
843 // downloader.board_list[index].selected = true;
844 // if(verbosity>0) qDebug() << "SELECTING BOARD " << index << " OF BUS " << k;
845 // }
846 
847 
848  for(int i=0;i<indexes.count();i++){
849  int index = indexes.at(i);
850  downloader.board_list[index].selected = true;
851  if(verbosity>0) qDebug() << "SELECTING BOARD " << index << " OF BUS " << bus;
852  if (downloader.board_list[index].status==BOARD_RUNNING){
853  if (downloader.startscheda(bus,
854  downloader.board_list[index].pid,
855  downloader.board_list[index].eeprom,
856  downloader.board_list[index].type)!=0){
857  *resultString = "Unable to start the board - Unable to send message 'start' or no answer received";
858  mutex.unlock();
859  return false;
860  } else {
861  if(verbosity>0) qDebug() << "START SCHEDA " << index << " ON BUS " << bus << " OK";
862  downloader.board_list[index].status=BOARD_WAITING;
863  }
864  download_type = downloader.board_list[index].type;
865  download_eeprom = downloader.board_list[index].eeprom;
866  }
867  }
868 
869 
870 
871 // // Start the download for the selected boards
872 // for (int i=0; i<downloader.board_list_size; i++){
873 // if (downloader.board_list[i].status==BOARD_RUNNING && downloader.board_list[i].selected==true){
874 // if (downloader.startscheda(downloader.board_list[i].bus,
875 // downloader.board_list[i].pid,
876 // downloader.board_list[i].eeprom,
877 // downloader.board_list[i].type)!=0){
878 // *resultString = "Unable to start the board - Unable to send message 'start' or no answer received";
879 // return false;
880 // } else {
881 // if(verbosity>0) qDebug() << "START SCHEDA " << i << " OK";
882 // downloader.board_list[i].status=BOARD_WAITING;
883 // }
884 // download_type = downloader.board_list[i].type;
885 // download_eeprom = downloader.board_list[i].eeprom;
886 // }
887 // }
888 
889 
890 
891  timer_start= yarp::os::Time::now();
892  finished = 0;
893  bool print00 = false, print25 = false, print50 = false, print75 = false, print99 = false;
894  // Start the download for the selected boards
895  do
896  {
897  ret = downloader.download_file(bus, 0x0F, download_type,download_eeprom);
898  if (float(downloader.progress)/downloader.file_length/busCount >0.0 && print00==false) {if(verbosity>0) qDebug("downloading %s, 1%% done\n",filename.toLatin1().data()); print00=true;}
899  if (float(downloader.progress)/downloader.file_length/busCount >0.25 && print25==false) {if(verbosity>0) qDebug("downloading %s, 25%% done\n",filename.toLatin1().data()); print25=true;}
900  if (float(downloader.progress)/downloader.file_length/busCount >0.50 && print50==false) {if(verbosity>0) qDebug("downloading %s, 50%% done\n",filename.toLatin1().data()); print50=true;}
901  if (float(downloader.progress)/downloader.file_length/busCount >0.75 && print75==false) {if(verbosity>0) qDebug("downloading %s, 75%% done\n",filename.toLatin1().data()); print75=true;}
902  if (float(downloader.progress)/downloader.file_length/busCount >0.99 && print99==false) {if(verbosity>0) qDebug("downloading %s, finished!\n",filename.toLatin1().data()); print99=true;}
903 
904  if (ret==1){
905  updateProgress(float(downloader.progress)/downloader.file_length/busCount);
906  }
907  if (ret==-1){
908  if(verbosity>0) qDebug() << "Fatal Error during download, terminate";
909  *resultString = "Fatal Error during download, terminate";
910  finished = 1;
911  }
912  if (ret==0){
913  if(verbosity>0) qDebug() << "Download terminated";
914  *resultString = "Download terminated";
915  finished = 1;
916  }
917 
918  }
919  while (finished!=1);
920 
921  // End the download for the selected boards
922 
923  if(downloader.stopscheda(bus, 15) != 0){
924  if(verbosity>0) qDebug() << "ERROR STOPPING SCHEDA";
925  }else{
926  if(verbosity>0) qDebug() << "scheda stopped";
927  }
928 
929  foreach (int index, indexes) {
930  downloader.board_list[index].selected = false;
931  if(verbosity>0) qDebug() << "DE-SELECTING BOARD " << index << " OF BUS " << bus;
932  }
933 
934 
935 
936  }
937  timer_end= yarp::os::Time::now();
938 
939 
940 
941 
942  //Display result message
943  if (ret == 0)
944  {
945  char time_text [50];
946  double download_time = (timer_end-timer_start) ;
947  sprintf (time_text, "All Board OK! Download Time (s): %.2f", download_time);
948 
949  *resultString = QString("Download Finished. %1").arg(time_text);
950 
951  updateProgress(1.0);
952  mutex.unlock();
953  return true;
954  }
955  else
956  {
957  //*resultString = "Error during file transfer";
958 
959  updateProgress(1.0);
960  mutex.unlock();
961  return false;
962  }
963 
964  mutex.unlock();
965  return true;
966 }
967 
968 #else
969 bool FirmwareUpdaterCore::uploadCanApplication(QString filename,QString *resultString, bool ee, QString address,int deviceId,QList <sBoard> *resultCanBoards)
970 {
971 // if(!address.isEmpty()){
972 // if(currentAddress != address){
973 // if(downloader.connected){
974 // downloader.stopdriver();
975 // }
976 // getCanBoardsFromEth(address,resultString);
977 // }
978 
979 // }
980  QString res;
981  if(!address.isEmpty() && deviceId == -1){
982  getCanBoardsFromEth(address,&res);
983  }else{
984  getCanBoardsFromDriver(address,deviceId,&res);
985  }
986 
987 
988  double timer_start =0;
989  double timer_end =0;
990 
991  if (downloader.connected == false){
992  *resultString ="Driver not running";
993  return false;
994  }
995 
996  //check if at least one board was selected
997  bool at_least_one_board_selected = false;
998  int i = 0;
999 
1000  for (i=0; i<downloader.board_list_size; i++){
1001  if (downloader.board_list[i].status==BOARD_RUNNING &&
1002  downloader.board_list[i].selected==true)
1003  at_least_one_board_selected = true;
1004  }
1005 
1006  if (!at_least_one_board_selected){
1007  *resultString = "No Boards selected! - Select one or more boards to update the firmware";
1008  return false;
1009  }
1010  if (downloader.open_file(filename.toLatin1().data())!=0){
1011  *resultString = "Error opening the selected file!";
1012  return false;
1013  }
1014  //TODO
1015 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1016  // if (strstr (buffer, "calibrationDataSN") != 0)
1017  // {
1018  // load_calibration (buffer);
1019  // return ALL_OK;
1020  // }
1021 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1022 
1023  // Get an identification of the firmware fot the file that you have selected
1024  int firmware_board_type=0;
1025  int firmware_version=0;
1026  int firmware_revision=0;
1027 
1028  //indentify download type from the type of the selected boards
1029  int download_type = icubCanProto_boardType__unknown;
1030  bool download_eeprom =false;
1031  for (i=0; i<downloader.board_list_size; i++)
1032  {
1033  if (downloader.board_list[i].selected==true)
1034  {
1035  download_type = downloader.board_list[i].type;
1036  download_eeprom = downloader.board_list[i].eeprom;
1037  }
1038 
1039  }
1040  download_eeprom = ee;
1041 
1042  // Start the download for the selected boards
1043  for (i=0; i<downloader.board_list_size; i++){
1044  if (downloader.board_list[i].status==BOARD_RUNNING && downloader.board_list[i].selected==true){
1045  //bool EE = downloader.board_list[i].eeprom;
1046  bool EE = ee;
1047  if (downloader.startscheda(downloader.board_list[i].bus, downloader.board_list[i].pid, EE, downloader.board_list[i].type)!=0){
1048  *resultString = "Unable to start the board - Unable to send message 'start' or no answer received";
1049  return false;
1050  } else {
1051  downloader.board_list[i].status=BOARD_WAITING;
1052  }
1053  }
1054  }
1055 
1056  int ret = 0;
1057  int finished = 0;
1058 
1059  timer_start= yarp::os::Time::now();
1060 
1061 // bool acemortest_notyetstopped = true;
1062 // const float acemortest_progress = 0.10;
1063 
1064  bool print00 = false, print25 = false, print50 = false, print75 = false, print99 = false;
1065  // Start the download for the selected boards
1066  do
1067  {
1068  ret = downloader.download_file(CanPacket::everyCANbus, 0x0F, download_type,download_eeprom);
1069  if (float(downloader.progress)/downloader.file_length >0.0 && print00==false) {if(verbosity>0) qDebug("programming %s: 1%% done",filename.toLatin1().data()); print00=true;}
1070  if (float(downloader.progress)/downloader.file_length >0.25 && print25==false) {if(verbosity>0) qDebug("programming %s: 25%% done",filename.toLatin1().data()); print25=true;}
1071  if (float(downloader.progress)/downloader.file_length >0.50 && print50==false) {if(verbosity>0) qDebug("programming %s: 50%% done",filename.toLatin1().data()); print50=true;}
1072  if (float(downloader.progress)/downloader.file_length >0.75 && print75==false) {if(verbosity>0) qDebug("programming %s: 75%% done",filename.toLatin1().data()); print75=true;}
1073  if (float(downloader.progress)/downloader.file_length >0.99 && print99==false) {if(verbosity>0) qDebug("programming %s: finished!",filename.toLatin1().data()); print99=true;}
1074 
1075 // if ((float(downloader.progress)/downloader.file_length > acemortest_progress) && (acemortest_notyetstopped))
1076 // {
1077 // // place you breakpoint in here.
1078 // acemortest_notyetstopped = false;
1079 // }
1080 
1081  if (ret==1)
1082  {
1083  updateProgress(float(downloader.progress)/downloader.file_length);
1084  }
1085  if (ret==-1)
1086  {
1087  *resultString = "Fatal Error during download, terminate";
1088  finished = 1;
1089  }
1090  if (ret==0)
1091  {
1092  *resultString = "Download terminated";
1093  finished = 1;
1094  }
1095 
1096  }
1097  while (finished!=1);
1098  timer_end= yarp::os::Time::now();
1099 
1100  // End the download for the selected boards
1101  int errors =0;
1102  downloader.stopscheda(CanPacket::everyCANbus, 15);
1103 
1104 
1105 
1106  yarp::os::Time::delay(3.0);
1107  //Display result message
1108  if (ret == 0)
1109  {
1110  char time_text [50];
1111  double download_time = (timer_end-timer_start) ;
1112  sprintf (time_text, "All Board OK! Download Time (s): %.2f", download_time);
1113 
1114  *resultString = QString("Download Finished. %1").arg(time_text);
1115 
1116  updateProgress(1.0);
1117  downloader.initschede();
1118 
1119  canBoards.clear();
1120  for(int i=0; i<downloader.board_list_size;i++){
1121  canBoards.append(downloader.board_list[i]);
1122  }
1123  if(resultCanBoards){
1124  *resultCanBoards = canBoards;
1125  }
1126  return true;
1127  }
1128  else
1129  {
1130  //*resultString = "Error during file transfer";
1131 
1132  updateProgress(1.0);
1133  downloader.initschede();
1134  return false;
1135  }
1136  downloader.initschede();
1137 
1138  canBoards.clear();
1139  for(int i=0; i<downloader.board_list_size;i++){
1140  canBoards.append(downloader.board_list[i]);
1141  }
1142  *resultCanBoards = canBoards;
1143  return true;
1144 }
1145 #endif
1146 
1147 bool FirmwareUpdaterCore::uploadEthApplication(QString filename,QString *resultString)
1148 {
1149  mutex.lock();
1150  FILE *programFile=fopen(filename.toLatin1().data(),"r");
1151 // if(verbosity>0) qDebug() << "attempting opening the selected file:" << filename << "or .." << filename.toLatin1().data();
1152  if (!programFile){
1153  //TODO ERROR
1154  if(verbosity>0) qDebug() << "Error opening the selected file:" << filename << "or .." << filename.toLatin1().data();
1155  mutex.unlock();
1156  return false;
1157  }
1158  eOipv4addr_t ipv4 = 0; // all selected
1159  eObrd_ethtype_t type = eobrd_ethtype_none;
1160  eOversion_t ver;
1161  ver.major = 0;
1162  ver.minor = 0;
1163  std::string result;
1164  bool ok = gMNT.program(ipv4, type, eApplication, ver, programFile, false, updateProgressCallback, false);
1165 
1166  fclose(programFile);
1167 
1168  *resultString = QString("%1").arg(result.c_str());
1169  if(ok){
1170  mutex.unlock();
1171  return true;
1172  }
1173  mutex.unlock();
1174  return false;
1175 }
1176 
1177 
1178 
1180 {
1181  return &downloader;
1182 }
1183 
1184 
1185 
1187 {
1188  mutex.lock();
1190  mutex.unlock();
1191 }
1192 
1193 
1195 {
1196  mutex.lock();
1197  gMNT.command_def2run(EthMaintainer::ipv4OfAllSelected, eApplication, false, false);
1198  mutex.unlock();
1199 }
1200 
1202 {
1203  mutex.lock();
1204  gMNT.command_def2run(EthMaintainer::ipv4OfAllSelected, eUpdater, false, false);
1205  mutex.unlock();
1206 }
eOipv4addr_t acetoipv4(ACE_UINT32 address)
ACE_UINT32 ipv4toace(eOipv4addr_t ipv4)
@ everyCANbus
Definition: driver.h:51
std::string moreinformation(eOipv4addr_t ipv4, bool forcemaintenance=false)
bool command_jump2updater(eOipv4addr_t ipv4)
EthBoardList discover(bool clearbeforediscovery=true, int numberofdiscoveries=1, double waittimeout=1.0)
static const eOipv4addr_t ipv4OfAllSelected
Definition: EthMaintainer.h:59
bool open(eOipv4addr_t ipv4=hostIPaddress, eOipv4port_t port=mainIPport)
bool command_def2run(eOipv4addr_t ipv4, eOuprot_process_t process, bool forcemaintenance=true, bool verify=true)
bool go2application(eOipv4addr_t ipv4, bool checkdef2runapplication=true, double bootstraptime=10.0, bool verify=true)
EthBoardList & boards_get(void)
void verbose(bool on)
bool go2maintenance(eOipv4addr_t ipv4, bool verify=true, int retries=6, double timegap=1.0)
bool command_info32_set(eOipv4addr_t ipv4, const string &info32)
vector< string > command_info32_get(eOipv4addr_t ipv4)
bool command_blink(eOipv4addr_t ipv4)
bool program(eOipv4addr_t ipv4, eObrd_ethtype_t type, eOuprot_process_t process, eOversion_t targetversion, FILE *fp, bool forcemaintenance=true, void progress(float)=NULL, bool restart2application=true)
bool command_eeprom_erase(eOipv4addr_t ipv4)
bool command_restart(eOipv4addr_t ipv4)
bool command_changeaddress(eOipv4addr_t ipv4, eOipv4addr_t ipv4new, bool checkifnewispresent=true, bool forcemaintenance=true, bool restart=false, bool verify=false)
QList< sBoard > getCanBoardsFromDriver(QString driver, int networkId, QString *retString, bool force=false)
int connectTo(QString device, QString id)
void setSelectedCanBoards(QList< sBoard > selectedBoards, QString address, int deviceId=-1)
bool setEthBoardAddress(int index, QString newAddress)
bool uploadCanApplication(QString filename, QString *resultString, bool ee, QString address="", int deviceId=-1, QList< sBoard > *resultCanBoards=NULL)
QString getProcessFromUint(uint8_t id, bool isMultiCore=false)
QString getEthBoardInfo(int index)
QList< sBoard > getCanBoardsFromEth(QString address, QString *retString, int canID=CanPacket::everyCANbus, bool force=false)
void setSelectedEthBoard(int index, bool selected)
void disconnectFrom(QString device, QString id)
FirmwareUpdaterCore(QObject *parent=0)
void setSelectedCanBoard(int index, bool selected, QString ethAddress="", int deviceId=-1)
QString getEthBoardAddress(int index)
boardInfo2_t getMoreDetails(int boardNum=EthMaintainer::ipv4OfAllSelected, QString *infoString=NULL, eOipv4addr_t *address=NULL)
EthBoardList getEthBoardList()
QList< QPair< QString, QVariant > > getDevices()
void updateProgress(float)
bool setEthBoardInfo(int index, QString newInfo)
bool uploadUpdater(QString filename, QString *resultString)
bool isBoardInMaintenanceMode(QString ip)
void setCanBoardInfo(int bus, int id, QString newInfo, QString ethAddress="", int deviceId=-1, QString *resultString=NULL)
bool setCanBoardAddress(int bus, int id, int canType, QString newAddress, QString ethAddress="", int deviceId=-1, QString *resultString=NULL)
bool uploadEthApplication(QString filename, QString *resultString)
bool uploadLoader(QString filename, QString *resultString)
bool init(Searchable &config, int port, QString address, int VerbositY)
int open_file(std::string file)
int change_card_address(int bus, int target_id, int new_id, int board_type)
int startscheda(int bus, int board_pid, bool board_eeprom, int download_type)
int stopscheda(int bus, int board_pid)
void set_verbose(bool verbose)
Definition: downloader.cpp:98
int change_board_info(int bus, int target_id, char *board_info)
int file_length
Definition: downloader.h:153
int initdriver(yarp::os::Searchable &config, bool verbose=true)
Definition: downloader.cpp:142
sBoard * board_list
Definition: downloader.h:150
int stopdriver()
Definition: downloader.cpp:122
int board_list_size
Definition: downloader.h:151
int initschede()
int download_file(int bus, int board_pid, int download_type, bool eeprom)
bool connected
Definition: downloader.h:158
#define BOARD_RUNNING
Definition: downloader.h:45
#define BOARD_WAITING
Definition: downloader.h:46
static void updateProgressCallback(float fraction)
#define DEFAULT_IP_ADDRESS
#define DEFAULT_IP_PORT
int canID
Definition: main.cpp:152
std::string networkType
Definition: main.cpp:156
unsigned int remoteAddr
Definition: main.cpp:154
unsigned int localAddr
Definition: main.cpp:153
int networkId
Definition: main.cpp:151
fclose(fileID)
bool eeprom
Definition: downloader.h:39
int bus
Definition: downloader.h:25
int type
Definition: downloader.h:27
int pid
Definition: downloader.h:26
int status
Definition: downloader.h:37
bool selected
Definition: downloader.h:38