|
|
|
@ -1,6 +1,6 @@ |
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
#include "TinyFrame.h" |
|
|
|
|
#include <malloc.h> |
|
|
|
|
#include <stdlib.h> // - for malloc() if dynamic constructor is used |
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// Compatibility with ESP8266 SDK
|
|
|
|
@ -18,27 +18,36 @@ |
|
|
|
|
// mandate configurable field sizes unless we use u32 everywhere (and possibly shorten
|
|
|
|
|
// it when encoding to the buffer). I don't really like this idea so much. -MP
|
|
|
|
|
|
|
|
|
|
#if TF_USE_MUTEX==0 |
|
|
|
|
/** Claim the TX interface before composing and sending a frame */ |
|
|
|
|
static inline void TF_ClaimTx(TinyFrame *tf) { (void)tf; } |
|
|
|
|
|
|
|
|
|
/** Free the TX interface after composing and sending a frame */ |
|
|
|
|
static inline void TF_ReleaseTx(TinyFrame *tf) { (void)tf; } |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
//region Checksums
|
|
|
|
|
#define CKSUM_RESET(cksum) do { (cksum) = TF_CksumStart(); } while (0) |
|
|
|
|
#define CKSUM_ADD(cksum, byte) do { (cksum) = TF_CksumAdd((cksum), (byte)); } while (0) |
|
|
|
|
#define CKSUM_FINALIZE(cksum) do { (cksum) = TF_CksumEnd((cksum)); } while (0) |
|
|
|
|
|
|
|
|
|
#if TF_CKSUM_TYPE == TF_CKSUM_NONE |
|
|
|
|
|
|
|
|
|
// NONE
|
|
|
|
|
#define CKSUM_RESET(cksum) |
|
|
|
|
#define CKSUM_ADD(cksum, byte) |
|
|
|
|
#define CKSUM_FINALIZE(cksum) |
|
|
|
|
TF_CKSUM TF_CksumStart(void) { return 0; } |
|
|
|
|
TF_CKSUM TF_CksumAdd(TF_CKSUM cksum, uint8_t byte) { return cksum; } |
|
|
|
|
TF_CKSUM TF_CksumEnd(TF_CKSUM cksum) { return cksum; } |
|
|
|
|
|
|
|
|
|
#elif TF_CKSUM_TYPE == TF_CKSUM_XOR |
|
|
|
|
|
|
|
|
|
// ~XOR
|
|
|
|
|
#define CKSUM_RESET(cksum) do { (cksum) = 0; } while (0) |
|
|
|
|
#define CKSUM_ADD(cksum, byte) do { (cksum) ^= (byte); } while(0) |
|
|
|
|
#define CKSUM_FINALIZE(cksum) do { (cksum) = (TF_CKSUM)~cksum; } while(0) |
|
|
|
|
TF_CKSUM TF_CksumStart(void) { return 0; } |
|
|
|
|
TF_CKSUM TF_CksumAdd(TF_CKSUM cksum, uint8_t byte) { return cksum ^ byte; } |
|
|
|
|
TF_CKSUM TF_CksumEnd(TF_CKSUM cksum) { return (TF_CKSUM) ~cksum; } |
|
|
|
|
|
|
|
|
|
#elif TF_CKSUM_TYPE == TF_CKSUM_CRC16 |
|
|
|
|
|
|
|
|
|
// TODO try to replace with an algorithm
|
|
|
|
|
/** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */ |
|
|
|
|
static const uint16_t crc16_table[256] = { |
|
|
|
|
// TODO try to replace with an algorithm
|
|
|
|
|
/** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */ |
|
|
|
|
static const uint16_t crc16_table[256] = { |
|
|
|
|
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, |
|
|
|
|
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, |
|
|
|
|
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, |
|
|
|
@ -71,21 +80,16 @@ static const uint16_t crc16_table[256] = { |
|
|
|
|
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, |
|
|
|
|
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, |
|
|
|
|
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static inline uint16_t crc16_byte(uint16_t cksum, const uint8_t byte) |
|
|
|
|
{ |
|
|
|
|
return (cksum >> 8) ^ crc16_table[(cksum ^ byte) & 0xff]; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
#define CKSUM_RESET(cksum) do { (cksum) = 0; } while (0) |
|
|
|
|
#define CKSUM_ADD(cksum, byte) do { (cksum) = crc16_byte((cksum), (byte)); } while(0) |
|
|
|
|
#define CKSUM_FINALIZE(cksum) |
|
|
|
|
TF_CKSUM TF_CksumStart(void) { return 0; } |
|
|
|
|
TF_CKSUM TF_CksumAdd(TF_CKSUM cksum, uint8_t byte) { return (cksum >> 8) ^ crc16_table[(cksum ^ byte) & 0xff]; } |
|
|
|
|
TF_CKSUM TF_CksumEnd(TF_CKSUM cksum) { return cksum; } |
|
|
|
|
|
|
|
|
|
#elif TF_CKSUM_TYPE == TF_CKSUM_CRC32 |
|
|
|
|
|
|
|
|
|
// TODO try to replace with an algorithm
|
|
|
|
|
static const uint32_t crc32_table[] = { /* CRC polynomial 0xedb88320 */ |
|
|
|
|
// TODO try to replace with an algorithm
|
|
|
|
|
static const uint32_t crc32_table[] = { /* CRC polynomial 0xedb88320 */ |
|
|
|
|
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, |
|
|
|
|
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, |
|
|
|
|
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, |
|
|
|
@ -129,16 +133,11 @@ static const uint32_t crc32_table[] = { /* CRC polynomial 0xedb88320 */ |
|
|
|
|
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, |
|
|
|
|
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, |
|
|
|
|
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static inline uint32_t crc32_byte(uint32_t cksum, const uint8_t byte) |
|
|
|
|
{ |
|
|
|
|
return (crc32_table[((cksum) ^ ((uint8_t)byte)) & 0xff] ^ ((cksum) >> 8)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#define CKSUM_RESET(cksum) do { (cksum) = (TF_CKSUM)0xFFFFFFFF; } while (0) |
|
|
|
|
#define CKSUM_ADD(cksum, byte) do { (cksum) = crc32_byte(cksum, byte); } while(0) |
|
|
|
|
#define CKSUM_FINALIZE(cksum) do { (cksum) = (TF_CKSUM)~(cksum); } while(0) |
|
|
|
|
TF_CKSUM TF_CksumStart(void) { return (TF_CKSUM)0xFFFFFFFF; } |
|
|
|
|
TF_CKSUM TF_CksumAdd(TF_CKSUM cksum, uint8_t byte) { return crc32_table[((cksum) ^ ((uint8_t)byte)) & 0xff] ^ ((cksum) >> 8); } |
|
|
|
|
TF_CKSUM TF_CksumEnd(TF_CKSUM cksum) { return (TF_CKSUM) ~cksum; } |
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
@ -586,8 +585,8 @@ void _TF_FN TF_AcceptChar(TinyFrame *tf, unsigned char c) |
|
|
|
|
if (tf->rxi == tf->len) { |
|
|
|
|
#if TF_CKSUM_TYPE == TF_CKSUM_NONE |
|
|
|
|
// All done
|
|
|
|
|
TF_HandleReceivedMessage(); |
|
|
|
|
TF_ResetParser(); |
|
|
|
|
TF_HandleReceivedMessage(tf); |
|
|
|
|
TF_ResetParser(tf); |
|
|
|
|
#else |
|
|
|
|
// Enter DATA_CKSUM state
|
|
|
|
|
tf->state = TFState_DATA_CKSUM; |
|
|
|
@ -898,19 +897,3 @@ void _TF_FN TF_Tick(TinyFrame *tf) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** Default impl for claiming write mutex; can be specific to the instance */ |
|
|
|
|
void __attribute__((weak)) TF_ClaimTx(TinyFrame *tf) |
|
|
|
|
{ |
|
|
|
|
(void) tf; |
|
|
|
|
|
|
|
|
|
// do nothing
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** Default impl for releasing write mutex; can be specific to the instance */ |
|
|
|
|
void __attribute__((weak)) TF_ReleaseTx(TinyFrame *tf) |
|
|
|
|
{ |
|
|
|
|
(void) tf; |
|
|
|
|
|
|
|
|
|
// do nothing
|
|
|
|
|
} |
|
|
|
|