Split out buf

This commit is contained in:
Ian Gulliver
2016-02-22 16:41:34 -08:00
parent f36642b823
commit b5219d9c1a
14 changed files with 72 additions and 60 deletions

View File

@@ -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

View File

@@ -3,6 +3,7 @@
#include <string.h>
#include "common.h"
#include "buf.h"
#include "hex.h"
#include "receive.h"
#include "uuid.h"

View File

@@ -4,7 +4,9 @@
#include <stdint.h>
#include "common.h"
#include "buf.h"
#include "receive.h"
#include "beast.h"
struct __attribute__((packed)) beast_common_overlay {

37
adsbus/buf.c Normal file
View File

@@ -0,0 +1,37 @@
#include <assert.h>
#include <string.h>
#include <unistd.h>
#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;
}
}

21
adsbus/buf.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <stddef.h>
#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);

View File

@@ -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",

View File

@@ -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

View File

@@ -4,6 +4,7 @@
#include <jansson.h>
#include "hex.h"
#include "buf.h"
#include "rand.h"
#include "receive.h"
#include "send.h"

View File

@@ -1,5 +1,7 @@
#pragma once
struct buf;
void json_init();
void json_serialize(struct packet *, struct buf *);

View File

@@ -7,7 +7,7 @@
#include <unistd.h>
#include <string.h>
#include "common.h"
#include "buf.h"
#include "rand.h"

View File

@@ -3,6 +3,7 @@
#include <string.h>
#include "common.h"
#include "buf.h"
#include "hex.h"
#include "uuid.h"

View File

@@ -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"

View File

@@ -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"

View File

@@ -3,7 +3,9 @@
#include <jansson.h>
#include "common.h"
#include "buf.h"
#include "json.h"
#include "stats.h"
static struct stats_state {