/**************************************************************************** * 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 "aqhome/ipc/data/ipc_data.h" #include "aqhome/ipc/data/msg_data_datapoints.h" #include "aqhome/ipc/endpoint_ipc.h" #include "aqhome/ipc/msg_ipc_result.h" #include /* ------------------------------------------------------------------------------------------------ * defines * ------------------------------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------------------------------ * forward declarations * ------------------------------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------------------------------ * implementations * ------------------------------------------------------------------------------------------------ */ void AqHomeData_HandleSetData(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *recvdMsg) { GWEN_MSG *outMsg; int resultCode=AQH_MSG_IPC_SUCCESS; if (AQH_IpcEndpoint_GetPermissions(ep) & AQH_IPCENDPOINT_PERMS_SETDATA) { if (AQH_DataPointsDataIpcMsg_IsValid(recvdMsg)) { uint32_t numValues; numValues=AQH_DataPointsDataIpcMsg_GetNumValues(recvdMsg); if (numValues==1) { const char *s; s=AQH_DataPointsDataIpcMsg_GetValueName(recvdMsg); if (s && *s) { AQH_VALUE *v; v=AQH_Storage_GetValueByNameForSystem(aqh->storage, s); if (v==NULL) { resultCode=AQH_MSG_IPC_ERROR_NOTFOUND; } else { const uint64_t *dataPoints; dataPoints=AQH_DataPointsDataIpcMsg_GetDataPoints(recvdMsg); if (dataPoints) { const char *driverName; driverName=AQH_Value_GetDriver(v); if (driverName && *driverName) { GWEN_MSG_ENDPOINT *ep; ep=AqHomeData_GetIpcEndpointByServiceName(aqh, driverName); if (ep) { GWEN_MSG *driverMsg; DBG_INFO(AQH_LOGDOMAIN, "Sending SETDATA msg to driver endpoint (%s)", GWEN_MsgEndpoint_GetName(ep)); driverMsg=AQH_DataPointsDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA, 0, /* flags */ AQH_Value_GetId(v), AQH_Value_GetNameForDriver(v), AQH_Value_GetValueUnits(v), dataPoints, numValues); GWEN_MsgEndpoint_AddSendMessage(ep, driverMsg); } else { DBG_INFO(NULL, "Driver \"%s\" not available", driverName); resultCode=AQH_MSG_IPC_ERROR_GENERIC; } } else { DBG_INFO(NULL, "No driver name"); resultCode=AQH_MSG_IPC_ERROR_GENERIC; } } else { DBG_INFO(NULL, "No datapoints"); resultCode=AQH_MSG_IPC_ERROR_BADDATA; } } } else { DBG_INFO(NULL, "Value without name "); resultCode=AQH_MSG_IPC_ERROR_INVALID; } } else { DBG_INFO(NULL, "Invalid number of datapoints"); resultCode=AQH_MSG_IPC_ERROR_BADDATA; } } else { DBG_INFO(NULL, "Invalid message received"); resultCode=AQH_MSG_IPC_ERROR_BADDATA; } } else { DBG_ERROR(AQH_LOGDOMAIN, "No permissions to set data"); resultCode=AQH_MSG_IPC_ERROR_PERMS; } outMsg=AQH_ResultIpcMsg_new(AQH_MSGTYPE_IPC_DATA_RESULT, resultCode); GWEN_MsgEndpoint_AddSendMessage(ep, outMsg); }