aqhome: more work on transformation to event2/ipc2.
This commit is contained in:
@@ -33,6 +33,8 @@
|
||||
</setVar>
|
||||
|
||||
<headers dist="true" >
|
||||
client.h
|
||||
client_p.h
|
||||
getvalues.h
|
||||
getdevices.h
|
||||
adddata.h
|
||||
@@ -47,6 +49,7 @@
|
||||
<sources>
|
||||
$(local/typefiles)
|
||||
|
||||
client.c
|
||||
getvalues.c
|
||||
getdevices.c
|
||||
adddata.c
|
||||
|
||||
222
apps/aqhome-tool/data/client.c
Normal file
222
apps/aqhome-tool/data/client.c
Normal file
@@ -0,0 +1,222 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* 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.
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "./client_p.h"
|
||||
#include "../utils.h"
|
||||
|
||||
#include "aqhome/aqhome.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/ipc2/endpoint.h"
|
||||
|
||||
#include <gwenhywfar/args.h>
|
||||
#include <gwenhywfar/i18n.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
#include <gwenhywfar/text.h>
|
||||
|
||||
|
||||
|
||||
GWEN_INHERIT(AQH_OBJECT, AQH_TOOL_CLIENT)
|
||||
|
||||
|
||||
|
||||
|
||||
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
|
||||
|
||||
static int _sendWaitHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo);
|
||||
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);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
AQH_OBJECT *AQH_ToolClient_new(AQH_EVENT_LOOP *eventLoop, GWEN_DB_NODE *dbGlobalArgs, const GWEN_ARGS *argDescrs)
|
||||
{
|
||||
AQH_OBJECT *o;
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
o=AQH_Object_new(eventLoop);
|
||||
GWEN_NEW_OBJECT(AQH_TOOL_CLIENT, xo);
|
||||
GWEN_INHERIT_SETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o, xo, _freeData);
|
||||
xo->dbGlobalArgs=dbGlobalArgs;
|
||||
xo->args=argDescrs;
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void GWENHYWFAR_CB _freeData(GWEN_UNUSED void *bp, void *p)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=(AQH_TOOL_CLIENT*)p;
|
||||
GWEN_DB_Group_free(xo->dbLocalArgs);
|
||||
GWEN_DB_Group_free(xo->dbGlobalArgs);
|
||||
AQH_Object_free(xo->ipcEndpoint);
|
||||
GWEN_FREE_OBJECT(xo);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_ToolClient_ReadLocalArgs(AQH_OBJECT *o, int argc, char **argv)
|
||||
{
|
||||
if (o) {
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
int rv;
|
||||
|
||||
GWEN_DB_Group_free(xo->dbLocalArgs);
|
||||
xo->dbLocalArgs=GWEN_DB_GetGroup(xo->dbGlobalArgs, GWEN_DB_FLAGS_DEFAULT, "local");
|
||||
rv=GWEN_Args_Check(argc, argv, 1, GWEN_ARGS_MODE_ALLOW_FREEPARAM, xo->args, xo->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(xo->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 1;
|
||||
}
|
||||
xo->timeoutInSeconds=GWEN_DB_GetIntValue(xo->dbLocalArgs, "timeout", 0, 5);
|
||||
|
||||
AQH_MergeConfigFileIntoConfig(xo->dbLocalArgs, "ConfigFile");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_ToolClient_SetCreateRequestMessageFn(AQH_OBJECT *o, AQH_TOOLCLIENT_CREATEREQUESTMESSAGE_FN f)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
xo->createRequestMessageFn=f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_ToolClient_SetHandleResponseMessageFn(AQH_OBJECT *o, AQH_TOOLCLIENT_HANDLERESPONSEMESSAGE_FN f)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
xo->handleResponseMessageFn=f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_ToolClient_Run(AQH_OBJECT *o)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
xo->ipcEndpoint=Utils2_SetupBrokerClientEndpoint(AQH_Object_GetEventLoop(o), xo->dbLocalArgs, 0);
|
||||
if (xo->ipcEndpoint==NULL) {
|
||||
DBG_ERROR(NULL, "ERROR creating TCP connection");
|
||||
return 2;
|
||||
}
|
||||
return _sendWaitHandle(o, xo);
|
||||
}
|
||||
return GWEN_ERROR_INVALID;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _sendWaitHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo)
|
||||
{
|
||||
AQH_EVENT_LOOP *eventLoop;
|
||||
AQH_MESSAGE *msgOut;
|
||||
uint32_t msgId;
|
||||
|
||||
eventLoop=AQH_Object_GetEventLoop(o);
|
||||
msgId=AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint);
|
||||
msgOut=_createRequestMessage(o, msgId);
|
||||
if (msgOut==NULL) {
|
||||
DBG_ERROR(NULL, "Error creating outbound message");
|
||||
return 2;
|
||||
}
|
||||
AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut);
|
||||
|
||||
for (;;) {
|
||||
AQH_MESSAGE *msgIn;
|
||||
|
||||
msgIn=Utils2_WaitForResponseMsg(eventLoop, xo->ipcEndpoint, msgId, xo->timeoutInSeconds);
|
||||
if (msgIn) {
|
||||
GWEN_TAG16_LIST *tagList;
|
||||
|
||||
tagList=AQH_IpcMessageTag16_ParsePayload(msgIn, 0);
|
||||
if (tagList) {
|
||||
int rv;
|
||||
|
||||
rv=_handleResponseMessage(o, msgIn, tagList);
|
||||
AQH_Message_free(msgIn);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return 3;
|
||||
}
|
||||
else if (rv==1) {
|
||||
DBG_ERROR(NULL, "Done.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
} /* for */
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
if (xo->createRequestMessageFn)
|
||||
return xo->createRequestMessageFn(o, msgId);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
if (xo->handleResponseMessageFn)
|
||||
return xo->handleResponseMessageFn(o, msg, tagList);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
33
apps/aqhome-tool/data/client.h
Normal file
33
apps/aqhome-tool/data/client.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/****************************************************************************
|
||||
* 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_CLIENT_H
|
||||
#define AQHOME_TOOL_CLIENT_H
|
||||
|
||||
|
||||
|
||||
#include "aqhome/msg/ipc/m_ipc_tag16.h"
|
||||
|
||||
#include <gwenhywfar/args.h>
|
||||
|
||||
|
||||
typedef AQH_MESSAGE* (*AQH_TOOLCLIENT_CREATEREQUESTMESSAGE_FN)(AQH_OBJECT *o, uint32_t msgId);
|
||||
typedef int (*AQH_TOOLCLIENT_HANDLERESPONSEMESSAGE_FN)(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList);
|
||||
|
||||
|
||||
AQH_OBJECT *AQH_ToolClient_new(AQH_EVENT_LOOP *eventLoop, GWEN_DB_NODE *dbGlobalArgs, const GWEN_ARGS *argDescrs);
|
||||
int AQH_ToolClient_ReadLocalArgs(AQH_OBJECT *o, int argc, char **argv);
|
||||
int AQH_ToolClient_Run(AQH_OBJECT *o);
|
||||
|
||||
void AQH_ToolClient_SetCreateRequestMessageFn(AQH_OBJECT *o, AQH_TOOLCLIENT_CREATEREQUESTMESSAGE_FN f);
|
||||
void AQH_ToolClient_SetHandleResponseMessageFn(AQH_OBJECT *o, AQH_TOOLCLIENT_HANDLERESPONSEMESSAGE_FN f);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
34
apps/aqhome-tool/data/client_p.h
Normal file
34
apps/aqhome-tool/data/client_p.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/****************************************************************************
|
||||
* 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_CLIENT_P_H
|
||||
#define AQHOME_TOOL_CLIENT_P_H
|
||||
|
||||
|
||||
|
||||
#include "./client.h"
|
||||
|
||||
|
||||
|
||||
typedef struct AQH_TOOL_CLIENT AQH_TOOL_CLIENT;
|
||||
struct AQH_TOOL_CLIENT {
|
||||
GWEN_DB_NODE *dbGlobalArgs;
|
||||
GWEN_DB_NODE *dbLocalArgs;
|
||||
const GWEN_ARGS *args;
|
||||
|
||||
AQH_TOOLCLIENT_CREATEREQUESTMESSAGE_FN createRequestMessageFn;
|
||||
AQH_TOOLCLIENT_HANDLERESPONSEMESSAGE_FN handleResponseMessageFn;
|
||||
|
||||
AQH_OBJECT *ipcEndpoint;
|
||||
int timeoutInSeconds;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -19,6 +19,12 @@
|
||||
#include "aqhome/ipc/data/msg_data_devices.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_devices.h"
|
||||
#include "aqhome/ipc2/endpoint.h"
|
||||
|
||||
#include <gwenhywfar/args.h>
|
||||
#include <gwenhywfar/i18n.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
@@ -33,7 +39,6 @@
|
||||
|
||||
|
||||
static int _doGetDevices(GWEN_DB_NODE *dbArgs);
|
||||
static void _sendCommand(GWEN_MSG_ENDPOINT *epTcp);
|
||||
|
||||
|
||||
|
||||
@@ -164,88 +169,91 @@ int AQH_Tool_GetDevices(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
|
||||
|
||||
int _doGetDevices(GWEN_DB_NODE *dbArgs)
|
||||
{
|
||||
GWEN_MSG_ENDPOINT *epTcp;
|
||||
AQH_EVENT_LOOP *eventLoop;
|
||||
AQH_OBJECT *epTcp;
|
||||
int timeoutInSeconds;
|
||||
GWEN_MSG *msg;
|
||||
int printHeader;
|
||||
AQH_MESSAGE *msgOut;
|
||||
uint32_t msgId;
|
||||
|
||||
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
|
||||
printHeader=GWEN_DB_GetIntValue(dbArgs, "printHeader", 0, 0);
|
||||
|
||||
epTcp=Utils_SetupBrokerClientEndpoint(dbArgs, 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;
|
||||
}
|
||||
|
||||
/*fprintf(stdout, "Sending GetDevices request\n");*/
|
||||
|
||||
_sendCommand(epTcp);
|
||||
msgId=AQH_Endpoint_GetNextMessageId(epTcp);
|
||||
msgOut=AQH_IpcMessage_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, AQH_MSGTYPE_IPC_DATA_GETDEVICES_REQ,
|
||||
msgId, 0,
|
||||
0, NULL);
|
||||
AQH_Endpoint_AddMsgOut(epTcp, msgOut);
|
||||
|
||||
for (;;) {
|
||||
AQH_MESSAGE *msgIn;
|
||||
uint16_t code;
|
||||
|
||||
msg=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_DATA_GETDEVICES_RSP, timeoutInSeconds);
|
||||
if (msg==NULL) {
|
||||
DBG_ERROR(NULL, "No response received");
|
||||
return 2;
|
||||
}
|
||||
code=GWEN_IpcMsg_GetCode(msg);
|
||||
if (code==AQH_MSGTYPE_IPC_DATA_GETDEVICES_RSP) {
|
||||
AQH_DEVICE_LIST *deviceList;
|
||||
msgIn=Utils2_WaitForResponseMsg(eventLoop, epTcp, msgId, timeoutInSeconds);
|
||||
if (msgIn) {
|
||||
GWEN_TAG16_LIST *tagList;
|
||||
|
||||
AQH_DevicesDataIpcMsg_Parse(msg, 0);
|
||||
deviceList=AQH_DevicesDataIpcMsg_ReadDeviceList(msg);
|
||||
if (deviceList) {
|
||||
AQH_DEVICE *device;
|
||||
code=AQH_IpcMessage_GetCode(msgIn);
|
||||
tagList=AQH_IpcMessageTag16_ParsePayload(msgIn, 0);
|
||||
if (tagList) {
|
||||
if (code==AQH_MSGTYPE_IPC_DATA_GETDEVICES_RSP) {
|
||||
AQH_DEVICE_LIST *deviceList;
|
||||
|
||||
device=AQH_Device_List_First(deviceList);
|
||||
while(device) {
|
||||
Utils_PrintDevice(device, printHeader);
|
||||
printHeader=0;
|
||||
device=AQH_Device_List_Next(device);
|
||||
deviceList=AQH_IpcdMessageDevices_ReadDeviceList(tagList);
|
||||
if (deviceList) {
|
||||
AQH_DEVICE *device;
|
||||
|
||||
device=AQH_Device_List_First(deviceList);
|
||||
while(device) {
|
||||
Utils_PrintDevice(device, printHeader);
|
||||
printHeader=0;
|
||||
device=AQH_Device_List_Next(device);
|
||||
}
|
||||
AQH_Device_List_free(deviceList);
|
||||
}
|
||||
|
||||
DBG_ERROR(NULL, "Flags: %08x", AQH_IpcdMessageDevices_GetFlags(tagList));
|
||||
if (AQH_IpcdMessageDevices_GetFlags(tagList) & AQH_MSGDATA_DEVICES_FLAGS_LASTMSG) {
|
||||
DBG_ERROR(NULL, "Last message received");
|
||||
GWEN_Tag16_List_free(tagList);
|
||||
break;
|
||||
}
|
||||
}
|
||||
AQH_Device_List_free(deviceList);
|
||||
}
|
||||
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
|
||||
uint32_t resultCode;
|
||||
|
||||
if (AQH_DevicesDataIpcMsg_GetFlags(msg) & AQH_MSGDATA_DEVICES_FLAGS_LASTMSG) {
|
||||
DBG_INFO(NULL, "Last message received");
|
||||
break;
|
||||
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);
|
||||
}
|
||||
}
|
||||
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
|
||||
uint32_t resultCode;
|
||||
|
||||
resultCode=AQH_ResultIpcMsg_GetResultCode(msg);
|
||||
fprintf(stderr, "ERROR: %d\n", resultCode);
|
||||
GWEN_MsgEndpoint_free(epTcp);
|
||||
return 3;
|
||||
}
|
||||
else {
|
||||
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
|
||||
GWEN_MsgEndpoint_free(epTcp);
|
||||
return 3;
|
||||
}
|
||||
} /* for */
|
||||
|
||||
GWEN_MsgEndpoint_free(epTcp);
|
||||
AQH_Object_free(epTcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _sendCommand(GWEN_MSG_ENDPOINT *epTcp)
|
||||
{
|
||||
GWEN_MSG *msgOut;
|
||||
|
||||
msgOut=GWEN_IpcMsg_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, AQH_MSGTYPE_IPC_DATA_GETDEVICES_REQ,
|
||||
GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0,
|
||||
0, NULL);
|
||||
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -19,6 +19,12 @@
|
||||
#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_values.h"
|
||||
#include "aqhome/ipc2/endpoint.h"
|
||||
|
||||
#include <gwenhywfar/args.h>
|
||||
#include <gwenhywfar/i18n.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
@@ -33,8 +39,6 @@
|
||||
|
||||
|
||||
static int _doGetValues(GWEN_DB_NODE *dbArgs);
|
||||
static uint32_t _sendRequest(GWEN_MSG_ENDPOINT *epTcp);
|
||||
static int _handleResponses(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds);
|
||||
|
||||
|
||||
|
||||
@@ -110,6 +114,17 @@ int AQH_Tool_GetValues(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
|
||||
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 */
|
||||
@@ -154,112 +169,88 @@ int AQH_Tool_GetValues(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
|
||||
|
||||
int _doGetValues(GWEN_DB_NODE *dbArgs)
|
||||
{
|
||||
GWEN_MSG_ENDPOINT *epTcp;
|
||||
AQH_EVENT_LOOP *eventLoop;
|
||||
AQH_OBJECT *epTcp;
|
||||
int timeoutInSeconds;
|
||||
int printHeader;
|
||||
AQH_MESSAGE *msgOut;
|
||||
uint32_t msgId;
|
||||
int rv;
|
||||
|
||||
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
|
||||
printHeader=GWEN_DB_GetIntValue(dbArgs, "printHeader", 0, 0);
|
||||
|
||||
epTcp=Utils_SetupBrokerClientEndpoint(dbArgs, 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=_sendRequest(epTcp);
|
||||
rv=_handleResponses(epTcp, msgId, timeoutInSeconds);
|
||||
if (rv!=0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
}
|
||||
GWEN_MsgEndpoint_free(epTcp);
|
||||
return rv;
|
||||
}
|
||||
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,
|
||||
msgId, 0,
|
||||
0, NULL);
|
||||
AQH_Endpoint_AddMsgOut(epTcp, msgOut);
|
||||
|
||||
|
||||
|
||||
uint32_t _sendRequest(GWEN_MSG_ENDPOINT *epTcp)
|
||||
{
|
||||
GWEN_MSG *msgOut;
|
||||
uint32_t msgId;
|
||||
|
||||
msgId=GWEN_MsgEndpoint_GetNextMessageId(epTcp);
|
||||
msgOut=GWEN_IpcMsg_new(AQH_IPC_PROTOCOL_DATA_ID, AQH_IPC_PROTOCOL_DATA_VERSION, AQH_MSGTYPE_IPC_DATA_GETVALUES_REQ,
|
||||
msgId, 0,
|
||||
0, NULL);
|
||||
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
||||
return msgId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _handleResponses(GWEN_MSG_ENDPOINT *epTcp, uint32_t msgId, int timeoutInSeconds)
|
||||
{
|
||||
for (;;) {
|
||||
GWEN_MSG *msg;
|
||||
AQH_MESSAGE *msgIn;
|
||||
uint16_t code;
|
||||
|
||||
msg=Utils_WaitForResponse(epTcp, msgId, timeoutInSeconds);
|
||||
if (msg) {
|
||||
uint16_t code;
|
||||
msgIn=Utils2_WaitForResponseMsg(eventLoop, epTcp, msgId, timeoutInSeconds);
|
||||
if (msgIn) {
|
||||
GWEN_TAG16_LIST *tagList;
|
||||
|
||||
code=GWEN_IpcMsg_GetCode(msg);
|
||||
if (code==AQH_MSGTYPE_IPC_DATA_GETVALUES_RSP) {
|
||||
AQH_VALUE_LIST *valueList;
|
||||
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;
|
||||
|
||||
AQH_ValuesDataIpcMsg_Parse(msg, 0);
|
||||
valueList=AQH_ValuesDataIpcMsg_ReadValueList(msg);
|
||||
if (valueList) {
|
||||
AQH_VALUE *v;
|
||||
valueList=AQH_IpcdMessageValues_ReadValueList(tagList);
|
||||
if (valueList) {
|
||||
AQH_VALUE *value;
|
||||
|
||||
v=AQH_Value_List_First(valueList);
|
||||
while(v) {
|
||||
uint64_t valueId;
|
||||
const char *valueName;
|
||||
const char *valueUnits;
|
||||
|
||||
valueId=AQH_Value_GetId(v);
|
||||
valueName=AQH_Value_GetNameForSystem(v);
|
||||
valueUnits=AQH_Value_GetValueUnits(v);
|
||||
|
||||
fprintf(stdout, "%lu\t%s\t%s\n",
|
||||
(unsigned long int) valueId,
|
||||
valueName?valueName:"",
|
||||
valueUnits?valueUnits:"");
|
||||
|
||||
v=AQH_Value_List_Next(v);
|
||||
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);
|
||||
}
|
||||
AQH_Value_List_free(valueList);
|
||||
}
|
||||
|
||||
if (AQH_ValuesDataIpcMsg_GetFlags(msg) & AQH_MSGDATA_VALUES_FLAGS_LASTMSG) {
|
||||
DBG_INFO(NULL, "Last message received");
|
||||
GWEN_Msg_free(msg);
|
||||
break;
|
||||
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;
|
||||
else if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
|
||||
uint32_t resultCode;
|
||||
|
||||
resultCode=AQH_ResultIpcMsg_GetResultCode(msg);
|
||||
fprintf(stderr, "ERROR: %d\n", resultCode);
|
||||
GWEN_Msg_free(msg);
|
||||
return 3;
|
||||
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);
|
||||
}
|
||||
else {
|
||||
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
|
||||
GWEN_Msg_free(msg);
|
||||
return 3;
|
||||
}
|
||||
} /* if msg */
|
||||
else {
|
||||
DBG_ERROR(NULL, "No response received");
|
||||
return 2;
|
||||
}
|
||||
} /* for */
|
||||
|
||||
AQH_Object_free(epTcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user