From 7eb462173c93ce3303a917330e8cb175b096feed Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Fri, 7 Apr 2023 23:22:40 +0200 Subject: [PATCH] simplified message handling, switch from XOR checksum to CRC8 with polynomial 0x97. 0x97 allows for detection of all 1-3 bit errors in a message of up to 119 bytes (see https://www.faa.gov/aircraft/air_cert/design_approvals/air_software/media/TC-14-49.pdf) --- aqhome/msg/msg_claimaddr.c | 24 ++++------ aqhome/msg/msg_claimaddr.h | 8 ---- aqhome/msg/msg_denyaddr.c | 25 ++++------- aqhome/msg/msg_denyaddr.h | 8 ---- aqhome/msg/msg_device.c | 65 +++++++--------------------- aqhome/msg/msg_device.h | 12 +---- aqhome/msg/msg_haveaddr.c | 24 ++++------ aqhome/msg/msg_haveaddr.h | 8 ---- aqhome/msg/msg_needaddr.c | 15 +++---- aqhome/msg/msg_needaddr.h | 7 --- aqhome/msg/msg_node.c | 89 ++++++++++++++++++++++++++++++-------- aqhome/msg/msg_node.h | 6 ++- aqhome/msg/msg_ping.c | 14 +++--- aqhome/msg/msg_ping.h | 7 --- aqhome/msg/msg_pong.c | 14 +++--- aqhome/msg/msg_pong.h | 7 --- aqhome/msg/msg_recvstats.c | 67 ++++++++++++++-------------- aqhome/msg/msg_recvstats.h | 3 ++ aqhome/msg/msg_sendstats.c | 47 ++++++-------------- aqhome/msg/msg_sendstats.h | 1 + aqhome/msg/msg_value.c | 27 ++---------- aqhome/msg/msg_value2.c | 53 ++++++----------------- aqhome/msg/msg_value2.h | 8 ---- aqhome/msgmanager.c | 2 +- avr/MESSAGES | 5 +++ avr/com.asm | 10 +++-- avr/com_crc.asm | 24 ++++++++-- avr/com_recv.asm | 8 ++-- avr/comproto.asm | 18 ++++---- avr/comproto_addr.asm | 2 +- avr/comproto_device.asm | 2 +- avr/comproto_stats.asm | 36 ++++++++++----- avr/comproto_values.asm | 2 +- 33 files changed, 281 insertions(+), 367 deletions(-) diff --git a/aqhome/msg/msg_claimaddr.c b/aqhome/msg/msg_claimaddr.c index 994015d..fb990b3 100644 --- a/aqhome/msg/msg_claimaddr.c +++ b/aqhome/msg/msg_claimaddr.c @@ -18,31 +18,23 @@ #include +#define AQH_MSG_OFFS_CLAIMADDR_UID 0 +#define AQH_MSG_OFFS_CLAIMADDR_ADDR 4 + +#define AQH_MSG_CLAIMADDR_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_CLAIMADDR_ADDR+1) + + uint32_t AQH_ClaimAddrMsg_GetUid(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_CLAIM_ADDRESS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_CLAIMADDR_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_CLAIMADDR_UID; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_CLAIMADDR_UID, 0); } uint8_t AQH_ClaimAddrMsg_GetAddress(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_CLAIM_ADDRESS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_CLAIMADDR_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_CLAIMADDR_ADDR; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_CLAIMADDR_ADDR, 0); } diff --git a/aqhome/msg/msg_claimaddr.h b/aqhome/msg/msg_claimaddr.h index 834c993..2092344 100644 --- a/aqhome/msg/msg_claimaddr.h +++ b/aqhome/msg/msg_claimaddr.h @@ -18,14 +18,6 @@ -#define AQH_MSG_OFFS_CLAIMADDR_UID 0 -#define AQH_MSG_OFFS_CLAIMADDR_ADDR 4 - -#define AQH_MSG_CLAIMADDR_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_CLAIMADDR_ADDR+1) - - - - AQHOME_API uint32_t AQH_ClaimAddrMsg_GetUid(const GWEN_MSG *msg); AQHOME_API uint8_t AQH_ClaimAddrMsg_GetAddress(const GWEN_MSG *msg); AQHOME_API void AQH_ClaimAddrMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); diff --git a/aqhome/msg/msg_denyaddr.c b/aqhome/msg/msg_denyaddr.c index cead745..2f2ce93 100644 --- a/aqhome/msg/msg_denyaddr.c +++ b/aqhome/msg/msg_denyaddr.c @@ -19,30 +19,23 @@ +#define AQH_MSG_OFFS_DENYADDR_UID 0 +#define AQH_MSG_OFFS_DENYADDR_ADDR 4 + +#define AQH_MSG_DENYADDR_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DENYADDR_ADDR+1) + + + uint32_t AQH_DenyAddrMsg_GetUid(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_DENY_ADDRESS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DENYADDR_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DENYADDR_UID; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DENYADDR_UID, 0); } uint8_t AQH_DenyAddrMsg_GetAddress(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_DENY_ADDRESS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DENYADDR_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DENYADDR_ADDR; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DENYADDR_ADDR, 0); } diff --git a/aqhome/msg/msg_denyaddr.h b/aqhome/msg/msg_denyaddr.h index 09d69a8..4423d41 100644 --- a/aqhome/msg/msg_denyaddr.h +++ b/aqhome/msg/msg_denyaddr.h @@ -18,14 +18,6 @@ -#define AQH_MSG_OFFS_DENYADDR_UID 0 -#define AQH_MSG_OFFS_DENYADDR_ADDR 4 - -#define AQH_MSG_DENYADDR_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DENYADDR_ADDR+1) - - - - AQHOME_API uint32_t AQH_DenyAddrMsg_GetUid(const GWEN_MSG *msg); AQHOME_API uint8_t AQH_DenyAddrMsg_GetAddress(const GWEN_MSG *msg); AQHOME_API void AQH_DenyAddrMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); diff --git a/aqhome/msg/msg_device.c b/aqhome/msg/msg_device.c index a2e400b..fa5bac4 100644 --- a/aqhome/msg/msg_device.c +++ b/aqhome/msg/msg_device.c @@ -18,82 +18,47 @@ #include +#define AQH_MSG_OFFS_DEVICE_UID 0 /* 4 bytes */ +#define AQH_MSG_OFFS_DEVICE_FWTYPE 4 /* 2 bytes */ +#define AQH_MSG_OFFS_DEVICE_FWLOW 6 /* 1 byte */ +#define AQH_MSG_OFFS_DEVICE_FWHIGH 7 /* 1 byte */ +#define AQH_MSG_OFFS_DEVICE_MODULES 8 /* 2 byte */ + +#define AQH_MSG_DEVICE_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_MODULES+2) + uint32_t AQH_DeviceMsg_GetUid(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DEVICE_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_UID; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_UID, 0); } uint16_t AQH_DeviceMsg_GetFirmwareType(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DEVICE_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_FWTYPE; - return (uint16_t)((ptr[0])+(ptr[1]<<8)); - } - return 0; + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_FWTYPE, 0); } uint8_t AQH_DeviceMsg_GetFirmwareLow(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DEVICE_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_FWLOW; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_FWLOW, 0); } uint8_t AQH_DeviceMsg_GetFirmwareHigh(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DEVICE_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_FWHIGH; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_FWHIGH, 0); } -uint8_t AQH_DeviceMsg_GetModuleMaskLow(const GWEN_MSG *msg) +uint16_t AQH_DeviceMsg_GetModuleMask(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DEVICE_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_MODULESLOW; - return ptr[0]; - } - return 0; -} - - - -uint8_t AQH_DeviceMsg_GetModuleMaskHigh(const GWEN_MSG *msg) -{ - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DEVICE_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_MODULESHIGH; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_MODULES, 0); } @@ -103,7 +68,7 @@ void AQH_DeviceMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const ch if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_DEVICE_MINSIZE) { uint16_t modules; - modules=(AQH_DeviceMsg_GetModuleMaskHigh(msg)<<8) | AQH_DeviceMsg_GetModuleMaskLow(msg); + modules=AQH_DeviceMsg_GetModuleMask(msg); GWEN_Buffer_AppendArgs(dbuf, "0x%02x->0x%02x: DEVICE %s (uid=0x%08x, fw type=%d, fw ver=%d.%d, mods=0x%04x", AQH_NodeMsg_GetSourceAddress(msg), AQH_NodeMsg_GetDestAddress(msg), diff --git a/aqhome/msg/msg_device.h b/aqhome/msg/msg_device.h index 67c12f7..8d37d5f 100644 --- a/aqhome/msg/msg_device.h +++ b/aqhome/msg/msg_device.h @@ -18,15 +18,6 @@ -#define AQH_MSG_OFFS_DEVICE_UID 0 /* 4 bytes */ -#define AQH_MSG_OFFS_DEVICE_FWTYPE 4 /* 2 bytes */ -#define AQH_MSG_OFFS_DEVICE_FWLOW 6 /* 1 byte */ -#define AQH_MSG_OFFS_DEVICE_FWHIGH 7 /* 1 byte */ -#define AQH_MSG_OFFS_DEVICE_MODULESLOW 8 /* 1 byte */ -#define AQH_MSG_OFFS_DEVICE_MODULESHIGH 9 /* 1 byte */ - -#define AQH_MSG_DEVICE_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_DEVICE_MODULESHIGH+1) - #define AQH_MSG_DEVICE_MASK_TIMER 0x02 #define AQH_MSG_DEVICE_MASK_COM 0x04 #define AQH_MSG_DEVICE_MASK_LED 0x08 @@ -40,8 +31,7 @@ AQHOME_API uint32_t AQH_DeviceMsg_GetUid(const GWEN_MSG *msg); AQHOME_API uint16_t AQH_DeviceMsg_GetFirmwareType(const GWEN_MSG *msg); AQHOME_API uint8_t AQH_DeviceMsg_GetFirmwareLow(const GWEN_MSG *msg); AQHOME_API uint8_t AQH_DeviceMsg_GetFirmwareHigh(const GWEN_MSG *msg); -AQHOME_API uint8_t AQH_DeviceMsg_GetModuleMaskLow(const GWEN_MSG *msg); -AQHOME_API uint8_t AQH_DeviceMsg_GetModuleMaskHigh(const GWEN_MSG *msg); +AQHOME_API uint16_t AQH_DeviceMsg_GetModuleMask(const GWEN_MSG *msg); AQHOME_API void AQH_DeviceMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); diff --git a/aqhome/msg/msg_haveaddr.c b/aqhome/msg/msg_haveaddr.c index 0020bf3..13c4467 100644 --- a/aqhome/msg/msg_haveaddr.c +++ b/aqhome/msg/msg_haveaddr.c @@ -18,31 +18,23 @@ #include +#define AQH_MSG_OFFS_HAVEADDR_UID 0 +#define AQH_MSG_OFFS_HAVEADDR_ADDR 4 + +#define AQH_MSG_HAVEADDR_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_HAVEADDR_ADDR+1) + + uint32_t AQH_HaveAddrMsg_GetUid(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_HAVE_ADDRESS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_HAVEADDR_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_HAVEADDR_UID; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_HAVEADDR_UID, 0); } uint8_t AQH_HaveAddrMsg_GetAddress(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_HAVE_ADDRESS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_HAVEADDR_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_HAVEADDR_ADDR; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_HAVEADDR_ADDR, 0); } diff --git a/aqhome/msg/msg_haveaddr.h b/aqhome/msg/msg_haveaddr.h index 7d75b7a..059165f 100644 --- a/aqhome/msg/msg_haveaddr.h +++ b/aqhome/msg/msg_haveaddr.h @@ -18,14 +18,6 @@ -#define AQH_MSG_OFFS_HAVEADDR_UID 0 -#define AQH_MSG_OFFS_HAVEADDR_ADDR 4 - -#define AQH_MSG_HAVEADDR_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_HAVEADDR_ADDR+1) - - - - AQHOME_API uint32_t AQH_HaveAddrMsg_GetUid(const GWEN_MSG *msg); AQHOME_API uint8_t AQH_HaveAddrMsg_GetAddress(const GWEN_MSG *msg); AQHOME_API void AQH_HaveAddrMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); diff --git a/aqhome/msg/msg_needaddr.c b/aqhome/msg/msg_needaddr.c index 6927f84..b5a6827 100644 --- a/aqhome/msg/msg_needaddr.c +++ b/aqhome/msg/msg_needaddr.c @@ -19,16 +19,15 @@ +#define AQH_MSG_OFFS_NEEDADDR_UID 0 + +#define AQH_MSG_NEEDADDR_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_NEEDADDR_UID+4) + + + uint32_t AQH_NeedAddrMsg_GetUid(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_NEED_ADDRESS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_NEEDADDR_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_NEEDADDR_UID; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_NEEDADDR_UID, 0); } diff --git a/aqhome/msg/msg_needaddr.h b/aqhome/msg/msg_needaddr.h index a55bda6..eb1cf21 100644 --- a/aqhome/msg/msg_needaddr.h +++ b/aqhome/msg/msg_needaddr.h @@ -18,13 +18,6 @@ -#define AQH_MSG_OFFS_NEEDADDR_UID 0 - -#define AQH_MSG_NEEDADDR_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_NEEDADDR_UID+4) - - - - AQHOME_API uint32_t AQH_NeedAddrMsg_GetUid(const GWEN_MSG *msg); AQHOME_API void AQH_NeedAddrMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); diff --git a/aqhome/msg/msg_node.c b/aqhome/msg/msg_node.c index 814cefb..ea02914 100644 --- a/aqhome/msg/msg_node.c +++ b/aqhome/msg/msg_node.c @@ -16,10 +16,15 @@ #include +#define COM_USE_CRC8 1 + + +#ifdef COM_USE_CRC8 +static uint8_t _calcCrc8Checksum(const uint8_t *ptr, uint8_t len); +#else static uint8_t _calcXorChecksum(const uint8_t *ptr, uint8_t len); - - +#endif uint8_t AQH_NodeMsg_GetDestAddress(const GWEN_MSG *msg) @@ -104,8 +109,13 @@ int AQH_NodeMsg_IsMsgComplete(const GWEN_MSG *msg) int AQH_NodeMsg_IsChecksumValid(const GWEN_MSG *msg) { - if (msg && AQH_NodeMsg_IsMsgComplete(msg)) + if (msg && AQH_NodeMsg_IsMsgComplete(msg)) { +#ifdef COM_USE_CRC8 + return (_calcCrc8Checksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg))==0)?1:0; +#else return (_calcXorChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg))==0)?1:0; +#endif + } return 0; } @@ -116,7 +126,11 @@ int AQH_NodeMsg_AddChecksum(GWEN_MSG *msg) if (msg) { int rv; +#ifdef COM_USE_CRC8 + rv=GWEN_Msg_AddByte(msg, _calcCrc8Checksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg))); +#else rv=GWEN_Msg_AddByte(msg, _calcXorChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg))); +#endif if (rv<0) { DBG_INFO(NULL, "here (%d)", rv); return rv; @@ -128,6 +142,45 @@ int AQH_NodeMsg_AddChecksum(GWEN_MSG *msg) +uint32_t AQH_NodeMsg_GetUint32At(const GWEN_MSG *msg, int offs, int defaultValue) +{ + if ((GWEN_Msg_GetBytesInBuffer(msg)>=offs+4)) { + const uint8_t *ptr; + + ptr=GWEN_Msg_GetConstBuffer(msg)+offs; + return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); + } + return defaultValue; +} + + + +uint16_t AQH_NodeMsg_GetUint16At(const GWEN_MSG *msg, int offs, int defaultValue) +{ + if ((GWEN_Msg_GetBytesInBuffer(msg)>=offs+2)) { + const uint8_t *ptr; + + ptr=GWEN_Msg_GetConstBuffer(msg)+offs; + return (uint16_t)(ptr[0])+(ptr[1]<<8); + } + return defaultValue; +} + + + +uint8_t AQH_NodeMsg_GetUint8At(const GWEN_MSG *msg, int offs, int defaultValue) +{ + if ((GWEN_Msg_GetBytesInBuffer(msg)>=offs+1)) { + const uint8_t *ptr; + + ptr=GWEN_Msg_GetConstBuffer(msg)+offs; + return ptr[0]; + } + return defaultValue; +} + + + void AQH_NodeMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText) { GWEN_Buffer_AppendArgs(dbuf, @@ -171,20 +224,7 @@ uint32_t AQH_NodeMsg_GetMsgGroup(uint8_t msgType) -uint8_t _calcXorChecksum(const uint8_t *ptr, uint8_t len) -{ - int i; - uint8_t x=0; - - for (i=0; i -#define AQH_MAXMSGSIZE 16 +#define AQH_MAXMSGSIZE 24 #define AQH_MSG_OFFS_ALL_DEST_ADDRESS 0 @@ -69,6 +69,10 @@ AQHOME_API int AQH_NodeMsg_IsMsgComplete(const GWEN_MSG *msg); AQHOME_API int AQH_NodeMsg_IsChecksumValid(const GWEN_MSG *msg); AQHOME_API int AQH_NodeMsg_AddChecksum(GWEN_MSG *msg); +AQHOME_API uint32_t AQH_NodeMsg_GetUint32At(const GWEN_MSG *msg, int offs, int defaultValue); +AQHOME_API uint16_t AQH_NodeMsg_GetUint16At(const GWEN_MSG *msg, int offs, int defaultValue); +AQHOME_API uint8_t AQH_NodeMsg_GetUint8At(const GWEN_MSG *msg, int offs, int defaultValue); + AQHOME_API void AQH_NodeMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); AQHOME_API uint32_t AQH_NodeMsg_GetMsgGroup(uint8_t msgType); diff --git a/aqhome/msg/msg_ping.c b/aqhome/msg/msg_ping.c index 85b8d2e..976c284 100644 --- a/aqhome/msg/msg_ping.c +++ b/aqhome/msg/msg_ping.c @@ -18,17 +18,15 @@ #include +#define AQH_MSG_OFFS_PING_TIMESTAMP 0 + +#define AQH_MSG_PING_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_PING_TIMESTAMP+4) + + uint32_t AQH_PingMsg_GetTimestamp(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_PING) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_PING_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_PING_TIMESTAMP; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_PING_TIMESTAMP, 0); } diff --git a/aqhome/msg/msg_ping.h b/aqhome/msg/msg_ping.h index d575fab..c4b9dde 100644 --- a/aqhome/msg/msg_ping.h +++ b/aqhome/msg/msg_ping.h @@ -18,13 +18,6 @@ -#define AQH_MSG_OFFS_PING_TIMESTAMP 0 - -#define AQH_MSG_PING_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_PING_TIMESTAMP+4) - - - - AQHOME_API uint32_t AQH_PingMsg_GetTimestamp(const GWEN_MSG *msg); AQHOME_API void AQH_PingMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); diff --git a/aqhome/msg/msg_pong.c b/aqhome/msg/msg_pong.c index 363a600..2ff9289 100644 --- a/aqhome/msg/msg_pong.c +++ b/aqhome/msg/msg_pong.c @@ -18,17 +18,15 @@ #include +#define AQH_MSG_OFFS_PONG_TIMESTAMP 0 + +#define AQH_MSG_PONG_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_PONG_TIMESTAMP+4) + + uint32_t AQH_PongMsg_GetTimestamp(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_PONG) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_PONG_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_PONG_TIMESTAMP; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_PONG_TIMESTAMP, 0); } diff --git a/aqhome/msg/msg_pong.h b/aqhome/msg/msg_pong.h index 693626d..0cb77bb 100644 --- a/aqhome/msg/msg_pong.h +++ b/aqhome/msg/msg_pong.h @@ -18,13 +18,6 @@ -#define AQH_MSG_OFFS_PONG_TIMESTAMP 0 - -#define AQH_MSG_PONG_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_PONG_TIMESTAMP+4) - - - - AQHOME_API uint32_t AQH_PongMsg_GetTimestamp(const GWEN_MSG *msg); AQHOME_API void AQH_PongMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); diff --git a/aqhome/msg/msg_recvstats.c b/aqhome/msg/msg_recvstats.c index 54b00a4..b710fd8 100644 --- a/aqhome/msg/msg_recvstats.c +++ b/aqhome/msg/msg_recvstats.c @@ -19,10 +19,13 @@ -#define AQH_MSG_OFFS_RECVSTATS_UID 0 -#define AQH_MSG_OFFS_RECVSTATS_PACKETSIN 4 -#define AQH_MSG_OFFS_RECVSTATS_CRCERRORS 6 +#define AQH_MSG_OFFS_RECVSTATS_UID 0 +#define AQH_MSG_OFFS_RECVSTATS_PACKETSIN 4 +#define AQH_MSG_OFFS_RECVSTATS_CRCERRORS 6 #define AQH_MSG_OFFS_RECVSTATS_IOERRORS 8 +#define AQH_MSG_OFFS_RECVSTATS_NOBUFFER 10 +#define AQH_MSG_OFFS_RECVSTATS_HANDLED 12 +#define AQH_MSG_OFFS_RECVSTATS_MISSED 14 #define AQH_MSG_RECVSTATS_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_IOERRORS+2) @@ -30,52 +33,49 @@ uint32_t AQH_RecvStatsMsg_GetUid(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_RECVSTATS_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_UID; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_UID, 0); } uint16_t AQH_RecvStatsMsg_GetPacketsIn(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_RECVSTATS_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_PACKETSIN; - return (uint16_t)(ptr[0])+(ptr[1]<<8); - } - return 0; + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_PACKETSIN, 0); } uint16_t AQH_RecvStatsMsg_GetCrcErrors(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_RECVSTATS_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_CRCERRORS; - return (uint16_t)(ptr[0])+(ptr[1]<<8); - } - return 0; + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_CRCERRORS, 0); } uint16_t AQH_RecvStatsMsg_GetIoErrors(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_RECVSTATS_MINSIZE) { - const uint8_t *ptr; + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_IOERRORS, 0); +} - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_IOERRORS; - return (uint16_t)(ptr[0])+(ptr[1]<<8); - } - return 0; + + +uint16_t AQH_RecvStatsMsg_GetNoBufferErrors(const GWEN_MSG *msg) +{ + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_NOBUFFER, 0); +} + + + +uint16_t AQH_RecvStatsMsg_GetHandled(const GWEN_MSG *msg) +{ + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_HANDLED, 0); +} + + + +uint16_t AQH_RecvStatsMsg_GetMissed(const GWEN_MSG *msg) +{ + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_MISSED, 0); } @@ -85,14 +85,17 @@ void AQH_RecvStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_COMRECVSTATS) && (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_RECVSTATS_MINSIZE)) { GWEN_Buffer_AppendArgs(dbuf, - "0x%02x->0x%02x: RECVSTATS %s (uid=0x%08x, in=%d, crc errors=%d, io errors=%d)\n", + "0x%02x->0x%02x: RECVSTATS %s (uid=0x%08x, in=%d, crc errs=%d, io errs=%d, nobuf errs=%d, handled=%d, missed=%d)\n", AQH_NodeMsg_GetSourceAddress(msg), AQH_NodeMsg_GetDestAddress(msg), sText, (unsigned int) AQH_RecvStatsMsg_GetUid(msg), AQH_RecvStatsMsg_GetPacketsIn(msg), AQH_RecvStatsMsg_GetCrcErrors(msg), - AQH_RecvStatsMsg_GetIoErrors(msg)); + AQH_RecvStatsMsg_GetIoErrors(msg), + AQH_RecvStatsMsg_GetNoBufferErrors(msg), + AQH_RecvStatsMsg_GetHandled(msg), + AQH_RecvStatsMsg_GetMissed(msg)); } } diff --git a/aqhome/msg/msg_recvstats.h b/aqhome/msg/msg_recvstats.h index ba0521f..503797f 100644 --- a/aqhome/msg/msg_recvstats.h +++ b/aqhome/msg/msg_recvstats.h @@ -22,6 +22,9 @@ AQHOME_API uint32_t AQH_RecvStatsMsg_GetUid(const GWEN_MSG *msg); AQHOME_API uint16_t AQH_RecvStatsMsg_GetPacketsIn(const GWEN_MSG *msg); AQHOME_API uint16_t AQH_RecvStatsMsg_GetCrcErrors(const GWEN_MSG *msg); AQHOME_API uint16_t AQH_RecvStatsMsg_GetIoErrors(const GWEN_MSG *msg); +AQHOME_API uint16_t AQH_RecvStatsMsg_GetNoBufferErrors(const GWEN_MSG *msg); +AQHOME_API uint16_t AQH_RecvStatsMsg_GetHandled(const GWEN_MSG *msg); +AQHOME_API uint16_t AQH_RecvStatsMsg_GetMissed(const GWEN_MSG *msg); AQHOME_API void AQH_RecvStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); diff --git a/aqhome/msg/msg_sendstats.c b/aqhome/msg/msg_sendstats.c index 0140da3..3f86f97 100644 --- a/aqhome/msg/msg_sendstats.c +++ b/aqhome/msg/msg_sendstats.c @@ -22,6 +22,7 @@ #define AQH_MSG_OFFS_SENDSTATS_PACKETSOUT 4 #define AQH_MSG_OFFS_SENDSTATS_COLLISIONS 6 #define AQH_MSG_OFFS_SENDSTATS_ABORTED 8 +#define AQH_MSG_OFFS_SENDSTATS_NOBUFFER 10 #define AQH_MSG_SENDSTATS_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_ABORTED+2) @@ -29,56 +30,35 @@ uint32_t AQH_SendStatsMsg_GetUid(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_COMSENDSTATS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_SENDSTATS_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_UID; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_UID, 0); } uint16_t AQH_SendStatsMsg_GetPacketsOut(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_COMSENDSTATS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_SENDSTATS_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_PACKETSOUT; - return (uint16_t)(ptr[0])+(ptr[1]<<8); - } - return 0; + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_PACKETSOUT, 0); } uint16_t AQH_SendStatsMsg_GetCollisions(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_COMSENDSTATS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_SENDSTATS_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_COLLISIONS; - return (uint16_t)(ptr[0])+(ptr[1]<<8); - } - return 0; + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_COLLISIONS, 0); } uint16_t AQH_SendStatsMsg_GetAborted(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_COMSENDSTATS) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_SENDSTATS_MINSIZE)) { - const uint8_t *ptr; + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_ABORTED, 0); +} - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_ABORTED; - return (uint16_t)(ptr[0])+(ptr[1]<<8); - } - return 0; + + +uint16_t AQH_SendStatsMsg_GetNoBufferErrors(const GWEN_MSG *msg) +{ + return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SENDSTATS_NOBUFFER, 0); } @@ -88,14 +68,15 @@ void AQH_SendStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_COMSENDSTATS) && (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_SENDSTATS_MINSIZE)) { GWEN_Buffer_AppendArgs(dbuf, - "0x%02x->0x%02x: SENDSTATS %s (uid=0x%08x, out=%d, collisions=%d, aborted=%d)\n", + "0x%02x->0x%02x: SENDSTATS %s (uid=0x%08x, out=%d, collisions=%d, aborted=%d, no buffer=%d)\n", AQH_NodeMsg_GetSourceAddress(msg), AQH_NodeMsg_GetDestAddress(msg), sText, (unsigned int) AQH_SendStatsMsg_GetUid(msg), AQH_SendStatsMsg_GetPacketsOut(msg), AQH_SendStatsMsg_GetCollisions(msg), - AQH_SendStatsMsg_GetAborted(msg)); + AQH_SendStatsMsg_GetAborted(msg), + AQH_SendStatsMsg_GetNoBufferErrors(msg)); } } diff --git a/aqhome/msg/msg_sendstats.h b/aqhome/msg/msg_sendstats.h index 88a87b5..2b4a33b 100644 --- a/aqhome/msg/msg_sendstats.h +++ b/aqhome/msg/msg_sendstats.h @@ -22,6 +22,7 @@ AQHOME_API uint32_t AQH_SendStatsMsg_GetUid(const GWEN_MSG *msg); AQHOME_API uint16_t AQH_SendStatsMsg_GetPacketsOut(const GWEN_MSG *msg); AQHOME_API uint16_t AQH_SendStatsMsg_GetCollisions(const GWEN_MSG *msg); AQHOME_API uint16_t AQH_SendStatsMsg_GetAborted(const GWEN_MSG *msg); +AQHOME_API uint16_t AQH_SendStatsMsg_GetNoBufferErrors(const GWEN_MSG *msg); AQHOME_API void AQH_SendStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText); diff --git a/aqhome/msg/msg_value.c b/aqhome/msg/msg_value.c index 1a678ed..bb2a0fa 100644 --- a/aqhome/msg/msg_value.c +++ b/aqhome/msg/msg_value.c @@ -22,42 +22,21 @@ uint32_t AQH_ValueMsg_GetTimestamp(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE_TIMESTAMP; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE_TIMESTAMP, 0); } uint8_t AQH_ValueMsg_GetValueId(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE_VALUEID; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE_VALUEID, 0); } uint8_t AQH_ValueMsg_GetValueType(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE_VALUETYPE; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE_VALUETYPE, 0); } diff --git a/aqhome/msg/msg_value2.c b/aqhome/msg/msg_value2.c index e149279..d7b01c6 100644 --- a/aqhome/msg/msg_value2.c +++ b/aqhome/msg/msg_value2.c @@ -19,72 +19,47 @@ +#define AQH_MSG_OFFS_VALUE2_UID 0 +#define AQH_MSG_OFFS_VALUE2_VALUEID 4 +#define AQH_MSG_OFFS_VALUE2_VALUETYPE 5 +#define AQH_MSG_OFFS_VALUE2_VALUE 6 +#define AQH_MSG_OFFS_VALUE2_DENOM 8 + +#define AQH_MSG_VALUE2_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_DENOM+2) + + uint32_t AQH_Value2Msg_GetUid(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE2) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE2_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_TIMESTAMP; - return (uint32_t)(ptr[0])+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24); - } - return 0; + return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_UID, 0); } uint8_t AQH_Value2Msg_GetValueId(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE2) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE2_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_VALUEID; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_VALUEID, 0); } uint8_t AQH_Value2Msg_GetValueType(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE2) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE2_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_VALUETYPE; - return ptr[0]; - } - return 0; + return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_VALUETYPE, 0); } int16_t AQH_Value2Msg_GetValueNom(const GWEN_MSG *msg) { - if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_VALUE2) && - (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE2_MINSIZE)) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_VALUE; - return (int16_t)((uint16_t)((ptr[0])+(ptr[1]<<8))); - } - return 0; + return (int16_t) AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_VALUE, 0); } int16_t AQH_Value2Msg_GetValueDenom(const GWEN_MSG *msg) { - if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_VALUE2_MINSIZE) { - const uint8_t *ptr; - - ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_DENOM; - return (int16_t)((uint16_t)((ptr[0])+(ptr[1]<<8))); - } - return 0; + return (int16_t) AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_DENOM, 0); } diff --git a/aqhome/msg/msg_value2.h b/aqhome/msg/msg_value2.h index 2c13d1b..02fd909 100644 --- a/aqhome/msg/msg_value2.h +++ b/aqhome/msg/msg_value2.h @@ -18,14 +18,6 @@ -#define AQH_MSG_OFFS_VALUE2_TIMESTAMP 0 -#define AQH_MSG_OFFS_VALUE2_VALUEID 4 -#define AQH_MSG_OFFS_VALUE2_VALUETYPE 5 -#define AQH_MSG_OFFS_VALUE2_VALUE 6 -#define AQH_MSG_OFFS_VALUE2_DENOM 8 - -#define AQH_MSG_VALUE2_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_VALUE2_DENOM+2) - #define AQH_MSG_VALUE2_TYPE_TEMP 1 #define AQH_MSG_VALUE2_TYPE_HUMIDITY 2 diff --git a/aqhome/msgmanager.c b/aqhome/msgmanager.c index b038b27..5e28b6d 100644 --- a/aqhome/msgmanager.c +++ b/aqhome/msgmanager.c @@ -370,7 +370,7 @@ void _handleMsgDevice(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, const 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_NodeInfo_SetModules(ni, AQH_DeviceMsg_GetModuleMask(msg)); AQH_NodeDb_SetModified(xmgr->nodeDb); } else { diff --git a/avr/MESSAGES b/avr/MESSAGES index 8b318ab..5141b9a 100644 --- a/avr/MESSAGES +++ b/avr/MESSAGES @@ -198,6 +198,7 @@ Offset Length Meaning 8 2 packets out 10 2 collisions 12 2 aborted + 14 2 no buffer errors @@ -215,5 +216,9 @@ Offset Length Meaning 8 2 packets in 10 2 crc errors 12 2 io errors + 14 2 no buffer errors + 16 2 handled packets + 18 2 missed + diff --git a/avr/com.asm b/avr/com.asm index 832159e..900e5e1 100644 --- a/avr/com.asm +++ b/avr/com.asm @@ -63,13 +63,15 @@ comDataBegin: comStatsRecvErrs: .byte 2 comStatsRecvCrcErrs: .byte 2 - comStatsCollisions: .byte 2 comStatsMissed: .byte 2 - comStatsAborted: .byte 2 comStatsIgnored: .byte 2 comStatsHandled: .byte 2 + + comStatsCollisions: .byte 2 + comStatsAborted: .byte 2 - comStatsNoBuffer: .byte 2 + comStatsSendNoBuffer: .byte 2 + comStatsRecvNoBuffer: .byte 2 comRecvBuffersReadPos: .byte 1 comRecvBuffersWritePos: .byte 1 @@ -223,6 +225,8 @@ COM_AllocBufferAndGetXY: sec ret COM_AllocBufferAndGetXY_error: + ldi xl, LOW(comStatsSendNoBuffer) + ldi xh, HIGH(comStatsSendNoBuffer) clc ret diff --git a/avr/com_crc.asm b/avr/com_crc.asm index 943bc5c..7ca2891 100644 --- a/avr/com_crc.asm +++ b/avr/com_crc.asm @@ -1,4 +1,10 @@ + +#define COM_USE_CRC8 1 +#define COM_CRC_POLYNOMIAL 0x97 + + + ; *************************************************************************** ; code @@ -14,15 +20,20 @@ ; - X : pointer to packet buffer ; OUT: ; - CFLAG: set if okay, clear otherwise -; MODIFIED REGS: R16, R17, R18, X +; MODIFIED REGS: R16, R17, R18, R19, X comCalcAndAddChecksumByte: adiw xh:xl, COM_MSG_OFFS_MSGLEN ld r18, X ; read msg len inc r18 ; account for dest address - inc r18 ; account for msg len bytes + inc r18 ; account for msg len byte sbiw xh:xl, COM_MSG_OFFS_MSGLEN +#ifdef COM_USE_CRC8 + ldi r19, COM_CRC_POLYNOMIAL + rcall cproCalcCrc8 ; (R16, R17, R18, R20, X) +#else rcall cproCalcXor ; (R16, R17, R18, X) +#endif st X, r16 ; add checksum byte sec ret @@ -36,15 +47,20 @@ comCalcAndAddChecksumByte: ; - X : pointer to packet buffer ; OUT: ; - CFLAG: set if okay, clear otherwise -; MODIFIED REGS: R16, R17, R18, X +; MODIFIED REGS: R16, R17, R18, R19, R20, X cproCheckMessageInBuffer: adiw xh:xl, COM_MSG_OFFS_MSGLEN ld r18, X ; read msg len inc r18 ; account for dest address - inc r18 ; account for msg len bytes + inc r18 ; account for msg len byte sbiw xh:xl, COM_MSG_OFFS_MSGLEN +#ifdef COM_USE_CRC8 + ldi r19, COM_CRC_POLYNOMIAL + rcall cproCalcCrc8 ; (R16, R17, R18, R20, X) +#else rcall cproCalcXor ; (R16, R17, R18, X) +#endif ld r17, X cp r16,r17 ; should be equal brne cproCheckMessageInBuffer_error diff --git a/avr/com_recv.asm b/avr/com_recv.asm index 0cb2757..1dd2e93 100644 --- a/avr/com_recv.asm +++ b/avr/com_recv.asm @@ -21,8 +21,8 @@ comReceivePacketHandleBuffer: rcall COM_BufferAlloc brcs comReceivePacketHandleBuffer_haveBuffer - ldi xl, LOW(comStatsNoBuffer) - ldi xh, HIGH(comStatsNoBuffer) + ldi xl, LOW(comStatsRecvNoBuffer) + ldi xh, HIGH(comStatsRecvNoBuffer) rjmp comReceivePacketHandleBuffer_errorWithCounter comReceivePacketHandleBuffer_haveBuffer: @@ -72,7 +72,7 @@ comReceivePacketHandleBuffer_notforme: ; OUT: ; - CFLAG: set if okay, clear otherwise ; - R16: error code if CFLAG cleared -; MODIFIED REGISTERS: r16, r17, r18, X (r20, r21, r22) +; MODIFIED REGISTERS: r16, r17, r18, X (r19, r20, r21, r22) comReceivePacketToXandCheck: push xh @@ -81,7 +81,7 @@ comReceivePacketToXandCheck: pop xl pop xh brcc comReceivePacketToXandCheck_error - rcall cproCheckMessageInBuffer ; (R16, R17, R18, X) + rcall cproCheckMessageInBuffer ; (R16, R17, R18, R19, R20, X) ldi r16, COM_ERR_CHECKSUM brcc comReceivePacketToXandCheck_error sec diff --git a/avr/comproto.asm b/avr/comproto.asm index 6b48710..49f9d85 100644 --- a/avr/comproto.asm +++ b/avr/comproto.asm @@ -31,12 +31,12 @@ .equ CPRO_PAYLOAD_FLAGS_SECONDS = 0x01 .equ CPRO_PAYLOAD_FLAGS_UID = 0x02 .equ CPRO_PAYLOAD_FLAGS_RESERVED1 = 0x04 -.equ CPRO_PAYLOAD_FLAGS_RESERVED2 = 0x08 -.equ CPRO_PAYLOAD_FLAGS_RESERVED3 = 0x10 -.equ CPRO_PAYLOAD_FLAGS_NUM0 = 0x20 -.equ CPRO_PAYLOAD_FLAGS_NUM1 = 0x40 -.equ CPRO_PAYLOAD_FLAGS_NUM3 = 0x80 -.equ CPRO_PAYLOAD_FLAGS_SHIFT_NUM = 5 +.equ CPRO_PAYLOAD_FLAGS_NUM0 = 0x08 +.equ CPRO_PAYLOAD_FLAGS_NUM1 = 0x10 +.equ CPRO_PAYLOAD_FLAGS_NUM2 = 0x20 +.equ CPRO_PAYLOAD_FLAGS_NUM3 = 0x40 +.equ CPRO_PAYLOAD_FLAGS_NUM4 = 0x80 +.equ CPRO_PAYLOAD_FLAGS_SHIFT_NUM = 3 .equ CPRO_PACKET_HAVEADDR_OFFS_ADDRESS = COM_MSG_OFFS_PAYLOAD+4 @@ -244,7 +244,7 @@ cproEnqueueMsgWithCmdAndSrcAddr: pop r20 pop xl pop xh - rcall comCalcAndAddChecksumByte + rcall comCalcAndAddChecksumByte ; (R16, R17, R18, R19, X) ; mark buffer as enqueued with PRIO given in R20 rcall COM_EnqueuePacket ; (R15, R16) @@ -331,9 +331,7 @@ cproCalcPayloadSize_l1: brcc cproCalcPayloadSize_l2 add r4, r17 ; add 4 bytes cproCalcPayloadSize_l2: - lsr r16 - lsr r16 - lsr r16 ; after this R16 contains CPRO_PAYLOAD_FLAGS_NUM0-3 + lsr r16 ; shift out reserved1, after this R16 contains CPRO_PAYLOAD_FLAGS_NUM0-4 add r16, r4 ; add previous bytes to R16 ret diff --git a/avr/comproto_addr.asm b/avr/comproto_addr.asm index 651fb40..81daa5b 100644 --- a/avr/comproto_addr.asm +++ b/avr/comproto_addr.asm @@ -514,7 +514,7 @@ cproEnqueueAddressPacket: st X+, r6 ; 5: value id pop xl pop xh - rcall comCalcAndAddChecksumByte + rcall comCalcAndAddChecksumByte ; (R16, R17, R18, R19, X) ; mark buffer as enqueued with PRIO "important" (higher retry count) ldi r20, COM_BUFFER_PRIO_IMPORTANT diff --git a/avr/comproto_device.asm b/avr/comproto_device.asm index f08c1ca..645e67d 100644 --- a/avr/comproto_device.asm +++ b/avr/comproto_device.asm @@ -40,7 +40,7 @@ CPRO_EnqueueDevice: st X+, r17 ; 11: modules mask low pop xl pop xh - rcall comCalcAndAddChecksumByte + rcall comCalcAndAddChecksumByte ; (R16, R17, R18, R19, X) ; mark buffer as enqueued with PRIO "normal" (slightly limited number of retries) ldi r20, COM_BUFFER_PRIO_NORMAL diff --git a/avr/comproto_stats.asm b/avr/comproto_stats.asm index f039a69..0478a32 100644 --- a/avr/comproto_stats.asm +++ b/avr/comproto_stats.asm @@ -23,26 +23,30 @@ CPRO_EnqueueComSendStats: pop r16 brcc CPRO_EnqueueComSendStats_error - ldi r17, CPRO_PAYLOAD_FLAGS_UID | (6<