diff --git a/Makefile b/Makefile index bb21431..c38911f 100644 --- a/Makefile +++ b/Makefile @@ -11,5 +11,5 @@ clean: %.o: %.c *.h $(CC) -c $(CFLAGS) $< -o $@ -adsbus: adsbus.o backend.o client.o airspy_adsb.o beast.o json.o stats.o common.o - $(CC) $(LDFLAGS) -o adsbus adsbus.o backend.o client.o airspy_adsb.o beast.o json.o stats.o common.o $(LIBS) +adsbus: adsbus.o backend.o client.o airspy_adsb.o beast.o json.o raw.o stats.o common.o + $(CC) $(LDFLAGS) -o adsbus adsbus.o backend.o client.o airspy_adsb.o beast.o json.o raw.o stats.o common.o $(LIBS) diff --git a/airspy_adsb.c b/airspy_adsb.c index 39f06f4..06ce724 100644 --- a/airspy_adsb.c +++ b/airspy_adsb.c @@ -48,7 +48,7 @@ void airspy_adsb_init() { static bool airspy_adsb_parse_mode_s_short(struct buf *buf, struct packet *packet, struct airspy_adsb_parser_state *state) { struct airspy_adsb_mode_s_short_overlay *short_overlay = (struct airspy_adsb_mode_s_short_overlay *) buf_at(buf, 0); - if (buf->length < 35 || + if (buf->length < sizeof(*short_overlay) || short_overlay->asterisk != '*' || short_overlay->semicolon != ';' || short_overlay->cr != '\r' || @@ -66,7 +66,7 @@ static bool airspy_adsb_parse_mode_s_short(struct buf *buf, struct packet *packe static bool airspy_adsb_parse_mode_s_long(struct buf *buf, struct packet *packet, struct airspy_adsb_parser_state *state) { struct airspy_adsb_mode_s_long_overlay *long_overlay = (struct airspy_adsb_mode_s_long_overlay *) buf_at(buf, 0); - if (buf->length < 49 || + if (buf->length < sizeof(*long_overlay) || long_overlay->asterisk != '*' || long_overlay->semicolon != ';' || long_overlay->cr != '\r' || diff --git a/backend.c b/backend.c index adad9f2..7ed8c20 100644 --- a/backend.c +++ b/backend.c @@ -9,6 +9,8 @@ #include "airspy_adsb.h" #include "beast.h" +#include "raw.h" + #include "client.h" #include "backend.h" @@ -33,6 +35,10 @@ struct parser { .name = "beast", .parse = beast_parse, }, + { + .name = "raw", + .parse = raw_parse, + }, }; #define NUM_PARSERS (sizeof(parsers) / sizeof(*parsers)) diff --git a/raw.c b/raw.c new file mode 100644 index 0000000..230a358 --- /dev/null +++ b/raw.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include + +#include "common.h" +#include "raw.h" + + +struct __attribute__((packed)) raw_mode_s_short_overlay { + char asterisk; + char payload[14]; + char semicolon; + char lf; +}; + +struct __attribute__((packed)) raw_mode_s_long_overlay { + char asterisk; + char payload[28]; + char semicolon; + char lf; +}; + + +void raw_init() { + assert(sizeof(struct raw_mode_s_short_overlay) < BUF_LEN_MAX); + assert(sizeof(struct raw_mode_s_long_overlay) < BUF_LEN_MAX); +} + +static bool raw_parse_mode_s_short(struct buf *buf, struct packet *packet) { + struct raw_mode_s_short_overlay *short_overlay = (struct raw_mode_s_short_overlay *) buf_at(buf, 0); + if (buf->length < sizeof(*short_overlay) || + short_overlay->asterisk != '*' || + short_overlay->semicolon != ';' || + short_overlay->lf != '\n') { + return false; + } + packet->type = MODE_S_SHORT; + hex_to_bin(packet->payload, short_overlay->payload, sizeof(short_overlay->payload) / 2); + buf_consume(buf, sizeof(*short_overlay)); + return true; +} + +static bool raw_parse_mode_s_long(struct buf *buf, struct packet *packet) { + struct raw_mode_s_long_overlay *long_overlay = (struct raw_mode_s_long_overlay *) buf_at(buf, 0); + if (buf->length < sizeof(*long_overlay) || + long_overlay->asterisk != '*' || + long_overlay->semicolon != ';' || + long_overlay->lf != '\n') { + return false; + } + packet->type = MODE_S_LONG; + hex_to_bin(packet->payload, long_overlay->payload, sizeof(long_overlay->payload) / 2); + buf_consume(buf, sizeof(*long_overlay)); + return true; +} + +bool raw_parse(struct backend *backend, struct packet *packet) { + struct buf *buf = &backend->buf; + + return ( + raw_parse_mode_s_short(buf, packet) || + raw_parse_mode_s_long(buf, packet)); +} diff --git a/raw.h b/raw.h new file mode 100644 index 0000000..56a3aca --- /dev/null +++ b/raw.h @@ -0,0 +1,8 @@ +#pragma once + +#include +#include "backend.h" +#include "common.h" + +void raw_init(); +bool raw_parse(struct backend *, struct packet *);