2017-02-12 14:35:05 -03:00
|
|
|
/** Loadable / plugin support.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, 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-05-05 19:24:16 +00:00
|
|
|
*
|
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-05-05 19:24:16 +00:00
|
|
|
*
|
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/>.
|
2017-02-12 14:35:05 -03:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2017-03-03 20:21:33 -04:00
|
|
|
#include <dlfcn.h>
|
2017-11-02 13:08:12 +01:00
|
|
|
#include <string.h>
|
2017-03-03 20:21:33 -04:00
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/plugin.h>
|
2017-02-12 14:35:05 -03:00
|
|
|
|
2017-03-03 20:21:33 -04:00
|
|
|
/** Global list of all known plugins */
|
2017-07-24 19:21:57 +02:00
|
|
|
struct list plugins = { .state = STATE_DESTROYED };
|
2017-03-03 20:21:33 -04:00
|
|
|
|
2017-07-02 23:53:48 +02:00
|
|
|
LIST_INIT_STATIC(&plugins)
|
2017-04-24 19:27:27 +02:00
|
|
|
|
2017-03-13 23:51:38 -03:00
|
|
|
int plugin_init(struct plugin *p)
|
2017-02-12 14:35:05 -03:00
|
|
|
{
|
2017-03-13 23:51:38 -03:00
|
|
|
assert(p->state == STATE_DESTROYED);
|
2017-02-12 14:35:05 -03:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
p->state = STATE_INITIALIZED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-03 20:21:33 -04:00
|
|
|
return 0;
|
2017-02-12 14:35:05 -03:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int plugin_parse(struct plugin *p, json_t *cfg)
|
2017-03-13 23:51:38 -03:00
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
path = json_string_value(cfg);
|
2017-03-13 23:51:38 -03:00
|
|
|
if (!path)
|
2017-08-03 00:19:27 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
p->path = strdup(path);
|
2017-03-13 23:51:38 -03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
int plugin_load(struct plugin *p)
|
|
|
|
{
|
|
|
|
p->handle = dlopen(p->path, RTLD_NOW);
|
|
|
|
if (!p->path)
|
|
|
|
return -1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
p->state = STATE_LOADED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int plugin_unload(struct plugin *p)
|
|
|
|
{
|
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(p->state == STATE_LOADED);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
ret = dlclose(p->handle);
|
|
|
|
if (ret)
|
|
|
|
return -1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
p->state = STATE_UNLOADED;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int plugin_destroy(struct plugin *p)
|
|
|
|
{
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(p->state != STATE_DESTROYED && p->state != STATE_LOADED);
|
2017-03-07 07:20:49 -04:00
|
|
|
|
|
|
|
if (p->path)
|
|
|
|
free(p->path);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-18 10:45:05 -05:00
|
|
|
return 0;
|
2017-02-12 14:35:05 -03:00
|
|
|
}
|
|
|
|
|
2017-03-03 20:21:33 -04:00
|
|
|
struct plugin * plugin_lookup(enum plugin_type type, const char *name)
|
2017-02-12 14:35:05 -03:00
|
|
|
{
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(&plugins); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct plugin *p = (struct plugin *) list_at(&plugins, i);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
|
|
|
if (p->type == type && strcmp(p->name, name) == 0)
|
|
|
|
return p;
|
2017-02-12 14:35:05 -03:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-02-12 14:35:05 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-03-03 20:21:33 -04:00
|
|
|
|
|
|
|
void plugin_dump(enum plugin_type type)
|
|
|
|
{
|
2017-03-25 21:23:31 +01:00
|
|
|
for (size_t i = 0; i < list_length(&plugins); i++) {
|
2017-10-18 15:39:53 +02:00
|
|
|
struct plugin *p = (struct plugin *) list_at(&plugins, i);
|
2017-03-25 21:23:31 +01:00
|
|
|
|
2017-03-05 11:09:37 -04:00
|
|
|
if (p->type == type)
|
2017-08-14 14:43:02 +02:00
|
|
|
printf(" - %-13s: %s\n", p->name, p->description);
|
2017-03-05 11:09:37 -04:00
|
|
|
}
|
2017-03-03 20:21:33 -04:00
|
|
|
}
|