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

@@ -36,7 +36,6 @@
ping.h
flash.h
getnodes.h
setnodevalue.h
</headers>
<sources>
@@ -45,7 +44,6 @@
ping.c
flash.c
getnodes.c
setnodevalue.c
</sources>
<useTargets>

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;
}

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,213 +11,156 @@
#endif
#include "./getnodes.h"
#include "../client.h"
#include "../utils.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/ipc/nodes/msg_ipc_getdevices_req.h"
#include "aqhome/ipc/nodes/msg_ipc_getdevices_rsp.h"
#include "aqhome/ipc/msg_ipc_result.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/ipc/m_ipc.h"
#include "aqhome/msg/ipc/m_ipc_result.h"
#include "aqhome/msg/ipc/nodes/m_ipcn.h"
#include "aqhome/msg/ipc/nodes/m_ipcn_getdevices_req.h"
#include "aqhome/msg/ipc/nodes/m_ipcn_getdevices_rsp.h"
#include <aqhome/nodes/nodeinfo.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/i18n.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
/* ------------------------------------------------------------------------------------------------
* defs
* ------------------------------------------------------------------------------------------------
*/
#define I18S(msg) msg
#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
#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
static int _doGetNodes(GWEN_DB_NODE *dbArgs);
static int _sendGetDevices(GWEN_MSG_ENDPOINT *epTcp);
static void _printNodeFromMsg(const GWEN_MSG *msg);
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId);
static int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first);
static void _printNode(const AQH_NODE_INFO *ni);
static void _printUintAsTextOrHex(uint32_t u, int bits);
/* ------------------------------------------------------------------------------------------------
* code
* ------------------------------------------------------------------------------------------------
*/
int AQH_Tool_GetNodes(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_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")
},
{
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("TCP port to connect to"), NULL},
{ A_ARG, A_INT, "timeout", 0, 1, "T", NULL, I18S("Specify timeout in seconds for response"), NULL},
{ 0, A_INT, "printHeader", 0, 1, "H", "printheader", I18S("Print header if given"), 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);
AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage);
AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage);
rv=AQH_ToolClient_ReadLocalArgs(o, argc, argv);
if (rv!=0)
return rv;
rv=AQH_ToolClient_Run(o);
AQH_Object_free(o);
AQH_EventLoop_free(eventLoop);
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");
AQH_MESSAGE *_createRequestMessage(GWEN_UNUSED AQH_OBJECT *o, uint32_t msgId)
{
return AQH_IpcnMessageGetDevicesReq_new(AQH_MSGTYPE_IPC_NODES_GETDEVICES_REQ, msgId, 0);
}
int _handleResponseMessage(GWEN_UNUSED AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first)
{
uint16_t code;
//GWEN_DB_NODE *dbArgs;
//int printHeader;
//dbArgs=AQH_ToolClient_GetDbLocalArgs(o);
//printHeader=first?GWEN_DB_GetIntValue(dbArgs, "printHeader", 0, 0):0;
code=AQH_IpcMessage_GetCode(msg);
if (code==AQH_MSGTYPE_IPC_NODES_GETDEVICES_RSP) {
AQH_NODE_INFO *ni;
ni=AQH_IpcnMessageGetDevicesRsp_ReadNodeInfo(tagList);
if (ni)
_printNode(ni);
if (AQH_IpcnMessageGetDevicesRsp_GetFlags(tagList) & AQH_MSGNODE_GETDEVICES_RSP_FLAGS_LASTMSG) {
DBG_INFO(NULL, "Last message received");
return 1;
}
fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
GWEN_Buffer_free(ubuf);
return 0;
}
return _doGetNodes(dbLocalArgs);
else if (code==AQH_MSGTYPE_IPC_NODES_RESULT)
return AQH_ToolClient_HandleResultMsg(msg, tagList, first);
else {
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
return 3;
}
}
int _doGetNodes(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT *epTcp;
int rv;
int timeoutInSeconds;
epTcp=Utils_SetupNodesClientEndpoint(dbArgs);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 2;
}
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
rv=Utils_SendAcceptedMsgGroups(epTcp, 0);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return 2;
}
rv=_sendGetDevices(epTcp);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return 2;
}
for (;;) {
GWEN_MSG *msg;
uint16_t code;
msg=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_NODES_GETDEVICES_RSP, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No GET_DEVICE response received.");
GWEN_MsgEndpoint_free(epTcp);
return 2;
}
code=GWEN_IpcMsg_GetCode(msg);
if (code==AQH_MSGTYPE_IPC_NODES_RESULT) {
fprintf(stdout, "No device list (%d)\n", AQH_ResultIpcMsg_GetResultCode(msg));
GWEN_Msg_free(msg);
break;
}
else if (code==AQH_MSGTYPE_IPC_NODES_GETDEVICES_RSP) {
uint8_t flags;
flags=AQH_GetDevicesResponseIpcMsg_GetFlags(msg);
_printNodeFromMsg(msg);
GWEN_Msg_free(msg);
if (flags & AQH_MSGIPC_GETDEVICES_RSP_FLAGS_LAST) {
DBG_INFO(NULL, "Last");
break;
}
}
else {
GWEN_Msg_free(msg);
return GWEN_ERROR_GENERIC;
}
}
GWEN_MsgEndpoint_free(epTcp);
return 0;
}
void _printNodeFromMsg(const GWEN_MSG *msg)
void _printNode(const AQH_NODE_INFO *ni)
{
uint32_t u;
int64_t ts64;
GWEN_TIMESTAMP *ts;
ts64=AQH_GetDevicesResponseIpcMsg_GetTimestamp(msg);
ts=GWEN_Timestamp_fromInt64(ts64); /* TODO: fix timestamp */
const GWEN_TIMESTAMP *ts;
fprintf(stdout, "- node: addr=%d, uid=0x%08x, device=",
AQH_GetDevicesResponseIpcMsg_GetBusAddress(msg),
(unsigned int) AQH_GetDevicesResponseIpcMsg_GetUid(msg));
u=AQH_GetDevicesResponseIpcMsg_GetManufacturer(msg);
AQH_NodeInfo_GetBusAddress(ni),
(unsigned int) AQH_NodeInfo_GetUid(ni));
ts=AQH_NodeInfo_GetTimestampLastChange(ni);
u=AQH_NodeInfo_GetManufacturer(ni);
_printUintAsTextOrHex(u, 32);
fprintf(stdout, ":");
u=AQH_GetDevicesResponseIpcMsg_GetDeviceType(msg);
u=AQH_NodeInfo_GetDeviceType(ni);
_printUintAsTextOrHex(u, 16);
u=AQH_GetDevicesResponseIpcMsg_GetDeviceVersion(msg);
u=AQH_NodeInfo_GetDeviceVersion(ni);
fprintf(stdout, " v%d.%d", (u>>8) & 0xff, u & 0xff);
u=AQH_GetDevicesResponseIpcMsg_GetFirmwareVersion(msg);
u=AQH_NodeInfo_GetFirmwareVersion(ni);
fprintf(stdout, ", firmware=%d.%d.%d (%d), ",
(u>>16) & 0xff, (u>>8) & 0xff, u & 0xff, (u>>24) & 0xff);
if (ts) {
if (ts)
fprintf(stdout, "last seen %s, ", GWEN_Timestamp_GetString(ts));
GWEN_Timestamp_free(ts);
}
fprintf(stdout, "pkg out: %d, pkg in: %d, collisions: %d, busy: %d, crc: %d, io: %d\n",
AQH_GetDevicesResponseIpcMsg_GetPkgOut(msg),
AQH_GetDevicesResponseIpcMsg_GetPkgIn(msg),
AQH_GetDevicesResponseIpcMsg_GetCollisions(msg),
AQH_GetDevicesResponseIpcMsg_GetBusy(msg),
AQH_GetDevicesResponseIpcMsg_GetCrcErrors(msg),
AQH_GetDevicesResponseIpcMsg_GetIoErrors(msg));
AQH_NodeInfo_GetStatsPacketsOut(ni),
AQH_NodeInfo_GetStatsPacketsIn(ni),
AQH_NodeInfo_GetStatsCollisions(ni),
AQH_NodeInfo_GetStatsBusy(ni),
AQH_NodeInfo_GetStatsCrcErrors(ni),
AQH_NodeInfo_GetStatsIoErrors(ni));
}
@@ -250,22 +193,3 @@ void _printUintAsTextOrHex(uint32_t u, int bits)
}
int _sendGetDevices(GWEN_MSG_ENDPOINT *epTcp)
{
GWEN_MSG *msgOut;
msgOut=AQH_GetDevicesRequestIpcMsg_new(AQH_MSGTYPE_IPC_NODES_GETDEVICES_REQ,
GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0);
if (msgOut==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
return 0;
}

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,128 @@
#endif
#include "./ping.h"
#include "../client.h"
#include "../utils.h"
#include "aqhome/ipc/nodes/msg_ipc_setaccmsggrps.h"
#include "aqhome/ipc/nodes/msg_ipc_ping.h"
#include "aqhome/ipc/nodes/msg_ipc_forward.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/ipc/m_ipc.h"
#include "aqhome/msg/ipc/m_ipc_result.h"
#include "aqhome/msg/ipc/nodes/m_ipcn.h"
#include "aqhome/msg/ipc/nodes/m_ipcn_forward.h"
#include "aqhome/msg/node/m_node.h"
#include "aqhome/msg/node/m_ping.h"
#include "aqhome/msg/node/m_pong.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 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
static int _doPing(GWEN_DB_NODE *dbArgs);
static int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr);
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId);
static int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
int AQH_Tool_Ping(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_Int, /* type */
"nodeAddr", /* name */
1, /* minnum */
1, /* maxnum */
"n", /* short option */
"nodeaddr", /* long option */
I18S("Specify bus addr of the node to ping"),
I18S("Specify bus addr of the node to ping")
},
{
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")
},
{
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_INT, "nodeAddr", 1, 1, "n", "nodeaddr", I18S("Specify bus addr of the node to ping"), 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);
AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage);
AQH_ToolClient_SetHandleResponseMessageFn(o, _handleResponseMessage);
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);
rv=AQH_ToolClient_Connect(o, AQH_TOOL_CLIENT_CONNECTFLAGS_WITHCONNECTMSG | AQH_TOOL_CLIENT_CONNECTFLAGS_WITHGRPMSG,
0, AQH_MSG_TYPEGROUP_ALL);
if (rv<0) {
DBG_INFO(NULL, "here(%d)", rv);
AQH_Object_free(o);
AQH_EventLoop_free(eventLoop);
return rv;
}
rv=AQH_ToolClient_RunConnectedWithNodeMsgs(o);
AQH_Object_free(o);
AQH_EventLoop_free(eventLoop);
return rv;
}
AQH_MESSAGE *_createRequestMessage(GWEN_UNUSED AQH_OBJECT *o, GWEN_UNUSED uint32_t msgId)
{
AQH_MESSAGE *nodeMsg;
GWEN_DB_NODE *dbArgs;
int nodeAddr;
dbArgs=AQH_ToolClient_GetDbLocalArgs(o);
nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0);
nodeMsg=AQH_PingMessage_new(nodeAddr, AQH_TOOL_CLIENT_NODEADDR, AQH_MSG_TYPE_PING, 0);
return nodeMsg;
}
int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, GWEN_UNUSED const GWEN_TAG16_LIST *tagList, GWEN_UNUSED int first)
{
GWEN_DB_NODE *dbArgs;
uint16_t code;
int nodeAddr;
dbArgs=AQH_ToolClient_GetDbLocalArgs(o);
nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0);
code=AQH_NodeMessage_GetMsgType(msg);
if ((code==AQH_MSG_TYPE_PONG) &&
(nodeAddr==0 || nodeAddr==0xff || nodeAddr==AQH_NodeMessage_GetSourceAddress(msg))) {
return 0;
}
else {
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
return 3;
}
return _doPing(dbLocalArgs);
}
int _doPing(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT *epTcp;
int rv;
int nodeAddr;
int timeoutInSeconds;
GWEN_MSG *msg;
epTcp=Utils_SetupNodesClientEndpoint(dbArgs);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 2;
}
nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0);
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
rv=Utils_SendAcceptedMsgGroups(epTcp, AQH_MSG_TYPEGROUP_ALL);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return 2;
}
fprintf(stdout, "Sending PING request\n");
rv=_sendPing(epTcp, nodeAddr);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return 2;
}
msg=Utils_WaitForSpecificNodeMessage(epTcp, AQH_MSG_TYPE_PONG, nodeAddr, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No PONG response received.");
return 2;
}
fprintf(stdout, "PONG response received\n");
GWEN_MsgEndpoint_free(epTcp);
return 0;
}
int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr)
{
GWEN_MSG *msgOut;
msgOut=AQH_PingIpcMsg_new(AQH_MSGTYPE_IPC_NODES_PING, GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0, nodeAddr);
if (msgOut==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
return 0;
}

View File

@@ -1,297 +0,0 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2024 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.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "./setnodevalue.h"
#include "../utils.h"
#include "aqhome/ipc/nodes/msg_ipc_setaccmsggrps.h"
#include "aqhome/ipc/nodes/msg_ipc_ping.h"
#include "aqhome/ipc/nodes/msg_ipc_forward.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_value3.h"
#include <gwenhywfar/args.h>
#include <gwenhywfar/i18n.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
#include <time.h>
#include <unistd.h>
#define I18S(msg) msg
#define I18N(msg) GWEN_I18N_Translate(PACKAGE, msg)
static int _doSetValue(GWEN_DB_NODE *dbArgs);
static GWEN_MSG *_waitForSetValueResponse(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr, int msgId, int timeoutInSeconds);
int AQH_Tool_SetNodeValue(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
{
GWEN_DB_NODE *dbLocalArgs;
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_Int, /* type */
"nodeAddr", /* name */
1, /* minnum */
1, /* maxnum */
"n", /* short option */
"nodeaddr", /* long option */
I18S("Specify bus addr of the node to send to"),
I18S("Specify bus addr of the node to send to")
},
{
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 response"),
I18S("Specify timeout in seconds for response")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Int, /* type */
"valueId", /* name */
1, /* minnum */
1, /* maxnum */
"V", /* short option */
"valueId", /* long option */
I18S("Specify id of value to change"),
I18S("Specify id of value to change")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Int, /* type */
"value", /* name */
1, /* minnum */
1, /* maxnum */
"v", /* short option */
"value", /* long option */
I18S("Specify value to send"),
I18S("Specify value to send")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Int, /* type */
"denom", /* name */
0, /* minnum */
1, /* maxnum */
"d", /* short option */
"denom", /* long option */
I18S("Specify denominator to send"),
I18S("Specify denominator to send")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Int, /* type */
"msgId", /* name */
0, /* minnum */
1, /* maxnum */
"m", /* short option */
"msgId", /* long option */
I18S("Specify a msg id"),
I18S("Specify a msg id")
},
{
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 */
}
};
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;
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;
}
return _doSetValue(dbLocalArgs);
}
int _doSetValue(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT *epTcp;
int rv;
int nodeAddr;
int timeoutInSeconds;
int valueId;
int value;
int denom;
int msgId;
GWEN_MSG *msgNode;
GWEN_MSG *msgOut;
GWEN_MSG *msg;
epTcp=Utils_SetupNodesClientEndpoint(dbArgs);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 2;
}
nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0);
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
valueId=GWEN_DB_GetIntValue(dbArgs, "valueId", 0, 0);
msgId=GWEN_DB_GetIntValue(dbArgs, "msgId", 0, 0);
value=GWEN_DB_GetIntValue(dbArgs, "value", 0, 0);
denom=GWEN_DB_GetIntValue(dbArgs, "denom", 0, 1);
rv=Utils_SendAcceptedMsgGroups(epTcp, AQH_MSG_TYPEGROUP_ALL);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return 2;
}
fprintf(stdout, "Sending SETVALUE request\n");
msgNode=AQH_Value3Msg_new(0xc1, nodeAddr, AQH_MSG_TYPE_VALUE_SET, msgId, valueId, value, denom);
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));
if (msgOut==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
msg=_waitForSetValueResponse(epTcp, nodeAddr, msgId, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No SETVALUE response received.");
return 2;
}
if (AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE_SET_ACK) {
fprintf(stdout, "Value set\n");
}
else {
fprintf(stdout, "Value not set\n");
}
GWEN_Msg_free(msg);
GWEN_MsgEndpoint_free(epTcp);
return 0;
}
GWEN_MSG *_waitForSetValueResponse(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr, int msgId, int timeoutInSeconds)
{
time_t startTime;
startTime=time(NULL);
for (;;) {
GWEN_MSG *msg;
time_t now;
while( (msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp)) ) {
if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_NODES_FORWARD) {
GWEN_MSG *nodeMsg;
DBG_INFO(NULL, "Received IPC FORWARD message");
nodeMsg=AQH_ForwardIpcMsg_GetCopyOfNodeMsg(msg);
if (nodeMsg) {
int recvNodeAddr;
int recvMsgType;
recvNodeAddr=AQH_NodeMsg_GetSourceAddress(nodeMsg);
recvMsgType=AQH_NodeMsg_GetMsgType(nodeMsg);
DBG_INFO(AQH_LOGDOMAIN, "Received node msg from %d (%d)", recvNodeAddr, recvNodeAddr);
if ((nodeAddr==0 || recvNodeAddr==nodeAddr) &&
(recvMsgType==AQH_MSG_TYPE_VALUE_SET_ACK || recvMsgType==AQH_MSG_TYPE_VALUE_SET_NACK)) {
int recvMsgId;
recvMsgId=AQH_Value3Msg_GetMsgId(nodeMsg);
if (recvMsgId==msgId) {
GWEN_Msg_free(msg);
return nodeMsg;
}
}
}
}
else {
DBG_INFO(NULL, "Received IPC message %d, ignoring", GWEN_IpcMsg_GetCode(msg));
}
GWEN_Msg_free(msg);
} /* while */
now=time(NULL);
if (now-startTime>timeoutInSeconds) {
DBG_INFO(NULL, "Timeout");
break;
}
GWEN_MsgEndpoint_IoLoop(epTcp, 2000); /* 2000 ms */
}
return NULL;
}

View File

@@ -1,20 +0,0 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2024 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.
****************************************************************************/
#ifndef AQHOME_TOOL_SETNODEVALUE_H
#define AQHOME_TOOL_SETNODEVALUE_H
#include <gwenhywfar/db.h>
int AQH_Tool_SetNodeValue(GWEN_DB_NODE *dbArgs, int argc, char **argv);
#endif