diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index 731ae5c..7357a60 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -115,6 +115,7 @@ int main(int argc, char *argv[]) { beast_init(); json_init(); + proto_init(); stats_init(); if (!parse_opts(argc, argv)) { diff --git a/adsbus/airspy_adsb.c b/adsbus/airspy_adsb.c index e8c35e6..c177905 100644 --- a/adsbus/airspy_adsb.c +++ b/adsbus/airspy_adsb.c @@ -151,10 +151,6 @@ bool airspy_adsb_parse(struct buf *buf, struct packet *packet, void *state_in) { } void airspy_adsb_serialize(struct packet *packet, struct buf *buf) { - if (!packet) { - return; - } - switch (packet->type) { case PACKET_TYPE_NONE: break; diff --git a/adsbus/beast.c b/adsbus/beast.c index 83cd8ef..ab409b8 100644 --- a/adsbus/beast.c +++ b/adsbus/beast.c @@ -180,10 +180,6 @@ bool beast_parse(struct buf *buf, struct packet *packet, void *state_in) { } void beast_serialize(struct packet *packet, struct buf *buf) { - if (!packet) { - return; - } - switch (packet->type) { case PACKET_TYPE_NONE: break; diff --git a/adsbus/json.c b/adsbus/json.c index 7c2d1f7..e5c2e4d 100644 --- a/adsbus/json.c +++ b/adsbus/json.c @@ -25,6 +25,7 @@ struct json_parser_state { }; static json_t *json_prev = NULL; +static struct buf json_hello_buf = BUF_INIT; static void json_serialize_to_buf(json_t *obj, struct buf *buf) { assert(json_dump_callback(obj, json_buf_append_callback, buf, 0) == 0); @@ -32,19 +33,6 @@ static void json_serialize_to_buf(json_t *obj, struct buf *buf) { buf_chr(buf, buf->length++) = '\n'; } -static void json_hello(struct buf *buf) { - json_t *hello = json_pack( - "{s:s, s:s, s:s, s:s, s:I, s:I, s:I}", - "type", "header", - "magic", JSON_MAGIC, - "server_version", server_version, - "server_id", server_id, - "mlat_timestamp_mhz", (json_int_t) PACKET_MLAT_MHZ, - "mlat_timestamp_max", (json_int_t) PACKET_MLAT_MAX, - "rssi_max", (json_int_t) PACKET_RSSI_MAX); - json_serialize_to_buf(hello, buf); -} - static void json_add_common(struct packet *packet, json_t *obj) { json_object_set_new(obj, "type", json_string(packet_type_names[packet->type])); json_object_set_new(obj, "source_id", json_string((const char *) packet->source_id)); @@ -192,6 +180,17 @@ void json_init() { size_t seed; rand_fill(&seed, sizeof(seed)); json_object_seed(seed); + + json_t *hello = json_pack( + "{s:s, s:s, s:s, s:s, s:I, s:I, s:I}", + "type", "header", + "magic", JSON_MAGIC, + "server_version", server_version, + "server_id", server_id, + "mlat_timestamp_mhz", (json_int_t) PACKET_MLAT_MHZ, + "mlat_timestamp_max", (json_int_t) PACKET_MLAT_MAX, + "rssi_max", (json_int_t) PACKET_RSSI_MAX); + json_serialize_to_buf(hello, &json_hello_buf); } void json_cleanup() { @@ -248,11 +247,6 @@ bool json_parse(struct buf *buf, struct packet *packet, void *state_in) { } void json_serialize(struct packet *packet, struct buf *buf) { - if (!packet) { - json_hello(buf); - return; - } - switch (packet->type) { case PACKET_TYPE_NONE: break; @@ -267,6 +261,10 @@ void json_serialize(struct packet *packet, struct buf *buf) { } } +void json_hello(struct buf **buf) { + *buf = &json_hello_buf; +} + int json_buf_append_callback(const char *buffer, size_t size, void *data) { struct buf *buf = data; if (buf->length + size + 1 > BUF_LEN_MAX) { diff --git a/adsbus/json.h b/adsbus/json.h index c5f2911..f0cad8a 100644 --- a/adsbus/json.h +++ b/adsbus/json.h @@ -9,5 +9,6 @@ void json_init(void); void json_cleanup(void); bool __attribute__ ((warn_unused_result)) json_parse(struct buf *, struct packet *, void *); void json_serialize(struct packet *, struct buf *); +void json_hello(struct buf **); int json_buf_append_callback(const char *, size_t, void *); diff --git a/adsbus/proto.c b/adsbus/proto.c index 5592154..d862a76 100644 --- a/adsbus/proto.c +++ b/adsbus/proto.c @@ -29,6 +29,7 @@ struct proto_parser_state { }; static Adsb *proto_prev = NULL; +static struct buf proto_hello_buf = BUF_INIT; static void proto_obj_to_buf(Adsb *wrapper, struct buf *buf) { assert(!buf->length); @@ -126,6 +127,21 @@ static bool proto_parse_packet(AdsbPacket *in, struct packet *packet, struct pro return true; } +void proto_init() { + AdsbHeader header = ADSB_HEADER__INIT; + header.magic = PROTO_MAGIC; + header.server_version = server_version; + header.server_id = (char *) server_id; + header.mlat_timestamp_mhz = PACKET_MLAT_MHZ; + header.mlat_timestamp_max = PACKET_MLAT_MAX; + header.rssi_max = PACKET_RSSI_MAX; + + Adsb wrapper = ADSB__INIT; + wrapper.header = &header; + + proto_obj_to_buf(&wrapper, &proto_hello_buf); +} + void proto_cleanup() { if (proto_prev) { adsb__free_unpacked(proto_prev, NULL); @@ -184,22 +200,6 @@ bool proto_parse(struct buf *buf, struct packet *packet, void *state_in) { } void proto_serialize(struct packet *packet, struct buf *buf) { - if (!packet) { - AdsbHeader header = ADSB_HEADER__INIT; - header.magic = PROTO_MAGIC; - header.server_version = server_version; - header.server_id = (char *) server_id; - header.mlat_timestamp_mhz = PACKET_MLAT_MHZ; - header.mlat_timestamp_max = PACKET_MLAT_MAX; - header.rssi_max = PACKET_RSSI_MAX; - - Adsb wrapper = ADSB__INIT; - wrapper.header = &header; - - proto_obj_to_buf(&wrapper, buf); - return; - } - switch (packet->type) { case PACKET_TYPE_NONE: break; @@ -213,3 +213,7 @@ void proto_serialize(struct packet *packet, struct buf *buf) { break; } } + +void proto_hello(struct buf **buf) { + *buf = &proto_hello_buf; +} diff --git a/adsbus/proto.h b/adsbus/proto.h index 21c377a..85a297f 100644 --- a/adsbus/proto.h +++ b/adsbus/proto.h @@ -5,6 +5,8 @@ struct buf; struct packet; +void proto_init(void); void proto_cleanup(void); bool __attribute__ ((warn_unused_result)) proto_parse(struct buf *, struct packet *, void *); void proto_serialize(struct packet *, struct buf *); +void proto_hello(struct buf **); diff --git a/adsbus/raw.c b/adsbus/raw.c index e6ab7d1..41e4309 100644 --- a/adsbus/raw.c +++ b/adsbus/raw.c @@ -87,10 +87,6 @@ bool raw_parse(struct buf *buf, struct packet *packet, void __attribute__((unuse } void raw_serialize(struct packet *packet, struct buf *buf) { - if (!packet) { - return; - } - switch (packet->type) { case PACKET_TYPE_NONE: break; diff --git a/adsbus/send.c b/adsbus/send.c index 3f87f87..42b2255 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -32,34 +32,42 @@ struct send { }; typedef void (*serialize)(struct packet *, struct buf *); +typedef void (*hello)(struct buf **); static struct serializer { char *name; serialize serialize; + hello hello; struct list_head send_head; } serializers[] = { { .name = "airspy_adsb", .serialize = airspy_adsb_serialize, + .hello = NULL, }, { .name = "beast", .serialize = beast_serialize, + .hello = NULL, }, { .name = "json", .serialize = json_serialize, + .hello = json_hello, }, { .name = "proto", .serialize = proto_serialize, + .hello = proto_hello, }, { .name = "raw", .serialize = raw_serialize, + .hello = NULL, }, { .name = "stats", .serialize = stats_serialize, + .hello = NULL, }, }; #define NUM_SERIALIZERS (sizeof(serializers) / sizeof(*serializers)) @@ -130,8 +138,7 @@ void send_new_wrapper(int fd, void *passthrough, struct peer *on_close) { void send_hello(struct buf **buf_pp, void *passthrough) { struct serializer *serializer = (struct serializer *) passthrough; - // TODO: change API to avoid special-case NULL packet*, and to allow static greetings. - serializer->serialize(NULL, *buf_pp); + serializer->hello(buf_pp); } void send_write(struct packet *packet) {