summaryrefslogtreecommitdiffstats
path: root/ctl-lib
diff options
context:
space:
mode:
Diffstat (limited to 'ctl-lib')
-rw-r--r--ctl-lib/ctl-lua.c16
-rw-r--r--ctl-lib/ctl-timer.c6
-rw-r--r--ctl-lib/ctl-timer.h7
3 files changed, 18 insertions, 11 deletions
diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c
index 85bd9c2..cdb245a 100644
--- a/ctl-lib/ctl-lua.c
+++ b/ctl-lib/ctl-lua.c
@@ -959,18 +959,23 @@ OnErrorExit:
// Timer Callback
// Set timer
-STATIC int LuaTimerSetCB (void *handle) {
- LuaCbHandleT *LuaCbHandle = (LuaCbHandleT*) handle;
+STATIC int LuaTimerSetCB (TimerHandleT *timer) {
+ LuaCbHandleT *LuaCbHandle = (LuaCbHandleT*) timer->context;
int count;
// push timer handle and user context on Lua stack
lua_getglobal(luaState, LuaCbHandle->callback);
- count=1;
// Push AFB client context on the stack
+ count=1;
LuaAfbSourceT *afbSource= LuaSourcePush(luaState, LuaCbHandle->source);
if (!afbSource) goto OnErrorExit;
+ // Push AFB client context on the stack
+ count ++;
+ lua_pushlightuserdata(luaState, timer);
+ if (!afbSource) goto OnErrorExit;
+
// Push user Context
count+= LuaPushArgument(LuaCbHandle->source, LuaCbHandle->context);
@@ -1014,13 +1019,14 @@ STATIC int LuaTimerSet(lua_State* luaState) {
json_object *contextJ = LuaPopOneArg(source, luaState, LUA_FIST_ARG + 3);
if (lua_gettop(luaState) != LUA_FIST_ARG+3 || !timerJ || !callback || !contextJ) {
- lua_pushliteral(luaState, "LuaTimerSet-Syntax timerset (source, timerT, 'callback', contextT)");
+ lua_pushliteral(luaState, "LuaTimerSet: Syntax timerset (source, timerT, 'callback', contextT)");
goto OnErrorExit;
}
int err = wrap_json_unpack(timerJ, "{ss, s?s si, si !}", "uid", &uid, "info", &info, "delay", &delay, "count", &count);
if (err) {
- lua_pushliteral(luaState, "LuaTimerSet-Syntax timerT={uid:xxx delay:ms, count:xx}");
+
+ lua_pushliteral(luaState, "LuaTimerSet: Syntax timerT={uid:xxx delay:ms, count:xx}");
goto OnErrorExit;
}
diff --git a/ctl-lib/ctl-timer.c b/ctl-lib/ctl-timer.c
index 9cab0b2..e1ced46 100644
--- a/ctl-lib/ctl-timer.c
+++ b/ctl-lib/ctl-timer.c
@@ -36,6 +36,9 @@ STATIC int TimerNext (sd_event_source* source, uint64_t timer, void* handle) {
int done;
uint64_t usec;
+ done= timerHandle->callback(timerHandle);
+ if (!done) goto OnErrorExit;
+
// Rearm timer if needed
timerHandle->count --;
if (timerHandle->count == 0) {
@@ -51,9 +54,6 @@ STATIC int TimerNext (sd_event_source* source, uint64_t timer, void* handle) {
sd_event_source_set_time(source, usec + timerHandle->delay*1000);
}
- done= timerHandle->callback(timerHandle->context);
- if (!done) goto OnErrorExit;
-
return 0;
OnErrorExit:
diff --git a/ctl-lib/ctl-timer.h b/ctl-lib/ctl-timer.h
index 6256dcc..333f172 100644
--- a/ctl-lib/ctl-timer.h
+++ b/ctl-lib/ctl-timer.h
@@ -23,7 +23,6 @@
// ctl-timer.c
// ----------------------
-typedef int (*timerCallbackT)(void *context);
typedef struct TimerHandleS {
int magic;
@@ -31,12 +30,14 @@ typedef struct TimerHandleS {
int delay;
const char*uid;
void *context;
- timerCallbackT callback;
sd_event_source *evtSource;
AFB_ApiT api;
- timerCallbackT freeCB;
+ int (*callback) (struct TimerHandleS *handle);
+ int (*freeCB) (void *context) ;
} TimerHandleT;
+typedef int (*timerCallbackT)(TimerHandleT *context);
+
PUBLIC int TimerEvtInit (AFB_ApiT apiHandle);
PUBLIC void TimerEvtStart(AFB_ApiT apiHandle, TimerHandleT *timerHandle, timerCallbackT callback, void *context);
PUBLIC void TimerEvtStop(TimerHandleT *timerHandle);