2016-02-16 02:28:05 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
2016-02-17 00:21:28 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <jansson.h>
|
2016-02-16 02:28:05 +00:00
|
|
|
|
2016-02-17 17:19:57 -08:00
|
|
|
#include "receive.h"
|
|
|
|
|
#include "send.h"
|
2016-02-22 16:27:44 -08:00
|
|
|
#include "rand.h"
|
|
|
|
|
#include "uuid.h"
|
2016-02-16 02:28:05 +00:00
|
|
|
#include "json.h"
|
|
|
|
|
|
2016-02-17 11:59:37 -08:00
|
|
|
static void json_serialize_to_buf(json_t *obj, struct buf *buf) {
|
|
|
|
|
assert(json_dump_callback(obj, json_buf_append_callback, buf, 0) == 0);
|
|
|
|
|
json_decref(obj);
|
|
|
|
|
buf_chr(buf, buf->length++) = '\n';
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 00:21:28 +00:00
|
|
|
static void json_hello(struct buf *buf) {
|
2016-02-17 17:19:57 -08:00
|
|
|
json_t *hello = json_pack("{sIsIsI}",
|
2016-02-17 00:21:28 +00:00
|
|
|
"mlat_timestamp_mhz", (json_int_t) MLAT_MHZ,
|
|
|
|
|
"mlat_timestamp_max", (json_int_t) MLAT_MAX,
|
|
|
|
|
"rssi_max", (json_int_t) RSSI_MAX);
|
2016-02-17 11:59:37 -08:00
|
|
|
json_serialize_to_buf(hello, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void json_add_common(struct packet *packet, json_t *obj) {
|
2016-02-17 12:13:21 -08:00
|
|
|
json_object_set_new(obj, "type", json_string(packet_type_names[packet->type]));
|
2016-02-17 11:59:37 -08:00
|
|
|
if (packet->mlat_timestamp) {
|
|
|
|
|
json_object_set_new(obj, "mlat_timestamp", json_integer(packet->mlat_timestamp));
|
|
|
|
|
}
|
|
|
|
|
if (packet->rssi) {
|
|
|
|
|
json_object_set_new(obj, "rssi", json_integer(packet->rssi));
|
|
|
|
|
}
|
2016-02-16 02:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 00:21:28 +00:00
|
|
|
static void json_serialize_mode_s_short(struct packet *packet, struct buf *buf) {
|
|
|
|
|
assert(packet->mlat_timestamp < MLAT_MAX);
|
2016-02-16 02:28:05 +00:00
|
|
|
char hexbuf[14];
|
2016-02-22 16:27:44 -08:00
|
|
|
hex_from_bin_upper(hexbuf, packet->payload, 7);
|
2016-02-17 11:59:37 -08:00
|
|
|
json_t *out = json_pack("{ss#}", "payload", hexbuf, 14);
|
|
|
|
|
json_add_common(packet, out);
|
|
|
|
|
json_serialize_to_buf(out, buf);
|
2016-02-16 02:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 00:21:28 +00:00
|
|
|
static void json_serialize_mode_s_long(struct packet *packet, struct buf *buf) {
|
|
|
|
|
assert(packet->mlat_timestamp < MLAT_MAX);
|
2016-02-17 11:07:18 -08:00
|
|
|
char hexbuf[28];
|
2016-02-22 16:27:44 -08:00
|
|
|
hex_from_bin_upper(hexbuf, packet->payload, 14);
|
2016-02-17 11:59:37 -08:00
|
|
|
json_t *out = json_pack("{ss#}", "payload", hexbuf, 28);
|
|
|
|
|
json_add_common(packet, out);
|
|
|
|
|
json_serialize_to_buf(out, buf);
|
2016-02-16 02:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 16:40:09 -08:00
|
|
|
void json_init() {
|
|
|
|
|
assert(JSON_INTEGER_IS_LONG_LONG);
|
2016-02-22 16:27:44 -08:00
|
|
|
|
|
|
|
|
size_t seed;
|
|
|
|
|
rand_fill(&seed, sizeof(seed));
|
|
|
|
|
json_object_seed(seed);
|
2016-02-17 16:40:09 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 00:21:28 +00:00
|
|
|
void json_serialize(struct packet *packet, struct buf *buf) {
|
2016-02-16 02:28:05 +00:00
|
|
|
if (!packet) {
|
2016-02-17 00:21:28 +00:00
|
|
|
json_hello(buf);
|
|
|
|
|
return;
|
2016-02-16 02:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (packet->type) {
|
|
|
|
|
case MODE_S_SHORT:
|
2016-02-17 00:21:28 +00:00
|
|
|
json_serialize_mode_s_short(packet, buf);
|
|
|
|
|
break;
|
2016-02-16 02:28:05 +00:00
|
|
|
|
|
|
|
|
case MODE_S_LONG:
|
2016-02-17 00:21:28 +00:00
|
|
|
json_serialize_mode_s_long(packet, buf);
|
|
|
|
|
break;
|
2016-02-16 02:28:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-17 16:40:09 -08:00
|
|
|
|
|
|
|
|
int json_buf_append_callback(const char *buffer, size_t size, void *data) {
|
|
|
|
|
struct buf *buf = data;
|
|
|
|
|
if (buf->length + size + 1 > BUF_LEN_MAX) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
memcpy(buf_at(buf, buf->length), buffer, size);
|
|
|
|
|
buf->length += size;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|