diff --git a/Makefile b/Makefile index ef040ea..7209566 100644 --- a/Makefile +++ b/Makefile @@ -11,5 +11,5 @@ clean: %.o: %.c *.h $(CC) -c $(CFLAGS) $< -o $@ -adsbus: adsbus.o backend.o client.o airspy_adsb.o beast.o json.o common.o - $(CC) $(LDFLAGS) -o adsbus adsbus.o backend.o client.o airspy_adsb.o beast.o json.o common.o $(LIBS) +adsbus: adsbus.o backend.o client.o airspy_adsb.o beast.o json.o stats.o common.o + $(CC) $(LDFLAGS) -o adsbus adsbus.o backend.o client.o airspy_adsb.o beast.o json.o stats.o common.o $(LIBS) diff --git a/adsbus.c b/adsbus.c index 38d23ad..1d8833d 100644 --- a/adsbus.c +++ b/adsbus.c @@ -11,6 +11,7 @@ #include "airspy_adsb.h" #include "beast.h" #include "json.h" +#include "stats.h" static bool add_dump(char *format) { @@ -108,6 +109,7 @@ int main(int argc, char *argv[]) { airspy_adsb_init(); beast_init(); json_init(); + stats_init(); int epoll_fd = epoll_create1(0); if (epoll_fd == -1) { diff --git a/client.c b/client.c index bdd9d8f..96c7228 100644 --- a/client.c +++ b/client.c @@ -7,6 +7,7 @@ #include #include "json.h" +#include "stats.h" #include "client.h" struct client { @@ -24,8 +25,11 @@ struct serializer { { .name = "json", .serialize = json_serialize, - .client_head = NULL, }, + { + .name = "stats", + .serialize = stats_serialize, + } }; #define NUM_SERIALIZERS (sizeof(serializers) / sizeof(*serializers)) diff --git a/common.c b/common.c index 7453d8a..e014fbe 100644 --- a/common.c +++ b/common.c @@ -57,6 +57,12 @@ void buf_consume(struct buf *buf, size_t length) { } +char *packet_type_names[] = { + "Mode-S short", + "Mode-S long", +}; + + uint64_t mlat_timestamp_scale_mhz_in(uint64_t timestamp, uint32_t mhz) { return timestamp * (MLAT_MHZ / mhz); } diff --git a/common.h b/common.h index 606cb60..dd19bd7 100644 --- a/common.h +++ b/common.h @@ -44,15 +44,16 @@ void buf_consume(struct buf *, size_t); struct backend; struct packet { enum { - MODE_AC, MODE_S_SHORT, MODE_S_LONG, + NUM_TYPES, } type; uint8_t payload[DATA_LEN_MAX]; uint64_t mlat_timestamp; uint32_t rssi; struct backend *backend; }; +extern char *packet_type_names[]; //////// mlat diff --git a/json.c b/json.c index d867e41..d7f8721 100644 --- a/json.c +++ b/json.c @@ -14,7 +14,7 @@ void json_init() { assert(JSON_INTEGER_IS_LONG_LONG); } -static int json_buf_append_callback(const char *buffer, size_t size, void *data) { +int json_buf_append_callback(const char *buffer, size_t size, void *data) { struct buf *buf = data; if (size > BUF_LEN_MAX - buf->length - 1) { return -1; @@ -70,9 +70,6 @@ void json_serialize(struct packet *packet, struct buf *buf) { } switch (packet->type) { - case MODE_AC: - break; - case MODE_S_SHORT: json_serialize_mode_s_short(packet, buf); break; @@ -80,5 +77,8 @@ void json_serialize(struct packet *packet, struct buf *buf) { case MODE_S_LONG: json_serialize_mode_s_long(packet, buf); break; + + case NUM_TYPES: + break; } } diff --git a/json.h b/json.h index d94e8f0..433af4c 100644 --- a/json.h +++ b/json.h @@ -5,3 +5,5 @@ void json_init(); void json_serialize(struct packet *, struct buf *); + +int json_buf_append_callback(const char *, size_t, void *); diff --git a/stats.c b/stats.c new file mode 100644 index 0000000..eae1120 --- /dev/null +++ b/stats.c @@ -0,0 +1,43 @@ +#include +#include +#include + +#include "common.h" +#include "backend.h" +#include "json.h" +#include "stats.h" + + +static struct stats_state { + uint64_t total_count; + uint64_t type_count[NUM_TYPES]; + struct timespec start; +} stats_state = { 0 }; + + +void stats_init() { + assert(clock_gettime(CLOCK_MONOTONIC, &stats_state.start) == 0); +} + + +void stats_serialize(struct packet *packet, struct buf *buf) { + if (packet) { + stats_state.total_count++; + stats_state.type_count[packet->type]++; + } + if (stats_state.total_count % 1000 != 0) { + return; + } + json_t *counts = json_object(); + for (int i = 0; i < NUM_TYPES; i++) { + json_object_set_new(counts, packet_type_names[i], json_integer(stats_state.type_count[i])); + } + struct timespec now; + assert(clock_gettime(CLOCK_MONOTONIC, &now) == 0); + json_t *out = json_pack("{sIso}", + "uptime_seconds", (json_int_t) (now.tv_sec - stats_state.start.tv_sec), + "packet_counts", counts); + assert(json_dump_callback(out, json_buf_append_callback, buf, 0) == 0); + json_decref(out); + buf_chr(buf, buf->length++) = '\n'; +} diff --git a/stats.h b/stats.h new file mode 100644 index 0000000..4903105 --- /dev/null +++ b/stats.h @@ -0,0 +1,7 @@ +#pragma once + +#include "common.h" + + +void stats_init(); +void stats_serialize(struct packet *, struct buf *);