From a4899ab57f08aeb2741d08f74d7593c85a0ad3f4 Mon Sep 17 00:00:00 2001 From: Fulup Ar Foll Date: Wed, 16 Aug 2017 19:18:46 +0200 Subject: Fix DoScript Lua Updated Documentation. --- Audio-Common/filescan-utils.c | 4 ++-- Audio-Common/filescan-utils.h | 2 +- Controler-afb/README.md | 9 ++++++++ Controler-afb/ctl-dispatch.c | 3 +-- Controler-afb/ctl-lua.c | 33 ++++++++++++++-------------- conf.d/project/lua.d/doscript-helloworld.lua | 8 +++---- htdocs/audio-control.html | 2 +- 7 files changed, 35 insertions(+), 26 deletions(-) diff --git a/Audio-Common/filescan-utils.c b/Audio-Common/filescan-utils.c index 8bb66d4..33dacdf 100644 --- a/Audio-Common/filescan-utils.c +++ b/Audio-Common/filescan-utils.c @@ -27,7 +27,7 @@ // List Avaliable Configuration Files -PUBLIC json_object* ScanForConfig (char* searchPath, CtlScanDirModeT mode, char *pre, char *ext) { +PUBLIC json_object* ScanForConfig (char* searchPath, CtlScanDirModeT mode, const char *pre, const char *ext) { json_object *responseJ; char *dirPath; char* dirList= strdup(searchPath); @@ -95,7 +95,7 @@ PUBLIC const char *GetMidleName(const char*name) { if (fullname[idx] == '-') { start = idx + 1; for (int jdx = start; fullname[jdx] != '\0'; jdx++) { - if (fullname[jdx] == '-') { + if (fullname[jdx] == '-' || fullname[jdx] == '.' || fullname[jdx] == '\0') { fullname[jdx] = '\0'; return &fullname[start]; break; diff --git a/Audio-Common/filescan-utils.h b/Audio-Common/filescan-utils.h index eba504e..671e728 100644 --- a/Audio-Common/filescan-utils.h +++ b/Audio-Common/filescan-utils.h @@ -35,7 +35,7 @@ typedef enum { PUBLIC const char *GetMidleName(const char*name); PUBLIC const char *GetBinderName(); -PUBLIC json_object* ScanForConfig (char* searchPath, CtlScanDirModeT mode, char *pre, char *ext); +PUBLIC json_object* ScanForConfig (char* searchPath, CtlScanDirModeT mode, const char *pre, const char *ext); #endif /* FILESCAN_UTILS_H */ diff --git a/Controler-afb/README.md b/Controler-afb/README.md index 3abe762..a81d4f7 100644 --- a/Controler-afb/README.md +++ b/Controler-afb/README.md @@ -95,6 +95,15 @@ Controler support tree categories of actions. Each action return a status status Note: Lua added functions systematically prefix. AGL standard AppFw functions are prefixed with AGL: (eg: AGL:notice(), AGL_success(), ...). User Lua functions added though the plugin and CTLP_Lua2C are prefix with plugin label (eg: MyPlug:HelloWorld1). +### Debugging Facilities + +Controler Lua script are check for syntax from CMAKE template with Luac. When needed to go further an developer API allow to +execute directly Lua command within controller context from Rest/Ws (api=control, verb=lua_doscript). DoScript API takes two +other optional arguments func=xxxx where xxxx is the function to execute within Lua script and args a JSON object to provide +input parameter. When funcname is not given by default the controller try to execute middle filename doscript-xxxx-????.lua. + +When executed from controller Lua script may use any AppFw Apis as well as any L2C user defined commands in plugin. + ## Config Sample Here after a simple configuration sample. diff --git a/Controler-afb/ctl-dispatch.c b/Controler-afb/ctl-dispatch.c index 22bee27..1afb846 100644 --- a/Controler-afb/ctl-dispatch.c +++ b/Controler-afb/ctl-dispatch.c @@ -589,8 +589,7 @@ PUBLIC int DispatchInit() { int index, err, luaLoaded = 0; char controlFile [CONTROL_MAXPATH_LEN]; - strncpy(controlFile, CONTROL_CONFIG_PRE, CONTROL_MAXPATH_LEN); - strncat(controlFile, "-", CONTROL_MAXPATH_LEN); + strncpy(controlFile, CONTROL_CONFIG_PRE "-", CONTROL_MAXPATH_LEN); strncat(controlFile, GetBinderName(), CONTROL_MAXPATH_LEN); // search for default dispatch config file diff --git a/Controler-afb/ctl-lua.c b/Controler-afb/ctl-lua.c index affeb83..7c59186 100644 --- a/Controler-afb/ctl-lua.c +++ b/Controler-afb/ctl-lua.c @@ -595,7 +595,6 @@ PUBLIC int LuaCallFunc (DispatchActionT *action, json_object *queryJ) { STATIC void LuaDoAction (LuaDoActionT action, afb_req request) { int err, count=0; - const char *middleName=NULL; json_object* queryJ = afb_req_json(request); @@ -645,23 +644,23 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) { case LUA_DOSCRIPT: { // Fulup need to fix argument passing const char *script; - char*func; + char*func=NULL; char *filename; char*fullpath; char luaScriptPath[CONTROL_MAXPATH_LEN]; - json_object *argsJ; + json_object *argsJ=NULL; int index; // scan luascript search path once static json_object *luaScriptPathJ =NULL; - if (!luaScriptPathJ) luaScriptPathJ= ScanForConfig(CONTROL_LUA_PATH , CTL_SCAN_RECURSIVE, CONTROL_DOSCRIPT_PRE, "lua"); - err= wrap_json_unpack (queryJ, "{s:s, s?s s?o s?o !}", "script", &script,"func", &func, "arg", &argsJ); + err= wrap_json_unpack (queryJ, "{s:s, s?s s?o !}", "script", &script,"func", &func, "args", &argsJ); if (err) { AFB_ERROR ("LUA-DOSCRIPT-SYNTAX:missing script|(args,arg) query=%s", json_object_get_string(queryJ)); goto OnErrorExit; } // search for filename=script in CONTROL_LUA_PATH + if (!luaScriptPathJ) luaScriptPathJ= ScanForConfig(CONTROL_LUA_PATH , CTL_SCAN_RECURSIVE,CONTROL_DOSCRIPT_PRE "-", script); for (index=0; index < json_object_array_length(luaScriptPathJ); index++) { json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, index); @@ -671,17 +670,20 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) { goto OnErrorExit; } - if (!middleName && !strcmp (filename, script)) { - middleName= GetMidleName(script); + if (index > 0) AFB_WARNING("LUA-DOSCRIPT-SCAN:Ignore second script=%s path=%s", filename, fullpath); + else { strncpy (luaScriptPath, fullpath, sizeof(luaScriptPath)); strncat (luaScriptPath, "/", sizeof(luaScriptPath)); - strncat (luaScriptPath, filename, sizeof(luaScriptPath)); - - } else { - AFB_WARNING("LUA-DOSCRIPT-SCAN:Ignore second script=%s path=%s", filename, fullpath); - } + strncat (luaScriptPath, filename, sizeof(luaScriptPath)); + } } + err= luaL_loadfile(luaState, luaScriptPath); + if (err) { + AFB_ERROR ("LUA-DOSCRIPT HOOPs Error in LUA loading scripts=%s err=%s", luaScriptPath, lua_tostring(luaState,-1)); + goto OnErrorExit; + } + // script was loaded we need to parse to make it executable err=lua_pcall(luaState, 0, 0, 0); if (err) { @@ -708,7 +710,7 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) { lua_pushnil(luaState); count++; } else { - count+= LuaPushArgument (argsJ); + count+= LuaPushArgument(argsJ); } break; @@ -722,7 +724,7 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) { // effectively exec LUA code (afb_reply/fail done later from callback) err=lua_pcall(luaState, count+1, 0, 0); if (err) { - AFB_ERROR ("LUA-DO-EXEC:FAIL query=%s err=%s", json_object_get_string(queryJ), lua_tostring(luaState,-1) ); + AFB_ERROR ("LUA-DO-EXEC:FAIL query=%s err=%s", json_object_get_string(queryJ), lua_tostring(luaState,-1)); goto OnErrorExit; } return; @@ -775,8 +777,7 @@ PUBLIC int LuaLibInit () { // search for default policy config file char fullprefix[CONTROL_MAXPATH_LEN]; - strncpy (fullprefix, CONTROL_CONFIG_PRE, sizeof(fullprefix)); - strncat (fullprefix, "-", sizeof(fullprefix)); + strncpy (fullprefix, CONTROL_CONFIG_PRE "-", sizeof(fullprefix)); strncat (fullprefix, GetBinderName(), sizeof(fullprefix)); strncat (fullprefix, "-", sizeof(fullprefix)); diff --git a/conf.d/project/lua.d/doscript-helloworld.lua b/conf.d/project/lua.d/doscript-helloworld.lua index b70ba8d..64bc94a 100644 --- a/conf.d/project/lua.d/doscript-helloworld.lua +++ b/conf.d/project/lua.d/doscript-helloworld.lua @@ -23,14 +23,14 @@ function helloworld (request, query) - AFB:notice ("LUA HelloWorld: simple test query=%s", query); + AFB:notice ("LUA HelloWorld: Simple test query=%s", query); if (query == nil) then - AFB:error ("LUA HelloWorld:FX query should not be empty"); + AFB:notice ("LUA HelloWorld:FX query should not be empty"); AFB:fail (request, "LUA HelloWorld: query should not be empty"); else - AFB:error ("LUA HelloWorld:OK query=%s", query); - AFB:sucess (request, {arg0="Demat", arg1="Bonjours", arg2="Gootentag", arg3="Morning"}); + AFB:notice ("LUA HelloWorld:OK query=%s", query); + AFB:success (request, {arg0="Demat", arg1="Bonjours", arg2="Gootentag", arg3="Morning"}); end end \ No newline at end of file diff --git a/htdocs/audio-control.html b/htdocs/audio-control.html index 89addcc..d51f527 100644 --- a/htdocs/audio-control.html +++ b/htdocs/audio-control.html @@ -31,7 +31,7 @@
  • -
  • +
  • -- cgit 1.2.3-korg