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>
|
|
|
|
* @copyright 2017, 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>
|
|
|
|
#include <villas/compat.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>
|
2017-04-15 20:38:58 +02:00
|
|
|
|
2018-05-09 09:17:40 +02:00
|
|
|
static json_t * json_pack_timestamps(struct sample *smp)
|
|
|
|
{
|
|
|
|
json_t *json_ts = json_object();
|
|
|
|
|
|
|
|
if (smp->flags & SAMPLE_HAS_ORIGIN)
|
|
|
|
json_object_set(json_ts, "origin", json_pack("[ I, I ]", smp->ts.origin.tv_sec, smp->ts.origin.tv_nsec));
|
|
|
|
|
|
|
|
if (smp->flags & SAMPLE_HAS_RECEIVED)
|
|
|
|
json_object_set(json_ts, "received", json_pack("[ I, I ]", smp->ts.received.tv_sec, smp->ts.received.tv_nsec));
|
|
|
|
|
|
|
|
return json_ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int json_unpack_timestamps(json_t *json_ts, struct sample *smp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
json_t *json_ts_origin = NULL, *json_ts_received = NULL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
smp->flags |= SAMPLE_HAS_ORIGIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
smp->flags |= SAMPLE_HAS_RECEIVED;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-09 09:17:40 +02:00
|
|
|
json_smp = json_pack_ex(&err, 0, "{ s: o }", "ts", json_pack_timestamps(smp));
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
if (smp->flags & SAMPLE_HAS_SEQUENCE) {
|
2017-08-22 12:21:17 +02:00
|
|
|
json_t *json_sequence = json_integer(smp->sequence);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +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
|
|
|
|
2018-05-12 18:01:48 +02:00
|
|
|
if (smp->flags & SAMPLE_HAS_VALUES) {
|
2017-08-22 12:21:17 +02:00
|
|
|
json_t *json_data = json_array();
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
for (int i = 0; i < smp->length; i++) {
|
|
|
|
json_t *json_value = sample_get_data_format(smp, i)
|
|
|
|
? json_integer(smp->data[i].i)
|
|
|
|
: json_real(smp->data[i].f);
|
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
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
for (int i = 0; i < cnt; i++) {
|
|
|
|
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;
|
|
|
|
json_t *json_data, *json_value, *json_ts = NULL;
|
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-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;
|
|
|
|
smp->flags |= SAMPLE_HAS_SEQUENCE;
|
|
|
|
}
|
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
|
|
|
|
2017-04-15 20:38:58 +02:00
|
|
|
switch (json_typeof(json_value)) {
|
|
|
|
case JSON_REAL:
|
2017-08-22 12:21:17 +02:00
|
|
|
smp->data[i].f = json_real_value(json_value);
|
|
|
|
sample_set_data_format(smp, i, SAMPLE_DATA_FORMAT_FLOAT);
|
2017-04-15 20:38:58 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_INTEGER:
|
2017-08-22 12:21:17 +02:00
|
|
|
smp->data[i].f = json_integer_value(json_value);
|
|
|
|
sample_set_data_format(smp, i, SAMPLE_DATA_FORMAT_INT);
|
2017-04-15 20:38:58 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-08-22 12:21:17 +02:00
|
|
|
return -2;
|
2017-04-15 20:38:58 +02:00
|
|
|
}
|
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)
|
2017-09-16 15:04:59 +02:00
|
|
|
smp->flags |= SAMPLE_HAS_VALUES;
|
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-05-12 18:01:48 +02:00
|
|
|
int json_sscan(struct io *io, 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
|
|
|
{
|
2017-08-05 21:02:09 +02:00
|
|
|
int ret, i;
|
|
|
|
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
|
|
|
{
|
2017-08-05 21:02:09 +02:00
|
|
|
int i, ret;
|
|
|
|
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++) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static struct plugin p = {
|
|
|
|
.name = "json",
|
|
|
|
.description = "Javascript Object Notation",
|
2018-05-12 13:56:12 +02:00
|
|
|
.type = PLUGIN_TYPE_FORMAT,
|
2018-05-13 13:51:28 +02:00
|
|
|
.format = {
|
2018-05-12 15:25:47 +02:00
|
|
|
.scan = json_scan,
|
|
|
|
.print = json_print,
|
2017-08-14 14:42:07 +02:00
|
|
|
.sscan = json_sscan,
|
|
|
|
.sprint = json_sprint,
|
2018-05-24 10:46:31 +02:00
|
|
|
.size = 0,
|
|
|
|
.delimiter = '\n'
|
2017-07-28 18:11:52 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_PLUGIN(&p);
|