aqhome-data, aqhome-tool: more work on new protocol.

This commit is contained in:
Martin Preuss
2025-02-27 23:50:18 +01:00
parent d887747b3c
commit f2d527cd2f
35 changed files with 2070 additions and 2353 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,35 +11,40 @@
#endif
#include "./setdata.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_set.h"
#include "aqhome/ipc/data/ipc_data.h"
#include "aqhome/msg/ipc/m_ipc.h"
#include "aqhome/msg/ipc/m_ipc_result.h"
#include "aqhome/msg/ipc/data/m_ipcd.h"
#include "aqhome/msg/ipc/data/m_ipcd_setdata.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
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static int _doSetData(GWEN_DB_NODE *dbArgs);
static uint32_t _sendRequest(GWEN_MSG_ENDPOINT *epTcp, const char *valueName, const char *valueUnits, const char *valueData);
static int _awaitResponse(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds);
static AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId);
@@ -50,249 +55,63 @@ static int _awaitResponse(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutI
int AQH_Tool_SetData(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 */
"valueName", /* name */
1, /* minnum */
1, /* maxnum */
"N", /* short option */
"valuename", /* long option */
I18S("Name/path of the value to set (e.g. server/temp/system)"),
I18S("Name/path of the value to set (e.g. server/temp/system)")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Char, /* type */
"valueUnits", /* name */
0, /* minnum */
1, /* maxnum */
"U", /* short option */
"valueunits", /* long option */
I18S("Units of the value to set (e.g. \"Grad Celsius\")"),
I18S("Units of the value to set (e.g. \"Grad Celsius\")")
},
{
GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
GWEN_ArgsType_Char, /* type */
"value", /* name */
1, /* minnum */
1, /* maxnum */
"v", /* short option */
"value", /* long option */
I18S("Value to set"),
I18S("Value to set")
},
{
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")
},
{
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},
{ A_ARG, A_CHAR, "valueName", 1, 1, "N", "valuename", I18S("Value name (e.g. server/temp/system)"), NULL},
{ A_ARG, A_CHAR, "valueUnits", 0, 1, "U", "valueunits", I18S("Value units (e.g. \"Grad Celsius\")"), NULL},
{ A_ARG, A_CHAR, "value", 1, 1, "v", "value", I18S("Value to set"), 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 _doSetData(dbLocalArgs);
}
int _doSetData(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT *epTcp;
int timeoutInSeconds;
const char *valueName;
const char *valueUnits;
const char *valueData;
uint32_t msgId;
int rv;
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
valueName=GWEN_DB_GetCharValue(dbArgs, "valueName", 0, NULL);
valueUnits=GWEN_DB_GetCharValue(dbArgs, "valueUnits", 0, NULL);
valueData=GWEN_DB_GetCharValue(dbArgs, "value", 0, NULL);
if (!(valueName && *valueName)) {
DBG_ERROR(NULL, "ERROR: Missing value name");
return 1;
}
if (!(valueData && *valueData)) {
DBG_ERROR(NULL, "ERROR: Missing data");
return 1;
}
epTcp=Utils_SetupBrokerClientEndpoint(dbArgs, 0);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 2;
}
msgId=_sendRequest(epTcp, valueName, valueUnits, valueData);
rv=_awaitResponse(epTcp, msgId, timeoutInSeconds);
if (rv!=0) {
DBG_ERROR(NULL, "here (%d)", rv);
}
GWEN_MsgEndpoint_free(epTcp);
eventLoop=AQH_EventLoop_new();
o=AQH_ToolClient_new(eventLoop, dbGlobalArgs, args);
AQH_ToolClient_SetCreateRequestMessageFn(o, _createRequestMessage);
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;
}
uint32_t _sendRequest(GWEN_MSG_ENDPOINT *epTcp, const char *valueName, const char *valueUnits, const char *valueData)
AQH_MESSAGE *_createRequestMessage(GWEN_UNUSED AQH_OBJECT *o, uint32_t msgId)
{
GWEN_MSG *msgOut;
AQH_MESSAGE *msg;
GWEN_DB_NODE *dbArgs;
const char *valueName;
const char *valueUnits;
const char *valueData;
AQH_VALUE *v;
uint32_t msgId;
dbArgs=AQH_ToolClient_GetDbLocalArgs(o);
valueName=GWEN_DB_GetCharValue(dbArgs, "valueName", 0, NULL);
valueUnits=GWEN_DB_GetCharValue(dbArgs, "valueUnits", 0, NULL);
valueData=GWEN_DB_GetCharValue(dbArgs, "value", 0, NULL);
if (!(valueName && *valueName)) {
DBG_ERROR(NULL, "ERROR: Missing value name");
return NULL;
}
v=AQH_Value_new();
AQH_Value_SetNameForSystem(v, valueName);
AQH_Value_SetValueUnits(v, valueUnits);
msgId=GWEN_MsgEndpoint_GetNextMessageId(epTcp);
msgOut=AQH_SetDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA, msgId, 0, v, valueData);
msg=AQH_IpcdMessageSetData_new(AQH_MSGTYPE_IPC_DATA_SETDATA, msgId, 0, v, valueData);
AQH_Value_free(v);
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
return msgId;
return msg;
}
int _awaitResponse(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds)
{
GWEN_MSG *msgResponse;
msgResponse=Utils_WaitForResponse(epTcp, msgId, timeoutInSeconds);
if (msgResponse) {
uint16_t code;
code=GWEN_IpcMsg_GetCode(msgResponse);
if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
uint32_t resultCode;
resultCode=AQH_ResultIpcMsg_GetResultCode(msgResponse);
if (resultCode!=AQH_MSG_IPC_SUCCESS) {
fprintf(stderr, "ERROR: %d\n", resultCode);
GWEN_Msg_free(msgResponse);
return 3;
}
else {
fprintf(stdout, "Value set.\n");
GWEN_Msg_free(msgResponse);
return 0;
}
}
else {
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
GWEN_Msg_free(msgResponse);
return 3;
}
}
else {
DBG_ERROR(NULL, "Timeout");
return 3;
}
return 0;
}