summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2018-07-12 18:18:01 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2018-12-13 15:02:55 +0100
commitd0035a84e055fe2224fd1333f814873578e3dde6 (patch)
treed0e6a7f5cfcc9824c0990b5dc6457892734ed62a
parentc9ed0f7a1556a7c0828aec0daf4db19cdf0f29a4 (diff)
Use prefix variable to find controller's plugins
Prefix is the most reliable variable to find files or variables for a controller, so this lets you access it wherever it is needed without using global hardcoded variables. This helps to search for controller's plugins in several locations depending on environment variables and hardcoded variables (CONTROL_PLUGIN_PATH, CONTROL_CONFIG_PATH). This implies also a change a LUA interpreter loading step to correctly set the package.path variables with the environment variables, too. Correct the missing 'extern' in function declarations. Depends-On: Ic448ff017e6158bec05895d63688b8968b5c6434 Change-Id: I0ad19242612559d1f4b66b6f9af9e7032d4675a8 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--ctl-lib/ctl-config.c10
-rw-r--r--ctl-lib/ctl-config.h43
-rw-r--r--ctl-lib/ctl-lua.c21
-rw-r--r--ctl-lib/ctl-lua.h12
-rw-r--r--ctl-lib/ctl-plugin.c35
-rw-r--r--ctl-lib/ctl-plugin.h2
-rw-r--r--ctl-lib/ctl-timer.h8
7 files changed, 70 insertions, 61 deletions
diff --git a/ctl-lib/ctl-config.c b/ctl-lib/ctl-config.c
index 3153f13..a98cb34 100644
--- a/ctl-lib/ctl-config.c
+++ b/ctl-lib/ctl-config.c
@@ -161,7 +161,7 @@ int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) {
return errcount;
}
-CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle, const char* filepath) {
+CtlConfigT *CtlLoadMetaDataUsingPrefix(AFB_ApiT apiHandle,const char* filepath, const char *prefix) {
json_object *ctlConfigJ;
CtlConfigT *ctlHandle=NULL;
int err;
@@ -195,9 +195,14 @@ CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle, const char* filepath) {
}
ctlHandle->configJ = ctlConfigJ;
+ ctlHandle->prefix = prefix;
return ctlHandle;
}
+CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle, const char* filepath) {
+ return CtlLoadMetaDataUsingPrefix(apiHandle, filepath, NULL);
+}
+
void wrap_json_array_add(void* array, json_object *val) {
json_object_array_add(array, (json_object*)val);
}
@@ -302,7 +307,7 @@ int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sect
int err;
#ifdef CONTROL_SUPPORT_LUA
- err= LuaConfigLoad(apiHandle);
+ err= LuaConfigLoad(apiHandle, ctlHandle->prefix);
if (err)
return 1;
#endif
@@ -313,6 +318,7 @@ int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sect
json_object * sectionJ;
int done = json_object_object_get_ex(ctlHandle->configJ, sections[idx].key, &sectionJ);
if (done) {
+ sections[idx].prefix = ctlHandle->prefix;
json_object* updatedSectionJ = LoadAdditionalsFiles(apiHandle, ctlHandle, sections[idx].key, sectionJ);
err += sections[idx].loadCB(apiHandle, &sections[idx], updatedSectionJ);
}
diff --git a/ctl-lib/ctl-config.h b/ctl-lib/ctl-config.h
index a174b27..be2113d 100644
--- a/ctl-lib/ctl-config.h
+++ b/ctl-lib/ctl-config.h
@@ -58,6 +58,7 @@ typedef struct ConfigSectionS {
const char *key;
const char *uid;
const char *info;
+ const char *prefix;
int (*loadCB)(AFB_ApiT apihandle, struct ConfigSectionS *section, json_object *sectionJ);
void *handle;
CtlActionT *actions;
@@ -70,6 +71,7 @@ typedef struct {
const char *version;
const char *author;
const char *date;
+ const char *prefix;
json_object *configJ;
json_object *requireJ;
CtlSectionT *sections;
@@ -88,39 +90,40 @@ typedef enum {
} SectionEnumT;
// ctl-action.c
-CtlActionT *ActionConfig(AFB_ApiT apiHandle, json_object *actionsJ, int exportApi);
-void ActionExecUID(AFB_ReqT request, CtlConfigT *ctlConfig, const char *uid, json_object *queryJ);
-int ActionExecOne( CtlSourceT *source, CtlActionT* action, json_object *queryJ);
-int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *, int exportApi);
-int ActionLabelToIndex(CtlActionT* actions, const char* actionLabel);
+extern CtlActionT *ActionConfig(AFB_ApiT apiHandle, json_object *actionsJ, int exportApi);
+extern void ActionExecUID(AFB_ReqT request, CtlConfigT *ctlConfig, const char *uid, json_object *queryJ);
+extern int ActionExecOne( CtlSourceT *source, CtlActionT* action, json_object *queryJ);
+extern int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *, int exportApi);
+extern int ActionLabelToIndex(CtlActionT* actions, const char* actionLabel);
// ctl-config.c
-int CtlConfigMagicNew();
-json_object* CtlConfigScan(const char *dirList, const char *prefix) ;
-char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ);
-char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) ;
-void DispatchRequiredApi(AFB_ApiT apiHandle, json_object * requireJ);
-int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) ;
-CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle,const char* filepath) ;
-int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sections);
+extern int CtlConfigMagicNew();
+extern json_object* CtlConfigScan(const char *dirList, const char *prefix) ;
+extern char* ConfigSearch(AFB_ApiT apiHandle, json_object *responseJ);
+extern char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) ;
+extern void DispatchRequiredApi(AFB_ApiT apiHandle, json_object * requireJ);
+extern int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) ;
+extern CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle,const char* filepath) ;
+extern CtlConfigT *CtlLoadMetaDataUsingPrefix(AFB_ApiT apiHandle,const char* filepath, const char *prefix) ;
+extern int CtlLoadSections(AFB_ApiT apiHandle, CtlConfigT *ctlHandle, CtlSectionT *sections);
// ctl-event.c
-int EventConfig(AFB_ApiT apihandle, CtlSectionT *section, json_object *actionsJ);
+extern int EventConfig(AFB_ApiT apihandle, CtlSectionT *section, json_object *actionsJ);
#ifdef AFB_BINDING_PREV3
-void CtrlDispatchApiEvent (AFB_ApiT apiHandle, const char *evtLabel, struct json_object *eventJ);
+extern void CtrlDispatchApiEvent (AFB_ApiT apiHandle, const char *evtLabel, struct json_object *eventJ);
#else
-void CtrlDispatchV2Event(const char *evtLabel, json_object *eventJ);
+extern void CtrlDispatchV2Event(const char *evtLabel, json_object *eventJ);
#endif
// ctl-control.c
-int ControlConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ);
+extern int ControlConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ);
// ctl-onload.c
-int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ);
+extern int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ);
// ctl-plugin.c
-int PluginConfig(AFB_ApiT UNUSED_ARG(apiHandle), CtlSectionT *section, json_object *pluginsJ);
-int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *callbackJ);
+extern int PluginConfig(AFB_ApiT UNUSED_ARG(apiHandle), CtlSectionT *section, json_object *pluginsJ);
+extern int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *callbackJ);
#ifdef __cplusplus
}
diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c
index 55cc18c..ab0abb6 100644
--- a/ctl-lib/ctl-lua.c
+++ b/ctl-lib/ctl-lua.c
@@ -1294,10 +1294,11 @@ int LuaConfigExec(AFB_ApiT apiHandle) {
// Load Lua Interpreter
-int LuaConfigLoad(AFB_ApiT apiHandle) {
+int LuaConfigLoad(AFB_ApiT apiHandle, const char *prefix) {
+ size_t total_len = 0, base_len = 0, spath_len = 0;
static int luaLoaded = 0;
- int i;
- //int err = 0;
+ int token_nb = 0, i = 0;
+ char *spath = NULL, *sep = NULL, *lua_str = NULL;
// Lua loads only once
if (luaLoaded) return 0;
@@ -1320,20 +1321,20 @@ int LuaConfigLoad(AFB_ApiT apiHandle) {
// set package.path lua variable use the CONTROL_PLUGIN_PATH as it could
// have to find external lua packages in those directories
- size_t base_len = strlen(LUA_PATH_VALUE);
- size_t spath_len = strlen(CONTROL_PLUGIN_PATH);
+ base_len = strlen(LUA_PATH_VALUE);
+ spath_len = strlen(CONTROL_PLUGIN_PATH);
- int token_nb = spath_len ? 1:0;
- char *spath = strdup(CONTROL_PLUGIN_PATH);
- char *sep = spath;
+ token_nb = spath_len ? 1:0;
+ spath = GetDefaultPluginSearchPath(apiHandle, prefix);
+ sep = spath;
while((sep = strchr(sep, ':')) != NULL) {
token_nb++;
sep++;
}
// token + the lua glob pattern which is 7 char length
- size_t total_len = base_len + spath_len + token_nb * 7 + 1;
- char *lua_str = malloc(total_len + 1);
+ total_len = base_len + spath_len + token_nb * 7 + 1;
+ lua_str = malloc(total_len + 1);
strncpy(lua_str, LUA_PATH_VALUE, total_len);
for (i = 0; i < token_nb; i++) {
sep = strsep(&spath, ":");
diff --git a/ctl-lib/ctl-lua.h b/ctl-lib/ctl-lua.h
index bb202d6..0ec4960 100644
--- a/ctl-lib/ctl-lua.h
+++ b/ctl-lib/ctl-lua.h
@@ -52,12 +52,12 @@ typedef enum {
} LuaDoActionT;
extern const char *lua_utils;
-int LuaLoadScript(const char *luaScriptPath);
-int LuaConfigLoad (AFB_ApiT apiHandle);
-void LuaL2cNewLib(luaL_Reg *l2cFunc, int count, const char *prefix);
-int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback);
-int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *queryJ) ;
-int LuaConfigExec(AFB_ApiT apiHandle);
+extern int LuaLoadScript(const char *luaScriptPath);
+extern int LuaConfigLoad (AFB_ApiT apiHandle, const char *prefix);
+extern void LuaL2cNewLib(luaL_Reg *l2cFunc, int count, const char *prefix);
+extern int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback);
+extern int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *queryJ) ;
+extern int LuaConfigExec(AFB_ApiT apiHandle);
#ifdef __cplusplus
}
diff --git a/ctl-lib/ctl-plugin.c b/ctl-lib/ctl-plugin.c
index 16b2056..bdcdb16 100644
--- a/ctl-lib/ctl-plugin.c
+++ b/ctl-lib/ctl-plugin.c
@@ -239,35 +239,32 @@ static int LoadFoundPlugins(AFB_ApiT apiHandle, json_object *scanResult, json_ob
return 0;
}
-static char *GetDefaultSearchPath(AFB_ApiT apiHandle)
+char *GetDefaultPluginSearchPath(AFB_ApiT apiHandle, const char *prefix)
{
char *searchPath;
const char *bindingPath;
- const char *envPath;
- size_t bindingPath_len, envPath_len, CTL_PLGN_len;
+ const char *envDirList;
+ size_t bindingPath_len, envDirList_len;
bindingPath = GetBindingDirPath(apiHandle);
- envPath = getenv("CONTROL_PLUGIN_PATH");
bindingPath_len = strlen(bindingPath);
- envPath_len = envPath ? strlen(envPath) : 0;
- CTL_PLGN_len = envPath_len ? 0 : strlen(CONTROL_PLUGIN_PATH);
+ envDirList = getEnvDirList(prefix, "PLUGIN_PATH");
/* Allocating with the size of binding root dir path + environment if found
* + 2 for the NULL terminating character and the additional separator
- * between bindingPath and envPath concatenation.
+ * between bindingPath and envDirList concatenation.
*/
- if(envPath) {
- searchPath = calloc(1, sizeof(char) *((bindingPath_len + envPath_len) + 2));
- strncat(searchPath, envPath, envPath_len);
+ if(envDirList) {
+ envDirList_len = strlen(CONTROL_PLUGIN_PATH) + strlen(envDirList) + bindingPath_len;
+ searchPath = malloc(envDirList_len + 1);
+ snprintf(searchPath, envDirList_len + 1, "%s:%s:%s", bindingPath, envDirList, CONTROL_PLUGIN_PATH);
}
else {
- searchPath = calloc(1, sizeof(char) * ((bindingPath_len + CTL_PLGN_len) + 2));
- strncat(searchPath, CONTROL_PLUGIN_PATH, CTL_PLGN_len);
+ envDirList_len = strlen(CONTROL_PLUGIN_PATH) + bindingPath_len;
+ searchPath = malloc(envDirList_len + 1);
+ snprintf(searchPath, envDirList_len + 1, "%s:%s", bindingPath, CONTROL_PLUGIN_PATH);
}
- strncat(searchPath, ":", sizeof(searchPath) - 1);
- strncat(searchPath, bindingPath, bindingPath_len);
-
return searchPath;
}
@@ -282,7 +279,7 @@ static int FindPlugins(AFB_ApiT apiHandle, const char *searchPath, const char *f
return 0;
}
-static int PluginLoad (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object *pluginJ, void *handle)
+static int PluginLoad (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object *pluginJ, void *handle, const char *prefix)
{
int err = 0, i = 0;
char *searchPath;
@@ -317,7 +314,7 @@ static int PluginLoad (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object *p
// if search path not in Json config file, then try default
searchPath = (sPath) ?
strdup(sPath) :
- GetDefaultSearchPath(apiHandle);
+ GetDefaultPluginSearchPath(apiHandle, prefix);
// default file equal uid
if (!fileJ) {
@@ -371,13 +368,13 @@ static int PluginParse(AFB_ApiT apiHandle, CtlSectionT *section, json_object *pl
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);
+ err += PluginLoad(apiHandle, &ctlPlugins[idx], pluginJ, section->handle, section->prefix);
}
break;
}
case json_type_object: {
ctlPlugins = calloc (2, sizeof(CtlPluginT));
- err += PluginLoad(apiHandle, &ctlPlugins[0], pluginsJ, section->handle);
+ err += PluginLoad(apiHandle, &ctlPlugins[0], pluginsJ, section->handle, section->prefix);
(*pluginNb)++;
break;
}
diff --git a/ctl-lib/ctl-plugin.h b/ctl-lib/ctl-plugin.h
index 6488e86..e4b8ccb 100644
--- a/ctl-lib/ctl-plugin.h
+++ b/ctl-lib/ctl-plugin.h
@@ -133,6 +133,8 @@ typedef int (*Lua2cWrapperT) (void*luaHandle, const char *funcname, Lua2cFunctio
int lua2c_ ## funcname (void* luaState){return((*Lua2cWrap)(luaState, MACRO_STR_VALUE(funcname), funcname));};\
int funcname (CtlSourceT* source, json_object* argsJ, json_object** responseJ)
+extern char *GetDefaultPluginSearchPath(AFB_ApiT apiHandle, const char *prefix);
+
#ifdef __cplusplus
}
#endif
diff --git a/ctl-lib/ctl-timer.h b/ctl-lib/ctl-timer.h
index 71f87af..a65a906 100644
--- a/ctl-lib/ctl-timer.h
+++ b/ctl-lib/ctl-timer.h
@@ -42,11 +42,11 @@ typedef struct TimerHandleS {
typedef int (*timerCallbackT)(TimerHandleT *context);
-int TimerEvtInit (AFB_ApiT apiHandle);
-void TimerEvtStart(AFB_ApiT apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context);
-void TimerEvtStop(TimerHandleT *timerHandle);
+extern int TimerEvtInit (AFB_ApiT apiHandle);
+extern void TimerEvtStart(AFB_ApiT apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context);
+extern void TimerEvtStop(TimerHandleT *timerHandle);
-uint64_t LockWait(AFB_ApiT apiHandle, uint64_t utimeout);
+extern uint64_t LockWait(AFB_ApiT apiHandle, uint64_t utimeout);
#ifdef __cplusplus
}
#endif