More work on the plugin API.

This commit is contained in:
Jan Vidar Krey
2010-06-06 16:15:15 +02:00
parent ed53034ad5
commit 07d4e4470c
6 changed files with 100 additions and 84 deletions

View File

@@ -20,8 +20,9 @@
#include "uhub.h"
#ifdef PLUGIN_SUPPORT
#include "plugin_api/handle.h"
struct uhub_plugin* uhub_plugin_open(const char* filename)
struct uhub_plugin* plugin_open(const char* filename)
{
#ifdef HAVE_DLOPEN
struct uhub_plugin* plugin = (struct uhub_plugin*) hub_malloc_zero(sizeof(struct uhub_plugin));
@@ -45,7 +46,7 @@ struct uhub_plugin* uhub_plugin_open(const char* filename)
#endif
}
void uhub_plugin_close(struct uhub_plugin* plugin)
void plugin_close(struct uhub_plugin* plugin)
{
#ifdef HAVE_DLOPEN
dlclose(plugin->handle);
@@ -53,7 +54,7 @@ void uhub_plugin_close(struct uhub_plugin* plugin)
#endif
}
void* uhub_plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol)
void* plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol)
{
#ifdef HAVE_DLOPEN
void* addr = dlsym(plugin->handle, symbol);
@@ -63,4 +64,52 @@ void* uhub_plugin_lookup_symbol(struct uhub_plugin* plugin, const char* symbol)
#endif
}
struct uhub_plugin_handle* plugin_load(const char* filename, const char* config)
{
plugin_register_f register_f;
plugin_unregister_f unregister_f;
int ret;
struct uhub_plugin_handle* handle = hub_malloc_zero(sizeof(struct uhub_plugin_handle));
struct uhub_plugin* plugin = plugin_open(filename);
if (!plugin)
return NULL;
if (!handle)
{
plugin_close(plugin);
return NULL;
}
handle->handle = plugin;
register_f = plugin_lookup_symbol(plugin, "plugin_register");
unregister_f = plugin_lookup_symbol(plugin, "plugin_unregister");
if (register_f && unregister_f)
{
ret = register_f(handle, config);
if (ret == 0)
{
if (handle->plugin_api_version == PLUGIN_API_VERSION && handle->plugin_funcs_size == sizeof(struct plugin_funcs))
{
LOG_INFO("Loaded plugin: %s: \"%s\", version %s.", filename, handle->name, handle->version);
return handle;
}
else
{
LOG_ERROR("Unable to load plugin: %s - API version mistmatch", filename);
}
}
else
{
LOG_ERROR("Unable to load plugin: %s - Failed to initialize", filename);
}
}
plugin_close(plugin);
hub_free(handle);
return NULL;
}
#endif /* PLUGIN_SUPPORT */

View File

@@ -22,6 +22,8 @@
#ifdef PLUGIN_SUPPORT
struct uhub_plugin_handle;
struct uhub_plugin
{
#ifdef HAVE_DLOPEN
@@ -29,11 +31,16 @@ struct uhub_plugin
#endif
};
extern struct uhub_plugin* uhub_plugin_open(const char* filename);
extern struct uhub_plugin* plugin_open(const char* filename);
extern void uhub_plugin_close(struct uhub_plugin*);
extern void plugin_close(struct uhub_plugin*);
extern void* uhub_plugin_lookup_symbol(struct uhub_plugin*, const char* symbol);
extern void* plugin_lookup_symbol(struct uhub_plugin*, const char* symbol);
// Load and register a plugin
extern struct uhub_plugin_handle* plugin_load(const char* filename, const char* config);
extern void plugin_unload(struct uhub_plugin_handle*);
#endif /* PLUGIN_SUPPORT */