diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-09-12 00:29:28 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-14 11:00:25 +0100 |
commit | d663f702c0e4fa562783504a483dce8f362d628e (patch) | |
tree | fdb656d0467a935aa6790dd9e05d3e373606a3e1 /controller | |
parent | 75cfbf3d0206f12422091e6479c508c69445bf4a (diff) |
Progression on config parsing.
Change-Id: Idd8d8987ccabef381f6d79c1d508930a2d4bae3c
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'controller')
-rw-r--r-- | controller/ctl-action.c | 12 | ||||
-rw-r--r-- | controller/ctl-config.c | 14 | ||||
-rw-r--r-- | controller/ctl-config.h | 35 | ||||
-rw-r--r-- | controller/ctl-lua.c | 28 | ||||
-rw-r--r-- | controller/ctl-lua.h | 35 | ||||
-rw-r--r-- | controller/ctl-onload.c | 10 | ||||
-rw-r--r-- | controller/ctl-plugin.c | 61 | ||||
-rw-r--r-- | controller/ctl-plugin.h | 9 | ||||
-rw-r--r-- | controller/ctl-timer.c | 8 | ||||
-rw-r--r-- | controller/ctl-timer.h | 8 |
10 files changed, 119 insertions, 101 deletions
diff --git a/controller/ctl-action.c b/controller/ctl-action.c index b24269c..5da24e8 100644 --- a/controller/ctl-action.c +++ b/controller/ctl-action.c @@ -25,7 +25,7 @@ #include "ctl-config.h" -PUBLIC int ActionExecOne(CtlActionT* action, json_object *queryJ) { +int ActionExecOne(CtlActionT* action, json_object *queryJ) { int err; @@ -50,7 +50,7 @@ PUBLIC int ActionExecOne(CtlActionT* action, json_object *queryJ) { json_object_object_add(queryJ, "args", action->argsJ); } } - + json_object_object_add(queryJ, "label", json_object_new_string(action->source.label)); int err = afb_service_call_sync(action->api, action->call, queryJ, &returnJ); @@ -96,7 +96,7 @@ OnErrorExit: // unpack individual action object -PUBLIC int ActionLoadOne(CtlActionT *action, json_object *actionJ) { +int ActionLoadOne(CtlActionT *action, json_object *actionJ) { char *api = NULL, *verb = NULL, *lua = NULL; int err, modeCount = 0; json_object *callbackJ=NULL; @@ -123,10 +123,10 @@ PUBLIC int ActionLoadOne(CtlActionT *action, json_object *actionJ) { if (callbackJ) { action->type = CTL_TYPE_CB; - modeCount++; + modeCount++; err = PluginGetCB (action, callbackJ); if (err) goto OnErrorExit; - + } // make sure at least one mode is selected @@ -145,7 +145,7 @@ OnErrorExit: return 1; }; -PUBLIC CtlActionT *ActionLoad(json_object *actionsJ) { +CtlActionT *ActionLoad(json_object *actionsJ) { int err; CtlActionT *actions; diff --git a/controller/ctl-config.c b/controller/ctl-config.c index 2e3fc40..95f6db8 100644 --- a/controller/ctl-config.c +++ b/controller/ctl-config.c @@ -28,8 +28,8 @@ // Load control config file -PUBLIC char* CtlConfigSearch(const char *dirList) { - int index, err; +char* CtlConfigSearch(const char *dirList) { + int index; char controlFile [CONTROL_MAXPATH_LEN]; strncpy(controlFile, CONTROL_CONFIG_PRE "-", CONTROL_MAXPATH_LEN); @@ -44,7 +44,7 @@ PUBLIC char* CtlConfigSearch(const char *dirList) { char *filename; char*fullpath; - err = wrap_json_unpack(entryJ, "{s:s, s:s !}", "fullpath", &fullpath, "filename", &filename); + int err = wrap_json_unpack(entryJ, "{s:s, s:s !}", "fullpath", &fullpath, "filename", &filename); if (err) { AFB_ERROR("CTL-INIT HOOPs invalid JSON entry= %s", json_object_get_string(entryJ)); return NULL; @@ -65,7 +65,7 @@ PUBLIC char* CtlConfigSearch(const char *dirList) { return NULL; } -PUBLIC int CtlConfigExec(CtlConfigT *ctlConfig) { +int CtlConfigExec(CtlConfigT *ctlConfig) { // best effort to initialise everything before starting if (ctlConfig->requireJ) { @@ -102,7 +102,7 @@ OnErrorExit: return 1; } -PUBLIC CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) { +CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) { json_object *ctlConfigJ; CtlConfigT *ctlConfig = calloc(1, sizeof (CtlConfigT)); int err; @@ -112,7 +112,7 @@ PUBLIC CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) { if (err) goto OnErrorExit; #endif - // Search for config in filepath + // Search for config in filepath filepath = CtlConfigSearch(filepath); if (!filepath) { @@ -145,7 +145,7 @@ PUBLIC CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) { err = afb_daemon_rename_api(ctlConfig->api); if (err) AFB_WARNING("Fail to rename api to:%s", ctlConfig->api); } - + } //load config sections diff --git a/controller/ctl-config.h b/controller/ctl-config.h index 2e3a16a..3b39f53 100644 --- a/controller/ctl-config.h +++ b/controller/ctl-config.h @@ -21,7 +21,14 @@ #ifndef _CTL_CONFIG_INCLUDE_ #define _CTL_CONFIG_INCLUDE_ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif + +#ifdef __cplusplus +extern "C" { +#endif + #define AFB_BINDING_VERSION 2 #include <afb/afb-binding.h> #include <json-c/json.h> @@ -72,7 +79,7 @@ typedef struct ConfigSectionS { const char *label; const char *info; int (*loadCB)(struct ConfigSectionS *section, json_object *sectionJ); - void *handle; + void *handle; } CtlSectionT; typedef struct { @@ -84,30 +91,28 @@ typedef struct { CtlSectionT *sections; } CtlConfigT; - #ifdef CONTROL_SUPPORT_LUA #include "ctl-lua.h" -#else - typedef void* Lua2cWrapperT; #endif - // ctl-action.c -PUBLIC CtlActionT *ActionLoad(json_object *actionsJ); -PUBLIC int ActionExecOne(CtlActionT* action, json_object *queryJ); -PUBLIC int ActionLoadOne(CtlActionT *action, json_object *actionJ); +CtlActionT *ActionLoad(json_object *actionsJ); +int ActionExecOne(CtlActionT* action, json_object *queryJ); +int ActionLoadOne(CtlActionT *action, json_object *actionJ); // ctl-config.c -PUBLIC CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections); -PUBLIC int CtlConfigExec(CtlConfigT *ctlConfig); +CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections); +int CtlConfigExec(CtlConfigT *ctlConfig); // ctl-onload.c -PUBLIC int OnloadConfig(CtlSectionT *section, json_object *actionsJ); - +int OnloadConfig(CtlSectionT *section, json_object *actionsJ); // ctl-plugin.c -PUBLIC int PluginConfig(CtlSectionT *section, json_object *pluginsJ); -PUBLIC int PluginGetCB (CtlActionT *action , json_object *callbackJ); +int PluginConfig(CtlSectionT *section, json_object *pluginsJ); +int PluginGetCB (CtlActionT *action , json_object *callbackJ); +#ifdef __cplusplus +} +#endif -#endif /* _CTL_CONFIG_INCLUDE_ */
\ No newline at end of file +#endif /* _CTL_CONFIG_INCLUDE_ */ diff --git a/controller/ctl-lua.c b/controller/ctl-lua.c index 7b68fe2..5d959f0 100644 --- a/controller/ctl-lua.c +++ b/controller/ctl-lua.c @@ -611,7 +611,7 @@ STATIC int LuaAfbEventMake(lua_State* luaState) { } // Function call from LUA when lua2c plugin L2C is used -PUBLIC int Lua2cWrapper(lua_State* luaState, char *funcname, Lua2cFunctionT callback) { +int Lua2cWrapper(lua_State* luaState, char *funcname, Lua2cFunctionT callback) { json_object *argsJ= LuaPopArgs(luaState, LUA_FIST_ARG+1); int response = (*callback) (funcname, argsJ); @@ -622,7 +622,7 @@ PUBLIC int Lua2cWrapper(lua_State* luaState, char *funcname, Lua2cFunctionT call } // Call a Lua function from a control action -PUBLIC int LuaCallFunc (CtlActionT *action, json_object *queryJ) { +int LuaCallFunc (CtlActionT *action, json_object *queryJ) { int err, count; @@ -820,15 +820,15 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) { return; } -PUBLIC void ctlapi_execlua (afb_req request) { +void ctlapi_execlua (afb_req request) { LuaDoAction (LUA_DOSTRING, request); } -PUBLIC void ctlapi_request (afb_req request) { +void ctlapi_request (afb_req request) { LuaDoAction (LUA_DOCALL, request); } -PUBLIC void ctlapi_debuglua (afb_req request) { +void ctlapi_debuglua (afb_req request) { LuaDoAction (LUA_DOSCRIPT, request); } @@ -958,7 +958,7 @@ OnErrorExit: } // Register a new L2c list of LUA user plugin commands -PUBLIC void LuaL2cNewLib(const char *label, luaL_Reg *l2cFunc, int count) { +void LuaL2cNewLib(const char *label, luaL_Reg *l2cFunc, int count) { // luaL_newlib(luaState, l2cFunc); macro does not work with pointer :( luaL_checkversion(luaState); lua_createtable(luaState, 0, count+1); @@ -987,8 +987,8 @@ static const luaL_Reg afbFunction[] = { }; // Load Lua Interpreter -PUBLIC int LuaConfigLoad () { - +int LuaConfigLoad () { + // open a new LUA interpretor luaState = luaL_newstate(); @@ -1003,16 +1003,16 @@ PUBLIC int LuaConfigLoad () { // redirect print to AFB_NOTICE luaL_newlib(luaState, afbFunction); lua_setglobal(luaState, "AFB"); - + return 0; - + OnErrorExit: return 1; } // Create Binding Event at Init Exec Time -PUBLIC int LuaConfigExec () { - +int LuaConfigExec () { + int err, index; // create default lua event to send test pause/resume luaDefaultEvt=calloc(1,sizeof(LuaAfbEvent)); @@ -1032,11 +1032,11 @@ PUBLIC int LuaConfigExec () { const char *dirList= getenv("CONTROL_LUA_PATH"); if (!dirList) dirList=CONTROL_LUA_PATH; - // special case for no lua even when avaliable + // special case for no lua even when avaliable if (!strcasecmp ("/dev/null", dirList)) { return 0; } - + json_object *luaScriptPathJ = ScanForConfig(dirList , CTL_SCAN_RECURSIVE, fullprefix, "lua"); // load+exec any file found in LUA search path diff --git a/controller/ctl-lua.h b/controller/ctl-lua.h index 6f2ce7d..628db7f 100644 --- a/controller/ctl-lua.h +++ b/controller/ctl-lua.h @@ -21,7 +21,9 @@ #ifndef _LUA_CTL_INCLUDE_ #define _LUA_CTL_INCLUDE_ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif // prefix start debug script #ifndef CONTROL_DOSCRIPT_PRE @@ -38,16 +40,19 @@ #define CONTROL_LUA_PATH CONTROL_CONFIG_PATH #endif +#define AFB_BINDING_VERSION 2 +#include <afb/afb-binding.h> + +#include <json-c/json.h> // standard lua include file -#ifdef CONTROL_SUPPORT_LUA -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" -#endif +#include <lua.h> +#include <lauxlib.h> +#include <lualib.h> #include "ctl-timer.h" +#include "ctl-config.h" -PUBLIC int LuaLibInit (); +int LuaLibInit (); typedef int (*Lua2cFunctionT)(char *funcname, json_object *argsJ); typedef int (*Lua2cWrapperT) (lua_State* luaState, char *funcname, Lua2cFunctionT callback); @@ -64,14 +69,14 @@ typedef enum { } LuaDoActionT; -PUBLIC int LuaConfigLoad(); -PUBLIC int LuaConfigExec(); -PUBLIC void LuaL2cNewLib(const char *label, luaL_Reg *l2cFunc, int count); -PUBLIC int Lua2cWrapper(lua_State* luaState, char *funcname, Lua2cFunctionT callback); -PUBLIC int LuaCallFunc (CtlActionT *action, json_object *queryJ) ; -PUBLIC void ctlapi_lua_docall (afb_req request); -PUBLIC void ctlapi_lua_dostring (afb_req request); -PUBLIC void ctlapi_lua_doscript (afb_req request); +int LuaConfigLoad(); +int LuaConfigExec(); +void LuaL2cNewLib(const char *label, luaL_Reg *l2cFunc, int count); +int Lua2cWrapper(lua_State* luaState, char *funcname, Lua2cFunctionT callback); +int LuaCallFunc (CtlActionT *action, json_object *queryJ) ; +void ctlapi_lua_docall (afb_req request); +void ctlapi_lua_dostring (afb_req request); +void ctlapi_lua_doscript (afb_req request); -#endif
\ No newline at end of file +#endif diff --git a/controller/ctl-onload.c b/controller/ctl-onload.c index 97bd109..5000542 100644 --- a/controller/ctl-onload.c +++ b/controller/ctl-onload.c @@ -23,19 +23,19 @@ #include "ctl-config.h" // onload section receive one action or an array of actions -PUBLIC int OnloadConfig(CtlSectionT *section, json_object *actionsJ) { +int OnloadConfig(CtlSectionT *section, json_object *actionsJ) { CtlActionT *actions; - + // Load time parse actions in config file if (actionsJ != NULL) { actions= ActionLoad(actionsJ); section->handle=actions; - + if (!actions) { AFB_ERROR ("OnloadLoad config fail processing onload actions"); goto OnErrorExit; } - + } else { // Exec time process onload action now actions=(CtlActionT*)section->handle; @@ -46,7 +46,7 @@ PUBLIC int OnloadConfig(CtlSectionT *section, json_object *actionsJ) { for (int idx=0; actions[idx].source.label != NULL; idx ++) { ActionExecOne(&actions[idx], NULL); - } + } } return 0; diff --git a/controller/ctl-plugin.c b/controller/ctl-plugin.c index f589035..748c78b 100644 --- a/controller/ctl-plugin.c +++ b/controller/ctl-plugin.c @@ -18,52 +18,60 @@ * Json load using json_unpack https://jansson.readthedocs.io/en/2.9/apiref.html#parsing-and-validating-values */ -#define _GNU_SOURCE + #define _GNU_SOURCE #include <string.h> #include <dlfcn.h> +#ifdef CONTROL_SUPPORT_LUA +#include "ctl-lua.h" +#else +typedef struct lua_State lua_State; +typedef void *Lua2cFunctionT; +typedef void *Lua2cWrapperT; +#endif + #include "ctl-config.h" static CtlPluginT *ctlPlugins=NULL; -PUBLIC int PluginGetCB (CtlActionT *action , json_object *callbackJ) { +int PluginGetCB (CtlActionT *action , json_object *callbackJ) { const char *plugin=NULL, *function=NULL; json_object *argsJ; int idx; if (!ctlPlugins) { AFB_ERROR ("PluginGetCB plugin section missing cannot call '%s'", json_object_get_string(callbackJ)); - goto OnErrorExit; + goto OnErrorExit; } - - - int err = wrap_json_unpack(callbackJ, "{ss,ss,s?s,s?o!}", "plugin", &plugin, "function", &function, "args", &argsJ); + + + int err = wrap_json_unpack(callbackJ, "{ss,ss,s?s,s?o!}", "plugin", &plugin, "function", &function, "args", &argsJ); if (err) { AFB_ERROR("PluginGet missing plugin|function|[args] in %s", json_object_get_string(callbackJ)); goto OnErrorExit; } - + for (idx=0; ctlPlugins[idx].label != NULL; idx++) { if (!strcasecmp (ctlPlugins[idx].label, plugin)) break; } - + if (!ctlPlugins[idx].label) { AFB_ERROR ("PluginGetCB no plugin with label=%s", plugin); goto OnErrorExit; } - - action->actionCB = dlsym(ctlPlugins[idx].dlHandle, function); + + action->actionCB = dlsym(ctlPlugins[idx].dlHandle, function); action->source.context = ctlPlugins[idx].context; - + if (!action->actionCB) { - AFB_ERROR ("PluginGetCB no plugin=%s no function=%s", plugin, function); + AFB_ERROR ("PluginGetCB no plugin=%s no function=%s", plugin, function); goto OnErrorExit; } - return 0; + return 0; OnErrorExit: return 1; - + } // Wrapper to Lua2c plugin command add context and delegate to LuaWrapper @@ -82,10 +90,10 @@ STATIC int PluginLoadOne (CtlPluginT *ctlPlugin, json_object *pluginJ, void* han const char*ldSearchPath = NULL, *basename = NULL; void *dlHandle; - + // plugin initialises at 1st load further init actions should be place into onload section if (!pluginJ) return 0; - + int err = wrap_json_unpack(pluginJ, "{ss,s?s,s?s,s?s,ss,s?o,s?o!}", "label", &ctlPlugin->label, "version", &ctlPlugin->version, "info", &ctlPlugin->info, "ldpath", &ldSearchPath, "basename", &basename, "lua2c", &lua2csJ, "actions", &actionsJ); if (err) { @@ -93,8 +101,9 @@ STATIC int PluginLoadOne (CtlPluginT *ctlPlugin, json_object *pluginJ, void* han goto OnErrorExit; } - // if search path not in Json config file, then try default - if (!ldSearchPath) ldSearchPath = CONTROL_PLUGIN_PATH; + // if search path not in Json config file, then try default binding dir + ldSearchPath = getenv("CONTROL_PLUGIN_PATH"); + if (!ldSearchPath) ldSearchPath = strncat(GetBindingDirPath(), "/lib", sizeof(GetBindingDirPath()) - strlen(GetBindingDirPath()) - 1); // search for default policy config file json_object *pluginPathJ = ScanForConfig(ldSearchPath, CTL_SCAN_RECURSIVE, basename, CTL_PLUGIN_EXT); @@ -132,10 +141,10 @@ STATIC int PluginLoadOne (CtlPluginT *ctlPlugin, json_object *pluginJ, void* han } else { AFB_NOTICE("CTL-PLUGIN-LOADONE %s successfully registered", ctlPluginMagic->label); } - + // store dlopen handle to enable onload action at exec time ctlPlugin->dlHandle = dlHandle; - + // Jose hack to make verbosity visible from sharelib struct afb_binding_data_v2 *afbHidenData = dlsym(dlHandle, "afbBindingV2data"); if (afbHidenData) *afbHidenData = afbBindingV2data; @@ -194,15 +203,15 @@ STATIC int PluginLoadOne (CtlPluginT *ctlPlugin, json_object *pluginJ, void* han ctlPlugin->context = (*ctlPluginOnload) (ctlPlugin, handle); } return 0; - + OnErrorExit: return 1; } -PUBLIC int PluginConfig(CtlSectionT *section, json_object *pluginsJ) { +int PluginConfig(CtlSectionT *section, json_object *pluginsJ) { int err=0; - + if (json_object_get_type(pluginsJ) == json_type_array) { int length = json_object_array_length(pluginsJ); ctlPlugins = calloc (length+1, sizeof(CtlPluginT)); @@ -212,8 +221,8 @@ PUBLIC int PluginConfig(CtlSectionT *section, json_object *pluginsJ) { } } else { ctlPlugins = calloc (2, sizeof(CtlPluginT)); - err += PluginLoadOne(&ctlPlugins[0], pluginsJ, section->handle); + err += PluginLoadOne(&ctlPlugins[0], pluginsJ, section->handle); } - + return err; -}
\ No newline at end of file +} diff --git a/controller/ctl-plugin.h b/controller/ctl-plugin.h index c0aa2e2..7c1d0f1 100644 --- a/controller/ctl-plugin.h +++ b/controller/ctl-plugin.h @@ -20,17 +20,16 @@ #ifndef _CTL_PLUGIN_INCLUDE_ #define _CTL_PLUGIN_INCLUDE_ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif + #include <json-c/json.h> #ifndef CTL_PLUGIN_MAGIC #define CTL_PLUGIN_MAGIC 852369147 #endif -#ifndef PUBLIC - #define PUBLIC -#endif - #ifndef STATIC #define STATIC static #endif @@ -71,4 +70,4 @@ typedef void*(*DispatchPluginInstallCbT)(CtlPluginT *plugin, void* handle); #define CTLP_ONLOAD(plugin, handle) void* CtlPluginOnload(CtlPluginT *plugin, void* handle) #define CTLP_CAPI(funcname, source, argsJ, queryJ, context) int funcname(CtlSourceT *source, json_object* argsJ, json_object* queryJ, void* context) -#endif /* _CTL_PLUGIN_INCLUDE_ */
\ No newline at end of file +#endif /* _CTL_PLUGIN_INCLUDE_ */ diff --git a/controller/ctl-timer.c b/controller/ctl-timer.c index 1de6f99..2d99472 100644 --- a/controller/ctl-timer.c +++ b/controller/ctl-timer.c @@ -61,14 +61,14 @@ OnErrorExit: return -1; } -PUBLIC void TimerEvtStop(TimerHandleT *timerHandle) { +void TimerEvtStop(TimerHandleT *timerHandle) { sd_event_source_unref(timerHandle->evtSource); free (timerHandle); } -PUBLIC void TimerEvtStart(TimerHandleT *timerHandle, timerCallbackT callback, void *context) { +void TimerEvtStart(TimerHandleT *timerHandle, timerCallbackT callback, void *context) { uint64_t usec; // populate CB handle @@ -80,13 +80,13 @@ PUBLIC void TimerEvtStart(TimerHandleT *timerHandle, timerCallbackT callback, vo sd_event_add_time(afb_daemon_get_event_loop(), &timerHandle->evtSource, CLOCK_MONOTONIC, usec+timerHandle->delay*1000, 250, TimerNext, timerHandle); } -PUBLIC afb_event TimerEvtGet(void) { +afb_event TimerEvtGet(void) { return afbevt; } // Create Binding Event at Init -PUBLIC int TimerEvtInit () { +int TimerEvtInit () { // create binder event to send test pause/resume afbevt = afb_daemon_make_event("control"); diff --git a/controller/ctl-timer.h b/controller/ctl-timer.h index b08299f..e1a1ef3 100644 --- a/controller/ctl-timer.h +++ b/controller/ctl-timer.h @@ -34,9 +34,9 @@ typedef struct TimerHandleS { sd_event_source *evtSource; } TimerHandleT; -PUBLIC int TimerEvtInit (void); -PUBLIC afb_event TimerEvtGet(void); -PUBLIC void TimerEvtStart(TimerHandleT *timerHandle, timerCallbackT callback, void *context); -PUBLIC void TimerEvtStop(TimerHandleT *timerHandle); +int TimerEvtInit (void); +afb_event TimerEvtGet(void); +void TimerEvtStart(TimerHandleT *timerHandle, timerCallbackT callback, void *context); +void TimerEvtStop(TimerHandleT *timerHandle); #endif // CTL_TIMER_INCLUDE |