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. 120
      gex/gex_client.c
  4. 12
      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,88 +13,71 @@
#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) { GexClient *gex = tf->userdata;
close(gex_serial_fd); gex->connected = true;
} fprintf(stderr, "GEX connected! Version string: %.*s\n", msg->len, msg->data);
exit(0); return TF_CLOSE;
}
TF_Result connectivityCheckCb(TF_Msg *msg)
{
GexClient *gc = msg->userdata;
gc->connected = true;
fprintf(stderr, "GEX connected! Version string: %.*s\n", msg->len, msg->data);
msg->userdata = NULL;
return TF_CLOSE;
} }
GexClient *GEX_Init(const char *device, int timeout_ms) 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 // Open the device
signal(SIGINT, sigintHandler); gex->acm_device = device;
gex->acm_fd = serial_open(device, false, (timeout_ms + 50) / 100);
// Open the device if (gex->acm_fd == -1) {
gc->acm_device = device; free(gex);
gc->acm_fd = serial_open(device, false, (timeout_ms+50)/100); return NULL;
if (gc->acm_fd == -1) { }
free(gc);
return NULL; gex->tf = TF_Init(TF_MASTER);
} gex->tf->userdata = gex;
gex_serial_fd = gc->acm_fd; // Test connectivity
TF_Msg msg;
// Test connectivity TF_ClearMsg(&msg);
TF_Msg msg; msg.type = 0x01; // TODO use constant
TF_ClearMsg(&msg); TF_Query(gex->tf, &msg, connectivityCheckCb, 0);
msg.type = 0x01; // TODO use constant GEX_Poll(gex);
msg.userdata = gc;
TF_Query(&msg, connectivityCheckCb, 0); if (!gex->connected) {
GEX_Poll(gc); fprintf(stderr, "GEX doesn't respond to ping!\n");
GEX_DeInit(gex);
if (!gc->connected) { return NULL;
fprintf(stderr, "GEX doesn't respond to ping!\n"); }
GEX_DeInit(gc);
return NULL; // TODO load and store unit callsigns + names
}
return gex;
// TODO load and store unit callsigns + names
return gc;
} }
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);
} }
} }
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); TF_DeInit(gc->tf);
gex_serial_fd = -1; free(gc);
free(gc);
} }

@ -8,11 +8,13 @@
#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_ {
const char *acm_device; TinyFrame *tf;
int acm_fd; const char *acm_device;
bool connected; int acm_fd;
bool connected;
}; };
typedef struct gex_client_ GexClient; typedef struct gex_client_ GexClient;
@ -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