/****************************************************************************
 * 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 "./db.h"
#include "./aqhomed_p.h"

#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_sendstats.h"
#include "aqhome/msg/msg_recvstats.h"
#include "aqhome/msg/msg_value2.h"
#include "aqhome/msg/msg_needaddr.h"
#include "aqhome/msg/msg_claimaddr.h"
#include "aqhome/msg/msg_haveaddr.h"
#include "aqhome/msg/msg_device.h"
#include "aqhome/msg/msg_flashready.h"


#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint_tcpd.h>
#include <gwenhywfar/timestamp.h>



/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static void _handleMsgValue2(AQHOMED *aqh, const GWEN_MSG *msg);
static void _handleMsgNeedAddress(AQHOMED *aqh, const GWEN_MSG *msg);
static void _handleMsgClaimAddress(AQHOMED *aqh, const GWEN_MSG *msg);
static void _handleMsgHaveAddress(AQHOMED *aqh, const GWEN_MSG *msg);
static void _handleMsgComSendStat(AQHOMED *aqh, const GWEN_MSG *msg);
static void _handleMsgComRecvStat(AQHOMED *aqh, const GWEN_MSG *msg);
static void _handleMsgDevice(AQHOMED *aqh, const GWEN_MSG *msg);
static void _handleMsgFlashReady(AQHOMED *aqh, const GWEN_MSG *msg);

static AQH_NODE_INFO *_getOrCreateNodeAndUpdateUidAddr(AQHOMED *aqh, const GWEN_MSG *msg, uint32_t uid);
static void _updateTimestampLastChange(AQH_NODE_INFO *ni);




/* ------------------------------------------------------------------------------------------------
 * implementations
 * ------------------------------------------------------------------------------------------------
 */

void AqHomed_NodeMsgToDb(AQHOMED *aqh, const GWEN_MSG *msg)
{
  int msgIsValid;
  uint8_t msgType;

  DBG_INFO(AQH_LOGDOMAIN,
	   "  - msg %d (%s) from %d to %d",
           AQH_NodeMsg_GetMsgType(msg),
           AQH_NodeMsg_MsgTypeToChar(AQH_NodeMsg_GetMsgType(msg)),
           AQH_NodeMsg_GetSourceAddress(msg),
           AQH_NodeMsg_GetDestAddress(msg));

  msgIsValid=(AQH_NodeMsg_IsChecksumValid(msg) && AQH_NodeMsg_IsMsgComplete(msg));
  msgType=AQH_NodeMsg_GetMsgType(msg);

  if (msgIsValid) {
    switch(msgType) {
      case AQH_MSG_TYPE_COMSENDSTATS:  _handleMsgComSendStat(aqh, msg); break;
      case AQH_MSG_TYPE_COMRECVSTATS:  _handleMsgComRecvStat(aqh, msg); break;
      case AQH_MSG_TYPE_VALUE2:        _handleMsgValue2(aqh, msg); break;
      case AQH_MSG_TYPE_NEED_ADDRESS:  _handleMsgNeedAddress(aqh, msg); break;
      case AQH_MSG_TYPE_CLAIM_ADDRESS: _handleMsgClaimAddress(aqh, msg); break;
      case AQH_MSG_TYPE_HAVE_ADDRESS:  _handleMsgHaveAddress(aqh, msg); break;
      case AQH_MSG_TYPE_DEVICE:        _handleMsgDevice(aqh, msg); break;
      case AQH_MSG_TYPE_FLASH_READY:   _handleMsgFlashReady(aqh, msg); break;
      default:                         break;
    }
  }
}



