Added support for custom checksums

pull/14/head
Ondřej Hruška 7 years ago
parent b74bda7cf5
commit 91e1c25c6d
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 14
      TF_Config.example.h
  2. 69
      TinyFrame.c
  3. 57
      TinyFrame.h
  4. 1
      demo/multipart_tx/TF_Config.h
  5. 1
      demo/simple/TF_Config.h
  6. 1
      demo/socket_demo/TF_Config.h

@ -8,6 +8,7 @@
#define TF_CONFIG_H
#include <stdint.h>
#include <stdio.h> // used by the TF_Error() macro defined below
//#include <esp8266.h> // when using with esphttpd
//----------------------------- FRAME FORMAT ---------------------------------
@ -28,11 +29,11 @@
#define TF_LEN_BYTES 2
#define TF_TYPE_BYTES 1
// Checksum type
//#define TF_CKSUM_TYPE TF_CKSUM_NONE
//#define TF_CKSUM_TYPE TF_CKSUM_XOR
#define TF_CKSUM_TYPE TF_CKSUM_CRC16
//#define TF_CKSUM_TYPE TF_CKSUM_CRC32
// Checksum type. Options:
// TF_CKSUM_NONE, TF_CKSUM_XOR, TF_CKSUM_CRC16, TF_CKSUM_CRC32
// TF_CKSUM_CUSTOM8, TF_CKSUM_CUSTOM16, TF_CKSUM_CUSTOM32
// Custom checksums require you to implement checksum functions (see TinyFrame.h)
#define TF_CKSUM_TYPE TF_CKSUM_CUSTOM8
// Use a SOF byte to mark the start of a frame
#define TF_USE_SOF_BYTE 1
@ -69,6 +70,9 @@ typedef uint8_t TF_COUNT;
// ticks = number of calls to TF_Tick()
#define TF_PARSER_TIMEOUT_TICKS 10
// Whether to use mutex - requires you to implement TF_ClaimTx() and TF_ReleaseTx()
#define TF_USE_MUTEX 1
// Error reporting function. To disable debug, change to empty define
#define TF_Error(format, ...) printf("[TF] " format "\n", ##__VA_ARGS__)

