3#include <EoUpdaterProtocol.h>
14bool FirmwareUpdaterCore::isValidIpAddress(QString addr)
18 ipFields = addr.split(
".");
19 if(ipFields.count() == 4 && ipFields[3].contains(
":"))
21 QString address_,port_;
22 int ipv0, ipv1, ipv2, ipv3;
24 address_ = addr.split(
":")[0];
25 port_ = ipFields[3].split(
":")[1];
28 if (!re.exactMatch(port_))
return false;
29 for(
int k=0; k<4; k++)
31 if (!re.exactMatch(address_.split(
".")[k]))
return false;
34 ipv0 = address_.split(
".")[0].toInt();
35 ipv1 = address_.split(
".")[1].toInt();
36 ipv2 = address_.split(
".")[2].toInt();
37 ipv3 = address_.split(
".")[3].toInt();
39 if(ipv0 == 10 && 0 < ipv1 < 255 && 0 < ipv2 < 255 && 0 < ipv3 < 255 && 0 < port_.toInt() < 255)
41 hostIPaddress = EO_COMMON_IPV4ADDR(ipv0, ipv1, ipv2, ipv3);
56 verbosity = VerbositY;
62 Bottle sensorSetConfig=config.findGroup(
"DRIVERS").tail();
64 for (
int t=0; t<sensorSetConfig.size(); ++t){
65 yarp::os::Bottle sensorConfig(sensorSetConfig.get(t).toString());
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;
72 int num = QString(
"%1").arg(line).toInt(&
ok);
74 devices.append(QPair<QString,QVariant>(type,num));
76 devices.append(QPair<QString,QVariant>(type,line));
83 if(isValidIpAddress(line))
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];
91 int ipv0, ipv1, ipv2, ipv3;
93 ipv0 = address.split(
".")[0].toInt();
94 ipv1 = address.split(
".")[1].toInt();
95 ipv2 = address.split(
".")[2].toInt();
96 ipv3 = address.split(
".")[3].toInt();
98 hostIPaddress = EO_COMMON_IPV4ADDR(ipv0, ipv1, ipv2, ipv3);
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:";
113 if(!gMNT.
open(hostIPaddress, port))
115 if(verbosity>0) qDebug(
"Can't open socket, aborting.");
127 bool lowers_are_verbose = (verb >= 1) ?
true :
false;
128 gMNT.
verbose(lowers_are_verbose);
138 for(
int i=0;i<devices.count();i++){
139 QPair<QString,QVariant>
p = devices.at(i);
140 names.append(
p.first);
193 if(!device.isEmpty() && !
id.isEmpty()){
194 if(device.contains(
"ETH")){
198 yDebug() <<
"FirmwareUpdaterCore::connectTo() has found " << num <<
" ETH boards";
216 QString boardIp = QString(
"%1").arg(gMNT.
boards_get()[i].getIPV4string().c_str());
218 return gMNT.
boards_get()[i].isInMaintenance();
233 gMNT.
boards_get()[index].setSelected(selected);
243 if(QString(
"%1").arg(gMNT.
boards_get()[i].getIPV4string().c_str()) == boardIp){
256 if(!address.isEmpty() && deviceId == -1){
262 this->canBoards = selectedBoards;
264 foreach (
sBoard b, selectedBoards) {
296 if(!address.isEmpty() && deviceId == -1){
312 *address = gMNT.
boards_get()[boardNum].getIPV4();
313 if(infoString && info.protversion == 0){
314 *infoString = QString(
"%1").arg(gMNT.
moreinformation(*address,
false).c_str());
326 case uprot_proc_Loader:
328 case uprot_proc_Updater:
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)
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)
343 return "eApplication_core_1";
344 case uprot_proc_ApplPROGupdater:
345 return "eApplPROGupdater";
359 if(downloader.
connected && (!currentAddress.isEmpty() || (currentDriver != driver || currentId !=
networkId ))){
362 if(currentDriver == driver && currentId ==
networkId){
369 yarp::os::Property params;
371 if(driver.contains(
"CFW2",Qt::CaseInsensitive)){
373 }
else if(driver.contains(
"ECAN",Qt::CaseInsensitive)){
375 }
else if(driver.contains(
"PCAN",Qt::CaseInsensitive)){
377 }
else if(driver.contains(
"SOCKET",Qt::CaseInsensitive)){
380 params.put(
"device",
networkType.toLatin1().data());
382 params.put(
"canTxQueue", 64);
383 params.put(
"canRxQueue", 64);
384 params.put(
"canTxTimeout", 2000);
385 params.put(
"canRxTimeout", 2000);
388 int ret = downloader.
initdriver(params, (verbosity>1) ?
true :
false);
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";
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";
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) +
">";
422 currentDriver = driver;
438 if(downloader.
connected && address != currentAddress && !currentAddress.isEmpty() || (currentAddress.isEmpty() && !currentDriver.isEmpty())){
442 if(currentAddress == address){
456 if(verbosity>0) qDebug() <<
"FirmwareUpdaterCore::getCanBoardsFromEth(): Init driver failed - Could not find network interface";
458 *retString =
"Init driver failed - Could not find network interface";
465 yarp::os::Property params;
466 params.put(
"device",
"ETH");
469 params.put(
"canid",
canID);
473 int ret = downloader.
initdriver(params, (verbosity>1) ?
true :
false);
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";
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?!";
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;
506 currentAddress = address;
516bool FirmwareUpdaterCore::compile_ip_addresses(
const char* addr,
unsigned int *
remoteAddr,
unsigned int *
localAddr)
518 ACE_UINT32 ip1,ip2,ip3,ip4;
519 sscanf(addr,
"%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4);
523 ACE_INET_Addr* addr_array=NULL;
524 int ret=ACE::get_ip_interfaces(count,addr_array);
532 *
localAddr=addr_array[0].get_ip_address();
534 for (
unsigned int a=1; a<count; ++a)
536 if ((*
remoteAddr & 0xFFFF0000)==(addr_array[a].get_ip_address() & 0xFFFF0000))
538 *
localAddr=addr_array[a].get_ip_address();
556 QString ret = QString(
"%1").arg(gMNT.
boards_get()[index].getInfoOnEEPROM().c_str());
564 char board_ipaddr[16];
566 sprintf(board_ipaddr,
"%d.%d.%d.%d",(ip>>24)&0xFF,(ip>>16)&0xFF,(ip>>8)&0xFF,ip&0xFF);
568 return QString(
"%1").arg(board_ipaddr);
574 eOipv4addr_t address = gMNT.
boards_get()[index].getIPV4();
577 if(verbosity>0) qDebug() <<
"setEthBoardInfo failed";
582 foreach (
string v, vv) {
583 if(verbosity>0) qDebug() << v.c_str();
588 if(verbosity>0) qDebug() << gMNT.
boards_get()[index].getInfoOnEEPROM().c_str();
598 if(!address.isEmpty() && deviceId == -1){
621 if(!address.isEmpty() && deviceId == -1){
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";
634 if(verbosity>0) qDebug() <<
"Error, same address set";
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){
676 ACE_UINT32 iNewAddress=(ip1<<24)|(ip2<<16)|(ip3<<8)|ip4;
680 ACE_UINT32 mask = 0xFFFFFF00;
682 if(iNewAddress == (iNewAddress & mask)){
683 if(verbosity>0) qDebug() <<
"Error Setting address";
688 if((~mask) == (iNewAddress & (~mask))){
689 if(verbosity>0) qDebug() <<
"Error Setting address";
694 if (iNewAddress == address){
695 if(verbosity>0) qDebug() <<
"Error, same address set";
700 sprintf(old_addr,
"%d.%d.%d.%d",(address>>24)&0xFF,(address>>16)&0xFF,(address>>8)&0xFF,address&0xFF);
714 FILE *programFile=fopen(filename.toLatin1().data(),
"r");
717 if(verbosity>0) qDebug() <<
"Error opening the selected file!";
721 eOipv4addr_t ipv4 = 0;
722 eObrd_ethtype_t type = eobrd_ethtype_none;
731 *resultString = QString(
"%1").arg(result.c_str());
744 FILE *programFile=fopen(filename.toLatin1().data(),
"r");
747 if(verbosity>0) qDebug() <<
"Error opening the selected file!";
751 eOipv4addr_t ipv4 = 0;
752 eObrd_ethtype_t type = eobrd_ethtype_none;
761 *resultString = QString(
"%1").arg(result.c_str());
775 if(!ethAddress.isEmpty()){
776 if(currentAddress != ethAddress){
784 double timer_start =0;
788 *resultString =
"Driver not running";
794 bool at_least_one_board_selected =
false;
799 at_least_one_board_selected =
true;
802 if (!at_least_one_board_selected){
803 *resultString =
"No Boards selected! - Select one or more boards to update the firmware";
808 QMap<int,int> canDevices;
815 if(verbosity>0) qDebug() <<
"FOUND SELECTED SCHEDA " << i <<
" ON BUS " << downloader.
board_list[i].
bus;
821 int busCount = canDevices.uniqueKeys().count();
822 for(
int k=0;k<busCount;k++){
824 int bus = canDevices.uniqueKeys().at(k);
826 if (downloader.
open_file(filename.toLatin1().data())!=0){
827 *resultString =
"Error opening the selected file!";
831 if(verbosity>0) qDebug() <<
"FILE " << filename <<
" OPENED";
847 int download_type = icubCanProto_boardType__unknown;
848 bool download_eeprom =
false;
851 QList<int> indexes = canDevices.values(bus);
858 for(
int i=0;i<indexes.count();i++){
859 int index = indexes.at(i);
861 if(verbosity>0) qDebug() <<
"SELECTING BOARD " << index <<
" OF BUS " << bus;
867 *resultString =
"Unable to start the board - Unable to send message 'start' or no answer received";
871 if(verbosity>0) qDebug() <<
"START SCHEDA " << index <<
" ON BUS " << bus <<
" OK";
901 timer_start= yarp::os::Time::now();
903 bool print00 =
false, print25 =
false, print50 =
false, print75 =
false, print99 =
false;
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;}
918 if(verbosity>0) qDebug() <<
"Fatal Error during download, terminate";
919 *resultString =
"Fatal Error during download, terminate";
923 if(verbosity>0) qDebug() <<
"Download terminated";
924 *resultString =
"Download terminated";
934 if(verbosity>0) qDebug() <<
"ERROR STOPPING SCHEDA";
936 if(verbosity>0) qDebug() <<
"scheda stopped";
939 foreach (
int index, indexes) {
941 if(verbosity>0) qDebug() <<
"DE-SELECTING BOARD " << index <<
" OF BUS " << bus;
947 timer_end= yarp::os::Time::now();
956 double download_time = (timer_end-timer_start) ;
957 sprintf (time_text,
"All Board OK! Download Time (s): %.2f", download_time);
959 *resultString = QString(
"Download Finished. %1").arg(time_text);
991 if(!address.isEmpty() && deviceId == -1){
998 double timer_start =0;
1002 *resultString =
"Driver not running";
1007 bool at_least_one_board_selected =
false;
1013 at_least_one_board_selected =
true;
1016 if (!at_least_one_board_selected){
1017 *resultString =
"No Boards selected! - Select one or more boards to update the firmware";
1020 if (downloader.
open_file(filename.toLatin1().data())!=0){
1021 *resultString =
"Error opening the selected file!";
1034 int firmware_board_type=0;
1035 int firmware_version=0;
1036 int firmware_revision=0;
1039 int download_type = icubCanProto_boardType__unknown;
1040 bool download_eeprom =
false;
1050 download_eeprom = ee;
1058 *resultString =
"Unable to start the board - Unable to send message 'start' or no answer received";
1069 timer_start= yarp::os::Time::now();
1074 bool print00 =
false, print25 =
false, print50 =
false, print75 =
false, print99 =
false;
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;}
1097 *resultString =
"Fatal Error during download, terminate";
1102 *resultString =
"Download terminated";
1107 while (finished!=1);
1108 timer_end= yarp::os::Time::now();
1116 yarp::os::Time::delay(3.0);
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);
1124 *resultString = QString(
"Download Finished. %1").arg(time_text);
1133 if(resultCanBoards){
1134 *resultCanBoards = canBoards;
1152 *resultCanBoards = canBoards;
1160 FILE *programFile=fopen(filename.toLatin1().data(),
"r");
1164 if(verbosity>0) qDebug() <<
"Error opening the selected file:" << filename <<
"or .." << filename.toLatin1().data();
1168 eOipv4addr_t ipv4 = 0;
1169 eObrd_ethtype_t type = eobrd_ethtype_none;
1178 *resultString = QString(
"%1").arg(result.c_str());
eOipv4addr_t acetoipv4(ACE_UINT32 address)
ACE_UINT32 ipv4toace(eOipv4addr_t ipv4)
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)
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)
EthBoardList getEthBoardList()
QList< QPair< QString, QVariant > > getDevices()
void updateProgress(float)
void bootFromApplication()
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)
QStringList getDevicesName()
bool uploadEthApplication(QString filename, QString *resultString)
bool uploadLoader(QString filename, QString *resultString)
bool init(Searchable &config, int port, QString address, int VerbositY)
cDownloader * getDownloader()
bool setVerbosity(int verb)
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)
int download_file(int bus, int board_pid, int download_type, bool eeprom)
FirmwareUpdaterCore * self
static void updateProgressCallback(float fraction)
#define DEFAULT_IP_ADDRESS