summaryrefslogtreecommitdiffstats
path: root/ctl-lib
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-12-17 16:03:20 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2018-12-17 16:05:04 +0100
commit444e62f0c4ecae594da71dbdd51305148c433fb5 (patch)
tree7ccd0acb383141af39bef6badf608510470bf147 /ctl-lib
parent30d2f58557ca954f1500b1d6a06ea966786f3a28 (diff)
Update since the conversion to library
Use the latest submodule git master branch revision. Some modifications has been lost since like the getter/setter functions and the plugin loading process. Change-Id: Ic0079f73bac46fba71f3de523a0f28371f85c1e6 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'ctl-lib')
-rw-r--r--ctl-lib/ctl-config.c32
-rw-r--r--ctl-lib/ctl-config.h7
-rw-r--r--ctl-lib/ctl-plugin.c105
3 files changed, 73 insertions, 71 deletions
diff --git a/ctl-lib/ctl-config.c b/ctl-lib/ctl-config.c
index 42391db..ef6038b 100644
--- a/ctl-lib/ctl-config.c
+++ b/ctl-lib/ctl-config.c
@@ -26,9 +26,13 @@
#include "filescan-utils.h"
#include "ctl-config.h"
+extern void* getExternalData(CtlConfigT *ctlConfig) {
+ return ctlConfig->external;
+}
-// Load control config file
-
+extern void setExternalData(CtlConfigT *ctlConfig, void *data) {
+ ctlConfig->external = data;
+}
int CtlConfigMagicNew() {
static int InitRandomDone=0;
@@ -163,30 +167,26 @@ int CtlConfigExec(afb_api_t apiHandle, CtlConfigT *ctlConfig) {
CtlConfigT *CtlLoadMetaDataJson(afb_api_t apiHandle, json_object *ctlConfigJ, const char *prefix) {
json_object *metadataJ;
CtlConfigT *ctlHandle=NULL;
- int err;
int done = json_object_object_get_ex(ctlConfigJ, "metadata", &metadataJ);
if (done) {
ctlHandle = calloc(1, sizeof (CtlConfigT));
- err = wrap_json_unpack(metadataJ, "{ss,ss,ss,s?s,s?o,s?s,s?s !}",
- "uid", &ctlHandle->uid,
- "version", &ctlHandle->version,
- "api", &ctlHandle->api,
- "info", &ctlHandle->info,
- "require", &ctlHandle->requireJ,
- "author", &ctlHandle->author,
- "date", &ctlHandle->date);
- if (err) {
+ if (wrap_json_unpack(metadataJ, "{ss,ss,ss,s?s,s?o,s?s,s?s !}",
+ "uid", &ctlHandle->uid,
+ "version", &ctlHandle->version,
+ "api", &ctlHandle->api,
+ "info", &ctlHandle->info,
+ "require", &ctlHandle->requireJ,
+ "author", &ctlHandle->author,
+ "date", &ctlHandle->date)) {
AFB_API_ERROR(apiHandle, "CTL-LOAD-CONFIG:METADATA Missing something uid|api|version|[info]|[require]|[author]|[date] in:\n-- %s", json_object_get_string(metadataJ));
free(ctlHandle);
return NULL;
}
+ ctlHandle->configJ = ctlConfigJ;
+ ctlHandle->prefix = prefix;
}
- ctlHandle->configJ = ctlConfigJ;
- ctlHandle->prefix = prefix;
- ctlHandle->ctlPlugins = &ctlPlugins;
-
return ctlHandle;
}
diff --git a/ctl-lib/ctl-config.h b/ctl-lib/ctl-config.h
index 285de7d..dc98bb2 100644
--- a/ctl-lib/ctl-config.h
+++ b/ctl-lib/ctl-config.h
@@ -18,6 +18,7 @@
* Json load using json_unpack https://jansson.readthedocs.io/en/2.9/apiref.html#parsing-and-validating-values
*/
+
#ifndef _CTL_CONFIG_INCLUDE_
#define _CTL_CONFIG_INCLUDE_
@@ -75,7 +76,7 @@ typedef struct {
json_object *configJ;
json_object *requireJ;
CtlSectionT *sections;
- CtlPluginT **ctlPlugins;
+ CtlPluginT *ctlPlugins;
void *external;
} CtlConfigT;
@@ -100,6 +101,8 @@ extern int ActionLabelToIndex(CtlActionT* actions, const char* actionLabel);
// ctl-config.c
extern int CtlConfigMagicNew();
+extern void* getExternalData(CtlConfigT *ctlConfig);
+extern void setExternalData(CtlConfigT *ctlConfig, void *data);
extern json_object* CtlConfigScan(const char *dirList, const char *prefix) ;
extern char* ConfigSearch(afb_api_t apiHandle, json_object *responseJ);
extern char* CtlConfigSearch(afb_api_t apiHandle, const char *dirList, const char *prefix) ;
@@ -124,6 +127,8 @@ extern int OnloadConfig(afb_api_t apiHandle, CtlSectionT *section, json_object *
// ctl-plugin.c
extern int PluginConfig(afb_api_t UNUSED_ARG(apiHandle), CtlSectionT *section, json_object *pluginsJ);
extern int PluginGetCB (afb_api_t apiHandle, CtlActionT *action , json_object *callbackJ);
+extern void* getPluginContext(CtlPluginT *plugin);
+extern void setPluginContext(CtlPluginT *plugin, void *context);
#ifdef __cplusplus
}
#endif
diff --git a/ctl-lib/ctl-plugin.c b/ctl-lib/ctl-plugin.c
index c65cee8..0a14e20 100644
--- a/ctl-lib/ctl-plugin.c
+++ b/ctl-lib/ctl-plugin.c
@@ -24,11 +24,22 @@
#include "ctl-config.h"
+void* getPluginContext(CtlPluginT *plugin) {
+ return plugin->context;
+}
+
+void setPluginContext(CtlPluginT *plugin, void *context) {
+ plugin->context = context;
+}
+
int PluginGetCB (afb_api_t apiHandle, CtlActionT *action , json_object *callbackJ) {
const char *plugin=NULL, *function=NULL;
json_object *argsJ;
int idx;
+ CtlConfigT *ctlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle);
+ CtlPluginT *ctlPlugins = ctlConfig ? ctlConfig->ctlPlugins : NULL;
+
if (!ctlPlugins) {
AFB_API_ERROR(apiHandle, "PluginGetCB plugin section missing cannot call '%s'", json_object_get_string(callbackJ));
return 1;
@@ -115,7 +126,7 @@ static int PluginLoadCOne(afb_api_t apiHandle, const char *pluginpath, json_obje
funcName = malloc(p_length + 1);
strncpy(funcName, "lua2c_", p_length);
- strncat(funcName, l2cName, p_length - strlen (funcName));
+ strncat(funcName, l2cName, sizeof(funcName) - strlen (funcName) + 1);
Lua2cFunctionT l2cFunction = (Lua2cFunctionT) dlsym(dlHandle, funcName);
if (!l2cFunction) {
@@ -372,74 +383,65 @@ static int PluginLoad (afb_api_t apiHandle, CtlPluginT *ctlPlugin, json_object *
return 0;
}
-static int PluginParse(afb_api_t apiHandle, CtlSectionT *section, json_object *pluginsJ, int *pluginNb) {
- int idx = 0, err = 0;
+static int PluginParse(afb_api_t apiHandle, CtlSectionT *section, json_object *pluginsJ) {
+ int err = 0, idx = 0, pluginToAddNumber, totalPluginNumber;
+
+ CtlConfigT *ctlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle);
+ CtlPluginT *ctlPluginsNew, *ctlPluginsOrig = ctlConfig ? ctlConfig->ctlPlugins : NULL;
+
+ while(ctlPluginsOrig && ctlPluginsOrig[idx].uid != NULL)
+ idx++;
+
+ totalPluginNumber = idx;
switch (json_object_get_type(pluginsJ)) {
case json_type_array: {
- *pluginNb = (int)json_object_array_length(pluginsJ);
- ctlPlugins = calloc (*pluginNb + 1, sizeof(CtlPluginT));
- for (idx=0; idx < *pluginNb; idx++) {
- json_object *pluginJ = json_object_array_get_idx(pluginsJ, idx);
- err += PluginLoad(apiHandle, &ctlPlugins[idx], pluginJ, section->handle, section->prefix);
- }
+ pluginToAddNumber = (int) json_object_array_length(pluginsJ);
break;
}
case json_type_object: {
- ctlPlugins = calloc (2, sizeof(CtlPluginT));
- err += PluginLoad(apiHandle, &ctlPlugins[0], pluginsJ, section->handle, section->prefix);
- (*pluginNb)++;
+ pluginToAddNumber = 1;
break;
}
default: {
AFB_API_ERROR(apiHandle, "Wrong JSON object passed: %s", json_object_get_string(pluginsJ));
- err = -1;
+ return -1;
}
}
- return err;
+ totalPluginNumber += pluginToAddNumber;
+
+ ctlPluginsNew = calloc (totalPluginNumber + 1, sizeof(CtlPluginT));
+ memcpy(ctlPluginsNew, ctlPluginsOrig, idx * sizeof(CtlPluginT));
+
+ while(idx < totalPluginNumber) {
+ json_object *pluginJ = json_object_is_type(pluginsJ, json_type_array) ?
+ json_object_array_get_idx(pluginsJ, idx) : pluginsJ;
+ err += PluginLoad(apiHandle, &ctlPluginsNew[idx], pluginJ, section->handle, section->prefix);
+ idx++;
+ }
+
+ ctlConfig->ctlPlugins = ctlPluginsNew;
+ free(ctlPluginsOrig);
+
+ return err;
}
int PluginConfig(afb_api_t apiHandle, CtlSectionT *section, json_object *pluginsJ) {
- int err = 0;
- int idx = 0, jdx = 0;
- int pluginNb = 0, newPluginsNb = 0, totalPluginNb = 0;
-
- if (ctlPlugins)
- {
- // There is something to add let that happens
- if(pluginsJ) {
- CtlPluginT *ctlPluginsNew = NULL, *ctlPluginsOrig = ctlPlugins;
- err = PluginParse(apiHandle, section, pluginsJ, &newPluginsNb);
- ctlPluginsNew = ctlPlugins;
-
- while(ctlPlugins[pluginNb].uid != NULL) {
- pluginNb++;
- }
-
- totalPluginNb = pluginNb + newPluginsNb;
- ctlPlugins = calloc(totalPluginNb + 1, sizeof(CtlPluginT));
- while(ctlPluginsOrig[idx].uid != NULL) {
- ctlPlugins[idx] = ctlPluginsOrig[idx];
- idx++;
- }
- while(ctlPluginsNew[jdx].uid != NULL && idx <= totalPluginNb) {
- ctlPlugins[idx] = ctlPluginsNew[jdx];
- idx++;
- jdx++;
- }
+ int err = 0, idx = 0;
- free(ctlPluginsOrig);
- free(ctlPluginsNew);
- }
+ CtlConfigT *ctlConfig = (CtlConfigT *) afb_api_get_userdata(apiHandle);
+ CtlPluginT *ctlPlugins = ctlConfig ? ctlConfig->ctlPlugins : NULL;
+ // First plugins load
+ if(pluginsJ) {
+ err = PluginParse(apiHandle, section, pluginsJ);
+ }
+ // Code executed executed at Controller ConfigExec step
+ else if (ctlPlugins) {
while(ctlPlugins[idx].uid != NULL)
{
- // Jose hack to make verbosity visible from sharedlib and
- // be able to call verb from others api inside the binder
- struct afb_binding_data_v2 *afbHidenData = dlsym(ctlPlugins[idx].dlHandle, "afbBindingV2data");
- if (afbHidenData) *afbHidenData = afbBindingV2data;
-
+ // Calling plugin Init function
DispatchPluginInstallCbT ctlPluginInit = dlsym(ctlPlugins[idx].dlHandle, "CtlPluginInit");
if (ctlPluginInit) {
if((*ctlPluginInit) (&ctlPlugins[idx], ctlPlugins[idx].context)) {
@@ -449,11 +451,6 @@ int PluginConfig(afb_api_t apiHandle, CtlSectionT *section, json_object *plugins
}
idx++;
}
- return 0;
- }
- else if(pluginsJ)
- {
- err = PluginParse(apiHandle, section, pluginsJ, &pluginNb);
}
return err;