kinda working demo
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
*.bin
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
|
||||
+14
-2
@@ -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})
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// Rename to TF_Config.h
|
||||
//
|
||||
|
||||
#ifndef TF_CONFIG_H
|
||||
#define TF_CONFIG_H
|
||||
|
||||
#include <stdint.h>
|
||||
//#include <esp8266.h> // 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
|
||||
+48
-9
@@ -1,6 +1,7 @@
|
||||
//---------------------------------------------------------------------------
|
||||
#include "TinyFrame.h"
|
||||
#include <string.h>
|
||||
//#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;
|
||||
}
|
||||
|
||||
|
||||
+27
-63
@@ -5,70 +5,15 @@
|
||||
#include <stdint.h> // for uint8_t etc
|
||||
#include <stdbool.h> // for bool
|
||||
#include <stdlib.h> // for NULL
|
||||
//#include "messages.h" // for your message IDs (enum or defines)
|
||||
//#include <esp8266.h> // 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 <TF_Config.h>
|
||||
|
||||
//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
|
||||
|
||||
+197
@@ -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 <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <signal.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/10/15.
|
||||
//
|
||||
|
||||
#ifndef TF_DEMO_H
|
||||
#define TF_DEMO_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#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
|
||||
@@ -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
|
||||
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/10/15.
|
||||
//
|
||||
|
||||
#ifndef TF_CONFIG_H
|
||||
#define TF_CONFIG_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/10/15.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <zconf.h>
|
||||
#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);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/10/15.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include "../../TinyFrame.h"
|
||||
#include "../demo.h"
|
||||
#include <unistd.h>
|
||||
#include <memory.h>
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -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
|
||||
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/10/15.
|
||||
//
|
||||
|
||||
#ifndef TF_CONFIG_H
|
||||
#define TF_CONFIG_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
@@ -1,11 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/10/15.
|
||||
//
|
||||
|
||||
#include "utils.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Created by MightyPork on 2017/10/15.
|
||||
//
|
||||
|
||||
#ifndef TF_UTILS_H
|
||||
#define TF_UTILS_H
|
||||
|
||||
#include <stdio.h>
|
||||
#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
|
||||
Reference in New Issue
Block a user