pull/9/head
Ondřej Hruška 7 years ago
parent b6532962a4
commit b89f458c19
  1. 17
      TinyFrame.c
  2. 2
      TinyFrame.h
  3. 39
      test.c

@ -40,7 +40,7 @@ typedef struct _GenericListener_struct_ {
/** /**
* Frame parser internal state * Frame parser internal state
*/ */
static struct TinyFrameInstance { static struct TinyFrameStruct {
/* Own state */ /* Own state */
TF_PEER peer_bit; //!< Own peer bit (unqiue to avoid msg ID clash) TF_PEER peer_bit; //!< Own peer bit (unqiue to avoid msg ID clash)
TF_ID next_id; //!< Next frame / frame chain ID TF_ID next_id; //!< Next frame / frame chain ID
@ -341,7 +341,7 @@ static void _TF_FN TF_HandleReceivedMessage(void)
// ID listeners first // ID listeners first
for (i = 0; i < tf.count_id_lst; i++) { for (i = 0; i < tf.count_id_lst; i++) {
ilst = &tf.id_listeners[i]; ilst = &tf.id_listeners[i];
if (ilst->fn && ilst->id == frame_id) { if (ilst->fn && ilst->id == msg.frame_id) {
msg.userdata = ilst->userdata; msg.userdata = ilst->userdata;
if (ilst->fn(&msg)) return; if (ilst->fn(&msg)) return;
} }
@ -352,7 +352,7 @@ static void _TF_FN TF_HandleReceivedMessage(void)
// Type listeners // Type listeners
for (i = 0; i < tf.count_type_lst; i++) { for (i = 0; i < tf.count_type_lst; i++) {
tlst = &tf.type_listeners[i]; tlst = &tf.type_listeners[i];
if (tlst->fn && tlst->type == type) { if (tlst->fn && tlst->type == msg.type) {
if (tlst->fn(&msg)) return; if (tlst->fn(&msg)) return;
} }
} }
@ -476,7 +476,7 @@ void _TF_FN TF_AcceptChar(unsigned char c)
CKSUM_RESET(tf.cksum); // Start collecting the payload CKSUM_RESET(tf.cksum); // Start collecting the payload
if (tf.len >= TF_MAX_PAYLOAD) { if (tf.len >= TF_MAX_PAYLOAD_RX) {
// ERROR - frame too long. Consume, but do not store. // ERROR - frame too long. Consume, but do not store.
tf.discard_data = true; tf.discard_data = true;
} }
@ -546,7 +546,7 @@ static inline int _TF_FN TF_Compose(uint8_t *outbuff, TF_ID *id_ptr,
CKSUM_RESET(cksum); CKSUM_RESET(cksum);
// sanitize len // sanitize len
if (data_len > TF_MAX_PAYLOAD) { if (data_len > TF_MAX_PAYLOAD_TX) {
return TF_ERROR; return TF_ERROR;
} }
@ -626,7 +626,7 @@ bool _TF_FN TF_Send(TF_MSG *msg, TF_LISTENER listener, TF_TICKS timeout)
if (len == TF_ERROR) return false; if (len == TF_ERROR) return false;
if (listener) TF_AddIdListener(msg->frame_id, listener); if (listener) TF_AddIdListener(msg, listener, timeout);
TF_WriteImpl((const uint8_t *) tf.sendbuf, (TF_LEN)len); TF_WriteImpl((const uint8_t *) tf.sendbuf, (TF_LEN)len);
return true; return true;
@ -644,6 +644,7 @@ bool _TF_FN TF_Respond(TF_MSG *msg, bool renew)
bool _TF_FN TF_RenewIdListener(TF_ID id) bool _TF_FN TF_RenewIdListener(TF_ID id)
{ {
size_t i;
IdListener *lst; IdListener *lst;
for (i = 0; i < tf.count_id_lst; i++) { for (i = 0; i < tf.count_id_lst; i++) {
lst = &tf.id_listeners[i]; lst = &tf.id_listeners[i];
@ -658,7 +659,7 @@ bool _TF_FN TF_RenewIdListener(TF_ID id)
/** Timebase hook - for timeouts */ /** Timebase hook - for timeouts */
void _TF_FN TF_Tick(void) void _TF_FN TF_Tick(void)
{ {
int i; size_t i;
TF_MSG msg; TF_MSG msg;
IdListener *lst; IdListener *lst;
@ -677,7 +678,7 @@ void _TF_FN TF_Tick(void)
msg.userdata = lst->userdata; msg.userdata = lst->userdata;
msg.data = NULL; // this is a signal that listener should clean up msg.data = NULL; // this is a signal that listener should clean up
lst->fn(msg); lst->fn(&msg);
lst->fn = NULL; // Discard listener lst->fn = NULL; // Discard listener
} }
} }

@ -139,7 +139,7 @@ typedef enum {
} TF_PEER; } TF_PEER;
/** Data structure for sending / receiving messages */ /** Data structure for sending / receiving messages */
typedef struct { typedef struct _TF_MSG_STRUCT_ {
TF_ID frame_id; // message ID TF_ID frame_id; // message ID
bool is_response; // internal flag, set when using the Respond function. frame_id is then kept unchanged. 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 TF_TYPE type; // received or sent message type

@ -3,6 +3,8 @@
#include <string.h> #include <string.h>
#include "TinyFrame.h" #include "TinyFrame.h"
typedef unsigned char* pu8;
static void dumpFrame(const uint8_t *buff, TF_LEN len); static void dumpFrame(const uint8_t *buff, TF_LEN len);
/** /**
@ -20,19 +22,21 @@ void TF_WriteImpl(const uint8_t *buff, TF_LEN len)
} }
/** An example listener function */ /** An example listener function */
bool myListener(TF_ID frame_id, TF_TYPE type, const uint8_t *buff, TF_LEN len) bool myListener(TF_MSG *msg)
{ {
printf("\033[33mRX frame\n" printf("\033[33mRX frame\n"
" type: %02Xh\n" " type: %02Xh\n"
" data: \"%.*s\"\n" " data: \"%.*s\"\n"
" len: %u\n" " len: %u\n"
" id: %Xh\033[0m\n", type, len, buff, len, frame_id); " id: %Xh\033[0m\n",
msg->type, msg->len, msg->data, msg->len, msg->frame_id);
return true; return true;
} }
bool testIdListener(TF_ID frame_id, TF_TYPE type, const uint8_t *buff, TF_LEN len) bool testIdListener(TF_MSG *msg)
{ {
printf("OK - ID Listener triggered for msg (type %02X, id %Xh)!", type, frame_id); printf("OK - ID Listener triggered for msg (type %02X, id %Xh)!",
msg->type, msg->frame_id);
return true; return true;
} }
@ -44,20 +48,27 @@ void main(void)
printf("------ Simulate sending a message --------\n"); printf("------ Simulate sending a message --------\n");
TF_Send(0x22, (unsigned char*)"Hello TinyFrame", 16, NULL, NULL); TF_MSG msg;
TF_ClearMsg(&msg);
msg.type = 0x22;
msg.data = (pu8)"Hello TinyFrame";
msg.len = 16;
TF_Send(&msg, NULL, 0);
const char *longstr = "Lorem ipsum dolor sit amet."; const char *longstr = "Lorem ipsum dolor sit amet.";
TF_Send(0x33, (unsigned char*)longstr, (TF_LEN)(strlen(longstr)+1), NULL, NULL); msg.type = 0x33;
msg.data = (pu8)longstr;
TF_Send(0x44, (unsigned char*)"Hello2", 7, NULL, NULL); msg.len = strlen(longstr)+1; // add the null byte
TF_Send(&msg, NULL, 0);
TF_Send0(0xF0, NULL, NULL);
TF_Send1(0xF1, 'Q', NULL, NULL);
TF_Send2(0xF2, 'A', 'Z', NULL, NULL); msg.type = 0x44;
msg.data = (pu8)"Hello2";
msg.len = 7;
TF_Send(&msg, NULL, 0);
TF_Send0(0x77, testIdListener, NULL); msg.len = 0;
msg.type = 0x77;
TF_Send(&msg, testIdListener, 0);
} }
// helper func for testing // helper func for testing

Loading…
Cancel
Save