aqhome-tool: cleanup of flash command.

This commit is contained in:
Martin Preuss
2023-04-22 14:46:25 +02:00
parent c73f671deb
commit 0f6ecdd95d
2 changed files with 155 additions and 65 deletions

View File

@@ -45,17 +45,32 @@
static int _doFlash(GWEN_DB_NODE *dbArgs); 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, static GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
unsigned int uid, int timeoutInSeconds); unsigned int uid, int timeoutInSeconds);
static int _sendRebootRequest(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid); static int _sendRebootRequest(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid);
static int _sendFlashStart(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 _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 _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds);
static int _sendFlashData(GWEN_MSG_ENDPOINT_MGR *emgr, static int _sendFlashRecord(GWEN_MSG_ENDPOINT_MGR *emgr,
GWEN_MSG_ENDPOINT *epTcp, GWEN_MSG_ENDPOINT *epTcp,
const AQH_FLASHRECORD *flashRecord, const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize, uint16_t pageSize,
int timeoutInSeconds); int timeoutInSeconds);
static int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason); static int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason);
@@ -182,7 +197,6 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
unsigned int uid; unsigned int uid;
const char *hexFilename; const char *hexFilename;
AQH_FLASHRECORD_LIST *flashRecordList=NULL; AQH_FLASHRECORD_LIST *flashRecordList=NULL;
AQH_FLASHRECORD *flashRecord;
GWEN_MSG *msg; GWEN_MSG *msg;
uint16_t pageSize; uint16_t pageSize;
@@ -195,25 +209,10 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
return 1; return 1;
} }
hexFilename=GWEN_DB_GetCharValue(dbArgs, "hexFilename", 0, NULL); hexFilename=GWEN_DB_GetCharValue(dbArgs, "hexFilename", 0, NULL);
if (!(hexFilename && *hexFilename)) { flashRecordList=_readHexfileIntoFlashRecordList(hexFilename);
DBG_ERROR(NULL, "Missing or invalid hexfilename"); if (flashRecordList==NULL) {
return 1; DBG_ERROR(NULL, "Error reading flash record list from hexfile");
} return 2;
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;
}
} }
/* setup client connection */ /* setup client connection */
@@ -234,25 +233,18 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
} }
if (doReboot) { if (doReboot) {
/* send REBOOT_REQUEST message */ fprintf(stdout, "Sending REBOOT request\n");
DBG_INFO(NULL, "Sending REBOOT REQUEST message"); rv=_rebootNode(emgr, epTcp, uid, timeoutInSeconds);
rv=_sendRebootRequest(epTcp, uid);
if (rv<0) { if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv); DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpointMgr_free(emgr); GWEN_MsgEndpointMgr_free(emgr);
return 3; return 3;
} }
fprintf(stdout, "Reboot in progress\n");
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");
} }
/* wait for FLASH_READY message */ /* wait for FLASH_READY message */
fprintf(stdout, "Waiting for node to become ready for flashing\n");
msg=_waitForFlashReadyMessageForUid(emgr, epTcp, uid, timeoutInSeconds); msg=_waitForFlashReadyMessageForUid(emgr, epTcp, uid, timeoutInSeconds);
if (msg==NULL) { if (msg==NULL) {
DBG_INFO(NULL, "No FLASH_READY message received."); 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"); DBG_INFO(NULL, "FLASH_READY message received");
pageSize=AQH_FlashReadyMsg_GetPagesize(msg); pageSize=AQH_FlashReadyMsg_GetPagesize(msg);
fprintf(stdout, "Node is ready for flashing (pagesize=%d bytes)\n", pageSize);
GWEN_Msg_free(msg); GWEN_Msg_free(msg);
/* send FLASH_START message */ /* perform flash */
DBG_INFO(NULL, "Sending FLASH START message"); rv=_performFlashProcedure(emgr, epTcp, uid, flashRecordList, pageSize, timeoutInSeconds);
rv=_sendFlashStart(epTcp, uid); 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) { if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv); DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpointMgr_free(emgr); return rv;
return 3;
} }
/* wait for response */ rv=_waitForRebootResponseMessage(emgr, epTcp, timeoutInSeconds);
rv=_waitForFlashResponseMessage(emgr, epTcp, timeoutInSeconds);
if (rv!=0) { if (rv!=0) {
DBG_INFO(NULL, "Bad or no response received (%d).", rv); DBG_INFO(NULL, "Bad or no reboot response received (%d).", rv);
GWEN_MsgEndpointMgr_free(emgr); return rv;
return 3;
} }
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); flashRecord=AQH_FlashRecord_List_First(flashRecordList);
while(flashRecord) { while(flashRecord) {
int rv;
DBG_ERROR(NULL, "Sending flash record at %08x", AQH_FlashRecord_GetAddress(flashRecord)); 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) { if (rv!=0) {
DBG_ERROR(NULL, "Error sending flash data (%d)", rv); DBG_ERROR(NULL, "Error sending flash data (%d)", rv);
AQH_FlashRecord_List_free(flashRecordList); return rv;
GWEN_MsgEndpointMgr_free(emgr);
return 3;
} }
flashRecord=AQH_FlashRecord_List_Next(flashRecord); flashRecord=AQH_FlashRecord_List_Next(flashRecord);
} }
@@ -299,23 +352,58 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
rv=_sendFlashEnd(epTcp, 0); rv=_sendFlashEnd(epTcp, 0);
if (rv<0) { if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv); DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpointMgr_free(emgr); return rv;
return 4;
} }
rv=Utils_FlushOutMessageQueue(emgr, epTcp, timeoutInSeconds); rv=Utils_FlushOutMessageQueue(emgr, epTcp, timeoutInSeconds);
if (rv<0) { if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv); DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpointMgr_free(emgr); return rv;
return 4;
} }
GWEN_MsgEndpointMgr_free(emgr);
return 0; 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<FLASH_TOOL_MAX_REPEAT; i++) {
if (i)
fprintf(stdout, " sending FLASH_START (retry %d)\n", i);
/* send FLASH_START message */
DBG_INFO(NULL, "Sending FLASH START message (%d)", i);
rv=_sendFlashStart(epTcp, uid);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
/* wait for response */
rv=_waitForFlashResponseMessage(emgr, epTcp, timeoutInSeconds);
if (rv!=0) {
DBG_INFO(NULL, "Bad or no response received (%d).", rv);
return rv;
}
break;
}
DBG_INFO(NULL, "FLASH_RESPONSE message received");
return 0;
}
GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
unsigned int uid, int timeoutInSeconds) unsigned int uid, int timeoutInSeconds)
{ {
@@ -435,11 +523,11 @@ int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason)
int _sendFlashData(GWEN_MSG_ENDPOINT_MGR *emgr, int _sendFlashRecord(GWEN_MSG_ENDPOINT_MGR *emgr,
GWEN_MSG_ENDPOINT *epTcp, GWEN_MSG_ENDPOINT *epTcp,
const AQH_FLASHRECORD *flashRecord, const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize, uint16_t pageSize,
int timeoutInSeconds) int timeoutInSeconds)
{ {
const uint8_t *ptr; const uint8_t *ptr;
uint32_t len; uint32_t len;
@@ -463,7 +551,10 @@ int _sendFlashData(GWEN_MSG_ENDPOINT_MGR *emgr,
GWEN_Text_LogString((const char*) ptr, sendLen, NULL, GWEN_LoggerLevel_Error); GWEN_Text_LogString((const char*) ptr, sendLen, NULL, GWEN_LoggerLevel_Error);
#endif #endif
fprintf(stdout, "Send data: dest addr=%04x, len=%d bytes\n", address, sendLen); if (i)
fprintf(stdout, "Send data: dest addr=%04x, len=%d bytes (retry %d)\n", address, sendLen, i);
else
fprintf(stdout, "Send data: dest addr=%04x, len=%d bytes\n", address, sendLen);
msgNode=AQH_FlashDataMsg_new(0, 0xc1, AQH_MSG_TYPE_FLASH_DATA, address, ptr, sendLen); msgNode=AQH_FlashDataMsg_new(0, 0xc1, AQH_MSG_TYPE_FLASH_DATA, address, ptr, sendLen);
if (msgNode==NULL) { if (msgNode==NULL) {

View File

@@ -82,7 +82,6 @@ int main(int argc, char **argv)
GWEN_Logger_Open(0, "aqhome-tool", 0, GWEN_LoggerType_Console, GWEN_LoggerFacility_User); GWEN_Logger_Open(0, "aqhome-tool", 0, GWEN_LoggerType_Console, GWEN_LoggerFacility_User);
GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Warning);
GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Notice); GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Notice);
rv=AQH_Init(); rv=AQH_Init();