aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-12-14 23:21:32 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2017-12-19 16:36:30 +0100
commit351c6ca6d1dc2fa2c532d549bc84cca8837f1bc7 (patch)
tree826c1488c91659f1159b25289a72d09c01ae8aee
parent796889d6b3aff40b3247e263675ea28926bb97a0 (diff)
Get back on track l2c functions.
Change-Id: I8425f0de60a35b4e287c8829fe72cbca80bc55df Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r--ctl-lib/ctl-action.c6
-rw-r--r--ctl-lib/ctl-config.h5
-rw-r--r--ctl-lib/ctl-lua.c15
-rw-r--r--ctl-lib/ctl-lua.h2
-rw-r--r--ctl-lib/ctl-plugin.c33
-rw-r--r--ctl-lib/ctl-plugin.h2
6 files changed, 48 insertions, 15 deletions
diff --git a/ctl-lib/ctl-action.c b/ctl-lib/ctl-action.c
index fc25d5b..e4306f9 100644
--- a/ctl-lib/ctl-action.c
+++ b/ctl-lib/ctl-action.c
@@ -24,6 +24,8 @@
#include "ctl-config.h"
+extern CtlLua2cFuncT *ctlLua2cFunc;
+
PUBLIC int ActionLabelToIndex(CtlActionT*actions, const char* actionLabel) {
for (int idx = 0; actions[idx].uid; idx++) {
if (!strcasecmp(actionLabel, actions[idx].uid)) return idx;
@@ -56,6 +58,10 @@ PUBLIC void ActionExecUID(AFB_ReqT request, CtlConfigT *ctlConfig, const char *u
PUBLIC void ActionExecOne(CtlSourceT *source, CtlActionT* action, json_object *queryJ) {
int err = 0;
+ if(action->type == CTL_TYPE_LUA && ctlLua2cFunc->l2cCount) {
+ LuaL2cNewLib (ctlLua2cFunc->l2cFunc, ctlLua2cFunc->l2cCount);
+ }
+
switch (action->type) {
case CTL_TYPE_API:
{
diff --git a/ctl-lib/ctl-config.h b/ctl-lib/ctl-config.h
index d6967bc..04e50de 100644
--- a/ctl-lib/ctl-config.h
+++ b/ctl-lib/ctl-config.h
@@ -67,6 +67,11 @@ typedef struct {
#ifdef CONTROL_SUPPORT_LUA
#include "ctl-lua.h"
+
+ typedef struct CtlLua2cFuncT {
+ luaL_Reg *l2cFunc;
+ int l2cCount;
+} CtlLua2cFuncT;
#endif
// This should not be global as application may want to define their own sections
diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c
index f8ab69d..f487ed4 100644
--- a/ctl-lib/ctl-lua.c
+++ b/ctl-lib/ctl-lua.c
@@ -31,7 +31,7 @@
#include "ctl-config.h"
-#define LUA_FIST_ARG 2 // when using luaL_newlib calllback receive libtable as 1st arg
+#define LUA_FIST_ARG 2 // when using luaL_newlib calllback receive libtable as 1st arg
#define LUA_MSG_MAX_LENGTH 512
#define JSON_ERROR (json_object*)-1
@@ -666,19 +666,22 @@ OnErrorExit:
}
// Function call from LUA when lua2c plugin L2C is used
+// Rfor: Not using LUA_FIRST_ARG here because we didn't use
+// luaL_newlib function, so first args is on stack at first
+// position.
PUBLIC int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback) {
lua_State* luaState = (lua_State*)luaHandle;
json_object *responseJ=NULL;
- CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG);
+ CtlSourceT *source= LuaSourcePop(luaState, 1);
- json_object *argsJ= LuaPopArgs(source, luaState, LUA_FIST_ARG+1);
+ json_object *argsJ= LuaPopArgs(source, luaState, 2);
int err= (*callback) (source, argsJ, &responseJ);
// push error code and eventual response to LUA
int count=1;
lua_pushinteger (luaState, err);
- if (!responseJ) count += LuaPushArgument (source, responseJ);
+ count += LuaPushArgument (source, responseJ);
return count;
}
@@ -1202,12 +1205,12 @@ OnErrorExit:
// Register a new L2c list of LUA user plugin commands
-PUBLIC void LuaL2cNewLib(const char *uid, luaL_Reg *l2cFunc, int count) {
+PUBLIC void LuaL2cNewLib(luaL_Reg *l2cFunc, int count) {
// luaL_newlib(luaState, l2cFunc); macro does not work with pointer :(
luaL_checkversion(luaState);
lua_createtable(luaState, 0, count+1);
luaL_setfuncs(luaState,l2cFunc,0);
- lua_setglobal(luaState, uid);
+ lua_setglobal(luaState, "_lua2c");
}
static const luaL_Reg afbFunction[] = {
diff --git a/ctl-lib/ctl-lua.h b/ctl-lib/ctl-lua.h
index 3a70b71..b3aea54 100644
--- a/ctl-lib/ctl-lua.h
+++ b/ctl-lib/ctl-lua.h
@@ -65,7 +65,7 @@ typedef enum {
PUBLIC int LuaConfigLoad (AFB_ApiT apiHandle);
PUBLIC int LuaConfigExec(AFB_ApiT apiHandle, const char * prefix);
-PUBLIC void LuaL2cNewLib(const char *uid, luaL_Reg *l2cFunc, int count);
+PUBLIC void LuaL2cNewLib(luaL_Reg *l2cFunc, int count);
PUBLIC int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback);
PUBLIC int LuaCallFunc (CtlSourceT *source, CtlActionT *action, json_object *queryJ) ;
PUBLIC void ctlapi_lua_docall (afb_req request);
diff --git a/ctl-lib/ctl-plugin.c b/ctl-lib/ctl-plugin.c
index 986512f..42f9d9e 100644
--- a/ctl-lib/ctl-plugin.c
+++ b/ctl-lib/ctl-plugin.c
@@ -24,7 +24,11 @@
#include "ctl-config.h"
-static CtlPluginT *ctlPlugins=NULL;
+#ifdef CONTROL_SUPPORT_LUA
+CtlLua2cFuncT *ctlLua2cFunc = NULL;
+#endif
+
+static CtlPluginT *ctlPlugins = NULL;
PUBLIC int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *callbackJ) {
const char *plugin=NULL, *function=NULL;
@@ -162,6 +166,8 @@ STATIC int PluginLoadOne (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object
*lua2cInPlug = DispatchOneL2c;
int Lua2cAddOne(luaL_Reg *l2cFunc, const char* l2cName, int index) {
+ if(ctlLua2cFunc->l2cCount)
+ {index += ctlLua2cFunc->l2cCount+1;}
char funcName[CONTROL_MAXPATH_LEN];
strncpy(funcName, "lua2c_", strlen ("lua2c_")+1);
strncat(funcName, l2cName, strlen (l2cName));
@@ -177,28 +183,43 @@ STATIC int PluginLoadOne (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object
return 0;
}
- int errCount = 0;
+ int count = 0, errCount = 0;
luaL_Reg *l2cFunc = NULL;
+ if(!ctlLua2cFunc) {
+ ctlLua2cFunc = calloc(1, sizeof(CtlLua2cFuncT));
+ }
// look on l2c command and push them to LUA
if (json_object_get_type(lua2csJ) == json_type_array) {
int length = json_object_array_length(lua2csJ);
- l2cFunc = calloc(length + 1, sizeof (luaL_Reg));
- for (int count = 0; count < length; count++) {
+ l2cFunc = calloc(length + ctlLua2cFunc->l2cCount + 1, sizeof (luaL_Reg));
+ for (count = 0; count < length; count++) {
int err;
const char *l2cName = json_object_get_string(json_object_array_get_idx(lua2csJ, count));
err = Lua2cAddOne(l2cFunc, l2cName, count);
if (err) errCount++;
}
} else {
- l2cFunc = calloc(2, sizeof (luaL_Reg));
+ l2cFunc = calloc(2 + ctlLua2cFunc->l2cCount, sizeof (luaL_Reg));
const char *l2cName = json_object_get_string(lua2csJ);
- errCount = Lua2cAddOne(l2cFunc, l2cName, 0);
+ errCount = Lua2cAddOne(l2cFunc, l2cName, count);
+ count++;
}
if (errCount) {
AFB_ApiError(apiHandle, "CTL-PLUGIN-LOADONE %d symbols not found in plugin='%s'", errCount, pluginpath);
goto OnErrorExit;
}
+ int total = ctlLua2cFunc->l2cCount + count;
+ if(ctlLua2cFunc->l2cCount) {
+ for (int offset = ctlLua2cFunc->l2cCount; offset < total; offset++)
+ {
+ int index = offset - ctlLua2cFunc->l2cCount;
+ l2cFunc[index] = ctlLua2cFunc->l2cFunc[index];
+ }
+ free(ctlLua2cFunc->l2cFunc);
+ }
+ ctlLua2cFunc->l2cFunc = l2cFunc;
+ ctlLua2cFunc->l2cCount = total;
}
#endif
DispatchPluginInstallCbT ctlPluginOnload = dlsym(dlHandle, "CtlPluginOnload");
diff --git a/ctl-lib/ctl-plugin.h b/ctl-lib/ctl-plugin.h
index 7210322..eaceea5 100644
--- a/ctl-lib/ctl-plugin.h
+++ b/ctl-lib/ctl-plugin.h
@@ -163,7 +163,6 @@ extern "C" {
#endif
-
typedef struct {
const char *uid;
const long magic;
@@ -177,7 +176,6 @@ typedef struct {
void *context;
} CtlPluginT;
-
typedef enum {
CTL_TYPE_NONE=0,
CTL_TYPE_API,