aboutsummaryrefslogtreecommitdiffstats
path: root/Controler-afb
diff options
context:
space:
mode:
authorfulup <fulup.arfoll@iot.bzh>2017-08-15 12:48:59 +0200
committerfulup <fulup.arfoll@iot.bzh>2017-08-15 12:48:59 +0200
commite8b7ffa70194d1cc3ef4cc52bb0ace9f133514cf (patch)
tree63670aff03ca38b0a1da102f23e7223997f5b1f3 /Controler-afb
parent96f0057d65fbb90d9faa02a3ae4ae37b357094db (diff)
docall working with simple table
Diffstat (limited to 'Controler-afb')
-rw-r--r--Controler-afb/ctl-binding.c3
-rw-r--r--Controler-afb/ctl-dispatch.c20
-rw-r--r--Controler-afb/ctl-lua.c67
3 files changed, 47 insertions, 43 deletions
diff --git a/Controler-afb/ctl-binding.c b/Controler-afb/ctl-binding.c
index 19f97eb..554199b 100644
--- a/Controler-afb/ctl-binding.c
+++ b/Controler-afb/ctl-binding.c
@@ -69,7 +69,8 @@ PUBLIC int CtlBindingInit () {
errcount += LuaLibInit();
// now that everything is initialised execute the onload action
- errcount += DispatchOneOnLoad(CONTROL_ONLOAD_DEFAULT);
+ if (!errcount)
+ errcount += DispatchOneOnLoad(CONTROL_ONLOAD_DEFAULT);
AFB_DEBUG ("Audio Policy Control Binding Done errcount=%d", errcount);
return errcount;
diff --git a/Controler-afb/ctl-dispatch.c b/Controler-afb/ctl-dispatch.c
index 7d6a31d..3318c8d 100644
--- a/Controler-afb/ctl-dispatch.c
+++ b/Controler-afb/ctl-dispatch.c
@@ -90,7 +90,7 @@ STATIC int DispatchOneControl (DispatchHandleT **controls, const char* controlLa
json_object_get( actions[idx].argsJ); // make sure afb_service_call does not free the argsJ
int err = afb_service_call_sync(actions[idx].api, actions[idx].call, actions[idx].argsJ, &returnJ);
if (err) {
- static const char*format="DispatchOneControl Api api=%s verb=%s args=%s";
+ static const char*format="DispatchOneControl(Api) api=%s verb=%s args=%s";
if (afb_req_is_valid(request))afb_req_fail_f(request, "DISPATCH-CTL-MODE:API", format, actions[idx].label, actions[idx].api, actions[idx].call);
else AFB_ERROR (format, actions[idx].api, actions[idx].call, actions[idx].label);
goto OnErrorExit;
@@ -101,9 +101,9 @@ STATIC int DispatchOneControl (DispatchHandleT **controls, const char* controlLa
case CTL_MODE_LUA:
err= LuaCallFunc (&actions[idx], queryJ);
if (err) {
- static const char*format= "DispatchOneControl func=%s args=%s";
- if (afb_req_is_valid(request)) afb_req_fail_f(request, "DISPATCH-CTL-MODE:Lua", format, actions[idx].call, json_object_get_string(actions[idx].argsJ));
- else AFB_ERROR (format, actions[idx].call, json_object_get_string(actions[idx].argsJ));
+ static const char*format= "DispatchOneControl(Lua) label=%s func=%s args=%s";
+ if (afb_req_is_valid(request)) afb_req_fail_f(request, "DISPATCH-CTL-MODE:Lua", format, actions[idx].label, actions[idx].call, json_object_get_string(actions[idx].argsJ));
+ else AFB_ERROR (format, actions[idx].label, actions[idx].call, json_object_get_string(actions[idx].argsJ));
goto OnErrorExit;
}
break;
@@ -111,17 +111,17 @@ STATIC int DispatchOneControl (DispatchHandleT **controls, const char* controlLa
case CTL_MODE_CB:
err= (*actions[idx].actionCB) (&actions[idx], queryJ, configHandle->context);
if (err) {
- static const char*format="DispatchOneControl func=%s args=%s";
- if (afb_req_is_valid(request)) afb_req_fail_f(request, "DISPATCH-CTL-MODE:Cb", format, actions[idx].call, json_object_get_string(actions[idx].argsJ));
- else AFB_ERROR (format, actions[idx].call, json_object_get_string(actions[idx].argsJ));
+ static const char*format="DispatchOneControl(Callback) label%s func=%s args=%s";
+ if (afb_req_is_valid(request)) afb_req_fail_f(request, "DISPATCH-CTL-MODE:Cb", format, actions[idx].label, actions[idx].call, json_object_get_string(actions[idx].argsJ));
+ else AFB_ERROR (format, actions[idx].label, actions[idx].call, json_object_get_string(actions[idx].argsJ));
goto OnErrorExit;
}
break;
default:{
- static const char*format="DispatchOneControl unknown mode control=%s action=%s";
- AFB_ERROR (format, controls[index]->label, actions[idx].label);
- if (afb_req_is_valid(request))afb_req_fail_f(request, "DISPATCH-CTL-MODE:Unknown", format, controls[index]->label, actions[idx].label);
+ static const char*format="DispatchOneControl(unknown) mode control=%s action=%s";
+ AFB_ERROR (format, controls[index]->label);
+ if (afb_req_is_valid(request))afb_req_fail_f(request, "DISPATCH-CTL-MODE:Unknown", format, controls[index]->label);
}
}
}
diff --git a/Controler-afb/ctl-lua.c b/Controler-afb/ctl-lua.c
index 275b9d3..a7cd52f 100644
--- a/Controler-afb/ctl-lua.c
+++ b/Controler-afb/ctl-lua.c
@@ -238,6 +238,9 @@ STATIC void LuaFormatMessage(lua_State* luaState, LuaAfbMessageT action) {
message="-- Empty Message ???";
goto PrintMessage;
}
+
+ //AFB_NOTICE("**** responseJ=%s", json_object_get_string(responseJ));
+
// if we have only on argument just return the value.
if (json_object_get_type(responseJ)!=json_type_array || json_object_array_length(responseJ) <2) {
@@ -256,22 +259,31 @@ STATIC void LuaFormatMessage(lua_State* luaState, LuaAfbMessageT action) {
if (format[idx]=='%' && format[idx] !='\0') {
json_object *slotJ= json_object_array_get_idx(responseJ, arrayIdx);
+ //if (slotJ) AFB_NOTICE("**** idx=%d slotJ=%s", arrayIdx, json_object_get_string(slotJ));
+
switch (format[++idx]) {
case 'd':
if (slotJ) targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"%d", json_object_get_int(slotJ));
else targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"nil");
+ arrayIdx++;
break;
case 'f':
if (slotJ) targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"%f", json_object_get_double(slotJ));
else targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"nil");
+ arrayIdx++;
+ break;
+
+ case'%':
+ targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"%");
break;
case 's':
default:
if (slotJ) targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"%s", json_object_get_string(slotJ));
else targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"nil");
- }
+ arrayIdx++;
+ }
} else {
message[targetIdx++] = format[idx];
@@ -497,7 +509,7 @@ STATIC int LuaAfbPushEvent(lua_State* luaState) {
// Generated some fake event based on watchdog/counter
PUBLIC int LuaCallFunc (DispatchActionT *action, json_object *queryJ) {
- int err, count=0;
+ int err, count;
json_object* argsJ = action->argsJ;
const char* func = action->call;
@@ -506,29 +518,22 @@ PUBLIC int LuaCallFunc (DispatchActionT *action, json_object *queryJ) {
lua_getglobal(luaState, func);
// push argsJ on the stack
+ count=0;
if (!argsJ) {
lua_pushnil(luaState);
count++;
- } else if (json_object_get_type(argsJ) != json_type_array) {
+ } else {
count+= LuaPushArgument (argsJ);
- } else {
- for (int idx=0; idx<json_object_array_length(argsJ); idx++) {
- count += LuaPushArgument (json_object_array_get_idx(argsJ, idx));
- }
- }
+ }
// push queryJ on the stack
if (!queryJ) {
lua_pushnil(luaState);
count++;
- } else if (json_object_get_type(queryJ) != json_type_array) {
+ } else {
count+= LuaPushArgument (queryJ);
- } else {
- for (int idx=0; idx<json_object_array_length(queryJ); idx++) {
- count += LuaPushArgument (json_object_array_get_idx(queryJ, idx));
- }
- }
-
+ }
+
// effectively exec LUA script code
err=lua_pcall(luaState, count, 1, 0);
if (err) {
@@ -571,14 +576,14 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) {
case LUA_DOCALL: {
const char *func;
- json_object *args;
- err= wrap_json_unpack (queryJ, "{s:s, s?o !}", "func", &func,"args", &args);
+ json_object *argsJ=NULL;
+
+ err= wrap_json_unpack (queryJ, "{s:s, s?o !}", "func", &func, "args", &argsJ);
if (err) {
- AFB_ERROR ("LUA-DOCALL-SYNTAX missing func|args args=%s", json_object_get_string(queryJ));
+ AFB_ERROR ("LUA-DOCALL-SYNTAX missing func|args query=%s", json_object_get_string(queryJ));
goto OnErrorExit;
}
-
// load function (should exist in CONTROL_PATH_LUA
lua_getglobal(luaState, func);
@@ -587,14 +592,13 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) {
if (!afbContext) goto OnErrorExit;
// push query on the stack
- if (json_object_get_type(args) != json_type_array) {
- count= LuaPushArgument (args);
- } else {
- for (int idx=0; idx<json_object_array_length(args); idx++) {
- count += LuaPushArgument (json_object_array_get_idx(args, idx));
- if (err) break;
- }
- }
+ if (!argsJ) {
+ lua_pushnil(luaState);
+ count++;
+ } else {
+ AFB_NOTICE("***** args=%s", json_object_get_string(argsJ));
+ count+= LuaPushArgument (argsJ);
+ }
break;
}
@@ -611,7 +615,7 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) {
err= wrap_json_unpack (queryJ, "{s:s, s?o s?o !}", "script", &script,"args", &args, "arg", &args);
if (err) {
- AFB_ERROR ("LUA-DOSCRIPT-SYNTAX:missing script|(args,arg) args=%s", json_object_get_string(queryJ));
+ AFB_ERROR ("LUA-DOSCRIPT-SYNTAX:missing script|(args,arg) query=%s", json_object_get_string(queryJ));
goto OnErrorExit;
}
@@ -668,8 +672,7 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) {
AFB_ERROR ("LUA-DO-EXEC:FAIL query=%s err=%s", json_object_get_string(queryJ), lua_tostring(luaState,-1) );
goto OnErrorExit;
}
-
-
+ return;
OnErrorExit:
afb_req_fail(request,"LUA:ERROR", lua_tostring(luaState,-1));
@@ -681,7 +684,7 @@ PUBLIC void ctlapi_lua_dostring (afb_req request) {
}
PUBLIC void ctlapi_lua_docall (afb_req request) {
- LuaDoAction (LUA_DOSCRIPT, request);
+ LuaDoAction (LUA_DOCALL, request);
}
PUBLIC void ctlapi_lua_doscript (afb_req request) {
@@ -778,6 +781,6 @@ PUBLIC int LuaLibInit () {
return 0;
OnErrorExit:
- return -1;
+ return 1;
}
\ No newline at end of file