diff options
Diffstat (limited to 'Controler-afb')
-rw-r--r-- | Controler-afb/CMakeLists.txt | 6 | ||||
-rw-r--r-- | Controler-afb/ctl-binding.h | 7 | ||||
-rw-r--r-- | Controler-afb/ctl-dispatch.c | 71 | ||||
-rw-r--r-- | Controler-afb/ctl-lua.c | 106 | ||||
-rw-r--r-- | Controler-afb/ctl-plugin-sample.c | 40 |
5 files changed, 127 insertions, 103 deletions
diff --git a/Controler-afb/CMakeLists.txt b/Controler-afb/CMakeLists.txt index 08bc377..64ea5e1 100644 --- a/Controler-afb/CMakeLists.txt +++ b/Controler-afb/CMakeLists.txt @@ -33,8 +33,7 @@ endmacro(SET_TARGET_GENSKEL) PROJECT_TARGET_ADD(control-afb) # Define project Targets - ADD_LIBRARY(${TARGET_NAME} MODULE ctl-binding.c ctl-events.c ctl-dispatch.c ctl-lua.c -) + ADD_LIBRARY(${TARGET_NAME} MODULE ctl-binding.c ctl-events.c ctl-dispatch.c ctl-lua.c) # Generate API-v2 hat from OpenAPI json definition SET_TARGET_GENSKEL(${TARGET_NAME} ctl-apidef) @@ -64,7 +63,8 @@ PROJECT_TARGET_ADD(ctl-plugin-sample) # Alsa Plugin properties SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES - PREFIX "audio-" + PREFIX ${CTL_PLUGIN_PRE} + SUFFIX ${CTL_PLUGIN_EXT} OUTPUT_NAME ${TARGET_NAME} ) diff --git a/Controler-afb/ctl-binding.h b/Controler-afb/ctl-binding.h index 77d1b6e..abcb2f4 100644 --- a/Controler-afb/ctl-binding.h +++ b/Controler-afb/ctl-binding.h @@ -28,6 +28,7 @@ #endif #define STATIC static + // polctl-binding.c PUBLIC int CtlBindingInit (); @@ -50,6 +51,10 @@ typedef enum { CTL_NONE=-1 } DispatchCtlEnumT; +typedef enum { + CTL_SCAN_FLAT=0, + CTL_SCAN_RECURSIVE=1, +} CtlScanDirModeT; typedef enum { CTL_MODE_NONE=0, @@ -74,7 +79,7 @@ PUBLIC void ctlapi_dispatch (DispatchCtlEnumT control, afb_req request); // ctl-lua.c PUBLIC int LuaLibInit (); -PUBLIC json_object* ScanForConfig (char* searchPath, char * pre, char *ext); +PUBLIC json_object* ScanForConfig (char* searchPath, CtlScanDirModeT mode, char *pre, char *ext); PUBLIC void ctlapi_lua_docall (afb_req request); PUBLIC void ctlapi_lua_dostring (afb_req request); PUBLIC void ctlapi_lua_doscript (afb_req request); 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"); diff --git a/Controler-afb/ctl-lua.c b/Controler-afb/ctl-lua.c index 8b40e38..38a3d5f 100644 --- a/Controler-afb/ctl-lua.c +++ b/Controler-afb/ctl-lua.c @@ -70,66 +70,35 @@ typedef enum { * https://stackoverflow.com/questions/45596493/lua-using-lua-newuserdata-from-lua-pcall */ -STATIC LuaAfbContextT *LuaCtxCheck (lua_State *luaState, int index) { - LuaAfbContextT *afbContext; - //luaL_checktype(luaState, index, LUA_TUSERDATA); - //afbContext = (LuaAfbContextT *)luaL_checkudata(luaState, index, CTX_TOKEN); - luaL_checktype(luaState, index, LUA_TLIGHTUSERDATA); - afbContext = (LuaAfbContextT *) lua_touserdata(luaState, index); - if (afbContext == NULL && afbContext->ctxMagic != CTX_MAGIC) { - luaL_error(luaState, "Fail to retrieve user data context=%s", CTX_TOKEN); - AFB_ERROR ("afbContextCheck error retrieving afbContext"); - return NULL; - } - return afbContext; -} - -STATIC LuaAfbContextT *LuaCtxPush (lua_State *luaState, afb_req request, const char* info) { - // LuaAfbContextT *afbContext = (LuaAfbContextT *)lua_newuserdata(luaState, sizeof(LuaAfbContextT)); - // luaL_setmetatable(luaState, CTX_TOKEN); - LuaAfbContextT *afbContext = (LuaAfbContextT *)calloc(1, sizeof(LuaAfbContextT)); - lua_pushlightuserdata(luaState, afbContext); - if (!afbContext) { - AFB_ERROR ("LuaCtxPush fail to allocate user data context"); - return NULL; - } - afbContext->ctxMagic=CTX_MAGIC; - afbContext->info=strdup(info); - afbContext->request= request; - return afbContext; -} - -STATIC void LuaCtxFree (LuaAfbContextT *afbContext) { - free (afbContext->info); -} - // List Avaliable Configuration Files -PUBLIC json_object* ScanForConfig (char* searchPath, char *pre, char *ext) { +PUBLIC json_object* ScanForConfig (char* searchPath, CtlScanDirModeT mode, char *pre, char *ext) { json_object *responseJ; - DIR *dirHandle; char *dirPath; char* dirList= strdup(searchPath); size_t extLen=0; - void ScanDir (char *dirpath) { + void ScanDir (char *searchPath) { + DIR *dirHandle; struct dirent *dirEnt; - dirHandle = opendir (dirPath); + dirHandle = opendir (searchPath); if (!dirHandle) { - AFB_NOTICE ("CONFIG-SCANNING dir=%s not readable", dirPath); + AFB_NOTICE ("CONFIG-SCANNING dir=%s not readable", searchPath); return; } - AFB_NOTICE ("CONFIG-SCANNING:ctl_listconfig scanning: %s", dirPath); + //AFB_NOTICE ("CONFIG-SCANNING:ctl_listconfig scanning: %s", searchPath); while ((dirEnt = readdir(dirHandle)) != NULL) { // recursively search embedded directories ignoring any directory starting by '.' or '_' - if (dirEnt->d_type == DT_DIR) { + if (dirEnt->d_type == DT_DIR && mode == CTL_SCAN_RECURSIVE) { char newpath[CONTROL_MAXPATH_LEN]; if (dirEnt->d_name[0]=='.' || dirEnt->d_name[0]=='_') continue; - strncpy(newpath, dirpath, sizeof(newpath)); + strncpy(newpath, searchPath, sizeof(newpath)); strncat(newpath, "/", sizeof(newpath)); strncat(newpath, dirEnt->d_name, sizeof(newpath)); + ScanDir(newpath); + continue; } // Unknown type is accepted to support dump filesystems @@ -142,12 +111,12 @@ PUBLIC json_object* ScanForConfig (char* searchPath, char *pre, char *ext) { if (ext && strcasecmp (ext, &dirEnt->d_name[extIdx])) continue; struct json_object *pathJ = json_object_new_object(); - json_object_object_add(pathJ, "dirpath", json_object_new_string(dirPath)); + json_object_object_add(pathJ, "fullpath", json_object_new_string(searchPath)); json_object_object_add(pathJ, "filename", json_object_new_string(dirEnt->d_name)); json_object_array_add(responseJ, pathJ); } - closedir(dirHandle); } + closedir(dirHandle); } if (ext) extLen=strlen(ext); @@ -162,6 +131,39 @@ PUBLIC json_object* ScanForConfig (char* searchPath, char *pre, char *ext) { return (responseJ); } +STATIC LuaAfbContextT *LuaCtxCheck (lua_State *luaState, int index) { + LuaAfbContextT *afbContext; + //luaL_checktype(luaState, index, LUA_TUSERDATA); + //afbContext = (LuaAfbContextT *)luaL_checkudata(luaState, index, CTX_TOKEN); + luaL_checktype(luaState, index, LUA_TLIGHTUSERDATA); + afbContext = (LuaAfbContextT *) lua_touserdata(luaState, index); + if (afbContext == NULL && afbContext->ctxMagic != CTX_MAGIC) { + luaL_error(luaState, "Fail to retrieve user data context=%s", CTX_TOKEN); + AFB_ERROR ("afbContextCheck error retrieving afbContext"); + return NULL; + } + return afbContext; +} + +STATIC LuaAfbContextT *LuaCtxPush (lua_State *luaState, afb_req request, const char* info) { + // LuaAfbContextT *afbContext = (LuaAfbContextT *)lua_newuserdata(luaState, sizeof(LuaAfbContextT)); + // luaL_setmetatable(luaState, CTX_TOKEN); + LuaAfbContextT *afbContext = (LuaAfbContextT *)calloc(1, sizeof(LuaAfbContextT)); + lua_pushlightuserdata(luaState, afbContext); + if (!afbContext) { + AFB_ERROR ("LuaCtxPush fail to allocate user data context"); + return NULL; + } + afbContext->ctxMagic=CTX_MAGIC; + afbContext->info=strdup(info); + afbContext->request= request; + return afbContext; +} + +STATIC void LuaCtxFree (LuaAfbContextT *afbContext) { + free (afbContext->info); +} + STATIC int LuaPushArgument (json_object *arg) { switch (json_object_get_type(arg)) { @@ -511,7 +513,7 @@ PUBLIC void LuaDoAction (LuaDoActionT action, afb_req request) { // scan luascript search path once static json_object *luaScriptPathJ =NULL; - if (!luaScriptPathJ) luaScriptPathJ= ScanForConfig(CONTROL_LUA_PATH , NULL, "lua"); + if (!luaScriptPathJ) luaScriptPathJ= ScanForConfig(CONTROL_LUA_PATH , CTL_SCAN_FLAT, NULL, "lua"); err= wrap_json_unpack (queryJ, "{s:s, s?o s?o !}", "script", &script,"args", &args, "arg", &args); if (err) { @@ -537,10 +539,10 @@ PUBLIC void LuaDoAction (LuaDoActionT action, afb_req request) { } for (index=0; index < json_object_array_length(luaScriptPathJ); index++) { - char *filename; char*dirpath; + char *filename; char*fullpath; json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, index); - err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "dirpath", &dirpath,"filename", &filename); + err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "fullpath", &fullpath,"filename", &filename); if (err) { AFB_ERROR ("LUA-DOSCRIPT HOOPs invalid LUA script path = %s", json_object_get_string(entryJ)); goto OnErrorExit; @@ -548,8 +550,8 @@ PUBLIC void LuaDoAction (LuaDoActionT action, afb_req request) { if (strcmp(filename, script)) continue; - char filepath[255]; - strncpy(filepath, dirpath, sizeof(filepath)); + char filepath[CONTROL_MAXPATH_LEN]; + strncpy(filepath, fullpath, sizeof(filepath)); strncat(filepath, "/", sizeof(filepath)); strncat(filepath, filename, sizeof(filepath)); err= luaL_loadfile(luaState, filepath); @@ -616,7 +618,7 @@ PUBLIC int LuaLibInit () { int err, index; // search for default policy config file - json_object *luaScriptPathJ = ScanForConfig(CONTROL_LUA_PATH , "onload", "lua"); + json_object *luaScriptPathJ = ScanForConfig(CONTROL_LUA_PATH , CTL_SCAN_RECURSIVE, "onload", "lua"); // open a new LUA interpretor luaState = luaL_newstate(); @@ -636,15 +638,15 @@ PUBLIC int LuaLibInit () { for (index=0; index < json_object_array_length(luaScriptPathJ); index++) { json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, 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 ("LUA-INIT HOOPs invalid config file path = %s", json_object_get_string(entryJ)); goto OnErrorExit; } char filepath[CONTROL_MAXPATH_LEN]; - strncpy(filepath, dirpath, sizeof(filepath)); + strncpy(filepath, fullpath, sizeof(filepath)); strncat(filepath, "/", sizeof(filepath)); strncat(filepath, filename, sizeof(filepath)); err= luaL_loadfile(luaState, filepath); diff --git a/Controler-afb/ctl-plugin-sample.c b/Controler-afb/ctl-plugin-sample.c index f232573..f7665f0 100644 --- a/Controler-afb/ctl-plugin-sample.c +++ b/Controler-afb/ctl-plugin-sample.c @@ -31,6 +31,20 @@ typedef struct { int count; } MyPluginCtxT; +// This tag is mandotory and used as sanity check when loading plugin +PUBLIC ulong CtlPluginMagic=CTL_PLUGIN_MAGIC; + +// Call at initialisation time +PUBLIC void* CtlPluginOnload(char* label, char* version, char* info) { + MyPluginCtxT *pluginCtx= (MyPluginCtxT*)calloc (1, sizeof(MyPluginCtxT)); + pluginCtx->magic = MY_PLUGIN_MAGIC; + pluginCtx->count = 0; + + fprintf(stderr, "*** CONTROLER-PLUGIN-SAMPLE:Install label=%s version=%s info=%s", label, info, version); + AFB_NOTICE ("CONTROLER-PLUGIN-SAMPLE:Install label=%s version=%s info=%s", label, info, version); + return (void*)pluginCtx; +} + STATIC const char* jsonToString (json_object *valueJ) { const char *value; if (valueJ) @@ -41,24 +55,18 @@ STATIC const char* jsonToString (json_object *valueJ) { return value; } -PUBLIC int SamplePolicyInstall (DispatchActionT *action, json_object *response, void *context) { - - MyPluginCtxT *pluginCtx= (MyPluginCtxT*)calloc (1, sizeof(MyPluginCtxT)); - pluginCtx->magic = MY_PLUGIN_MAGIC; - pluginCtx->count = 0; - - AFB_INFO ("CONTROLER-PLUGIN-SAMPLE SamplePolicyInstall action=%s args=%s", action->label, jsonToString(action->argsJ)); - - return 0; -} -PUBLIC int SamplePolicyCount (DispatchActionT *action, json_object *response, void *context) { +PUBLIC void SamplePolicyCount (afb_req request, DispatchActionT *action, void *context) { MyPluginCtxT *pluginCtx= (MyPluginCtxT*)context; - //pluginCtx->magic = MY_PLUGIN_MAGIC; - //pluginCtx->count = 0; - - AFB_INFO ("CONTROLER-PLUGIN-SAMPLE SamplePolicyCount action=%s args=%s count=%d", action->label, jsonToString(action->argsJ), pluginCtx->count); - return 0; + if (!context || pluginCtx->magic != MY_PLUGIN_MAGIC) { + AFB_ERROR("CONTROLER-PLUGIN-SAMPLE:count (Hoops) Invalid Sample Plugin Context"); + return; + + }; + + pluginCtx->count++; + + AFB_INFO ("CONTROLER-PLUGIN-SAMPLE:Count SamplePolicyCount action=%s args=%s count=%d", action->label, jsonToString(action->argsJ), pluginCtx->count); } |