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)
This commit is contained in:
@@ -16,10 +16,15 @@
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
#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<len; i++, ptr++) {
|
||||
x^=*ptr;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef COM_USE_CRC8
|
||||
uint8_t _calcCrc8Checksum(const uint8_t *ptr, uint8_t len)
|
||||
{
|
||||
int i;
|
||||
@@ -204,6 +244,21 @@ uint8_t _calcCrc8Checksum(const uint8_t *ptr, uint8_t len)
|
||||
|
||||
return x;
|
||||
}
|
||||
#else
|
||||
uint8_t _calcXorChecksum(const uint8_t *ptr, uint8_t len)
|
||||
{
|
||||
int i;
|
||||
uint8_t x=0;
|
||||
|
||||
for (i=0; i<len; i++, ptr++) {
|
||||
x^=*ptr;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user