/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2025 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 "./s_addvalue.h"
#include "./server_p.h"
#include "aqhome/ipc2/endpoint.h"
#include "aqhome/msg/ipc/m_ipc.h"
#include "aqhome/msg/ipc/data/m_ipcd.h"
#include "aqhome/msg/ipc/data/m_ipcd_values.h"
#include "aqhome/msg/ipc/m_ipc_result.h"
#include "aqhome/msg/ipc/m_ipc_tag16.h"

#include <gwenhywfar/debug.h>



/* ------------------------------------------------------------------------------------------------
 * code
 * ------------------------------------------------------------------------------------------------
 */

void AqHomeDataServer_HandleAddValue(AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *msg, const GWEN_TAG16_LIST *tagList)
{
  if (tagList) {
    AQHOME_SERVER *xo;
  
    xo=AqHomeDataServer_GetServerData(o);
    if (xo) {
      AQH_MESSAGE *outMsg;
      int resultCode=AQH_MSGDATA_RESULT_SUCCESS;
      AQH_VALUE *recvdValue;
  
      recvdValue=AQH_IpcdMessageValues_ReadFirstValue(tagList);
      if (recvdValue) {
        AQH_VALUE *value;
  
        value=AqHomeDataServer_GetOrCreateValueForDriverWithTemplate(o, ep, recvdValue);
        if (value==NULL)
          resultCode=AQH_MSGDATA_RESULT_ERROR_PERMS;
        AQH_Value_free(recvdValue);
      }
      else
        resultCode=AQH_MSGDATA_RESULT_ERROR_BADDATA;
  
      outMsg=AQH_IpcMessageResult_new(AQH_IPC_PROTOCOL_DATA_ID,
                                      AQH_IPC_PROTOCOL_DATA_VERSION,
                                      AQH_MSGTYPE_IPC_DATA_RESULT,
                                      AQH_Endpoint_GetNextMessageId(ep),
                                      AQH_IpcMessage_GetMsgId(msg),
                                      resultCode, NULL);
      AQH_Endpoint_AddMsgOut(ep, outMsg);
    }
  }
}



