From 5e919fde0a4c66b0203c46b8f06f303fcceaedde Mon Sep 17 00:00:00 2001 From: Fulup Ar Foll Date: Thu, 17 Aug 2017 00:56:43 +0200 Subject: Implemented Timer and Event from Lua --- Controler-afb/CMakeLists.txt | 2 +- Controler-afb/ctl-apidef.h | 8 +- Controler-afb/ctl-apidef.json | 2 +- Controler-afb/ctl-binding.h | 15 ++ Controler-afb/ctl-events.c | 155 -------------- Controler-afb/ctl-lua.c | 293 +++++++++++++++++++++----- Controler-afb/ctl-timer.c | 156 ++++++++++++++ conf.d/project/lua.d/onload-audio-oncall.lua | 31 --- conf.d/project/lua.d/onload-audio-timer.lua | 73 +++++++ htdocs/audio-control.html | 1 + nbproject/configurations.xml | 302 ++++++++++----------------- 11 files changed, 602 insertions(+), 436 deletions(-) delete mode 100644 Controler-afb/ctl-events.c create mode 100644 Controler-afb/ctl-timer.c create mode 100644 conf.d/project/lua.d/onload-audio-timer.lua 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 #include #include +#include + #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-events.c b/Controler-afb/ctl-events.c deleted file mode 100644 index 18aac53..0000000 --- a/Controler-afb/ctl-events.c +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (C) 2016 "IoT.bzh" - * Author Fulup Ar Foll - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include - -#include "ctl-binding.h" - -#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) { - TimerHandleT *timerHandle = (TimerHandleT*) handle; - int done; - uint64_t usec; - - // Rearm timer if needed - timerHandle->count --; - if (timerHandle->count == 0) sd_event_source_unref(source); - 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); - } - - done= timerHandle->callback(timerHandle->context); - if (!done) goto OnErrorExit; - - return 0; - -OnErrorExit: - AFB_WARNING("TimerNext Callback Fail Tag=%s", timerHandle->context->label); - return -1; -} - - -STATIC int DoSendEvent (void *context) { - AutoTestCtxT *ctx= (AutoTestCtxT*)context; - json_object *ctlEventJ; - - if (ctx->value) ctx->value =0; - else ctx->value =1; - - ctlEventJ = json_object_new_object(); - json_object_object_add(ctlEventJ,"signal", json_object_new_string(ctx->label)); - json_object_object_add(ctlEventJ,"value" , json_object_new_int(ctx->value)); - int done = afb_event_push(afbevt, ctlEventJ); - - AFB_NOTICE ("DoSendEvent {action: '%s', value:%d} status=%d", ctx->label, ctx->value, done); - - 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) { - json_object *queryJ, *tmpJ; - TimerHandleT *timerHandle = malloc (sizeof (TimerHandleT)); - AutoTestCtxT *context = calloc (1, sizeof (AutoTestCtxT)); - int done; - - queryJ= afb_req_json(request); - - // Closing call only has one parameter - done=json_object_object_get_ex(queryJ, "closing", &tmpJ); - if (done) return; - - done=json_object_object_get_ex(queryJ, "label", &tmpJ); - if (!done) { - afb_req_fail_f(request, "TEST-LABEL-MISSING", "label is mandatory for event_test"); - goto OnErrorExit; - } - context->label = strdup(json_object_get_string (tmpJ)); - - json_object_object_get_ex(queryJ, "delay", &tmpJ); - timerHandle->delay = json_object_get_int (tmpJ) * 1000; - if (timerHandle->delay == 0) timerHandle->delay=DEFAULT_PAUSE_DELAY * 1000; - - json_object_object_get_ex(queryJ, "count", &tmpJ); - timerHandle->count = json_object_get_int (tmpJ); - if (timerHandle->count == 0) timerHandle->count=DEFAULT_TEST_COUNT; - - // start a lool timer - TimerEvtStart (timerHandle, context); - - afb_req_success(request, NULL, NULL); - return; - - OnErrorExit: - return; -} - -// Create Binding Event at Init -PUBLIC int TimerEvtInit () { - - // create binder event to send test pause/resume - afbevt = afb_daemon_make_event("control"); - if (!afb_event_is_valid(afbevt)) { - AFB_ERROR ("POLCTL_INIT: Cannot register ctl-events"); - return 1; - } - - AFB_DEBUG ("Audio Control-Events Init Done"); - return 0; -} - 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-timer.c b/Controler-afb/ctl-timer.c new file mode 100644 index 0000000..f7ee6f0 --- /dev/null +++ b/Controler-afb/ctl-timer.c @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author Fulup Ar Foll + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include + +#include "ctl-binding.h" + +#define DEFAULT_PAUSE_DELAY 3000 +#define DEFAULT_TEST_COUNT 1 +typedef struct { + int value; + const char *label; +} AutoTestCtxT; + +static afb_event afbevt; + +STATIC int TimerNext (sd_event_source* source, uint64_t timer, void* handle) { + TimerHandleT *timerHandle = (TimerHandleT*) handle; + int done; + uint64_t usec; + + // Rearm timer if needed + timerHandle->count --; + 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*1000); + } + + done= timerHandle->callback(timerHandle->context); + if (!done) goto OnErrorExit; + + return 0; + +OnErrorExit: + 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; + json_object *ctlEventJ; + + if (ctx->value) ctx->value =0; + else ctx->value =1; + + ctlEventJ = json_object_new_object(); + json_object_object_add(ctlEventJ,"signal", json_object_new_string(ctx->label)); + json_object_object_add(ctlEventJ,"value" , json_object_new_int(ctx->value)); + int done = afb_event_push(afbevt, ctlEventJ); + + AFB_NOTICE ("DoSendEvent {action: '%s', value:%d} status=%d", ctx->label, ctx->value, done); + + return (done); +} + +// Generated some fake event based on watchdog/counter +PUBLIC void ctlapi_timer_test (afb_req request) { + json_object *queryJ, *tmpJ; + TimerHandleT *timerHandle = malloc (sizeof (TimerHandleT)); + AutoTestCtxT *context = calloc (1, sizeof (AutoTestCtxT)); + int done; + + queryJ= afb_req_json(request); + + // Closing call only has one parameter + done=json_object_object_get_ex(queryJ, "closing", &tmpJ); + if (done) return; + + done=json_object_object_get_ex(queryJ, "label", &tmpJ); + if (!done) { + afb_req_fail_f(request, "TEST-LABEL-MISSING", "label is mandatory for event_test"); + goto OnErrorExit; + } + timerHandle->label = strdup(json_object_get_string (tmpJ)); + + json_object_object_get_ex(queryJ, "delay", &tmpJ); + timerHandle->delay = json_object_get_int (tmpJ) * 1000; + if (timerHandle->delay == 0) timerHandle->delay=DEFAULT_PAUSE_DELAY * 1000; + + json_object_object_get_ex(queryJ, "count", &tmpJ); + timerHandle->count = json_object_get_int (tmpJ); + if (timerHandle->count == 0) timerHandle->count=DEFAULT_TEST_COUNT; + + // start a lool timer + TimerEvtStart (timerHandle, DoSendEvent, context); + + afb_req_success(request, NULL, NULL); + return; + + OnErrorExit: + return; +} + +// Create Binding Event at Init +PUBLIC int TimerEvtInit () { + + // create binder event to send test pause/resume + afbevt = afb_daemon_make_event("control"); + if (!afb_event_is_valid(afbevt)) { + AFB_ERROR ("POLCTL_INIT: Cannot register ctl-events"); + return 1; + } + + AFB_DEBUG ("Audio Control-Events Init Done"); + return 0; +} + diff --git a/conf.d/project/lua.d/onload-audio-oncall.lua b/conf.d/project/lua.d/onload-audio-oncall.lua index 13cda4b..08b25e5 100644 --- a/conf.d/project/lua.d/onload-audio-oncall.lua +++ b/conf.d/project/lua.d/onload-audio-oncall.lua @@ -68,34 +68,3 @@ function Test_Call_Sync (request, args) end end --- create a new event name -function Test_Event_Make (request, args) - - AFB:notice ("Test_Event_Make args=%s", args) - - local err evt_handle= AFB:event (args["evtname"]) - if (err) then - AFB:fail ("AFB:Test_Event_Make fail event=%s", args["evtname"]); - else - AFB:success (request, {}) - end - - local evtData = { - ["val1"]="My 1st private Event", - ["val2"]=5678 - } - - AFB:notify (evt_handle, evtData) -end - --- send an event on default binder event -function Test_Event_Notify (request, args) - - AFB:notice ("Test_Event_Notify args=%s", args) - local err AFB:notify (args) - if (err) then - AFB:fail ("AFB:Test_Event_Make fail event=%s", args["evtname"]); - else - AFB:success (request, {}) - end -end diff --git a/conf.d/project/lua.d/onload-audio-timer.lua b/conf.d/project/lua.d/onload-audio-timer.lua new file mode 100644 index 0000000..5f25de7 --- /dev/null +++ b/conf.d/project/lua.d/onload-audio-timer.lua @@ -0,0 +1,73 @@ +--[[ + Copyright (C) 2016 "IoT.bzh" + Author Fulup Ar Foll + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + Provide Sample Timer Handing to push event from LUA +--]] + +-- Create event on Lua script load +print ("*** Create MyTestEvent ***") +MyEventHandle= AFB:evtmake("MyTestEvent") + +-- Call count time every delay/ms +function Timer_Test_CB (timer, context) + + local evtinfo= AFB:timerget(timer) + print ("timer=", Dump_Table(evtinfo)) + + --send an event an event with count as value + AFB:evtpush (MyEventHandle, {["label"]= evtinfo["label"], ["count"]=evtinfo["count"], ["info"]=context["info"]}) + + -- note when timerCB return!=0 timer is kill + return 0 + +end + +-- sendback event depending on count and delay +function Simple_Timer_Test (request, args) + + local context = { + ["info"]="My 1st private Event", + } + + -- if delay not defined default is 5s + if (args["delay"]==nil) then args["delay"]=5000 end + + -- if count is not defined default is 10 + if (args["count"]==nil) then args["count"]=10 end + + -- we could use directly args but it is a sample + local myTimer = { + ["label"]=args["label"], + ["delay"]=args["delay"], + ["count"]=args["count"], + } + AFB:notice ("Test_Timer myTimer=%s", myTimer) + + -- subscribe to event + AFB:subscribe (request, MyEventHandle) + + -- settimer take a table with delay+count as input (count==0 means infinite) + AFB:timerset (myTimer, "Timer_Test_CB", context) + + -- nothing special to return send back args + AFB:success (request, myTimer) + + return 0 +end + + + diff --git a/htdocs/audio-control.html b/htdocs/audio-control.html index d51f527..d51ae55 100644 --- a/htdocs/audio-control.html +++ b/htdocs/audio-control.html @@ -32,6 +32,7 @@
  • +
  • diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index fb247d1..a5d9f8e 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -46,10 +46,10 @@ ctl-binding.c ctl-dispatch.c - ctl-events.c ctl-lua.c ctl-misc.c ctl-plugin-sample.c + ctl-timer.c @@ -147,39 +147,87 @@ ex="false" tool="0" flavor2="3"> - + - + + + ../../../opt/include/alsa + /usr/include/json-c + Audio-Common + ../../../opt/include + build/Alsa-afb + - + + + build/Alsa-afb + - + + + ../../../opt/include/alsa + /usr/include/json-c + Audio-Common + ../../../opt/include + build/Alsa-afb + - + + + ../../../opt/include/alsa + /usr/include/json-c + Audio-Common + ../../../opt/include + build/Alsa-afb + - + + + ../../../opt/include/alsa + /usr/include/json-c + Audio-Common + ../../../opt/include + build/Alsa-afb + - + + + ../../../opt/include/afb + Audio-Common + /usr/include/json-c + build/Audio-Common + - - + + + + ../../../opt/include/afb + Audio-Common + /usr/include/json-c + build/Audio-Common + - + + + Audio-Common + /usr/include/json-c + build/Audio-Common + @@ -218,37 +266,19 @@ - + - ../../../opt/include - ../../../opt/include/alsa - /usr/include/p11-kit-1 + ../../../opt/include/afb + Controler-afb /usr/include/json-c /usr/include/lua5.3 Audio-Common + ../../../opt/include build/Controler-afb - - CONTROL_CONFIG_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/config.d:/usr/local/controler/config.d" - CONTROL_CONFIG_POST="control" - CONTROL_CONFIG_PRE="onload" - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_LUA_EVENT="luaevt" - CONTROL_LUA_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/lua.d:/usr/local/controler/ctl-lua.d" - CONTROL_MAXPATH_LEN=255 - CONTROL_ONLOAD_DEFAULT="onload-default" - CONTROL_PLUGIN_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/build:/home/fulup/opt/audio-bindings/ctlplug:/usr/lib/afb/ctlplug" - CONTROL_SUPPORT_LUA - CTL_PLUGIN_MAGIC=2468013579 - MAX_LINEAR_DB_SCALE=24 - MAX_SND_CARD=16 - NATIVE_LINUX - TLV_BYTE_SIZE=256 - control_afb_EXPORTS - - + ../../../opt/include @@ -279,94 +309,65 @@ - - + + - ../../../opt/include - ../../../opt/include/alsa - /usr/include/p11-kit-1 + ../../../opt/include/afb + Controler-afb /usr/include/json-c - /usr/include/lua5.3 - Audio-Common build/Controler-afb - - CONTROL_CONFIG_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/config.d:/usr/local/controler/config.d" - CONTROL_CONFIG_POST="control" - CONTROL_CONFIG_PRE="onload" - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_LUA_EVENT="luaevt" - CONTROL_LUA_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/lua.d:/usr/local/controler/ctl-lua.d" - CONTROL_MAXPATH_LEN=255 - CONTROL_ONLOAD_DEFAULT="onload-default" - CONTROL_PLUGIN_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/build:/home/fulup/opt/audio-bindings/ctlplug:/usr/lib/afb/ctlplug" - CONTROL_SUPPORT_LUA - CTL_PLUGIN_MAGIC=2468013579 - MAX_LINEAR_DB_SCALE=24 - MAX_SND_CARD=16 - NATIVE_LINUX - TLV_BYTE_SIZE=256 - control_afb_EXPORTS - - + ../../../opt/include/afb Controler-afb /usr/include/json-c + /usr/include/lua5.3 build/Controler-afb - - + + - ../../../opt/include - ../../../opt/include/alsa - /usr/include/p11-kit-1 + ../../../opt/include/afb + Controler-afb /usr/include/json-c - /usr/include/lua5.3 - Audio-Common + ../../../opt/include build/Controler-afb - - CONTROL_CONFIG_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/config.d:/usr/local/controler/config.d" - CONTROL_CONFIG_POST="control" - CONTROL_CONFIG_PRE="onload" - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_LUA_EVENT="luaevt" - CONTROL_LUA_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/lua.d:/usr/local/controler/ctl-lua.d" - CONTROL_MAXPATH_LEN=255 - CONTROL_ONLOAD_DEFAULT="onload-default" - CONTROL_PLUGIN_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/build:/home/fulup/opt/audio-bindings/ctlplug:/usr/lib/afb/ctlplug" - CONTROL_SUPPORT_LUA - CTL_PLUGIN_MAGIC=2468013579 - MAX_LINEAR_DB_SCALE=24 - MAX_SND_CARD=16 - NATIVE_LINUX - TLV_BYTE_SIZE=256 - audio_plugin_sample_EXPORTS - - + + + Audio-Common + build/HAL-afb/HAL-interface + - + + + Audio-Common + build/HAL-afb/HAL-interface + - + + + build/HAL-afb/HAL-interface + @@ -374,21 +375,21 @@ - + - + - + @@ -453,11 +454,11 @@ - ../../../opt/include + Alsa-Plugin/Alsa-Policy-Hook ../../../opt/include/alsa - /usr/include/p11-kit-1 /usr/include/json-c - /usr/include/lua5.3 + ../../../opt/include/afb + ../../../opt/include build/Alsa-Plugin/Alsa-Policy-Hook @@ -484,62 +485,9 @@ - ../../../opt/include - ../../../opt/include/alsa - /usr/include/p11-kit-1 - /usr/include/json-c - /usr/include/lua5.3 - Audio-Common - build/Alsa-afb - - - CONTROL_CONFIG_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/config.d:/usr/local/controler/config.d" - CONTROL_CONFIG_POST="control" - CONTROL_CONFIG_PRE="onload" - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_LUA_EVENT="luaevt" - CONTROL_LUA_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/lua.d:/usr/local/controler/ctl-lua.d" - CONTROL_MAXPATH_LEN=255 - CONTROL_ONLOAD_DEFAULT="onload-default" - CONTROL_PLUGIN_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/build:/home/fulup/opt/audio-bindings/ctlplug:/usr/lib/afb/ctlplug" - CONTROL_SUPPORT_LUA - CTL_PLUGIN_MAGIC=2468013579 - MAX_LINEAR_DB_SCALE=24 - MAX_SND_CARD=16 - NATIVE_LINUX - TLV_BYTE_SIZE=256 - alsa_lowlevel_EXPORTS - - - - - - - ../../../opt/include - ../../../opt/include/alsa - /usr/include/p11-kit-1 - /usr/include/json-c - /usr/include/lua5.3 - Audio-Common - build/Audio-Common + ../../../opt/include/afb + Alsa-afb - - CONTROL_CONFIG_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/config.d:/usr/local/controler/config.d" - CONTROL_CONFIG_POST="control" - CONTROL_CONFIG_PRE="onload" - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_LUA_EVENT="luaevt" - CONTROL_LUA_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/conf.d/project/lua.d:/usr/local/controler/ctl-lua.d" - CONTROL_MAXPATH_LEN=255 - CONTROL_ONLOAD_DEFAULT="onload-default" - CONTROL_PLUGIN_PATH="/home/fulup/Workspace/AGL-AppFW/audio-bindings-dev/build:/home/fulup/opt/audio-bindings/ctlplug:/usr/lib/afb/ctlplug" - CONTROL_SUPPORT_LUA - CTL_PLUGIN_MAGIC=2468013579 - MAX_LINEAR_DB_SCALE=24 - MAX_SND_CARD=16 - NATIVE_LINUX - TLV_BYTE_SIZE=256 - @@ -604,15 +552,9 @@ + ../../../opt/include/afb HAL-afb/HAL-interface - Audio-Common - build/HAL-afb/HAL-interface - - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_SUPPORT_LUA - NATIVE_LINUX - @@ -635,46 +577,34 @@ - HAL-afb/HAL-interface + ../../../opt/include/afb + HAL-afb/HDA-intel Audio-Common + HAL-afb/HAL-interface build/HAL-afb/HDA-intel - - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_SUPPORT_LUA - NATIVE_LINUX - hal_intel_hda_EXPORTS - - HAL-afb/HAL-interface + HAL-afb/Jabra-Solemate + ../../../opt/include/afb Audio-Common + HAL-afb/HAL-interface build/HAL-afb/Jabra-Solemate - - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_SUPPORT_LUA - NATIVE_LINUX - hal_jabra_usb_EXPORTS - - HAL-afb/HAL-interface + ../../../opt/include/afb + HAL-afb/Scarlett-Focusrite Audio-Common + HAL-afb/HAL-interface build/HAL-afb/Scarlett-Focusrite - - CONTROL_DOSCRIPT_PRE="doscript" - CONTROL_SUPPORT_LUA - NATIVE_LINUX - hal_scalett_usb_EXPORTS - @@ -1231,11 +1161,11 @@ - + - + @@ -1614,7 +1544,7 @@ - + Audio-Common @@ -1625,7 +1555,7 @@ - + Audio-Common @@ -1636,24 +1566,24 @@ - + - Audio-Common build/Controler-afb - control_afb_EXPORTS + audio_plugin_sample_EXPORTS - + + Audio-Common build/Controler-afb - audio_plugin_sample_EXPORTS + control_afb_EXPORTS -- cgit 1.2.3-korg