Enable -Weverything, fix a ton of bugs.
This commit is contained in:
@@ -47,9 +47,9 @@ static void json_hello(struct buf *buf) {
|
||||
|
||||
static void json_add_common(struct packet *packet, json_t *obj) {
|
||||
json_object_set_new(obj, "type", json_string(packet_type_names[packet->type]));
|
||||
json_object_set_new(obj, "source_id", json_string(packet->source_id));
|
||||
json_object_set_new(obj, "source_id", json_string((const char *) packet->source_id));
|
||||
if (packet->mlat_timestamp) {
|
||||
json_object_set_new(obj, "mlat_timestamp", json_integer(packet->mlat_timestamp));
|
||||
json_object_set_new(obj, "mlat_timestamp", json_integer(packet->mlat_timestamp % INT64_MAX));
|
||||
}
|
||||
if (packet->rssi) {
|
||||
json_object_set_new(obj, "rssi", json_integer(packet->rssi));
|
||||
@@ -58,7 +58,7 @@ static void json_add_common(struct packet *packet, json_t *obj) {
|
||||
|
||||
static void json_serialize_mode_s_short(struct packet *packet, struct buf *buf) {
|
||||
assert(packet->mlat_timestamp < PACKET_MLAT_MAX);
|
||||
char hexbuf[14];
|
||||
uint8_t hexbuf[14];
|
||||
hex_from_bin_upper(hexbuf, packet->payload, 7);
|
||||
json_t *out = json_pack("{ss#}", "payload", hexbuf, 14);
|
||||
json_add_common(packet, out);
|
||||
@@ -67,7 +67,7 @@ static void json_serialize_mode_s_short(struct packet *packet, struct buf *buf)
|
||||
|
||||
static void json_serialize_mode_s_long(struct packet *packet, struct buf *buf) {
|
||||
assert(packet->mlat_timestamp < PACKET_MLAT_MAX);
|
||||
char hexbuf[28];
|
||||
uint8_t hexbuf[28];
|
||||
hex_from_bin_upper(hexbuf, packet->payload, 14);
|
||||
json_t *out = json_pack("{ss#}", "payload", hexbuf, 28);
|
||||
json_add_common(packet, out);
|
||||
@@ -91,11 +91,17 @@ static bool json_parse_header(json_t *in, struct packet *packet, struct json_par
|
||||
return false;
|
||||
}
|
||||
|
||||
state->mlat_timestamp_mhz = mlat_timestamp_mhz;
|
||||
state->mlat_timestamp_max = mlat_timestamp_max;
|
||||
state->rssi_max = rssi_max;
|
||||
if (mlat_timestamp_mhz > UINT16_MAX ||
|
||||
mlat_timestamp_max < 0 ||
|
||||
rssi_max > UINT32_MAX) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!strcmp(json_server_id, server_id)) {
|
||||
state->mlat_timestamp_mhz = (uint16_t) mlat_timestamp_mhz;
|
||||
state->mlat_timestamp_max = (uint64_t) mlat_timestamp_max;
|
||||
state->rssi_max = (uint32_t) rssi_max;
|
||||
|
||||
if (!strcmp(json_server_id, (const char *) server_id)) {
|
||||
fprintf(stderr, "R %s: Attempt to receive json data from our own server ID (%s); loop!\n", packet->source_id, server_id);
|
||||
return false;
|
||||
}
|
||||
@@ -118,8 +124,12 @@ static bool json_parse_common(json_t *in, struct packet *packet, struct json_par
|
||||
|
||||
json_t *mlat_timestamp = json_object_get(in, "mlat_timestamp");
|
||||
if (mlat_timestamp && json_is_integer(mlat_timestamp)) {
|
||||
json_int_t val = json_integer_value(mlat_timestamp);
|
||||
if (val < 0) {
|
||||
return false;
|
||||
}
|
||||
packet->mlat_timestamp = packet_mlat_timestamp_scale_in(
|
||||
json_integer_value(mlat_timestamp),
|
||||
(uint64_t) val,
|
||||
state->mlat_timestamp_max,
|
||||
state->mlat_timestamp_mhz,
|
||||
&state->mlat_state);
|
||||
@@ -127,7 +137,11 @@ static bool json_parse_common(json_t *in, struct packet *packet, struct json_par
|
||||
|
||||
json_t *rssi = json_object_get(in, "rssi");
|
||||
if (rssi && json_is_integer(rssi)) {
|
||||
packet->rssi = packet_rssi_scale_in(json_integer_value(rssi), state->rssi_max);
|
||||
json_int_t val = json_integer_value(rssi);
|
||||
if (val > state->rssi_max) {
|
||||
return false;
|
||||
}
|
||||
packet->rssi = packet_rssi_scale_in((uint32_t) val, state->rssi_max);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -143,7 +157,7 @@ static bool json_parse_mode_s_short(json_t *in, struct packet *packet, struct js
|
||||
return false;
|
||||
}
|
||||
|
||||
hex_to_bin(packet->payload, json_string_value(payload), 7);
|
||||
hex_to_bin(packet->payload, (const uint8_t *) json_string_value(payload), 7);
|
||||
packet->type = PACKET_TYPE_MODE_S_SHORT;
|
||||
return true;
|
||||
}
|
||||
@@ -158,7 +172,7 @@ static bool json_parse_mode_s_long(json_t *in, struct packet *packet, struct jso
|
||||
return false;
|
||||
}
|
||||
|
||||
hex_to_bin(packet->payload, json_string_value(payload), 14);
|
||||
hex_to_bin(packet->payload, (const uint8_t *) json_string_value(payload), 14);
|
||||
packet->type = PACKET_TYPE_MODE_S_LONG;
|
||||
return true;
|
||||
}
|
||||
@@ -187,7 +201,7 @@ bool json_parse(struct buf *buf, struct packet *packet, void *state_in) {
|
||||
}
|
||||
|
||||
json_error_t err;
|
||||
json_t *in = json_loadb(buf_at(buf, 0), buf->length, JSON_DISABLE_EOF_CHECK | JSON_REJECT_DUPLICATES, &err);
|
||||
json_t *in = json_loadb((const char *) buf_at(buf, 0), buf->length, JSON_DISABLE_EOF_CHECK | JSON_REJECT_DUPLICATES, &err);
|
||||
if (!in) {
|
||||
return false;
|
||||
}
|
||||
@@ -216,7 +230,8 @@ bool json_parse(struct buf *buf, struct packet *packet, void *state_in) {
|
||||
return false;
|
||||
}
|
||||
|
||||
buf_consume(buf, err.position);
|
||||
assert(err.position > 0);
|
||||
buf_consume(buf, (size_t) err.position);
|
||||
while (buf->length && (buf_chr(buf, 0) == '\r' || buf_chr(buf, 0) == '\n')) {
|
||||
buf_consume(buf, 1);
|
||||
}
|
||||
@@ -231,6 +246,9 @@ void json_serialize(struct packet *packet, struct buf *buf) {
|
||||
}
|
||||
|
||||
switch (packet->type) {
|
||||
case PACKET_TYPE_NONE:
|
||||
break;
|
||||
|
||||
case PACKET_TYPE_MODE_S_SHORT:
|
||||
json_serialize_mode_s_short(packet, buf);
|
||||
break;
|
||||
@@ -238,9 +256,6 @@ void json_serialize(struct packet *packet, struct buf *buf) {
|
||||
case PACKET_TYPE_MODE_S_LONG:
|
||||
json_serialize_mode_s_long(packet, buf);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user