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>
|
|
|
|
* @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-03-27 12:28:13 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2017-09-04 16:02:43 +02:00
|
|
|
#include <stdbool.h>
|
2017-03-27 12:28:13 +02:00
|
|
|
#include <ctype.h>
|
2017-08-14 14:42:07 +02:00
|
|
|
#include <inttypes.h>
|
2017-07-28 18:11:52 +02:00
|
|
|
#include <string.h>
|
2017-03-27 12:28:13 +02:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/io.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/timing.h>
|
|
|
|
#include <villas/sample.h>
|
|
|
|
#include <villas/io/villas_human.h>
|
2017-07-25 11:57:01 +02:00
|
|
|
|
2017-09-04 16:02:43 +02:00
|
|
|
struct villas_human {
|
|
|
|
bool header_written;
|
|
|
|
};
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
size_t villas_human_sprint_single(char *buf, size_t len, struct sample *s, int flags)
|
2017-03-27 12:28:13 +02:00
|
|
|
{
|
2017-09-04 14:28:55 +02:00
|
|
|
size_t off = 0;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
if (flags & SAMPLE_HAS_ORIGIN) {
|
2017-09-04 14:28:55 +02:00
|
|
|
off += snprintf(buf + off, len - off, "%llu", (unsigned long long) s->ts.origin.tv_sec);
|
2017-03-27 12:28:13 +02:00
|
|
|
off += snprintf(buf + off, len - off, ".%09llu", (unsigned long long) s->ts.origin.tv_nsec);
|
2017-09-04 14:28:55 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
if (flags & SAMPLE_HAS_RECEIVED)
|
2017-03-27 12:28:13 +02:00
|
|
|
off += snprintf(buf + off, len - off, "%+e", time_delta(&s->ts.origin, &s->ts.received));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
if (flags & SAMPLE_HAS_SEQUENCE)
|
2017-03-27 12:28:13 +02:00
|
|
|
off += snprintf(buf + off, len - off, "(%u)", s->sequence);
|
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
if (flags & SAMPLE_HAS_VALUES) {
|
2017-03-27 12:28:13 +02:00
|
|
|
for (int i = 0; i < s->length; i++) {
|
2017-09-04 23:03:00 +02:00
|
|
|
switch (sample_get_data_format(s, i)) {
|
2017-03-27 12:28:13 +02:00
|
|
|
case SAMPLE_DATA_FORMAT_FLOAT:
|
2017-08-14 14:42:07 +02:00
|
|
|
off += snprintf(buf + off, len - off, "\t%.6lf", s->data[i].f);
|
2017-03-27 12:28:13 +02:00
|
|
|
break;
|
|
|
|
case SAMPLE_DATA_FORMAT_INT:
|
2017-08-14 14:42:07 +02:00
|
|
|
off += snprintf(buf + off, len - off, "\t%" PRIi64, s->data[i].i);
|
2017-03-27 12:28:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
off += snprintf(buf + off, len - off, "\n");
|
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
return off;
|
2017-03-27 12:28:13 +02:00
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
size_t villas_human_sscan_single(const char *buf, size_t len, struct sample *s, int flags)
|
2017-03-27 12:28:13 +02: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;
|
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
s->flags = 0;
|
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 */
|
|
|
|
s->ts.origin.tv_sec = (uint32_t) strtoul(ptr, &end, 10);
|
2017-08-22 12:21:17 +02:00
|
|
|
if (ptr == end || *end == '\n')
|
|
|
|
return -1;
|
2017-03-27 12:28:13 +02:00
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
s->flags |= SAMPLE_HAS_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;
|
|
|
|
|
|
|
|
s->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
|
|
|
|
s->ts.origin.tv_nsec = 0;
|
|
|
|
|
|
|
|
/* Optional: offset / delay */
|
|
|
|
if (*end == '+' || *end == '-') {
|
|
|
|
ptr = end;
|
|
|
|
|
|
|
|
offset = strtof(ptr, &end); /* offset is ignored for now */
|
|
|
|
if (ptr != end)
|
2017-09-16 15:04:59 +02:00
|
|
|
s->flags |= SAMPLE_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
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
s->sequence = strtoul(ptr, &end, 10);
|
|
|
|
if (ptr != end)
|
2017-09-16 15:04:59 +02:00
|
|
|
s->flags |= SAMPLE_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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ptr = end, s->length = 0;
|
|
|
|
s->length < s->capacity;
|
|
|
|
ptr = end, s->length++) {
|
2017-08-22 12:21:17 +02:00
|
|
|
if (*end == '\n')
|
|
|
|
break;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
switch (s->format & (1 << s->length)) {
|
|
|
|
case SAMPLE_DATA_FORMAT_FLOAT:
|
|
|
|
s->data[s->length].f = strtod(ptr, &end);
|
|
|
|
break;
|
|
|
|
case SAMPLE_DATA_FORMAT_INT:
|
|
|
|
s->data[s->length].i = strtol(ptr, &end, 10);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
/* There are no valid FP values anymore. */
|
|
|
|
if (end == ptr)
|
2017-03-27 12:28:13 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-22 12:21:17 +02:00
|
|
|
if (*end == '\n')
|
|
|
|
end++;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
if (s->length > 0)
|
2017-09-16 15:04:59 +02:00
|
|
|
s->flags |= SAMPLE_HAS_VALUES;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
if (s->flags & SAMPLE_HAS_OFFSET) {
|
2017-03-27 12:28:13 +02:00
|
|
|
struct timespec off = time_from_double(offset);
|
|
|
|
s->ts.received = time_add(&s->ts.origin, &off);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-09-16 15:04:59 +02:00
|
|
|
s->flags |= SAMPLE_HAS_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
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
int villas_human_sprint(char *buf, size_t len, size_t *wbytes, struct sample *smps[], unsigned cnt, int flags)
|
2017-03-27 12:28:13 +02:00
|
|
|
{
|
2017-08-14 14:42:07 +02:00
|
|
|
int i;
|
2017-08-05 21:02:09 +02:00
|
|
|
size_t off = 0;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
for (i = 0; i < cnt && off < len; i++)
|
2017-09-04 16:19:27 +02:00
|
|
|
off += villas_human_sprint_single(buf + off, len - off, smps[i], flags);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
if (wbytes)
|
|
|
|
*wbytes = off;
|
2017-03-27 12:28:13 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
return i;
|
2017-08-05 21:02:09 +02:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
int villas_human_sscan(char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt, int flags)
|
2017-08-05 21:02:09 +02:00
|
|
|
{
|
2017-08-14 14:42:07 +02:00
|
|
|
int i;
|
2017-08-05 21:02:09 +02:00
|
|
|
size_t off = 0;
|
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
for (i = 0; i < cnt && off < len; i++)
|
2017-09-04 16:19:27 +02:00
|
|
|
off += villas_human_sscan_single(buf + off, len - off, smps[i], flags);
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
if (rbytes)
|
|
|
|
*rbytes = off;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
return i;
|
2017-03-27 12:28:13 +02:00
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
int villas_human_fscan_single(FILE *f, struct sample *s, int flags)
|
2017-03-27 12:28:13 +02:00
|
|
|
{
|
|
|
|
char *ptr, line[4096];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
skip: if (fgets(line, sizeof(line), f) == NULL)
|
|
|
|
return -1; /* An error occured */
|
|
|
|
|
|
|
|
/* Skip whitespaces, empty and comment lines */
|
|
|
|
for (ptr = line; isspace(*ptr); ptr++);
|
|
|
|
if (*ptr == '\0' || *ptr == '#')
|
|
|
|
goto skip;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
return villas_human_sscan_single(line, strlen(line), s, flags);
|
2017-08-05 21:02:09 +02:00
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
int villas_human_fprint_single(FILE *f, struct sample *s, int flags)
|
2017-08-05 21:02:09 +02:00
|
|
|
{
|
2017-08-14 14:42:07 +02:00
|
|
|
int ret;
|
2017-08-05 21:02:09 +02:00
|
|
|
char line[4096];
|
2017-08-14 14:42:07 +02:00
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
ret = villas_human_sprint_single(line, sizeof(line), s, flags);
|
2017-08-14 14:42:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2017-09-04 14:28:55 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
fputs(line, f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
int villas_human_fprint(FILE *f, struct sample *smps[], unsigned cnt, int flags)
|
2017-08-14 14:42:07 +02:00
|
|
|
{
|
2017-08-05 21:02:09 +02:00
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++) {
|
2017-09-04 16:19:27 +02:00
|
|
|
ret = villas_human_fprint_single(f, smps[i], flags);
|
2017-08-05 21:02:09 +02:00
|
|
|
if (ret < 0)
|
2017-08-14 14:42:07 +02:00
|
|
|
return ret;
|
2017-08-05 21:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
int villas_human_print(struct io *io, struct sample *smps[], unsigned cnt)
|
2017-09-04 16:02:43 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct villas_human *h = (struct villas_human *) io->_vd;
|
2017-09-04 16:02:43 +02:00
|
|
|
|
|
|
|
FILE *f = io->mode == IO_MODE_ADVIO
|
2018-05-12 10:41:40 +02:00
|
|
|
? io->output.stream.adv->file
|
|
|
|
: io->output.stream.std;
|
2017-09-04 16:02:43 +02:00
|
|
|
|
|
|
|
if (!h->header_written) {
|
|
|
|
fprintf(f, "# %-20s\t\t%s\n", "sec.nsec+offset", "data[]");
|
|
|
|
|
|
|
|
if (io->flags & IO_FLUSH)
|
|
|
|
io_flush(io);
|
|
|
|
|
|
|
|
h->header_written = true;
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
return villas_human_fprint(f, smps, cnt, io->flags);
|
2017-09-04 16:02:43 +02:00
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
int villas_human_fscan(FILE *f, struct sample *smps[], unsigned cnt, int flags)
|
2017-08-05 21:02:09 +02:00
|
|
|
{
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++) {
|
2017-09-04 16:19:27 +02:00
|
|
|
ret = villas_human_fscan_single(f, smps[i], flags);
|
2017-08-05 21:02:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
int villas_human_open(struct io *io, const char *uri)
|
2017-08-05 21:02:09 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct villas_human *h = (struct villas_human *) io->_vd;
|
2017-08-05 21:02:09 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
ret = io_stream_open(io, uri);
|
2017-08-05 21:02:09 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-09-04 16:02:43 +02:00
|
|
|
h->header_written = false;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-09-04 16:02:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-09-04 16:19:27 +02:00
|
|
|
void villas_human_rewind(struct io *io)
|
2017-09-04 16:02:43 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct villas_human *h = (struct villas_human *) io->_vd;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-09-04 16:02:43 +02:00
|
|
|
h->header_written = false;
|
|
|
|
|
|
|
|
io_stream_rewind(io);
|
2017-03-27 12:28:13 +02:00
|
|
|
}
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
static struct plugin p = {
|
2018-04-09 08:28:54 +02:00
|
|
|
.name = "villas.human",
|
2017-08-05 21:02:09 +02:00
|
|
|
.description = "VILLAS human readable format",
|
2017-08-14 14:42:07 +02:00
|
|
|
.type = PLUGIN_TYPE_IO,
|
2017-07-28 18:11:52 +02:00
|
|
|
.io = {
|
2017-09-04 16:19:27 +02:00
|
|
|
.open = villas_human_open,
|
|
|
|
.rewind = villas_human_rewind,
|
|
|
|
.print = villas_human_print,
|
|
|
|
.fprint = villas_human_fprint,
|
|
|
|
.fscan = villas_human_fscan,
|
|
|
|
.sprint = villas_human_sprint,
|
|
|
|
.sscan = villas_human_sscan,
|
2017-09-04 16:02:43 +02:00
|
|
|
.size = sizeof(struct villas_human)
|
2017-07-28 18:11:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_PLUGIN(&p);
|