Files
adsb-tools/airspy_adsb.c

67 lines
1.7 KiB
C
Raw Normal View History

2016-02-15 06:47:40 +00:00
#include <assert.h>
2016-02-14 22:26:34 +00:00
#include <stdbool.h>
2016-02-14 07:11:26 +00:00
#include <stdio.h>
#include <string.h>
#include "common.h"
#include "airspy_adsb.h"
2016-02-15 19:07:56 +00:00
struct airspy_adsb_parser_state {
2016-02-15 06:47:40 +00:00
uint64_t mlat_timestamp_last;
uint64_t mlat_timestamp_generation;
};
2016-02-14 22:26:34 +00:00
2016-02-15 19:07:56 +00:00
static bool airspy_adsb_parse_common(char *, struct packet *, struct airspy_adsb_parser_state *);
2016-02-14 22:26:34 +00:00
2016-02-15 06:47:40 +00:00
void airspy_adsb_init() {
2016-02-15 19:07:56 +00:00
assert(sizeof(struct airspy_adsb_parser_state) <= PARSER_STATE_LEN);
2016-02-15 06:47:40 +00:00
}
2016-02-15 19:07:56 +00:00
bool airspy_adsb_parse(struct backend *backend, struct packet *packet) {
struct buf *buf = &backend->buf;
struct airspy_adsb_parser_state *state = (struct airspy_adsb_parser_state *) backend->parser_state;
2016-02-14 20:35:43 +00:00
if (buf->length < 35 ||
buf_chr(buf, 0) != '*') {
return false;
}
2016-02-14 20:35:43 +00:00
if (buf->length >= 35 &&
2016-02-14 22:26:34 +00:00
buf_chr(buf, 33) == '\r' &&
2016-02-14 20:35:43 +00:00
buf_chr(buf, 34) == '\n' &&
buf_chr(buf, 15) == ';') {
2016-02-15 06:47:40 +00:00
if (!airspy_adsb_parse_common(buf_at(buf, 16), packet, state)) {
2016-02-14 22:26:34 +00:00
return false;
}
2016-02-14 20:35:43 +00:00
packet->type = MODE_S_SHORT;
2016-02-14 22:26:34 +00:00
hex_to_bin(packet->data, buf_at(buf, 1), 7);
2016-02-14 07:11:26 +00:00
buf_consume(buf, 35);
return true;
}
2016-02-14 20:35:43 +00:00
if (buf->length >= 49 &&
2016-02-14 22:26:34 +00:00
buf_chr(buf, 47) == '\r' &&
2016-02-14 20:35:43 +00:00
buf_chr(buf, 48) == '\n' &&
buf_chr(buf, 29) == ';') {
2016-02-15 06:47:40 +00:00
if (!airspy_adsb_parse_common(buf_at(buf, 30), packet, state)) {
2016-02-14 22:26:34 +00:00
return false;
}
2016-02-14 20:35:43 +00:00
packet->type = MODE_S_LONG;
2016-02-14 22:26:34 +00:00
hex_to_bin(packet->data, buf_at(buf, 1), 14);
2016-02-14 07:11:26 +00:00
buf_consume(buf, 49);
return true;
}
2016-02-14 07:11:26 +00:00
return false;
}
2016-02-14 22:26:34 +00:00
2016-02-15 19:07:56 +00:00
static bool airspy_adsb_parse_common(char *in, struct packet *packet, struct airspy_adsb_parser_state *state) {
2016-02-14 22:26:34 +00:00
if (in[8] != ';' ||
in[11] != ';' ||
in[16] != ';') {
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);
2016-02-15 00:29:46 +00:00
packet->rssi = hex_to_int(&in[12], 2) * (RSSI_MAX / UINT16_MAX);
2016-02-14 22:26:34 +00:00
return true;
}