aqhome-tool: rewrote SETDATA command.
This commit is contained in:
@@ -33,7 +33,8 @@
|
||||
|
||||
|
||||
static int _doSetData(GWEN_DB_NODE *dbArgs);
|
||||
static void _sendCommand(GWEN_MSG_ENDPOINT *epTcp, const char *valueName, const char *valueUnits, const char *valueData);
|
||||
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);
|
||||
|
||||
|
||||
|
||||
@@ -191,7 +192,8 @@ int _doSetData(GWEN_DB_NODE *dbArgs)
|
||||
const char *valueName;
|
||||
const char *valueUnits;
|
||||
const char *valueData;
|
||||
GWEN_MSG *msg;
|
||||
uint32_t msgId;
|
||||
int rv;
|
||||
|
||||
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
|
||||
valueName=GWEN_DB_GetCharValue(dbArgs, "valueName", 0, NULL);
|
||||
@@ -216,62 +218,75 @@ int _doSetData(GWEN_DB_NODE *dbArgs)
|
||||
return 2;
|
||||
}
|
||||
|
||||
_sendCommand(epTcp, valueName, valueUnits, valueData);
|
||||
|
||||
for (;;) {
|
||||
uint16_t code;
|
||||
|
||||
msg=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_DATA_RESULT, timeoutInSeconds);
|
||||
if (msg==NULL) {
|
||||
DBG_ERROR(NULL, "No response received");
|
||||
return 2;
|
||||
}
|
||||
code=GWEN_IpcMsg_GetCode(msg);
|
||||
if (code==AQH_MSGTYPE_IPC_DATA_RESULT) {
|
||||
uint32_t resultCode;
|
||||
|
||||
resultCode=AQH_ResultIpcMsg_GetResultCode(msg);
|
||||
if (resultCode!=AQH_MSG_IPC_SUCCESS) {
|
||||
fprintf(stderr, "ERROR: %d\n", resultCode);
|
||||
GWEN_MsgEndpoint_free(epTcp);
|
||||
return 3;
|
||||
}
|
||||
else {
|
||||
fprintf(stdout, "Value set.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_INFO(NULL, "Unexpected message \"%d\"", code);
|
||||
GWEN_MsgEndpoint_free(epTcp);
|
||||
return 3;
|
||||
}
|
||||
} /* for */
|
||||
|
||||
msgId=_sendRequest(epTcp, valueName, valueUnits, valueData);
|
||||
rv=_awaitResponse(epTcp, msgId, timeoutInSeconds);
|
||||
if (rv!=0) {
|
||||
DBG_ERROR(NULL, "here (%d)", rv);
|
||||
}
|
||||
GWEN_MsgEndpoint_free(epTcp);
|
||||
return 0;
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _sendCommand(GWEN_MSG_ENDPOINT *epTcp, const char *valueName, const char *valueUnits, const char *valueData)
|
||||
uint32_t _sendRequest(GWEN_MSG_ENDPOINT *epTcp, const char *valueName, const char *valueUnits, const char *valueData)
|
||||
{
|
||||
GWEN_MSG *msgOut;
|
||||
AQH_VALUE *v;
|
||||
uint32_t msgId;
|
||||
|
||||
v=AQH_Value_new();
|
||||
AQH_Value_SetNameForSystem(v, valueName);
|
||||
AQH_Value_SetValueUnits(v, valueUnits);
|
||||
|
||||
msgOut=AQH_SetDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA,
|
||||
GWEN_MsgEndpoint_GetNextMessageId(epTcp), 0,
|
||||
v, valueData);
|
||||
msgId=GWEN_MsgEndpoint_GetNextMessageId(epTcp);
|
||||
msgOut=AQH_SetDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA, msgId, 0, v, valueData);
|
||||
AQH_Value_free(v);
|
||||
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
|
||||
return msgId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user