From 5ba23e942fdee397ce78ecd10ca918673ba75c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 26 Jan 2018 11:44:10 +0100 Subject: [PATCH] added example of using custom checksums --- demo/simple_custom_cksum/Makefile | 11 ++++ demo/simple_custom_cksum/TF_Config.h | 28 +++++++++ demo/simple_custom_cksum/test.c | 88 ++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 demo/simple_custom_cksum/Makefile create mode 100644 demo/simple_custom_cksum/TF_Config.h create mode 100644 demo/simple_custom_cksum/test.c diff --git a/demo/simple_custom_cksum/Makefile b/demo/simple_custom_cksum/Makefile new file mode 100644 index 0000000..b485f62 --- /dev/null +++ b/demo/simple_custom_cksum/Makefile @@ -0,0 +1,11 @@ +CFILES=../utils.c ../../TinyFrame.c +INCLDIRS=-I. -I.. -I../.. +CFLAGS=-O0 -ggdb --std=gnu99 -Wno-main -Wno-unused -Wall -Wextra $(CFILES) $(INCLDIRS) + +run: test.bin + ./test.bin + +build: test.bin + +test.bin: test.c $(CFILES) + gcc test.c $(CFLAGS) -o test.bin diff --git a/demo/simple_custom_cksum/TF_Config.h b/demo/simple_custom_cksum/TF_Config.h new file mode 100644 index 0000000..46a5cc2 --- /dev/null +++ b/demo/simple_custom_cksum/TF_Config.h @@ -0,0 +1,28 @@ +// +// Created by MightyPork on 2018/01/26. +// + +#ifndef TF_CONFIG_H +#define TF_CONFIG_H + +#include +#include + +#define TF_ID_BYTES 1 +#define TF_LEN_BYTES 2 +#define TF_TYPE_BYTES 1 +#define TF_CKSUM_TYPE TF_CKSUM_CUSTOM8 +#define TF_USE_SOF_BYTE 1 +#define TF_SOF_BYTE 0x01 +typedef uint16_t TF_TICKS; +typedef uint8_t TF_COUNT; +#define TF_MAX_PAYLOAD_RX 1024 +#define TF_SENDBUF_LEN 1024 +#define TF_MAX_ID_LST 10 +#define TF_MAX_TYPE_LST 10 +#define TF_MAX_GEN_LST 5 +#define TF_PARSER_TIMEOUT_TICKS 10 + +#define TF_Error(format, ...) printf("[TF] " format "\n", ##__VA_ARGS__) + +#endif //TF_CONFIG_H diff --git a/demo/simple_custom_cksum/test.c b/demo/simple_custom_cksum/test.c new file mode 100644 index 0000000..caad865 --- /dev/null +++ b/demo/simple_custom_cksum/test.c @@ -0,0 +1,88 @@ +#include +#include +#include "../../TinyFrame.h" +#include "../utils.h" + +TinyFrame *demo_tf; + +bool do_corrupt = false; + +/** + * This function should be defined in the application code. + * It implements the lowest layer - sending bytes to UART (or other) + */ +void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len) +{ + printf("--------------------\n"); + printf("\033[32mTF_WriteImpl - sending frame:\033[0m\n"); + + uint8_t *xbuff = (uint8_t *)buff; + if (do_corrupt) { + printf("(corrupting to test checksum checking...)\n"); + xbuff[8]++; + } + + dumpFrame(xbuff, len); + + // Send it back as if we received it + TF_Accept(tf, xbuff, len); +} + +/** An example listener function */ +TF_Result myListener(TinyFrame *tf, TF_Msg *msg) +{ + dumpFrameInfo(msg); + return TF_STAY; +} + +TF_Result testIdListener(TinyFrame *tf, TF_Msg *msg) +{ + printf("OK - ID Listener triggered for msg!\n"); + dumpFrameInfo(msg); + return TF_CLOSE; +} + +void main(void) +{ + TF_Msg msg; + const char *longstr = "Lorem ipsum dolor sit amet."; + + // Set up the TinyFrame library + demo_tf = TF_Init(TF_MASTER); // 1 = master, 0 = slave + TF_AddGenericListener(demo_tf, myListener); + + printf("------ Simulate sending a message --------\n"); + + TF_ClearMsg(&msg); + msg.type = 0x22; + msg.data = (pu8) "Hello TinyFrame"; + msg.len = 16; + TF_Send(demo_tf, &msg); + + printf("This should fail:\n"); + + // test checksums are tested + do_corrupt = true; + msg.type = 0x44; + msg.data = (pu8) "Hello2"; + msg.len = 7; + TF_Send(demo_tf, &msg); +} + + +// a made up custom checksum - just to test it's used and works + +TF_CKSUM TF_CksumStart(void) +{ + return 0; +} + +TF_CKSUM TF_CksumAdd(TF_CKSUM cksum, uint8_t byte) +{ + return cksum ^ byte + 1; +} + +TF_CKSUM TF_CksumEnd(TF_CKSUM cksum) +{ + return ~cksum ^ 0xA5; +}