aboutsummaryrefslogtreecommitdiffstats
path: root/controller
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-09-20 16:54:14 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-12-14 11:00:25 +0100
commit78202d5bd8a8d09a55bb71b9df6d3f54b18184b8 (patch)
treeafbc89a7c104a70e7771939765309e977a5ab96a /controller
parentb68f95e384aa35b54e453b5e7d09dabf8d82340c (diff)
Make LUA works
- Added LUA library at link - Make app-template crash build if a checked file fails - Fix old defined variable CONTROL_LUA_PATH to a default one in the binding data directory - Add an example with unit conversion made in LUA Change-Id: I1b4712bde32a38044ad6b14ebd38b6782fd01fe9 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'controller')
-rw-r--r--controller/CMakeLists.txt10
-rw-r--r--controller/ctl-config.c5
-rw-r--r--controller/ctl-config.h16
-rw-r--r--controller/ctl-lua.c15
-rw-r--r--controller/ctl-lua.h16
-rw-r--r--controller/ctl-plugin.c8
-rw-r--r--controller/ctl-plugin.h8
7 files changed, 42 insertions, 36 deletions
diff --git a/controller/CMakeLists.txt b/controller/CMakeLists.txt
index b3d0c32..6b4662e 100644
--- a/controller/CMakeLists.txt
+++ b/controller/CMakeLists.txt
@@ -15,16 +15,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################
-
# Include LUA only when requested
-if(CONTROL_SUPPORT_LUA)
+get_property(COMPDEF DIRECTORY PROPERTY COMPILE_DEFINITIONS)
+if("CONTROL_SUPPORT_LUA=1" IN_LIST COMPDEF)
message(STATUS "Notice: LUA Controler Support Selected")
set(CTL_LUA_SOURCE ctl-lua.c ctl-timer.c)
ADD_COMPILE_OPTIONS(-DCONTROL_SUPPORT_LUA)
-else(CONTROL_SUPPORT_LUA)
+else()
message(STATUS "Warning: LUA Without Support ")
-endif(CONTROL_SUPPORT_LUA)
-
+endif()
# Add target to project dependency list
PROJECT_TARGET_ADD(afb-controller)
@@ -41,4 +40,3 @@ PROJECT_TARGET_ADD(afb-controller)
TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
)
-
diff --git a/controller/ctl-config.c b/controller/ctl-config.c
index 888b420..580c68c 100644
--- a/controller/ctl-config.c
+++ b/controller/ctl-config.c
@@ -155,6 +155,9 @@ int CtlConfigExec(CtlConfigT *ctlConfig) {
errcount += ctlConfig->sections[idx].loadCB(&ctlConfig->sections[idx], NULL);
}
return errcount;
+
+ OnErrorExit:
+ return 1;
}
CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) {
@@ -163,7 +166,7 @@ CtlConfigT *CtlConfigLoad(const char* filepath, CtlSectionT *sections) {
int err;
#ifdef CONTROL_SUPPORT_LUA
- err= LuaConfigLoad();
+ err = LuaConfigLoad();
if (err) goto OnErrorExit;
#endif
diff --git a/controller/ctl-config.h b/controller/ctl-config.h
index 590a265..25a066c 100644
--- a/controller/ctl-config.h
+++ b/controller/ctl-config.h
@@ -21,14 +21,14 @@
#ifndef _CTL_CONFIG_INCLUDE_
#define _CTL_CONFIG_INCLUDE_
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
#ifdef __cplusplus
extern "C" {
#endif
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
#define AFB_BINDING_VERSION 2
#include <afb/afb-binding.h>
#include <json-c/json.h>
@@ -42,7 +42,7 @@ extern "C" {
#endif
#ifndef CONTROL_CONFIG_PRE
- #define CONTROL_CONFIG_PRE "onload"
+ #define CONTROL_CONFIG_PRE "init"
#endif
#ifndef CTL_PLUGIN_EXT
@@ -90,7 +90,11 @@ typedef struct {
} CtlConfigT;
#ifdef CONTROL_SUPPORT_LUA
- #include "ctl-lua.h"
+ #include "ctl-lua.h"
+#else
+ typedef struct lua_State lua_State;
+ typedef void *Lua2cFunctionT;
+ typedef void* Lua2cWrapperT;
#endif
// ctl-action.c
diff --git a/controller/ctl-lua.c b/controller/ctl-lua.c
index 5d959f0..7e51d2a 100644
--- a/controller/ctl-lua.c
+++ b/controller/ctl-lua.c
@@ -736,12 +736,13 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) {
goto OnErrorExit;
}
- // search for filename=script in CONTROL_LUA_PATH
- if (!luaScriptPathJ) {
+ // search for filename=script in binding data dir
+ if (!luaScriptPathJ) {
+ const char* dirList = strncat(GetBindingDirPath(), "/data", sizeof(GetBindingDirPath()) - strlen(GetBindingDirPath()) - 1);
strncpy(luaScriptPath,CONTROL_DOSCRIPT_PRE, sizeof(luaScriptPath));
strncat(luaScriptPath,"-", sizeof(luaScriptPath));
strncat(luaScriptPath,target, sizeof(luaScriptPath));
- luaScriptPathJ= ScanForConfig(CONTROL_LUA_PATH , CTL_SCAN_RECURSIVE,luaScriptPath,".lua");
+ luaScriptPathJ= ScanForConfig(dirList, CTL_SCAN_RECURSIVE,luaScriptPath,".lua");
}
for (index=0; index < json_object_array_length(luaScriptPathJ); index++) {
json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, index);
@@ -988,8 +989,6 @@ static const luaL_Reg afbFunction[] = {
// Load Lua Interpreter
int LuaConfigLoad () {
-
-
// open a new LUA interpretor
luaState = luaL_newstate();
if (!luaState) {
@@ -1029,8 +1028,8 @@ int LuaConfigExec () {
strncat (fullprefix, GetBinderName(), sizeof(fullprefix));
strncat (fullprefix, "-", sizeof(fullprefix));
- const char *dirList= getenv("CONTROL_LUA_PATH");
- if (!dirList) dirList=CONTROL_LUA_PATH;
+ const char *dirList = getenv("CONTROL_LUA_PATH");
+ if (!dirList) dirList = strncat(GetBindingDirPath(), "/data", sizeof(GetBindingDirPath()) - strlen(GetBindingDirPath()) - 1);
// special case for no lua even when avaliable
if (!strcasecmp ("/dev/null", dirList)) {
@@ -1073,7 +1072,7 @@ int LuaConfigExec () {
AFB_WARNING ("POLICY-INIT:WARNING (setenv CONTROL_LUA_PATH) No LUA '%s*.lua' in '%s'", fullprefix, dirList);
}
- AFB_DEBUG ("Audio control-LUA Init Done");
+ AFB_DEBUG("Control-LUA Init Done");
return 0;
OnErrorExit:
diff --git a/controller/ctl-lua.h b/controller/ctl-lua.h
index 2a85543..2fbebf5 100644
--- a/controller/ctl-lua.h
+++ b/controller/ctl-lua.h
@@ -25,6 +25,10 @@
#define _GNU_SOURCE
#endif
+#ifdef __cplusplus
+extern "C" {
+#endif
+
// prefix start debug script
#ifndef CONTROL_DOSCRIPT_PRE
#define CONTROL_DOSCRIPT_PRE "debug"
@@ -35,11 +39,6 @@
#define CONTROL_LUA_EVENT "luaevt"
#endif
-// default use same search path for config.json and script.lua
-#ifndef CONTROL_LUA_PATH
-#define CONTROL_LUA_PATH CONTROL_CONFIG_PATH
-#endif
-
#define AFB_BINDING_VERSION 2
#include <afb/afb-binding.h>
@@ -65,7 +64,7 @@ typedef int (*Lua2cWrapperT) (lua_State* luaState, char *funcname, Lua2cFunction
#define CTLP_LUALOAD Lua2cWrapperT Lua2cWrap;
#define CTLP_LUA2C(FuncName, label,argsJ, context) static int FuncName(char*label,json_object*argsJ);\
- int lua2c_ ## FuncName(lua_State* luaState){return((*Lua2cWrap)(luaState, MACRO_STR_VALUE(FuncName), FuncName, PLUGIN_NAME));};\
+ int lua2c_ ## FuncName(lua_State* luaState){return((*Lua2cWrap)(luaState, MACRO_STR_VALUE(FuncName), FuncName));};\
static int FuncName(char* label, json_object* argsJ, void* context)
typedef enum {
@@ -74,7 +73,6 @@ typedef enum {
LUA_DOSCRIPT,
} LuaDoActionT;
-
int LuaConfigLoad();
int LuaConfigExec();
void LuaL2cNewLib(const char *label, luaL_Reg *l2cFunc, int count);
@@ -85,4 +83,8 @@ void ctlapi_lua_dostring (afb_req request);
void ctlapi_lua_doscript (afb_req request);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/controller/ctl-plugin.c b/controller/ctl-plugin.c
index d982b79..550924f 100644
--- a/controller/ctl-plugin.c
+++ b/controller/ctl-plugin.c
@@ -25,14 +25,6 @@
#include <stdio.h>
#include <stdlib.h>
-#ifdef CONTROL_SUPPORT_LUA
-#include "ctl-lua.h"
-#else
-typedef struct lua_State lua_State;
-typedef void *Lua2cFunctionT;
-typedef void *Lua2cWrapperT;
-#endif
-
#include "ctl-config.h"
static CtlPluginT *ctlPlugins=NULL;
diff --git a/controller/ctl-plugin.h b/controller/ctl-plugin.h
index 1c772c7..7934d8d 100644
--- a/controller/ctl-plugin.h
+++ b/controller/ctl-plugin.h
@@ -20,6 +20,10 @@
#ifndef _CTL_PLUGIN_INCLUDE_
#define _CTL_PLUGIN_INCLUDE_
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
@@ -70,4 +74,8 @@ typedef void*(*DispatchPluginInstallCbT)(CtlPluginT *plugin, void* handle);
#define CTLP_ONLOAD(plugin, handle) void* CtlPluginOnload(CtlPluginT *plugin, void* handle)
#define CTLP_CAPI(funcname, source, argsJ, queryJ, context) int funcname(CtlSourceT *source, json_object* argsJ, json_object* queryJ, void* context)
+#ifdef __cplusplus
+}
+#endif
+
#endif /* _CTL_PLUGIN_INCLUDE_ */