From 01c8ab7f9069babd104557498d091915860ec120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 15 Oct 2017 18:28:27 +0200 Subject: [PATCH] oops checksums didnt work --- TinyFrame.c | 40 +++++++++++++++++++++------------------- TinyFrame.h | 13 +++++++++---- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/TinyFrame.c b/TinyFrame.c index ad76c00..cf8cb4d 100644 --- a/TinyFrame.c +++ b/TinyFrame.c @@ -23,8 +23,8 @@ enum TFState { typedef struct _IdListener_struct_ { TF_ID id; TF_LISTENER fn; - TF_COUNT timeout; // nr of ticks remaining to disable this listener - TF_COUNT timeout_max; // the original timeout is stored here + TF_TICKS timeout; // nr of ticks remaining to disable this listener + TF_TICKS timeout_max; // the original timeout is stored here void *userdata; } IdListener; @@ -77,21 +77,21 @@ static struct TinyFrameStruct { //region Checksums -#if TF_CKSUM_TYPE == 0 +#if TF_CKSUM_TYPE == TF_CKSUM_NONE // NONE #define CKSUM_RESET(cksum) #define CKSUM_ADD(cksum, byte) #define CKSUM_FINALIZE(cksum) -#elif TF_CKSUM_TYPE == 8 +#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) -#elif TF_CKSUM_TYPE == 16 +#elif TF_CKSUM_TYPE == TF_CKSUM_CRC16 /** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */ static const uint16_t crc16_table[256] = { @@ -138,7 +138,7 @@ static struct TinyFrameStruct { #define CKSUM_ADD(cksum, byte) do { cksum = crc16_byte(cksum, byte); } while(0) #define CKSUM_FINALIZE(cksum) -#elif TF_CKSUM_TYPE == 32 +#elif TF_CKSUM_TYPE == TF_CKSUM_CRC32 static const uint32_t crc32_table[] = { /* CRC polynomial 0xedb88320 */ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, @@ -191,9 +191,9 @@ static struct TinyFrameStruct { 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) + #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) #endif @@ -486,7 +486,7 @@ void _TF_FN TF_AcceptChar(unsigned char c) case TFState_TYPE: CKSUM_ADD(tf.cksum, c); COLLECT_NUMBER(tf.type, TF_TYPE) { - #if TF_CKSUM_TYPE == 0 + #if TF_CKSUM_TYPE == TF_CKSUM_NONE tf.state = TFState_DATA; tf.rxi = 0; #else @@ -536,7 +536,7 @@ void _TF_FN TF_AcceptChar(unsigned char c) } if (tf.rxi == tf.len) { - #if TF_CKSUM_TYPE == 0 + #if TF_CKSUM_TYPE == TF_CKSUM_NONE // All done TF_HandleReceivedMessage(); TF_ResetParser(); @@ -581,13 +581,15 @@ static inline size_t _TF_FN TF_Compose(uint8_t *outbuff, TF_ID *id_ptr, const uint8_t *data, TF_LEN data_len, TF_ID explicit_id, bool use_expl_id) { - char si; // signed small int - TF_LEN i; - uint8_t b; - TF_ID id; - TF_CKSUM cksum; + char si = 0; // signed small int + TF_LEN i = 0; + uint8_t b = 0; + TF_ID id = 0; + TF_CKSUM cksum = 0; size_t pos = 0; // can be needed to grow larger than TF_LEN + (void)cksum; + CKSUM_RESET(cksum); // sanitize len @@ -633,7 +635,7 @@ static inline size_t _TF_FN TF_Compose(uint8_t *outbuff, TF_ID *id_ptr, WRITENUM_CKSUM(TF_LEN, data_len); WRITENUM_CKSUM(TF_TYPE, type); - #if TF_CKSUM_TYPE != 0 + #if TF_CKSUM_TYPE != TF_CKSUM_NONE CKSUM_FINALIZE(cksum); WRITENUM(TF_CKSUM, cksum); #endif @@ -649,7 +651,7 @@ static inline size_t _TF_FN TF_Compose(uint8_t *outbuff, TF_ID *id_ptr, CKSUM_ADD(cksum, b); } - #if TF_CKSUM_TYPE != 0 + #if TF_CKSUM_TYPE != TF_CKSUM_NONE CKSUM_FINALIZE(cksum); WRITENUM(TF_CKSUM, cksum); #endif @@ -705,7 +707,7 @@ bool _TF_FN TF_RenewIdListener(TF_ID id) /** Timebase hook - for timeouts */ void _TF_FN TF_Tick(void) { - TF_COUNT i; + TF_COUNT i = 0; IdListener *lst; // increment parser timeout (timeout is handled when receiving next byte) diff --git a/TinyFrame.h b/TinyFrame.h index 4db7258..90678ae 100644 --- a/TinyFrame.h +++ b/TinyFrame.h @@ -49,7 +49,12 @@ #define TF_TYPE_BYTES 1 // Select checksum type (0 = none, 8 = ~XOR, 16 = CRC16 0x8005, 32 = CRC32) -#define TF_CKSUM_TYPE 16 +#define TF_CKSUM_NONE 0 +#define TF_CKSUM_XOR 8 +#define TF_CKSUM_CRC16 16 +#define TF_CKSUM_CRC32 32 + +#define TF_CKSUM_TYPE TF_CKSUM_CRC16 // Use a SOF byte to mark the start of a frame #define TF_USE_SOF_BYTE 1 @@ -100,13 +105,13 @@ typedef uint8_t TF_COUNT; #endif -#if TF_CKSUM_TYPE == 8 || TF_CKSUM_TYPE == 0 +#if TF_CKSUM_TYPE == TF_CKSUM_XOR || TF_CKSUM_TYPE == TF_CKSUM_NONE // ~XOR (if 0, still use 1 byte - it won't be used) typedef uint8_t TF_CKSUM; -#elif TF_CKSUM_TYPE == 16 +#elif TF_CKSUM_TYPE == TF_CKSUM_CRC16 // CRC16 typedef uint16_t TF_CKSUM; -#elif TF_CKSUM_TYPE == 32 +#elif TF_CKSUM_TYPE == TF_CKSUM_CRC32 // CRC32 typedef uint32_t TF_CKSUM; #else