From cbae234327a3cc2a50e8528d9d5c45aab35d0b3e Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 15 Feb 2016 22:01:32 +0000 Subject: [PATCH] MLAT (width and frequency) and RSSI (width) scaling working. --- airspy_adsb.c | 7 +++---- common.c | 28 ++++++++++++++++++++++++++++ common.h | 17 +++++++++++++++-- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/airspy_adsb.c b/airspy_adsb.c index 81d8e97..71969fd 100644 --- a/airspy_adsb.c +++ b/airspy_adsb.c @@ -7,8 +7,7 @@ #include "airspy_adsb.h" struct airspy_adsb_parser_state { - uint64_t mlat_timestamp_last; - uint64_t mlat_timestamp_generation; + struct mlat_state mlat_state; }; static bool airspy_adsb_parse_common(char *, struct packet *, struct airspy_adsb_parser_state *); @@ -60,7 +59,7 @@ static bool airspy_adsb_parse_common(char *in, struct packet *packet, struct air return false; } uint16_t mlat_mhz = 2 * hex_to_int(&in[9], 1); - packet->mlat_timestamp = hex_to_int(in, 4) * (MLAT_MHZ / mlat_mhz); - packet->rssi = hex_to_int(&in[12], 2) * (RSSI_MAX / UINT16_MAX); + packet->mlat_timestamp = mlat_timestamp_scale_in(hex_to_int(in, 4), UINT32_MAX, mlat_mhz, &state->mlat_state); + packet->rssi = rssi_scale_in(hex_to_int(&in[12], 2), UINT16_MAX); return true; } diff --git a/common.c b/common.c index d33945d..2989fef 100644 --- a/common.c +++ b/common.c @@ -7,6 +7,10 @@ #include "common.h" +#define MLAT_MHZ 120 +#define RSSI_MAX UINT32_MAX + + void buf_init(struct buf *buf) { buf->start = 0; buf->length = 0; @@ -40,6 +44,30 @@ void buf_consume(struct buf *buf, size_t length) { } +uint64_t mlat_timestamp_scale_mhz_in(uint64_t timestamp, uint32_t mhz) { + return timestamp * (MLAT_MHZ / mhz); +} + +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); +} + + +uint32_t rssi_scale_in(uint32_t value, uint32_t max) { + return value * (RSSI_MAX / max); +} + + static uint8_t hex_table[256] = {0}; void hex_init() { diff --git a/common.h b/common.h index 7ea384c..ec7c7be 100644 --- a/common.h +++ b/common.h @@ -34,8 +34,6 @@ void buf_consume(struct buf *, size_t); //////// packet -#define MLAT_MHZ 120 -#define RSSI_MAX UINT32_MAX #define DATA_LEN_MAX 14 struct packet { enum { @@ -49,6 +47,21 @@ struct packet { }; +//////// mlat + +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 *); + + +//////// rssi + +uint32_t rssi_scale_in(uint32_t, uint32_t); + + //////// hex void hex_init();