2016-02-16 19:26:30 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2016-02-22 16:41:34 -08:00
|
|
|
#include "buf.h"
|
2016-02-22 16:58:13 -08:00
|
|
|
#include "packet.h"
|
2016-02-17 17:19:57 -08:00
|
|
|
#include "receive.h"
|
2016-02-22 16:41:34 -08:00
|
|
|
|
2016-02-16 03:42:41 +00:00
|
|
|
#include "beast.h"
|
|
|
|
|
|
2016-02-27 21:50:15 -08:00
|
|
|
struct __attribute__((packed)) beast_overlay {
|
2016-02-16 19:26:30 +00:00
|
|
|
uint8_t one_a;
|
|
|
|
|
uint8_t type;
|
|
|
|
|
uint8_t mlat_timestamp[6];
|
|
|
|
|
uint8_t rssi;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct beast_parser_state {
|
2016-02-22 16:58:13 -08:00
|
|
|
struct packet_mlat_state mlat_state;
|
2016-02-16 19:26:30 +00:00
|
|
|
};
|
|
|
|
|
|
2016-02-27 21:50:15 -08:00
|
|
|
#define BEAST_MLAT_MHZ 12
|
|
|
|
|
|
2016-02-16 19:26:30 +00:00
|
|
|
static uint64_t beast_parse_mlat(uint8_t *mlat_timestamp) {
|
|
|
|
|
return (
|
|
|
|
|
((uint64_t) mlat_timestamp[0]) << 40 |
|
|
|
|
|
((uint64_t) mlat_timestamp[1]) << 32 |
|
|
|
|
|
((uint64_t) mlat_timestamp[2]) << 24 |
|
|
|
|
|
((uint64_t) mlat_timestamp[3]) << 16 |
|
|
|
|
|
((uint64_t) mlat_timestamp[4]) << 8 |
|
|
|
|
|
((uint64_t) mlat_timestamp[5]));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 18:01:39 -08:00
|
|
|
static void beast_write_mlat(uint64_t timestamp, uint8_t *mlat_timestamp) {
|
|
|
|
|
mlat_timestamp[0] = (timestamp >> 40) & 0xff;
|
|
|
|
|
mlat_timestamp[1] = (timestamp >> 32) & 0xff;
|
|
|
|
|
mlat_timestamp[2] = (timestamp >> 24) & 0xff;
|
|
|
|
|
mlat_timestamp[3] = (timestamp >> 16) & 0xff;
|
|
|
|
|
mlat_timestamp[4] = (timestamp >> 8) & 0xff;
|
|
|
|
|
mlat_timestamp[5] = (timestamp) & 0xff;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 16:40:09 -08:00
|
|
|
static ssize_t beast_unescape(struct buf *out, const struct buf *in, size_t out_bytes) {
|
2016-02-25 23:37:37 -08:00
|
|
|
size_t o = 0, i = 0;
|
2016-02-16 19:26:30 +00:00
|
|
|
for (; i < in->length && o < out_bytes; i++, o++) {
|
|
|
|
|
if (i > 0 && buf_chr(in, i) == 0x1a) {
|
|
|
|
|
if (i == in->length - 1 || buf_chr(in, i + 1) != 0x1a) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
buf_chr(out, o) = buf_chr(in, i);
|
|
|
|
|
}
|
|
|
|
|
if (o == out_bytes) {
|
2016-02-25 23:37:37 -08:00
|
|
|
return (ssize_t) i;
|
2016-02-16 19:26:30 +00:00
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 18:01:39 -08:00
|
|
|
static void beast_escape(struct buf *out, const struct buf *in) {
|
2016-02-25 23:37:37 -08:00
|
|
|
for (size_t i = 0; i < in->length; i++, out->length++) {
|
2016-02-17 18:01:39 -08:00
|
|
|
buf_chr(out, out->length) = buf_chr(in, i);
|
|
|
|
|
if (i > 0 && buf_chr(in, i) == 0x1a) {
|
|
|
|
|
buf_chr(out, ++(out->length)) = 0x1a;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-16 19:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-27 21:50:15 -08:00
|
|
|
static bool beast_parse_packet(struct buf *buf, struct packet *packet, struct beast_parser_state *state, enum packet_type type) {
|
2016-02-16 19:26:30 +00:00
|
|
|
struct buf buf2 = BUF_INIT;
|
2016-02-27 21:50:15 -08:00
|
|
|
size_t payload_bytes = packet_payload_len[type];
|
|
|
|
|
ssize_t in_bytes = beast_unescape(&buf2, buf, sizeof(struct beast_overlay) + payload_bytes);
|
2016-02-16 19:26:30 +00:00
|
|
|
if (in_bytes < 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-02-27 21:50:15 -08:00
|
|
|
struct beast_overlay *overlay = (struct beast_overlay *) buf_at(&buf2, 0);
|
|
|
|
|
packet->type = type;
|
2016-02-16 19:26:30 +00:00
|
|
|
uint64_t source_mlat = beast_parse_mlat(overlay->mlat_timestamp);
|
2016-02-27 21:50:15 -08:00
|
|
|
packet->mlat_timestamp = packet_mlat_timestamp_scale_in(source_mlat, UINT64_C(0xffffffffffff), BEAST_MLAT_MHZ, &state->mlat_state);
|
2016-02-22 16:58:13 -08:00
|
|
|
packet->rssi = packet_rssi_scale_in(overlay->rssi, UINT8_MAX);
|
2016-02-27 21:50:15 -08:00
|
|
|
memcpy(packet->payload, buf_at(&buf2, sizeof(*overlay)), payload_bytes);
|
2016-02-25 23:37:37 -08:00
|
|
|
buf_consume(buf, (size_t) in_bytes);
|
2016-02-16 19:26:30 +00:00
|
|
|
return true;
|
2016-02-16 03:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-27 22:11:28 -08:00
|
|
|
static void beast_serialize_packet(struct packet *packet, struct buf *buf, uint8_t beast_type) {
|
2016-02-17 18:01:39 -08:00
|
|
|
struct buf buf2 = BUF_INIT;
|
2016-02-27 22:11:28 -08:00
|
|
|
size_t payload_bytes = packet_payload_len[packet->type];
|
2016-02-27 21:50:15 -08:00
|
|
|
struct beast_overlay *overlay = (struct beast_overlay *) buf_at(&buf2, 0);
|
|
|
|
|
overlay->one_a = 0x1a;
|
2016-02-27 22:11:28 -08:00
|
|
|
overlay->type = beast_type;
|
2016-02-27 21:50:15 -08:00
|
|
|
memcpy(buf_at(&buf2, sizeof(*overlay)), packet->payload, payload_bytes);
|
2016-02-17 18:01:39 -08:00
|
|
|
beast_write_mlat(
|
2016-02-27 21:50:15 -08:00
|
|
|
packet_mlat_timestamp_scale_out(packet->mlat_timestamp, UINT64_C(0xffffffffffff), BEAST_MLAT_MHZ),
|
2016-02-17 18:01:39 -08:00
|
|
|
overlay->mlat_timestamp);
|
|
|
|
|
|
|
|
|
|
if (packet->rssi) {
|
2016-02-25 23:37:37 -08:00
|
|
|
overlay->rssi = (uint8_t) packet_rssi_scale_out(packet->rssi, UINT8_MAX);
|
2016-02-17 18:01:39 -08:00
|
|
|
} else {
|
|
|
|
|
overlay->rssi = UINT8_MAX;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-27 21:50:15 -08:00
|
|
|
buf2.length = sizeof(*overlay) + payload_bytes;
|
2016-02-17 18:01:39 -08:00
|
|
|
beast_escape(buf, &buf2);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 23:37:37 -08:00
|
|
|
void beast_init() {
|
|
|
|
|
assert(sizeof(struct beast_parser_state) <= PARSER_STATE_LEN);
|
2016-02-27 21:50:15 -08:00
|
|
|
assert((sizeof(struct beast_overlay) + PACKET_PAYLOAD_LEN_MAX) * 2 <= BUF_LEN_MAX);
|
2016-02-25 23:37:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool beast_parse(struct buf *buf, struct packet *packet, void *state_in) {
|
|
|
|
|
struct beast_parser_state *state = (struct beast_parser_state *) state_in;
|
2016-02-27 21:50:15 -08:00
|
|
|
struct beast_overlay *overlay = (struct beast_overlay *) buf_at(buf, 0);
|
2016-02-25 23:37:37 -08:00
|
|
|
|
2016-02-27 21:50:15 -08:00
|
|
|
if (buf->length < sizeof(*overlay) ||
|
2016-02-25 23:37:37 -08:00
|
|
|
buf_chr(buf, 0) != 0x1a) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (overlay->type) {
|
2016-02-27 21:50:15 -08:00
|
|
|
case 0x31:
|
|
|
|
|
return beast_parse_packet(buf, packet, state, PACKET_TYPE_MODE_AC);
|
|
|
|
|
|
2016-02-25 23:37:37 -08:00
|
|
|
case 0x32:
|
2016-02-27 21:50:15 -08:00
|
|
|
return beast_parse_packet(buf, packet, state, PACKET_TYPE_MODE_S_SHORT);
|
2016-02-25 23:37:37 -08:00
|
|
|
|
|
|
|
|
case 0x33:
|
2016-02-27 21:50:15 -08:00
|
|
|
return beast_parse_packet(buf, packet, state, PACKET_TYPE_MODE_S_LONG);
|
2016-02-25 23:37:37 -08:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 18:01:39 -08:00
|
|
|
void beast_serialize(struct packet *packet, struct buf *buf) {
|
|
|
|
|
switch (packet->type) {
|
2016-02-23 13:49:23 -08:00
|
|
|
case PACKET_TYPE_NONE:
|
|
|
|
|
break;
|
|
|
|
|
|
2016-02-27 21:50:15 -08:00
|
|
|
case PACKET_TYPE_MODE_AC:
|
|
|
|
|
beast_serialize_packet(packet, buf, 0x31);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-02-23 13:49:23 -08:00
|
|
|
case PACKET_TYPE_MODE_S_SHORT:
|
2016-02-27 21:50:15 -08:00
|
|
|
beast_serialize_packet(packet, buf, 0x32);
|
2016-02-17 18:01:39 -08:00
|
|
|
break;
|
|
|
|
|
|
2016-02-23 13:49:23 -08:00
|
|
|
case PACKET_TYPE_MODE_S_LONG:
|
2016-02-27 21:50:15 -08:00
|
|
|
beast_serialize_packet(packet, buf, 0x33);
|
2016-02-17 18:01:39 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|