1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/hook.c

236 lines
4.8 KiB
C
Raw Normal View History

/** Hook-releated functions.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2019-01-13 00:42:39 +01:00
* @copyright 2014-2019, 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-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-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/>.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <string.h>
#include <math.h>
2017-12-09 02:19:28 +08:00
#include <villas/timing.h>
#include <villas/node/config.h>
2017-12-09 02:19:28 +08:00
#include <villas/hook.h>
#include <villas/path.h>
#include <villas/utils.h>
#include <villas/node.h>
#include <villas/plugin.h>
const char *hook_reasons[] = {
"ok", "error", "skip-sample", "stop-processing"
};
int hook_init(struct hook *h, struct hook_type *vt, struct path *p, struct node *n)
{
2017-03-17 01:08:48 -03:00
int ret;
assert(h->state == STATE_DESTROYED);
h->enabled = 1;
h->priority = vt->priority;
2017-03-27 12:26:11 +02:00
h->path = p;
h->node = n;
h->signals.state = STATE_DESTROYED;
ret = signal_list_init(&h->signals);
if (ret)
return ret;
h->_vt = vt;
h->_vd = alloc(vt->size);
2017-03-27 12:26:11 +02:00
2019-02-24 11:06:24 +01:00
ret = hook_type(h)->init ? hook_type(h)->init(h) : 0;
2017-03-17 01:08:48 -03:00
if (ret)
return ret;
// We dont need to parse builtin hooks
h->state = hook_type(h)->flags & HOOK_BUILTIN ? STATE_PARSED : STATE_INITIALIZED;
return 0;
}
int hook_prepare(struct hook *h, struct vlist *signals)
{
int ret;
assert(h->state == STATE_PARSED);
if (!h->enabled)
return 0;
ret = signal_list_copy(&h->signals, signals);
if (ret)
return -1;
ret = hook_type(h)->init_signals ? hook_type(h)->init_signals(h) : 0;
if (ret)
return ret;
h->state = STATE_PREPARED;
2017-03-17 01:08:48 -03:00
return 0;
}
int hook_parse(struct hook *h, json_t *cfg)
{
2017-03-17 01:08:48 -03:00
int ret;
json_error_t err;
2017-03-17 01:08:48 -03:00
assert(h->state != STATE_DESTROYED);
ret = json_unpack_ex(cfg, &err, 0, "{ s?: i, s?: b }",
"priority", &h->priority,
"enabled", &h->enabled
);
if (ret)
2019-02-24 11:06:24 +01:00
jerror(&err, "Failed to parse configuration of hook '%s'", hook_type_name(hook_type(h)));
2017-03-27 12:26:11 +02:00
2019-02-24 11:06:24 +01:00
ret = hook_type(h)->parse ? hook_type(h)->parse(h, cfg) : 0;
2017-03-27 12:26:11 +02:00
if (ret)
return ret;
h->cfg = cfg;
2017-03-17 01:08:48 -03:00
h->state = STATE_PARSED;
2017-03-17 01:08:48 -03:00
return 0;
}
2017-03-17 01:08:48 -03:00
int hook_destroy(struct hook *h)
{
2017-03-17 01:08:48 -03:00
int ret;
assert(h->state != STATE_DESTROYED && h->state != STATE_STARTED);
ret = signal_list_destroy(&h->signals);
if (ret)
return ret;
2019-02-24 11:06:24 +01:00
ret = hook_type(h)->destroy ? hook_type(h)->destroy(h) : 0;
2017-03-17 01:08:48 -03:00
if (ret)
return ret;
if (h->_vd)
free(h->_vd);
2017-03-27 12:26:11 +02:00
h->state = STATE_DESTROYED;
return 0;
}
2017-03-27 12:26:11 +02:00
int hook_start(struct hook *h)
{
int ret;
assert(h->state == STATE_PREPARED);
if (!h->enabled)
return 0;
debug(LOG_HOOK | 10, "Start hook %s: priority=%d", hook_type_name(hook_type(h)), h->priority);
2017-07-09 19:35:15 +02:00
ret = hook_type(h)->start ? hook_type(h)->start(h) : 0;
if (ret)
return ret;
h->state = STATE_STARTED;
return 0;
2017-03-27 12:26:11 +02:00
}
int hook_stop(struct hook *h)
{
int ret;
assert(h->state == STATE_STARTED);
if (!h->enabled)
return 0;
debug(LOG_HOOK | 10, "Stopping hook %s: priority=%d", hook_type_name(hook_type(h)), h->priority);
2017-07-09 19:35:15 +02:00
ret = hook_type(h)->stop ? hook_type(h)->stop(h) : 0;
if (ret)
return ret;
h->state = STATE_STOPPED;
return 0;
2017-03-27 12:26:11 +02:00
}
int hook_periodic(struct hook *h)
{
assert(h->state == STATE_STARTED);
if (!h->enabled)
return 0;
2019-02-24 11:06:24 +01:00
if (hook_type(h)->periodic) {
debug(LOG_HOOK | 10, "Periodic hook %s: priority=%d", hook_type_name(hook_type(h)), h->priority);
2017-07-09 19:35:15 +02:00
2019-02-24 11:06:24 +01:00
return hook_type(h)->periodic(h);
2017-09-04 14:32:48 +02:00
}
else
return 0;
2017-03-27 12:26:11 +02:00
}
int hook_restart(struct hook *h)
{
int ret;
assert(h->state == STATE_STARTED);
if (!h->enabled)
return 0;
debug(LOG_HOOK | 10, "Restarting hook %s: priority=%d", hook_type_name(hook_type(h)), h->priority);
ret = hook_type(h)->restart ? hook_type(h)->restart(h) : 0;
if (ret)
return ret;
return 0;
2017-03-27 12:26:11 +02:00
}
int hook_process(struct hook *h, struct sample *smp)
{
int ret;
assert(h->state == STATE_STARTED);
if (!h->enabled)
return 0;
ret = hook_type(h)->process ? hook_type(h)->process(h, smp) : 0;
if (ret)
return ret;
debug(LOG_HOOK | 10, "Hook %s processed: priority=%d, return=%s", hook_type_name(hook_type(h)), h->priority, hook_reasons[ret]);
return 0;
}
int hook_cmp_priority(const void *a, const void *b)
2017-03-27 12:26:11 +02:00
{
struct hook *ha = (struct hook *) a;
struct hook *hb = (struct hook *) b;
2017-03-27 12:26:11 +02:00
return ha->priority - hb->priority;
}
2017-03-27 12:26:11 +02:00
const char * hook_type_name(struct hook_type *vt)
{
return plugin_name(vt);
}