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,13 +45,28 @@
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,
static int _sendFlashRecord(GWEN_MSG_ENDPOINT_MGR *emgr,
GWEN_MSG_ENDPOINT *epTcp,
const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize,
@@ -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,26 +209,11 @@ 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);
flashRecordList=_readHexfileIntoFlashRecordList(hexFilename);
if (flashRecordList==NULL) {
DBG_ERROR(NULL, "Error reading flash record list from hexfile");
return 2;
}
}
/* setup client connection */
emgr=GWEN_MsgEndpointMgr_new();
@@ -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);
return 3;
AQH_FlashRecord_List_free(flashRecordList);
return 4;
}
}
/* wait for response */
rv=_waitForFlashResponseMessage(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, "FLASH_RESPONSE message received");
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);
if (rv!=0) {
DBG_ERROR(NULL, "Error sending flash data (%d)", rv);
AQH_FlashRecord_List_free(flashRecordList);
GWEN_MsgEndpointMgr_free(emgr);
return 3;
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);
return rv;
}
rv=_waitForRebootResponseMessage(emgr, epTcp, timeoutInSeconds);
if (rv!=0) {
DBG_INFO(NULL, "Bad or no reboot response received (%d).", rv);
return rv;
}
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) {
DBG_ERROR(NULL, "Sending flash record at %08x", AQH_FlashRecord_GetAddress(flashRecord));
rv=_sendFlashRecord(emgr, epTcp, flashRecord, pageSize, timeoutInSeconds);
if (rv!=0) {
DBG_ERROR(NULL, "Error sending flash data (%d)", rv);
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<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,
unsigned int uid, int timeoutInSeconds)
{
@@ -435,7 +523,7 @@ 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,
const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize,
@@ -463,6 +551,9 @@ int _sendFlashData(GWEN_MSG_ENDPOINT_MGR *emgr,
GWEN_Text_LogString((const char*) ptr, sendLen, NULL, GWEN_LoggerLevel_Error);
#endif
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);

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_SetLevel(0, GWEN_LoggerLevel_Warning);
GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Notice);
rv=AQH_Init();