210 lines
6.4 KiB
C
210 lines
6.4 KiB
C
/****************************************************************************
|
|
* 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.
|
|
****************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
|
|
#include "./r_setdata.h"
|
|
#include "./aqhomed_p.h"
|
|
|
|
#include "aqhome/ipc/endpoint_ipc.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/msg_value3.h"
|
|
|
|
#include <gwenhywfar/debug.h>
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* definitions
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
|
|
#define R_SETDATA_REQUEST_EXPIRE_SECS 20
|
|
#define R_SETDATA_SUBREQUEST_EXPIRE_SECS 10
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* forward declarations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
static void _rqSubRequestFinished(GWEN_MSG_REQUEST *rq, GWEN_MSG_REQUEST *subRq, int reason);
|
|
static void _rqAbort(GWEN_MSG_REQUEST *rq);
|
|
|
|
static GWEN_MSG_REQUEST *_mkSubRequest_SetData(AQHOMED *aqh, int destAddr, int valueId, uint16_t dataVal, uint16_t dataDenom);
|
|
static int _subRqHandleResponse(GWEN_MSG_REQUEST *rq, GWEN_MSG *msg);
|
|
static void _subRqAbort(GWEN_MSG_REQUEST *rq);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* implementations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
GWEN_MSG_REQUEST *AqHomeNodes_MkRequest_SetData(AQHOMED *aqh,
|
|
GWEN_MSG_ENDPOINT *ep, uint32_t requestMsgId,
|
|
int destAddr, int valueId, uint16_t dataVal, uint16_t dataDenom)
|
|
{
|
|
GWEN_MSG_REQUEST *rq;
|
|
GWEN_MSG_REQUEST *subRq;
|
|
|
|
rq=GWEN_MsgRequest_new();
|
|
GWEN_MsgRequest_SetPrivateData(rq, aqh);
|
|
GWEN_MsgRequest_SetEndpoint(rq, ep);
|
|
GWEN_MsgRequest_SetRequestMsgId(rq, requestMsgId);
|
|
GWEN_MsgRequest_SetSubRequestFinishedFn(rq, _rqSubRequestFinished);
|
|
GWEN_MsgRequest_SetAbortFn(rq, _rqAbort);
|
|
GWEN_MsgRequest_SetTimestamps(rq, R_SETDATA_REQUEST_EXPIRE_SECS);
|
|
|
|
subRq=_mkSubRequest_SetData(aqh, destAddr, valueId, dataVal, dataDenom);
|
|
GWEN_MsgRequest_Tree2_AddChild(rq, subRq);
|
|
|
|
return rq;
|
|
}
|
|
|
|
|
|
|
|
void _rqSubRequestFinished(GWEN_MSG_REQUEST *rq, GWEN_MSG_REQUEST *subRq, int reason)
|
|
{
|
|
GWEN_MSG_ENDPOINT *ep;
|
|
uint32_t refMsgId;
|
|
int result;
|
|
|
|
DBG_INFO(NULL, "SubRequest finished");
|
|
refMsgId=GWEN_MsgRequest_GetRequestMsgId(rq);
|
|
ep=GWEN_MsgRequest_GetEndpoint(rq);
|
|
result=GWEN_MsgRequest_GetResult(subRq);
|
|
|
|
if (reason==GWEN_MSG_REQUEST_REASON_ABORTED)
|
|
AQH_IpcEndpoint_SendResponseResult(ep, refMsgId, AQH_MSGTYPE_IPC_DATA_RESULT, AQH_MSG_IPC_ERROR_GENERIC);
|
|
else
|
|
AQH_IpcEndpoint_SendResponseResult(ep, refMsgId, AQH_MSGTYPE_IPC_DATA_RESULT, result);
|
|
|
|
GWEN_MsgRequest_SetResult(rq, result);
|
|
GWEN_MsgRequest_SetState(rq, GWEN_MSG_REQUEST_STATE_DONE);
|
|
}
|
|
|
|
|
|
|
|
void _rqAbort(GWEN_MSG_REQUEST *rq)
|
|
{
|
|
GWEN_MSG_ENDPOINT *ep;
|
|
uint32_t refMsgId;
|
|
GWEN_MSG_REQUEST *rqParent;
|
|
|
|
DBG_INFO(NULL, "Aborting request");
|
|
refMsgId=GWEN_MsgRequest_GetRequestMsgId(rq);
|
|
ep=GWEN_MsgRequest_GetEndpoint(rq);
|
|
AQH_IpcEndpoint_SendResponseResult(ep, refMsgId, AQH_MSGTYPE_IPC_DATA_RESULT, AQH_MSG_IPC_ERROR_GENERIC);
|
|
GWEN_MsgRequest_SetState(rq, GWEN_MSG_REQUEST_STATE_DONE);
|
|
|
|
rqParent=GWEN_MsgRequest_Tree2_GetParent(rq);
|
|
if (rqParent)
|
|
GWEN_MsgRequest_SubRequestFinished(rqParent, rq, GWEN_MSG_REQUEST_REASON_ABORTED);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GWEN_MSG_REQUEST *_mkSubRequest_SetData(AQHOMED *aqh, int destAddr, int valueId, uint16_t dataVal, uint16_t dataDenom)
|
|
{
|
|
GWEN_MSG_REQUEST *rq;
|
|
uint16_t msgId;
|
|
GWEN_MSG *msgOut;
|
|
|
|
rq=GWEN_MsgRequest_new();
|
|
GWEN_MsgRequest_SetPrivateData(rq, aqh);
|
|
GWEN_MsgRequest_SetEndpoint(rq, aqh->ttyEndpoint);
|
|
|
|
GWEN_MsgRequest_SetHandleResponseFn(rq, _subRqHandleResponse);
|
|
GWEN_MsgRequest_SetAbortFn(rq, _subRqAbort);
|
|
|
|
msgId=GWEN_MsgEndpoint_GetNextMessageId(aqh->ttyEndpoint) & 0xffff;
|
|
GWEN_MsgRequest_SetRequestMsgId(rq, msgId);
|
|
GWEN_MsgRequest_SetTimestamps(rq, R_SETDATA_SUBREQUEST_EXPIRE_SECS);
|
|
|
|
msgOut=AQH_Value3Msg_new(aqh->nodeAddress, destAddr, AQH_MSG_TYPE_VALUE_SET, msgId, valueId, dataVal, dataDenom);
|
|
GWEN_MsgEndpoint_AddSendMessage(aqh->ttyEndpoint, msgOut);
|
|
|
|
return rq;
|
|
}
|
|
|
|
|
|
|
|
int _subRqHandleResponse(GWEN_MSG_REQUEST *rq, GWEN_MSG *msg)
|
|
{
|
|
AQHOMED *aqh;
|
|
uint8_t destAddr;
|
|
|
|
DBG_DEBUG(NULL, "Checking message from %02x", AQH_NodeMsg_GetSourceAddress(msg));
|
|
aqh=(AQHOMED*)GWEN_MsgRequest_GetPrivateData(rq);
|
|
|
|
destAddr=AQH_NodeMsg_GetDestAddress(msg);
|
|
if (destAddr==0xff || destAddr==aqh->nodeAddress) {
|
|
uint8_t msgCode;
|
|
|
|
msgCode=AQH_NodeMsg_GetMsgType(msg);
|
|
if (msgCode==AQH_MSG_TYPE_VALUE_SET_ACK || msgCode==AQH_MSG_TYPE_VALUE_SET_NACK) {
|
|
uint16_t msgId;
|
|
|
|
msgId=AQH_Value3Msg_GetMsgId(msg);
|
|
if (msgId==GWEN_MsgRequest_GetRequestMsgId(rq)) {
|
|
GWEN_MSG_REQUEST *rqParent;
|
|
|
|
DBG_INFO(NULL,
|
|
"Received response (%02x) for msg id %04x from %02x",
|
|
msgCode, msgId, AQH_NodeMsg_GetSourceAddress(msg));
|
|
GWEN_MsgRequest_SetResult(rq, (msgCode==AQH_MSG_TYPE_VALUE_SET_ACK)?AQH_MSG_IPC_SUCCESS:AQH_MSG_IPC_ERROR_GENERIC);
|
|
GWEN_MsgRequest_SetState(rq, GWEN_MSG_REQUEST_STATE_DONE);
|
|
rqParent=GWEN_MsgRequest_Tree2_GetParent(rq);
|
|
if (rqParent)
|
|
GWEN_MsgRequest_SubRequestFinished(rqParent, rq, GWEN_MSG_REQUEST_REASON_DONE);
|
|
return GWEN_MSG_REQUEST_RESULT_HANDLED;
|
|
}
|
|
else {
|
|
DBG_DEBUG(NULL, " Non-matching message id");
|
|
}
|
|
}
|
|
else {
|
|
DBG_DEBUG(NULL, " Non-matching message code");
|
|
}
|
|
}
|
|
return GWEN_MSG_REQUEST_RESULT_NOT_HANDLED;
|
|
}
|
|
|
|
|
|
|
|
void _subRqAbort(GWEN_MSG_REQUEST *rq)
|
|
{
|
|
GWEN_MSG_REQUEST *rqParent;
|
|
|
|
DBG_INFO(NULL, "Aborting request");
|
|
|
|
GWEN_MsgRequest_SetResult(rq, AQH_MSG_IPC_ERROR_GENERIC);
|
|
GWEN_MsgRequest_SetState(rq, GWEN_MSG_REQUEST_STATE_DONE);
|
|
|
|
rqParent=GWEN_MsgRequest_Tree2_GetParent(rq);
|
|
if (rqParent)
|
|
GWEN_MsgRequest_SubRequestFinished(rqParent, rq, GWEN_MSG_REQUEST_REASON_ABORTED);
|
|
}
|
|
|
|
|
|
|
|
|