diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-06-01 12:34:29 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-06-01 12:34:29 +0200 |
commit | b67e18b39830a01750721787bf3bdc5d71983144 (patch) | |
tree | 20104692fd55fcb27b3a20aae0b144bb64286150 /bindings/samples/HelloWorld.c | |
parent | 4dc768d67031aa99e2b885a0df7e643fdd1fa80c (diff) |
Add hooking for events
Change-Id: If5fe736e04c9f4298302c3cbba568f1d6346ee67
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'bindings/samples/HelloWorld.c')
-rw-r--r-- | bindings/samples/HelloWorld.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/bindings/samples/HelloWorld.c b/bindings/samples/HelloWorld.c index e2f38ee4..0f58a43b 100644 --- a/bindings/samples/HelloWorld.c +++ b/bindings/samples/HelloWorld.c @@ -109,6 +109,13 @@ static int event_push(struct json_object *args, const char *tag) return e ? afb_event_push(e->event, json_object_get(args)) : -1; } +static int event_broadcast(struct json_object *args, const char *tag) +{ + struct event *e; + e = event_get(tag); + return e ? afb_event_broadcast(e->event, json_object_get(args)) : -1; +} + // Sample Generic Ping Debug API static void ping(afb_req request, json_object *jresp, const char *tag) { @@ -359,6 +366,31 @@ static void exitnow (afb_req request) exit(code); } +static void broadcast(afb_req request) +{ + const char *tag = afb_req_value(request, "tag"); + const char *name = afb_req_value(request, "name"); + const char *data = afb_req_value(request, "data"); + json_object *object = data ? json_tokener_parse(data) : NULL; + + if (tag != NULL) { + pthread_mutex_lock(&mutex); + if (0 > event_broadcast(object, tag)) + afb_req_fail(request, "failed", "broadcast error"); + else + afb_req_success(request, NULL, NULL); + pthread_mutex_unlock(&mutex); + } else if (name != NULL) { + if (0 > afb_daemon_broadcast_event(name, object)) + afb_req_fail(request, "failed", "broadcast error"); + else + afb_req_success(request, NULL, NULL); + } else { + afb_req_fail(request, "failed", "bad arguments"); + } + json_object_put(object); +} + static int preinit() { NOTICE("hello binding comes to live"); @@ -395,6 +427,7 @@ static const afb_verb_v2 verbs[]= { { "call", call , NULL, AFB_SESSION_NONE }, { "callsync", callsync , NULL, AFB_SESSION_NONE }, { "verbose", verbose , NULL, AFB_SESSION_NONE }, + { "broadcast", broadcast , NULL, AFB_SESSION_NONE }, { "exit", exitnow , NULL, AFB_SESSION_NONE }, { NULL} }; |