diff options
author | Fulup Ar Foll <fulup@iot.bzh> | 2017-08-17 00:56:43 +0200 |
---|---|---|
committer | Fulup Ar Foll <fulup@iot.bzh> | 2017-08-17 00:56:43 +0200 |
commit | 5e919fde0a4c66b0203c46b8f06f303fcceaedde (patch) | |
tree | 6e4937b46d7370e9c8abe1f9a829759deda75ba2 /Controler-afb | |
parent | a4899ab57f08aeb2741d08f74d7593c85a0ad3f4 (diff) |
Implemented Timer and Event from Lua
Diffstat (limited to 'Controler-afb')
-rw-r--r-- | Controler-afb/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Controler-afb/ctl-apidef.h | 8 | ||||
-rw-r--r-- | Controler-afb/ctl-apidef.json | 2 | ||||
-rw-r--r-- | Controler-afb/ctl-binding.h | 15 | ||||
-rw-r--r-- | Controler-afb/ctl-lua.c | 293 | ||||
-rw-r--r-- | Controler-afb/ctl-timer.c (renamed from Controler-afb/ctl-events.c) | 65 |
6 files changed, 289 insertions, 96 deletions
diff --git a/Controler-afb/CMakeLists.txt b/Controler-afb/CMakeLists.txt index 30fcb37..a9e8e60 100644 --- a/Controler-afb/CMakeLists.txt +++ b/Controler-afb/CMakeLists.txt @@ -38,7 +38,7 @@ endif(CONTROL_SUPPORT_LUA) PROJECT_TARGET_ADD(control-afb) # Define project Targets - ADD_LIBRARY(${TARGET_NAME} MODULE ctl-binding.c ctl-events.c ctl-dispatch.c ${CTL_LUA_SOURCE}) + ADD_LIBRARY(${TARGET_NAME} MODULE ctl-binding.c ctl-timer.c ctl-dispatch.c ${CTL_LUA_SOURCE}) # Generate API-v2 hat from OpenAPI json definition SET_TARGET_GENSKEL(${TARGET_NAME} ctl-apidef) diff --git a/Controler-afb/ctl-apidef.h b/Controler-afb/ctl-apidef.h index 9eb271d..848fd14 100644 --- a/Controler-afb/ctl-apidef.h +++ b/Controler-afb/ctl-apidef.h @@ -31,7 +31,7 @@ static const char _afb_description_v2_control[] = " End\",\"get\":{\"x-permissions\":{\"$ref\":\"#/components/x-permissions" "/monitor\"},\"parameters\":[{\"in\":\"query\",\"name\":\"event_patern\"," "\"required\":true,\"schema\":{\"type\":\"string\"}}],\"responses\":{\"20" - "0\":{\"$ref\":\"#/components/responses/200\"}}}},\"/event_test\":{\"desc" + "0\":{\"$ref\":\"#/components/responses/200\"}}}},\"/timer_test\":{\"desc" "ription\":\"Pause Resume Test\",\"get\":{\"x-permissions\":{\"$ref\":\"#" "/components/x-permissions/monitor\"},\"parameters\":[{\"in\":\"query\",\"" "name\":\"delay\",\"required\":false,\"schema\":{\"type\":\"interger\"}}," @@ -64,7 +64,7 @@ static const struct afb_auth _afb_auths_v2_control[] = { }; void ctlapi_monitor(struct afb_req req); - void ctlapi_event_test(struct afb_req req); + void ctlapi_timer_test(struct afb_req req); void ctlapi_dispatch(struct afb_req req); void ctlapi_lua_docall(struct afb_req req); void ctlapi_lua_dostring(struct afb_req req); @@ -79,8 +79,8 @@ static const struct afb_verb_v2 _afb_verbs_v2_control[] = { .session = AFB_SESSION_NONE_V2 }, { - .verb = "event_test", - .callback = ctlapi_event_test, + .verb = "timer_test", + .callback = ctlapi_timer_test, .auth = &_afb_auths_v2_control[0], .info = NULL, .session = AFB_SESSION_NONE_V2 diff --git a/Controler-afb/ctl-apidef.json b/Controler-afb/ctl-apidef.json index 93456ab..621fd5e 100644 --- a/Controler-afb/ctl-apidef.json +++ b/Controler-afb/ctl-apidef.json @@ -147,7 +147,7 @@ } } }, - "/event_test": { + "/timer_test": { "description": "Pause Resume Test", "get": { "x-permissions": { diff --git a/Controler-afb/ctl-binding.h b/Controler-afb/ctl-binding.h index 33a9049..c998007 100644 --- a/Controler-afb/ctl-binding.h +++ b/Controler-afb/ctl-binding.h @@ -24,6 +24,8 @@ #include <json-c/json.h> #include <filescan-utils.h> #include <wrap-json.h> +#include <systemd/sd-event.h> + #ifdef CONTROL_SUPPORT_LUA #include "lua.h" @@ -46,9 +48,22 @@ PUBLIC int CtlBindingInit (); // ctl-timerevt.c // ---------------------- +typedef int (*timerCallbackT)(void *context); + +typedef struct TimerHandleS { + int count; + int delay; + const char*label; + void *context; + timerCallbackT callback; + sd_event_source *evtSource; +} TimerHandleT; + PUBLIC int TimerEvtInit (void); PUBLIC afb_event TimerEvtGet(void); PUBLIC void ctlapi_event_test (afb_req request); +PUBLIC void TimerEvtStart(TimerHandleT *timerHandle, timerCallbackT callback, void *context); +PUBLIC void TimerEvtStop(TimerHandleT *timerHandle); // ctl-policy // ----------- diff --git a/Controler-afb/ctl-lua.c b/Controler-afb/ctl-lua.c index 7c59186..9d2cceb 100644 --- a/Controler-afb/ctl-lua.c +++ b/Controler-afb/ctl-lua.c @@ -31,6 +31,8 @@ #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 +static afb_req NULL_AFBREQ = {}; + static lua_State* luaState; @@ -47,12 +49,14 @@ static LuaAfbEvent *luaDefaultEvt; typedef struct { int ctxMagic; afb_req request; + void *handle; char *info; } LuaAfbContextT; typedef struct { const char *callback; json_object *context; + void *handle; } LuaCallServiceT; typedef enum { @@ -83,7 +87,7 @@ STATIC LuaAfbContextT *LuaCtxCheck (lua_State *luaState, int index) { return afbContext; } -STATIC LuaAfbContextT *LuaCtxPush (lua_State *luaState, afb_req request, const char* info) { +STATIC LuaAfbContextT *LuaCtxPush (lua_State *luaState, afb_req request, void *handle, const char* info) { // LuaAfbContextT *afbContext = (LuaAfbContextT *)lua_newuserdata(luaState, sizeof(LuaAfbContextT)); // luaL_setmetatable(luaState, CTX_TOKEN); LuaAfbContextT *afbContext = (LuaAfbContextT *)calloc(1, sizeof(LuaAfbContextT)); @@ -95,11 +99,13 @@ STATIC LuaAfbContextT *LuaCtxPush (lua_State *luaState, afb_req request, const c afbContext->ctxMagic=CTX_MAGIC; afbContext->info=strdup(info); afbContext->request= request; + afbContext->handle= handle; return afbContext; } STATIC void LuaCtxFree (LuaAfbContextT *afbContext) { - free (afbContext->info); + free(afbContext->info); + free(afbContext); } // Push a json structure on the stack as a LUA table @@ -153,7 +159,7 @@ STATIC int LuaPushArgument (json_object *argsJ) { return 1; } -STATIC json_object *PopOneArg (lua_State* luaState, int idx); +STATIC json_object *LuaPopOneArg (lua_State* luaState, int idx); STATIC json_object *LuaTableToJson (lua_State* luaState, int index) { int idx; @@ -173,7 +179,7 @@ STATIC json_object *LuaTableToJson (lua_State* luaState, int index) { snprintf(number, sizeof(number),"%d", idx); key=number; } - json_object *argJ= PopOneArg(luaState, LUA_VALUE_INDEX); + json_object *argJ= LuaPopOneArg(luaState, LUA_VALUE_INDEX); json_object_object_add(tableJ, key, argJ); lua_pop(luaState, 1); // removes 'value'; keeps 'key' for next iteration } @@ -186,7 +192,7 @@ STATIC json_object *LuaTableToJson (lua_State* luaState, int index) { return tableJ; } -STATIC json_object *PopOneArg (lua_State* luaState, int idx) { +STATIC json_object *LuaPopOneArg (lua_State* luaState, int idx) { json_object *value=NULL; int luaType = lua_type(luaState, idx); @@ -205,7 +211,7 @@ STATIC json_object *PopOneArg (lua_State* luaState, int idx) { value= json_object_new_boolean(lua_toboolean(luaState, idx)); break; case LUA_TSTRING: - value= json_object_new_string(lua_tostring(luaState, idx)); + value= json_object_new_string(lua_tostring(luaState, idx)); break; case LUA_TTABLE: value= LuaTableToJson(luaState, idx); @@ -213,9 +219,12 @@ STATIC json_object *PopOneArg (lua_State* luaState, int idx) { case LUA_TNIL: value=json_object_new_string("nil") ; break; + case LUA_TUSERDATA: + value=json_object_new_int64((int64_t)lua_touserdata(luaState, idx)); // store userdata as int !!! + break; default: - AFB_NOTICE ("PopOneArg: script returned Unknown/Unsupported idx=%d type:%d/%s", idx, luaType, lua_typename(luaState, luaType)); + AFB_NOTICE ("LuaPopOneArg: script returned Unknown/Unsupported idx=%d type:%d/%s", idx, luaType, lua_typename(luaState, luaType)); value=NULL; } @@ -230,12 +239,12 @@ static json_object *LuaPopArgs (lua_State* luaState, int start) { // start at 2 because we are using a function array lib if (start == stop) { - responseJ=PopOneArg (luaState, start); + responseJ=LuaPopOneArg (luaState, start); } else { // loop on remaining return arguments responseJ= json_object_new_array(); for (int idx=start; idx <= stop; idx++) { - json_object *argJ=PopOneArg (luaState, idx); + json_object *argJ=LuaPopOneArg (luaState, idx); if (!argJ) goto OnErrorExit; json_object_array_add(responseJ, argJ); } @@ -413,6 +422,8 @@ STATIC void LuaAfbServiceCB(void *handle, int iserror, struct json_object *respo if (err) { AFB_ERROR ("LUA-SERICE-CB:FAIL response=%s err=%s", json_object_get_string(responseJ), lua_tostring(luaState,-1) ); } + + free (contextCB); } @@ -472,60 +483,69 @@ STATIC int LuaAfbServiceSync(lua_State* luaState) { return 1; } -STATIC int LuaAfbMakeEvent(lua_State* luaState) { - int count = lua_gettop(luaState); - LuaAfbEvent *afbevt=calloc(1,sizeof(LuaAfbEvent)); +STATIC int LuaAfbEventPush(lua_State* luaState) { + LuaAfbEvent *afbevt; + int index; - if (count != 1 || !lua_isstring(luaState, 1)) { - lua_pushliteral (luaState, "LuaAfbMakeEvent-Syntax is evtHandle= AFB:event ('myEventName')"); - goto OnErrorExit; + // if no private event handle then use default binding event + if (lua_islightuserdata(luaState, LUA_FIST_ARG)) { + afbevt = (LuaAfbEvent*) lua_touserdata(luaState, LUA_FIST_ARG); + index=LUA_FIST_ARG+1; + } else { + index=LUA_FIST_ARG; + afbevt=luaDefaultEvt; } - - // event name should be the only argument - afbevt->name= strdup (lua_tostring(luaState,1)); - // create a new binder event - afbevt->event = afb_daemon_make_event(afbevt->name); if (!afb_event_is_valid(afbevt->event)) { - lua_pushliteral (luaState, "LuaAfbMakeEvent-Fail to Create Binder event"); + lua_pushliteral (luaState, "LuaAfbMakePush-Fail invalid event"); goto OnErrorExit; } - - // push event handler as a LUA opaque handle - lua_pushlightuserdata(luaState, afbevt); - return 1; + + json_object *ctlEventJ= LuaTableToJson(luaState, index); + if (!ctlEventJ) { + lua_pushliteral (luaState, "LuaAfbEventPush-Syntax is AFB:signal ([evtHandle], {lua table})"); + goto OnErrorExit; + } + + int done = afb_event_push(afbevt->event, ctlEventJ); + if (!done) { + lua_pushliteral (luaState, "LuaAfbEventPush-Fail No Subscriber to event"); + AFB_ERROR ("LuaAfbEventPush-Fail name subscriber event=%s count=%d", afbevt->name, afbevt->count); + goto OnErrorExit; + } + afbevt->count++; + return 0; OnErrorExit: lua_error(luaState); return 1; } -STATIC int LuaAfbPushEvent(lua_State* luaState) { +STATIC int LuaAfbEventSubscribe(lua_State* luaState) { LuaAfbEvent *afbevt; - int index; - + + LuaAfbContextT *afbContext= LuaCtxCheck(luaState, LUA_FIST_ARG); + if (!afbContext) { + lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail Invalid request handle"); + goto OnErrorExit; + } + // if no private event handle then use default binding event - if (lua_islightuserdata(luaState, 1)) { - afbevt = (LuaAfbEvent*) lua_touserdata(luaState, 1); - index=1; + if (lua_islightuserdata(luaState, LUA_FIST_ARG+1)) { + afbevt = (LuaAfbEvent*) lua_touserdata(luaState, LUA_FIST_ARG+1); } else { - index=2; afbevt=luaDefaultEvt; } - if (!lua_isstring(luaState, index)) { - lua_pushliteral (luaState, "LuaAfbPushEvent-Syntax is AFB:signal ([evtHandle], 'myEventName', 'data-1', ... 'data-n')"); - goto OnErrorExit; + if (!afb_event_is_valid(afbevt->event)) { + lua_pushliteral (luaState, "LuaAfbMakePush-Fail invalid event handle"); + goto OnErrorExit; } - - // use every other arguments as event parameters - index++; - json_object *ctlEventJ= LuaTableToJson(luaState, index); - - int done = afb_event_push(afbevt->event, ctlEventJ); - if (!done) { - lua_pushliteral (luaState, "LuaAfbPushEvent-Fail to Push Binder event"); - AFB_ERROR ("LuaAfbPushEvent-Fail to Push Binder event=%s count=%d", afbevt->name, afbevt->count); + + int err = afb_req_subscribe(afbContext->request, afbevt->event); + if (err) { + lua_pushliteral (luaState, "LuaAfbEventSubscribe-Fail No Subscriber to event"); + AFB_ERROR ("LuaAfbEventPush-Fail name subscriber event=%s count=%d", afbevt->name, afbevt->count); goto OnErrorExit; } afbevt->count++; @@ -536,6 +556,34 @@ STATIC int LuaAfbPushEvent(lua_State* luaState) { return 1; } +STATIC int LuaAfbEventMake(lua_State* luaState) { + int count = lua_gettop(luaState); + LuaAfbEvent *afbevt=calloc(1,sizeof(LuaAfbEvent)); + + if (count != LUA_FIST_ARG || !lua_isstring(luaState, LUA_FIST_ARG)) { + lua_pushliteral (luaState, "LuaAfbEventMake-Syntax is evtHandle= AFB:event ('myEventName')"); + goto OnErrorExit; + } + + // event name should be the only argument + afbevt->name= strdup (lua_tostring(luaState,LUA_FIST_ARG)); + + // create a new binder event + afbevt->event = afb_daemon_make_event(afbevt->name); + if (!afb_event_is_valid(afbevt->event)) { + lua_pushliteral (luaState, "LuaAfbEventMake-Fail to Create Binder event"); + goto OnErrorExit; + } + + // push event handler as a LUA opaque handle + lua_pushlightuserdata(luaState, afbevt); + return 1; + + OnErrorExit: + lua_error(luaState); + return 1; +} + // Function call from LUA when lua2c plugin L2C is used PUBLIC int Lua2cWrapper(lua_State* luaState, char *funcname, Lua2cFunctionT callback, void *context) { @@ -608,7 +656,7 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) { goto OnErrorExit; } // Push AFB client context on the stack - LuaAfbContextT *afbContext= LuaCtxPush(luaState, request, script); + LuaAfbContextT *afbContext= LuaCtxPush(luaState, request,NULL,script); if (!afbContext) goto OnErrorExit; break; @@ -628,7 +676,7 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) { lua_getglobal(luaState, func); // Push AFB client context on the stack - LuaAfbContextT *afbContext= LuaCtxPush(luaState, request, func); + LuaAfbContextT *afbContext= LuaCtxPush(luaState, request, NULL, func); if (!afbContext) goto OnErrorExit; // push query on the stack @@ -702,7 +750,7 @@ STATIC void LuaDoAction (LuaDoActionT action, afb_req request) { lua_getglobal(luaState, func); // Push AFB client context on the stack - LuaAfbContextT *afbContext= LuaCtxPush(luaState, request, func); + LuaAfbContextT *afbContext= LuaCtxPush(luaState, request, NULL, func); if (!afbContext) goto OnErrorExit; // push function arguments @@ -746,6 +794,131 @@ PUBLIC void ctlapi_lua_doscript (afb_req request) { LuaDoAction (LUA_DOSCRIPT, request); } +STATIC int LuaTimerClear (lua_State* luaState) { + + // Get Timer Handle + LuaAfbContextT *afbContext= LuaCtxCheck(luaState, LUA_FIST_ARG); + if (!afbContext) goto OnErrorExit; + + // retrieve useful information opaque handle + TimerHandleT *timerHandle = (TimerHandleT*)afbContext->handle; + + AFB_NOTICE ("LuaTimerClear timer=%s", timerHandle->label); + TimerEvtStop(timerHandle); + + return 0; //happy end + +OnErrorExit: + return 1; +} +STATIC int LuaTimerGet (lua_State* luaState) { + + // Get Timer Handle + LuaAfbContextT *afbContext= LuaCtxCheck(luaState, LUA_FIST_ARG); + if (!afbContext) goto OnErrorExit; + + // retrieve useful information opaque handle + TimerHandleT *timerHandle = (TimerHandleT*)afbContext->handle; + + // create response as a JSON object + json_object *responseJ= json_object_new_object(); + json_object_object_add(responseJ,"label", json_object_new_string(timerHandle->label)); + json_object_object_add(responseJ,"delay", json_object_new_int(timerHandle->delay)); + json_object_object_add(responseJ,"count", json_object_new_int(timerHandle->count)); + + // return JSON object as Lua table + int count=LuaPushArgument(responseJ); + + // free json object + json_object_put(responseJ); + + return count; // return argument + +OnErrorExit: + return 0; +} + +// Timer Callback + +// Set timer +STATIC int LuaTimerSetCB (void *handle) { + LuaCallServiceT *contextCB =(LuaCallServiceT*) handle; + TimerHandleT *timerHandle = (TimerHandleT*) contextCB->handle; + int count; + + // push timer handle and user context on Lua stack + lua_getglobal(luaState, contextCB->callback); + + // Push timer handle + LuaAfbContextT *afbContext= LuaCtxPush(luaState, NULL_AFBREQ, contextCB->handle, timerHandle->label); + if (!afbContext) goto OnErrorExit; + count=1; + + // Push user Context + count+= LuaPushArgument(contextCB->context); + + int err=lua_pcall(luaState, count, LUA_MULTRET, 0); + if (err) { + AFB_ERROR ("LUA-TIMER-CB:FAIL response=%s err=%s", json_object_get_string(contextCB->context), lua_tostring(luaState,-1)); + goto OnErrorExit; + } + + // get return parameter + if (!lua_isboolean(luaState, -1)) { + return (lua_toboolean(luaState, -1)); + } + + // timer last run free context resource + if (timerHandle->count == 1) { + LuaCtxFree(afbContext); + } + return 0; // By default we are happy + + OnErrorExit: + return 1; // stop timer +} + +STATIC int LuaTimerSet(lua_State* luaState) { + const char *label=NULL, *info=NULL; + int delay=0, count=0; + + json_object *timerJ = LuaPopOneArg(luaState, LUA_FIST_ARG); + const char *callback = lua_tostring(luaState, LUA_FIST_ARG + 1); + json_object *contextJ = LuaPopOneArg(luaState, LUA_FIST_ARG + 2); + + if (lua_gettop(luaState) != LUA_FIST_ARG+2 || !timerJ || !callback || !contextJ) { + lua_pushliteral(luaState, "LuaTimerSet-Syntax timerset (timerT, 'callback', contextT)"); + goto OnErrorExit; + } + + int err = wrap_json_unpack(timerJ, "{ss, s?s si, si !}", "label", &label, "info", &info, "delay", &delay, "count", &count); + if (err) { + lua_pushliteral(luaState, "LuaTimerSet-Syntax timerT={label:xxx delay:ms, count:xx}"); + goto OnErrorExit; + } + + // everything look fine create timer structure + TimerHandleT *timerHandle = malloc (sizeof (TimerHandleT)); + timerHandle->delay=delay; + timerHandle->count=count; + timerHandle->label=label; + + // Allocate handle to store context and callback + LuaCallServiceT *contextCB = calloc (1, sizeof(LuaCallServiceT)); + contextCB->callback= callback; + contextCB->context = contextJ; + contextCB->handle = timerHandle; + + // fire timer + TimerEvtStart (timerHandle, LuaTimerSetCB, contextCB); + + return 0; // Happy No Return Function + +OnErrorExit: + lua_error(luaState); + return 1; // return error code +} + // Register a new L2c list of LUA user plugin commands PUBLIC void LuaL2cNewLib(const char *label, luaL_Reg *l2cFunc, int count) { // luaL_newlib(luaState, l2cFunc); macro does not work with pointer :( @@ -756,17 +929,21 @@ PUBLIC void LuaL2cNewLib(const char *label, luaL_Reg *l2cFunc, int count) { } static const luaL_Reg afbFunction[] = { - {"notice" , LuaPrintNotice}, - {"info" , LuaPrintInfo}, - {"warning", LuaPrintWarning}, - {"debug" , LuaPrintDebug}, - {"error" , LuaPrintError}, - {"callsync", LuaAfbServiceSync}, - {"service", LuaAfbService}, - {"success", LuaAfbSuccess}, - {"fail" , LuaAfbFail}, - {"event" , LuaAfbMakeEvent}, - {"signal" , LuaAfbPushEvent}, + {"timerclear", LuaTimerClear}, + {"timerget" , LuaTimerGet}, + {"timerset" , LuaTimerSet}, + {"notice" , LuaPrintNotice}, + {"info" , LuaPrintInfo}, + {"warning" , LuaPrintWarning}, + {"debug" , LuaPrintDebug}, + {"error" , LuaPrintError}, + {"callsync" , LuaAfbServiceSync}, + {"service" , LuaAfbService}, + {"success" , LuaAfbSuccess}, + {"fail" , LuaAfbFail}, + {"subscribe" , LuaAfbEventSubscribe}, + {"evtmake" , LuaAfbEventMake}, + {"evtpush" , LuaAfbEventPush}, {NULL, NULL} /* sentinel */ }; diff --git a/Controler-afb/ctl-events.c b/Controler-afb/ctl-timer.c index 18aac53..f7ee6f0 100644 --- a/Controler-afb/ctl-events.c +++ b/Controler-afb/ctl-timer.c @@ -25,20 +25,11 @@ #define DEFAULT_PAUSE_DELAY 3000 #define DEFAULT_TEST_COUNT 1 -typedef int (*timerCallbackT)(void *context); typedef struct { int value; const char *label; } AutoTestCtxT; -typedef struct TimerHandleS { - int count; - int delay; - AutoTestCtxT *context; - timerCallbackT callback; - sd_event_source *evtSource; -} TimerHandleT; - static afb_event afbevt; STATIC int TimerNext (sd_event_source* source, uint64_t timer, void* handle) { @@ -48,12 +39,16 @@ STATIC int TimerNext (sd_event_source* source, uint64_t timer, void* handle) { // Rearm timer if needed timerHandle->count --; - if (timerHandle->count == 0) sd_event_source_unref(source); + if (timerHandle->count == 0) { + sd_event_source_unref(source); + free (handle); + return 0; + } else { // otherwise validate timer for a new run sd_event_now(afb_daemon_get_event_loop(), CLOCK_MONOTONIC, &usec); sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); - sd_event_source_set_time(source, usec + timerHandle->delay); + sd_event_source_set_time(source, usec + timerHandle->delay*1000); } done= timerHandle->callback(timerHandle->context); @@ -62,10 +57,33 @@ STATIC int TimerNext (sd_event_source* source, uint64_t timer, void* handle) { return 0; OnErrorExit: - AFB_WARNING("TimerNext Callback Fail Tag=%s", timerHandle->context->label); + AFB_WARNING("TimerNext Callback Fail Tag=%s", timerHandle->label); return -1; } +PUBLIC void TimerEvtStop(TimerHandleT *timerHandle) { + + sd_event_source_unref(timerHandle->evtSource); + free (timerHandle); +} + + +PUBLIC void TimerEvtStart(TimerHandleT *timerHandle, timerCallbackT callback, void *context) { + uint64_t usec; + + // populate CB handle + timerHandle->callback=callback; + timerHandle->context=context; + + // set a timer with ~250us accuracy + sd_event_now(afb_daemon_get_event_loop(), CLOCK_MONOTONIC, &usec); + sd_event_add_time(afb_daemon_get_event_loop(), &timerHandle->evtSource, CLOCK_MONOTONIC, usec+timerHandle->delay*1000, 250, TimerNext, timerHandle); +} + +PUBLIC afb_event TimerEvtGet(void) { + return afbevt; +} + STATIC int DoSendEvent (void *context) { AutoTestCtxT *ctx= (AutoTestCtxT*)context; @@ -84,25 +102,8 @@ STATIC int DoSendEvent (void *context) { return (done); } -STATIC void TimerEvtStart(TimerHandleT *timerHandle, void *context) { - uint64_t usec; - - // populate CB handle - timerHandle->callback=DoSendEvent; - timerHandle->context=context; - - // set a timer with ~250us accuracy - sd_event_now(afb_daemon_get_event_loop(), CLOCK_MONOTONIC, &usec); - sd_event_add_time(afb_daemon_get_event_loop(), &timerHandle->evtSource, CLOCK_MONOTONIC, usec+timerHandle->delay, 250, TimerNext, timerHandle); -} - -PUBLIC afb_event TimerEvtGet(void) { - return afbevt; -} - - // Generated some fake event based on watchdog/counter -PUBLIC void ctlapi_event_test (afb_req request) { +PUBLIC void ctlapi_timer_test (afb_req request) { json_object *queryJ, *tmpJ; TimerHandleT *timerHandle = malloc (sizeof (TimerHandleT)); AutoTestCtxT *context = calloc (1, sizeof (AutoTestCtxT)); @@ -119,7 +120,7 @@ PUBLIC void ctlapi_event_test (afb_req request) { afb_req_fail_f(request, "TEST-LABEL-MISSING", "label is mandatory for event_test"); goto OnErrorExit; } - context->label = strdup(json_object_get_string (tmpJ)); + timerHandle->label = strdup(json_object_get_string (tmpJ)); json_object_object_get_ex(queryJ, "delay", &tmpJ); timerHandle->delay = json_object_get_int (tmpJ) * 1000; @@ -130,7 +131,7 @@ PUBLIC void ctlapi_event_test (afb_req request) { if (timerHandle->count == 0) timerHandle->count=DEFAULT_TEST_COUNT; // start a lool timer - TimerEvtStart (timerHandle, context); + TimerEvtStart (timerHandle, DoSendEvent, context); afb_req_success(request, NULL, NULL); return; |