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