aqhome: finished transformation of aqhome-data and aqhome-tool.

This commit is contained in:
Martin Preuss
2025-03-02 21:48:22 +01:00
parent 2f468e4f78
commit 58c6d12e36
44 changed files with 1279 additions and 1597 deletions

View File

@@ -1,6 +1,6 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
* AqHome (c) by 2025 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
@@ -11,181 +11,124 @@
#endif
#include "./flash.h"
#include "../utils.h"
#include "../client.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/ipc/nodes/msg_ipc_setaccmsggrps.h"
#include "aqhome/ipc/nodes/msg_ipc_forward.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_flashready.h"
#include "aqhome/msg/msg_flashstart.h"
#include "aqhome/msg/msg_flashresponse.h"
#include "aqhome/msg/msg_flashdata.h"
#include "aqhome/msg/msg_flashend.h"
#include "aqhome/msg/msg_reboot.h"
#include "aqhome/msg/ipc/nodes/m_ipcn.h"
#include "aqhome/msg/node/m_node.h"
#include "aqhome/msg/node/m_flashready.h"
#include "aqhome/msg/node/m_flashstart.h"
#include "aqhome/msg/node/m_flashend.h"
#include "aqhome/msg/node/m_flashdata.h"
#include "aqhome/msg/node/m_flashresponse.h"
#include "aqhome/msg/node/m_reboot.h"
#include "aqhome/hexfile/hexfile.h"
#include "aqhome/hexfile/flashrecord.h"
#include <gwenhywfar/args.h>
#include <gwenhywfar/i18n.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
#include <time.h>
#include <unistd.h>
/* ------------------------------------------------------------------------------------------------
* defs
* ------------------------------------------------------------------------------------------------
*/
#define I18S(msg) msg
#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
#define FLASH_TOOL_MAX_REPEAT 16
#define A_ARG GWEN_ARGS_FLAGS_HAS_ARGUMENT
#define A_END (GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST)
#define A_CHAR GWEN_ArgsType_Char
#define A_INT GWEN_ArgsType_Int
/*#define DEBUG_FLASH*/
#define FLASH_TOOL_MAX_REPEAT 16
#define FLASH_TOOL_DEFAULT_TIMEOUTINSECS 5
static int _doFlash(GWEN_DB_NODE *dbArgs);
static AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename);
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static int _rebootNode(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds);
static int _performFlashProcedure(GWEN_MSG_ENDPOINT *epTcp,
static int doFlash(AQH_OBJECT *o);
static int _rebootNode(AQH_OBJECT *o, unsigned int uid, int timeoutInSeconds);
static int _performFlashProcedure(AQH_OBJECT *o,
unsigned int uid,
const AQH_FLASHRECORD_LIST *flashRecordList,
int pageSize,
int timeoutInSeconds);
static int _flashStart(AQH_OBJECT *o, unsigned int uid, int timeoutInSeconds);
static int _flashRecord(AQH_OBJECT *o,
const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize,
int timeoutInSeconds);
static int _flashData(AQH_OBJECT *o, uint32_t address, const uint8_t *ptr, uint32_t len, int timeoutInSeconds);
static int _flashEnd(AQH_OBJECT *o, int reason, int timeoutInSeconds);
static int _waitForFlashResponse(AQH_OBJECT *o, int timeoutInSeconds);
static AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename);
static int _flashStart(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds);
static GWEN_MSG *_waitForFlashReadyMessageForUid(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 *epTcp, int timeoutInSeconds);
static int _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds);
static int _sendFlashRecord(GWEN_MSG_ENDPOINT *epTcp,
const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize,
int timeoutInSeconds);
static int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason, int timeoutInSeconds);
/* ------------------------------------------------------------------------------------------------
* code
* ------------------------------------------------------------------------------------------------
*/
int AQH_Tool_Flash(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
{
GWEN_DB_NODE *dbLocalArgs;
AQH_EVENT_LOOP *eventLoop;
AQH_OBJECT *o;
int rv;
const GWEN_ARGS args[]= {
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Char, /* type */
"tcpAddress", /* name */
0, /* minnum */
1, /* maxnum */
"t", /* short option */
"tcpaddress", /* long option */
I18S("Specify TCP address to connect to (defaults to 127.0.0.1)"),
I18S("Specify TCP address to connect to (defaults to 127.0.0.1)")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Int, /* type */
"tcpPort", /* name */
0, /* minnum */
1, /* maxnum */
"P", /* short option */
"tcpport", /* long option */
I18S("Specify the TCP port to listen on"),
I18S("Specify the TCP port to listen on")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Char, /* type */
"uid", /* name */
1, /* minnum */
1, /* maxnum */
"u", /* short option */
"uid", /* long option */
I18S("Specify UID of the node to flash"),
I18S("Specify UID of the node to flash)")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Char, /* type */
"hexFilename", /* name */
1, /* minnum */
1, /* maxnum */
"H", /* short option */
NULL, /* long option */
I18S("Specify hexfile to flash"),
I18S("Specify hexfile to flash)")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Int, /* type */
"timeout", /* name */
0, /* minnum */
1, /* maxnum */
"T", /* short option */
NULL, /* long option */
I18S("Specify timeout in seconds for PONG response"),
I18S("Specify timeout in seconds for PONG response")
},
{
0, /* flags */
GWEN_ArgsType_Int, /* type */
"reboot", /* name */
0, /* minnum */
1, /* maxnum */
"R", /* short option */
NULL, /* long option */
I18S("Request node reboot before trying to flash"),
I18S("Request node reboot before trying to flash")
},
{
GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
GWEN_ArgsType_Int, /* type */
"help", /* name */
0, /* minnum */
0, /* maxnum */
"h", /* short option */
"help", /* long option */
"Show this help screen", /* short description */
"Show this help screen" /* long description */
}
/* flags type name min max s long short_descr, long_descr */
{ A_ARG, A_CHAR, "tcpAddress", 0, 1, "t", "tcpaddress", I18S("TCP address to connect to [127.0.0.1]"), NULL},
{ A_ARG, A_INT, "tcpPort", 0, 1, "P", "tcpport", I18S("Specify the TCP port to listen on"), NULL},
{ A_ARG, A_INT, "timeout", 0, 1, "T", NULL, I18S("Specify timeout in seconds for response"), NULL},
{ A_ARG, A_CHAR, "userId", 0, 1, "u", "userid", I18S("Specify user id"), NULL},
{ A_ARG, A_CHAR, "password", 0, 1, "p", "password", I18S("Specify service password"), NULL},
{ A_ARG, A_CHAR, "uid", 1, 1, "u", "uid", I18S("Specify UID of the node to flash"),NULL},
{ A_ARG, A_CHAR, "hexFilename", 1, 1, "H", NULL, I18S("Specify hexfile to flash"), NULL},
{ 0, A_INT, "reboot", 0, 1, "R", NULL, I18S("Request node reboot before trying to flash"), NULL},
{ A_END, A_INT, "help", 0, 0, "h", "help", I18S("Show this help screen"), NULL}
};
dbLocalArgs=GWEN_DB_GetGroup(dbGlobalArgs, GWEN_DB_FLAGS_DEFAULT, "local");
rv=GWEN_Args_Check(argc, argv, 1,
GWEN_ARGS_MODE_ALLOW_FREEPARAM,
args,
dbLocalArgs);
if (rv==GWEN_ARGS_RESULT_ERROR) {
fprintf(stderr, "ERROR: Could not parse arguments\n");
return 1;
}
else if (rv==GWEN_ARGS_RESULT_HELP) {
GWEN_BUFFER *ubuf;
eventLoop=AQH_EventLoop_new();
o=AQH_ToolClient_new(eventLoop, AQH_IPC_PROTOCOL_NODES_ID, AQH_IPC_PROTOCOL_NODES_VERSION, dbGlobalArgs, args);
rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv);
if (rv!=0)
return rv;
ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
fprintf(stderr, "ERROR: Could not create help string\n");
return 1;
}
fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
GWEN_Buffer_free(ubuf);
return 0;
rv=AQH_ToolClient_Connect(o, AQH_TOOL_CLIENT_CONNECTFLAGS_WITHCONNECTMSG | AQH_TOOL_CLIENT_CONNECTFLAGS_WITHGRPMSG,
0, AQH_MSG_TYPEGROUP_FLASH);
if (rv<0) {
DBG_INFO(NULL, "here(%d)", rv);
AQH_Object_free(o);
AQH_EventLoop_free(eventLoop);
return rv;
}
return _doFlash(dbLocalArgs);
rv=doFlash(o);
if (rv<0) {
DBG_INFO(NULL, "here(%d)", rv);
AQH_Object_free(o);
AQH_EventLoop_free(eventLoop);
return rv;
}
AQH_Object_free(o);
AQH_EventLoop_free(eventLoop);
return 0;
}
int _doFlash(GWEN_DB_NODE *dbArgs)
int doFlash(AQH_OBJECT *o)
{
GWEN_MSG_ENDPOINT *epTcp;
GWEN_DB_NODE *dbArgs;
int rv;
int timeoutInSeconds;
int doReboot;
@@ -193,17 +136,21 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
unsigned int uid;
const char *hexFilename;
AQH_FLASHRECORD_LIST *flashRecordList=NULL;
GWEN_MSG *msg;
AQH_MESSAGE *msg;
uint16_t pageSize;
dbArgs=AQH_ToolClient_GetDbLocalArgs(o);
/* read data */
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 30);
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, FLASH_TOOL_DEFAULT_TIMEOUTINSECS);
doReboot=GWEN_DB_GetIntValue(dbArgs, "reboot", 0, 0);
s=GWEN_DB_GetCharValue(dbArgs, "uid", 0, NULL);
if (1!=sscanf(s, "%x", &uid)) {
DBG_ERROR(NULL, "Bad uid \"%s\"", s);
return 1;
}
/* read hex file */
hexFilename=GWEN_DB_GetCharValue(dbArgs, "hexFilename", 0, NULL);
flashRecordList=_readHexfileIntoFlashRecordList(hexFilename);
if (flashRecordList==NULL) {
@@ -211,27 +158,12 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
return 2;
}
/* setup client connection */
epTcp=Utils_SetupNodesClientEndpoint(dbArgs);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 3;
}
/* declare accepted message type groups */
rv=Utils_SendAcceptedMsgGroups(epTcp, AQH_MSG_TYPEGROUP_FLASH);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpoint_free(epTcp);
return 3;
}
/* probably reboot node */
if (doReboot) {
fprintf(stdout, "Sending REBOOT request\n");
rv=_rebootNode(epTcp, uid, timeoutInSeconds);
rv=_rebootNode(o, uid, timeoutInSeconds);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpoint_free(epTcp);
return 3;
}
fprintf(stdout, "Reboot in progress\n");
@@ -239,35 +171,258 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
/* wait for FLASH_READY message */
fprintf(stdout, "Waiting for node to become ready for flashing\n");
msg=_waitForFlashReadyMessageForUid(epTcp, uid, timeoutInSeconds);
msg=AQH_ToolClient_WaitForNodeMsg(o, 0, AQH_MSG_TYPE_FLASH_READY, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No FLASH_READY message received.");
GWEN_MsgEndpoint_free(epTcp);
return 3;
}
DBG_INFO(NULL, "FLASH_READY message received");
pageSize=AQH_FlashReadyMsg_GetPagesize(msg);
pageSize=AQH_FlashReadyMessage_GetPagesize(msg);
fprintf(stdout, "Node is ready for flashing (pagesize=%d bytes)\n", pageSize);
GWEN_Msg_free(msg);
AQH_Message_free(msg);
/* perform flash */
rv=_performFlashProcedure(epTcp, uid, flashRecordList, pageSize, timeoutInSeconds);
rv=_performFlashProcedure(o, uid, flashRecordList, pageSize, timeoutInSeconds);
if (rv<0) {
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpoint_free(epTcp);
AQH_FlashRecord_List_free(flashRecordList);
return 4;
}
}
AQH_FlashRecord_List_free(flashRecordList);
GWEN_MsgEndpoint_free(epTcp);
return 0;
}
int _rebootNode(AQH_OBJECT *o, unsigned int uid, int timeoutInSeconds)
{
AQH_MESSAGE *nodeMsg;
/* send REBOOT_REQUEST message */
fprintf(stdout, "- sending REBOOT request\n");
DBG_INFO(NULL, "Sending REBOOT REQUEST message");
nodeMsg=AQH_RebootMessage_new(0, 0xff, AQH_MSG_TYPE_REBOOT_REQ, uid);
if (nodeMsg==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
AQH_ToolClient_SendNodeMsg(o, nodeMsg);
nodeMsg=AQH_ToolClient_WaitForNodeMsg(o, 0, AQH_MSG_TYPE_REBOOT_RSP, timeoutInSeconds);
if (nodeMsg==NULL) {
DBG_INFO(NULL, "Bad or no reboot response received.");
return GWEN_ERROR_GENERIC;
}
DBG_INFO(NULL, "REBOOT_RESPONSE message received");
AQH_Message_free(nodeMsg);
return 0;
}
int _performFlashProcedure(AQH_OBJECT *o,
unsigned int uid,
const AQH_FLASHRECORD_LIST *flashRecordList,
int pageSize,
int timeoutInSeconds)
{
int rv;
const AQH_FLASHRECORD *flashRecord;
rv=_flashStart(o, uid, timeoutInSeconds);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
flashRecord=AQH_FlashRecord_List_First(flashRecordList);
while(flashRecord) {
DBG_ERROR(NULL, "Sending flash record at %08x", AQH_FlashRecord_GetAddress(flashRecord));
rv=_flashRecord(o, flashRecord, pageSize, timeoutInSeconds);
if (rv!=0) {
DBG_ERROR(NULL, "Error sending flash data (%d)", rv);
return rv;
}
flashRecord=AQH_FlashRecord_List_Next(flashRecord);
}
rv=_flashEnd(o, 0, timeoutInSeconds);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
DBG_ERROR(NULL, "Flash finished.");
return 0;
}
int _flashStart(AQH_OBJECT *o, unsigned int uid, int timeoutInSeconds)
{
int i;
for (i=0; i<FLASH_TOOL_MAX_REPEAT; i++) {
AQH_MESSAGE *nodeMsg;
int rv;
if (i)
fprintf(stdout, "- sending FLASH_START (retry %d)\n", i);
else
fprintf(stdout, "- sending FLASH_START\n");
/* send FLASH_START message */
DBG_INFO(NULL, "Sending FLASH START message (%d)", i);
nodeMsg=AQH_FlashStartMessage_new(0, 0xc1, AQH_MSG_TYPE_FLASH_START, uid);
if (nodeMsg==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
AQH_ToolClient_SendNodeMsg(o, nodeMsg);
rv=_waitForFlashResponse(o, timeoutInSeconds);
if (rv==0) {
return 0;
}
}
DBG_INFO(NULL, "No FLASH_RESPONSE message received");
return GWEN_ERROR_GENERIC;
}
int _flashRecord(AQH_OBJECT *o,
const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize,
int timeoutInSeconds)
{
const uint8_t *ptr;
uint32_t len;
uint32_t address;
ptr=AQH_FlashRecord_GetDataPointer(flashRecord);
len=AQH_FlashRecord_GetDataLength(flashRecord);
address=AQH_FlashRecord_GetAddress(flashRecord);
DBG_ERROR(NULL, "Sending record: addr=%04x, len=%d, pagesize=%d", address, len, pageSize);
while(ptr && len) {
int rv;
uint32_t sendLen;
usleep(100000);
sendLen=(len>pageSize)?pageSize:len;
rv=_flashData(o, address, ptr, sendLen, timeoutInSeconds);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
ptr+=sendLen;
address+=sendLen;
len-=sendLen;
} /* while */
DBG_ERROR(NULL, "Full record sent and acknowledged");
return 0;
}
int _flashData(AQH_OBJECT *o, uint32_t address, const uint8_t *ptr, uint32_t len, int timeoutInSeconds)
{
int i;
for (i=0;i<FLASH_TOOL_MAX_REPEAT;i++) {
AQH_MESSAGE *nodeMsg;
int rv;
#ifdef DEBUG_FLASH
DBG_ERROR(NULL, " Sending message: addr=%04x, data len=%d, pagesize=%d", address, len, pageSize);
GWEN_Text_LogString((const char*) ptr, len, NULL, GWEN_LoggerLevel_Error);
#endif
if (i)
fprintf(stdout, "- send data: dest addr=%04x, len=%d bytes (retry %d)\n", address, len, i);
else
fprintf(stdout, "- send data: dest addr=%04x, len=%d bytes\n", address, len);
nodeMsg=AQH_FlashDataMessage_new(0, 0xc1, AQH_MSG_TYPE_FLASH_DATA, address, ptr, len);
if (nodeMsg==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
AQH_ToolClient_SendNodeMsg(o, nodeMsg);
rv=_waitForFlashResponse(o, timeoutInSeconds);
if (rv==0)
return 0;
} /* for */
DBG_INFO(NULL, "No FLASH_RESPONSE message received");
return GWEN_ERROR_GENERIC;
}
int _flashEnd(AQH_OBJECT *o, int reason, int timeoutInSeconds)
{
int i;
for (i=0; i<FLASH_TOOL_MAX_REPEAT; i++) {
AQH_MESSAGE *nodeMsg;
int rv;
if (i)
fprintf(stdout, "- sending FLASH_END (retry %d)\n", i);
else
fprintf(stdout, "- sending FLASH_END\n");
/* send FLASH_START message */
DBG_INFO(NULL, "Sending FLASH END message (%d)", i);
nodeMsg=AQH_FlashEndMessage_new(0, 0xc1, AQH_MSG_TYPE_FLASH_END, reason);
if (nodeMsg==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
AQH_ToolClient_SendNodeMsg(o, nodeMsg);
rv=_waitForFlashResponse(o, timeoutInSeconds);
if (rv==0)
return 0;
}
DBG_INFO(NULL, "No FLASH_RESPONSE message received");
return GWEN_ERROR_GENERIC;
}
int _waitForFlashResponse(AQH_OBJECT *o, int timeoutInSeconds)
{
AQH_MESSAGE *nodeMsg;
nodeMsg=AQH_ToolClient_WaitForNodeMsg(o, 0, AQH_MSG_TYPE_FLASH_RSP, timeoutInSeconds);
if (nodeMsg==NULL) {
DBG_INFO(NULL, "Bad or no flash response received.");
return GWEN_ERROR_GENERIC;
}
else {
uint8_t result;
result=AQH_FlashResponseMessage_GetCode(nodeMsg);
AQH_Message_free(nodeMsg);
if (AQH_FlashResponseMessage_GetCode(nodeMsg)!=0) {
DBG_ERROR(NULL, "Negative FLASH_RESPONSE received (%d)", result);
return GWEN_ERROR_GENERIC;
}
else {
DBG_INFO(NULL, "FLASH_RESPONSE message received");
return 0;
}
}
DBG_INFO(NULL, "No FLASH_RESPONSE message received");
return GWEN_ERROR_GENERIC;
}
AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename)
{
AQH_HEXFILE *hexfile;
@@ -293,327 +448,4 @@ AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename)
int _rebootNode(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(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 *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(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(epTcp, flashRecord, pageSize, timeoutInSeconds);
if (rv!=0) {
DBG_ERROR(NULL, "Error sending flash data (%d)", rv);
return rv;
}
flashRecord=AQH_FlashRecord_List_Next(flashRecord);
}
rv=_sendFlashEnd(epTcp, 0, timeoutInSeconds);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
rv=Utils_FlushOutMessageQueue(epTcp, timeoutInSeconds);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
return 0;
}
int _flashStart(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(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 *epTcp, unsigned int uid, int timeoutInSeconds)
{
int i;
for (i=0;i<16;i++) {
GWEN_MSG *msg;
unsigned int receivedUid;
msg=Utils_WaitForSpecificNodeMessage(epTcp, AQH_MSG_TYPE_FLASH_READY, 0, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No FLASH_READY message received.");
return NULL;
}
receivedUid=AQH_FlashReadyMsg_GetUid(msg);
if (receivedUid==uid) {
DBG_INFO(NULL, "Received FLASH_READY message for given uid");
return msg;
}
else {
DBG_INFO(NULL, "Received FLASH_READY message but not for given uid (%08x)", receivedUid);
}
GWEN_Msg_free(msg);
}
return NULL;
}
int _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
{
GWEN_MSG *msg;
msg=Utils_WaitForSpecificNodeMessage(epTcp, AQH_MSG_TYPE_REBOOT_RSP, 0, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No REBOOT_RSP message received.");
return GWEN_ERROR_IO;
}
GWEN_Msg_free(msg);
return 0;
}
int _waitForFlashResponseMessage(GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
{
GWEN_MSG *msg;
int responseCode;
msg=Utils_WaitForSpecificNodeMessage(epTcp, AQH_MSG_TYPE_FLASH_RSP, 0, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No FLASH_RSP message received.");
return GWEN_ERROR_IO;
}
responseCode=AQH_FlashResponseMsg_GetCode(msg);
GWEN_Msg_free(msg);
return responseCode;
}
int _sendRebootRequest(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid)
{
GWEN_MSG *msgNode;
GWEN_MSG *msgOut;
msgNode=AQH_RebootRequestMsg_new(0, 0xff, AQH_MSG_TYPE_REBOOT_REQ, uid);
if (msgNode==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
msgOut=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_NODES_FORWARD,
GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0,
GWEN_Msg_GetConstBuffer(msgNode), GWEN_Msg_GetBytesInBuffer(msgNode));
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_Msg_free(msgNode);
return 0;
}
int _sendFlashStart(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid)
{
GWEN_MSG *msgNode;
GWEN_MSG *msgOut;
msgNode=AQH_FlashStartMsg_new(0, 0xc1, AQH_MSG_TYPE_FLASH_START, uid);
if (msgNode==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
msgOut=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_NODES_FORWARD,
GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0,
GWEN_Msg_GetConstBuffer(msgNode), GWEN_Msg_GetBytesInBuffer(msgNode));
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_Msg_free(msgNode);
return 0;
}
int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason, int timeoutInSeconds)
{
int i;
for (i=0;i<FLASH_TOOL_MAX_REPEAT;i++) {
GWEN_MSG *msgNode;
GWEN_MSG *msgOut;
int rv;
fprintf(stdout, "Sending end\n");
usleep(100000);
msgNode=AQH_FlashEndMsg_new(0, 0xc1, AQH_MSG_TYPE_FLASH_END, reason);
if (msgNode==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
msgOut=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_NODES_FORWARD,
GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0,
GWEN_Msg_GetConstBuffer(msgNode), GWEN_Msg_GetBytesInBuffer(msgNode));
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_Msg_free(msgNode);
rv=_waitForFlashResponseMessage(epTcp, timeoutInSeconds);
if (rv==0) {
fprintf(stdout, "-> ACK received.\n");
break;
}
else {
DBG_ERROR(NULL, "Negative response to flash end message (%d)", rv);
}
} /* for */
if (i>=FLASH_TOOL_MAX_REPEAT) {
DBG_ERROR(NULL, "Too many errors (tried %d times), giving up", i);
return GWEN_ERROR_IO;
}
return 0;
}
int _sendFlashRecord(GWEN_MSG_ENDPOINT *epTcp,
const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize,
int timeoutInSeconds)
{
const uint8_t *ptr;
uint32_t len;
uint32_t address;
ptr=AQH_FlashRecord_GetDataPointer(flashRecord);
len=AQH_FlashRecord_GetDataLength(flashRecord);
address=AQH_FlashRecord_GetAddress(flashRecord);
while(ptr && len) {
uint32_t sendLen;
int i;
sendLen=(len>pageSize)?pageSize:len;
for (i=0;i<FLASH_TOOL_MAX_REPEAT;i++) {
GWEN_MSG *msgNode;
GWEN_MSG *msgOut;
int rv;
#ifdef DEBUG_FLASH
DBG_ERROR(NULL, " Sending message: addr=%04x, data len=%d, pagesize=%d", address, sendLen, pageSize);
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);
usleep(100000);
msgNode=AQH_FlashDataMsg_new(0, 0xc1, AQH_MSG_TYPE_FLASH_DATA, address, ptr, sendLen);
if (msgNode==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
msgOut=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_NODES_FORWARD,
GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0,
GWEN_Msg_GetConstBuffer(msgNode), GWEN_Msg_GetBytesInBuffer(msgNode));
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_Msg_free(msgNode);
rv=_waitForFlashResponseMessage(epTcp, timeoutInSeconds);
if (rv==0) {
fprintf(stdout, "-> ACK received.\n");
break;
}
else {
DBG_ERROR(NULL, "Negative response to flash data message (%d)", rv);
}
} /* for */
if (i>=FLASH_TOOL_MAX_REPEAT) {
DBG_ERROR(NULL, "Too many errors (tried %d times), giving up", i);
/* TODO: send flash end message */
return GWEN_ERROR_IO;
}
ptr+=sendLen;
address+=sendLen;
len-=sendLen;
} /* while */
return 0;
}