From 46883a9baa361720d5270679b23d30bddce9f0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 15 Oct 2017 22:40:53 +0200 Subject: [PATCH] kinda working demo --- .gitignore | 1 + CMakeLists.txt | 16 ++- Makefile | 10 -- TF_Config.example.h | 70 +++++++++++++ TinyFrame.c | 57 +++++++++-- TinyFrame.h | 90 +++++------------ demo/demo.c | 197 +++++++++++++++++++++++++++++++++++++ demo/demo.h | 20 ++++ demo/hello/Makefile | 17 ++++ demo/hello/TF_Config.h | 25 +++++ demo/hello/master.c | 38 +++++++ demo/hello/slave.c | 29 ++++++ demo/test/Makefile | 11 +++ demo/test/TF_Config.h | 25 +++++ test.c => demo/test/test.c | 42 ++------ demo/utils.c | 32 ++++++ demo/utils.h | 15 +++ 17 files changed, 578 insertions(+), 117 deletions(-) delete mode 100644 Makefile create mode 100644 TF_Config.example.h create mode 100644 demo/demo.c create mode 100644 demo/demo.h create mode 100644 demo/hello/Makefile create mode 100644 demo/hello/TF_Config.h create mode 100644 demo/hello/master.c create mode 100644 demo/hello/slave.c create mode 100644 demo/test/Makefile create mode 100644 demo/test/TF_Config.h rename test.c => demo/test/test.c (55%) create mode 100644 demo/utils.c create mode 100644 demo/utils.h diff --git a/.gitignore b/.gitignore index 6dc0790..be999a8 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ *.i*86 *.x86_64 *.hex +*.bin # Debug files *.dSYM/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 9fbd81f..b644369 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,8 +4,20 @@ project(tf) set(CMAKE_CXX_STANDARD GNU89) set(SOURCE_FILES - test.c + demo/test/test.c + demo/test/TF_Config.h + demo/hello/master.c + demo/hello/slave.c + demo/hello/TF_Config.h + demo/demo.c + demo/demo.h TinyFrame.c - TinyFrame.h) + TinyFrame.h + TF_Config.example.h + demo/utils.c + demo/utils.h + ) + +include_directories(demo/test) add_executable(tf ${SOURCE_FILES}) diff --git a/Makefile b/Makefile deleted file mode 100644 index 259b362..0000000 --- a/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -build: tf.bin - -run: tf.bin - ./tf.bin - -debug: tf.bin - gdb -q -ex run ./tf.bin - -tf.bin: test.c TinyFrame.c TinyFrame.h - gcc -O0 -ggdb --std=gnu89 -Wall -Wno-main -Wextra test.c TinyFrame.c -I. -o tf.bin diff --git a/TF_Config.example.h b/TF_Config.example.h new file mode 100644 index 0000000..edbd372 --- /dev/null +++ b/TF_Config.example.h @@ -0,0 +1,70 @@ +// +// Rename to TF_Config.h +// + +#ifndef TF_CONFIG_H +#define TF_CONFIG_H + +#include +//#include // when using with esphttpd + +//----------------------------- FRAME FORMAT --------------------------------- +// The format can be adjusted to fit your particular application needs + +// If the connection is reliable, you can disable the SOF byte and checksums. +// That can save up to 9 bytes of overhead. + +// ,-----+----+-----+------+------------+- - - -+------------, +// | SOF | ID | LEN | TYPE | HEAD_CKSUM | DATA | PLD_CKSUM | +// | 1 | ? | ? | ? | ? | ... | ? | <- size (bytes) +// '-----+----+-----+------+------------+- - - -+------------' + +// !!! BOTH SIDES MUST USE THE SAME SETTINGS !!! + +// Adjust sizes as desired (1,2,4) +#define TF_ID_BYTES 1 +#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 + +// Use a SOF byte to mark the start of a frame +#define TF_USE_SOF_BYTE 1 +// Value of the SOF byte (if TF_USE_SOF_BYTE == 1) +#define TF_SOF_BYTE 0x01 + +//----------------------- PLATFORM COMPATIBILITY ---------------------------- + +// used for timeout tick counters - should be large enough for all used timeouts +typedef uint16_t TF_TICKS; + +// used in loops iterating over listeners +typedef uint8_t TF_COUNT; + +//----------------------------- PARAMETERS ---------------------------------- + +// Maximum send / receive payload size (static buffers size) +// Larger payloads will be rejected. +#define TF_MAX_PAYLOAD_RX 1024 +#define TF_MAX_PAYLOAD_TX 1024 + +// --- Listener counts - determine sizes of the static slot tables --- + +// Frame ID listeners (wait for response / multi-part message) +#define TF_MAX_ID_LST 10 +// Frame Type listeners (wait for frame with a specific first payload byte) +#define TF_MAX_TYPE_LST 10 +// Generic listeners (fallback if no other listener catches it) +#define TF_MAX_GEN_LST 5 + +// Timeout for receiving & parsing a frame +// ticks = number of calls to TF_Tick() +#define TF_PARSER_TIMEOUT_TICKS 10 + +//------------------------- End of user config ------------------------------ + +#endif //TF_CONFIG_H diff --git a/TinyFrame.c b/TinyFrame.c index cf8cb4d..b8af068 100644 --- a/TinyFrame.c +++ b/TinyFrame.c @@ -1,6 +1,7 @@ //--------------------------------------------------------------------------- #include "TinyFrame.h" #include +//#include "demo/utils.h" //--------------------------------------------------------------------------- // Compatibility with ESP8266 SDK @@ -87,9 +88,9 @@ static struct TinyFrameStruct { #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) + #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 == TF_CKSUM_CRC16 @@ -134,8 +135,8 @@ static struct TinyFrameStruct { 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_RESET(cksum) do { (cksum) = 0; } while (0) + #define CKSUM_ADD(cksum, byte) do { (cksum) = crc16_byte((cksum), (byte)); } while(0) #define CKSUM_FINALIZE(cksum) #elif TF_CKSUM_TYPE == TF_CKSUM_CRC32 @@ -375,6 +376,8 @@ static void _TF_FN TF_HandleReceivedMessage(void) msg.data = tf.data; msg.len = tf.len; + //dumpFrameInfo(&msg); + // Any listener can consume the message (return true), // or let someone else handle it. @@ -385,6 +388,7 @@ static void _TF_FN TF_HandleReceivedMessage(void) for (i = 0; i < tf.count_id_lst; i++) { ilst = &tf.id_listeners[i]; if (ilst->fn && ilst->id == msg.frame_id) { + msg.renew = false; msg.userdata = ilst->userdata; // pass userdata pointer to the callback if (ilst->fn(&msg)) return; ilst->userdata = msg.userdata; // put it back (may have changed the pointer or set to NULL) @@ -561,6 +565,12 @@ void _TF_FN TF_AcceptChar(unsigned char c) } break; } + + // we get here after finishing HEAD, if no data are to be received - handle and clear + if (tf.len == 0 && tf.state == TFState_DATA) { + TF_HandleReceivedMessage(); + TF_ResetParser(); + } } /** @@ -660,7 +670,36 @@ static inline size_t _TF_FN TF_Compose(uint8_t *outbuff, TF_ID *id_ptr, return pos; } -bool _TF_FN TF_Send(TF_MSG *msg, TF_LISTENER listener, TF_TICKS timeout) +// send without listener +bool _TF_FN TF_Send(TF_MSG *msg) +{ + return TF_Query(msg, NULL, 0); +} + +// send without listener and struct +bool _TF_FN TF_SendSimple(TF_TYPE type, const uint8_t *data, TF_LEN len) +{ + TF_MSG msg; + TF_ClearMsg(&msg); + msg.type = type; + msg.data = data; + msg.len = len; + return TF_Send(&msg); +} + +// send without listener and struct +bool _TF_FN TF_QuerySimple(TF_TYPE type, const uint8_t *data, TF_LEN len, TF_LISTENER listener, TF_TICKS timeout) +{ + TF_MSG msg; + TF_ClearMsg(&msg); + msg.type = type; + msg.data = data; + msg.len = len; + return TF_Query(&msg, listener, timeout); +} + +// send with listener +bool _TF_FN TF_Query(TF_MSG *msg, TF_LISTENER listener, TF_TICKS timeout) { size_t len; len = TF_Compose(tf.sendbuf, @@ -680,12 +719,12 @@ bool _TF_FN TF_Send(TF_MSG *msg, TF_LISTENER listener, TF_TICKS timeout) } // Like TF_Send, but with explicit frame ID -bool _TF_FN TF_Respond(TF_MSG *msg, bool renew) +bool _TF_FN TF_Respond(TF_MSG *msg) { msg->is_response = true; - bool suc = TF_Send(msg, NULL, 0); + bool suc = TF_Send(msg); - if (suc && renew) TF_RenewIdListener(msg->frame_id); + if (suc && msg->renew) TF_RenewIdListener(msg->frame_id); return suc; } diff --git a/TinyFrame.h b/TinyFrame.h index 270233f..1ada271 100644 --- a/TinyFrame.h +++ b/TinyFrame.h @@ -5,70 +5,15 @@ #include // for uint8_t etc #include // for bool #include // for NULL -//#include "messages.h" // for your message IDs (enum or defines) -//#include // when using with esphttpd //--------------------------------------------------------------------------- - -//----------------------------- PARAMETERS ---------------------------------- - -// Maximum send / receive payload size (static buffers size) -// Larger payloads will be rejected. -#define TF_MAX_PAYLOAD_RX 1024 -#define TF_MAX_PAYLOAD_TX 1024 - -// --- Listener counts - determine sizes of the static slot tables --- - -// Frame ID listeners (wait for response / multi-part message) -#define TF_MAX_ID_LST 10 -// Frame Type listeners (wait for frame with a specific first payload byte) -#define TF_MAX_TYPE_LST 10 -// Generic listeners (fallback if no other listener catches it) -#define TF_MAX_GEN_LST 5 - -// Timeout for receiving & parsing a frame -// ticks = number of calls to TF_Tick() -#define TF_PARSER_TIMEOUT_TICKS 10 - -//----------------------------- FRAME FORMAT --------------------------------- -// The format can be adjusted to fit your particular application needs - -// If the connection is reliable, you can disable the SOF byte and checksums. -// That can save up to 9 bytes of overhead. - -// ,-----+----+-----+------+------------+- - - -+------------, -// | SOF | ID | LEN | TYPE | HEAD_CKSUM | DATA | PLD_CKSUM | -// | 1 | ? | ? | ? | ? | ... | ? | <- size (bytes) -// '-----+----+-----+------+------------+- - - -+------------' - -// !!! BOTH SIDES MUST USE THE SAME SETTINGS !!! - -// Adjust sizes as desired (1,2,4) -#define TF_ID_BYTES 1 -#define TF_LEN_BYTES 2 -#define TF_TYPE_BYTES 1 - // Select checksum type (0 = none, 8 = ~XOR, 16 = CRC16 0x8005, 32 = CRC32) -#define TF_CKSUM_NONE 0 -#define TF_CKSUM_XOR 8 +#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 -// Value of the SOF byte (if TF_USE_SOF_BYTE == 1) -#define TF_SOF_BYTE 0x01 - -// used for timeout tick counters - should be large enough for all used timeouts -typedef uint16_t TF_TICKS; - -// used in loops iterating over listeners -typedef uint8_t TF_COUNT; - -//------------------------- End of user config ------------------------------ - +#include //region Resolve data types @@ -153,6 +98,7 @@ typedef struct _TF_MSG_STRUCT_ { const uint8_t *data; //!< buffer of received data or data to send. NULL = listener timed out, free userdata! TF_LEN len; //!< length of the buffer void *userdata; //!< here's a place for custom data; this data will be stored with the listener + bool renew; //!< Renew the ID listener - if using timeout } TF_MSG; /** @@ -161,11 +107,12 @@ typedef struct _TF_MSG_STRUCT_ { static inline void TF_ClearMsg(TF_MSG *msg) { msg->frame_id = 0; - msg->is_response = 0; + msg->is_response = false; msg->type = 0; msg->data = NULL; msg->len = 0; msg->userdata = NULL; + msg->renew = false; } /** @@ -241,6 +188,24 @@ bool TF_AddGenericListener(TF_LISTENER cb); */ bool TF_RemoveGenericListener(TF_LISTENER cb); +/** + * Send a frame, no listener + * + * @param msg - message struct. ID is stored in the frame_id field + * @return success + */ +bool TF_Send(TF_MSG *msg); + +/** + * Like TF_Send, but without the struct + */ +bool TF_SendSimple(TF_TYPE type, const uint8_t *data, TF_LEN len); + +/** + * Like TF_Query, but without the struct + */ +bool TF_QuerySimple(TF_TYPE type, const uint8_t *data, TF_LEN len, TF_LISTENER listener, TF_TICKS timeout); + /** * Send a frame, and optionally attach an ID listener. * @@ -249,16 +214,15 @@ bool TF_RemoveGenericListener(TF_LISTENER cb); * @param timeout - listener expiry time in ticks * @return success */ -bool TF_Send(TF_MSG *msg, TF_LISTENER listener, TF_TICKS timeout); +bool TF_Query(TF_MSG *msg, TF_LISTENER listener, TF_TICKS timeout); /** * Send a response to a received message. * - * @param msg - message struct. ID is read from frame_id - * @param renew - renew the listener timeout (waiting for more data) + * @param msg - message struct. ID is read from frame_id. set ->renew to reset listener timeout * @return success */ -bool TF_Respond(TF_MSG *msg, bool renew); +bool TF_Respond(TF_MSG *msg); /** * Renew ID listener timeout diff --git a/demo/demo.c b/demo/demo.c new file mode 100644 index 0000000..9a5c95b --- /dev/null +++ b/demo/demo.c @@ -0,0 +1,197 @@ +// +// Created by MightyPork on 2017/10/15. +// + +// http://www.thegeekstuff.com/2011/12/c-socket-programming/?utm_source=feedburner + +#include "demo.h" + +#include +#include +#include +#include +#include +#include + +volatile int sockfd = -1; +volatile bool conn_disband = false; + +void demo_disconn(void) +{ + conn_disband = true; + if (sockfd >= 0) close(sockfd); +} + +void TF_WriteImpl(const uint8_t *buff, size_t len) +{ + printf("--------------------\n"); + printf("\033[32mTF_WriteImpl - sending frame:\033[0m\n"); + dumpFrame(buff, len); + + if (sockfd != -1) { + write(sockfd, buff, len); + } else { + printf("\nNo peer!\n"); + } +} + +static bool demo_client(void) +{ + pid_t childPID; + ssize_t n = 0; + uint8_t recvBuff[1024]; + struct sockaddr_in serv_addr; + + printf("\n--- STARTING CLIENT! ---\n"); + + memset(recvBuff, '0', sizeof(recvBuff)); + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + printf("\n Error : Could not create socket \n"); + return false; + } + + memset(&serv_addr, '0', sizeof(serv_addr)); + + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(PORT); + + if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) { + printf("\n inet_pton error occured\n"); + return false; + } + + if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + printf("\n Error : Connect Failed \n"); + perror("PERROR "); + return false; + } + + childPID = fork(); + if (childPID >= 0) { // fork was successful + if (childPID == 0) {// child process + printf("\n Child Process \n"); + + while ((n = read(sockfd, recvBuff, sizeof(recvBuff) - 1)) > 0 && !conn_disband) { + dumpFrame(recvBuff, (size_t) n); + TF_Accept(recvBuff, (size_t) n); + } + printf("\n End read \n"); + + if (n < 0) { + printf("\n Read error \n"); + } + + printf("\n Close sock \n"); + close(sockfd); + sockfd = -1; + + return true; + } + else { //Parent process + printf("\n Parent process \n"); + + return true; + } + } + else { // fork failed + printf("\n Fork failed!!!!!! \n"); + return false; + } +} + +static bool demo_server(void) +{ + pid_t childPID; + ssize_t n; + int listenfd = 0; + uint8_t recvBuff[1024]; + struct sockaddr_in serv_addr; + int option; + + printf("\n--- STARTING SERVER! ---\n"); + + listenfd = socket(AF_INET, SOCK_STREAM, 0); + memset(&serv_addr, '0', sizeof(serv_addr)); + + option = 1; + setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char*)&option, sizeof(option)); + + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); + serv_addr.sin_port = htons(PORT); + + if (bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + perror("Failed to bind "); + return false; + } + + if (listen(listenfd, 10) < 0) { + perror("Failed to listen "); + return false; + } + + childPID = fork(); + if (childPID >= 0) { // fork was successful + if (childPID == 0) {// child process + printf("\n Child Process \n"); + + while (1) { + printf("\nWaiting for client...\n"); + sockfd = accept(listenfd, (struct sockaddr *) NULL, NULL); + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&option, sizeof(option)); + printf("\nClient connected\n"); + conn_disband = false; + + while ((n = read(sockfd, recvBuff, sizeof(recvBuff) - 1)) > 0 && !conn_disband) { + printf("...read %ld\n", n); + dumpFrame(recvBuff, n); + TF_Accept(recvBuff, (size_t) n); + } + + if (n < 0) { + printf("\n Read error \n"); + } + + printf("Closing socket\n"); + close(sockfd); + sockfd = -1; + } + + return true; + } + else { //Parent process + printf("\n Parent process \n"); + + return true; + } + } + else { // fork failed + printf("\n Fork failed!!!!!! \n"); + return false; + } +} + +void signal_handler(int sig) +{ + (void)sig; + printf("Shutting down..."); + demo_disconn(); + exit(0); +} + +void demo_init(TF_PEER peer) +{ + signal(SIGTERM, signal_handler); + signal(SIGINT, signal_handler); + + bool suc; + if (peer == TF_MASTER) { + suc = demo_client(); + } else { + suc = demo_server(); + } + + if (!suc) { + signal_handler(9); + } +} diff --git a/demo/demo.h b/demo/demo.h new file mode 100644 index 0000000..0b64d61 --- /dev/null +++ b/demo/demo.h @@ -0,0 +1,20 @@ +// +// Created by MightyPork on 2017/10/15. +// + +#ifndef TF_DEMO_H +#define TF_DEMO_H + +#include +#include "../TinyFrame.h" +#include "utils.h" + +#define PORT 9798 + +/** Init server - DOES NOT init TinyFrame! */ +void demo_init(TF_PEER peer); + +/** Disconnect client from the server - claled by a server-side callback */ +void demo_disconn(void); + +#endif //TF_DEMO_H diff --git a/demo/hello/Makefile b/demo/hello/Makefile new file mode 100644 index 0000000..6c3e627 --- /dev/null +++ b/demo/hello/Makefile @@ -0,0 +1,17 @@ +CFILES=../demo.c ../utils.c ../../TinyFrame.c +INCLDIRS=-I. -I.. -I../.. +CFLAGS=-O0 -ggdb --std=gnu99 -Wno-main -Wall -Wextra $(CFILES) $(INCLDIRS) + +build: master.bin slave.bin + +master: master.bin + ./master.bin + +slave: slave.bin + ./slave.bin + +master.bin: master.c $(CFILES) + gcc master.c $(CFLAGS) -o master.bin + +slave.bin: slave.c $(CFILES) + gcc slave.c $(CFLAGS) -o slave.bin diff --git a/demo/hello/TF_Config.h b/demo/hello/TF_Config.h new file mode 100644 index 0000000..ea34ae6 --- /dev/null +++ b/demo/hello/TF_Config.h @@ -0,0 +1,25 @@ +// +// Created by MightyPork on 2017/10/15. +// + +#ifndef TF_CONFIG_H +#define TF_CONFIG_H + +#include + +#define TF_ID_BYTES 1 +#define TF_LEN_BYTES 2 +#define TF_TYPE_BYTES 1 +#define TF_CKSUM_TYPE TF_CKSUM_CRC32 +#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_MAX_PAYLOAD_TX 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 + +#endif //TF_CONFIG_H diff --git a/demo/hello/master.c b/demo/hello/master.c new file mode 100644 index 0000000..3c64960 --- /dev/null +++ b/demo/hello/master.c @@ -0,0 +1,38 @@ +// +// Created by MightyPork on 2017/10/15. +// + +#include +#include +#include +#include "../../TinyFrame.h" +#include "../demo.h" + +bool testIdListener(TF_MSG *msg) +{ + printf("testIdListener()\n"); + dumpFrameInfo(msg); + return true; +} + +bool testGenericListener(TF_MSG *msg) +{ + printf("testGenericListener()\n"); + dumpFrameInfo(msg); + return true; +} + +int main(void) +{ + TF_Init(TF_MASTER); + TF_AddGenericListener(testGenericListener); + + demo_init(TF_MASTER); + + TF_SendSimple(1, (pu8)"Ahoj", 5); + TF_SendSimple(1, (pu8)"Hello", 6); + + TF_QuerySimple(1, (pu8)"Query!", 6, testIdListener, 0); + + while(1) usleep(10); +} diff --git a/demo/hello/slave.c b/demo/hello/slave.c new file mode 100644 index 0000000..1f1f873 --- /dev/null +++ b/demo/hello/slave.c @@ -0,0 +1,29 @@ +// +// Created by MightyPork on 2017/10/15. +// + +#include +#include "../../TinyFrame.h" +#include "../demo.h" +#include +#include + +bool helloListener(TF_MSG *msg) +{ + printf("helloListener()\n"); + dumpFrameInfo(msg); + msg->data = (const uint8_t *) "jak se mas?"; + msg->len = (TF_LEN) strlen((const char *) msg->data); + TF_Respond(msg); + return true; +} + +int main(void) +{ + TF_Init(TF_SLAVE); + TF_AddTypeListener(1, helloListener); + + demo_init(TF_SLAVE); + printf("MAIN PROCESS CONTINUES...\n"); + while(1) usleep(10); +} diff --git a/demo/test/Makefile b/demo/test/Makefile new file mode 100644 index 0000000..118c385 --- /dev/null +++ b/demo/test/Makefile @@ -0,0 +1,11 @@ +CFILES=../utils.c ../../TinyFrame.c +INCLDIRS=-I. -I.. -I../.. +CFLAGS=-O0 -ggdb --std=gnu99 -Wno-main -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/test/TF_Config.h b/demo/test/TF_Config.h new file mode 100644 index 0000000..306b833 --- /dev/null +++ b/demo/test/TF_Config.h @@ -0,0 +1,25 @@ +// +// Created by MightyPork on 2017/10/15. +// + +#ifndef TF_CONFIG_H +#define TF_CONFIG_H + +#include + +#define TF_ID_BYTES 1 +#define TF_LEN_BYTES 2 +#define TF_TYPE_BYTES 1 +#define TF_CKSUM_TYPE TF_CKSUM_CRC16 +#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_MAX_PAYLOAD_TX 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 + +#endif //TF_CONFIG_H diff --git a/test.c b/demo/test/test.c similarity index 55% rename from test.c rename to demo/test/test.c index e128839..dc686cd 100644 --- a/test.c +++ b/demo/test/test.c @@ -1,11 +1,8 @@ #include -#include #include -#include "TinyFrame.h" +#include "../../TinyFrame.h" +#include "../utils.h" -typedef unsigned char* pu8; - -static void dumpFrame(const uint8_t *buff, TF_LEN len); /** * This function should be defined in the application code. @@ -24,19 +21,14 @@ void TF_WriteImpl(const uint8_t *buff, size_t len) /** An example listener function */ bool myListener(TF_MSG *msg) { - printf("\033[33mRX frame\n" - " type: %02Xh\n" - " data: \"%.*s\"\n" - " len: %u\n" - " id: %Xh\033[0m\n", - msg->type, msg->len, msg->data, msg->len, msg->frame_id); + dumpFrameInfo(msg); return true; } bool testIdListener(TF_MSG *msg) { - printf("OK - ID Listener triggered for msg (type %02X, id %Xh)!", - msg->type, msg->frame_id); + printf("OK - ID Listener triggered for msg!\n"); + dumpFrameInfo(msg); return true; } @@ -55,35 +47,19 @@ void main(void) msg.type = 0x22; msg.data = (pu8)"Hello TinyFrame"; msg.len = 16; - TF_Send(&msg, NULL, 0); + TF_Send(&msg); msg.type = 0x33; msg.data = (pu8)longstr; msg.len = (TF_LEN) (strlen(longstr)+1); // add the null byte - TF_Send(&msg, NULL, 0); + TF_Send(&msg); msg.type = 0x44; msg.data = (pu8)"Hello2"; msg.len = 7; - TF_Send(&msg, NULL, 0); + TF_Send(&msg); msg.len = 0; msg.type = 0x77; - TF_Send(&msg, testIdListener, 0); -} - -// helper func for testing -static void dumpFrame(const uint8_t *buff, TF_LEN len) -{ - int i; - for(i = 0; i < len; i++) { - printf("%3u \033[34m%02X\033[0m", buff[i], buff[i]); - if (buff[i] >= 0x20 && buff[i] < 127) { - printf(" %c", buff[i]); - } else { - printf(" \033[31m.\033[0m"); - } - printf("\n"); - } - printf("--- end of frame ---\n"); + TF_Query(&msg, testIdListener, 0); } diff --git a/demo/utils.c b/demo/utils.c new file mode 100644 index 0000000..8c2c891 --- /dev/null +++ b/demo/utils.c @@ -0,0 +1,32 @@ +// +// Created by MightyPork on 2017/10/15. +// + +#include "utils.h" +#include + +// helper func for testing +void dumpFrame(const uint8_t *buff, size_t len) +{ + size_t i; + for(i = 0; i < len; i++) { + printf("%3u \033[34m%02X\033[0m", buff[i], buff[i]); + if (buff[i] >= 0x20 && buff[i] < 127) { + printf(" %c", buff[i]); + } else { + printf(" \033[31m.\033[0m"); + } + printf("\n"); + } + printf("--- end of frame ---\n"); +} + +void dumpFrameInfo(TF_MSG *msg) +{ + printf("\033[33mRX frame\n" + " type: %02Xh\n" + " data: \"%.*s\"\n" + " len: %u\n" + " id: %Xh\033[0m\n", + msg->type, msg->len, msg->data, msg->len, msg->frame_id); +} diff --git a/demo/utils.h b/demo/utils.h new file mode 100644 index 0000000..06c4af5 --- /dev/null +++ b/demo/utils.h @@ -0,0 +1,15 @@ +// +// Created by MightyPork on 2017/10/15. +// + +#ifndef TF_UTILS_H +#define TF_UTILS_H + +#include +#include "../TinyFrame.h" + +typedef unsigned char* pu8; +void dumpFrame(const uint8_t *buff, size_t len); +void dumpFrameInfo(TF_MSG *msg); + +#endif //TF_UTILS_H