From b65178642727536972d0c04e53b2310be58beb2e Mon Sep 17 00:00:00 2001 From: fulup Date: Fri, 11 Aug 2017 19:44:21 +0200 Subject: Almost working version with Json Config, Lua script and sharelib callbacks. --- Controler-afb/ctl-lua.c | 59 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) (limited to 'Controler-afb/ctl-lua.c') diff --git a/Controler-afb/ctl-lua.c b/Controler-afb/ctl-lua.c index 38a3d5f..71ef8b0 100644 --- a/Controler-afb/ctl-lua.c +++ b/Controler-afb/ctl-lua.c @@ -36,11 +36,6 @@ static lua_State* luaState; -typedef enum { - LUA_DOCALL, - LUA_DOSTRING, - LUA_DOSCRIPT, -} LuaDoActionT; #define CTX_MAGIC 123456789 #define CTX_TOKEN "AFB_ctx" @@ -191,6 +186,7 @@ STATIC int LuaPushArgument (json_object *arg) { lua_pushnumber(luaState, json_object_get_double(arg)); break; default: + AFB_ERROR("LuaPushArgument: unsupported Json object type %s", json_object_get_string(arg)); return 0; } @@ -454,7 +450,56 @@ STATIC int LuaAfbService(lua_State* luaState) { // Generated some fake event based on watchdog/counter -PUBLIC void LuaDoAction (LuaDoActionT action, afb_req request) { +PUBLIC int LuaCallFunc (afb_req request, DispatchActionT *action) { + + int err, count=0; + + json_object* queryJ = afb_req_json(request); + json_object* argsJ = action->argsJ; + const char* func = action->call; + + // load function (should exist in CONTROL_PATH_LUA + lua_getglobal(luaState, func); + + // Push AFB client context on the stack + LuaAfbContextT *afbContext= LuaCtxPush(luaState, request, func); + if (!afbContext) goto OnErrorExit; + + // push argsJ on the stack + if (json_object_get_type(argsJ) != json_type_array) { + count+= LuaPushArgument (argsJ); + } else { + for (int idx=0; idx