/**************************************************************************** * 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 "./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_value3.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 #include #include #include #include /* ------------------------------------------------------------------------------------------------ * forward declarations * ------------------------------------------------------------------------------------------------ */ static void _handleMsgValue2(AQHOMED *aqh, const GWEN_MSG *msg); static void _handleMsgValue3(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); static void _assignDeviceId(AQHOMED *aqh, AQH_NODE_INFO *ni, uint32_t uid); /* ------------------------------------------------------------------------------------------------ * 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_VALUE_REPORT: _handleMsgValue3(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 AqHomed_WriteNodeDb(AQHOMED *aqh) { if (aqh->dbFile) { GWEN_DB_NODE *dbNodeDb; AQH_NodeDb_ClearModified(aqh->nodeDb); dbNodeDb=GWEN_DB_Group_new("nodeDb"); AQH_NodeDb_toDb(aqh->nodeDb, dbNodeDb); GWEN_DB_WriteFile(dbNodeDb, aqh->dbFile, GWEN_DB_FLAGS_DEFAULT); GWEN_DB_Group_free(dbNodeDb); } } 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 _handleMsgValue3(AQHOMED *aqh, const GWEN_MSG *msg) { AQH_NODE_INFO *ni; uint32_t uid; uid=AQH_Value3Msg_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) { const char *s; AQH_NodeInfo_SetManufacturer(ni, AQH_DeviceMsg_GetManufacturer(msg)); AQH_NodeInfo_SetDeviceType(ni, AQH_DeviceMsg_GetDeviceType(msg)); AQH_NodeInfo_SetDeviceVersion(ni, (AQH_DeviceMsg_GetDeviceVersion(msg)<<8)+AQH_DeviceMsg_GetDeviceRevision(msg)); AQH_NodeInfo_SetFirmwareVersion(ni, (AQH_DeviceMsg_GetFirmwareVariant(msg)<<24) | (AQH_DeviceMsg_GetFirmwareVersionMajor(msg)<<16) | (AQH_DeviceMsg_GetFirmwareVersionMinor(msg)<<8) | AQH_DeviceMsg_GetFirmwareVersionPatchlevel(msg)); s=AQH_NodeInfo_GetDeviceId(ni); if (!(s && *s)) _assignDeviceId(aqh, ni, uid); _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) { const char *s; AQH_NodeInfo_SetManufacturer(ni, AQH_FlashReadyMsg_GetManufacturer(msg)); AQH_NodeInfo_SetDeviceType(ni, AQH_FlashReadyMsg_GetDeviceType(msg)); AQH_NodeInfo_SetDeviceVersion(ni, (AQH_FlashReadyMsg_GetDeviceVersion(msg)<<8)+AQH_FlashReadyMsg_GetDeviceRevision(msg)); AQH_NodeInfo_SetFirmwareVersion(ni, (AQH_FlashReadyMsg_GetFirmwareVariant(msg)<<24) | (AQH_FlashReadyMsg_GetFirmwareVersionMajor(msg)<<16) | (AQH_FlashReadyMsg_GetFirmwareVersionMinor(msg)<<8) | AQH_FlashReadyMsg_GetFirmwareVersionPatchlevel(msg)); s=AQH_NodeInfo_GetDeviceId(ni); if (!(s && *s)) _assignDeviceId(aqh, ni, uid); _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 _assignDeviceId(AQHOMED *aqh, AQH_NODE_INFO *ni, uint32_t uid) { const AQHNODE_DEVICE *dev; dev=AqHomed_FindDeviceDef(aqh, AQH_NodeInfo_GetManufacturer(ni), AQH_NodeInfo_GetDeviceType(ni), AQH_NodeInfo_GetDeviceVersion(ni)); if (dev==NULL) { DBG_ERROR(NULL, "Unknown NODE device encountered (%08x, %04x, %04x)", AQH_NodeInfo_GetManufacturer(ni), AQH_NodeInfo_GetDeviceType(ni), AQH_NodeInfo_GetDeviceVersion(ni)); } else { const char *s; s=AQHNODE_Device_GetName(dev); DBG_ERROR(NULL, "Found device \"%s\" (%08x)", s?s:"", uid); AQH_NodeInfo_SetDeviceId(ni, s); } } void _updateTimestampLastChange(AQH_NODE_INFO *ni) { GWEN_TIMESTAMP *t; t=GWEN_Timestamp_NowInLocalTime(); AQH_NodeInfo_SetTimestampLastChange(ni, t); GWEN_Timestamp_free(t); }