Clean up buf silliness.

This commit is contained in:
Ian Gulliver
2016-02-14 20:35:43 +00:00
parent d660117f7a
commit 93558c0a23
4 changed files with 31 additions and 34 deletions

View File

@@ -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; int opt;
while ((opt = getopt(argc, argv, "h:p:")) != -1) { while ((opt = getopt(argc, argv, "h:p:")) != -1) {
switch (opt) { 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 = { struct addrinfo hints = {
.ai_family = AF_UNSPEC, .ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM, .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); int efd = epoll_create(10);
if (efd == -1) { if (efd == -1) {
perror("epoll_create"); perror("epoll_create");
@@ -106,9 +106,10 @@ int loop(int bfd) {
} }
} }
struct buf buf; struct buf buf = {
char buf_main[BUF_LEN], buf_temp[BUF_LEN]; .start = 0,
buf_init(&buf, buf_main, buf_temp); .length = 0,
};
while (1) { while (1) {
#define MAX_EVENTS 10 #define MAX_EVENTS 10
@@ -126,15 +127,12 @@ int loop(int bfd) {
return -1; return -1;
} }
struct buf tmp;
struct packet packet; struct packet packet;
buf_alias(&tmp, &buf); while (airspy_adsb_parse(&buf, &packet)) {
while (airspy_adsb_parse(&tmp, &packet)) {
buf_alias(&buf, &tmp);
} }
if (buf.length == BUF_LEN) { 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; return -1;
} }
} }

View File

@@ -5,14 +5,21 @@
#include "airspy_adsb.h" #include "airspy_adsb.h"
bool airspy_adsb_parse(struct buf *buf, struct packet *packet) { 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; 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); buf_consume(buf, 35);
return true; 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); buf_consume(buf, 49);
return true; return true;
} }

View File

@@ -7,17 +7,6 @@
#include "common.h" #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) { ssize_t buf_fill(struct buf *buf, int fd) {
if (buf->start + buf->length == BUF_LEN) { if (buf->start + buf->length == BUF_LEN) {
assert(buf->start > 0); assert(buf->start > 0);

View File

@@ -2,14 +2,23 @@
#include <unistd.h> #include <unistd.h>
//////// buf
#define BUF_LEN 4096 #define BUF_LEN 4096
struct buf { struct buf {
char *buf; char buf[BUF_LEN];
char *tmp;
size_t start; size_t start;
size_t length; 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 MLAT_HZ 60000000
#define DATA_MAX 14 #define DATA_MAX 14
@@ -25,10 +34,4 @@ struct packet {
}; };
#define buf_at(buff, at) (&(buff)->buf[(buff)->start + (at)]) //////// hex
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);