2017-03-27 12:28:13 +02:00
|
|
|
/** The internal datastructure for a sample of simulation data.
|
|
|
|
*
|
|
|
|
* @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-03-27 12:28:13 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstring>
|
2017-03-27 12:28:13 +02:00
|
|
|
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/sample.h>
|
2018-05-12 18:02:18 +02:00
|
|
|
#include <villas/signal.h>
|
2021-05-10 00:12:30 +02:00
|
|
|
#include <villas/formats/villas_human.hpp>
|
2017-07-25 11:57:01 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
using namespace villas::node;
|
|
|
|
|
|
|
|
size_t VILLASHumanFormat::sprintLine(char *buf, size_t len, const struct sample *smp)
|
2017-03-27 12:28:13 +02:00
|
|
|
{
|
2017-09-04 14:28:55 +02:00
|
|
|
size_t off = 0;
|
2018-08-20 18:30:24 +02:00
|
|
|
struct signal *sig;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_TS_ORIGIN) {
|
2019-06-23 16:13:23 +02:00
|
|
|
if (smp->flags & (int) SampleFlags::HAS_TS_ORIGIN) {
|
2018-10-20 16:23:57 +02:00
|
|
|
off += snprintf(buf + off, len - off, "%llu", (unsigned long long) smp->ts.origin.tv_sec);
|
|
|
|
off += snprintf(buf + off, len - off, ".%09llu", (unsigned long long) smp->ts.origin.tv_nsec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
off += snprintf(buf + off, len - off, "nan.nan");
|
2017-09-04 14:28:55 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_OFFSET) {
|
2019-06-23 16:13:23 +02:00
|
|
|
if (smp->flags & (int) SampleFlags::HAS_TS_RECEIVED)
|
2018-10-20 16:23:57 +02:00
|
|
|
off += snprintf(buf + off, len - off, "%+e", time_delta(&smp->ts.origin, &smp->ts.received));
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_SEQUENCE) {
|
2020-09-11 14:57:05 +02:00
|
|
|
if (smp->flags & (int) SampleFlags::HAS_SEQUENCE)
|
2018-10-20 16:23:57 +02:00
|
|
|
off += snprintf(buf + off, len - off, "(%" PRIu64 ")", smp->sequence);
|
|
|
|
}
|
2018-08-20 18:30:24 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_DATA) {
|
2019-04-07 15:13:40 +02:00
|
|
|
for (unsigned i = 0; i < smp->length; i++) {
|
|
|
|
sig = (struct signal *) vlist_at_safe(smp->signals, i);
|
2018-08-20 18:30:24 +02:00
|
|
|
if (!sig)
|
|
|
|
break;
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
off += snprintf(buf + off, len - off, "\t");
|
|
|
|
off += signal_data_print_str(&smp->data[i], sig->type, buf + off, len - off, real_precision);
|
2017-03-27 12:28:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
off += snprintf(buf + off, len - off, "%c", delimiter);
|
2017-03-27 12:28:13 +02:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
return off;
|
2017-03-27 12:28:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
size_t VILLASHumanFormat::sscanLine(const char *buf, size_t len, struct sample *smp)
|
2017-03-27 12:28:13 +02:00
|
|
|
{
|
2018-08-20 18:30:24 +02:00
|
|
|
int ret;
|
2019-03-09 13:34:51 +01:00
|
|
|
char *end;
|
2017-08-05 21:02:09 +02:00
|
|
|
const char *ptr = buf;
|
2017-03-27 12:28:13 +02:00
|
|
|
|
|
|
|
double offset = 0;
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->flags = 0;
|
2021-05-10 00:12:30 +02:00
|
|
|
smp->signals = signals;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-05-05 19:24:16 +00:00
|
|
|
/* Format: Seconds.NanoSeconds+Offset(SequenceNumber) Value1 Value2 ...
|
2017-03-27 12:28:13 +02:00
|
|
|
* RegEx: (\d+(?:\.\d+)?)([-+]\d+(?:\.\d+)?(?:e[+-]?\d+)?)?(?:\((\d+)\))?
|
|
|
|
*
|
|
|
|
* Please note that only the seconds and at least one value are mandatory
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Mandatory: seconds */
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->ts.origin.tv_sec = (uint32_t) strtoul(ptr, &end, 10);
|
2021-05-10 00:12:30 +02:00
|
|
|
if (ptr == end || *end == delimiter)
|
2017-08-22 12:21:17 +02:00
|
|
|
return -1;
|
2017-03-27 12:28:13 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
/* Optional: nano seconds */
|
|
|
|
if (*end == '.') {
|
|
|
|
ptr = end + 1;
|
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->ts.origin.tv_nsec = (uint32_t) strtoul(ptr, &end, 10);
|
2017-09-04 14:28:55 +02:00
|
|
|
if (ptr == end)
|
2017-03-27 12:28:13 +02:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
else
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->ts.origin.tv_nsec = 0;
|
2017-03-27 12:28:13 +02:00
|
|
|
|
|
|
|
/* Optional: offset / delay */
|
|
|
|
if (*end == '+' || *end == '-') {
|
|
|
|
ptr = end;
|
|
|
|
|
|
|
|
offset = strtof(ptr, &end); /* offset is ignored for now */
|
|
|
|
if (ptr != end)
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_OFFSET;
|
2017-03-27 12:28:13 +02:00
|
|
|
else
|
|
|
|
return -4;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
/* Optional: sequence */
|
|
|
|
if (*end == '(') {
|
|
|
|
ptr = end + 1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->sequence = strtoul(ptr, &end, 10);
|
2017-03-27 12:28:13 +02:00
|
|
|
if (ptr != end)
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_SEQUENCE;
|
2017-03-27 12:28:13 +02:00
|
|
|
else
|
|
|
|
return -5;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
if (*end == ')')
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
unsigned i;
|
2018-08-20 18:30:24 +02:00
|
|
|
for (ptr = end + 1, i = 0; i < smp->capacity; ptr = end + 1, i++) {
|
2021-05-10 00:12:30 +02:00
|
|
|
if (*end == delimiter)
|
2018-08-20 18:30:24 +02:00
|
|
|
goto out;
|
2017-03-27 12:28:13 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
struct signal *sig = (struct signal *) vlist_at_safe(signals, i);
|
2018-08-20 18:30:24 +02:00
|
|
|
if (!sig)
|
|
|
|
goto out;
|
|
|
|
|
2021-02-19 06:37:21 +01:00
|
|
|
ret = signal_data_parse_str(&smp->data[i], sig->type, ptr, &end);
|
2018-08-20 18:30:24 +02:00
|
|
|
if (ret || end == ptr) /* There are no valid values anymore. */
|
|
|
|
goto out;
|
2017-03-27 12:28:13 +02:00
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
out: if (*end == delimiter)
|
2017-08-22 12:21:17 +02:00
|
|
|
end++;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->length = i;
|
|
|
|
if (smp->length > 0)
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_DATA;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (smp->flags & (int) SampleFlags::HAS_OFFSET) {
|
2017-03-27 12:28:13 +02:00
|
|
|
struct timespec off = time_from_double(offset);
|
2018-08-20 18:30:24 +02:00
|
|
|
smp->ts.received = time_add(&smp->ts.origin, &off);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_RECEIVED;
|
2017-03-27 12:28:13 +02:00
|
|
|
}
|
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
return end - buf;
|
2017-03-27 12:28:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
void VILLASHumanFormat::header(FILE *f, const struct vlist *sigs)
|
2017-03-27 12:28:13 +02:00
|
|
|
{
|
2021-05-10 00:12:30 +02:00
|
|
|
/* Abort if we are not supposed to, or have already printed the header */
|
|
|
|
if (!print_header || header_printed)
|
|
|
|
return;
|
2017-09-04 16:02:43 +02:00
|
|
|
|
2018-10-20 16:23:57 +02:00
|
|
|
fprintf(f, "# ");
|
2018-05-12 18:02:18 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_TS_ORIGIN)
|
2018-10-20 16:23:57 +02:00
|
|
|
fprintf(f, "seconds.nanoseconds");
|
2018-05-12 18:02:18 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_OFFSET)
|
2018-10-20 16:23:57 +02:00
|
|
|
fprintf(f, "+offset");
|
2018-05-12 18:02:18 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_SEQUENCE)
|
2018-10-20 16:23:57 +02:00
|
|
|
fprintf(f, "(sequence)");
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (flags & (int) SampleFlags::HAS_DATA) {
|
|
|
|
for (unsigned i = 0; i < vlist_length(sigs); i++) {
|
|
|
|
struct signal *sig = (struct signal *) vlist_at_safe(sigs, i);
|
2020-07-06 15:07:05 +02:00
|
|
|
if (!sig)
|
|
|
|
break;
|
2018-10-20 16:23:57 +02:00
|
|
|
|
|
|
|
if (sig->name)
|
2021-05-10 00:12:30 +02:00
|
|
|
fprintf(f, "\t%s", sig->name);
|
2018-10-20 16:23:57 +02:00
|
|
|
else
|
2021-05-10 00:12:30 +02:00
|
|
|
fprintf(f, "\tsignal%u", i);
|
2018-10-20 16:23:57 +02:00
|
|
|
|
|
|
|
if (sig->unit)
|
|
|
|
fprintf(f, "[%s]", sig->unit);
|
|
|
|
}
|
2018-05-12 18:02:18 +02:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
fprintf(f, "%c", delimiter);
|
2017-09-04 16:02:43 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
LineFormat::header(f, sigs);
|
2019-04-09 14:27:55 +02:00
|
|
|
}
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
static char n[] = "villas.human";
|
|
|
|
static char d[] = "VILLAS human readable format";
|
|
|
|
static LineFormatPlugin<VILLASHumanFormat, n, d, (int) SampleFlags::HAS_TS_ORIGIN | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA, '\n'> p;
|