diff options
author | Fulup Ar Foll <fulup@iot.bzh> | 2017-08-16 19:18:46 +0200 |
---|---|---|
committer | Fulup Ar Foll <fulup@iot.bzh> | 2017-08-16 19:18:46 +0200 |
commit | a4899ab57f08aeb2741d08f74d7593c85a0ad3f4 (patch) | |
tree | 64ee18c96a149f67259ddc166814bf2d74eabe00 /Controler-afb | |
parent | 61f0f263c335ad403c6693a1b8b6f5428ac180a8 (diff) |
Fix DoScript Lua Updated Documentation.
Diffstat (limited to 'Controler-afb')
-rw-r--r-- | Controler-afb/README.md | 9 | ||||
-rw-r--r-- | Controler-afb/ctl-dispatch.c | 3 | ||||
-rw-r--r-- | Controler-afb/ctl-lua.c | 33 |
3 files changed, 27 insertions, 18 deletions
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)); |