aqhome-data, aqhome-tool: more work on new protocol.
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
#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_connect.h"
|
||||
#include "aqhome/ipc2/endpoint.h"
|
||||
|
||||
#include <gwenhywfar/args.h>
|
||||
@@ -34,8 +36,10 @@ 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 int _waitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo, uint32_t msgId);
|
||||
static int _exchangeConnect(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo, uint32_t flags);
|
||||
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);
|
||||
static int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first);
|
||||
|
||||
|
||||
|
||||
@@ -62,8 +66,6 @@ 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);
|
||||
}
|
||||
@@ -131,17 +133,94 @@ void AQH_ToolClient_SetHandleResponseMessageFn(AQH_OBJECT *o, AQH_TOOLCLIENT_HAN
|
||||
|
||||
|
||||
|
||||
GWEN_DB_NODE *AQH_ToolClient_GetDbGlobalArgs(const AQH_OBJECT *o)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
return xo->dbGlobalArgs;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
GWEN_DB_NODE *AQH_ToolClient_GetDbLocalArgs(const AQH_OBJECT *o)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
return xo->dbLocalArgs;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint32_t AQH_ToolClient_GetFlags(const AQH_OBJECT *o)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
return xo->flags;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_ToolClient_SetFlags(AQH_OBJECT *o, uint32_t f)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
xo->flags=f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_ToolClient_AddFlags(AQH_OBJECT *o, uint32_t f)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
xo->flags|=f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_ToolClient_SubFlags(AQH_OBJECT *o, uint32_t f)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo)
|
||||
xo->flags&=~f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_ToolClient_Run(AQH_OBJECT *o)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
int rv;
|
||||
|
||||
xo->ipcEndpoint=Utils2_SetupBrokerClientEndpoint(AQH_Object_GetEventLoop(o), xo->dbLocalArgs, 0);
|
||||
if (xo->ipcEndpoint==NULL) {
|
||||
DBG_ERROR(NULL, "ERROR creating TCP connection");
|
||||
return 2;
|
||||
}
|
||||
|
||||
rv=_exchangeConnect(o, xo, xo->flags);
|
||||
if (rv!=AQH_MSGDATA_RESULT_SUCCESS) {
|
||||
DBG_ERROR(NULL, "Connect response: %d", rv);
|
||||
return 2;
|
||||
}
|
||||
return _sendWaitHandle(o, xo);
|
||||
}
|
||||
return GWEN_ERROR_INVALID;
|
||||
@@ -149,13 +228,54 @@ int AQH_ToolClient_Run(AQH_OBJECT *o)
|
||||
|
||||
|
||||
|
||||
int AQH_ToolClient_Watch(AQH_OBJECT *o)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_TOOL_CLIENT, o);
|
||||
if (xo) {
|
||||
int rv;
|
||||
|
||||
xo->ipcEndpoint=Utils2_SetupBrokerClientEndpoint(AQH_Object_GetEventLoop(o), xo->dbLocalArgs, 0);
|
||||
if (xo->ipcEndpoint==NULL) {
|
||||
DBG_ERROR(NULL, "ERROR creating TCP connection");
|
||||
return 2;
|
||||
}
|
||||
|
||||
rv=_exchangeConnect(o, xo, xo->flags);
|
||||
if (rv!=AQH_MSGDATA_RESULT_SUCCESS) {
|
||||
DBG_ERROR(NULL, "Connect response: %d", rv);
|
||||
return 2;
|
||||
}
|
||||
return _waitAndHandle(o, xo, 0);
|
||||
}
|
||||
return GWEN_ERROR_INVALID;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_ToolClient_HandleResultMsg(GWEN_UNUSED const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, GWEN_UNUSED int first)
|
||||
{
|
||||
int result;
|
||||
|
||||
result=AQH_IpcMessageResult_GetResult(tagList);
|
||||
if (result!=AQH_MSGDATA_RESULT_SUCCESS) {
|
||||
char *text;
|
||||
|
||||
text=AQH_IpcMessageResult_GetText(tagList);
|
||||
DBG_ERROR(NULL, "ERROR: %d (%s)", result, text?text:"");
|
||||
return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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) {
|
||||
@@ -164,6 +284,18 @@ int _sendWaitHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo)
|
||||
}
|
||||
AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut);
|
||||
|
||||
return _waitAndHandle(o, xo, msgId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _waitAndHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo, uint32_t msgId)
|
||||
{
|
||||
AQH_EVENT_LOOP *eventLoop;
|
||||
int first=1;
|
||||
|
||||
eventLoop=AQH_Object_GetEventLoop(o);
|
||||
|
||||
for (;;) {
|
||||
AQH_MESSAGE *msgIn;
|
||||
|
||||
@@ -175,8 +307,10 @@ int _sendWaitHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo)
|
||||
if (tagList) {
|
||||
int rv;
|
||||
|
||||
rv=_handleResponseMessage(o, msgIn, tagList);
|
||||
rv=_handleResponseMessage(o, msgIn, tagList, first);
|
||||
GWEN_Tag16_List_free(tagList);
|
||||
AQH_Message_free(msgIn);
|
||||
first=0;
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
return 3;
|
||||
@@ -193,6 +327,60 @@ int _sendWaitHandle(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo)
|
||||
|
||||
|
||||
|
||||
int _exchangeConnect(AQH_OBJECT *o, AQH_TOOL_CLIENT *xo, uint32_t flags)
|
||||
{
|
||||
AQH_EVENT_LOOP *eventLoop;
|
||||
AQH_MESSAGE *msgOut;
|
||||
uint32_t msgId;
|
||||
const char *clientId;
|
||||
const char *userId;
|
||||
const char *passw;
|
||||
|
||||
clientId=GWEN_DB_GetCharValue(xo->dbLocalArgs, "brokerClientId", 0, "aqhome-tool");
|
||||
userId=GWEN_DB_GetCharValue(xo->dbLocalArgs, "userId", 0, NULL);
|
||||
passw=GWEN_DB_GetCharValue(xo->dbLocalArgs, "password", 0, NULL);
|
||||
|
||||
eventLoop=AQH_Object_GetEventLoop(o);
|
||||
msgId=AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint);
|
||||
msgOut=AQH_IpcdMessageConnect_new(AQH_MSGTYPE_IPC_DATA_CONNECT_REQ, msgId, 0, clientId, userId, passw, flags);
|
||||
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 code;
|
||||
|
||||
code=AQH_IpcMessage_GetCode(msgIn);
|
||||
if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
|
||||
int result;
|
||||
|
||||
result=AQH_IpcMessageResult_GetResult(tagList);
|
||||
GWEN_Tag16_List_free(tagList);
|
||||
AQH_Message_free(msgIn);
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Unexpected message received (%x)", code);
|
||||
GWEN_Tag16_List_free(tagList);
|
||||
AQH_Message_free(msgIn);
|
||||
return AQH_MSGDATA_RESULT_ERROR_GENERIC;
|
||||
}
|
||||
GWEN_Tag16_List_free(tagList);
|
||||
}
|
||||
AQH_Message_free(msgIn);
|
||||
}
|
||||
} /* for */
|
||||
return AQH_MSGDATA_RESULT_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId)
|
||||
{
|
||||
AQH_TOOL_CLIENT *xo;
|
||||
@@ -207,14 +395,25 @@ AQH_MESSAGE *_createRequestMessage(AQH_OBJECT *o, uint32_t msgId)
|
||||
|
||||
|
||||
|
||||
int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList)
|
||||
int _handleResponseMessage(AQH_OBJECT *o, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList, int first)
|
||||
{
|
||||
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 xo->handleResponseMessageFn(o, msg, tagList, first);
|
||||
else {
|
||||
uint16_t code;
|
||||
|
||||
code=AQH_IpcMessage_GetCode(msg);
|
||||
if (code==AQH_MSGTYPE_IPC_DATA_RESULT)
|
||||
return AQH_ToolClient_HandleResultMsg(msg, tagList, first);
|
||||
else {
|
||||
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user