2016-06-08 22:38:50 +02:00
|
|
|
/** Hook-releated functions.
|
2014-08-31 14:43:28 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2016-02-09 05:33:19 +01:00
|
|
|
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
|
2016-06-08 23:21:42 +02:00
|
|
|
* This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential.
|
2015-08-07 01:11:43 +02:00
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2015-03-18 16:13:18 +01:00
|
|
|
|
2014-08-31 14:43:28 +00:00
|
|
|
#include <string.h>
|
2015-09-28 19:18:39 +02:00
|
|
|
#include <math.h>
|
2016-11-20 12:59:37 -05:00
|
|
|
#include <libconfig.h>
|
2014-08-31 14:43:28 +00:00
|
|
|
|
2015-06-10 15:05:36 +02:00
|
|
|
#include "timing.h"
|
2015-05-06 11:38:57 +02:00
|
|
|
#include "config.h"
|
2014-08-31 14:43:28 +00:00
|
|
|
#include "msg.h"
|
2016-11-20 13:11:37 -05:00
|
|
|
#include "hook.h"
|
2015-03-18 16:13:18 +01:00
|
|
|
#include "path.h"
|
|
|
|
#include "utils.h"
|
2015-12-13 00:42:59 +01:00
|
|
|
#include "node.h"
|
2017-03-05 10:06:32 -04:00
|
|
|
#include "plugin.h"
|
2015-06-10 15:10:51 +02:00
|
|
|
|
2017-03-12 17:01:24 -03:00
|
|
|
int hook_init(struct hook *h, struct super_node *sn)
|
2016-10-22 20:42:05 -04:00
|
|
|
{
|
|
|
|
struct hook_info i = {
|
2017-03-12 17:01:24 -03:00
|
|
|
.nodes = &sn->nodes,
|
|
|
|
.paths = &sn->paths
|
2016-10-22 20:42:05 -04:00
|
|
|
};
|
|
|
|
|
2016-11-20 02:50:12 -05:00
|
|
|
if (h->type & HOOK_INIT)
|
|
|
|
return h->cb(h, HOOK_INIT, &i);
|
|
|
|
else
|
|
|
|
return 0;
|
2016-10-22 20:42:05 -04:00
|
|
|
}
|
2016-01-15 15:34:28 +01:00
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
int hook_destroy(struct hook *h)
|
2016-01-15 15:34:28 +01:00
|
|
|
{
|
2016-10-22 20:42:05 -04:00
|
|
|
struct hook_info i = { NULL };
|
2016-11-20 03:46:53 -05:00
|
|
|
|
|
|
|
if (h->type & HOOK_DESTROY)
|
2017-03-05 10:06:32 -04:00
|
|
|
return h->cb(h, HOOK_DESTROY, &i);
|
|
|
|
else
|
|
|
|
return 0;
|
2016-10-22 20:42:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int hook_copy(struct hook *h, struct hook *c)
|
|
|
|
{
|
|
|
|
memcpy(c, h, sizeof(struct hook));
|
|
|
|
|
|
|
|
c->_vd =
|
|
|
|
c->prev =
|
|
|
|
c->last = NULL;
|
|
|
|
|
|
|
|
return 0;
|
2016-01-15 15:34:28 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
int hook_cmp_priority(const void *a, const void *b) {
|
2016-01-14 22:59:57 +01:00
|
|
|
struct hook *ha = (struct hook *) a;
|
|
|
|
struct hook *hb = (struct hook *) b;
|
|
|
|
|
|
|
|
return ha->priority - hb->priority;
|
|
|
|
}
|
|
|
|
|
2016-06-08 22:38:50 +02:00
|
|
|
int hook_run(struct path *p, struct sample *smps[], size_t cnt, int when)
|
2016-02-07 14:24:58 +01:00
|
|
|
{
|
2016-10-22 20:42:05 -04:00
|
|
|
struct hook_info i = {
|
|
|
|
.path = p,
|
|
|
|
.smps = smps,
|
|
|
|
.cnt = cnt
|
|
|
|
};
|
|
|
|
|
2016-02-07 14:24:58 +01:00
|
|
|
list_foreach(struct hook *h, &p->hooks) {
|
2016-06-08 22:38:50 +02:00
|
|
|
if (h->type & when) {
|
2016-10-22 20:42:05 -04:00
|
|
|
cnt = h->cb(h, when, &i);
|
2016-11-20 13:01:17 -05:00
|
|
|
|
2017-03-07 07:12:24 -04:00
|
|
|
debug(LOG_HOOK | 22, "Ran hook '%s' when=%u prio=%u, cnt=%zu", plugin_name(h), when, h->priority, cnt);
|
2016-11-20 13:01:17 -05:00
|
|
|
|
2016-06-08 22:38:50 +02:00
|
|
|
if (cnt == 0)
|
|
|
|
break;
|
2015-10-09 13:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-12 16:16:25 +02:00
|
|
|
|
2016-06-08 22:38:50 +02:00
|
|
|
return cnt;
|
2015-10-12 16:16:25 +02:00
|
|
|
}
|
|
|
|
|
2016-10-30 22:52:06 -04:00
|
|
|
void * hook_storage(struct hook *h, int when, size_t len, ctor_cb_t ctor, dtor_cb_t dtor)
|
2015-10-09 13:30:41 +02:00
|
|
|
{
|
|
|
|
switch (when) {
|
2016-06-08 22:38:50 +02:00
|
|
|
case HOOK_INIT:
|
|
|
|
h->_vd = alloc(len);
|
2016-10-30 22:52:06 -04:00
|
|
|
|
|
|
|
if (ctor)
|
|
|
|
ctor(h->_vd);
|
|
|
|
|
2015-10-09 13:30:41 +02:00
|
|
|
break;
|
|
|
|
|
2016-10-22 20:42:05 -04:00
|
|
|
case HOOK_DESTROY:
|
2016-10-30 22:52:06 -04:00
|
|
|
if (dtor)
|
|
|
|
dtor(h->_vd);
|
|
|
|
|
2016-06-08 22:38:50 +02:00
|
|
|
free(h->_vd);
|
|
|
|
h->_vd = NULL;
|
2015-10-09 13:30:41 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-08 22:38:50 +02:00
|
|
|
return h->_vd;
|
2016-11-20 12:59:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse an array or single hook function.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* hooks = [ "print", "fir" ]
|
|
|
|
* hooks = "log"
|
|
|
|
*/
|
2017-03-05 10:06:32 -04:00
|
|
|
int hook_parse_list(struct list *list, config_setting_t *cfg)
|
|
|
|
{
|
2016-11-20 12:59:37 -05:00
|
|
|
switch (config_setting_type(cfg)) {
|
|
|
|
case CONFIG_TYPE_STRING:
|
|
|
|
hook_parse(cfg, list);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONFIG_TYPE_ARRAY:
|
|
|
|
for (int i = 0; i < config_setting_length(cfg); i++)
|
|
|
|
hook_parse(config_setting_get_elem(cfg, i), list);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
cerror(cfg, "Invalid hook functions");
|
|
|
|
}
|
|
|
|
|
|
|
|
return list_length(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
int hook_parse(config_setting_t *cfg, struct list *list)
|
|
|
|
{
|
2017-03-05 10:06:32 -04:00
|
|
|
struct hook *hook;
|
|
|
|
struct plugin *plg;
|
|
|
|
|
2016-11-20 12:59:37 -05:00
|
|
|
char *name, *param;
|
|
|
|
const char *hookline = config_setting_get_string(cfg);
|
|
|
|
if (!hookline)
|
|
|
|
cerror(cfg, "Invalid hook function");
|
|
|
|
|
|
|
|
name = strtok((char *) hookline, ":");
|
|
|
|
param = strtok(NULL, "");
|
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
plg = plugin_lookup(PLUGIN_TYPE_HOOK, name);
|
|
|
|
if (!plg)
|
2016-11-20 12:59:37 -05:00
|
|
|
cerror(cfg, "Unknown hook function '%s'", name);
|
|
|
|
|
2017-03-11 23:20:24 -03:00
|
|
|
hook = memdup(&plg->hook, sizeof(plg->hook));
|
2017-03-05 10:06:32 -04:00
|
|
|
hook->parameter = param;
|
|
|
|
|
|
|
|
list_push(list, hook);
|
2016-11-20 12:59:37 -05:00
|
|
|
|
|
|
|
return 0;
|
2015-10-09 13:30:41 +02:00
|
|
|
}
|