implemented ipc messages getdevices req/rsp

This commit is contained in:
Martin Preuss
2023-04-22 19:13:59 +02:00
parent 1893d50908
commit 5f7e192e27
16 changed files with 782 additions and 4 deletions

View File

@@ -35,6 +35,7 @@
<headers dist="true" >
ping.h
flash.h
getdevices.h
utils.h
</headers>
@@ -43,6 +44,7 @@
main.c
ping.c
flash.c
getdevices.c
utils.c
</sources>

View File

@@ -0,0 +1,220 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 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 "./getdevices.h"
#include "./utils.h"
#include "aqhome/ipc/msg_ipc_getdevices_req.h"
#include "aqhome/ipc/msg_ipc_getdevices_rsp.h"
#include "aqhome/ipc/msg_ipc_error.h"
#include "aqhome/ipc/endpoint_ipc_tcpc.h"
#include "aqhome/msg/msg_node.h"
#include <gwenhywfar/args.h>
#include <gwenhywfar/i18n.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint_tcpc.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 _doGetDevices(GWEN_DB_NODE *dbArgs);
static int _sendGetDevices(GWEN_MSG_ENDPOINT *epTcp);
int AQH_Tool_GetDevices(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 */
"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 */
}
};
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 _doGetDevices(dbLocalArgs);
}
int _doGetDevices(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT_MGR *emgr;
GWEN_MSG_ENDPOINT *epTcp;
int rv;
int timeoutInSeconds;
emgr=GWEN_MsgEndpointMgr_new();
epTcp=Utils_SetupIpcEndpoint(emgr, dbArgs);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 2;
}
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTcp);
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(emgr, epTcp, AQH_MSGTYPE_IPC_GETDEVICES_RSP, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No GET_DEVICE response received.");
GWEN_MsgEndpointMgr_free(emgr);
return 2;
}
code=GWEN_IpcMsg_GetCode(msg);
if (code==AQH_MSGTYPE_IPC_ERROR) {
fprintf(stdout, "No device list (%d)\n", AQH_ErrorIpcMsg_GetErrorCode(msg));
GWEN_Msg_free(msg);
break;
}
else if (code==AQH_MSGTYPE_IPC_GETDEVICES_RSP) {
uint16_t fwVersion;
int64_t ts64;
GWEN_TIMESTAMP *ts;
uint8_t flags;
flags=AQH_GetDevicesResponseIpcMsg_GetFlags(msg);
fwVersion=AQH_GetDevicesResponseIpcMsg_GetFirmwareVersion(msg);
ts64=AQH_GetDevicesResponseIpcMsg_GetTimestamp(msg);
ts=GWEN_Timestamp_fromInt64(ts64); /* TODO: fix timestamp */
fprintf(stdout, "- node: %02x (uid=%04x, firmware type=%02x, firmware version=%d.%d, timeStamp: %016lx)\n",
AQH_GetDevicesResponseIpcMsg_GetBusAddress(msg),
AQH_GetDevicesResponseIpcMsg_GetUid(msg),
AQH_GetDevicesResponseIpcMsg_GetFirmwareType(msg),
(fwVersion>>8) & 0xff,
fwVersion & 0xff,
(unsigned long int)ts64);
GWEN_Timestamp_free(ts);
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_MsgEndpointMgr_free(emgr);
return 0;
}
int _sendGetDevices(GWEN_MSG_ENDPOINT *epTcp)
{
GWEN_MSG *msgOut;
msgOut=AQH_GetDevicesRequestIpcMsg_new(AQH_MSGTYPE_IPC_GETDEVICES_REQ);
if (msgOut==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
return 0;
}

View File

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

View File

@@ -12,6 +12,7 @@
#include "./ping.h"
#include "./flash.h"
#include "./getdevices.h"
#include <aqhome/api.h>
#include <aqhome/aqhome.h>
@@ -67,6 +68,7 @@ int main(int argc, char **argv)
const GWEN_FUNCS cmdDefArray[]= {
GWEN_FE_DAH("ping", AQH_Tool_Ping, I18N("Ping a given node on the network")),
GWEN_FE_DAH("flash", AQH_Tool_Flash, I18N("Flash a given node on the network")),
GWEN_FE_DAH("getdevices", AQH_Tool_GetDevices, I18N("Request list of known devices on the network")),
GWEN_FE_END(),
};
const GWEN_FUNCS *func;

View File

@@ -93,6 +93,47 @@ GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG
GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
int msgCode,
int timeoutInSeconds)
{
time_t startTime;
startTime=time(NULL);
for (;;) {
GWEN_MSG *msg;
time_t now;
GWEN_MsgEndpointMgr_RunAllEndpoints(emgr);
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
if (msg) {
uint16_t code;
code=GWEN_IpcMsg_GetCode(msg);
if (code==msgCode) {
DBG_INFO(NULL, "Received expected IPC message");
return msg;
}
else if (code==AQH_MSGTYPE_IPC_ERROR) {
DBG_INFO(NULL, "Received IPC error message");
return msg;
}
GWEN_Msg_free(msg);
}
now=time(NULL);
if (now-startTime>timeoutInSeconds) {
DBG_INFO(NULL, "Timeout");
break;
}
}
return NULL;
}
int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
{
time_t startTime;

View File

@@ -22,6 +22,10 @@ GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG
int nodeAddr,
int timeoutInSeconds);
GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
int msgCode,
int timeoutInSeconds);
int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds);
int Utils_SendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp, uint32_t groups);