aboutsummaryrefslogtreecommitdiffstats
path: root/Controler-afb/ctl-dispatch.c
diff options
context:
space:
mode:
Diffstat (limited to 'Controler-afb/ctl-dispatch.c')
-rw-r--r--Controler-afb/ctl-dispatch.c71
1 files changed, 40 insertions, 31 deletions
diff --git a/Controler-afb/ctl-dispatch.c b/Controler-afb/ctl-dispatch.c
index cda3431..4fa1844 100644
--- a/Controler-afb/ctl-dispatch.c
+++ b/Controler-afb/ctl-dispatch.c
@@ -23,12 +23,10 @@
#include <string.h>
#include <dlfcn.h>
-
-#include <json-c/json_object.h>
-
#include "wrap-json.h"
#include "ctl-binding.h"
+typedef void*(*DispatchPluginInstallCbT)(const char* label, const char*version, const char*info);
typedef struct {
const char* label;
@@ -37,11 +35,11 @@ typedef struct {
DispatchActionT *actions;
} DispatchHandleT;
-
typedef struct {
const char* label;
const char *info;
const char *version;
+ void *context;
char *plugin;
void *dlHandle;
DispatchHandleT *onload;
@@ -56,8 +54,7 @@ PUBLIC void ctlapi_dispatch (DispatchCtlEnumT control, afb_req request) {
json_object* argsJ= afb_req_json(request);
int done=json_object_object_get_ex(argsJ, "closing", &tmpJ);
- if (done) return;
-
+ if (done) return;
}
// List Avaliable Configuration Files
@@ -69,15 +66,15 @@ PUBLIC void ctlapi_config (struct afb_req request) {
if (argsJ && json_object_object_get_ex (argsJ, "cfgpath" , &tmpJ)) {
dirList = strdup (json_object_get_string(tmpJ));
} else {
- dirList = strdup (CONTROL_DISPATCH_PATH);
- AFB_NOTICE ("CONFIG-MISSING: use default CONTROL_DISPATCH_PATH=%s", CONTROL_DISPATCH_PATH);
+ dirList = strdup (CONTROL_CONFIG_PATH);
+ AFB_NOTICE ("CONFIG-MISSING: use default CONTROL_CONFIG_PATH=%s", CONTROL_CONFIG_PATH);
}
// get list of config file
- struct json_object *responseJ = ScanForConfig(dirList, "onload", "json");
+ struct json_object *responseJ = ScanForConfig(dirList, CTL_SCAN_RECURSIVE, "onload", "json");
if (json_object_array_length(responseJ) == 0) {
- afb_req_fail(request, "CONFIGPATH:EMPTY", "No Config Found in CONTROL_DISPATCH_PATH");
+ afb_req_fail(request, "CONFIGPATH:EMPTY", "No Config Found in CONTROL_CONFIG_PATH");
} else {
afb_req_success(request, responseJ, NULL);
}
@@ -166,6 +163,7 @@ STATIC DispatchActionT *DispatchLoadActions (json_object *actionsJ) {
}
+
STATIC DispatchConfigT *DispatchLoadConfig (const char* filepath) {
json_object *dispatchConfigJ, *ignoreJ;
int err;
@@ -185,7 +183,7 @@ STATIC DispatchConfigT *DispatchLoadConfig (const char* filepath) {
DispatchConfigT *dispatchConfig = calloc (1, sizeof(DispatchConfigT));
if (metadataJ) {
- err= wrap_json_unpack(metadataJ, "{so,s?s,ss !}", "label", &dispatchConfig->label, "info",&dispatchConfig->info, "version",&dispatchConfig->version);
+ err= wrap_json_unpack(metadataJ, "{ss,s?s,ss !}", "label", &dispatchConfig->label, "info",&dispatchConfig->info, "version",&dispatchConfig->version);
if (err) {
AFB_ERROR ("DISPATCH-LOAD-CONFIG:METADATA Missing something label|version|[label] in %s", json_object_get_string(metadataJ));
goto OnErrorExit;
@@ -205,33 +203,44 @@ STATIC DispatchConfigT *DispatchLoadConfig (const char* filepath) {
if (dispatchConfig->plugin) {
// search for default policy config file
- json_object *pluginPathJ = ScanForConfig(CONTROL_PLUGIN_PATH , dispatchConfig->plugin, NULL);
+ json_object *pluginPathJ = ScanForConfig(CONTROL_PLUGIN_PATH , CTL_SCAN_RECURSIVE, dispatchConfig->plugin, NULL);
if (!pluginPathJ || json_object_array_length(pluginPathJ) == 0) {
AFB_ERROR ("DISPATCH-LOAD-CONFIG:PLUGIN Missing plugin=%s in path=%s", dispatchConfig->plugin, CONTROL_PLUGIN_PATH);
goto OnErrorExit;
}
- char *filename; char*dirpath;
- err= wrap_json_unpack (json_object_array_get_idx(pluginPathJ,1), "{s:s, s:s !}", "dirpath", &dirpath,"filename", &filename);
+ char *filename; char*fullpath;
+ err= wrap_json_unpack (json_object_array_get_idx(pluginPathJ,0), "{s:s, s:s !}", "fullpath", &fullpath,"filename", &filename);
if (err) {
AFB_ERROR ("DISPATCH-LOAD-CONFIG:PLUGIN HOOPs invalid plugin file path = %s", json_object_get_string(pluginPathJ));
goto OnErrorExit;
}
if (json_object_array_length(pluginPathJ) > 1) {
- AFB_WARNING ("DISPATCH-LOAD-CONFIG:PLUGIN plugin multiple instances in searchpath will use %s/%s", dirpath, filepath);
+ AFB_WARNING ("DISPATCH-LOAD-CONFIG:PLUGIN plugin multiple instances in searchpath will use %s/%s", fullpath, filepath);
}
-
- char filepath[255];
- strncpy(filepath, dirpath, sizeof(filepath));
- strncat(filepath, "/", sizeof(filepath));
- strncat(filepath, filename, sizeof(filepath));
- dispatchConfig->dlHandle = dlopen(filepath, RTLD_NOW);
+
+ char pluginpath[CONTROL_MAXPATH_LEN];
+ strncpy(pluginpath, fullpath, sizeof(pluginpath));
+ strncat(pluginpath, "/", sizeof(pluginpath));
+ strncat(pluginpath, filename, sizeof(pluginpath));
+ dispatchConfig->dlHandle = dlopen(pluginpath, RTLD_NOW);
if (!dispatchConfig->dlHandle) {
- AFB_ERROR ("DISPATCH-LOAD-CONFIG:PLIUGIN Fail to load filepath=%s err= %s", filepath, dlerror());
+ AFB_ERROR ("DISPATCH-LOAD-CONFIG:PLIUGIN Fail to load pluginpath=%s err= %s", pluginpath, dlerror());
goto OnErrorExit;
}
- }
+
+ ulong *ctlPluginMagic = (ulong*)dlsym(dispatchConfig->dlHandle, "CtlPluginMagic");
+ if (!ctlPluginMagic || *ctlPluginMagic != CTL_PLUGIN_MAGIC) {
+ AFB_ERROR("DISPATCH-LOAD-CONFIG:PLIUGIN symbol'CtlPluginMagic' missing or != CTL_PLUGIN_MAGIC plugin=%s", pluginpath);
+ goto OnErrorExit;
+ }
+
+ DispatchPluginInstallCbT ctlPluginInstall = dlsym(dispatchConfig->dlHandle, "CtlPluginOnload");
+ if (ctlPluginInstall) {
+ dispatchConfig->context = (*ctlPluginInstall) (dispatchConfig->label, dispatchConfig->version, dispatchConfig->info);
+ }
+ }
dispatchHandle->actions= DispatchLoadActions(actionsJ);
dispatchConfig->onload= dispatchHandle;
@@ -275,21 +284,21 @@ PUBLIC int DispatchInit () {
// search for default dispatch config file
- json_object* responseJ = ScanForConfig(CONTROL_DISPATCH_PATH, "onload", "json");
+ json_object* responseJ = ScanForConfig(CONTROL_CONFIG_PATH, CTL_SCAN_RECURSIVE,"onload", "json");
for (index=0; index < json_object_array_length(responseJ); index++) {
json_object *entryJ=json_object_array_get_idx(responseJ, index);
- char *filename; char*dirpath;
- err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "dirpath", &dirpath,"filename", &filename);
+ char *filename; char*fullpath;
+ err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "fullpath", &fullpath,"filename", &filename);
if (err) {
- AFB_ERROR ("DISPATCH-INIT HOOPs invalid config file path = %s", json_object_get_string(entryJ));
+ AFB_ERROR ("DISPATCH-INIT HOOPs invalid JSON entry= %s", json_object_get_string(entryJ));
goto OnErrorExit;
}
- if (strcasestr(filename, CONTROL_DISPATCH_FILE)) {
- char filepath[255];
- strncpy(filepath, dirpath, sizeof(filepath));
+ if (strcasestr(filename, CONTROL_CONFIG_FILE)) {
+ char filepath[CONTROL_MAXPATH_LEN];
+ strncpy(filepath, fullpath, sizeof(filepath));
strncat(filepath, "/", sizeof(filepath));
strncat(filepath, filename, sizeof(filepath));
ctlHandle = DispatchLoadConfig (filepath);
@@ -303,7 +312,7 @@ PUBLIC int DispatchInit () {
// no dispatch config found remove control API from binder
if (index == 0) {
- AFB_WARNING ("DISPATCH-INIT:WARNING No Control dispatch file [%s]", CONTROL_DISPATCH_FILE);
+ AFB_WARNING ("DISPATCH-INIT:WARNING No Control dispatch file [%s]", CONTROL_CONFIG_FILE);
}
AFB_NOTICE ("DISPATCH-INIT:SUCCES: Audio Control Dispatch Init");