/****************************************************************************
 * 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 <aqhome/ipc/msg_ipc_value.h>

#include <gwenhywfar/msg.h>
#include <gwenhywfar/buffer.h>

#include <gwenhywfar/misc.h>
#include <gwenhywfar/list.h>
#include <gwenhywfar/error.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/msg_ipc.h>


#define AQH_MSGIPC_VALUE_OFFS_UID         0   /* 4 bytes */
#define AQH_MSGIPC_VALUE_OFFS_VALUEID     4   /* 1 byte */
#define AQH_MSGIPC_VALUE_OFFS_VALUETYPE   5   /* 1 byte */
#define AQH_MSGIPC_VALUE_OFFS_VALUE_NOM   6   /* 2 bytes */
#define AQH_MSGIPC_VALUE_OFFS_VALUE_DENOM 8   /* 2 bytes */

#define AQH_MSGIPC_VALUE_MINSIZE          (GWEN_MSGIPC_OFFS_PAYLOAD+10)




GWEN_MSG *AQH_ValueIpcMsg_new(uint16_t code,
                              uint32_t uid,
                              uint8_t valueId,
                              uint8_t valueType,
                              int16_t valueNom,
                              int16_t valueDenom)
{
  GWEN_MSG *msg;
  uint8_t *ptr;

  msg=GWEN_IpcMsg_new(AQH_IPC_PROTOCOL_ID, AQH_IPC_PROTOCOL_VERSION, code, AQH_MSGIPC_VALUE_MINSIZE, NULL);
  ptr=GWEN_Msg_GetBuffer(msg);
  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_UID+0]=uid & 0xff;
  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_UID+1]=(uid>>8) & 0xff;
  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_UID+2]=(uid>>16) & 0xff;
  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_UID+3]=(uid>>24) & 0xff;

  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUEID]=valueId;
  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUETYPE]=valueType;

  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUE_NOM+0]=valueNom & 0xff;
  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUE_NOM+1]=(valueNom>>8) & 0xff;

  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUE_DENOM+0]=valueDenom & 0xff;
  ptr[GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUE_DENOM+1]=(valueDenom>>8) & 0xff;

  return msg;
}



uint32_t AQH_ValueIpcMsg_GetUid(const GWEN_MSG *msg)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGIPC_VALUE_MINSIZE) {
    const uint8_t *ptr;

    ptr=GWEN_Msg_GetConstBuffer(msg)+GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_UID;
    return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24);
  }
  return 0;
}



uint8_t AQH_ValueIpcMsg_GetValueId(const GWEN_MSG *msg)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGIPC_VALUE_MINSIZE) {
    const uint8_t *ptr;

    ptr=GWEN_Msg_GetConstBuffer(msg)+GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUEID;
    return (uint8_t)(ptr[0]);
  }
  return 0;
}



uint8_t AQH_ValueIpcMsg_GetValueType(const GWEN_MSG *msg)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGIPC_VALUE_MINSIZE) {
    const uint8_t *ptr;

    ptr=GWEN_Msg_GetConstBuffer(msg)+GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUETYPE;
    return (uint8_t)(ptr[0]);
  }
  return 0;
}



int16_t AQH_ValueIpcMsg_GetValueNom(const GWEN_MSG *msg)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGIPC_VALUE_MINSIZE) {
    const uint8_t *ptr;

    ptr=GWEN_Msg_GetConstBuffer(msg)+GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUE_NOM;
    return (int16_t)((uint16_t)((ptr[0])+(ptr[1]<<8)));
  }
  return 0;
}



int16_t AQH_ValueIpcMsg_GetValueDenom(const GWEN_MSG *msg)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGIPC_VALUE_MINSIZE) {
    const uint8_t *ptr;

    ptr=GWEN_Msg_GetConstBuffer(msg)+GWEN_MSGIPC_OFFS_PAYLOAD+AQH_MSGIPC_VALUE_OFFS_VALUE_DENOM;
    return (int16_t)((uint16_t)((ptr[0])+(ptr[1]<<8)));
  }
  return 0;
}



double AQH_ValueIpcMsg_GetValueAsDouble(const GWEN_MSG *msg)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGIPC_VALUE_MINSIZE) {
    double nom;
    int16_t rawDenom;
    double denom;

    nom=(double) AQH_ValueIpcMsg_GetValueNom(msg);
    rawDenom=AQH_ValueIpcMsg_GetValueDenom(msg);
    if (rawDenom==0)
      denom=1.0;
    else
      denom=(double) rawDenom;
    return (double)(nom/denom);
  }
  return 0.0;
}



void AQH_ValueIpcMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
  if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSGIPC_VALUE_MINSIZE) {
    GWEN_Buffer_AppendArgs(dbuf,
                           "VALUE (code=%d, proto=%d, proto version=%d, uid=0x%08x, value_id=0x%02x, type=0x%02x, value=%f)\n",
                           GWEN_IpcMsg_GetCode(msg),
                           GWEN_IpcMsg_GetProtoId(msg),
                           GWEN_IpcMsg_GetProtoVersion(msg),
                           (unsigned int) AQH_ValueIpcMsg_GetUid(msg),
                           AQH_ValueIpcMsg_GetValueId(msg),
                           AQH_ValueIpcMsg_GetValueType(msg),
                           AQH_ValueIpcMsg_GetValueAsDouble(msg));
  }
}







