|
|
@ -56,13 +56,6 @@ static struct TinyFrameStruct { |
|
|
|
char sendbuf[TF_MAX_PAYLOAD+1]; |
|
|
|
char sendbuf[TF_MAX_PAYLOAD+1]; |
|
|
|
} tf; |
|
|
|
} tf; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Initialize the TinyFrame engine. |
|
|
|
|
|
|
|
* This can also be used to completely reset it (removing all listeners etc) |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param peer_bit - peer bit to use for self |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
void TF_Init(bool peer_bit) |
|
|
|
void TF_Init(bool peer_bit) |
|
|
|
{ |
|
|
|
{ |
|
|
|
memset(&tf, 0, sizeof(struct TinyFrameStruct)); |
|
|
|
memset(&tf, 0, sizeof(struct TinyFrameStruct)); |
|
|
@ -70,24 +63,12 @@ void TF_Init(bool peer_bit) |
|
|
|
tf.peer_bit = peer_bit; |
|
|
|
tf.peer_bit = peer_bit; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Reset the frame parser state machine. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
void TF_ResetParser(void) |
|
|
|
void TF_ResetParser(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
tf.state = TFState_SOF; |
|
|
|
tf.state = TFState_SOF; |
|
|
|
tf.cksum = 0; |
|
|
|
tf.cksum = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Register a frame type listener. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param frame_type - frame ID to listen for |
|
|
|
|
|
|
|
* @param cb - callback |
|
|
|
|
|
|
|
* @return slot index (for removing), or TF_ERROR (-1) |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
int TF_AddIdListener(unsigned int frame_id, TinyFrameListener cb) |
|
|
|
int TF_AddIdListener(unsigned int frame_id, TinyFrameListener cb) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int i; |
|
|
|
int i; |
|
|
@ -103,14 +84,6 @@ int TF_AddIdListener(unsigned int frame_id, TinyFrameListener cb) |
|
|
|
return TF_ERROR; |
|
|
|
return TF_ERROR; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Register a frame type listener. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param frame_type - frame type to listen for |
|
|
|
|
|
|
|
* @param cb - callback |
|
|
|
|
|
|
|
* @return slot index (for removing), or TF_ERROR (-1) |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
int TF_AddTypeListener(unsigned char frame_type, TinyFrameListener cb) |
|
|
|
int TF_AddTypeListener(unsigned char frame_type, TinyFrameListener cb) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int i; |
|
|
|
int i; |
|
|
@ -126,13 +99,6 @@ int TF_AddTypeListener(unsigned char frame_type, TinyFrameListener cb) |
|
|
|
return TF_ERROR; |
|
|
|
return TF_ERROR; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Register a generic listener. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param cb - callback |
|
|
|
|
|
|
|
* @return slot index (for removing), or TF_ERROR (-1) |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
int TF_AddGenericListener(TinyFrameListener cb) |
|
|
|
int TF_AddGenericListener(TinyFrameListener cb) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int i; |
|
|
|
int i; |
|
|
@ -147,12 +113,6 @@ int TF_AddGenericListener(TinyFrameListener cb) |
|
|
|
return TF_ERROR; |
|
|
|
return TF_ERROR; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Remove a rx callback function by the index received when registering it |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param index - index in the callbacks table |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
void TF_RemoveListener(unsigned int index) |
|
|
|
void TF_RemoveListener(unsigned int index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// all listener arrays share common "address space"
|
|
|
|
// all listener arrays share common "address space"
|
|
|
@ -187,12 +147,6 @@ void TF_RemoveTypeListener(unsigned char type) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Remove a callback by the function pointer |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param cb - callback function to remove |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
void TF_RemoveListenerFn(TinyFrameListener cb) |
|
|
|
void TF_RemoveListenerFn(TinyFrameListener cb) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int i; |
|
|
|
int i; |
|
|
@ -216,13 +170,6 @@ void TF_RemoveListenerFn(TinyFrameListener cb) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Accept incoming bytes & parse frames |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param buffer - byte buffer to process |
|
|
|
|
|
|
|
* @param count - nr of bytes in the buffer |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
void TF_Accept(const unsigned char *buffer, unsigned int count) |
|
|
|
void TF_Accept(const unsigned char *buffer, unsigned int count) |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int i; |
|
|
|
unsigned int i; |
|
|
@ -232,12 +179,6 @@ void TF_Accept(const unsigned char *buffer, unsigned int count) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Process a single received character |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param c - rx character |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
void TF_AcceptChar(unsigned char c) |
|
|
|
void TF_AcceptChar(unsigned char c) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int i; |
|
|
|
int i; |
|
|
@ -323,17 +264,6 @@ void TF_AcceptChar(unsigned char c) |
|
|
|
tf.cksum ^= c; |
|
|
|
tf.cksum ^= c; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Compose a frame |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param outbuff - buffer to store the result in |
|
|
|
|
|
|
|
* @param msgid - message ID is stored here, if not NULL |
|
|
|
|
|
|
|
* @param payload - data buffer |
|
|
|
|
|
|
|
* @param len - payload size in bytes, 0 to use strlen |
|
|
|
|
|
|
|
* @param explicit_id - ID to use, -1 to chose next free |
|
|
|
|
|
|
|
* @return nr of bytes in outbuff used by the frame, -1 on failure |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
int TF_Compose(unsigned char *outbuff, unsigned int *msgid, |
|
|
|
int TF_Compose(unsigned char *outbuff, unsigned int *msgid, |
|
|
|
const unsigned char *payload, unsigned int payload_len, |
|
|
|
const unsigned char *payload, unsigned int payload_len, |
|
|
|
int explicit_id |
|
|
|
int explicit_id |
|
|
@ -377,17 +307,10 @@ int TF_Compose(unsigned char *outbuff, unsigned int *msgid, |
|
|
|
return payload_len + 4; |
|
|
|
return payload_len + 4; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Send a frame, and optionally attach an ID listener for response. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param payload - data to send |
|
|
|
|
|
|
|
* @param payload_len - nr of bytes to send |
|
|
|
|
|
|
|
* @return listener ID if listener was attached, else -1 (FT_ERROR) |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
int TF_Send(const unsigned char *payload, |
|
|
|
int TF_Send(const unsigned char *payload, |
|
|
|
unsigned int payload_len, |
|
|
|
unsigned int payload_len, |
|
|
|
TinyFrameListener listener) |
|
|
|
TinyFrameListener listener, |
|
|
|
|
|
|
|
unsigned int *id_ptr) |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned int msgid; |
|
|
|
unsigned int msgid; |
|
|
|
int len; |
|
|
|
int len; |
|
|
@ -395,20 +318,15 @@ int TF_Send(const unsigned char *payload, |
|
|
|
len = TF_Compose(tf.sendbuf, &msgid, payload, payload_len, TF_NEXT_ID); |
|
|
|
len = TF_Compose(tf.sendbuf, &msgid, payload, payload_len, TF_NEXT_ID); |
|
|
|
if (listener) lstid = TF_AddIdListener(msgid, listener); |
|
|
|
if (listener) lstid = TF_AddIdListener(msgid, listener); |
|
|
|
TF_WriteImpl(tf.sendbuf, len); |
|
|
|
TF_WriteImpl(tf.sendbuf, len); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (id_ptr) *id_ptr = msgid; |
|
|
|
|
|
|
|
|
|
|
|
return lstid; |
|
|
|
return lstid; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Send a response to a received message. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param payload - data to send |
|
|
|
|
|
|
|
* @param payload_len - nr of bytes to send |
|
|
|
|
|
|
|
* @param frame_id - ID of the original frame |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
void TF_Respond(const unsigned char *payload, |
|
|
|
void TF_Respond(const unsigned char *payload, |
|
|
|
unsigned int payload_len, |
|
|
|
unsigned int payload_len, |
|
|
|
int frame_id) |
|
|
|
unsigned int frame_id) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int len; |
|
|
|
int len; |
|
|
|
len = TF_Compose(tf.sendbuf, NULL, payload, payload_len, frame_id); |
|
|
|
len = TF_Compose(tf.sendbuf, NULL, payload, payload_len, frame_id); |
|
|
@ -417,17 +335,19 @@ void TF_Respond(const unsigned char *payload, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int TF_Send1(const unsigned char b0, |
|
|
|
int TF_Send1(const unsigned char b0, |
|
|
|
TinyFrameListener listener) |
|
|
|
TinyFrameListener listener, |
|
|
|
|
|
|
|
unsigned int *id_ptr) |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned char b[] = {b0}; |
|
|
|
unsigned char b[] = {b0}; |
|
|
|
return TF_Send(b, 1, listener); |
|
|
|
return TF_Send(b, 1, listener, id_ptr); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int TF_Send2(const unsigned char b0, |
|
|
|
int TF_Send2(const unsigned char b0, |
|
|
|
const unsigned char b1, |
|
|
|
const unsigned char b1, |
|
|
|
TinyFrameListener listener) |
|
|
|
TinyFrameListener listener, |
|
|
|
|
|
|
|
unsigned int *id_ptr) |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned char b[] = {b0, b1}; |
|
|
|
unsigned char b[] = {b0, b1}; |
|
|
|
return TF_Send(b, 2, listener); |
|
|
|
return TF_Send(b, 2, listener, id_ptr); |
|
|
|
} |
|
|
|
} |
|
|
|