2 * Copyright (C) 2015-2019 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 #include <json-c/json.h>
27 #include <afb/afb-event-x2-itf.h>
28 #include <afb/afb-event-x1.h>
39 * Structure for event listeners
41 struct afb_evt_listener {
43 /* chaining listeners */
44 struct afb_evt_listener *next;
46 /* interface for callbacks */
47 const struct afb_evt_itf *itf;
49 /* closure for the callback */
52 /* head of the list of events listened */
53 struct afb_evt_watch *watchs;
55 /* rwlock of the listener */
56 pthread_rwlock_t rwlock;
58 /* count of reference to the listener */
63 * Structure for describing events
68 struct afb_event_x2 eventid;
71 struct afb_evtid *next;
73 /* head of the list of listeners watching the event */
74 struct afb_evt_watch *watchs;
76 /* rwlock of the event */
77 pthread_rwlock_t rwlock;
93 /* fullname of the event */
98 * Structure for associating events and listeners
100 struct afb_evt_watch {
103 struct afb_evtid *evtid;
105 /* link to the next watcher for the same evtid */
106 struct afb_evt_watch *next_by_evtid;
109 struct afb_evt_listener *listener;
111 /* link to the next watcher for the same listener */
112 struct afb_evt_watch *next_by_listener;
119 * structure for job of broadcasting events
123 /** object atached to the event */
124 struct json_object *object;
126 /** the uuid of the event */
132 /** name of the event to broadcast */
137 * structure for job of broadcasting or pushing events
141 /** the event to broadcast */
142 struct afb_evtid *evtid;
144 /** object atached to the event */
145 struct json_object *object;
148 /* the interface for events */
149 static struct afb_event_x2_itf afb_evt_event_x2_itf = {
150 .broadcast = (void*)afb_evt_evtid_broadcast,
151 .push = (void*)afb_evt_evtid_push,
152 .unref = (void*)afb_evt_evtid_unref,
153 .name = (void*)afb_evt_evtid_name,
154 .addref = (void*)afb_evt_evtid_addref
158 /* the interface for events */
159 static struct afb_event_x2_itf afb_evt_hooked_event_x2_itf = {
160 .broadcast = (void*)afb_evt_evtid_hooked_broadcast,
161 .push = (void*)afb_evt_evtid_hooked_push,
162 .unref = (void*)afb_evt_evtid_hooked_unref,
163 .name = (void*)afb_evt_evtid_hooked_name,
164 .addref = (void*)afb_evt_evtid_hooked_addref
168 /* job groups for events push/broadcast */
169 #define BROADCAST_JOB_GROUP (&afb_evt_event_x2_itf)
170 #define PUSH_JOB_GROUP (&afb_evt_event_x2_itf)
172 /* head of the list of listeners */
173 static pthread_rwlock_t listeners_rwlock = PTHREAD_RWLOCK_INITIALIZER;
174 static struct afb_evt_listener *listeners = NULL;
176 /* handling id of events */
177 static pthread_rwlock_t events_rwlock = PTHREAD_RWLOCK_INITIALIZER;
178 static struct afb_evtid *evtids = NULL;
179 static uint16_t event_genid = 0;
180 static uint16_t event_count = 0;
182 /* head of uniqueness of events */
183 #if !defined(EVENT_BROADCAST_HOP_MAX)
184 # define EVENT_BROADCAST_HOP_MAX 10
186 #if !defined(EVENT_BROADCAST_MEMORY_COUNT)
187 # define EVENT_BROADCAST_MEMORY_COUNT 8
190 #if EVENT_BROADCAST_MEMORY_COUNT
192 pthread_mutex_t mutex;
195 uuid_binary_t uuids[EVENT_BROADCAST_MEMORY_COUNT];
197 .mutex = PTHREAD_MUTEX_INITIALIZER,
204 * Create structure for job of broadcasting string 'event' with 'object'
205 * Returns the created structure or NULL if out of memory
207 static struct job_broadcast *make_job_broadcast(const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
209 size_t sz = 1 + strlen(event);
210 struct job_broadcast *jb = malloc(sz + sizeof *jb);
213 memcpy(jb->uuid, uuid, sizeof jb->uuid);
215 memcpy(jb->event, event, sz);
221 * Destroy structure 'jb' for job of broadcasting string events
223 static void destroy_job_broadcast(struct job_broadcast *jb)
225 json_object_put(jb->object);
230 * Create structure for job of broadcasting or pushing 'evtid' with 'object'
231 * Returns the created structure or NULL if out of memory
233 static struct job_evtid *make_job_evtid(struct afb_evtid *evtid, struct json_object *object)
235 struct job_evtid *je = malloc(sizeof *je);
237 je->evtid = afb_evt_evtid_addref(evtid);
244 * Destroy structure for job of broadcasting or pushing evtid
246 static void destroy_job_evtid(struct job_evtid *je)
248 afb_evt_evtid_unref(je->evtid);
249 json_object_put(je->object);
254 * Broadcasts the 'event' of 'id' with its 'object'
256 static void broadcast(struct job_broadcast *jb)
258 struct afb_evt_listener *listener;
260 pthread_rwlock_rdlock(&listeners_rwlock);
261 listener = listeners;
263 if (listener->itf->broadcast != NULL)
264 listener->itf->broadcast(listener->closure, jb->event, json_object_get(jb->object), jb->uuid, jb->hop);
265 listener = listener->next;
267 pthread_rwlock_unlock(&listeners_rwlock);
271 * Jobs callback for broadcasting string asynchronously
273 static void broadcast_job(int signum, void *closure)
275 struct job_broadcast *jb = closure;
279 destroy_job_broadcast(jb);
283 * Broadcasts the string 'event' with its 'object'
285 static int unhooked_broadcast(const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
287 uuid_binary_t local_uuid;
288 struct job_broadcast *jb;
290 #if EVENT_BROADCAST_MEMORY_COUNT
294 /* check if lately sent */
296 uuid_new_binary(local_uuid);
298 hop = EVENT_BROADCAST_HOP_MAX;
299 #if EVENT_BROADCAST_MEMORY_COUNT
300 pthread_mutex_lock(&uniqueness.mutex);
302 pthread_mutex_lock(&uniqueness.mutex);
303 iter = (int)uniqueness.base;
304 count = (int)uniqueness.count;
306 if (0 == memcmp(uuid, uniqueness.uuids[iter], sizeof(uuid_binary_t))) {
307 pthread_mutex_unlock(&uniqueness.mutex);
310 if (++iter == EVENT_BROADCAST_MEMORY_COUNT)
315 iter = (int)uniqueness.base;
316 if (uniqueness.count < EVENT_BROADCAST_MEMORY_COUNT)
317 iter += (int)(uniqueness.count++);
318 else if (++uniqueness.base == EVENT_BROADCAST_MEMORY_COUNT)
320 memcpy(uniqueness.uuids[iter], uuid, sizeof(uuid_binary_t));
321 pthread_mutex_unlock(&uniqueness.mutex);
326 /* create the structure for the job */
327 jb = make_job_broadcast(event, object, uuid, hop);
329 ERROR("Cant't create broadcast string job item for %s(%s)",
330 event, json_object_to_json_string(object));
331 json_object_put(object);
336 rc = jobs_queue(BROADCAST_JOB_GROUP, 0, broadcast_job, jb);
338 ERROR("cant't queue broadcast string job item for %s(%s)",
339 event, json_object_to_json_string(object));
340 destroy_job_broadcast(jb);
346 * Broadcasts the event 'evtid' with its 'object'
347 * 'object' is released (like json_object_put)
348 * Returns the count of listener that received the event.
350 int afb_evt_evtid_broadcast(struct afb_evtid *evtid, struct json_object *object)
352 return unhooked_broadcast(evtid->fullname, object, NULL, 0);
357 * Broadcasts the event 'evtid' with its 'object'
358 * 'object' is released (like json_object_put)
359 * Returns the count of listener that received the event.
361 int afb_evt_evtid_hooked_broadcast(struct afb_evtid *evtid, struct json_object *object)
365 json_object_get(object);
367 if (evtid->hookflags & afb_hook_flag_evt_broadcast_before)
368 afb_hook_evt_broadcast_before(evtid->fullname, evtid->id, object);
370 result = afb_evt_evtid_broadcast(evtid, object);
372 if (evtid->hookflags & afb_hook_flag_evt_broadcast_after)
373 afb_hook_evt_broadcast_after(evtid->fullname, evtid->id, object, result);
375 json_object_put(object);
381 int afb_evt_rebroadcast(const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
386 json_object_get(object);
387 afb_hook_evt_broadcast_before(event, 0, object);
390 result = unhooked_broadcast(event, object, uuid, hop);
393 afb_hook_evt_broadcast_after(event, 0, object, result);
394 json_object_put(object);
400 * Broadcasts the 'event' with its 'object'
401 * 'object' is released (like json_object_put)
402 * Returns the count of listener having receive the event.
404 int afb_evt_broadcast(const char *event, struct json_object *object)
406 return afb_evt_rebroadcast(event, object, NULL, 0);
410 * Pushes the event 'evtid' with 'obj' to its listeners
411 * Returns the count of listener that received the event.
413 static void push_evtid(struct afb_evtid *evtid, struct json_object *object)
416 struct afb_evt_watch *watch;
417 struct afb_evt_listener *listener;
420 pthread_rwlock_rdlock(&evtid->rwlock);
421 watch = evtid->watchs;
423 listener = watch->listener;
424 assert(listener->itf->push != NULL);
425 if (watch->activity != 0) {
426 listener->itf->push(listener->closure, evtid->fullname, evtid->id, json_object_get(object));
429 watch = watch->next_by_evtid;
431 evtid->has_client = has_client;
432 pthread_rwlock_unlock(&evtid->rwlock);
436 * Jobs callback for pushing evtid asynchronously
438 static void push_job_evtid(int signum, void *closure)
440 struct job_evtid *je = closure;
443 push_evtid(je->evtid, je->object);
444 destroy_job_evtid(je);
448 * Pushes the event 'evtid' with 'obj' to its listeners
449 * 'obj' is released (like json_object_put)
450 * Returns 1 if at least one listener exists or 0 if no listener exists or
451 * -1 in case of error and the event can't be delivered
453 int afb_evt_evtid_push(struct afb_evtid *evtid, struct json_object *object)
455 struct job_evtid *je;
458 je = make_job_evtid(evtid, object);
460 ERROR("Cant't create push evtid job item for %s(%s)",
461 evtid->fullname, json_object_to_json_string(object));
462 json_object_put(object);
466 rc = jobs_queue(PUSH_JOB_GROUP, 0, push_job_evtid, je);
468 rc = evtid->has_client;
470 ERROR("cant't queue push evtid job item for %s(%s)",
471 evtid->fullname, json_object_to_json_string(object));
472 destroy_job_evtid(je);
480 * Pushes the event 'evtid' with 'obj' to its listeners
481 * 'obj' is released (like json_object_put)
482 * Emits calls to hooks.
483 * Returns the count of listener taht received the event.
485 int afb_evt_evtid_hooked_push(struct afb_evtid *evtid, struct json_object *obj)
490 /* lease the object */
491 json_object_get(obj);
493 /* hook before push */
494 if (evtid->hookflags & afb_hook_flag_evt_push_before)
495 afb_hook_evt_push_before(evtid->fullname, evtid->id, obj);
498 result = afb_evt_evtid_push(evtid, obj);
500 /* hook after push */
501 if (evtid->hookflags & afb_hook_flag_evt_push_after)
502 afb_hook_evt_push_after(evtid->fullname, evtid->id, obj, result);
504 /* release the object */
505 json_object_put(obj);
513 static void remove_watch(struct afb_evt_watch *watch)
515 struct afb_evt_watch **prv;
516 struct afb_evtid *evtid;
517 struct afb_evt_listener *listener;
519 /* notify listener if needed */
520 evtid = watch->evtid;
521 listener = watch->listener;
522 if (watch->activity != 0 && listener->itf->remove != NULL)
523 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
525 /* unlink the watch for its event */
526 prv = &evtid->watchs;
528 prv = &(*prv)->next_by_evtid;
529 *prv = watch->next_by_evtid;
531 /* unlink the watch for its listener */
532 prv = &listener->watchs;
534 prv = &(*prv)->next_by_listener;
535 *prv = watch->next_by_listener;
542 * Creates an event of name 'fullname' and returns it or NULL on error.
544 struct afb_evtid *afb_evt_evtid_create(const char *fullname)
547 struct afb_evtid *evtid, *oevt;
550 /* allocates the event */
551 len = strlen(fullname);
552 evtid = malloc(len + 1 + sizeof * evtid);
556 /* allocates the id */
557 pthread_rwlock_wrlock(&events_rwlock);
558 if (event_count == UINT16_MAX) {
559 pthread_rwlock_unlock(&events_rwlock);
561 ERROR("Can't create more events");
566 /* TODO add a guard (counting number of event created) */
569 id = event_genid = 1;
571 while(oevt != NULL && oevt->id != id)
573 } while (oevt != NULL);
575 /* initialize the event */
576 memcpy(evtid->fullname, fullname, len + 1);
577 evtid->next = evtids;
579 evtid->watchs = NULL;
581 evtid->has_client = 0;
582 pthread_rwlock_init(&evtid->rwlock, NULL);
585 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
586 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_event_x2_itf : &afb_evt_event_x2_itf;
587 if (evtid->hookflags & afb_hook_flag_evt_create)
588 afb_hook_evt_create(evtid->fullname, evtid->id);
590 evtid->eventid.itf = &afb_evt_event_x2_itf;
592 pthread_rwlock_unlock(&events_rwlock);
594 /* returns the event */
601 * Creates an event of name 'prefix'/'name' and returns it or NULL on error.
603 struct afb_evtid *afb_evt_evtid_create2(const char *prefix, const char *name)
605 size_t prelen, postlen;
608 /* makes the event fullname */
609 prelen = strlen(prefix);
610 postlen = strlen(name);
611 fullname = alloca(prelen + postlen + 2);
612 memcpy(fullname, prefix, prelen);
613 fullname[prelen] = '/';
614 memcpy(fullname + prelen + 1, name, postlen + 1);
616 /* create the event */
617 return afb_evt_evtid_create(fullname);
621 * increment the reference count of the event 'evtid'
623 struct afb_evtid *afb_evt_evtid_addref(struct afb_evtid *evtid)
625 __atomic_add_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED);
631 * increment the reference count of the event 'evtid'
633 struct afb_evtid *afb_evt_evtid_hooked_addref(struct afb_evtid *evtid)
635 if (evtid->hookflags & afb_hook_flag_evt_addref)
636 afb_hook_evt_addref(evtid->fullname, evtid->id);
637 return afb_evt_evtid_addref(evtid);
642 * decrement the reference count of the event 'evtid'
643 * and destroy it when the count reachs zero
645 void afb_evt_evtid_unref(struct afb_evtid *evtid)
648 struct afb_evtid **prv;
649 struct afb_evt_listener *listener;
651 if (!__atomic_sub_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED)) {
652 /* unlinks the event if valid! */
653 pthread_rwlock_wrlock(&events_rwlock);
656 while (*prv && !(found = (*prv == evtid)))
662 pthread_rwlock_unlock(&events_rwlock);
664 /* destroys the event */
666 ERROR("event not found");
668 /* removes all watchers */
669 while(evtid->watchs != NULL) {
670 listener = evtid->watchs->listener;
671 pthread_rwlock_wrlock(&listener->rwlock);
672 pthread_rwlock_wrlock(&evtid->rwlock);
673 remove_watch(evtid->watchs);
674 pthread_rwlock_unlock(&evtid->rwlock);
675 pthread_rwlock_unlock(&listener->rwlock);
679 pthread_rwlock_destroy(&evtid->rwlock);
687 * decrement the reference count of the event 'evtid'
688 * and destroy it when the count reachs zero
690 void afb_evt_evtid_hooked_unref(struct afb_evtid *evtid)
692 if (evtid->hookflags & afb_hook_flag_evt_unref)
693 afb_hook_evt_unref(evtid->fullname, evtid->id);
694 afb_evt_evtid_unref(evtid);
699 * Returns the true name of the 'event'
701 const char *afb_evt_evtid_fullname(struct afb_evtid *evtid)
703 return evtid->fullname;
707 * Returns the name of the 'event'
709 const char *afb_evt_evtid_name(struct afb_evtid *evtid)
711 const char *name = strchr(evtid->fullname, '/');
712 return name ? name + 1 : evtid->fullname;
717 * Returns the name associated to the event 'evtid'.
719 const char *afb_evt_evtid_hooked_name(struct afb_evtid *evtid)
721 const char *result = afb_evt_evtid_name(evtid);
722 if (evtid->hookflags & afb_hook_flag_evt_name)
723 afb_hook_evt_name(evtid->fullname, evtid->id, result);
729 * Returns the id of the 'event'
731 uint16_t afb_evt_evtid_id(struct afb_evtid *evtid)
737 * Returns an instance of the listener defined by the 'send' callback
739 * Returns NULL in case of memory depletion.
741 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
743 struct afb_evt_listener *listener;
745 /* search if an instance already exists */
746 pthread_rwlock_wrlock(&listeners_rwlock);
747 listener = listeners;
748 while (listener != NULL) {
749 if (listener->itf == itf && listener->closure == closure) {
750 listener = afb_evt_listener_addref(listener);
753 listener = listener->next;
757 listener = calloc(1, sizeof *listener);
758 if (listener != NULL) {
761 listener->closure = closure;
762 listener->watchs = NULL;
763 listener->refcount = 1;
764 pthread_rwlock_init(&listener->rwlock, NULL);
765 listener->next = listeners;
766 listeners = listener;
769 pthread_rwlock_unlock(&listeners_rwlock);
774 * Increases the reference count of 'listener' and returns it
776 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
778 __atomic_add_fetch(&listener->refcount, 1, __ATOMIC_RELAXED);
783 * Decreases the reference count of the 'listener' and destroys it
786 void afb_evt_listener_unref(struct afb_evt_listener *listener)
788 struct afb_evt_listener **prv;
789 struct afb_evtid *evtid;
791 if (listener && !__atomic_sub_fetch(&listener->refcount, 1, __ATOMIC_RELAXED)) {
793 /* unlink the listener */
794 pthread_rwlock_wrlock(&listeners_rwlock);
796 while (*prv != listener)
798 *prv = listener->next;
799 pthread_rwlock_unlock(&listeners_rwlock);
801 /* remove the watchers */
802 pthread_rwlock_wrlock(&listener->rwlock);
803 while (listener->watchs != NULL) {
804 evtid = listener->watchs->evtid;
805 pthread_rwlock_wrlock(&evtid->rwlock);
806 remove_watch(listener->watchs);
807 pthread_rwlock_unlock(&evtid->rwlock);
809 pthread_rwlock_unlock(&listener->rwlock);
811 /* free the listener */
812 pthread_rwlock_destroy(&listener->rwlock);
818 * Makes the 'listener' watching 'evtid'
819 * Returns 0 in case of success or else -1.
821 int afb_evt_watch_add_evtid(struct afb_evt_listener *listener, struct afb_evtid *evtid)
823 struct afb_evt_watch *watch;
825 /* check parameter */
826 if (listener->itf->push == NULL) {
831 /* search the existing watch for the listener */
832 pthread_rwlock_wrlock(&listener->rwlock);
833 watch = listener->watchs;
834 while(watch != NULL) {
835 if (watch->evtid == evtid)
837 watch = watch->next_by_listener;
840 /* not found, allocate a new */
841 watch = malloc(sizeof *watch);
843 pthread_rwlock_unlock(&listener->rwlock);
848 /* initialise and link */
849 watch->evtid = evtid;
851 watch->listener = listener;
852 watch->next_by_listener = listener->watchs;
853 listener->watchs = watch;
854 pthread_rwlock_wrlock(&evtid->rwlock);
855 watch->next_by_evtid = evtid->watchs;
856 evtid->watchs = watch;
857 pthread_rwlock_unlock(&evtid->rwlock);
860 if (watch->activity == 0 && listener->itf->add != NULL)
861 listener->itf->add(listener->closure, evtid->fullname, evtid->id);
863 evtid->has_client = 1;
864 pthread_rwlock_unlock(&listener->rwlock);
870 * Avoids the 'listener' to watch 'evtid'
871 * Returns 0 in case of success or else -1.
873 int afb_evt_watch_sub_evtid(struct afb_evt_listener *listener, struct afb_evtid *evtid)
875 struct afb_evt_watch *watch;
877 /* search the existing watch */
878 pthread_rwlock_wrlock(&listener->rwlock);
879 watch = listener->watchs;
880 while(watch != NULL) {
881 if (watch->evtid == evtid) {
882 if (watch->activity != 0) {
884 if (watch->activity == 0 && listener->itf->remove != NULL)
885 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
887 pthread_rwlock_unlock(&listener->rwlock);
890 watch = watch->next_by_listener;
892 pthread_rwlock_unlock(&listener->rwlock);
898 * Avoids the 'listener' to watch 'eventid'
899 * Returns 0 in case of success or else -1.
901 int afb_evt_watch_sub_eventid(struct afb_evt_listener *listener, uint16_t eventid)
903 struct afb_evt_watch *watch;
904 struct afb_evtid *evtid;
906 /* search the existing watch */
907 pthread_rwlock_wrlock(&listener->rwlock);
908 watch = listener->watchs;
909 while(watch != NULL) {
910 evtid = watch->evtid;
911 if (evtid->id == eventid) {
912 if (watch->activity != 0) {
914 if (watch->activity == 0 && listener->itf->remove != NULL)
915 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
917 pthread_rwlock_unlock(&listener->rwlock);
920 watch = watch->next_by_listener;
922 pthread_rwlock_unlock(&listener->rwlock);
929 * update the hooks for events
931 void afb_evt_update_hooks()
933 struct afb_evtid *evtid;
935 pthread_rwlock_rdlock(&events_rwlock);
936 for (evtid = evtids ; evtid ; evtid = evtid->next) {
937 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
938 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_event_x2_itf : &afb_evt_event_x2_itf;
940 pthread_rwlock_unlock(&events_rwlock);
944 inline struct afb_evtid *afb_evt_event_x2_to_evtid(struct afb_event_x2 *eventid)
946 return (struct afb_evtid*)eventid;
949 inline struct afb_event_x2 *afb_evt_event_x2_from_evtid(struct afb_evtid *evtid)
951 return &evtid->eventid;
955 * Creates an event of 'fullname' and returns it.
956 * Returns an event with closure==NULL in case of error.
958 struct afb_event_x2 *afb_evt_event_x2_create(const char *fullname)
960 return afb_evt_event_x2_from_evtid(afb_evt_evtid_create(fullname));
964 * Creates an event of name 'prefix'/'name' and returns it.
965 * Returns an event with closure==NULL in case of error.
967 struct afb_event_x2 *afb_evt_event_x2_create2(const char *prefix, const char *name)
969 return afb_evt_event_x2_from_evtid(afb_evt_evtid_create2(prefix, name));
973 * Returns the fullname of the 'eventid'
975 const char *afb_evt_event_x2_fullname(struct afb_event_x2 *eventid)
977 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
978 return evtid ? evtid->fullname : NULL;
982 * Returns the id of the 'eventid'
984 uint16_t afb_evt_event_x2_id(struct afb_event_x2 *eventid)
986 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
987 return evtid ? evtid->id : 0;
991 * Makes the 'listener' watching 'eventid'
992 * Returns 0 in case of success or else -1.
994 int afb_evt_event_x2_add_watch(struct afb_evt_listener *listener, struct afb_event_x2 *eventid)
996 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
998 /* check parameter */
1004 /* search the existing watch for the listener */
1005 return afb_evt_watch_add_evtid(listener, evtid);
1009 * Avoids the 'listener' to watch 'eventid'
1010 * Returns 0 in case of success or else -1.
1012 int afb_evt_event_x2_remove_watch(struct afb_evt_listener *listener, struct afb_event_x2 *eventid)
1014 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1016 /* check parameter */
1022 /* search the existing watch */
1023 return afb_evt_watch_sub_evtid(listener, evtid);
1026 int afb_evt_event_x2_push(struct afb_event_x2 *eventid, struct json_object *object)
1029 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1031 return afb_evt_evtid_hooked_push(evtid, object);
1032 json_object_put(object);
1036 __attribute__((alias("afb_evt_event_x2_unhooked_push")));
1039 int afb_evt_event_x2_unhooked_push(struct afb_event_x2 *eventid, struct json_object *object)
1041 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1043 return afb_evt_evtid_push(evtid, object);
1044 json_object_put(object);
1048 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
1049 struct afb_event_x1 afb_evt_event_from_evtid(struct afb_evtid *evtid)
1053 ? (struct afb_event_x1){ .itf = &afb_evt_hooked_event_x2_itf, .closure = &evtid->eventid }
1055 ? (struct afb_event_x1){ .itf = &afb_evt_event_x2_itf, .closure = &evtid->eventid }
1057 : (struct afb_event_x1){ .itf = NULL, .closure = NULL };
1061 void afb_evt_event_x2_unref(struct afb_event_x2 *eventid)
1063 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1065 afb_evt_evtid_unref(evtid);
1068 struct afb_event_x2 *afb_evt_event_x2_addref(struct afb_event_x2 *eventid)
1070 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1072 afb_evt_evtid_addref(evtid);