From 2ce2ed5bf597d04c08902d374a4e61a2a41627d5 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 22 Feb 2016 16:58:13 -0800 Subject: [PATCH] Split out packet. Delete common. --- adsbus/Makefile | 4 +-- adsbus/adsbus.c | 1 - adsbus/airspy_adsb.c | 12 ++++----- adsbus/beast.c | 20 +++++++-------- adsbus/common.c | 61 -------------------------------------------- adsbus/common.h | 41 ----------------------------- adsbus/incoming.c | 1 - adsbus/json.c | 11 ++++---- adsbus/json.h | 1 + adsbus/outgoing.c | 1 - adsbus/packet.c | 44 ++++++++++++++++++++++++++++++++ adsbus/packet.h | 33 ++++++++++++++++++++++++ adsbus/raw.c | 2 +- adsbus/receive.c | 1 + adsbus/receive.h | 2 -- adsbus/send.c | 2 -- adsbus/send.h | 2 +- adsbus/stats.c | 2 +- adsbus/uuid.c | 1 - adsbus/wakeup.c | 1 - 20 files changed, 106 insertions(+), 137 deletions(-) delete mode 100644 adsbus/common.c delete mode 100644 adsbus/common.h create mode 100644 adsbus/packet.c create mode 100644 adsbus/packet.h diff --git a/adsbus/Makefile b/adsbus/Makefile index d6d1c7f..42f41b2 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -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 = incoming.o outgoing.o receive.o send.o peer.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 = buf.o hex.o opts.o rand.o uuid.o wakeup.o common.o +OBJ_UTIL = buf.o hex.o opts.o packet.o peer.o rand.o uuid.o wakeup.o all: adsbus diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index 209d769..b4e7fe1 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -6,7 +6,6 @@ #include #include "beast.h" -#include "common.h" #include "hex.h" #include "incoming.h" #include "json.h" diff --git a/adsbus/airspy_adsb.c b/adsbus/airspy_adsb.c index 31e2365..2652c87 100644 --- a/adsbus/airspy_adsb.c +++ b/adsbus/airspy_adsb.c @@ -2,9 +2,9 @@ #include #include -#include "common.h" #include "buf.h" #include "hex.h" +#include "packet.h" #include "receive.h" #include "uuid.h" @@ -40,7 +40,7 @@ struct __attribute__((packed)) airspy_adsb_mode_s_long_overlay { }; struct airspy_adsb_parser_state { - struct mlat_state mlat_state; + struct packet_mlat_state mlat_state; }; static bool airspy_adsb_parse_common(const struct airspy_adsb_common_overlay *overlay, struct packet *packet, struct airspy_adsb_parser_state *state) { @@ -50,8 +50,8 @@ static bool airspy_adsb_parse_common(const struct airspy_adsb_common_overlay *ov return false; } uint16_t mlat_mhz = 2 * hex_to_int(overlay->mlat_precision, sizeof(overlay->mlat_precision) / 2); - packet->mlat_timestamp = mlat_timestamp_scale_in(hex_to_int(overlay->mlat_timestamp, sizeof(overlay->mlat_timestamp) / 2), UINT32_MAX, mlat_mhz, &state->mlat_state); - packet->rssi = rssi_scale_in(hex_to_int(overlay->rssi, sizeof(overlay->rssi) / 2), UINT16_MAX); + packet->mlat_timestamp = packet_mlat_timestamp_scale_in(hex_to_int(overlay->mlat_timestamp, sizeof(overlay->mlat_timestamp) / 2), UINT32_MAX, mlat_mhz, &state->mlat_state); + packet->rssi = packet_rssi_scale_in(hex_to_int(overlay->rssi, sizeof(overlay->rssi) / 2), UINT16_MAX); return true; } @@ -95,10 +95,10 @@ static void airspy_adsb_fill_common(struct packet *packet, struct airspy_adsb_co overlay->semicolon1 = overlay->semicolon2 = overlay->semicolon3 = ';'; hex_from_int_upper( overlay->mlat_timestamp, - mlat_timestamp_scale_out(packet->mlat_timestamp, UINT32_MAX, SEND_MHZ), + packet_mlat_timestamp_scale_out(packet->mlat_timestamp, UINT32_MAX, SEND_MHZ), sizeof(overlay->mlat_timestamp) / 2); hex_from_int_upper(overlay->mlat_precision, SEND_MHZ / 2, sizeof(overlay->mlat_precision) / 2); - hex_from_int_upper(overlay->rssi, rssi_scale_out(packet->rssi, UINT16_MAX), sizeof(overlay->rssi) / 2); + hex_from_int_upper(overlay->rssi, packet_rssi_scale_out(packet->rssi, UINT16_MAX), sizeof(overlay->rssi) / 2); } static void airspy_adsb_serialize_mode_s_short(struct packet *packet, struct buf *buf) { diff --git a/adsbus/beast.c b/adsbus/beast.c index 6f97c90..f057099 100644 --- a/adsbus/beast.c +++ b/adsbus/beast.c @@ -3,8 +3,8 @@ #include #include -#include "common.h" #include "buf.h" +#include "packet.h" #include "receive.h" #include "beast.h" @@ -29,7 +29,7 @@ struct __attribute__((packed)) beast_mode_s_long_overlay { }; struct beast_parser_state { - struct mlat_state mlat_state; + struct packet_mlat_state mlat_state; }; static uint64_t beast_parse_mlat(uint8_t *mlat_timestamp) { @@ -87,8 +87,8 @@ static bool beast_parse_mode_s_short(struct buf *buf, struct packet *packet, str struct beast_mode_s_short_overlay *overlay = (struct beast_mode_s_short_overlay *) buf_at(&buf2, 0); packet->type = MODE_S_SHORT; uint64_t source_mlat = beast_parse_mlat(overlay->mlat_timestamp); - packet->mlat_timestamp = mlat_timestamp_scale_in(source_mlat, UINT64_C(0xffffffffffff), 12, &state->mlat_state); - packet->rssi = rssi_scale_in(overlay->rssi, UINT8_MAX); + packet->mlat_timestamp = packet_mlat_timestamp_scale_in(source_mlat, UINT64_C(0xffffffffffff), 12, &state->mlat_state); + packet->rssi = packet_rssi_scale_in(overlay->rssi, UINT8_MAX); memcpy(packet->payload, overlay->payload, sizeof(overlay->payload)); buf_consume(buf, in_bytes); return true; @@ -103,8 +103,8 @@ static bool beast_parse_mode_s_long(struct buf *buf, struct packet *packet, stru struct beast_mode_s_long_overlay *overlay = (struct beast_mode_s_long_overlay *) buf_at(&buf2, 0); packet->type = MODE_S_LONG; uint64_t source_mlat = beast_parse_mlat(overlay->mlat_timestamp); - packet->mlat_timestamp = mlat_timestamp_scale_in(source_mlat, UINT64_C(0xffffffffffff), 12, &state->mlat_state); - packet->rssi = rssi_scale_in(overlay->rssi == UINT8_MAX ? 0 : overlay->rssi, UINT8_MAX); + packet->mlat_timestamp = packet_mlat_timestamp_scale_in(source_mlat, UINT64_C(0xffffffffffff), 12, &state->mlat_state); + packet->rssi = packet_rssi_scale_in(overlay->rssi == UINT8_MAX ? 0 : overlay->rssi, UINT8_MAX); memcpy(packet->payload, overlay->payload, sizeof(overlay->payload)); buf_consume(buf, in_bytes); return true; @@ -146,11 +146,11 @@ void beast_serialize_mode_s_short(struct packet *packet, struct buf *buf) { overlay->common.type = 0x32; memcpy(overlay->payload, packet->payload, sizeof(overlay->payload)); beast_write_mlat( - mlat_timestamp_scale_out(packet->mlat_timestamp, UINT64_C(0xffffffffffff), 12), + packet_mlat_timestamp_scale_out(packet->mlat_timestamp, UINT64_C(0xffffffffffff), 12), overlay->mlat_timestamp); if (packet->rssi) { - overlay->rssi = rssi_scale_out(packet->rssi, UINT8_MAX); + overlay->rssi = packet_rssi_scale_out(packet->rssi, UINT8_MAX); } else { overlay->rssi = UINT8_MAX; } @@ -166,11 +166,11 @@ void beast_serialize_mode_s_long(struct packet *packet, struct buf *buf) { overlay->common.type = 0x33; memcpy(overlay->payload, packet->payload, sizeof(overlay->payload)); beast_write_mlat( - mlat_timestamp_scale_out(packet->mlat_timestamp, UINT64_C(0xffffffffffff), 12), + packet_mlat_timestamp_scale_out(packet->mlat_timestamp, UINT64_C(0xffffffffffff), 12), overlay->mlat_timestamp); if (packet->rssi) { - overlay->rssi = rssi_scale_out(packet->rssi, UINT8_MAX); + overlay->rssi = packet_rssi_scale_out(packet->rssi, UINT8_MAX); } else { overlay->rssi = UINT8_MAX; } diff --git a/adsbus/common.c b/adsbus/common.c deleted file mode 100644 index c6809f6..0000000 --- a/adsbus/common.c +++ /dev/null @@ -1,61 +0,0 @@ -#define _GNU_SOURCE - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "uuid.h" -#include "wakeup.h" - - -char *packet_type_names[] = { - "Mode-S short", - "Mode-S long", -}; - - -static uint64_t mlat_timestamp_scale_mhz_in(uint64_t timestamp, uint32_t mhz) { - return timestamp * (MLAT_MHZ / mhz); -} - -static uint64_t mlat_timestamp_scale_width_in(uint64_t timestamp, uint64_t max, struct mlat_state *state) { - if (timestamp < state->timestamp_last) { - // Counter reset - state->timestamp_generation += max; - } - - state->timestamp_last = timestamp; - return state->timestamp_generation + timestamp; -} - -uint64_t mlat_timestamp_scale_in(uint64_t timestamp, uint64_t max, uint16_t mhz, struct mlat_state *state) { - return mlat_timestamp_scale_mhz_in(mlat_timestamp_scale_width_in(timestamp, max, state), mhz); -} - -static uint64_t mlat_timestamp_scale_mhz_out(uint64_t timestamp, uint64_t mhz) { - return timestamp / (MLAT_MHZ / mhz); -} - -static uint64_t mlat_timestamp_scale_width_out(uint64_t timestamp, uint64_t max) { - return timestamp % max; -} - -uint64_t mlat_timestamp_scale_out(uint64_t timestamp, uint64_t max, uint16_t mhz) { - return mlat_timestamp_scale_width_out(mlat_timestamp_scale_mhz_out(timestamp, mhz), max); -} - - -uint32_t rssi_scale_in(uint32_t value, uint32_t max) { - return value * (RSSI_MAX / max); -} - -uint32_t rssi_scale_out(uint32_t value, uint32_t max) { - return value / (RSSI_MAX / max); -} diff --git a/adsbus/common.h b/adsbus/common.h deleted file mode 100644 index d5c586a..0000000 --- a/adsbus/common.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include - - -//////// packet - -#define DATA_LEN_MAX 14 -struct packet { - enum { - MODE_S_SHORT, - MODE_S_LONG, - } type; - #define NUM_TYPES 2 - uint8_t payload[DATA_LEN_MAX]; - uint64_t mlat_timestamp; - uint32_t rssi; -}; -extern char *packet_type_names[]; - - -//////// mlat - -#define MLAT_MHZ 120 -// Use the signed max to avoid problems with some consumers; it's large enough to not matter. -#define MLAT_MAX INT64_MAX -#define RSSI_MAX UINT32_MAX - -struct mlat_state { - uint64_t timestamp_last; - uint64_t timestamp_generation; -}; - -uint64_t mlat_timestamp_scale_in(uint64_t, uint64_t, uint16_t, struct mlat_state *); -uint64_t mlat_timestamp_scale_out(uint64_t, uint64_t, uint16_t); - - -//////// rssi - -uint32_t rssi_scale_in(uint32_t, uint32_t); -uint32_t rssi_scale_out(uint32_t, uint32_t); diff --git a/adsbus/incoming.c b/adsbus/incoming.c index a48ec80..9797373 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -10,7 +10,6 @@ #include #include -#include "common.h" #include "peer.h" #include "wakeup.h" #include "uuid.h" diff --git a/adsbus/json.c b/adsbus/json.c index 3aa5160..7b6317b 100644 --- a/adsbus/json.c +++ b/adsbus/json.c @@ -5,6 +5,7 @@ #include "hex.h" #include "buf.h" +#include "packet.h" #include "rand.h" #include "receive.h" #include "send.h" @@ -20,9 +21,9 @@ static void json_serialize_to_buf(json_t *obj, struct buf *buf) { static void json_hello(struct buf *buf) { json_t *hello = json_pack("{sIsIsI}", - "mlat_timestamp_mhz", (json_int_t) MLAT_MHZ, - "mlat_timestamp_max", (json_int_t) MLAT_MAX, - "rssi_max", (json_int_t) RSSI_MAX); + "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); } @@ -37,7 +38,7 @@ static void json_add_common(struct packet *packet, json_t *obj) { } static void json_serialize_mode_s_short(struct packet *packet, struct buf *buf) { - assert(packet->mlat_timestamp < MLAT_MAX); + assert(packet->mlat_timestamp < PACKET_MLAT_MAX); char hexbuf[14]; hex_from_bin_upper(hexbuf, packet->payload, 7); json_t *out = json_pack("{ss#}", "payload", hexbuf, 14); @@ -46,7 +47,7 @@ static void json_serialize_mode_s_short(struct packet *packet, struct buf *buf) } static void json_serialize_mode_s_long(struct packet *packet, struct buf *buf) { - assert(packet->mlat_timestamp < MLAT_MAX); + assert(packet->mlat_timestamp < PACKET_MLAT_MAX); char hexbuf[28]; hex_from_bin_upper(hexbuf, packet->payload, 14); json_t *out = json_pack("{ss#}", "payload", hexbuf, 28); diff --git a/adsbus/json.h b/adsbus/json.h index b15600a..ecd4ed2 100644 --- a/adsbus/json.h +++ b/adsbus/json.h @@ -1,6 +1,7 @@ #pragma once struct buf; +struct packet; void json_init(); void json_serialize(struct packet *, struct buf *); diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 056be89..57d6de6 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -8,7 +8,6 @@ #include #include -#include "common.h" #include "peer.h" #include "wakeup.h" #include "uuid.h" diff --git a/adsbus/packet.c b/adsbus/packet.c new file mode 100644 index 0000000..7127275 --- /dev/null +++ b/adsbus/packet.c @@ -0,0 +1,44 @@ +#include "packet.h" + +char *packet_type_names[] = { + "Mode-S short", + "Mode-S long", +}; + +static uint64_t packet_mlat_timestamp_scale_mhz_in(uint64_t timestamp, uint32_t mhz) { + return timestamp * (PACKET_MLAT_MHZ / mhz); +} + +static uint64_t packet_mlat_timestamp_scale_width_in(uint64_t timestamp, uint64_t max, struct packet_mlat_state *state) { + if (timestamp < state->timestamp_last) { + // Counter reset + state->timestamp_generation += max; + } + + state->timestamp_last = timestamp; + return state->timestamp_generation + timestamp; +} + +static uint64_t packet_mlat_timestamp_scale_mhz_out(uint64_t timestamp, uint64_t mhz) { + return timestamp / (PACKET_MLAT_MHZ / mhz); +} + +static uint64_t packet_mlat_timestamp_scale_width_out(uint64_t timestamp, uint64_t max) { + return timestamp % max; +} + +uint64_t packet_mlat_timestamp_scale_in(uint64_t timestamp, uint64_t max, uint16_t mhz, struct packet_mlat_state *state) { + return packet_mlat_timestamp_scale_mhz_in(packet_mlat_timestamp_scale_width_in(timestamp, max, state), mhz); +} + +uint64_t packet_mlat_timestamp_scale_out(uint64_t timestamp, uint64_t max, uint16_t mhz) { + return packet_mlat_timestamp_scale_width_out(packet_mlat_timestamp_scale_mhz_out(timestamp, mhz), max); +} + +uint32_t packet_rssi_scale_in(uint32_t value, uint32_t max) { + return value * (PACKET_RSSI_MAX / max); +} + +uint32_t packet_rssi_scale_out(uint32_t value, uint32_t max) { + return value / (PACKET_RSSI_MAX / max); +} diff --git a/adsbus/packet.h b/adsbus/packet.h new file mode 100644 index 0000000..01d7b63 --- /dev/null +++ b/adsbus/packet.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +#define PACKET_DATA_LEN_MAX 14 +struct packet { + enum { + MODE_S_SHORT, + MODE_S_LONG, + } type; + #define NUM_TYPES 2 + uint8_t payload[PACKET_DATA_LEN_MAX]; + uint64_t mlat_timestamp; + uint32_t rssi; +}; +extern char *packet_type_names[]; + +#define PACKET_MLAT_MHZ 120 +// Use the signed max to avoid problems with some consumers; it's large enough to not matter. +#define PACKET_MLAT_MAX INT64_MAX +#define PACKET_RSSI_MAX UINT32_MAX + +struct packet_mlat_state { + uint64_t timestamp_last; + uint64_t timestamp_generation; +}; + +uint64_t packet_mlat_timestamp_scale_in(uint64_t, uint64_t, uint16_t, struct packet_mlat_state *); +uint64_t packet_mlat_timestamp_scale_out(uint64_t, uint64_t, uint16_t); + +uint32_t packet_rssi_scale_in(uint32_t, uint32_t); +uint32_t packet_rssi_scale_out(uint32_t, uint32_t); + diff --git a/adsbus/raw.c b/adsbus/raw.c index ed4c036..679f16a 100644 --- a/adsbus/raw.c +++ b/adsbus/raw.c @@ -2,9 +2,9 @@ #include #include -#include "common.h" #include "buf.h" #include "hex.h" +#include "packet.h" #include "uuid.h" #include "raw.h" diff --git a/adsbus/receive.c b/adsbus/receive.c index 543a24c..b66e147 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -7,6 +7,7 @@ #include "airspy_adsb.h" #include "beast.h" #include "buf.h" +#include "packet.h" #include "peer.h" #include "raw.h" #include "send.h" diff --git a/adsbus/receive.h b/adsbus/receive.h index 4b6d264..8b6d492 100644 --- a/adsbus/receive.h +++ b/adsbus/receive.h @@ -2,8 +2,6 @@ #include -#include "common.h" - #define PARSER_STATE_LEN 256 void receive_new(int, void *); diff --git a/adsbus/send.c b/adsbus/send.c index 4f5efac..9081ce2 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -6,8 +6,6 @@ #include #include -#include "common.h" - #include "airspy_adsb.h" #include "beast.h" #include "buf.h" diff --git a/adsbus/send.h b/adsbus/send.h index d696870..3058ac9 100644 --- a/adsbus/send.h +++ b/adsbus/send.h @@ -1,6 +1,6 @@ #pragma once -#include "common.h" +struct packet; void send_init(); void send_cleanup(); diff --git a/adsbus/stats.c b/adsbus/stats.c index 306de43..0ee5f97 100644 --- a/adsbus/stats.c +++ b/adsbus/stats.c @@ -2,9 +2,9 @@ #include #include -#include "common.h" #include "buf.h" #include "json.h" +#include "packet.h" #include "stats.h" diff --git a/adsbus/uuid.c b/adsbus/uuid.c index 10cb5ea..3464add 100644 --- a/adsbus/uuid.c +++ b/adsbus/uuid.c @@ -1,6 +1,5 @@ #include -#include "common.h" #include "hex.h" #include "rand.h" diff --git a/adsbus/wakeup.c b/adsbus/wakeup.c index f3e5d60..9fc9b7e 100644 --- a/adsbus/wakeup.c +++ b/adsbus/wakeup.c @@ -10,7 +10,6 @@ #include #include -#include "common.h" #include "peer.h" #include "rand.h"