diff --git a/adsbus/Makefile b/adsbus/Makefile index 06545bd..635b9ce 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -3,9 +3,9 @@ CFLAGS ?= -Wall -Werror -O4 -g --std=gnu11 --pedantic-errors -fPIE -pie -fstack- LDFLAGS ?= $(CFLAGS) -Wl,-z,relro -Wl,-z,now LIBS ?= -ljansson -OBJ_NETWORK = receive.o send.o incoming.o outgoing.o +OBJ_NETWORK = incoming.o outgoing.o receive.o send.o OBJ_PROTOCOL = airspy_adsb.o beast.o json.o raw.o stats.o -OBJ_UTIL = rand.o uuid.o wakeup.o opts.o hex.o common.o +OBJ_UTIL = buf.o hex.o opts.o rand.o uuid.o wakeup.o common.o all: adsbus diff --git a/adsbus/airspy_adsb.c b/adsbus/airspy_adsb.c index 46885bf..31e2365 100644 --- a/adsbus/airspy_adsb.c +++ b/adsbus/airspy_adsb.c @@ -3,6 +3,7 @@ #include #include "common.h" +#include "buf.h" #include "hex.h" #include "receive.h" #include "uuid.h" diff --git a/adsbus/beast.c b/adsbus/beast.c index 6b318c4..6f97c90 100644 --- a/adsbus/beast.c +++ b/adsbus/beast.c @@ -4,7 +4,9 @@ #include #include "common.h" +#include "buf.h" #include "receive.h" + #include "beast.h" struct __attribute__((packed)) beast_common_overlay { diff --git a/adsbus/buf.c b/adsbus/buf.c new file mode 100644 index 0000000..41fd330 --- /dev/null +++ b/adsbus/buf.c @@ -0,0 +1,37 @@ +#include +#include +#include + +#include "buf.h" + +void buf_init(struct buf *buf) { + buf->start = 0; + buf->length = 0; +} + +ssize_t buf_fill(struct buf *buf, int fd) { + if (buf->start + buf->length == BUF_LEN_MAX) { + assert(buf->start > 0); + memmove(buf->buf, buf_at(buf, 0), buf->length); + buf->start = 0; + } + + size_t space = BUF_LEN_MAX - buf->length - buf->start; + ssize_t in = read(fd, buf_at(buf, buf->length), space); + if (in <= 0) { + return in; + } + buf->length += in; + return in; +} + +void buf_consume(struct buf *buf, size_t length) { + assert(buf->length >= length); + + buf->length -= length; + if (buf->length) { + buf->start += length; + } else { + buf->start = 0; + } +} diff --git a/adsbus/buf.h b/adsbus/buf.h new file mode 100644 index 0000000..bb967c7 --- /dev/null +++ b/adsbus/buf.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +#define BUF_LEN_MAX 256 +struct buf { + char buf[BUF_LEN_MAX]; + 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)) + +void buf_init(struct buf *); +ssize_t buf_fill(struct buf *, int); +void buf_consume(struct buf *, size_t); diff --git a/adsbus/common.c b/adsbus/common.c index 5a29105..000ec21 100644 --- a/adsbus/common.c +++ b/adsbus/common.c @@ -85,39 +85,6 @@ void peer_loop() { } -void buf_init(struct buf *buf) { - buf->start = 0; - buf->length = 0; -} - -ssize_t buf_fill(struct buf *buf, int fd) { - if (buf->start + buf->length == BUF_LEN_MAX) { - assert(buf->start > 0); - memmove(buf->buf, buf_at(buf, 0), buf->length); - buf->start = 0; - } - - size_t space = BUF_LEN_MAX - buf->length - buf->start; - ssize_t in = read(fd, buf_at(buf, buf->length), space); - if (in <= 0) { - return in; - } - buf->length += in; - return in; -} - -void buf_consume(struct buf *buf, size_t length) { - assert(buf->length >= length); - - buf->length -= length; - if (buf->length) { - buf->start += length; - } else { - buf->start = 0; - } -} - - char *packet_type_names[] = { "Mode-S short", "Mode-S long", diff --git a/adsbus/common.h b/adsbus/common.h index f761531..3976bf2 100644 --- a/adsbus/common.h +++ b/adsbus/common.h @@ -19,27 +19,6 @@ void peer_epoll_del(struct peer *); void peer_loop(); -//////// buf - -#define BUF_LEN_MAX 256 -struct buf { - char buf[BUF_LEN_MAX]; - 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)) - -void buf_init(struct buf *); -ssize_t buf_fill(struct buf *, int); -void buf_consume(struct buf *, size_t); - - //////// packet #define DATA_LEN_MAX 14 diff --git a/adsbus/json.c b/adsbus/json.c index 13a3623..3aa5160 100644 --- a/adsbus/json.c +++ b/adsbus/json.c @@ -4,6 +4,7 @@ #include #include "hex.h" +#include "buf.h" #include "rand.h" #include "receive.h" #include "send.h" diff --git a/adsbus/json.h b/adsbus/json.h index bc324a6..b15600a 100644 --- a/adsbus/json.h +++ b/adsbus/json.h @@ -1,5 +1,7 @@ #pragma once +struct buf; + void json_init(); void json_serialize(struct packet *, struct buf *); diff --git a/adsbus/rand.c b/adsbus/rand.c index c6a79e1..1bba340 100644 --- a/adsbus/rand.c +++ b/adsbus/rand.c @@ -7,7 +7,7 @@ #include #include -#include "common.h" +#include "buf.h" #include "rand.h" diff --git a/adsbus/raw.c b/adsbus/raw.c index c735cd2..ed4c036 100644 --- a/adsbus/raw.c +++ b/adsbus/raw.c @@ -3,6 +3,7 @@ #include #include "common.h" +#include "buf.h" #include "hex.h" #include "uuid.h" diff --git a/adsbus/receive.c b/adsbus/receive.c index 3ccbb0c..b361902 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -6,10 +6,9 @@ #include "airspy_adsb.h" #include "beast.h" +#include "buf.h" #include "raw.h" - #include "send.h" - #include "uuid.h" #include "receive.h" diff --git a/adsbus/send.c b/adsbus/send.c index 08163be..41a6e70 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -10,10 +10,10 @@ #include "airspy_adsb.h" #include "beast.h" +#include "buf.h" #include "json.h" #include "raw.h" #include "stats.h" - #include "uuid.h" #include "send.h" diff --git a/adsbus/stats.c b/adsbus/stats.c index 723705f..306de43 100644 --- a/adsbus/stats.c +++ b/adsbus/stats.c @@ -3,7 +3,9 @@ #include #include "common.h" +#include "buf.h" #include "json.h" + #include "stats.h" static struct stats_state {