iCub-main
Loading...
Searching...
No Matches
firmwareupdatercore.cpp
Go to the documentation of this file.
2#include <qdebug.h>
3#include <EoUpdaterProtocol.h>
4
6
7
8static 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
14bool 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
49FirmwareUpdaterCore::FirmwareUpdaterCore(QObject *parent) : QObject(parent), mutex(QMutex::Recursive)
50{
51 self = this;
52}
53
54bool 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
159
164
165
166
168{
169 mutex.lock();
171 mutex.unlock();
172 return ret;
173}
174
175QList<QPair<QString,QVariant> > FirmwareUpdaterCore::getDevices()
176{
177 return devices;
178}
179
180void 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
190int 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
228
229void 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();
237}
238
239void 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();
250}
251
252void 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();
276}
277
282
283void 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();
304}
305
306
307boardInfo2_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
323QString FirmwareUpdaterCore::getProcessFromUint(uint8_t id, bool isMultiCore, eObrd_ethtype_t boardtype)
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 if ((boardtype == eobrd_ethtype_amc) && isMultiCore)
332 return "eApplication_core_0";
333 else if ((boardtype == eobrd_ethtype_amcfoc) && isMultiCore)
334 return "app.yri" ;
335 else
336 return "eApplication";
337 case uprot_proc_Application01:
338 if ((boardtype == eobrd_ethtype_amc) && isMultiCore)
339 return "eApplication_core_1";
340 else if ((boardtype == eobrd_ethtype_amcfoc) && isMultiCore)
341 return "app.mot" ;
342 else
343 return "eApplication_core_1";
344 case uprot_proc_ApplPROGupdater:
345 return "eApplPROGupdater";
346 default:
347 return "None";
348 break;
349 }
350}
351
352QList<sBoard> FirmwareUpdaterCore::getCanBoardsFromDriver(QString driver, int networkId, QString *retString, bool force)
353{
354 mutex.lock();
355
356 if(force){
357 downloader.stopdriver();
358 }else{
359 if(downloader.connected && (!currentAddress.isEmpty() || (currentDriver != driver || currentId != networkId ))){
360 downloader.stopdriver();
361 }
362 if(currentDriver == driver && currentId == networkId){
363 mutex.unlock();
364 return canBoards;
365 }
366 }
367
368 canBoards.clear();
369 yarp::os::Property params;
370 QString networkType;
371 if(driver.contains("CFW2",Qt::CaseInsensitive)){
372 networkType="cfw2can";
373 } else if(driver.contains("ECAN",Qt::CaseInsensitive)){
374 networkType = "ecan";
375 } else if(driver.contains("PCAN",Qt::CaseInsensitive)){
376 networkType = "pcan";
377 } else if(driver.contains("SOCKET",Qt::CaseInsensitive)){
378 networkType="socketcan";
379 }
380 params.put("device", networkType.toLatin1().data());
381 params.put("canDeviceNum", networkId);
382 params.put("canTxQueue", 64);
383 params.put("canRxQueue", 64);
384 params.put("canTxTimeout", 2000);
385 params.put("canRxTimeout", 2000);
386
387 //try to connect to the driver
388 int ret = downloader.initdriver(params, (verbosity>1) ? true : false);
389
390 if (0 != ret){
391 if(-2 == ret){
392 if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromDriver(): Init ETH driver - The ETH board has just jumped to eUpdater\n Connect again";
393 *retString = "FirmwareUpdaterCore::getCanBoardsFromDriver(): Init ETH driver - The ETH board has just jumped to eUpdater\n Connect again";
394 // TODO DIALOG
395 } else {
396 if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromDriver(): Init driver failed - Hardware busy or not connected?!";
397 *retString = "Cannot init driver " + driver + "<" + QString::number(networkId) + "> ... HW is busy or not connected";
398 // TODO DIALOG
399 }
400 mutex.unlock();
401 return canBoards;
402 }
403
404
405 ret = downloader.initschede();
406
407 if (ret == -1)
408 {
409 if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromDriver(): No answer received from CAN boards after a successful driver init.";
410 *retString = "No CAN boards found beneath " + driver + "<" + QString::number(networkId) + ">";
411 downloader.stopdriver();
412 currentAddress = "";
413 //not_connected_status();
414 mutex.unlock();
415 return canBoards;
416 }
417
418 for(int i=0; i<downloader.board_list_size;i++){
419 canBoards.append(downloader.board_list[i]);
420 }
421 currentAddress = "";
422 currentDriver = driver;
423 currentId = networkId;
424
425 //downloader.stopdriver();
426 mutex.unlock();
427 return canBoards;
428
429}
430
431QList<sBoard > FirmwareUpdaterCore::getCanBoardsFromEth(QString address, QString *retString, int canID, bool force)
432{
433 mutex.lock();
434
435 if(force){
436 downloader.stopdriver();
437 }else{
438 if(downloader.connected && address != currentAddress && !currentAddress.isEmpty() || (currentAddress.isEmpty() && !currentDriver.isEmpty())){
439 downloader.stopdriver();
440 }
441
442 if(currentAddress == address){
443 mutex.unlock();
444 return canBoards;
445 }
446 }
447
448
449 canBoards.clear();
450 unsigned int remoteAddr;
451 unsigned int localAddr;
452
453
454
455 if (!compile_ip_addresses(address.toLatin1().data(),&remoteAddr,&localAddr)){
456 if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromEth(): Init driver failed - Could not find network interface";
457 // TODO DIALOG
458 *retString = "Init driver failed - Could not find network interface";
459 address = "";
460 mutex.unlock();
461 return canBoards;
462 }
463
464
465 yarp::os::Property params;
466 params.put("device", "ETH");
467 params.put("local", int( localAddr));
468 params.put("remote",int(remoteAddr));
469 params.put("canid",canID);
470
471
472 //try to connect to the driver
473 int ret = downloader.initdriver(params, (verbosity>1) ? true : false);
474
475 if (0 != ret){
476 if(-2 == ret){
477 if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromEth((): Init ETH driver - The ETH board has just jumped to eUpdater\n Connect again";
478 *retString = "FirmwareUpdaterCore::getCanBoardsFromEth((): Init ETH driver - The ETH board has just jumped to eUpdater\n Connect again";
479 // TODO DIALOG
480 } else {
481 if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromEth((): Init driver failed - Hardware busy or not connected?!";
482 *retString = "FirmwareUpdaterCore::getCanBoardsFromEth(): Init driver failed - Hardware busy or not connected?!";
483 // TODO DIALOG
484 }
485 mutex.unlock();
486 return canBoards;
487 }
488
489
490 ret = downloader.initschede();
491
492 if (ret == -1)
493 {
494 if(verbosity>0) qDebug() << "FirmwareUpdaterCore::getCanBoardsFromEth(): No CAN boards found beneath " << address << " after a successful driver init.";
495 *retString = "No CAN boards found beneath " + address;
496 downloader.stopdriver();
497 address = "";
498 //not_connected_status();
499 mutex.unlock();
500 return canBoards;
501 }
502
503 for(int i=0; i<downloader.board_list_size;i++){
504 canBoards.append(downloader.board_list[i]);
505 }
506 currentAddress = address;
507
508 //downloader.stopdriver();
509 mutex.unlock();
510 return canBoards;
511
512}
513
514
515
516bool FirmwareUpdaterCore::compile_ip_addresses(const char* addr,unsigned int *remoteAddr,unsigned int *localAddr)
517{
518 ACE_UINT32 ip1,ip2,ip3,ip4;
519 sscanf(addr,"%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4);
520 *remoteAddr=(ip1<<24)|(ip2<<16)|(ip3<<8)|ip4;
521
522 size_t count=0;
523 ACE_INET_Addr* addr_array=NULL;
524 int ret=ACE::get_ip_interfaces(count,addr_array);
525
526 if (ret || count<=0)
527 {
528 mutex.unlock();
529 return false;
530 }
531
532 *localAddr=addr_array[0].get_ip_address();
533
534 for (unsigned int a=1; a<count; ++a)
535 {
536 if ((*remoteAddr & 0xFFFF0000)==(addr_array[a].get_ip_address() & 0xFFFF0000))
537 {
538 *localAddr=addr_array[a].get_ip_address();
539 break;
540 }
541 }
542
543 return true;
544}
545
547{
548 mutex.lock();
550 mutex.unlock();
551}
552
554{
555 mutex.lock();
556 QString ret = QString("%1").arg(gMNT.boards_get()[index].getInfoOnEEPROM().c_str());
557 mutex.unlock();
558 return ret;
559}
560
562{
563 mutex.lock();
564 char board_ipaddr[16];
565 ACE_UINT32 ip = ipv4toace(gMNT.boards_get()[index].getIPV4());
566 sprintf(board_ipaddr,"%d.%d.%d.%d",(ip>>24)&0xFF,(ip>>16)&0xFF,(ip>>8)&0xFF,ip&0xFF);
567 mutex.unlock();
568 return QString("%1").arg(board_ipaddr);
569}
570
571bool FirmwareUpdaterCore::setEthBoardInfo(int index, QString newInfo)
572{
573 mutex.lock();
574 eOipv4addr_t address = gMNT.boards_get()[index].getIPV4();
575 bool ret = gMNT.command_info32_set(address, newInfo.toLatin1().data());
576 if(!ret){
577 if(verbosity>0) qDebug() << "setEthBoardInfo failed";
578 }
579
580
581 vector<string> vv = gMNT.command_info32_get(address);
582 foreach (string v, vv) {
583 if(verbosity>0) qDebug() << v.c_str();
584 }
585 // TODO chiedere
586 // it already sets it internally to commandInfo32Get()
587 if(vv.size() > 0){
588 if(verbosity>0) qDebug() << gMNT.boards_get()[index].getInfoOnEEPROM().c_str();
589 }
590 mutex.unlock();
591 return true;
592}
593
594void FirmwareUpdaterCore::setCanBoardInfo(int bus, int id, QString newInfo,QString address, int deviceId,QString *resultString)
595{
596 mutex.lock();
597 QString res;
598 if(!address.isEmpty() && deviceId == -1){
599 getCanBoardsFromEth(address,&res);
600 }else{
601 getCanBoardsFromDriver(address,deviceId,&res);
602 }
603 downloader.change_board_info(bus, id, newInfo.toLatin1().data());
604
605// if(!ethAddress.isEmpty()){
606// if(currentAddress != ethAddress){
607// if(downloader.connected){
608// downloader.stopdriver();
609// }
610// getCanBoardsFromEth(ethAddress,resultString);
611// }
612// downloader.change_board_info(bus, id, newInfo.toLatin1().data());
613// }
614 mutex.unlock();
615}
616
617bool FirmwareUpdaterCore::setCanBoardAddress(int bus, int id, int canType,QString newAddress,QString address,int deviceId,QString *resultString)
618{
619 mutex.lock();
620 QString res;
621 if(!address.isEmpty() && deviceId == -1){
622 getCanBoardsFromEth(address,&res);
623 }else{
624 getCanBoardsFromDriver(address,deviceId,&res);
625 }
626
627 int new_val = newAddress.toInt();
628 if (new_val <=0 || new_val> 15){
629 if(verbosity>0) qDebug() << "Error, new address out of range 0 - 15";
630 return false;
631 }
632
633 if (new_val == id){
634 if(verbosity>0) qDebug() << "Error, same address set";
635 return false;
636 }
637
638 downloader.change_card_address(bus, id, new_val,canType);
639
640// if(!ethAddress.isEmpty()){
641// if(currentAddress != ethAddress){
642// if(downloader.connected){
643// downloader.stopdriver();
644// }
645// getCanBoardsFromEth(ethAddress,resultString);
646// }
647
648// int new_val = newAddress.toInt();
649// if (new_val <=0 || new_val> 15){
650// if(verbosity>0) qDebug() << "Error, new address out of range 0 - 15";
651// return false;
652// }
653
654// if (new_val == id){
655// if(verbosity>0) qDebug() << "Error, same address set";
656// return false;
657// }
658
659// downloader.change_card_address(bus, id, new_val,canType);
660// }
661 mutex.unlock();
662
663 return true;
664}
665
666bool FirmwareUpdaterCore::setEthBoardAddress(int index, QString newAddress)
667{
668
669 mutex.lock();
670 int ip1,ip2,ip3,ip4;
671 sscanf(newAddress.toLatin1().data(),"%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4);
672 if (ip1<0 || ip1>255 || ip2<0 || ip2>255 || ip3<0 || ip3>255 || ip4<0 || ip4>255){
673 mutex.unlock();
674 return false;
675 }
676 ACE_UINT32 iNewAddress=(ip1<<24)|(ip2<<16)|(ip3<<8)|ip4;
677
678
679 ACE_UINT32 address = ipv4toace(gMNT.boards_get()[index].getIPV4());
680 ACE_UINT32 mask = 0xFFFFFF00;
681
682 if(iNewAddress == (iNewAddress & mask)){ // checks new ip address is not a network address . For example x.y.z.w/24 x.y.z.0
683 if(verbosity>0) qDebug() << "Error Setting address";
684 mutex.unlock();
685 return false;
686 }
687
688 if((~mask) == (iNewAddress & (~mask))){ // checks new ip address is not a broadcast address . For example x.y.z.w/24 x.y.z.255
689 if(verbosity>0) qDebug() << "Error Setting address";
690 mutex.unlock();
691 return false;
692 }
693
694 if (iNewAddress == address){
695 if(verbosity>0) qDebug() << "Error, same address set";
696 mutex.unlock();
697 return false;
698 }
699 char old_addr[16];
700 sprintf(old_addr,"%d.%d.%d.%d",(address>>24)&0xFF,(address>>16)&0xFF,(address>>8)&0xFF,address&0xFF);
701
702 bool ret = gMNT.command_changeaddress(acetoipv4(address), acetoipv4(iNewAddress), true, true, true, true);
703 mutex.unlock();
704 return ret;
705
706
707
708}
709
710
711bool FirmwareUpdaterCore::uploadLoader(QString filename,QString *resultString)
712{
713 mutex.lock();
714 FILE *programFile=fopen(filename.toLatin1().data(),"r");
715 if (!programFile){
716 //TODO ERROR
717 if(verbosity>0) qDebug() << "Error opening the selected file!";
718 mutex.unlock();
719 return false;
720 }
721 eOipv4addr_t ipv4 = 0; // all selected
722 eObrd_ethtype_t type = eobrd_ethtype_none;
723 eOversion_t ver;
724 ver.major = 0;
725 ver.minor = 0;
726 std::string result;
727 bool ok = gMNT.program(ipv4, type, eLoader, ver, programFile, false, updateProgressCallback, false);
728
729 fclose(programFile);
730
731 *resultString = QString("%1").arg(result.c_str());
732 if(ok){
733 mutex.unlock();
734 return true;
735 }
736 mutex.unlock();
737 return false;
738}
739
740
741bool FirmwareUpdaterCore::uploadUpdater(QString filename,QString *resultString)
742{
743 mutex.lock();
744 FILE *programFile=fopen(filename.toLatin1().data(),"r");
745 if (!programFile){
746 //TODO ERROR
747 if(verbosity>0) qDebug() << "Error opening the selected file!";
748 mutex.unlock();
749 return false;
750 }
751 eOipv4addr_t ipv4 = 0; // all selected
752 eObrd_ethtype_t type = eobrd_ethtype_none;
753 eOversion_t ver;
754 ver.major = 0;
755 ver.minor = 0;
756 std::string result;
757 bool ok = gMNT.program(ipv4, type, eUpdater, ver, programFile, false, updateProgressCallback, false);
758
759 fclose(programFile);
760
761 *resultString = QString("%1").arg(result.c_str());
762 if(ok){
763 mutex.unlock();
764 return true;
765 }
766 mutex.unlock();
767 return false;
768}
769
770
771#ifdef SERIALMETHOD
772bool FirmwareUpdaterCore::uploadCanApplication(QString filename,QString *resultString, QString ethAddress)
773{
774 mutex.lock();
775 if(!ethAddress.isEmpty()){
776 if(currentAddress != ethAddress){
777 if(downloader.connected){
778 downloader.stopdriver();
779 }
780 getCanBoardsFromEth(ethAddress,resultString);
781 }
782
783 }
784 double timer_start =0;
785 double timer_end =0;
786
787 if (downloader.connected == false){
788 *resultString ="Driver not running";
789 mutex.unlock();
790 return false;
791 }
792
793 //check if at least one board was selected
794 bool at_least_one_board_selected = false;
795
796 for (int i=0; i<downloader.board_list_size; i++){
797 if (downloader.board_list[i].status==BOARD_RUNNING &&
798 downloader.board_list[i].selected==true)
799 at_least_one_board_selected = true;
800 }
801
802 if (!at_least_one_board_selected){
803 *resultString = "No Boards selected! - Select one or more boards to update the firmware";
804 mutex.unlock();
805 return false;
806 }
807
808 QMap<int,int> canDevices;
809 for (int i=0; i<downloader.board_list_size; i++)
810 {
811 if (downloader.board_list[i].selected==true)
812 {
813 canDevices.insertMulti(downloader.board_list[i].bus,i);
814 downloader.board_list[i].selected = false;
815 if(verbosity>0) qDebug() << "FOUND SELECTED SCHEDA " << i << " ON BUS " << downloader.board_list[i].bus;
816 }
817 }
818
819 int ret = 0;
820 int finished = 0;
821 int busCount = canDevices.uniqueKeys().count();
822 for(int k=0;k<busCount;k++){
823
824 int bus = canDevices.uniqueKeys().at(k);
825
826 if (downloader.open_file(filename.toLatin1().data())!=0){
827 *resultString = "Error opening the selected file!";
828 mutex.unlock();
829 return false;
830 }
831 if(verbosity>0) qDebug() << "FILE " << filename << " OPENED";
832 //TODO
833 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
834 // if (strstr (buffer, "calibrationDataSN") != 0)
835 // {
836 // load_calibration (buffer);
837 // return ALL_OK;
838 // }
839 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
840
841 // Get an identification of the firmware fot the file that you have selected
842 // int firmware_board_type=0;
843 // int firmware_version=0;
844 // int firmware_revision=0;
845
846 //indentify download type from the type of the selected boards
847 int download_type = icubCanProto_boardType__unknown;
848 bool download_eeprom =false;
849
850
851 QList<int> indexes = canDevices.values(bus);
852// foreach (int index, indexes) {
853// downloader.board_list[index].selected = true;
854// if(verbosity>0) qDebug() << "SELECTING BOARD " << index << " OF BUS " << k;
855// }
856
857
858 for(int i=0;i<indexes.count();i++){
859 int index = indexes.at(i);
860 downloader.board_list[index].selected = true;
861 if(verbosity>0) qDebug() << "SELECTING BOARD " << index << " OF BUS " << bus;
862 if (downloader.board_list[index].status==BOARD_RUNNING){
863 if (downloader.startscheda(bus,
864 downloader.board_list[index].pid,
865 downloader.board_list[index].eeprom,
866 downloader.board_list[index].type)!=0){
867 *resultString = "Unable to start the board - Unable to send message 'start' or no answer received";
868 mutex.unlock();
869 return false;
870 } else {
871 if(verbosity>0) qDebug() << "START SCHEDA " << index << " ON BUS " << bus << " OK";
872 downloader.board_list[index].status=BOARD_WAITING;
873 }
874 download_type = downloader.board_list[index].type;
875 download_eeprom = downloader.board_list[index].eeprom;
876 }
877 }
878
879
880
881// // Start the download for the selected boards
882// for (int i=0; i<downloader.board_list_size; i++){
883// if (downloader.board_list[i].status==BOARD_RUNNING && downloader.board_list[i].selected==true){
884// if (downloader.startscheda(downloader.board_list[i].bus,
885// downloader.board_list[i].pid,
886// downloader.board_list[i].eeprom,
887// downloader.board_list[i].type)!=0){
888// *resultString = "Unable to start the board - Unable to send message 'start' or no answer received";
889// return false;
890// } else {
891// if(verbosity>0) qDebug() << "START SCHEDA " << i << " OK";
892// downloader.board_list[i].status=BOARD_WAITING;
893// }
894// download_type = downloader.board_list[i].type;
895// download_eeprom = downloader.board_list[i].eeprom;
896// }
897// }
898
899
900
901 timer_start= yarp::os::Time::now();
902 finished = 0;
903 bool print00 = false, print25 = false, print50 = false, print75 = false, print99 = false;
904 // Start the download for the selected boards
905 do
906 {
907 ret = downloader.download_file(bus, 0x0F, download_type,download_eeprom);
908 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;}
909 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;}
910 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;}
911 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;}
912 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;}
913
914 if (ret==1){
915 updateProgress(float(downloader.progress)/downloader.file_length/busCount);
916 }
917 if (ret==-1){
918 if(verbosity>0) qDebug() << "Fatal Error during download, terminate";
919 *resultString = "Fatal Error during download, terminate";
920 finished = 1;
921 }
922 if (ret==0){
923 if(verbosity>0) qDebug() << "Download terminated";
924 *resultString = "Download terminated";
925 finished = 1;
926 }
927
928 }
929 while (finished!=1);
930
931 // End the download for the selected boards
932
933 if(downloader.stopscheda(bus, 15) != 0){
934 if(verbosity>0) qDebug() << "ERROR STOPPING SCHEDA";
935 }else{
936 if(verbosity>0) qDebug() << "scheda stopped";
937 }
938
939 foreach (int index, indexes) {
940 downloader.board_list[index].selected = false;
941 if(verbosity>0) qDebug() << "DE-SELECTING BOARD " << index << " OF BUS " << bus;
942 }
943
944
945
946 }
947 timer_end= yarp::os::Time::now();
948
949
950
951
952 //Display result message
953 if (ret == 0)
954 {
955 char time_text [50];
956 double download_time = (timer_end-timer_start) ;
957 sprintf (time_text, "All Board OK! Download Time (s): %.2f", download_time);
958
959 *resultString = QString("Download Finished. %1").arg(time_text);
960
961 updateProgress(1.0);
962 mutex.unlock();
963 return true;
964 }
965 else
966 {
967 //*resultString = "Error during file transfer";
968
969 updateProgress(1.0);
970 mutex.unlock();
971 return false;
972 }
973
974 mutex.unlock();
975 return true;
976}
977
978#else
979bool FirmwareUpdaterCore::uploadCanApplication(QString filename,QString *resultString, bool ee, QString address,int deviceId,QList <sBoard> *resultCanBoards)
980{
981// if(!address.isEmpty()){
982// if(currentAddress != address){
983// if(downloader.connected){
984// downloader.stopdriver();
985// }
986// getCanBoardsFromEth(address,resultString);
987// }
988
989// }
990 QString res;
991 if(!address.isEmpty() && deviceId == -1){
992 getCanBoardsFromEth(address,&res);
993 }else{
994 getCanBoardsFromDriver(address,deviceId,&res);
995 }
996
997
998 double timer_start =0;
999 double timer_end =0;
1000
1001 if (downloader.connected == false){
1002 *resultString ="Driver not running";
1003 return false;
1004 }
1005
1006 //check if at least one board was selected
1007 bool at_least_one_board_selected = false;
1008 int i = 0;
1009
1010 for (i=0; i<downloader.board_list_size; i++){
1011 if (downloader.board_list[i].status==BOARD_RUNNING &&
1012 downloader.board_list[i].selected==true)
1013 at_least_one_board_selected = true;
1014 }
1015
1016 if (!at_least_one_board_selected){
1017 *resultString = "No Boards selected! - Select one or more boards to update the firmware";
1018 return false;
1019 }
1020 if (downloader.open_file(filename.toLatin1().data())!=0){
1021 *resultString = "Error opening the selected file!";
1022 return false;
1023 }
1024 //TODO
1025//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1026 // if (strstr (buffer, "calibrationDataSN") != 0)
1027 // {
1028 // load_calibration (buffer);
1029 // return ALL_OK;
1030 // }
1031//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1032
1033 // Get an identification of the firmware fot the file that you have selected
1034 int firmware_board_type=0;
1035 int firmware_version=0;
1036 int firmware_revision=0;
1037
1038 //indentify download type from the type of the selected boards
1039 int download_type = icubCanProto_boardType__unknown;
1040 bool download_eeprom =false;
1041 for (i=0; i<downloader.board_list_size; i++)
1042 {
1043 if (downloader.board_list[i].selected==true)
1044 {
1045 download_type = downloader.board_list[i].type;
1046 download_eeprom = downloader.board_list[i].eeprom;
1047 }
1048
1049 }
1050 download_eeprom = ee;
1051
1052 // Start the download for the selected boards
1053 for (i=0; i<downloader.board_list_size; i++){
1054 if (downloader.board_list[i].status==BOARD_RUNNING && downloader.board_list[i].selected==true){
1055 //bool EE = downloader.board_list[i].eeprom;
1056 bool EE = ee;
1057 if (downloader.startscheda(downloader.board_list[i].bus, downloader.board_list[i].pid, EE, downloader.board_list[i].type)!=0){
1058 *resultString = "Unable to start the board - Unable to send message 'start' or no answer received";
1059 return false;
1060 } else {
1061 downloader.board_list[i].status=BOARD_WAITING;
1062 }
1063 }
1064 }
1065
1066 int ret = 0;
1067 int finished = 0;
1068
1069 timer_start= yarp::os::Time::now();
1070
1071// bool acemortest_notyetstopped = true;
1072// const float acemortest_progress = 0.10;
1073
1074 bool print00 = false, print25 = false, print50 = false, print75 = false, print99 = false;
1075 // Start the download for the selected boards
1076 do
1077 {
1078 ret = downloader.download_file(CanPacket::everyCANbus, 0x0F, download_type,download_eeprom);
1079 if (float(downloader.progress)/downloader.file_length >0.0 && print00==false) {if(verbosity>0) qDebug("programming %s: 1%% done",filename.toLatin1().data()); print00=true;}
1080 if (float(downloader.progress)/downloader.file_length >0.25 && print25==false) {if(verbosity>0) qDebug("programming %s: 25%% done",filename.toLatin1().data()); print25=true;}
1081 if (float(downloader.progress)/downloader.file_length >0.50 && print50==false) {if(verbosity>0) qDebug("programming %s: 50%% done",filename.toLatin1().data()); print50=true;}
1082 if (float(downloader.progress)/downloader.file_length >0.75 && print75==false) {if(verbosity>0) qDebug("programming %s: 75%% done",filename.toLatin1().data()); print75=true;}
1083 if (float(downloader.progress)/downloader.file_length >0.99 && print99==false) {if(verbosity>0) qDebug("programming %s: finished!",filename.toLatin1().data()); print99=true;}
1084
1085// if ((float(downloader.progress)/downloader.file_length > acemortest_progress) && (acemortest_notyetstopped))
1086// {
1087// // place you breakpoint in here.
1088// acemortest_notyetstopped = false;
1089// }
1090
1091 if (ret==1)
1092 {
1093 updateProgress(float(downloader.progress)/downloader.file_length);
1094 }
1095 if (ret==-1)
1096 {
1097 *resultString = "Fatal Error during download, terminate";
1098 finished = 1;
1099 }
1100 if (ret==0)
1101 {
1102 *resultString = "Download terminated";
1103 finished = 1;
1104 }
1105
1106 }
1107 while (finished!=1);
1108 timer_end= yarp::os::Time::now();
1109
1110 // End the download for the selected boards
1111 int errors =0;
1112 downloader.stopscheda(CanPacket::everyCANbus, 15);
1113
1114
1115
1116 yarp::os::Time::delay(3.0);
1117 //Display result message
1118 if (ret == 0)
1119 {
1120 char time_text [50];
1121 double download_time = (timer_end-timer_start) ;
1122 sprintf (time_text, "All Board OK! Download Time (s): %.2f", download_time);
1123
1124 *resultString = QString("Download Finished. %1").arg(time_text);
1125
1126 updateProgress(1.0);
1127 downloader.initschede();
1128
1129 canBoards.clear();
1130 for(int i=0; i<downloader.board_list_size;i++){
1131 canBoards.append(downloader.board_list[i]);
1132 }
1133 if(resultCanBoards){
1134 *resultCanBoards = canBoards;
1135 }
1136 return true;
1137 }
1138 else
1139 {
1140 //*resultString = "Error during file transfer";
1141
1142 updateProgress(1.0);
1143 downloader.initschede();
1144 return false;
1145 }
1146 downloader.initschede();
1147
1148 canBoards.clear();
1149 for(int i=0; i<downloader.board_list_size;i++){
1150 canBoards.append(downloader.board_list[i]);
1151 }
1152 *resultCanBoards = canBoards;
1153 return true;
1154}
1155#endif
1156
1157bool FirmwareUpdaterCore::uploadEthApplication(QString filename,QString *resultString)
1158{
1159 mutex.lock();
1160 FILE *programFile=fopen(filename.toLatin1().data(),"r");
1161// if(verbosity>0) qDebug() << "attempting opening the selected file:" << filename << "or .." << filename.toLatin1().data();
1162 if (!programFile){
1163 //TODO ERROR
1164 if(verbosity>0) qDebug() << "Error opening the selected file:" << filename << "or .." << filename.toLatin1().data();
1165 mutex.unlock();
1166 return false;
1167 }
1168 eOipv4addr_t ipv4 = 0; // all selected
1169 eObrd_ethtype_t type = eobrd_ethtype_none;
1170 eOversion_t ver;
1171 ver.major = 0;
1172 ver.minor = 0;
1173 std::string result;
1174 bool ok = gMNT.program(ipv4, type, eApplication, ver, programFile, false, updateProgressCallback, false);
1175
1176 fclose(programFile);
1177
1178 *resultString = QString("%1").arg(result.c_str());
1179 if(ok){
1180 mutex.unlock();
1181 return true;
1182 }
1183 mutex.unlock();
1184 return false;
1185}
1186
1187
1188
1190{
1191 return &downloader;
1192}
1193
1194
1195
1197{
1198 mutex.lock();
1200 mutex.unlock();
1201}
1202
1203
1205{
1206 mutex.lock();
1207 gMNT.command_def2run(EthMaintainer::ipv4OfAllSelected, eApplication, false, false);
1208 mutex.unlock();
1209}
1210
1212{
1213 mutex.lock();
1214 gMNT.command_def2run(EthMaintainer::ipv4OfAllSelected, eUpdater, false, false);
1215 mutex.unlock();
1216}
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
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 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 getProcessFromUint(uint8_t id, bool isMultiCore=false, eObrd_ethtype_t boardtype=eobrd_ethtype_unknown)
QString getEthBoardAddress(int index)
boardInfo2_t getMoreDetails(int boardNum=EthMaintainer::ipv4OfAllSelected, QString *infoString=NULL, eOipv4addr_t *address=NULL)
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)
int change_board_info(int bus, int target_id, char *board_info)
int initdriver(yarp::os::Searchable &config, bool verbose=true)
sBoard * board_list
Definition downloader.h:150
int board_list_size
Definition downloader.h:151
int download_file(int bus, int board_pid, int download_type, bool eeprom)
#define BOARD_RUNNING
Definition downloader.h:45
#define BOARD_WAITING
Definition downloader.h:46
FirmwareUpdaterCore * self
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