parent
e89ca3a6cb
commit
46883a9baa
@ -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
|
@ -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); |
||||
} |
||||
} |
@ -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
|
@ -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
|
Loading…
Reference in new issue