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/hooks.c

102 lines
2 KiB
C
Raw Normal View History

/** Hook-releated functions.
*
* @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
*********************************************************************************/
#include <string.h>
#include <math.h>
#include "timing.h"
#include "config.h"
#include "msg.h"
#include "hooks.h"
#include "path.h"
#include "utils.h"
#include "node.h"
struct list hooks;
2015-08-07 01:11:43 +02:00
int hook_init(struct hook *h, struct list *nodes, struct list *paths, struct settings *settings)
{
struct hook_info i = {
.paths = paths,
.nodes = nodes,
.settings = settings
};
2016-11-20 02:50:12 -05:00
if (h->type & HOOK_INIT)
return h->cb(h, HOOK_INIT, &i);
else
return 0;
}
void hook_destroy(struct hook *h)
{
struct hook_info i = { NULL };
h->cb(h, HOOK_DESTROY, &i);
}
int hook_copy(struct hook *h, struct hook *c)
{
memcpy(c, h, sizeof(struct hook));
c->_vd =
c->prev =
c->last = NULL;
return 0;
}
int hooks_sort_priority(const void *a, const void *b) {
struct hook *ha = (struct hook *) a;
struct hook *hb = (struct hook *) b;
return ha->priority - hb->priority;
}
int hook_run(struct path *p, struct sample *smps[], size_t cnt, int when)
{
struct hook_info i = {
.path = p,
.smps = smps,
.cnt = cnt
};
list_foreach(struct hook *h, &p->hooks) {
if (h->type & when) {
debug(DBG_HOOK | 22, "Running hook when=%u '%s' prio=%u, cnt=%zu", when, h->name, h->priority, cnt);
2015-08-07 01:11:43 +02:00
cnt = h->cb(h, when, &i);
if (cnt == 0)
break;
}
}
2015-10-12 16:16:25 +02:00
return cnt;
2015-10-12 16:16:25 +02:00
}
void * hook_storage(struct hook *h, int when, size_t len, ctor_cb_t ctor, dtor_cb_t dtor)
{
switch (when) {
case HOOK_INIT:
h->_vd = alloc(len);
if (ctor)
ctor(h->_vd);
break;
case HOOK_DESTROY:
if (dtor)
dtor(h->_vd);
free(h->_vd);
h->_vd = NULL;
break;
}
return h->_vd;
}