Split out packet. Delete common.
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
|
||||
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
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "beast.h"
|
||||
#include "common.h"
|
||||
#include "hex.h"
|
||||
#include "incoming.h"
|
||||
#include "json.h"
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#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) {
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
//////// 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);
|
||||
@@ -10,7 +10,6 @@
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "peer.h"
|
||||
#include "wakeup.h"
|
||||
#include "uuid.h"
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
struct buf;
|
||||
struct packet;
|
||||
|
||||
void json_init();
|
||||
void json_serialize(struct packet *, struct buf *);
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "peer.h"
|
||||
#include "wakeup.h"
|
||||
#include "uuid.h"
|
||||
|
||||
44
adsbus/packet.c
Normal file
44
adsbus/packet.c
Normal file
@@ -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);
|
||||
}
|
||||
33
adsbus/packet.h
Normal file
33
adsbus/packet.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "buf.h"
|
||||
#include "hex.h"
|
||||
#include "packet.h"
|
||||
#include "uuid.h"
|
||||
|
||||
#include "raw.h"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define PARSER_STATE_LEN 256
|
||||
|
||||
void receive_new(int, void *);
|
||||
|
||||
@@ -6,8 +6,6 @@
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "airspy_adsb.h"
|
||||
#include "beast.h"
|
||||
#include "buf.h"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
struct packet;
|
||||
|
||||
void send_init();
|
||||
void send_cleanup();
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
#include <time.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "buf.h"
|
||||
#include "json.h"
|
||||
#include "packet.h"
|
||||
|
||||
#include "stats.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "hex.h"
|
||||
#include "rand.h"
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "peer.h"
|
||||
#include "rand.h"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user