diff --git a/adsbus.c b/adsbus.c index 288cde6..6f9582e 100644 --- a/adsbus.c +++ b/adsbus.c @@ -131,7 +131,6 @@ int loop(int bfd) { buf_alias(&tmp, &buf); while (airspy_adsb_parse(&tmp, &packet)) { buf_alias(&buf, &tmp); - fprintf(stderr, "packet!\n"); } if (buf.length == BUF_LEN) { diff --git a/airspy_adsb.c b/airspy_adsb.c index 20be191..36df2f5 100644 --- a/airspy_adsb.c +++ b/airspy_adsb.c @@ -1,20 +1,20 @@ +#include #include #include "common.h" #include "airspy_adsb.h" bool airspy_adsb_parse(struct buf *buf, struct packet *packet) { - if (buf->length < 35) { - // Minimum frame length + if (buf->length < 1 || *buf_at(buf, 0) != '*') { return false; } - if (buf->buf[buf->start] != '*') { - return false; + if (buf->length >= 35 && *buf_at(buf, 34) == '\n') { + buf_consume(buf, 35); + return true; } - char *last = memchr(&buf->buf[buf->start], '\n', buf->length); - if (!last) { - return false; + if (buf->length >= 49 && *buf_at(buf, 48) == '\n') { + buf_consume(buf, 49); + return true; } - buf_consume(buf, last - &buf->buf[buf->start] + 1); - return true; + return false; } diff --git a/common.c b/common.c index dd84a20..83ec336 100644 --- a/common.c +++ b/common.c @@ -21,13 +21,12 @@ void buf_alias(struct buf *to, struct buf *from) { ssize_t buf_fill(struct buf *buf, int fd) { if (buf->start + buf->length == BUF_LEN) { assert(buf->start > 0); - memmove(buf->buf, &buf->buf[buf->start], buf->length); + memmove(buf->buf, buf_at(buf, 0), buf->length); buf->start = 0; } size_t space = BUF_LEN - buf->length - buf->start; - size_t end = buf->start + buf->length; - ssize_t in = read(fd, &buf->buf[end], space); + ssize_t in = read(fd, buf_at(buf, buf->length), space); if (in < 0) { return in; } diff --git a/common.h b/common.h index d3d6dfb..417a40e 100644 --- a/common.h +++ b/common.h @@ -25,6 +25,8 @@ 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 *);