@ -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,21 +18,30 @@
// 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
@ -73,14 +82,9 @@ static const uint16_t crc16_table[256] = {
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
@ -131,14 +135,9 @@ static const uint32_t crc32_table[] = { /* CRC polynomial 0xedb88320 */
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
}

@ -4,13 +4,13 @@
/**
* TinyFrame protocol library
*
* (c) Ondřej Hruška 2017, MIT License
* (c) Ondřej Hruška 2017-2018, MIT License
* no liability/warranty, free for any use, must retain this notice & license
*
* Upstream URL: https://github.com/MightyPork/TinyFrame
*/
#define TF_VERSION "2.1.0"
#define TF_VERSION "2.2.0"
//---------------------------------------------------------------------------
#include <stdint.h> // for uint8_t etc
@ -19,11 +19,14 @@
#include <string.h> // for memset()
//---------------------------------------------------------------------------
// Select checksum type (0 = none, 8 = ~XOR, 16 = CRC16 0x8005, 32 = CRC32)
// Checksum type (0 = none, 8 = ~XOR, 16 = CRC16 0x8005, 32 = CRC32)
#define TF_CKSUM_NONE 0
#define TF_CKSUM_XOR 8
#define TF_CKSUM_CRC16 16
#define TF_CKSUM_CRC32 32
#define TF_CKSUM_CUSTOM8 1
#define TF_CKSUM_CUSTOM16 2
#define TF_CKSUM_CUSTOM32 3
#include "TF_Config.h"
@ -62,17 +65,17 @@ typedef uint32_t TF_ID;
#endif
#if TF_CKSUM_TYPE == TF_CKSUM_XOR || TF_CKSUM_TYPE == TF_CKSUM_NONE
#if (TF_CKSUM_TYPE == TF_CKSUM_XOR) || (TF_CKSUM_TYPE == TF_CKSUM_NONE) || (TF_CKSUM_TYPE == TF_CKSUM_CUSTOM8)
// ~XOR (if 0, still use 1 byte - it won't be used)
typedef uint8_t TF_CKSUM;
#elif TF_CKSUM_TYPE == TF_CKSUM_CRC16
#elif (TF_CKSUM_TYPE == TF_CKSUM_CRC16) || (TF_CKSUM_TYPE == TF_CKSUM_CUSTOM16)
// CRC16
typedef uint16_t TF_CKSUM;
#elif TF_CKSUM_TYPE == TF_CKSUM_CRC32
#elif (TF_CKSUM_TYPE == TF_CKSUM_CRC32) || (TF_CKSUM_TYPE == TF_CKSUM_CUSTOM32)
// CRC32
typedef uint32_t TF_CKSUM;
#else
#error Bad value for TF_CKSUM_TYPE, must be 8, 16 or 32
#error Bad value for TF_CKSUM_TYPE
#endif
//endregion
@ -100,7 +103,7 @@ typedef enum {
} TF_Result;
/** Data structure for sending / receiving messages */
typedef struct _TF_MSG_STRUCT_ {
typedef struct TF_Msg_ {
TF_ID frame_id; //!< message ID
bool is_response; //!< internal flag, set when using the Respond function. frame_id is then kept unchanged.
TF_TYPE type; //!< received or sent message type
@ -135,7 +138,7 @@ typedef TF_Result (*TF_Listener)(TinyFrame *tf, TF_Msg *msg);
// region Internal
enum TFState_ {
enum TF_State_ {
TFState_SOF = 0, //!< Wait for SOF
TFState_LEN, //!< Wait for Number Of Bytes
TFState_HEAD_CKSUM, //!< Wait for header Checksum
@ -178,7 +181,7 @@ struct TinyFrame_ {
TF_ID next_id; //!< Next frame / frame chain ID
/* Parser state */
enum TFState_ state;
enum TF_State_ state;
TF_TICKS parser_timeout_ticks;
TF_ID id; //!< Incoming packet ID
TF_LEN len; //!< Payload length
@ -375,6 +378,9 @@ void TF_Tick(TinyFrame *tf);
*/
extern void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, size_t len);
// Mutex functions
#if TF_USE_MUTEX
/** Claim the TX interface before composing and sending a frame */
extern void TF_ClaimTx(TinyFrame *tf);
@ -382,3 +388,34 @@ extern void TF_ClaimTx(TinyFrame *tf);
extern void TF_ReleaseTx(TinyFrame *tf);
#endif
// Custom checksum functions
#if (TF_CKSUM_TYPE == TF_CKSUM_CUSTOM8) || (TF_CKSUM_TYPE == TF_CKSUM_CUSTOM16) || (TF_CKSUM_TYPE == TF_CKSUM_CUSTOM32)
/**
* Initialize a checksum
*
* @return initial checksum value
*/
extern TF_CKSUM TF_CksumStart(void);
/**
* Update a checksum with a byte
*
* @param cksum - previous checksum value
* @param byte - byte to add
* @return updated checksum value
*/
extern TF_CKSUM TF_CksumAdd(TF_CKSUM cksum, uint8_t byte);
/**
* Finalize the checksum calculation
*
* @param cksum - previous checksum value
* @return final checksum value
*/
extern TF_CKSUM TF_CksumEnd(TF_CKSUM cksum);
#endif
#endif

@ -6,6 +6,7 @@
#define TF_CONFIG_H
#include <stdint.h>
#include <stdio.h>
#define TF_ID_BYTES 1
#define TF_LEN_BYTES 2

@ -6,6 +6,7 @@
#define TF_CONFIG_H
#include <stdint.h>
#include <stdio.h>
#define TF_ID_BYTES 1
#define TF_LEN_BYTES 2

@ -6,6 +6,7 @@
#define TF_CONFIG_H
#include <stdint.h>
#include <stdio.h>
#define TF_ID_BYTES 1
#define TF_LEN_BYTES 2

Loading…
Cancel
Save