Split out buf
This commit is contained in:
@@ -3,9 +3,9 @@ CFLAGS ?= -Wall -Werror -O4 -g --std=gnu11 --pedantic-errors -fPIE -pie -fstack-
|
|||||||
LDFLAGS ?= $(CFLAGS) -Wl,-z,relro -Wl,-z,now
|
LDFLAGS ?= $(CFLAGS) -Wl,-z,relro -Wl,-z,now
|
||||||
LIBS ?= -ljansson
|
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_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
|
all: adsbus
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "buf.h"
|
||||||
#include "hex.h"
|
#include "hex.h"
|
||||||
#include "receive.h"
|
#include "receive.h"
|
||||||
#include "uuid.h"
|
#include "uuid.h"
|
||||||
|
|||||||
@@ -4,7 +4,9 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "buf.h"
|
||||||
#include "receive.h"
|
#include "receive.h"
|
||||||
|
|
||||||
#include "beast.h"
|
#include "beast.h"
|
||||||
|
|
||||||
struct __attribute__((packed)) beast_common_overlay {
|
struct __attribute__((packed)) beast_common_overlay {
|
||||||
|
|||||||
37
adsbus/buf.c
Normal file
37
adsbus/buf.c
Normal 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
21
adsbus/buf.h
Normal 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);
|
||||||
@@ -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[] = {
|
char *packet_type_names[] = {
|
||||||
"Mode-S short",
|
"Mode-S short",
|
||||||
"Mode-S long",
|
"Mode-S long",
|
||||||
|
|||||||
@@ -19,27 +19,6 @@ void peer_epoll_del(struct peer *);
|
|||||||
void peer_loop();
|
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
|
//////// packet
|
||||||
|
|
||||||
#define DATA_LEN_MAX 14
|
#define DATA_LEN_MAX 14
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
|
|
||||||
#include "hex.h"
|
#include "hex.h"
|
||||||
|
#include "buf.h"
|
||||||
#include "rand.h"
|
#include "rand.h"
|
||||||
#include "receive.h"
|
#include "receive.h"
|
||||||
#include "send.h"
|
#include "send.h"
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
struct buf;
|
||||||
|
|
||||||
void json_init();
|
void json_init();
|
||||||
void json_serialize(struct packet *, struct buf *);
|
void json_serialize(struct packet *, struct buf *);
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "buf.h"
|
||||||
|
|
||||||
#include "rand.h"
|
#include "rand.h"
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "buf.h"
|
||||||
#include "hex.h"
|
#include "hex.h"
|
||||||
#include "uuid.h"
|
#include "uuid.h"
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,9 @@
|
|||||||
|
|
||||||
#include "airspy_adsb.h"
|
#include "airspy_adsb.h"
|
||||||
#include "beast.h"
|
#include "beast.h"
|
||||||
|
#include "buf.h"
|
||||||
#include "raw.h"
|
#include "raw.h"
|
||||||
|
|
||||||
#include "send.h"
|
#include "send.h"
|
||||||
|
|
||||||
#include "uuid.h"
|
#include "uuid.h"
|
||||||
|
|
||||||
#include "receive.h"
|
#include "receive.h"
|
||||||
|
|||||||
@@ -10,10 +10,10 @@
|
|||||||
|
|
||||||
#include "airspy_adsb.h"
|
#include "airspy_adsb.h"
|
||||||
#include "beast.h"
|
#include "beast.h"
|
||||||
|
#include "buf.h"
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
#include "raw.h"
|
#include "raw.h"
|
||||||
#include "stats.h"
|
#include "stats.h"
|
||||||
|
|
||||||
#include "uuid.h"
|
#include "uuid.h"
|
||||||
|
|
||||||
#include "send.h"
|
#include "send.h"
|
||||||
|
|||||||
@@ -3,7 +3,9 @@
|
|||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "buf.h"
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
|
|
||||||
#include "stats.h"
|
#include "stats.h"
|
||||||
|
|
||||||
static struct stats_state {
|
static struct stats_state {
|
||||||
|
|||||||
Reference in New Issue
Block a user