/**************************************************************************** * 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 #endif #include "./c_setdata.h" #include "./aqhome_data_p.h" #include "./loop.h" #include "aqhome/aqhome.h" #include "aqhome/ipc/requests.h" #include "aqhome/ipc/data/ipc_data.h" #include "aqhome/ipc/data/msg_data_set.h" #include "aqhome/ipc/endpoint_ipc.h" #include "aqhome/ipc/msg_ipc_result.h" #include "aqhome/ipc/msg_ipc_tag16.h" #include /* ------------------------------------------------------------------------------------------------ * defines * ------------------------------------------------------------------------------------------------ */ #define R_SETDATA_REQUEST_EXPIRE_SECS 20 #define R_SETDATA_SUBREQUEST_EXPIRE_SECS 10 /* ------------------------------------------------------------------------------------------------ * forward declarations * ------------------------------------------------------------------------------------------------ */ static GWEN_MSG_REQUEST *_mkRequest_SetData(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *epSrc, uint32_t requestMsgId, GWEN_MSG_ENDPOINT *epDriver, const AQH_VALUE *v, const char *data); static void _rqSubRequestFinished(GWEN_MSG_REQUEST *rq, GWEN_MSG_REQUEST *subRq, int reason); static void _rqAbort(GWEN_MSG_REQUEST *rq, int reason); static GWEN_MSG_REQUEST *_mkSubRequest_SetData(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *epDriver, const AQH_VALUE *v, const char *data); static int _subRqHandleResponse(GWEN_MSG_REQUEST *rq, GWEN_MSG *msg); static void _subRqAbort(GWEN_MSG_REQUEST *rq, int reason); /* ------------------------------------------------------------------------------------------------ * implementations * ------------------------------------------------------------------------------------------------ */ void AqHomeData_HandleSetData(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *epSrc, GWEN_MSG *recvdMsg) { uint32_t msgId; AQH_VALUE *recvdValue; const char *valueName; char *valueDataFreeable; AQH_VALUE *systemValue; msgId=GWEN_IpcMsg_GetMsgId(recvdMsg); DBG_INFO(NULL, "Received IPC SetDataRequest message (msgId=%d)", msgId); AQH_SetDataIpcMsg_Parse(recvdMsg, 0); recvdValue=AQH_SetDataIpcMsg_ReadValue(recvdMsg); valueName=recvdValue?AQH_Value_GetNameForSystem(recvdValue):NULL; valueDataFreeable=AQH_SetDataIpcMsg_ReadData(recvdMsg); systemValue=AQH_Storage_GetValueByNameForSystem(aqh->storage, valueName); if (systemValue) { if (AQH_Value_GetValueType(systemValue)==AQH_ValueType_Actor) { const char *driverName; driverName=AQH_Value_GetDriver(systemValue); if (driverName && *driverName) { GWEN_MSG_ENDPOINT *epDriver; epDriver=AqHomeData_GetIpcEndpointByServiceName(aqh, driverName); if (epDriver) { GWEN_MSG_REQUEST *rq; DBG_DEBUG(NULL, "Creating SETDATA request for driver endpoint (%s)", GWEN_MsgEndpoint_GetName(epDriver)); rq=_mkRequest_SetData(aqh, epSrc, msgId, epDriver, systemValue, valueDataFreeable); AqHomeData_AddRequestToTree(aqh, rq); } else { DBG_ERROR(NULL, "Driver \"%s\" not available", driverName); AQH_IpcEndpoint_SendResponseResult(epSrc, msgId, AQH_MSGTYPE_IPC_DATA_RESULT, AQH_MSG_IPC_ERROR_GENERIC); } } else { DBG_ERROR(NULL, "No driver name"); AQH_IpcEndpoint_SendResponseResult(epSrc, msgId, AQH_MSGTYPE_IPC_DATA_RESULT, AQH_MSG_IPC_ERROR_GENERIC); } } else { DBG_ERROR(NULL, "Value \"%s\" is not an actor", valueName); AQH_IpcEndpoint_SendResponseResult(epSrc, msgId, AQH_MSGTYPE_IPC_DATA_RESULT, AQH_MSG_IPC_ERROR_INVALID); } } else { DBG_ERROR(NULL, "Unknown value \"%s\"", valueName); AQH_IpcEndpoint_SendResponseResult(epSrc, msgId, AQH_MSGTYPE_IPC_DATA_RESULT, AQH_MSG_IPC_ERROR_NOTFOUND); } AQH_Value_free(recvdValue); free(valueDataFreeable); } /* ------------------------------------------------------------------------------------------------ * IPC Request SETDATA */ GWEN_MSG_REQUEST *_mkRequest_SetData(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *epSrc, uint32_t requestMsgId, GWEN_MSG_ENDPOINT *epDriver, const AQH_VALUE *v, const char *data) { GWEN_MSG_REQUEST *rq; GWEN_MSG_REQUEST *subRq; rq=GWEN_MsgRequest_new(); GWEN_MsgRequest_SetPrivateData(rq, aqh); GWEN_MsgRequest_SetEndpoint(rq, epSrc); 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, epDriver, v, data); 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_DEBUG(NULL, "SubRequest finished (reason: %d)", reason); 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, int reason) { 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, reason); } /* ------------------------------------------------------------------------------------------------ * Driver Request SETDATA */ GWEN_MSG_REQUEST *_mkSubRequest_SetData(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *epDriver, const AQH_VALUE *v, const char *data) { GWEN_MSG_REQUEST *rq; uint16_t msgId; GWEN_MSG *driverMsg; rq=GWEN_MsgRequest_new(); GWEN_MsgRequest_SetPrivateData(rq, aqh); GWEN_MsgRequest_SetEndpoint(rq, epDriver); GWEN_MsgRequest_SetHandleResponseFn(rq, _subRqHandleResponse); GWEN_MsgRequest_SetAbortFn(rq, _subRqAbort); msgId=GWEN_MsgEndpoint_GetNextMessageId(epDriver); GWEN_MsgRequest_SetRequestMsgId(rq, msgId); GWEN_MsgRequest_SetTimestamps(rq, R_SETDATA_SUBREQUEST_EXPIRE_SECS); driverMsg=AQH_SetDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA, msgId, 0, v, data); GWEN_MsgEndpoint_AddSendMessage(epDriver, driverMsg); return rq; } int _subRqHandleResponse(GWEN_MSG_REQUEST *rq, GWEN_MSG *msg) { DBG_DEBUG(NULL, "Checking message from driver"); if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_DATA_RESULT) { uint32_t result; GWEN_MSG_REQUEST *rqParent; result=AQH_ResultIpcMsg_GetResultCode(msg); DBG_INFO(NULL, "Received result for request: %d", result); GWEN_MsgRequest_SetResult(rq, result); 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_ERROR(NULL, "Unexpected response message %d", GWEN_IpcMsg_GetCode(msg)); } return GWEN_MSG_REQUEST_RESULT_NOT_HANDLED; } void _subRqAbort(GWEN_MSG_REQUEST *rq, int reason) { 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, reason); }