aboutsummaryrefslogtreecommitdiffstats
path: root/ctl-lib
diff options
context:
space:
mode:
Diffstat (limited to 'ctl-lib')
-rw-r--r--ctl-lib/ctl-action.c36
-rw-r--r--ctl-lib/ctl-config.c16
-rw-r--r--ctl-lib/ctl-config.h18
-rw-r--r--ctl-lib/ctl-control.c6
-rw-r--r--ctl-lib/ctl-event.c26
-rw-r--r--ctl-lib/ctl-lua.c116
-rw-r--r--ctl-lib/ctl-lua.h2
-rw-r--r--ctl-lib/ctl-onload.c10
-rw-r--r--ctl-lib/ctl-plugin.c44
-rw-r--r--ctl-lib/ctl-plugin.h44
10 files changed, 158 insertions, 160 deletions
diff --git a/ctl-lib/ctl-action.c b/ctl-lib/ctl-action.c
index bb4b874..80e4484 100644
--- a/ctl-lib/ctl-action.c
+++ b/ctl-lib/ctl-action.c
@@ -77,7 +77,7 @@ PUBLIC void ActionExecOne(CtlSourceT *source, CtlActionT* action, json_object *q
json_object_object_add(queryJ, "args", action->argsJ);
}
}
-
+
json_object_object_add(queryJ, "uid", json_object_new_string(source->uid));
int err = AFB_ServiceSync(action->api, action->exec.subcall.api, action->exec.subcall.verb, queryJ, &returnJ);
@@ -112,21 +112,21 @@ PUBLIC void ActionExecOne(CtlSourceT *source, CtlActionT* action, json_object *q
}
-// Direct Request Call in APIV3
+// Direct Request Call in APIV3
#ifdef AFB_BINDING_PREV3
STATIC void ActionDynRequest (AFB_ReqT request) {
-
+
// retrieve action handle from request and execute the request
json_object *queryJ = afb_request_json(request);
CtlActionT* action = (CtlActionT*)afb_request_get_vcbdata(request);
-
+
CtlSourceT source;
source.uid = action->uid;
source.request = request;
source.api = action->api;
-
+
// provide request and execute the action
- ActionExecOne(&source, action, queryJ);
+ ActionExecOne(&source, action, queryJ);
}
#endif
@@ -142,11 +142,11 @@ PUBLIC int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *ac
AFB_ApiError(apiHandle,"ACTION-LOAD-ONE Action missing uid|[info]|[callback]|[lua]|[subcall]|[args] in:\n-- %s", json_object_get_string(actionJ));
goto OnErrorExit;
}
-
+
// save per action api handle
action->api = apiHandle;
-
- // in API V3 each control is optionally map to a verb
+
+ // in API V3 each control is optionally map to a verb
#ifdef AFB_BINDING_PREV3
if (apiHandle && exportApi) {
err = afb_dynapi_add_verb(apiHandle, action->uid, action->info, ActionDynRequest, action, NULL, 0);
@@ -156,11 +156,11 @@ PUBLIC int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *ac
}
action->api = apiHandle;
}
-#endif
+#endif
- if (luaJ) {
+ if (luaJ) {
modeCount++;
-
+
action->type = CTL_TYPE_LUA;
switch (json_object_get_type(luaJ)) {
case json_type_object:
@@ -173,7 +173,7 @@ PUBLIC int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *ac
case json_type_string:
action->exec.lua.funcname = json_object_get_string(luaJ);
break;
- default:
+ default:
AFB_ApiError(apiHandle,"ACTION-LOAD-ONE Lua action invalid syntax in:\n-- %s", json_object_get_string(luaJ));
goto OnErrorExit;
}
@@ -182,20 +182,20 @@ PUBLIC int ActionLoadOne(AFB_ApiT apiHandle, CtlActionT *action, json_object *ac
if (subcallJ) {
modeCount++;
action->type = CTL_TYPE_API;
-
+
err = wrap_json_unpack(luaJ, "{s?s,s:s !}", "api", &action->exec.subcall.api, "verb", &action->exec.subcall.verb);
if (err) {
AFB_ApiError(apiHandle,"ACTION-LOAD-ONE Subcall missing [load]|func in:\n-- %s", json_object_get_string(luaJ));
goto OnErrorExit;
- }
+ }
}
if (callbackJ) {
modeCount++;
action->type = CTL_TYPE_CB;
- modeCount++;
+ modeCount++;
err = PluginGetCB (apiHandle, action, callbackJ);
- if (err) goto OnErrorExit;
+ if (err) goto OnErrorExit;
}
// make sure at least one mode is selected
@@ -225,7 +225,7 @@ PUBLIC CtlActionT *ActionConfig(AFB_ApiT apiHandle, json_object *actionsJ, int e
for (int idx = 0; idx < count; idx++) {
json_object *actionJ = json_object_array_get_idx(actionsJ, idx);
-
+
err = ActionLoadOne(apiHandle, &actions[idx], actionJ, exportApi);
if (err) goto OnErrorExit;
}
diff --git a/ctl-lib/ctl-config.c b/ctl-lib/ctl-config.c
index 9b04df6..42d2897 100644
--- a/ctl-lib/ctl-config.c
+++ b/ctl-lib/ctl-config.c
@@ -39,7 +39,7 @@ PUBLIC int CtlConfigMagicNew() {
gettimeofday(&tv,NULL);
srand ((int)tv.tv_usec);
}
-
+
return ((long)rand());
}
@@ -56,7 +56,7 @@ PUBLIC json_object* CtlConfigScan(const char *dirList, const char *prefix) {
PUBLIC char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char *prefix) {
int index, err;
-
+
// search for default dispatch config file
json_object* responseJ = CtlConfigScan (dirList, prefix);
@@ -86,7 +86,7 @@ PUBLIC char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char
}
PUBLIC int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) {
-
+
// best effort to initialise everything before starting
if (ctlConfig->requireJ) {
@@ -115,12 +115,12 @@ PUBLIC int CtlConfigExec(AFB_ApiT apiHandle, CtlConfigT *ctlConfig) {
// Loop on every section and process config
int errcount=0;
for (int idx = 0; ctlConfig->sections[idx].key != NULL; idx++) {
-
+
if (!ctlConfig->sections[idx].actions) {
AFB_ApiNotice(apiHandle, "CtlConfigLoad: notice empty section '%s'", ctlConfig->sections[idx].key);
continue;
}
-
+
errcount += ctlConfig->sections[idx].loadCB(apiHandle, &ctlConfig->sections[idx], NULL);
}
return errcount;
@@ -154,10 +154,10 @@ PUBLIC CtlConfigT *CtlLoadMetaData(AFB_ApiT apiHandle, const char* filepath) {
goto OnErrorExit;
}
}
-
- ctlHandle->configJ = ctlConfigJ;
+
+ ctlHandle->configJ = ctlConfigJ;
return ctlHandle;
-
+
OnErrorExit:
if (ctlHandle) free(ctlHandle);
return NULL;
diff --git a/ctl-lib/ctl-config.h b/ctl-lib/ctl-config.h
index c3f5d4b..f7eb44c 100644
--- a/ctl-lib/ctl-config.h
+++ b/ctl-lib/ctl-config.h
@@ -45,7 +45,7 @@ typedef struct ConfigSectionS {
const char *info;
int (*loadCB)(AFB_ApiT apihandle, struct ConfigSectionS *section, json_object *sectionJ);
void *handle;
- CtlActionT *actions;
+ CtlActionT *actions;
} CtlSectionT;
typedef struct {
@@ -62,16 +62,16 @@ typedef struct {
#ifdef CONTROL_SUPPORT_LUA
#include "ctl-lua.h"
#endif
-
-// This should not be global as application may want to define their own sections
+
+// This should not be global as application may want to define their own sections
typedef enum {
- CTL_SECTION_PLUGIN,
- CTL_SECTION_ONLOAD,
- CTL_SECTION_CONTROL,
+ CTL_SECTION_PLUGIN,
+ CTL_SECTION_ONLOAD,
+ CTL_SECTION_CONTROL,
CTL_SECTION_EVENT,
CTL_SECTION_HAL,
-
- CTL_SECTION_ENDTAG,
+
+ CTL_SECTION_ENDTAG,
} SectionEnumT;
@@ -112,4 +112,4 @@ PUBLIC int PluginConfig(AFB_ApiT UNUSED_ARG(apiHandle), CtlSectionT *section, js
PUBLIC int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *callbackJ);
-#endif /* _CTL_CONFIG_INCLUDE_ */ \ No newline at end of file
+#endif /* _CTL_CONFIG_INCLUDE_ */
diff --git a/ctl-lib/ctl-control.c b/ctl-lib/ctl-control.c
index 2248756..914b51f 100644
--- a/ctl-lib/ctl-control.c
+++ b/ctl-lib/ctl-control.c
@@ -24,15 +24,15 @@
// onload section receive one action or an array of actions
PUBLIC int ControlConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ) {
-
+
// Load time parse actions in config file
if (actionsJ != NULL) {
section->actions= ActionConfig(apiHandle, actionsJ, 1);
-
+
if (!section->actions) {
AFB_ApiError (apiHandle, "ControlLoad config fail processing onload actions");
goto OnErrorExit;
- }
+ }
}
return 0;
diff --git a/ctl-lib/ctl-event.c b/ctl-lib/ctl-event.c
index 62632e6..5be8b24 100644
--- a/ctl-lib/ctl-event.c
+++ b/ctl-lib/ctl-event.c
@@ -27,27 +27,27 @@
#ifdef AFB_BINDING_PREV3
PUBLIC void CtrlDispatchApiEvent (AFB_ApiT apiHandle, const char *evtLabel, struct json_object *eventJ) {
AFB_ApiNotice (apiHandle, "Received event=%s, query=%s", evtLabel, json_object_get_string(eventJ));
-
+
// retrieve section config from api handle
CtlConfigT *ctrlConfig = (CtlConfigT*) afb_dynapi_get_userdata(apiHandle);
-
+
CtlActionT* actions = ctrlConfig->sections[CTL_SECTION_EVENT].actions;
-
+
int index= ActionLabelToIndex(actions, evtLabel);
if (index < 0) {
AFB_ApiWarning(apiHandle, "CtlDispatchEvent: fail to find uid=%s in action event section", evtLabel);
return;
}
- // create a dummy source for action
+ // create a dummy source for action
CtlSourceT source;
source.uid = actions[index].uid;
source.api = actions[index].api;
source.request = NULL;
-
- // Best effort ignoring error to exec corresponding action
+
+ // Best effort ignoring error to exec corresponding action
(void) ActionExecOne (&source, &actions[index], eventJ);
-
+
}
#else
// In API-V2 controller config is unique and static
@@ -56,34 +56,34 @@ extern CtlConfigT *ctrlConfig;
// call action attached to even name if any
PUBLIC void CtrlDispatchV2Event(const char *evtLabel, json_object *eventJ) {
CtlActionT* actions = ctrlConfig->sections[CTL_SECTION_EVENT].actions;
-
+
int index= ActionLabelToIndex(actions, evtLabel);
if (index < 0) {
AFB_WARNING ("CtlDispatchEvent: fail to find uid=%s in action event section", evtLabel);
return;
}
-
+
CtlSourceT source;
source.uid = actions[index].uid;
source.api = actions[index].api;
source.request = AFB_ReqNone;
- // Best effort ignoring error to exec corresponding action
+ // Best effort ignoring error to exec corresponding action
(void) ActionExecOne (&source, &actions[index], eventJ);
}
#endif
// onload section receive one action or an array of actions
PUBLIC int EventConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ) {
-
+
// Load time parse actions in config file
if (actionsJ != NULL) {
section->actions= ActionConfig(apiHandle, actionsJ, 0);
-
+
if (!section->actions) {
AFB_ApiError (apiHandle, "EventLoad config fail processing onload actions");
goto OnErrorExit;
- }
+ }
}
return 0;
diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c
index 1db0ed3..4d32794 100644
--- a/ctl-lib/ctl-lua.c
+++ b/ctl-lib/ctl-lua.c
@@ -86,7 +86,7 @@ STATIC LuaAfbSourceT *LuaSourcePush (lua_State *luaState, CtlSourceT *source) {
AFB_ApiError(source->api, "LuaSourcePush fail to allocate user data context");
return NULL;
}
-
+
lua_pushlightuserdata(luaState, afbSource);
afbSource->ctxMagic=CTX_MAGIC;
afbSource->source= source;
@@ -269,18 +269,18 @@ static json_object *LuaPopArgs (CtlSourceT *source, lua_State* luaState, int sta
STATIC int LuaFormatMessage(lua_State* luaState, int verbosity, int level) {
char *message;
-
+
CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG);
if (!source) goto OnErrorExit;
\
-
+
// if log level low then silently ignore message
#ifdef AFB_BINDING_PREV3
- if(source->api->verbosity < verbosity) return 0;
+ if(source->api->verbosity < verbosity) return 0;
#else
- if(afb_get_verbosity() < verbosity) return 0;
+ if(afb_get_verbosity() < verbosity) return 0;
#endif
-
+
json_object *responseJ= LuaPopArgs(source, luaState, LUA_FIST_ARG+1);
if (!responseJ) {
@@ -324,7 +324,7 @@ STATIC int LuaFormatMessage(lua_State* luaState, int verbosity, int level) {
message[targetIdx]='%';
targetIdx++;
break;
-
+
case 'A':
targetIdx += snprintf (&message[targetIdx], LUA_MSG_MAX_LENGTH-targetIdx,"level: %s", source->uid);
break;
@@ -349,7 +349,7 @@ STATIC int LuaFormatMessage(lua_State* luaState, int verbosity, int level) {
message[targetIdx]='\0';
PrintMessage:
- // TBD: __file__ and __line__ should match LUA source code
+ // TBD: __file__ and __line__ should match LUA source code
AFB_ApiVerbose(source->api, level,__FILE__,__LINE__,source->uid, message);
return 0; // nothing return to lua
@@ -426,7 +426,7 @@ STATIC void LuaAfbServiceCB(void *handle, int iserror, struct json_object *respo
if (iserror) handleCb->source->status = CTL_STATUS_ERR;
else handleCb->source->status = CTL_STATUS_DONE;
LuaSourcePush(luaState, handleCb->source);
-
+
// push response
count+= LuaPushArgument(handleCb->source, responseJ);
if (handleCb->context) count+= LuaPushArgument(handleCb->source, handleCb->context);
@@ -449,7 +449,7 @@ STATIC int LuaAfbService(lua_State* luaState) {
lua_pushliteral (luaState, "LuaAfbService-Fail Invalid request handle");
goto OnErrorExit;
}
-
+
// note: argument start at 2 because of AFB: table
if (count <6 || !lua_isstring(luaState, 3) || !lua_isstring(luaState, 4) || !lua_isstring(luaState, 6)) {
lua_pushliteral (luaState, "ERROR: syntax AFB:service(source, api, verb, {[Lua Table]})");
@@ -465,7 +465,7 @@ STATIC int LuaAfbService(lua_State* luaState) {
LuaCbHandleT *handleCb = calloc (1, sizeof(LuaCbHandleT));
handleCb->callback= lua_tostring(luaState, 6);
handleCb->context = LuaPopArgs(source, luaState, 7);
-
+
// source need to be duplicate because request return free it
handleCb->source = malloc(sizeof(CtlSourceT));
handleCb->source = memcpy (handleCb->source, source, sizeof(CtlSourceT));
@@ -488,14 +488,14 @@ STATIC int LuaAfbServiceSync(lua_State* luaState) {
lua_pushliteral (luaState, "LuaAfbServiceSync-Fail Invalid request handle");
goto OnErrorExit;
}
-
+
// note: argument start at 2 because of AFB: table
if (count != 5 || !lua_isstring(luaState, LUA_FIST_ARG+1) || !lua_isstring(luaState, LUA_FIST_ARG+2) || !lua_istable(luaState, LUA_FIST_ARG+3)) {
lua_pushliteral (luaState, "ERROR: syntax AFB:servsync(api, verb, {[Lua Table]})");
goto OnErrorExit;
}
-
+
// get source/api/verb+query
const char *api = lua_tostring(luaState,LUA_FIST_ARG+1);
const char *verb= lua_tostring(luaState,LUA_FIST_ARG+2);
@@ -522,15 +522,15 @@ STATIC int LuaAfbEventPush(lua_State* luaState) {
lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail Invalid request handle");
goto OnErrorExit;
}
-
+
// if no private event handle then use default binding event
if (!lua_islightuserdata(luaState, LUA_FIST_ARG+1)) {
lua_pushliteral (luaState, "LuaAfbMakePush-Fail missing event handle");
goto OnErrorExit;
}
-
+
afbevt = (LuaAfbEvent*) lua_touserdata(luaState, LUA_FIST_ARG+1);
-
+
if (!AFB_EventIsValid(afbevt->event)) {
lua_pushliteral (luaState, "LuaAfbMakePush-Fail invalid event");
goto OnErrorExit;
@@ -570,7 +570,7 @@ STATIC int LuaAfbEventSubscribe(lua_State* luaState) {
lua_pushliteral (luaState, "LuaAfbMakePush-Fail missing event handle");
goto OnErrorExit;
}
-
+
afbevt = (LuaAfbEvent*) lua_touserdata(luaState, LUA_FIST_ARG+1);
if (!AFB_EventIsValid(afbevt->event)) {
@@ -634,7 +634,7 @@ STATIC int LuaAfbGetUid (lua_State* luaState) {
lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail Invalid request handle");
goto OnErrorExit;
}
-
+
// extract and return afbSource from timer handle
lua_pushstring(luaState, source->uid);
@@ -651,7 +651,7 @@ STATIC int LuaAfbGetStatus (lua_State* luaState) {
lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail Invalid request handle");
goto OnErrorExit;
}
-
+
// extract and return afbSource from timer handle
lua_pushinteger(luaState, source->status);
@@ -665,7 +665,7 @@ OnErrorExit:
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);
json_object *argsJ= LuaPopArgs(source, luaState, LUA_FIST_ARG+1);
@@ -675,7 +675,7 @@ PUBLIC int Lua2cWrapper(void* luaHandle, char *funcname, Lua2cFunctionT callback
int count=1;
lua_pushinteger (luaState, err);
if (!responseJ) count += LuaPushArgument (source, responseJ);
-
+
return count;
}
@@ -735,7 +735,7 @@ STATIC void LuaDoAction (LuaDoActionT action, AFB_ReqT request) {
source->request = request;
json_object* queryJ = AFB_ReqJson(request);
-
+
switch (action) {
@@ -897,10 +897,10 @@ PUBLIC void ctlapi_debuglua (AFB_ReqT request) {
STATIC TimerHandleT *LuaTimerPop (lua_State *luaState, int index) {
TimerHandleT *timerHandle;
-
+
luaL_checktype(luaState, index, LUA_TLIGHTUSERDATA);
timerHandle = (TimerHandleT *) lua_touserdata(luaState, index);
-
+
if (timerHandle == NULL && timerHandle->magic != TIMER_MAGIC) {
luaL_error(luaState, "Invalid source handle");
fprintf(stderr, "LuaSourcePop error retrieving afbSource");
@@ -930,7 +930,7 @@ STATIC int LuaTimerGet (lua_State* luaState) {
TimerHandleT *timerHandle = LuaTimerPop(luaState, LUA_FIST_ARG);
if (!timerHandle) goto OnErrorExit;
LuaCbHandleT *luaCbHandle = (LuaCbHandleT*) timerHandle->context;
-
+
// create response as a JSON object
json_object *responseJ= json_object_new_object();
json_object_object_add(responseJ,"uid", json_object_new_string(timerHandle->uid));
@@ -987,17 +987,17 @@ STATIC int LuaTimerSetCB (void *handle) {
// Free Timer context handle
STATIC int LuaTimerCtxFree(void *handle) {
LuaCbHandleT *LuaCbHandle = (LuaCbHandleT*) handle;
-
+
free (LuaCbHandle->source);
free (LuaCbHandle);
-
+
return 0;
}
STATIC int LuaTimerSet(lua_State* luaState) {
const char *uid=NULL, *info=NULL;
int delay=0, count=0;
-
+
// Get source handle
CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG);
if (!source) goto OnErrorExit;
@@ -1023,7 +1023,7 @@ STATIC int LuaTimerSet(lua_State* luaState) {
handleCb->context = contextJ;
handleCb->source = malloc(sizeof(CtlSourceT));
memcpy (handleCb->source, source, sizeof(CtlSourceT)); // Fulup need to be free when timer is done
-
+
// everything look fine create timer structure
TimerHandleT *timerHandle = malloc (sizeof (TimerHandleT));
timerHandle->magic= TIMER_MAGIC;
@@ -1034,13 +1034,13 @@ STATIC int LuaTimerSet(lua_State* luaState) {
// fire timer
TimerEvtStart (source->api, timerHandle, LuaTimerSetCB, handleCb);
-
+
// Fulup finir les timers avec handle
// return empty error code plus timer handle
lua_pushnil(luaState);
lua_pushlightuserdata(luaState, timerHandle);
- return 2;
+ return 2;
OnErrorExit:
lua_error(luaState);
@@ -1055,10 +1055,10 @@ typedef struct {
STATIC void *LuaClientCtxNew (void * handle) {
-
+
LuaClientCtxT *clientCtx = (LuaClientCtxT*) handle;
int count=1;
-
+
// push callback and client context on Lua stack
lua_getglobal(luaState, clientCtx->callback);
@@ -1080,7 +1080,7 @@ STATIC void *LuaClientCtxNew (void * handle) {
// If function return an error status then free client context
if (lua_tointeger(luaState, -1)) {
- free (clientCtx);
+ free (clientCtx);
goto OnErrorExit;
}
@@ -1088,19 +1088,19 @@ STATIC void *LuaClientCtxNew (void * handle) {
OnErrorExit:
return NULL;
-
+
}
STATIC void LuaClientCtxFree (void * handle) {
-
+
LuaClientCtxT *clientCtx = (LuaClientCtxT*) handle;
int count=1;
-
+
if (!handle) return;
-
+
// let's Lua script know about new/free
lua_getglobal(luaState, clientCtx->callback);
-
+
// set source status to notify lua script about free
clientCtx->source->status = CTL_STATUS_FREE;
@@ -1119,7 +1119,7 @@ STATIC void LuaClientCtxFree (void * handle) {
// If function return an error status then free client context
if (lua_toboolean(luaState, -1)) {
- free (clientCtx);
+ free (clientCtx);
goto OnErrorExit;
}
@@ -1127,27 +1127,27 @@ STATIC void LuaClientCtxFree (void * handle) {
OnErrorExit:
return;
-
+
}
// set a context that will be free when WS is closed
STATIC int LuaClientCtx(lua_State* luaState) {
-
+
// Get source handle
CtlSourceT *source= LuaSourcePop(luaState, LUA_FIST_ARG);
if (!source) goto OnErrorExit;
-
+
if (!AFB_ReqIsValid (source->request)) {
lua_pushliteral(luaState, "LuaSessionSet-Syntax should be called within client request context");
- goto OnErrorExit;
+ goto OnErrorExit;
}
-
+
// in only one arg then we should free the clientCtx
if (lua_gettop(luaState) == LUA_FIST_ARG) {
AFB_ClientCtxClear(source->request);
goto OnSuccessExit;
- }
-
+ }
+
const char *callback = lua_tostring(luaState, LUA_FIST_ARG + 1);
json_object *clientCtxJ = LuaPopOneArg(source, luaState, LUA_FIST_ARG + 2);
@@ -1167,13 +1167,13 @@ STATIC int LuaClientCtx(lua_State* luaState) {
void *done = AFB_ClientCtxSet (source->request, 1, LuaClientCtxNew, LuaClientCtxFree, clientCtx);
if (!done) {
lua_pushliteral(luaState, "LuaClientCtx-Fail to allocate client context)");
- goto OnErrorExit;
+ goto OnErrorExit;
}
OnSuccessExit:
lua_pushnil(luaState);
- return 1;
-
+ return 1;
+
OnErrorExit:
lua_error(luaState);
return 1; // return error code
@@ -1219,7 +1219,7 @@ PUBLIC int LuaConfigLoad (AFB_ApiT apiHandle) {
// Lua loads only once
if (luaLoaded) return 0;
luaLoaded=1;
-
+
// open a new LUA interpretor
luaState = luaL_newstate();
if (!luaState) {
@@ -1233,7 +1233,7 @@ PUBLIC int LuaConfigLoad (AFB_ApiT apiHandle) {
// redirect print to AFB_NOTICE
luaL_newlib(luaState, afbFunction);
lua_setglobal(luaState, "AFB");
-
+
// initialise static magic for context
#ifndef CTX_MAGIC
CTX_MAGIC=CtlConfigMagicNew();
@@ -1242,16 +1242,16 @@ PUBLIC int LuaConfigLoad (AFB_ApiT apiHandle) {
#ifndef TIMER_MAGIC
TIMER_MAGIC=CtlConfigMagicNew();
#endif
-
+
return 0;
-
+
OnErrorExit:
return 1;
}
// Create Binding Event at Init Exec Time
PUBLIC int LuaConfigExec (AFB_ApiT apiHandle, const char* prefix) {
-
+
int err, index;
// search for default policy config files
@@ -1264,11 +1264,11 @@ PUBLIC int LuaConfigExec (AFB_ApiT apiHandle, const char* prefix) {
const char *dirList= getenv("CONTROL_LUA_PATH");
if (!dirList) dirList=CONTROL_LUA_PATH;
- // special case for no lua even when avaliable
+ // special case for no lua even when avaliable
if (!strcasecmp ("/dev/null", dirList)) {
return 0;
}
-
+
json_object *luaScriptPathJ = ScanForConfig(dirList , CTL_SCAN_RECURSIVE, fullprefix, "lua");
// load+exec any file found in LUA search path
@@ -1298,7 +1298,7 @@ PUBLIC int LuaConfigExec (AFB_ApiT apiHandle, const char* prefix) {
AFB_ApiError(apiHandle, "LUA-LOAD HOOPs Error in LUA exec scripts=%s err=%s", filepath, lua_tostring(luaState,-1));
goto OnErrorExit;
} else {
- AFB_ApiNotice(apiHandle, "LUA-LOAD '%s'", filepath);
+ AFB_ApiNotice(apiHandle, "LUA-LOAD '%s'", filepath);
}
}
diff --git a/ctl-lib/ctl-lua.h b/ctl-lib/ctl-lua.h
index c9ab5fd..8a1a3f1 100644
--- a/ctl-lib/ctl-lua.h
+++ b/ctl-lib/ctl-lua.h
@@ -67,4 +67,4 @@ PUBLIC void ctlapi_lua_dostring (afb_req request);
PUBLIC void ctlapi_lua_doscript (afb_req request);
-#endif \ No newline at end of file
+#endif
diff --git a/ctl-lib/ctl-onload.c b/ctl-lib/ctl-onload.c
index 7658622..5455c8a 100644
--- a/ctl-lib/ctl-onload.c
+++ b/ctl-lib/ctl-onload.c
@@ -24,16 +24,16 @@
// onload section receive one action or an array of actions
PUBLIC int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *actionsJ) {
-
+
// Load time parse actions in control file
if (actionsJ != NULL) {
section->actions= ActionConfig(apiHandle, actionsJ, 0);
-
+
if (!section->actions) {
AFB_ApiError (apiHandle, "OnloadConfig control fail processing onload actions");
goto OnErrorExit;
}
-
+
} else {
// Exec time process onload action now
if (!section->actions) {
@@ -46,9 +46,9 @@ PUBLIC int OnloadConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *a
source.uid = section->actions[idx].uid;
source.api = section->actions[idx].api;
source.request = AFB_ReqNone;
-
+
ActionExecOne(&source, &section->actions[idx], NULL);
- }
+ }
}
return 0;
diff --git a/ctl-lib/ctl-plugin.c b/ctl-lib/ctl-plugin.c
index 9ecb7d0..4d9414f 100644
--- a/ctl-lib/ctl-plugin.c
+++ b/ctl-lib/ctl-plugin.c
@@ -33,37 +33,37 @@ PUBLIC int PluginGetCB (AFB_ApiT apiHandle, CtlActionT *action , json_object *ca
if (!ctlPlugins) {
AFB_ApiError(apiHandle, "PluginGetCB plugin section missing cannot call '%s'", json_object_get_string(callbackJ));
- goto OnErrorExit;
+ goto OnErrorExit;
}
-
- int err = wrap_json_unpack(callbackJ, "{ss,ss,s?s,s?o!}", "plugin", &plugin, "function", &function, "args", &argsJ);
+
+ int err = wrap_json_unpack(callbackJ, "{ss,ss,s?s,s?o!}", "plugin", &plugin, "function", &function, "args", &argsJ);
if (err) {
AFB_ApiError(apiHandle, "PluginGet missing plugin|function|[args] in %s", json_object_get_string(callbackJ));
goto OnErrorExit;
}
-
+
for (idx=0; ctlPlugins[idx].uid != NULL; idx++) {
if (!strcasecmp (ctlPlugins[idx].uid, plugin)) break;
}
-
+
if (!ctlPlugins[idx].uid) {
AFB_ApiError(apiHandle, "PluginGetCB no plugin with uid=%s", plugin);
goto OnErrorExit;
}
-
- action->exec.cb.funcname = function;
- action->exec.cb.callback = dlsym(ctlPlugins[idx].dlHandle, function);
+
+ action->exec.cb.funcname = function;
+ action->exec.cb.callback = dlsym(ctlPlugins[idx].dlHandle, function);
action->exec.cb.plugin= &ctlPlugins[idx];
-
+
if (!action->exec.cb.callback) {
- AFB_ApiError(apiHandle, "PluginGetCB no plugin=%s no function=%s", plugin, function);
+ AFB_ApiError(apiHandle, "PluginGetCB no plugin=%s no function=%s", plugin, function);
goto OnErrorExit;
}
- return 0;
+ return 0;
OnErrorExit:
return 1;
-
+
}
// Wrapper to Lua2c plugin command add context and delegate to LuaWrapper
@@ -82,17 +82,17 @@ STATIC int PluginLoadOne (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object
const char*ldSearchPath = NULL, *basename = NULL;
void *dlHandle;
-
+
// plugin initialises at 1st load further init actions should be place into onload section
if (!pluginJ) return 0;
-
+
int err = wrap_json_unpack(pluginJ, "{ss,s?s,s?s,s?s !}",
"uid", &ctlPlugin->uid, "info", &ctlPlugin->info, "ldpath", &ldSearchPath, "basename", &basename);
if (err) {
AFB_ApiError(apiHandle, "CTL-PLUGIN-LOADONE Plugin missing uid|[info]|[basename]|[ldpath] in:\n-- %s", json_object_get_string(pluginJ));
goto OnErrorExit;
}
-
+
// default basename equal uid
if (!basename) basename=ctlPlugin->uid;
@@ -135,10 +135,10 @@ STATIC int PluginLoadOne (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object
} else {
AFB_ApiNotice(apiHandle, "CTL-PLUGIN-LOADONE %s successfully registered", ctlPluginMagic->uid);
}
-
+
// store dlopen handle to enable onload action at exec time
ctlPlugin->dlHandle = dlHandle;
-
+
#ifndef AFB_BINDING_PREV3
// Jose hack to make verbosity visible from sharelib with API-V2
struct afb_binding_data_v2 *afbHidenData = dlsym(dlHandle, "afbBindingV2data");
@@ -200,7 +200,7 @@ STATIC int PluginLoadOne (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object
ctlPlugin->context = (*ctlPluginOnload) (ctlPlugin, handle);
}
return 0;
-
+
OnErrorExit:
return 1;
}
@@ -208,7 +208,7 @@ OnErrorExit:
PUBLIC int PluginConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *pluginsJ) {
int err=0;
-
+
if (json_object_get_type(pluginsJ) == json_type_array) {
int length = json_object_array_length(pluginsJ);
ctlPlugins = calloc (length+1, sizeof(CtlPluginT));
@@ -218,8 +218,8 @@ PUBLIC int PluginConfig(AFB_ApiT apiHandle, CtlSectionT *section, json_object *p
}
} else {
ctlPlugins = calloc (2, sizeof(CtlPluginT));
- err += PluginLoadOne(apiHandle, &ctlPlugins[0], pluginsJ, section->handle);
+ err += PluginLoadOne(apiHandle, &ctlPlugins[0], pluginsJ, section->handle);
}
-
+
return err;
-} \ No newline at end of file
+}
diff --git a/ctl-lib/ctl-plugin.h b/ctl-lib/ctl-plugin.h
index 2613f9b..f2bc33e 100644
--- a/ctl-lib/ctl-plugin.h
+++ b/ctl-lib/ctl-plugin.h
@@ -52,7 +52,7 @@
#define AFB_ReqDebug(request, ...) AFB_REQUEST_DEBUG (request, __VA_ARGS__)
#define AFB_ReqError(request, ...) AFB_REQUEST_ERROR (request, __VA_ARGS__)
#define AFB_ReqInfo(request, ...) AFB_REQUEST_INFO (request, __VA_ARGS__)
-
+
#define AFB_ApiVerbose(api, level, ...) afb_dynapi_verbose(api, level, __VA_ARGS__)
#define AFB_ApiNotice(api, ...) AFB_DYNAPI_NOTICE (api, __VA_ARGS__)
#define AFB_ApiWarning(api, ...) AFB_DYNAPI_WARNING (api, __VA_ARGS__)
@@ -63,23 +63,23 @@
#define AFB_ReqIsValid(request) request
#define AFB_EvtIsValid(evtHandle) evtHandle
- #define AFB_ServiceCall(api, ...) afb_dynapi_call(api, __VA_ARGS__)
- #define AFB_ServiceSync(api, ...) afb_dynapi_call_sync(api, __VA_ARGS__)
+ #define AFB_ServiceCall(api, ...) afb_dynapi_call(api, __VA_ARGS__)
+ #define AFB_ServiceSync(api, ...) afb_dynapi_call_sync(api, __VA_ARGS__)
- #define AFB_RequireApi(api, ...) afb_dynapi_require_api(api, __VA_ARGS__)
+ #define AFB_RequireApi(api, ...) afb_dynapi_require_api(api, __VA_ARGS__)
- #define AFB_GetEventLoop(api) afb_dynapi_get_event_loop(api)
+ #define AFB_GetEventLoop(api) afb_dynapi_get_event_loop(api)
#define AFB_ClientCtxSet(request, replace, createCB, freeCB, handle) afb_request_context(request, replace, createCB, freeCB, handle)
#define AFB_ClientCtxClear(request) afb_request_context_clear(request)
- typedef struct {
+ typedef struct {
const char *verb; /* name of the verb, NULL only at end of the array */
void (*callback)(AFB_ReqT req); /* callback function implementing the verb */
const struct afb_auth *auth; /* required authorisation, can be NULL */
const char *info; /* some info about the verb, can be NULL */
- uint32_t session;
+ uint32_t session;
} AFB_ApiVerbs;
#else
@@ -89,8 +89,8 @@
typedef afb_req AFB_ReqT;
typedef void* AFB_ApiT;
#define AFB_ReqNone (struct afb_req){0,0}
-
- typedef afb_event AFB_EventT;
+
+ typedef afb_event AFB_EventT;
#define AFB_EventPush afb_event_push
#define AFB_ReqSubscribe afb_req_subscribe
#define AFB_EventIsValid(event) afb_event_is_valid(event)
@@ -118,15 +118,12 @@
#define AFB_ReqIsValid(request) afb_req_is_valid(request)
#define AFB_EvtIsValid(evtHandle) afb_event_is_valid(evtHandle)
- #define AFB_ServiceCall(api, ...) afb_service_call(__VA_ARGS__)
- #define AFB_ServiceSync(api, ...) afb_service_call_sync(__VA_ARGS__)
+ #define AFB_ServiceCall(api, ...) afb_service_call(__VA_ARGS__)
+ #define AFB_ServiceSync(api, ...) afb_service_call_sync(__VA_ARGS__)
- #define AFB_RequireApi(api, ...) afb_daemon_require_api(__VA_ARGS__)
+ #define AFB_RequireApi(api, ...) afb_daemon_require_api(__VA_ARGS__)
- #define AFB_GetEventLoop(api) afb_daemon_get_event_loop()
-
- #define AFB_ClientCtxSet(request, replace, createCB, freeCB, handle) afb_req_context_set(request, createCB(handle), freeCB)
- #define AFB_ClientCtxClear(request) afb_req_context_clear(request)
+ #define AFB_GetEventLoop(api) afb_daemon_get_event_loop()
static inline void* AFB_ClientCtxSet(afb_req request, int replace, void *(*create_context)(void *closure), void (*free_context)(void*), void *closure)
{
@@ -136,6 +133,7 @@
return ctx;
}
+ #define AFB_ClientCtxClear(request) afb_req_context_clear(request)
#define AFB_ApiVerbs afb_verb_v2
#endif
@@ -159,15 +157,15 @@
#endif
-
+
typedef struct {
const char *uid;
const long magic;
} CtlPluginMagicT;
typedef struct {
- const char *uid;
- const char *info;
+ const char *uid;
+ const char *info;
AFB_ApiT api;
void *dlHandle;
void *context;
@@ -198,7 +196,7 @@ typedef struct {
typedef struct {
const char *uid;
- const char *info;
+ const char *info;
const char *privileges;
AFB_ApiT api;
json_object *argsJ;
@@ -208,17 +206,17 @@ typedef struct {
const char* api;
const char* verb;
} subcall;
-
+
struct {
const char* load;
const char* funcname;
} lua;
-
+
struct {
const char* funcname;
int (*callback)(CtlSourceT *source, json_object *argsJ, json_object *queryJ);
CtlPluginT *plugin;
- } cb;
+ } cb;
} exec;
} CtlActionT;