From 869b777af8fab6ba416a9687932e7b26f7d78aa4 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Tue, 18 Dec 2018 10:53:19 +0100 Subject: Make available the unsubscribe from a LUA script Until now only the subscribe operation was available from a LUA script, this implements the missing unsubscribe. Change-Id: I9e6e9c727cd65fe5f167d06e7c871575f3e705c4 Signed-off-by: Romain Forlot --- ctl-lib/ctl-lua.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c index 9b0d572..40459dc 100644 --- a/ctl-lib/ctl-lua.c +++ b/ctl-lib/ctl-lua.c @@ -589,6 +589,36 @@ static int LuaAfbEventSubscribe(lua_State* luaState) { if (err) { lua_pushliteral(luaState, "LuaAfbEventSubscribe-Fail No Subscriber to event"); AFB_API_ERROR(source->api, "LuaAfbEventPush-Fail name subscriber event=%s count=%d", afbevt->name, afbevt->count); + +static int LuaAfbEventUnsubscribe(lua_State* luaState) { + LuaAfbEvent *afbevt; + + CtlSourceT *source = LuaSourcePop(luaState, LUA_FIRST_ARG); + if (!source) { + lua_pushliteral(luaState, "LuaAfbEventUnsubscribe-Fail Invalid request handle"); + lua_error(luaState); + return 1; + } + + // if no private event handle then use default binding event + if (!lua_islightuserdata(luaState, LUA_FIRST_ARG + 1)) { + lua_pushliteral(luaState, "LuaAfbEventUnsubscribe-Fail missing event handle"); + lua_error(luaState); + return 1; + } + + afbevt = (LuaAfbEvent*) lua_touserdata(luaState, LUA_FIRST_ARG + 1); + + if (!afb_event_is_valid(afbevt->event)) { + lua_pushliteral(luaState, "LuaAfbEventUnsubscribe-Fail invalid event handle"); + lua_error(luaState); + return 1; + } + + int err = afb_req_unsubscribe(source->request, afbevt->event); + if (err) { + lua_pushliteral(luaState, "LuaAfbEventUnsubscribe-Fail No Subscriber to event"); + AFB_API_ERROR(source->api, "LuaAfbEventUnsubscribe-Fail name unsubscriber event=%s count=%d", afbevt->name, afbevt->count); lua_error(luaState); return 1; } @@ -1288,6 +1318,7 @@ static const luaL_Reg afbFunction[] = { {"success", LuaAfbSuccess}, {"fail", LuaAfbFail}, {"subscribe", LuaAfbEventSubscribe}, + {"unsubscribe", LuaAfbEventUnsubscribe}, {"evtmake", LuaAfbEventMake}, {"evtpush", LuaAfbEventPush}, {"getapiname", LuaAfbGetApiName}, -- cgit 1.2.3-korg