aqhome-data, aqhome-tool: more work on new protocol.
This commit is contained in:
@@ -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,246 +11,131 @@
|
||||
#endif
|
||||
|
||||
#include "./getvalues.h"
|
||||
#include "./client.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "aqhome/aqhome.h"
|
||||
#include "aqhome/msg/msg_node.h"
|
||||
#include "aqhome/ipc/msg_ipc_result.h"
|
||||
#include "aqhome/ipc/data/msg_data_values.h"
|
||||
#include "aqhome/ipc/data/ipc_data.h"
|
||||
|
||||
#include "aqhome/msg/ipc/m_ipc.h"
|
||||
#include "aqhome/msg/ipc/m_ipc_tag16.h"
|
||||
#include "aqhome/msg/ipc/m_ipc_result.h"
|
||||
#include "aqhome/msg/ipc/data/m_ipcd.h"
|
||||
#include "aqhome/msg/ipc/data/m_ipcd_values.h"
|
||||
#include "aqhome/ipc2/endpoint.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 _doGetValues(GWEN_DB_NODE *dbArgs);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* 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);
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* code
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
int AQH_Tool_GetValues(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 */
|
||||
"brokerAddress", /* 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 */
|
||||
"brokerPort", /* 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 response"),
|
||||
I18S("Specify timeout in seconds for response")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Char, /* type */
|
||||
"brokerClientId", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"c", /* short option */
|
||||
"clientid", /* long option */
|
||||
I18S("Specify CLIENTID"),
|
||||
I18S("Specify CLIENTID")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Char, /* type */
|
||||
"userId", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"u", /* short option */
|
||||
"userid", /* long option */
|
||||
I18S("Specify user id"),
|
||||
I18S("Specify user id")
|
||||
},
|
||||
{
|
||||
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
|
||||
GWEN_ArgsType_Char, /* type */
|
||||
"password", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"p", /* short option */
|
||||
"password", /* long option */
|
||||
I18S("Specify service password"),
|
||||
I18S("Specify service password")
|
||||
},
|
||||
{
|
||||
0, /* flags */
|
||||
GWEN_ArgsType_Int, /* type */
|
||||
"printHeader", /* name */
|
||||
0, /* minnum */
|
||||
1, /* maxnum */
|
||||
"H", /* short option */
|
||||
"printheader", /* long option */
|
||||
I18S("Print header if given"),
|
||||
I18S("Print header if given")
|
||||
},
|
||||
{
|
||||
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, "brokerAddress", 0, 1, "t", "tcpaddress", I18S("TCP address to connect to [127.0.0.1]"), NULL},
|
||||
{ A_ARG, A_INT, "brokerPort", 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, "brokerClientId", 0, 1, "c", "clientid", I18S("Specify CLIENTID"), 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},
|
||||
{ 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
AQH_MergeConfigFileIntoConfig(dbLocalArgs, "ConfigFile");
|
||||
|
||||
return _doGetValues(dbLocalArgs);
|
||||
eventLoop=AQH_EventLoop_new();
|
||||
o=AQH_ToolClient_new(eventLoop, 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _doGetValues(GWEN_DB_NODE *dbArgs)
|
||||
AQH_MESSAGE *_createRequestMessage(GWEN_UNUSED AQH_OBJECT *o, uint32_t msgId)
|
||||
{
|
||||
AQH_EVENT_LOOP *eventLoop;
|
||||
AQH_OBJECT *epTcp;
|
||||
int timeoutInSeconds;
|
||||
int printHeader;
|
||||
AQH_MESSAGE *msgOut;
|
||||
uint32_t msgId;
|
||||
|
||||
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
|
||||
printHeader=GWEN_DB_GetIntValue(dbArgs, "printHeader", 0, 0);
|
||||
|
||||
eventLoop=AQH_EventLoop_new();
|
||||
epTcp=Utils2_SetupBrokerClientEndpoint(eventLoop, dbArgs, 0);
|
||||
if (epTcp==NULL) {
|
||||
DBG_ERROR(NULL, "ERROR creating TCP connection");
|
||||
AQH_EventLoop_free(eventLoop);
|
||||
return 2;
|
||||
}
|
||||
|
||||
msgId=AQH_Endpoint_GetNextMessageId(epTcp);
|
||||
msgOut=AQH_IpcMessage_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, AQH_MSGTYPE_IPC_DATA_GETVALUES_REQ,
|
||||
return AQH_IpcMessage_new(AQH_IPC_PROTOCOL_DATA_ID,
|
||||
AQH_IPC_PROTOCOL_DATA_VERSION,
|
||||
AQH_MSGTYPE_IPC_DATA_GETVALUES_REQ,
|
||||
msgId, 0,
|
||||
0, NULL);
|
||||
AQH_Endpoint_AddMsgOut(epTcp, msgOut);
|
||||
|
||||
for (;;) {
|
||||
AQH_MESSAGE *msgIn;
|
||||
uint16_t code;
|
||||
|
||||
msgIn=Utils2_WaitForResponseMsg(eventLoop, epTcp, msgId, timeoutInSeconds);
|
||||
if (msgIn) {
|
||||
GWEN_TAG16_LIST *tagList;
|
||||
|
||||
code=AQH_IpcMessage_GetCode(msgIn);
|
||||
tagList=AQH_IpcMessageTag16_ParsePayload(msgIn, 0);
|
||||
if (tagList) {
|
||||
if (code==AQH_MSGTYPE_IPC_DATA_GETVALUES_RSP) {
|
||||
AQH_VALUE_LIST *valueList;
|
||||
|
||||
valueList=AQH_IpcdMessageValues_ReadValueList(tagList);
|
||||
if (valueList) {
|
||||
AQH_VALUE *value;
|
||||
|
||||
value=AQH_Value_List_First(valueList);
|
||||
while(value) {
|
||||
Utils_PrintValue(value, printHeader);
|
||||
printHeader=0;
|
||||
value=AQH_Value_List_Next(value);
|
||||
}
|
||||
AQH_Value_List_free(valueList);
|
||||
}
|
||||
|
||||
if (AQH_IpcdMessageValues_GetFlags(tagList) & AQH_MSGDATA_VALUES_FLAGS_LASTMSG) {
|
||||
DBG_INFO(NULL, "Last message received");
|
||||
GWEN_Tag16_List_free(tagList);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
|
||||
uint32_t resultCode;
|
||||
|
||||
resultCode=AQH_IpcMessageResult_GetResult(tagList);
|
||||
fprintf(stderr, "ERROR: %d\n", resultCode);
|
||||
GWEN_Tag16_List_free(tagList);
|
||||
AQH_Object_free(epTcp);
|
||||
AQH_EventLoop_free(eventLoop);
|
||||
return 3;
|
||||
}
|
||||
else {
|
||||
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
|
||||
GWEN_Tag16_List_free(tagList);
|
||||
AQH_Object_free(epTcp);
|
||||
AQH_EventLoop_free(eventLoop);
|
||||
return 3;
|
||||
}
|
||||
GWEN_Tag16_List_free(tagList);
|
||||
}
|
||||
}
|
||||
} /* for */
|
||||
|
||||
AQH_Object_free(epTcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _handleResponseMessage(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_DATA_GETVALUES_RSP) {
|
||||
AQH_VALUE_LIST *valueList;
|
||||
|
||||
valueList=AQH_IpcdMessageValues_ReadValueList(tagList);
|
||||
if (valueList) {
|
||||
AQH_VALUE *value;
|
||||
|
||||
value=AQH_Value_List_First(valueList);
|
||||
while(value) {
|
||||
Utils_PrintValue(value, printHeader);
|
||||
printHeader=0;
|
||||
value=AQH_Value_List_Next(value);
|
||||
}
|
||||
AQH_Value_List_free(valueList);
|
||||
}
|
||||
|
||||
if (AQH_IpcdMessageValues_GetFlags(tagList) & AQH_MSGDATA_VALUES_FLAGS_LASTMSG) {
|
||||
DBG_INFO(NULL, "Last message received");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT)
|
||||
return AQH_ToolClient_HandleResultMsg(msg, tagList, first);
|
||||
else {
|
||||
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user