diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2018-12-18 10:53:19 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-12-18 10:53:19 +0100 |
commit | 869b777af8fab6ba416a9687932e7b26f7d78aa4 (patch) | |
tree | 7301b312870f4d792d6f5b37f0a05019fd9bc570 /ctl-lib/ctl-lua.c | |
parent | 444e62f0c4ecae594da71dbdd51305148c433fb5 (diff) |
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 <romain.forlot@iot.bzh>
Diffstat (limited to 'ctl-lib/ctl-lua.c')
-rw-r--r-- | ctl-lib/ctl-lua.c | 31 |
1 files changed, 31 insertions, 0 deletions
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}, |