2017-07-28 18:11:52 +02:00
|
|
|
/** JSON serializtion of sample data.
|
2017-04-15 21:22:19 +02:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-15 21:22:19 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/sample.h>
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/compat.hpp>
|
2018-08-20 18:30:24 +02:00
|
|
|
#include <villas/signal.h>
|
2018-05-12 15:25:47 +02:00
|
|
|
#include <villas/io.h>
|
2018-05-12 13:56:12 +02:00
|
|
|
#include <villas/formats/json.h>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
|
|
|
|
using namespace villas;
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
static enum SignalType json_detect_format(json_t *val)
|
2018-08-20 18:30:24 +02:00
|
|
|
{
|
|
|
|
int type = json_typeof(val);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case JSON_REAL:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::FLOAT;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
case JSON_INTEGER:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::INTEGER;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
case JSON_TRUE:
|
|
|
|
case JSON_FALSE:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::BOOLEAN;
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
case JSON_OBJECT:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::COMPLEX; /* must be a complex number */
|
2018-08-20 18:30:24 +02:00
|
|
|
|
|
|
|
default:
|
2019-06-23 16:13:23 +02:00
|
|
|
return SignalType::INVALID;
|
2018-08-20 18:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
static json_t * json_pack_timestamps(struct io *io, struct sample *smp)
|
2018-05-09 09:17:40 +02:00
|
|
|
{
|
|
|
|
json_t *json_ts = json_object();
|
|
|
|
|
2019-04-17 18:18:40 +02:00
|
|
|
#ifdef __arm__ // TODO: check
|
|
|
|
const char *fmt = "[ i, i ]";
|
|
|
|
#else
|
|
|
|
const char *fmt = "[ I, I ]";
|
|
|
|
#endif
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (io->flags & (int) SampleFlags::HAS_TS_ORIGIN) {
|
|
|
|
if (smp->flags & (int) SampleFlags::HAS_TS_ORIGIN)
|
2019-04-17 18:18:40 +02:00
|
|
|
json_object_set(json_ts, "origin", json_pack(fmt, smp->ts.origin.tv_sec, smp->ts.origin.tv_nsec));
|
2019-04-17 18:18:08 +02:00
|
|
|
}
|
2018-05-09 09:17:40 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (io->flags & (int) SampleFlags::HAS_TS_RECEIVED) {
|
|
|
|
if (smp->flags & (int) SampleFlags::HAS_TS_RECEIVED)
|
2019-04-17 18:18:40 +02:00
|
|
|
json_object_set(json_ts, "received", json_pack(fmt, smp->ts.received.tv_sec, smp->ts.received.tv_nsec));
|
2019-04-17 18:18:08 +02:00
|
|
|
}
|
2018-05-09 09:17:40 +02:00
|
|
|
|
|
|
|
return json_ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_unpack_timestamps(json_t *json_ts, struct sample *smp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
2019-06-23 16:58:28 +02:00
|
|
|
json_t *json_ts_origin = nullptr;
|
|
|
|
json_t *json_ts_received = nullptr;
|
2018-05-09 09:17:40 +02:00
|
|
|
|
|
|
|
json_unpack_ex(json_ts, &err, 0, "{ s?: o, s?: o }",
|
|
|
|
"origin", &json_ts_origin,
|
|
|
|
"received", &json_ts_received
|
|
|
|
);
|
|
|
|
|
|
|
|
if (json_ts_origin) {
|
|
|
|
ret = json_unpack_ex(json_ts_origin, &err, 0, "[ I, I ]", &smp->ts.origin.tv_sec, &smp->ts.origin.tv_nsec);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
|
2018-05-09 09:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (json_ts_received) {
|
|
|
|
ret = json_unpack_ex(json_ts_received, &err, 0, "[ I, I ]", &smp->ts.received.tv_sec, &smp->ts.received.tv_nsec);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_RECEIVED;
|
2018-05-09 09:17:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-12 18:11:39 +02:00
|
|
|
static int json_pack_sample(struct io *io, json_t **j, struct sample *smp)
|
2017-04-15 20:38:58 +02:00
|
|
|
{
|
2017-08-22 12:21:17 +02:00
|
|
|
json_t *json_smp;
|
2017-04-15 20:38:58 +02:00
|
|
|
json_error_t err;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
json_smp = json_pack_ex(&err, 0, "{ s: o }", "ts", json_pack_timestamps(io, smp));
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (io->flags & (int) SampleFlags::HAS_SEQUENCE) {
|
|
|
|
if (smp->flags & (int) SampleFlags::HAS_SEQUENCE) {
|
2019-04-17 18:18:08 +02:00
|
|
|
json_t *json_sequence = json_integer(smp->sequence);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2019-04-17 18:18:08 +02:00
|
|
|
json_object_set(json_smp, "sequence", json_sequence);
|
|
|
|
}
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (io->flags & (int) SampleFlags::HAS_DATA) {
|
2017-08-22 12:21:17 +02:00
|
|
|
json_t *json_data = json_array();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
for (unsigned i = 0; i < smp->length; i++) {
|
2020-07-01 17:02:22 +02:00
|
|
|
struct signal *sig = (struct signal *) vlist_at_safe(smp->signals, i);
|
2020-07-06 15:07:05 +02:00
|
|
|
if (!sig)
|
|
|
|
return -1;
|
2020-07-01 17:02:22 +02:00
|
|
|
|
2021-02-19 06:37:21 +01:00
|
|
|
json_t *json_value = signal_data_to_json(&smp->data[i], sig->type);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
json_array_append(json_data, json_value);
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
json_object_set(json_smp, "data", json_data);
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
*j = json_smp;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-12 18:11:39 +02:00
|
|
|
static int json_pack_samples(struct io *io, json_t **j, struct sample *smps[], unsigned cnt)
|
2017-04-15 20:38:58 +02:00
|
|
|
{
|
2017-08-22 12:21:17 +02:00
|
|
|
int ret;
|
|
|
|
json_t *json_smps = json_array();
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
for (unsigned i = 0; i < cnt; i++) {
|
2017-08-22 12:21:17 +02:00
|
|
|
json_t *json_smp;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
ret = json_pack_sample(io, &json_smp, smps[i]);
|
2017-08-22 12:21:17 +02:00
|
|
|
if (ret)
|
|
|
|
break;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
json_array_append(json_smps, json_smp);
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
*j = json_smps;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2018-05-12 18:11:39 +02:00
|
|
|
static int json_unpack_sample(struct io *io, json_t *json_smp, struct sample *smp)
|
2017-08-22 12:21:17 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2018-05-09 09:17:40 +02:00
|
|
|
json_error_t err;
|
2019-06-23 16:58:28 +02:00
|
|
|
json_t *json_data, *json_value, *json_ts = nullptr;
|
2017-08-22 12:21:17 +02:00
|
|
|
size_t i;
|
2018-05-09 09:17:40 +02:00
|
|
|
int64_t sequence = -1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->signals = io->signals;
|
|
|
|
|
2018-05-09 09:17:40 +02:00
|
|
|
ret = json_unpack_ex(json_smp, &err, 0, "{ s?: o, s?: I, s: o }",
|
|
|
|
"ts", &json_ts,
|
|
|
|
"sequence", &sequence,
|
2017-04-15 20:38:58 +02:00
|
|
|
"data", &json_data);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-05-09 09:17:40 +02:00
|
|
|
smp->flags = 0;
|
|
|
|
smp->length = 0;
|
|
|
|
|
|
|
|
if (json_ts) {
|
|
|
|
ret = json_unpack_timestamps(json_ts, smp);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
if (!json_is_array(json_data))
|
|
|
|
return -1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-09 09:17:40 +02:00
|
|
|
if (sequence >= 0) {
|
|
|
|
smp->sequence = sequence;
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_SEQUENCE;
|
2018-05-09 09:17:40 +02:00
|
|
|
}
|
2017-04-15 20:38:58 +02:00
|
|
|
|
|
|
|
json_array_foreach(json_data, i, json_value) {
|
2017-08-22 12:21:17 +02:00
|
|
|
if (i >= smp->capacity)
|
|
|
|
break;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
struct signal *sig = (struct signal *) vlist_at_safe(smp->signals, i);
|
2018-08-20 18:30:24 +02:00
|
|
|
if (!sig)
|
|
|
|
return -1;
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
enum SignalType fmt = json_detect_format(json_value);
|
2021-02-16 14:15:14 +01:00
|
|
|
if (sig->type != fmt)
|
|
|
|
throw RuntimeError("Received invalid data type in JSON payload: Received {}, expected {} for signal {} (index {}).",
|
2018-08-20 18:30:24 +02:00
|
|
|
signal_type_to_str(fmt), signal_type_to_str(sig->type), sig->name, i);
|
|
|
|
|
2021-02-19 06:37:21 +01:00
|
|
|
ret = signal_data_parse_json(&smp->data[i], sig->type, json_value);
|
2018-08-20 18:30:24 +02:00
|
|
|
if (ret)
|
|
|
|
return -3;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
smp->length++;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
|
|
|
if (smp->length > 0)
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_DATA;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-12 18:11:39 +02:00
|
|
|
static int json_unpack_samples(struct io *io, json_t *json_smps, struct sample *smps[], unsigned cnt)
|
2017-08-22 12:21:17 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
json_t *json_smp;
|
|
|
|
size_t i;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
if (!json_is_array(json_smps))
|
|
|
|
return -1;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
json_array_foreach(json_smps, i, json_smp) {
|
|
|
|
if (i >= cnt)
|
|
|
|
break;
|
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
ret = json_unpack_sample(io, json_smp, smps[i]);
|
2017-08-22 12:21:17 +02:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
int json_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct sample *smps[], unsigned cnt)
|
2017-04-15 20:38:58 +02:00
|
|
|
{
|
2017-08-22 12:21:17 +02:00
|
|
|
int ret;
|
2017-04-15 20:38:58 +02:00
|
|
|
json_t *json;
|
2017-08-22 12:21:17 +02:00
|
|
|
size_t wr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
ret = json_pack_samples(io, &json, smps, cnt);
|
2017-08-22 12:21:17 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
wr = json_dumpb(json, buf, len, 0);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
json_decref(json);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
if (wbytes)
|
|
|
|
*wbytes = wr;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
return ret;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
int json_sscan(struct io *io, const char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt)
|
2017-04-15 20:38:58 +02:00
|
|
|
{
|
2017-08-22 12:21:17 +02:00
|
|
|
int ret;
|
2017-04-15 20:38:58 +02:00
|
|
|
json_t *json;
|
|
|
|
json_error_t err;
|
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
json = json_loadb(buf, len, 0, &err);
|
|
|
|
if (!json)
|
|
|
|
return -1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
ret = json_unpack_samples(io, json, smps, cnt);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
json_decref(json);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-05-09 09:17:40 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
if (rbytes)
|
|
|
|
*rbytes = err.position;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
return ret;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2018-05-12 15:25:47 +02:00
|
|
|
int json_print(struct io *io, struct sample *smps[], unsigned cnt)
|
2017-07-28 18:11:52 +02:00
|
|
|
{
|
2019-04-07 15:13:40 +02:00
|
|
|
int ret;
|
|
|
|
unsigned i;
|
2017-08-05 21:02:09 +02:00
|
|
|
json_t *json;
|
|
|
|
|
2018-05-12 15:25:47 +02:00
|
|
|
FILE *f = io_stream_output(io);
|
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
for (i = 0; i < cnt; i++) {
|
2018-05-12 18:01:48 +02:00
|
|
|
ret = json_pack_sample(io, &json, smps[i]);
|
2017-08-05 21:02:09 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = json_dumpf(json, f, 0);
|
2018-05-24 10:46:31 +02:00
|
|
|
fputc(io->delimiter, f);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
|
|
|
json_decref(json);
|
2018-05-12 18:02:37 +02:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-08-05 21:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
2017-07-28 18:11:52 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 15:25:47 +02:00
|
|
|
int json_scan(struct io *io, struct sample *smps[], unsigned cnt)
|
2017-07-28 18:11:52 +02:00
|
|
|
{
|
2019-04-07 15:13:40 +02:00
|
|
|
int ret;
|
|
|
|
unsigned i;
|
2017-08-05 21:02:09 +02:00
|
|
|
json_t *json;
|
|
|
|
json_error_t err;
|
|
|
|
|
2018-05-12 15:25:47 +02:00
|
|
|
FILE *f = io_stream_input(io);
|
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
for (i = 0; i < cnt; i++) {
|
2018-10-22 13:11:05 +01:00
|
|
|
if (feof(f))
|
|
|
|
return -1;
|
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
skip: json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err);
|
|
|
|
if (!json)
|
|
|
|
break;
|
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
ret = json_unpack_sample(io, json, smps[i]);
|
2017-08-05 21:02:09 +02:00
|
|
|
if (ret)
|
|
|
|
goto skip;
|
|
|
|
|
|
|
|
json_decref(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
2017-07-28 18:11:52 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 14:27:55 +02:00
|
|
|
static struct plugin p;
|
|
|
|
|
|
|
|
__attribute__((constructor(110))) static void UNIQUE(__ctor)() {
|
|
|
|
p.name = "json";
|
|
|
|
p.description = "Javascript Object Notation";
|
2019-06-23 16:13:23 +02:00
|
|
|
p.type = PluginType::FORMAT;
|
2019-04-09 14:27:55 +02:00
|
|
|
p.format.print = json_print;
|
|
|
|
p.format.scan = json_scan;
|
|
|
|
p.format.sprint = json_sprint;
|
|
|
|
p.format.sscan = json_sscan;
|
|
|
|
p.format.size = 0;
|
2019-06-23 16:13:23 +02:00
|
|
|
p.format.flags = (int) SampleFlags::HAS_TS_ORIGIN | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA;
|
2019-04-09 14:27:55 +02:00
|
|
|
p.format.delimiter = '\n';
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
vlist_init_and_push(&plugins, &p);
|
2019-04-09 14:27:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((destructor(110))) static void UNIQUE(__dtor)() {
|
2020-06-16 02:35:34 +02:00
|
|
|
vlist_remove_all(&plugins, &p);
|
2019-04-09 14:27:55 +02:00
|
|
|
}
|