From 77850451b48dbf2cd5553eef266bb09d4f98c99f Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 17 Aug 2018 10:51:23 +0200 Subject: [PATCH] hooks: add new builtin "fix" hook which adds missing fields to samples --- lib/hooks/CMakeLists.txt | 1 + lib/hooks/fix.c | 75 ++++++++++++++++++++++++++++++++++++++++ lib/node.c | 18 ---------- 3 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 lib/hooks/fix.c diff --git a/lib/hooks/CMakeLists.txt b/lib/hooks/CMakeLists.txt index 7b51192a6..86cf1ddb3 100644 --- a/lib/hooks/CMakeLists.txt +++ b/lib/hooks/CMakeLists.txt @@ -32,6 +32,7 @@ set(HOOK_SRC ts.c limit_rate.c scale.c + fix.c ) if(WITH_IO) diff --git a/lib/hooks/fix.c b/lib/hooks/fix.c new file mode 100644 index 000000000..347ecf8c6 --- /dev/null +++ b/lib/hooks/fix.c @@ -0,0 +1,75 @@ + +/** Drop hook. + * + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + * @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. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *********************************************************************************/ + +/** @addtogroup hooks Hook functions + * @{ + */ + +#include + +#include +#include +#include +#include +#include + +static int fix_read(struct hook *h, struct sample *smps[], unsigned *cnt) +{ + struct timespec now = time_now(); + + for (int i = 0; i < *cnt; i++) { + struct sample *smp = smps[i]; + + if (!(smp->flags & SAMPLE_HAS_SEQUENCE) && h->node) { + smp->sequence = h->node->sequence++; + smp->flags |= SAMPLE_HAS_SEQUENCE; + } + + if (!(smp->flags & SAMPLE_HAS_TS_RECEIVED)) { + smp->ts.received = now; + smp->flags |= SAMPLE_HAS_TS_RECEIVED; + } + + if (!(smp->flags & SAMPLE_HAS_TS_ORIGIN)) { + smp->ts.origin = smp->ts.received; + smp->flags |= SAMPLE_HAS_TS_ORIGIN; + } + } + + return 0; +} + +static struct plugin p = { + .name = "fix", + .description = "Fix received data by adding missing fields", + .type = PLUGIN_TYPE_HOOK, + .hook = { + .flags = HOOK_BUILTIN | HOOK_NODE, + .priority = 1, + .read = fix_read + } +}; + +REGISTER_PLUGIN(&p) + +/** @} */ diff --git a/lib/node.c b/lib/node.c index 8725a0ed3..29b8dd4cb 100644 --- a/lib/node.c +++ b/lib/node.c @@ -409,24 +409,6 @@ int node_read(struct node *n, struct sample *smps[], unsigned cnt, unsigned *rel return nread; } - /* Add missing fields */ - for (int i = 0; i < nread; i++) { - if (!(smps[i]->flags & SAMPLE_HAS_SEQUENCE)) - smps[i]->sequence = n->sequence++; - - if (!(smps[i]->flags & SAMPLE_HAS_ORIGIN) || - !(smps[i]->flags & SAMPLE_HAS_RECEIVED)) { - - struct timespec now = time_now(); - - if (!(smps[i]->flags & SAMPLE_HAS_RECEIVED)) - smps[i]->ts.received = now; - - if (!(smps[i]->flags & SAMPLE_HAS_ORIGIN)) - smps[i]->ts.origin = now; - } - } - #ifdef WITH_HOOKS /* Run read hooks */ int rread = hook_read_list(&n->in.hooks, smps, nread);