189 lines
3.8 KiB
C
189 lines
3.8 KiB
C
/****************************************************************************
|
|
* 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/msg/msg_node.h"
|
|
|
|
#include <gwenhywfar/debug.h>
|
|
|
|
|
|
|
|
static uint8_t _calcChecksum(const uint8_t *ptr, uint8_t len);
|
|
|
|
|
|
|
|
|
|
uint8_t AQH_NodeMsg_GetDestAddress(const GWEN_MSG *msg)
|
|
{
|
|
if (msg && GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
|
|
const uint8_t *ptr;
|
|
|
|
ptr=GWEN_Msg_GetConstBuffer(msg);
|
|
return ptr[AQH_MSG_OFFS_ALL_DEST_ADDRESS];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
uint8_t AQH_NodeMsg_GetMsgType(const GWEN_MSG *msg)
|
|
{
|
|
if (msg && GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
|
|
const uint8_t *ptr;
|
|
|
|
ptr=GWEN_Msg_GetConstBuffer(msg);
|
|
return ptr[AQH_MSG_OFFS_ALL_MSG_TYPE];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
uint8_t AQH_NodeMsg_GetSourceAddress(const GWEN_MSG *msg)
|
|
{
|
|
if (msg && GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
|
|
const uint8_t *ptr;
|
|
|
|
ptr=GWEN_Msg_GetConstBuffer(msg);
|
|
return ptr[AQH_MSG_OFFS_ALL_SRC_ADDRESS];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
uint8_t AQH_NodeMsg_GetMsgPayloadLen(const GWEN_MSG *msg)
|
|
{
|
|
if (msg && GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
|
|
const uint8_t *ptr;
|
|
|
|
ptr=GWEN_Msg_GetConstBuffer(msg);
|
|
return ptr[AQH_MSG_OFFS_ALL_PAYLOAD_LEN];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int AQH_NodeMsg_IsMsgComplete(const GWEN_MSG *msg)
|
|
{
|
|
if (msg) {
|
|
uint8_t msgLen;
|
|
|
|
msgLen=GWEN_Msg_GetBytesInBuffer(msg);
|
|
if (msgLen>=AQH_MSG_OFFS_ALL_DATA_BEGIN) {
|
|
const uint8_t *ptr;
|
|
uint8_t len;
|
|
|
|
ptr=GWEN_Msg_GetConstBuffer(msg);
|
|
len=ptr[AQH_MSG_OFFS_ALL_PAYLOAD_LEN]+AQH_MSG_OFFS_ALL_PAYLOAD_BEGIN+1;
|
|
if (len>AQH_MAXMSGSIZE)
|
|
return -1;
|
|
else if (msgLen>=len)
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int AQH_NodeMsg_IsChecksumValid(const GWEN_MSG *msg)
|
|
{
|
|
if (msg && AQH_NodeMsg_IsMsgComplete(msg))
|
|
return (_calcChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg))==0)?1:0;
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
int AQH_NodeMsg_AddChecksum(GWEN_MSG *msg)
|
|
{
|
|
if (msg) {
|
|
int rv;
|
|
|
|
rv=GWEN_Msg_AddByte(msg, _calcChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg)));
|
|
if (rv<0) {
|
|
DBG_INFO(NULL, "here (%d)", rv);
|
|
return rv;
|
|
}
|
|
return 0;
|
|
}
|
|
return GWEN_ERROR_GENERIC;
|
|
}
|
|
|
|
|
|
|
|
void AQH_NodeMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
|
|
{
|
|
GWEN_Buffer_AppendArgs(dbuf,
|
|
"0x%02x->0x%02x: %d %s\n",
|
|
AQH_NodeMsg_GetSourceAddress(msg),
|
|
AQH_NodeMsg_GetDestAddress(msg),
|
|
AQH_NodeMsg_GetMsgType(msg),
|
|
sText);
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t AQH_NodeMsg_GetMsgGroup(uint8_t msgType)
|
|
{
|
|
switch(msgType) {
|
|
case AQH_MSG_TYPE_PING:
|
|
case AQH_MSG_TYPE_PONG:
|
|
case AQH_MSG_TYPE_COMSENDSTATS:
|
|
case AQH_MSG_TYPE_COMRECVSTATS:
|
|
case AQH_MSG_TYPE_TWIBUSMEMBER:
|
|
case AQH_MSG_TYPE_DEBUG:
|
|
case AQH_MSG_TYPE_DEVICE:
|
|
return AQH_MSG_TYPEGROUP_INFO;
|
|
case AQH_MSG_TYPE_VALUE:
|
|
case AQH_MSG_TYPE_VALUE2:
|
|
return AQH_MSG_TYPEGROUP_VALUES;
|
|
case AQH_MSG_TYPE_NEED_ADDRESS:
|
|
case AQH_MSG_TYPE_HAVE_ADDRESS:
|
|
case AQH_MSG_TYPE_CLAIM_ADDRESS:
|
|
case AQH_MSG_TYPE_DENY_ADDRESS:
|
|
case AQH_MSG_TYPE_ADDRESS_RANGE:
|
|
return AQH_MSG_TYPEGROUP_ADDRESS;
|
|
case AQH_MSG_TYPE_NET_SET_ACCEPTED_MSGGROUPS:
|
|
return AQH_MSG_TYPEGROUP_ADMIN;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t _calcChecksum(const uint8_t *ptr, uint8_t len)
|
|
{
|
|
int i;
|
|
uint8_t x=0;
|
|
|
|
for (i=0; i<len; i++, ptr++) {
|
|
x^=*ptr;
|
|
}
|
|
|
|
return x;
|
|
}
|
|
|
|
|
|
|
|
|