aqhome: started parsing messages and extract info for node db.
This commit is contained in:
@@ -11,19 +11,38 @@
|
||||
#endif
|
||||
|
||||
|
||||
#include "aqhome/msgmanager.h"
|
||||
#include "aqhome/msgmanager_p.h"
|
||||
#include "aqhome/msg/endpointmgr.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 <gwenhywfar/misc.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
GWEN_INHERIT(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER)
|
||||
|
||||
|
||||
|
||||
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
|
||||
static void _loopOnceOverEndpoints(GWEN_MSG_ENDPOINT_MGR *emgr);
|
||||
static void _handleEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep);
|
||||
static void _handleNodeMsg(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
|
||||
static void _handleIpcMsg(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
|
||||
static void _handleMsgValue2(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
|
||||
static void _handleMsgNeedAddress(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
|
||||
static void _handleMsgClaimAddress(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
|
||||
static void _handleMsgHaveAddress(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
|
||||
static void _handleMsgComSendStat(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
|
||||
static void _handleMsgComRecvStat(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
|
||||
static void _handleMsgDevice(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg);
|
||||
static AQH_NODE_INFO *_getOrCreateNodeAndUpdateUidAddr(GWEN_MSG_ENDPOINT_MGR *emgr, const GWEN_MSG *msg, uint32_t uid);
|
||||
|
||||
|
||||
|
||||
@@ -31,7 +50,26 @@ static void _handleIpcMsg(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, co
|
||||
|
||||
GWEN_MSG_ENDPOINT_MGR *AQH_MsgManager_new(uint8_t busAddr)
|
||||
{
|
||||
return AQH_MsgEndpointMgr_new(busAddr);
|
||||
GWEN_MSG_ENDPOINT_MGR *mgr;
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
mgr=AQH_MsgEndpointMgr_new(busAddr);
|
||||
GWEN_NEW_OBJECT(AQH_MSG_MANAGER, xmgr);
|
||||
GWEN_INHERIT_SETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, mgr, xmgr, _freeData);
|
||||
xmgr->nodeDb=AQH_NodeDb_new();
|
||||
|
||||
return mgr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _freeData(void *bp, void *p)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
xmgr=(AQH_MSG_MANAGER*) p;
|
||||
AQH_NodeDb_free(xmgr->nodeDb);
|
||||
GWEN_FREE_OBJECT(xmgr);
|
||||
}
|
||||
|
||||
|
||||
@@ -50,9 +88,11 @@ int AQH_MsgManager_LoopOnce(GWEN_MSG_ENDPOINT_MGR *emgr)
|
||||
|
||||
void _loopOnceOverEndpoints(GWEN_MSG_ENDPOINT_MGR *emgr)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
GWEN_MSG_ENDPOINT_LIST *endpointList;
|
||||
|
||||
DBG_DEBUG(AQH_LOGDOMAIN, "Handle endpoint messages");
|
||||
xmgr=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, emgr);
|
||||
endpointList=GWEN_MsgEndpointMgr_GetEndpointList(emgr);
|
||||
if (endpointList) {
|
||||
GWEN_MSG_ENDPOINT *ep;
|
||||
@@ -63,6 +103,15 @@ void _loopOnceOverEndpoints(GWEN_MSG_ENDPOINT_MGR *emgr)
|
||||
ep=GWEN_MsgEndpoint_List_Next(ep);
|
||||
} /* while */
|
||||
}
|
||||
|
||||
if (AQH_NodeDb_IsModified(xmgr->nodeDb)) {
|
||||
GWEN_DB_NODE *dbNodeDb;
|
||||
|
||||
dbNodeDb=GWEN_DB_Group_new("nodeDb");
|
||||
AQH_NodeDb_toDb(xmgr->nodeDb, dbNodeDb);
|
||||
GWEN_DB_WriteFile(dbNodeDb, "nodedb.db", GWEN_DB_FLAGS_DEFAULT);
|
||||
GWEN_DB_Group_free(dbNodeDb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,11 +140,31 @@ void _handleEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep)
|
||||
|
||||
void _handleNodeMsg(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
|
||||
{
|
||||
int msgIsValid;
|
||||
uint8_t msgType;
|
||||
|
||||
DBG_INFO(AQH_LOGDOMAIN,
|
||||
" - msg %d from %d to %d",
|
||||
AQH_NodeMsg_GetMsgType(msg), AQH_NodeMsg_GetSourceAddress(msg), AQH_NodeMsg_GetDestAddress(msg));
|
||||
|
||||
|
||||
AQH_MsgEndpointMgr_DistributeMsgFromNodeEndpoint(emgr, ep, msg, AQH_MSGMGR_ENDPOINTGROUP_NODE);
|
||||
|
||||
msgIsValid=(AQH_NodeMsg_IsChecksumValid(msg) && AQH_NodeMsg_IsMsgComplete(msg));
|
||||
msgType=AQH_NodeMsg_GetMsgType(msg);
|
||||
|
||||
if (msgIsValid) {
|
||||
switch(msgType) {
|
||||
case AQH_MSG_TYPE_COMSENDSTATS: _handleMsgComSendStat(emgr, ep, msg); break;
|
||||
case AQH_MSG_TYPE_COMRECVSTATS: _handleMsgComRecvStat(emgr, ep, msg); break;
|
||||
case AQH_MSG_TYPE_VALUE2: _handleMsgValue2(emgr, ep, msg); break;
|
||||
case AQH_MSG_TYPE_NEED_ADDRESS: _handleMsgNeedAddress(emgr, ep, msg); break;
|
||||
case AQH_MSG_TYPE_CLAIM_ADDRESS: _handleMsgClaimAddress(emgr, ep, msg); break;
|
||||
case AQH_MSG_TYPE_HAVE_ADDRESS: _handleMsgHaveAddress(emgr, ep, msg); break;
|
||||
case AQH_MSG_TYPE_DEVICE: _handleMsgDevice(emgr, ep, msg); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,5 +176,198 @@ void _handleIpcMsg(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWE
|
||||
|
||||
|
||||
|
||||
void _handleMsgValue2(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
xmgr=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, emgr);
|
||||
if (xmgr) {
|
||||
AQH_NODE_INFO *ni;
|
||||
uint32_t uid;
|
||||
|
||||
uid=AQH_Value2Msg_GetUid(msg);
|
||||
ni=_getOrCreateNodeAndUpdateUidAddr(emgr, msg, uid);
|
||||
if (ni==NULL) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleMsgNeedAddress(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
xmgr=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, emgr);
|
||||
if (xmgr) {
|
||||
AQH_NODE_INFO *ni;
|
||||
uint32_t uid;
|
||||
|
||||
uid=AQH_NeedAddrMsg_GetUid(msg);
|
||||
ni=_getOrCreateNodeAndUpdateUidAddr(emgr, msg, uid);
|
||||
if (ni==NULL) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleMsgClaimAddress(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
xmgr=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, emgr);
|
||||
if (xmgr) {
|
||||
AQH_NODE_INFO *ni;
|
||||
uint32_t uid;
|
||||
|
||||
uid=AQH_ClaimAddrMsg_GetUid(msg);
|
||||
ni=_getOrCreateNodeAndUpdateUidAddr(emgr, msg, uid);
|
||||
if (ni==NULL) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleMsgHaveAddress(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
xmgr=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, emgr);
|
||||
if (xmgr) {
|
||||
AQH_NODE_INFO *ni;
|
||||
uint32_t uid;
|
||||
|
||||
uid=AQH_HaveAddrMsg_GetUid(msg);
|
||||
ni=_getOrCreateNodeAndUpdateUidAddr(emgr, msg, uid);
|
||||
if (ni==NULL) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleMsgComSendStat(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
xmgr=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, emgr);
|
||||
if (xmgr) {
|
||||
AQH_NODE_INFO *ni;
|
||||
uint32_t uid;
|
||||
|
||||
uid=AQH_SendStatsMsg_GetUid(msg);
|
||||
ni=_getOrCreateNodeAndUpdateUidAddr(emgr, 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_SetStatsAborted(ni, AQH_SendStatsMsg_GetAborted(msg));
|
||||
AQH_NodeDb_SetModified(xmgr->nodeDb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleMsgComRecvStat(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
xmgr=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, emgr);
|
||||
if (xmgr) {
|
||||
AQH_NODE_INFO *ni;
|
||||
uint32_t uid;
|
||||
|
||||
uid=AQH_RecvStatsMsg_GetUid(msg);
|
||||
ni=_getOrCreateNodeAndUpdateUidAddr(emgr, msg, uid);
|
||||
if (ni==NULL) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
|
||||
}
|
||||
AQH_NodeInfo_SetStatsPacketsIn(ni, AQH_RecvStatsMsg_GetPacketsIn(msg));
|
||||
AQH_NodeInfo_SetStatsErrors(ni, AQH_RecvStatsMsg_GetErrors(msg));
|
||||
AQH_NodeInfo_SetStatsHandled(ni, AQH_RecvStatsMsg_GetHandled(msg));
|
||||
AQH_NodeDb_SetModified(xmgr->nodeDb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleMsgDevice(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
xmgr=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, emgr);
|
||||
if (xmgr) {
|
||||
AQH_NODE_INFO *ni;
|
||||
uint32_t uid;
|
||||
|
||||
uid=AQH_DeviceMsg_GetUid(msg);
|
||||
ni=_getOrCreateNodeAndUpdateUidAddr(emgr, 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_GetModuleMaskHigh(msg)<<8) | AQH_DeviceMsg_GetModuleMaskLow(msg));
|
||||
AQH_NodeDb_SetModified(xmgr->nodeDb);
|
||||
}
|
||||
else {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQH_NODE_INFO *_getOrCreateNodeAndUpdateUidAddr(GWEN_MSG_ENDPOINT_MGR *emgr, const GWEN_MSG *msg, uint32_t uid)
|
||||
{
|
||||
AQH_MSG_MANAGER *xmgr;
|
||||
|
||||
xmgr=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT_MGR, AQH_MSG_MANAGER, emgr);
|
||||
if (xmgr) {
|
||||
uint8_t busAddr;
|
||||
AQH_NODE_INFO *ni;
|
||||
|
||||
busAddr=AQH_NodeMsg_GetSourceAddress(msg);
|
||||
ni=AQH_NodeDb_GetNodeInfoByUid(xmgr->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);
|
||||
AQH_NodeDb_SetModified(xmgr->nodeDb);
|
||||
}
|
||||
}
|
||||
else {
|
||||
int rv;
|
||||
|
||||
ni=AQH_NodeInfo_new();
|
||||
AQH_NodeInfo_SetBusAddress(ni, busAddr);
|
||||
AQH_NodeInfo_SetUid(ni, uid);
|
||||
rv=AQH_NodeDb_AddNodeInfo(xmgr->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;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user