diff --git a/apps/aqhome-tool/flash.c b/apps/aqhome-tool/flash.c index d9a8991..48bf4e4 100644 --- a/apps/aqhome-tool/flash.c +++ b/apps/aqhome-tool/flash.c @@ -45,17 +45,32 @@ static int _doFlash(GWEN_DB_NODE *dbArgs); + +static AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename); + +static int _rebootNode(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds); +static int _performFlashProcedure(GWEN_MSG_ENDPOINT_MGR *emgr, + GWEN_MSG_ENDPOINT *epTcp, + unsigned int uid, + const AQH_FLASHRECORD_LIST *flashRecordList, + int pageSize, + int timeoutInSeconds); + + +static int _flashStart(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds); + + static GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds); static int _sendRebootRequest(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid); static int _sendFlashStart(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid); static int _waitForFlashResponseMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds); static int _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds); -static int _sendFlashData(GWEN_MSG_ENDPOINT_MGR *emgr, - GWEN_MSG_ENDPOINT *epTcp, - const AQH_FLASHRECORD *flashRecord, - uint16_t pageSize, - int timeoutInSeconds); +static int _sendFlashRecord(GWEN_MSG_ENDPOINT_MGR *emgr, + GWEN_MSG_ENDPOINT *epTcp, + const AQH_FLASHRECORD *flashRecord, + uint16_t pageSize, + int timeoutInSeconds); static int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason); @@ -182,7 +197,6 @@ int _doFlash(GWEN_DB_NODE *dbArgs) unsigned int uid; const char *hexFilename; AQH_FLASHRECORD_LIST *flashRecordList=NULL; - AQH_FLASHRECORD *flashRecord; GWEN_MSG *msg; uint16_t pageSize; @@ -195,25 +209,10 @@ int _doFlash(GWEN_DB_NODE *dbArgs) return 1; } hexFilename=GWEN_DB_GetCharValue(dbArgs, "hexFilename", 0, NULL); - if (!(hexFilename && *hexFilename)) { - DBG_ERROR(NULL, "Missing or invalid hexfilename"); - return 1; - } - else { - AQH_HEXFILE *hexfile; - - /* read hexfile */ - hexfile=AQH_Hexfile_fromFile(hexFilename); - if (hexfile==NULL) { - DBG_ERROR(NULL, "Error loading hexfile \"%s\"", hexFilename); - return 2; - } - flashRecordList=AQH_FlashRecord_fromHexfileRecords(AQH_Hexfile_GetRecordList(hexfile)); - AQH_Hexfile_free(hexfile); - if (flashRecordList==NULL) { - DBG_ERROR(NULL, "Error reading flash record list from hexfile"); - return 2; - } + flashRecordList=_readHexfileIntoFlashRecordList(hexFilename); + if (flashRecordList==NULL) { + DBG_ERROR(NULL, "Error reading flash record list from hexfile"); + return 2; } /* setup client connection */ @@ -234,25 +233,18 @@ int _doFlash(GWEN_DB_NODE *dbArgs) } if (doReboot) { - /* send REBOOT_REQUEST message */ - DBG_INFO(NULL, "Sending REBOOT REQUEST message"); - rv=_sendRebootRequest(epTcp, uid); + fprintf(stdout, "Sending REBOOT request\n"); + rv=_rebootNode(emgr, epTcp, uid, timeoutInSeconds); if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); GWEN_MsgEndpointMgr_free(emgr); return 3; } - - rv=_waitForRebootResponseMessage(emgr, epTcp, timeoutInSeconds); - if (rv!=0) { - DBG_INFO(NULL, "Bad or no reboot response received (%d).", rv); - GWEN_MsgEndpointMgr_free(emgr); - return 3; - } - DBG_INFO(NULL, "REBOOT_RESPONSE message received"); + fprintf(stdout, "Reboot in progress\n"); } /* wait for FLASH_READY message */ + fprintf(stdout, "Waiting for node to become ready for flashing\n"); msg=_waitForFlashReadyMessageForUid(emgr, epTcp, uid, timeoutInSeconds); if (msg==NULL) { DBG_INFO(NULL, "No FLASH_READY message received."); @@ -261,37 +253,98 @@ int _doFlash(GWEN_DB_NODE *dbArgs) } DBG_INFO(NULL, "FLASH_READY message received"); pageSize=AQH_FlashReadyMsg_GetPagesize(msg); + fprintf(stdout, "Node is ready for flashing (pagesize=%d bytes)\n", pageSize); GWEN_Msg_free(msg); - /* send FLASH_START message */ - DBG_INFO(NULL, "Sending FLASH START message"); - rv=_sendFlashStart(epTcp, uid); + /* perform flash */ + rv=_performFlashProcedure(emgr, epTcp, uid, flashRecordList, pageSize, timeoutInSeconds); + if (rv<0) { + if (rv<0) { + DBG_INFO(NULL, "here (%d)", rv); + GWEN_MsgEndpointMgr_free(emgr); + AQH_FlashRecord_List_free(flashRecordList); + return 4; + } + } + + AQH_FlashRecord_List_free(flashRecordList); + GWEN_MsgEndpointMgr_free(emgr); + return 0; +} + + + +AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename) +{ + AQH_HEXFILE *hexfile; + AQH_FLASHRECORD_LIST *flashRecordList; + + /* read hexfile */ + hexfile=AQH_Hexfile_fromFile(hexFilename); + if (hexfile==NULL) { + DBG_ERROR(NULL, "Error loading hexfile \"%s\"", hexFilename); + return NULL; + } + flashRecordList=AQH_FlashRecord_fromHexfileRecords(AQH_Hexfile_GetRecordList(hexfile)); + AQH_Hexfile_free(hexfile); + if (flashRecordList==NULL) { + DBG_ERROR(NULL, "Error reading flash record list from hexfile"); + return NULL; + } + + return flashRecordList; +} + + + +int _rebootNode(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds) +{ + int rv; + + /* send REBOOT_REQUEST message */ + DBG_INFO(NULL, "Sending REBOOT REQUEST message"); + rv=_sendRebootRequest(epTcp, uid); if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); - GWEN_MsgEndpointMgr_free(emgr); - return 3; + return rv; } - /* wait for response */ - rv=_waitForFlashResponseMessage(emgr, epTcp, timeoutInSeconds); + rv=_waitForRebootResponseMessage(emgr, epTcp, timeoutInSeconds); if (rv!=0) { - DBG_INFO(NULL, "Bad or no response received (%d).", rv); - GWEN_MsgEndpointMgr_free(emgr); - return 3; + DBG_INFO(NULL, "Bad or no reboot response received (%d).", rv); + return rv; } - DBG_INFO(NULL, "FLASH_RESPONSE message received"); + DBG_INFO(NULL, "REBOOT_RESPONSE message received"); + return 0; +} + + +int _performFlashProcedure(GWEN_MSG_ENDPOINT_MGR *emgr, + GWEN_MSG_ENDPOINT *epTcp, + unsigned int uid, + const AQH_FLASHRECORD_LIST *flashRecordList, + int pageSize, + int timeoutInSeconds) +{ + int rv; + const AQH_FLASHRECORD *flashRecord; + + fprintf(stdout, "Sending FLASH_START\n"); + rv=_flashStart(emgr, epTcp, uid, timeoutInSeconds); + if (rv<0) { + DBG_INFO(NULL, "here (%d)", rv); + return rv; + } + + /* send FLASH_DATA requests and retrieve FLASH_RSP responses */ flashRecord=AQH_FlashRecord_List_First(flashRecordList); while(flashRecord) { - int rv; - DBG_ERROR(NULL, "Sending flash record at %08x", AQH_FlashRecord_GetAddress(flashRecord)); - rv=_sendFlashData(emgr, epTcp, flashRecord, pageSize, timeoutInSeconds); + rv=_sendFlashRecord(emgr, epTcp, flashRecord, pageSize, timeoutInSeconds); if (rv!=0) { DBG_ERROR(NULL, "Error sending flash data (%d)", rv); - AQH_FlashRecord_List_free(flashRecordList); - GWEN_MsgEndpointMgr_free(emgr); - return 3; + return rv; } flashRecord=AQH_FlashRecord_List_Next(flashRecord); } @@ -299,23 +352,58 @@ int _doFlash(GWEN_DB_NODE *dbArgs) rv=_sendFlashEnd(epTcp, 0); if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); - GWEN_MsgEndpointMgr_free(emgr); - return 4; + return rv; } rv=Utils_FlushOutMessageQueue(emgr, epTcp, timeoutInSeconds); if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); - GWEN_MsgEndpointMgr_free(emgr); - return 4; + return rv; } - GWEN_MsgEndpointMgr_free(emgr); return 0; } +int _flashStart(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds) +{ + int rv; + int i; + + for (i=0; i