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