2016-02-16 02:28:05 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
|
#include "json.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Hobo JSON to avoid overhead. Assumes that we can't get quotes in the data.
|
|
|
|
|
|
|
|
|
|
void json_init() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t json_hello(char *buf) {
|
|
|
|
|
int len = snprintf(buf, SERIALIZE_LEN,
|
2016-02-16 06:45:02 +00:00
|
|
|
"{\"mlat_timestamp_mhz\":%ju, \"mlat_timestamp_max\":%ju, \"rssi_max\":%ju}\n",
|
2016-02-16 02:28:05 +00:00
|
|
|
(uintmax_t) MLAT_MHZ,
|
|
|
|
|
(uintmax_t) MLAT_MAX,
|
|
|
|
|
(uintmax_t) RSSI_MAX);
|
|
|
|
|
assert(len < SERIALIZE_LEN);
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t json_serialize_mode_s_short(struct packet *packet, char *buf) {
|
|
|
|
|
char hexbuf[14];
|
2016-02-16 03:26:52 +00:00
|
|
|
hex_from_bin(hexbuf, packet->payload, 7);
|
2016-02-16 02:28:05 +00:00
|
|
|
int len = snprintf(buf, SERIALIZE_LEN,
|
2016-02-16 06:45:02 +00:00
|
|
|
"{\"payload\":\"%.14s\", \"mlat_timestamp\":%ju, \"rssi\":%ju}\n",
|
2016-02-16 02:28:05 +00:00
|
|
|
hexbuf,
|
|
|
|
|
(uintmax_t) packet->mlat_timestamp,
|
|
|
|
|
(uintmax_t) packet->rssi);
|
|
|
|
|
assert(len < SERIALIZE_LEN);
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t json_serialize_mode_s_long(struct packet *packet, char *buf) {
|
|
|
|
|
char hexbuf[28];
|
2016-02-16 03:26:52 +00:00
|
|
|
hex_from_bin(hexbuf, packet->payload, 14);
|
2016-02-16 02:28:05 +00:00
|
|
|
int len = snprintf(buf, SERIALIZE_LEN,
|
2016-02-16 06:45:02 +00:00
|
|
|
"{\"payload\":\"%.28s\", \"mlat_timestamp\":%ju, \"rssi\":%ju}\n",
|
2016-02-16 02:28:05 +00:00
|
|
|
hexbuf,
|
|
|
|
|
(uintmax_t) packet->mlat_timestamp,
|
|
|
|
|
(uintmax_t) packet->rssi);
|
|
|
|
|
assert(len < SERIALIZE_LEN);
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t json_serialize(struct packet *packet, char *buf) {
|
|
|
|
|
if (!packet) {
|
|
|
|
|
return json_hello(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (packet->type) {
|
|
|
|
|
case MODE_AC:
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
case MODE_S_SHORT:
|
|
|
|
|
return json_serialize_mode_s_short(packet, buf);
|
|
|
|
|
|
|
|
|
|
case MODE_S_LONG:
|
|
|
|
|
return json_serialize_mode_s_long(packet, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|