make it work with new tinyframe

master
Ondřej Hruška 7 years ago
parent 6dce9ec8e3
commit 852c0946bb
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 2
      CMakeLists.txt
  2. 5
      gex/TF_Integration.c
  3. 62
      gex/gex_client.c
  4. 6
      gex/gex_client.h
  5. 9
      gex/protocol/TinyFrame.c
  6. 9
      gex/protocol/TinyFrame.h
  7. 17
      main.c

@ -11,7 +11,7 @@ set(SOURCE_FILES
gex/gex_client.h gex/gex_client.h
gex/utils/hexdump.c gex/utils/hexdump.c
gex/utils/hexdump.h gex/utils/hexdump.h
gex/protocol/TF_Integration.c gex/TF_Integration.c
gex/utils/payload_builder.c gex/utils/payload_builder.c
gex/utils/payload_builder.h gex/utils/payload_builder.h
gex/utils/payload_parser.c gex/utils/payload_parser.c

@ -8,9 +8,10 @@
void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, size_t len) void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, size_t len)
{ {
assert(gex_serial_fd != 0); // TODO update after TF has instances GexClient *gc = tf->userdata;
assert(gc->acm_fd != 0);
ssize_t rv = write(gex_serial_fd, buff, len); ssize_t rv = write(gc->acm_fd, buff, len);
if (rv != len) { if (rv != len) {
fprintf(stderr, "ERROR %d in TF write: %s\n", errno, strerror(errno)); fprintf(stderr, "ERROR %d in TF write: %s\n", errno, strerror(errno));
} }

@ -5,7 +5,6 @@
#include <malloc.h> #include <malloc.h>
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
@ -14,24 +13,11 @@
#include "gex_client.h" #include "gex_client.h"
#include "serial.h" #include "serial.h"
int gex_serial_fd = -1; TF_Result connectivityCheckCb(TinyFrame *tf, TF_Msg *msg)
/** ^C handler to close it gracefully */
static void sigintHandler(int sig)
{
if (gex_serial_fd != -1) {
close(gex_serial_fd);
}
exit(0);
}
TF_Result connectivityCheckCb(TF_Msg *msg)
{ {
GexClient *gc = msg->userdata; GexClient *gex = tf->userdata;
gc->connected = true; gex->connected = true;
fprintf(stderr, "GEX connected! Version string: %.*s\n", msg->len, msg->data); fprintf(stderr, "GEX connected! Version string: %.*s\n", msg->len, msg->data);
msg->userdata = NULL;
return TF_CLOSE; return TF_CLOSE;
} }
@ -39,54 +25,51 @@ GexClient *GEX_Init(const char *device, int timeout_ms)
{ {
assert(device != NULL); assert(device != NULL);
GexClient *gc = calloc(1, sizeof(GexClient)); GexClient *gex = calloc(1, sizeof(GexClient));
assert(gc != NULL); assert(gex != NULL);
// Bind ^C handler for safe shutdown
signal(SIGINT, sigintHandler);
// Open the device // Open the device
gc->acm_device = device; gex->acm_device = device;
gc->acm_fd = serial_open(device, false, (timeout_ms+50)/100); gex->acm_fd = serial_open(device, false, (timeout_ms + 50) / 100);
if (gc->acm_fd == -1) { if (gex->acm_fd == -1) {
free(gc); free(gex);
return NULL; return NULL;
} }
gex_serial_fd = gc->acm_fd; gex->tf = TF_Init(TF_MASTER);
gex->tf->userdata = gex;
// Test connectivity // Test connectivity
TF_Msg msg; TF_Msg msg;
TF_ClearMsg(&msg); TF_ClearMsg(&msg);
msg.type = 0x01; // TODO use constant msg.type = 0x01; // TODO use constant
msg.userdata = gc; TF_Query(gex->tf, &msg, connectivityCheckCb, 0);
TF_Query(&msg, connectivityCheckCb, 0); GEX_Poll(gex);
GEX_Poll(gc);
if (!gc->connected) { if (!gex->connected) {
fprintf(stderr, "GEX doesn't respond to ping!\n"); fprintf(stderr, "GEX doesn't respond to ping!\n");
GEX_DeInit(gc); GEX_DeInit(gex);
return NULL; return NULL;
} }
// TODO load and store unit callsigns + names // TODO load and store unit callsigns + names
return gc; return gex;
} }
void GEX_Poll(GexClient *gc) void GEX_Poll(GexClient *gex)
{ {
static uint8_t pollbuffer[4096]; uint8_t pollbuffer[1024];
assert(gc != NULL); assert(gex != NULL);
ssize_t len = read(gc->acm_fd, pollbuffer, 4096); ssize_t len = read(gex->acm_fd, pollbuffer, 1024);
if (len < 0) { if (len < 0) {
fprintf(stderr, "ERROR %d in GEX Poll: %s\n", errno, strerror(errno)); fprintf(stderr, "ERROR %d in GEX Poll: %s\n", errno, strerror(errno));
} else { } else {
//hexDump("Received", pollbuffer, (uint32_t) len); //hexDump("Received", pollbuffer, (uint32_t) len);
TF_Accept(pollbuffer, (size_t) len); TF_Accept(gex->tf, pollbuffer, (size_t) len);
} }
} }
@ -94,8 +77,7 @@ void GEX_Poll(GexClient *gc)
void GEX_DeInit(GexClient *gc) void GEX_DeInit(GexClient *gc)
{ {
if (gc == NULL) return; if (gc == NULL) return;
close(gc->acm_fd); close(gc->acm_fd);
gex_serial_fd = -1; TF_DeInit(gc->tf);
free(gc); free(gc);
} }

@ -8,8 +8,10 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "TinyFrame.h"
struct gex_client_ { struct gex_client_ {
TinyFrame *tf;
const char *acm_device; const char *acm_device;
int acm_fd; int acm_fd;
bool connected; bool connected;
@ -28,9 +30,9 @@ GexClient *GEX_Init(const char *device, int timeout_ms);
/** /**
* Poll for new messages * Poll for new messages
* @param gc - client * @param gex - client
*/ */
void GEX_Poll(GexClient *gc); void GEX_Poll(GexClient *gex);
/** /**
* Safely release all resources used up by GEX_Init() * Safely release all resources used up by GEX_Init()

@ -169,6 +169,13 @@ TinyFrame * _TF_FN TF_Init(TF_Peer peer_bit)
return tf; return tf;
} }
/** Release the struct */
void TF_DeInit(TinyFrame *tf)
{
if (tf == NULL) return;
free(tf);
}
//region Listeners //region Listeners
/** Reset ID listener's timeout to the original value */ /** Reset ID listener's timeout to the original value */
@ -696,7 +703,7 @@ static size_t _TF_FN TF_ComposeBody(uint8_t *outbuff,
* @param cksum - checksum variable used for the body * @param cksum - checksum variable used for the body
* @return nr of bytes in outbuff used * @return nr of bytes in outbuff used
*/ */
static size_t _TF_FN TF_ComposeTail(uint8_t *outbuff, const TF_CKSUM *cksum) static size_t _TF_FN TF_ComposeTail(uint8_t *outbuff, TF_CKSUM *cksum)
{ {
int8_t si = 0; // signed small int int8_t si = 0; // signed small int
uint8_t b = 0; uint8_t b = 0;

@ -10,7 +10,7 @@
* Upstream URL: https://github.com/MightyPork/TinyFrame * Upstream URL: https://github.com/MightyPork/TinyFrame
*/ */
#define TF_VERSION "2.0.0" #define TF_VERSION "2.0.1"
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include <stdint.h> // for uint8_t etc #include <stdint.h> // for uint8_t etc
@ -235,6 +235,13 @@ TinyFrame *TF_Init(TF_Peer peer_bit);
*/ */
void TF_InitStatic(TinyFrame *tf, TF_Peer peer_bit); void TF_InitStatic(TinyFrame *tf, TF_Peer peer_bit);
/**
* De-init the dynamically allocated TF instance
*
* @param tf
*/
void TF_DeInit(TinyFrame *tf);
/** /**
* Reset the frame parser state machine. * Reset the frame parser state machine.
* This does not affect registered listeners. * This does not affect registered listeners.

@ -1,10 +1,25 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "gex_client.h" #include "gex_client.h"
#include <signal.h>
static GexClient *gex;
/** ^C handler to close it gracefully */
static void sigintHandler(int sig)
{
if (gex != NULL) {
GEX_DeInit(gex);
}
exit(0);
}
int main() int main()
{ {
GexClient *gex = GEX_Init("/dev/ttyACM0", 200); // Bind ^C handler for safe shutdown
signal(SIGINT, sigintHandler);
gex = GEX_Init("/dev/ttyACM0", 200);
if (!gex) { if (!gex) {
fprintf(stderr, "FAILED TO CONNECT, ABORTING!\n"); fprintf(stderr, "FAILED TO CONNECT, ABORTING!\n");
exit(1); exit(1);

Loading…
Cancel
Save