void _handleMsgValue2(AQHOMED *aqh, const GWEN_MSG *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_Value2Msg_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(aqh, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
}



void _handleMsgNeedAddress(AQHOMED *aqh, const GWEN_MSG *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_NeedAddrMsg_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(aqh, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
}



void _handleMsgClaimAddress(AQHOMED *aqh, const GWEN_MSG *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_ClaimAddrMsg_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(aqh, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
}



void _handleMsgHaveAddress(AQHOMED *aqh, const GWEN_MSG *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_HaveAddrMsg_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(aqh, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
}



void _handleMsgComSendStat(AQHOMED *aqh, const GWEN_MSG *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_SendStatsMsg_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(aqh, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
  AQH_NodeInfo_SetStatsPacketsOut(ni, AQH_SendStatsMsg_GetPacketsOut(msg));
  AQH_NodeInfo_SetStatsCollisions(ni, AQH_SendStatsMsg_GetCollisions(msg));
  AQH_NodeInfo_SetStatsBusy(ni, AQH_SendStatsMsg_GetBusyErrors(msg));
  AQH_NodeDb_SetModified(aqh->nodeDb);
  _updateTimestampLastChange(ni);
}



void _handleMsgComRecvStat(AQHOMED *aqh, const GWEN_MSG *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_RecvStatsMsg_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(aqh, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
  AQH_NodeInfo_SetStatsPacketsIn(ni, AQH_RecvStatsMsg_GetPacketsIn(msg));
  AQH_NodeInfo_SetStatsCrcErrors(ni, AQH_RecvStatsMsg_GetCrcErrors(msg));
  AQH_NodeInfo_SetStatsIoErrors(ni, AQH_RecvStatsMsg_GetIoErrors(msg));
  AQH_NodeDb_SetModified(aqh->nodeDb);
  _updateTimestampLastChange(ni);
}



void _handleMsgDevice(AQHOMED *aqh, const GWEN_MSG *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_DeviceMsg_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(aqh, msg, uid);
  if (ni) {
    AQH_NodeInfo_SetFirmwareType(ni, AQH_DeviceMsg_GetFirmwareType(msg));
    AQH_NodeInfo_SetFirmwareVersion(ni, (AQH_DeviceMsg_GetFirmwareHigh(msg)<<8) | AQH_DeviceMsg_GetFirmwareLow(msg));
    AQH_NodeInfo_SetModules(ni, AQH_DeviceMsg_GetModuleMask(msg));
    _updateTimestampLastChange(ni);
    AQH_NodeDb_SetModified(aqh->nodeDb);
  }
  else {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
}



void _handleMsgFlashReady(AQHOMED *aqh, const GWEN_MSG *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_FlashReadyMsg_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(aqh, msg, uid);
  if (ni) {
    AQH_NodeInfo_SetFirmwareType(ni, AQH_FlashReadyMsg_GetFirmwareType(msg));
    AQH_NodeInfo_SetFirmwareVersion(ni, AQH_FlashReadyMsg_GetFirmwareVersion(msg));
    _updateTimestampLastChange(ni);
    AQH_NodeDb_SetModified(aqh->nodeDb);
  }
  else {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
}



AQH_NODE_INFO *_getOrCreateNodeAndUpdateUidAddr(AQHOMED *aqh, const GWEN_MSG *msg, uint32_t uid)
{
  uint8_t busAddr;
  AQH_NODE_INFO *ni;

  busAddr=AQH_NodeMsg_GetSourceAddress(msg);
  ni=AQH_NodeDb_GetNodeInfoByUid(aqh->nodeDb, uid);
  if (ni) {
    uint8_t storedBusAddr;

    storedBusAddr=AQH_NodeInfo_GetBusAddress(ni);
    if (busAddr!=0 && storedBusAddr!=busAddr) {
      DBG_INFO(AQH_LOGDOMAIN, "Changed busaddr for %08x from %02x to %02x", uid, storedBusAddr, busAddr);
      AQH_NodeInfo_SetBusAddress(ni, busAddr);
      _updateTimestampLastChange(ni);
      AQH_NodeDb_SetModified(aqh->nodeDb);
    }
  }
  else {
    int rv;

    ni=AQH_NodeInfo_new();
    AQH_NodeInfo_SetBusAddress(ni, busAddr);
    AQH_NodeInfo_SetUid(ni, uid);
    _updateTimestampLastChange(ni);
    rv=AQH_NodeDb_AddNodeInfo(aqh->nodeDb, ni);
    if (rv<0) {
      DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
      AQH_NodeInfo_free(ni);
      return NULL;
    }
    else {
      DBG_INFO(AQH_LOGDOMAIN, "Added node %08x (%02x)", uid, busAddr);
    }
  }
  return ni;
}



void _updateTimestampLastChange(AQH_NODE_INFO *ni)
{
  GWEN_TIMESTAMP *t;

  t=GWEN_Timestamp_NowInLocalTime();
  AQH_NodeInfo_SetTimestampLastChange(ni, t);
  GWEN_Timestamp_free(t);
}



