From 93558c0a23e752b2b43c8c473cb404a266c9c3e9 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 14 Feb 2016 20:35:43 +0000 Subject: [PATCH] Clean up buf silliness. --- adsbus.c | 20 +++++++++----------- airspy_adsb.c | 13 ++++++++++--- common.c | 11 ----------- common.h | 21 ++++++++++++--------- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/adsbus.c b/adsbus.c index 6f9582e..161ba60 100644 --- a/adsbus.c +++ b/adsbus.c @@ -19,7 +19,7 @@ struct opts { }; -int parse_opts(int argc, char *argv[], struct opts *opts) { +static int parse_opts(int argc, char *argv[], struct opts *opts) { int opt; while ((opt = getopt(argc, argv, "h:p:")) != -1) { switch (opt) { @@ -39,7 +39,7 @@ int parse_opts(int argc, char *argv[], struct opts *opts) { } -int connect_backend(struct opts *opts) { +static int connect_backend(struct opts *opts) { struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM, @@ -86,7 +86,7 @@ int connect_backend(struct opts *opts) { } -int loop(int bfd) { +static int loop(int bfd) { int efd = epoll_create(10); if (efd == -1) { perror("epoll_create"); @@ -106,9 +106,10 @@ int loop(int bfd) { } } - struct buf buf; - char buf_main[BUF_LEN], buf_temp[BUF_LEN]; - buf_init(&buf, buf_main, buf_temp); + struct buf buf = { + .start = 0, + .length = 0, + }; while (1) { #define MAX_EVENTS 10 @@ -126,15 +127,12 @@ int loop(int bfd) { return -1; } - struct buf tmp; struct packet packet; - buf_alias(&tmp, &buf); - while (airspy_adsb_parse(&tmp, &packet)) { - buf_alias(&buf, &tmp); + while (airspy_adsb_parse(&buf, &packet)) { } if (buf.length == BUF_LEN) { - fprintf(stderr, "Input buffer overrun\n"); + fprintf(stderr, "Input buffer overrun. This probably means that adsbus doesn't understand the protocol that this source is speaking.\n"); return -1; } } diff --git a/airspy_adsb.c b/airspy_adsb.c index e636dbb..ea64312 100644 --- a/airspy_adsb.c +++ b/airspy_adsb.c @@ -5,14 +5,21 @@ #include "airspy_adsb.h" bool airspy_adsb_parse(struct buf *buf, struct packet *packet) { - if (buf->length < 1 || *buf_at(buf, 0) != '*') { + if (buf->length < 35 || + buf_chr(buf, 0) != '*') { return false; } - if (buf->length >= 35 && *buf_at(buf, 34) == '\n') { + if (buf->length >= 35 && + buf_chr(buf, 34) == '\n' && + buf_chr(buf, 15) == ';') { + packet->type = MODE_S_SHORT; buf_consume(buf, 35); return true; } - if (buf->length >= 49 && *buf_at(buf, 48) == '\n') { + if (buf->length >= 49 && + buf_chr(buf, 48) == '\n' && + buf_chr(buf, 29) == ';') { + packet->type = MODE_S_LONG; buf_consume(buf, 49); return true; } diff --git a/common.c b/common.c index 83ec336..e0e25fd 100644 --- a/common.c +++ b/common.c @@ -7,17 +7,6 @@ #include "common.h" -void buf_init(struct buf *buf, char *main, char *tmp) { - buf->buf = main; - buf->tmp = tmp; - buf->start = 0; - buf->length = 0; -} - -void buf_alias(struct buf *to, struct buf *from) { - memcpy(to, from, sizeof(*to)); -} - ssize_t buf_fill(struct buf *buf, int fd) { if (buf->start + buf->length == BUF_LEN) { assert(buf->start > 0); diff --git a/common.h b/common.h index 417a40e..7e7a7af 100644 --- a/common.h +++ b/common.h @@ -2,14 +2,23 @@ #include +//////// buf + #define BUF_LEN 4096 struct buf { - char *buf; - char *tmp; + char buf[BUF_LEN]; size_t start; size_t length; }; +#define buf_chr(buff, at) ((buff)->buf[(buff)->start + (at)]) +#define buf_at(buff, at) (&buf_chr(buff, at)) + +ssize_t buf_fill(struct buf *, int); +void buf_consume(struct buf *, size_t); + + +//////// packet #define MLAT_HZ 60000000 #define DATA_MAX 14 @@ -25,10 +34,4 @@ struct packet { }; -#define buf_at(buff, at) (&(buff)->buf[(buff)->start + (at)]) - -void buf_init(struct buf *, char *, char *); -void buf_alias(struct buf *, struct buf *); - -ssize_t buf_fill(struct buf *, int); -void buf_consume(struct buf *, size_t); +//////// hex