Clean up backend detection flow.
This commit is contained in:
41
adsbus.c
41
adsbus.c
@@ -18,18 +18,11 @@ struct opts {
|
||||
char *backend_service;
|
||||
};
|
||||
|
||||
typedef bool (*parser)(struct buf *, struct packet *, void *);
|
||||
static parser parsers[] = {
|
||||
airspy_adsb_parse,
|
||||
};
|
||||
#define NUM_PARSERS (sizeof(parsers) / sizeof(*parsers))
|
||||
|
||||
struct backend {
|
||||
struct buf buf;
|
||||
char parser_state[PARSER_STATE_LEN];
|
||||
parser parser;
|
||||
};
|
||||
|
||||
struct client {
|
||||
int placeholder;
|
||||
};
|
||||
@@ -47,14 +40,7 @@ struct peer {
|
||||
};
|
||||
#define PEER_BACKEND_INIT { \
|
||||
.type = BACKEND, \
|
||||
.backend = { \
|
||||
.buf = { \
|
||||
.start = 0, \
|
||||
.length = 0, \
|
||||
}, \
|
||||
.parser_state = { 0 }, \
|
||||
.parser = NULL, \
|
||||
}, \
|
||||
.backend = BACKEND_INIT, \
|
||||
}
|
||||
|
||||
|
||||
@@ -77,7 +63,6 @@ static int parse_opts(int argc, char *argv[], struct opts *opts) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int connect_backend(struct opts *opts) {
|
||||
struct addrinfo hints = {
|
||||
.ai_family = AF_UNSPEC,
|
||||
@@ -124,6 +109,15 @@ static int connect_backend(struct opts *opts) {
|
||||
return bfd;
|
||||
}
|
||||
|
||||
bool backend_autodetect_parse(struct backend *backend, struct packet *packet) {
|
||||
for (int i = 0; i < NUM_PARSERS; i++) {
|
||||
if (parsers[i](backend, packet)) {
|
||||
backend->parser = parsers[i];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int loop(int bfd) {
|
||||
struct peer backend = PEER_BACKEND_INIT;
|
||||
@@ -167,19 +161,7 @@ static int loop(int bfd) {
|
||||
}
|
||||
|
||||
struct packet packet;
|
||||
if (!peer->backend.parser) {
|
||||
// Attempt to autodetect format
|
||||
for (int i = 0; i < NUM_PARSERS; i++) {
|
||||
if (parsers[i](&peer->backend.buf, &packet, peer->backend.parser_state)) {
|
||||
peer->backend.parser = parsers[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (peer->backend.parser) {
|
||||
while (peer->backend.parser(&peer->backend.buf, &packet, peer->backend.parser_state)) {
|
||||
}
|
||||
while (peer->backend.parser(&peer->backend, &packet)) {
|
||||
}
|
||||
|
||||
if (peer->backend.buf.length == BUF_LEN_MAX) {
|
||||
@@ -196,7 +178,6 @@ static int loop(int bfd) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
hex_init();
|
||||
airspy_adsb_init();
|
||||
|
||||
@@ -6,24 +6,26 @@
|
||||
#include "common.h"
|
||||
#include "airspy_adsb.h"
|
||||
|
||||
struct backend_state {
|
||||
struct airspy_adsb_parser_state {
|
||||
uint64_t mlat_timestamp_last;
|
||||
uint64_t mlat_timestamp_generation;
|
||||
};
|
||||
|
||||
static bool airspy_adsb_parse_common(char *, struct packet *, struct backend_state *);
|
||||
static bool airspy_adsb_parse_common(char *, struct packet *, struct airspy_adsb_parser_state *);
|
||||
|
||||
|
||||
void airspy_adsb_init() {
|
||||
assert(sizeof(struct backend_state) <= PARSER_STATE_LEN);
|
||||
assert(sizeof(struct airspy_adsb_parser_state) <= PARSER_STATE_LEN);
|
||||
}
|
||||
|
||||
bool airspy_adsb_parse(struct buf *buf, struct packet *packet, void *state_in) {
|
||||
bool airspy_adsb_parse(struct backend *backend, struct packet *packet) {
|
||||
struct buf *buf = &backend->buf;
|
||||
struct airspy_adsb_parser_state *state = (struct airspy_adsb_parser_state *) backend->parser_state;
|
||||
|
||||
if (buf->length < 35 ||
|
||||
buf_chr(buf, 0) != '*') {
|
||||
return false;
|
||||
}
|
||||
struct backend_state *state = state_in;
|
||||
if (buf->length >= 35 &&
|
||||
buf_chr(buf, 33) == '\r' &&
|
||||
buf_chr(buf, 34) == '\n' &&
|
||||
@@ -51,7 +53,7 @@ bool airspy_adsb_parse(struct buf *buf, struct packet *packet, void *state_in) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool airspy_adsb_parse_common(char *in, struct packet *packet, struct backend_state *state) {
|
||||
static bool airspy_adsb_parse_common(char *in, struct packet *packet, struct airspy_adsb_parser_state *state) {
|
||||
if (in[8] != ';' ||
|
||||
in[11] != ';' ||
|
||||
in[16] != ';') {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
void airspy_adsb_init();
|
||||
bool airspy_adsb_parse(struct buf *, struct packet *, void *);
|
||||
bool airspy_adsb_parse(struct backend *, struct packet *);
|
||||
|
||||
27
common.h
27
common.h
@@ -1,12 +1,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
//////// misc
|
||||
|
||||
#define PARSER_STATE_LEN 256
|
||||
|
||||
|
||||
//////// buf
|
||||
|
||||
#define BUF_LEN_MAX 256
|
||||
@@ -15,6 +11,10 @@ struct buf {
|
||||
size_t start;
|
||||
size_t length;
|
||||
};
|
||||
#define BUF_INIT { \
|
||||
.start = 0, \
|
||||
.length = 0, \
|
||||
}
|
||||
|
||||
#define buf_chr(buff, at) ((buff)->buf[(buff)->start + (at)])
|
||||
#define buf_at(buff, at) (&buf_chr(buff, at))
|
||||
@@ -40,6 +40,23 @@ struct packet {
|
||||
};
|
||||
|
||||
|
||||
//////// backend
|
||||
|
||||
#define PARSER_STATE_LEN 256
|
||||
struct backend;
|
||||
typedef bool (*parser)(struct backend *, struct packet *);
|
||||
struct backend {
|
||||
struct buf buf;
|
||||
char parser_state[PARSER_STATE_LEN];
|
||||
parser parser;
|
||||
};
|
||||
#define BACKEND_INIT { \
|
||||
.buf = BUF_INIT, \
|
||||
.parser_state = { 0 }, \
|
||||
.parser = backend_autodetect_parse, \
|
||||
}
|
||||
|
||||
|
||||
//////// hex
|
||||
void hex_init();
|
||||
void hex_to_bin(char *, char *, size_t);
|
||||
|
||||
Reference in New Issue
Block a user