1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

refactor more code to new plugin system

This commit is contained in:
Steffen Vogel 2017-02-18 10:51:43 -05:00
parent 7e411ed5ab
commit f03fe00bff
5 changed files with 46 additions and 32 deletions

View file

@ -25,10 +25,20 @@
#include "queue.h"
#include "list.h"
/* The configuration of hook parameters is done in "config.h" */
#include "cfg.h"
#define REGISTER_HOOK(nam, desc, prio, hist, fnc, typ) \
__attribute__((constructor)) void __register_ ## fnc () { \
static struct hook h = { \
.name = nam, \
.description = desc, \
.priority = prio, \
.history = hist, \
.type = typ, \
.cb = fnc \
}; \
list_push(&hooks, &h); \
}
/* Forward declarations */
struct path;

View file

@ -11,8 +11,11 @@
#include "hooks.h"
#include "api.h"
#include "fpga/ip.h"
#include "nodes/cbuilder.h"
#define REGISTER_PLUGIN(p) \
__attribute__((constructor)) static void UNIQUE(__ctor)() { \
list_push(&plugins, p); \
@ -51,18 +54,23 @@ struct plugin {
int (*unload)(struct plugin *p);
union {
struct api_ressource api;
struct hook hook;
struct node_type node;
struct api_ressource api;
struct node_type node;
struct fpga_ip_type ip;
struct hook hook;
struct cbuilder_model cb;
};
};
struct plugin * plugin_lookup(char *name, enum plugin_types type);
int plugin_init(struct plugin *p, char *name, char *path);
int plugin_destroy(struct plugin *p);
int plugin_parse(struct plugin *p, config_setting_t *cfg);
int plugin_load(struct plugin *p);
int plugin_unload(struct plugin *p);
int plugin_destroy(struct plugin *p);
/** Find registered and loaded plugin with given name and type. */
struct plugin * plugin_lookup(enum plugin_types type, const char *name);

View file

@ -12,8 +12,6 @@
#include "nodes/cbuilder.h"
struct list cbmodels; /**< Table of existing CBuilder models */
int cbuilder_parse(struct node *n, config_setting_t *cfg)
{
struct cbuilder *cb = n->_vd;
@ -27,7 +25,7 @@ int cbuilder_parse(struct node *n, config_setting_t *cfg)
if (!config_setting_lookup_string(cfg, "model", &model))
cerror(cfg, "CBuilder model requires 'model' setting");
cb->model = list_lookup(&cbmodels, model);
cb->model = (struct cbuilder_model *) plugin_lookup(PLUGIN_TYPE_MODEL_CBUILDER, model);
if (!cb->model)
cerror(cfg, "Unknown model '%s'", model);

View file

@ -546,18 +546,19 @@ int websocket_write(struct node *n, struct sample *smps[], unsigned cnt)
return cnt;
}
static struct node_type vt = {
static struct plugin p = {
.name = "websocket",
.description = "Send and receive samples of a WebSocket connection (libwebsockets)",
.vectorize = 0, /* unlimited */
.size = sizeof(struct websocket),
.open = websocket_open,
.close = websocket_close,
.destroy = websocket_destroy,
.read = websocket_read,
.write = websocket_write,
.init = websocket_init,
.deinit = websocket_deinit
.type = PLUGIN_TYPE_NODE,
.node = {
.vectorize = 0, /* unlimited */
.size = sizeof(struct websocket),
.open = websocket_open,
.close = websocket_close,
.destroy = websocket_destroy,
.read = websocket_read,
.write = websocket_write,
}
};
REGISTER_NODE_TYPE(&vt)
REGISTER_PLUGIN(&p)

View file

@ -54,7 +54,7 @@ int plugin_destroy(struct plugin *p)
return 0;
}
int plugin_parse(struct plugin *p, config_setting_t *lcs)
int plugin_parse(struct plugin *p, config_setting_t *cfg)
{
const char *path;
@ -62,23 +62,20 @@ int plugin_parse(struct plugin *p, config_setting_t *lcs)
if (!path)
cerror(cfg, "Setting 'plugin' must be a string.");
p->
handle = dlopen(path, RTLD_NOW);
if (!handle)
error("Failed to load plugin %s", dlerror());
handle = dlopen(path, RTLD_NOW);
if (!handle)
error("Failed to load plugin %s", dlerror());
list_push_back(&cfg->plugins, handle);
}
list_push_back(&cfg->plugins, handle);
return 0;
}
struct plugin * plugin_lookup(char *name, enum plugin_types type)
struct plugin * plugin_lookup(enum plugin_types type, const char *name)
{
list_foreach(struct plugin *l, &plugins) {
if (l->type == type && strcmp(l->name, name) == 0)
return l
return l;
}
return NULL;