2 * Copyright (C) 2015-2020 "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;
90 /* fullname of the event */
95 * Structure for associating events and listeners
97 struct afb_evt_watch {
100 struct afb_evtid *evtid;
102 /* link to the next watcher for the same evtid */
103 struct afb_evt_watch *next_by_evtid;
106 struct afb_evt_listener *listener;
108 /* link to the next watcher for the same listener */
109 struct afb_evt_watch *next_by_listener;
113 * structure for job of broadcasting events
117 /** object atached to the event */
118 struct json_object *object;
120 /** the uuid of the event */
126 /** name of the event to broadcast */
131 * structure for job of broadcasting or pushing events
135 /** the event to broadcast */
136 struct afb_evtid *evtid;
138 /** object atached to the event */
139 struct json_object *object;
142 /* the interface for events */
143 static struct afb_event_x2_itf afb_evt_event_x2_itf = {
144 .broadcast = (void*)afb_evt_evtid_broadcast,
145 .push = (void*)afb_evt_evtid_push,
146 .unref = (void*)afb_evt_evtid_unref,
147 .name = (void*)afb_evt_evtid_name,
148 .addref = (void*)afb_evt_evtid_addref
152 /* the interface for events */
153 static struct afb_event_x2_itf afb_evt_hooked_event_x2_itf = {
154 .broadcast = (void*)afb_evt_evtid_hooked_broadcast,
155 .push = (void*)afb_evt_evtid_hooked_push,
156 .unref = (void*)afb_evt_evtid_hooked_unref,
157 .name = (void*)afb_evt_evtid_hooked_name,
158 .addref = (void*)afb_evt_evtid_hooked_addref
162 /* job groups for events push/broadcast */
163 #define BROADCAST_JOB_GROUP (&afb_evt_event_x2_itf)
164 #define PUSH_JOB_GROUP (&afb_evt_event_x2_itf)
166 /* head of the list of listeners */
167 static pthread_rwlock_t listeners_rwlock = PTHREAD_RWLOCK_INITIALIZER;
168 static struct afb_evt_listener *listeners = NULL;
170 /* handling id of events */
171 static pthread_rwlock_t events_rwlock = PTHREAD_RWLOCK_INITIALIZER;
172 static struct afb_evtid *evtids = NULL;
173 static uint16_t event_genid = 0;
174 static uint16_t event_count = 0;
176 /* head of uniqueness of events */
177 #if !defined(EVENT_BROADCAST_HOP_MAX)
178 # define EVENT_BROADCAST_HOP_MAX 10
180 #if !defined(EVENT_BROADCAST_MEMORY_COUNT)
181 # define EVENT_BROADCAST_MEMORY_COUNT 8
184 #if EVENT_BROADCAST_MEMORY_COUNT
186 pthread_mutex_t mutex;
189 uuid_binary_t uuids[EVENT_BROADCAST_MEMORY_COUNT];
191 .mutex = PTHREAD_MUTEX_INITIALIZER,
198 * Create structure for job of broadcasting string 'event' with 'object'
199 * Returns the created structure or NULL if out of memory
201 static struct job_broadcast *make_job_broadcast(const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
203 size_t sz = 1 + strlen(event);
204 struct job_broadcast *jb = malloc(sz + sizeof *jb);
207 memcpy(jb->uuid, uuid, sizeof jb->uuid);
209 memcpy(jb->event, event, sz);
215 * Destroy structure 'jb' for job of broadcasting string events
217 static void destroy_job_broadcast(struct job_broadcast *jb)
219 json_object_put(jb->object);
224 * Create structure for job of broadcasting or pushing 'evtid' with 'object'
225 * Returns the created structure or NULL if out of memory
227 static struct job_evtid *make_job_evtid(struct afb_evtid *evtid, struct json_object *object)
229 struct job_evtid *je = malloc(sizeof *je);
231 je->evtid = afb_evt_evtid_addref(evtid);
238 * Destroy structure for job of broadcasting or pushing evtid
240 static void destroy_job_evtid(struct job_evtid *je)
242 afb_evt_evtid_unref(je->evtid);
243 json_object_put(je->object);
248 * Broadcasts the 'event' of 'id' with its 'object'
250 static void broadcast(struct job_broadcast *jb)
252 struct afb_evt_listener *listener;
254 pthread_rwlock_rdlock(&listeners_rwlock);
255 listener = listeners;
257 if (listener->itf->broadcast != NULL)
258 listener->itf->broadcast(listener->closure, jb->event, json_object_get(jb->object), jb->uuid, jb->hop);
259 listener = listener->next;
261 pthread_rwlock_unlock(&listeners_rwlock);
265 * Jobs callback for broadcasting string asynchronously
267 static void broadcast_job(int signum, void *closure)
269 struct job_broadcast *jb = closure;
273 destroy_job_broadcast(jb);
277 * Broadcasts the string 'event' with its 'object'
279 static int unhooked_broadcast(const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
281 uuid_binary_t local_uuid;
282 struct job_broadcast *jb;
284 #if EVENT_BROADCAST_MEMORY_COUNT
288 /* check if lately sent */
290 uuid_new_binary(local_uuid);
292 hop = EVENT_BROADCAST_HOP_MAX;
293 #if EVENT_BROADCAST_MEMORY_COUNT
294 pthread_mutex_lock(&uniqueness.mutex);
296 pthread_mutex_lock(&uniqueness.mutex);
297 iter = (int)uniqueness.base;
298 count = (int)uniqueness.count;
300 if (0 == memcmp(uuid, uniqueness.uuids[iter], sizeof(uuid_binary_t))) {
301 pthread_mutex_unlock(&uniqueness.mutex);
304 if (++iter == EVENT_BROADCAST_MEMORY_COUNT)
309 iter = (int)uniqueness.base;
310 if (uniqueness.count < EVENT_BROADCAST_MEMORY_COUNT)
311 iter += (int)(uniqueness.count++);
312 else if (++uniqueness.base == EVENT_BROADCAST_MEMORY_COUNT)
314 memcpy(uniqueness.uuids[iter], uuid, sizeof(uuid_binary_t));
315 pthread_mutex_unlock(&uniqueness.mutex);
320 /* create the structure for the job */
321 jb = make_job_broadcast(event, object, uuid, hop);
323 ERROR("Cant't create broadcast string job item for %s(%s)",
324 event, json_object_to_json_string(object));
325 json_object_put(object);
330 rc = jobs_queue(BROADCAST_JOB_GROUP, 0, broadcast_job, jb);
332 ERROR("cant't queue broadcast string job item for %s(%s)",
333 event, json_object_to_json_string(object));
334 destroy_job_broadcast(jb);
340 * Broadcasts the event 'evtid' with its 'object'
341 * 'object' is released (like json_object_put)
342 * Returns the count of listener that received the event.
344 int afb_evt_evtid_broadcast(struct afb_evtid *evtid, struct json_object *object)
346 return unhooked_broadcast(evtid->fullname, object, NULL, 0);
351 * Broadcasts the event 'evtid' with its 'object'
352 * 'object' is released (like json_object_put)
353 * Returns the count of listener that received the event.
355 int afb_evt_evtid_hooked_broadcast(struct afb_evtid *evtid, struct json_object *object)
359 json_object_get(object);
361 if (evtid->hookflags & afb_hook_flag_evt_broadcast_before)
362 afb_hook_evt_broadcast_before(evtid->fullname, evtid->id, object);
364 result = afb_evt_evtid_broadcast(evtid, object);
366 if (evtid->hookflags & afb_hook_flag_evt_broadcast_after)
367 afb_hook_evt_broadcast_after(evtid->fullname, evtid->id, object, result);
369 json_object_put(object);
375 int afb_evt_rebroadcast(const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop)
380 json_object_get(object);
381 afb_hook_evt_broadcast_before(event, 0, object);
384 result = unhooked_broadcast(event, object, uuid, hop);
387 afb_hook_evt_broadcast_after(event, 0, object, result);
388 json_object_put(object);
394 * Broadcasts the 'event' with its 'object'
395 * 'object' is released (like json_object_put)
396 * Returns the count of listener having receive the event.
398 int afb_evt_broadcast(const char *event, struct json_object *object)
400 return afb_evt_rebroadcast(event, object, NULL, 0);
404 * Pushes the event 'evtid' with 'obj' to its listeners
405 * Returns the count of listener that received the event.
407 static void push_evtid(struct afb_evtid *evtid, struct json_object *object)
409 struct afb_evt_watch *watch;
410 struct afb_evt_listener *listener;
412 pthread_rwlock_rdlock(&evtid->rwlock);
413 watch = evtid->watchs;
415 listener = watch->listener;
416 assert(listener->itf->push != NULL);
417 listener->itf->push(listener->closure, evtid->fullname, evtid->id, json_object_get(object));
418 watch = watch->next_by_evtid;
420 pthread_rwlock_unlock(&evtid->rwlock);
424 * Jobs callback for pushing evtid asynchronously
426 static void push_job_evtid(int signum, void *closure)
428 struct job_evtid *je = closure;
431 push_evtid(je->evtid, je->object);
432 destroy_job_evtid(je);
436 * Pushes the event 'evtid' with 'obj' to its listeners
437 * 'obj' is released (like json_object_put)
438 * Returns 1 if at least one listener exists or 0 if no listener exists or
439 * -1 in case of error and the event can't be delivered
441 int afb_evt_evtid_push(struct afb_evtid *evtid, struct json_object *object)
443 struct job_evtid *je;
449 je = make_job_evtid(evtid, object);
451 ERROR("Cant't create push evtid job item for %s(%s)",
452 evtid->fullname, json_object_to_json_string(object));
453 json_object_put(object);
457 rc = jobs_queue(PUSH_JOB_GROUP, 0, push_job_evtid, je);
461 ERROR("cant't queue push evtid job item for %s(%s)",
462 evtid->fullname, json_object_to_json_string(object));
463 destroy_job_evtid(je);
471 * Pushes the event 'evtid' with 'obj' to its listeners
472 * 'obj' is released (like json_object_put)
473 * Emits calls to hooks.
474 * Returns the count of listener taht received the event.
476 int afb_evt_evtid_hooked_push(struct afb_evtid *evtid, struct json_object *obj)
481 /* lease the object */
482 json_object_get(obj);
484 /* hook before push */
485 if (evtid->hookflags & afb_hook_flag_evt_push_before)
486 afb_hook_evt_push_before(evtid->fullname, evtid->id, obj);
489 result = afb_evt_evtid_push(evtid, obj);
491 /* hook after push */
492 if (evtid->hookflags & afb_hook_flag_evt_push_after)
493 afb_hook_evt_push_after(evtid->fullname, evtid->id, obj, result);
495 /* release the object */
496 json_object_put(obj);
501 static void unwatch(struct afb_evt_listener *listener, struct afb_evtid *evtid, int remove)
503 /* notify listener if needed */
504 if (remove && listener->itf->remove != NULL)
505 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
508 static void evtid_unwatch(struct afb_evtid *evtid, struct afb_evt_listener *listener, struct afb_evt_watch *watch, int remove)
510 struct afb_evt_watch **prv;
512 /* notify listener if needed */
513 unwatch(listener, evtid, remove);
515 /* unlink the watch for its event */
516 pthread_rwlock_wrlock(&listener->rwlock);
517 prv = &listener->watchs;
520 *prv = watch->next_by_listener;
523 prv = &(*prv)->next_by_listener;
525 pthread_rwlock_unlock(&listener->rwlock);
531 static void listener_unwatch(struct afb_evt_listener *listener, struct afb_evtid *evtid, struct afb_evt_watch *watch, int remove)
533 struct afb_evt_watch **prv;
535 /* notify listener if needed */
536 unwatch(listener, evtid, remove);
538 /* unlink the watch for its event */
539 pthread_rwlock_wrlock(&evtid->rwlock);
540 prv = &evtid->watchs;
543 *prv = watch->next_by_evtid;
546 prv = &(*prv)->next_by_evtid;
548 pthread_rwlock_unlock(&evtid->rwlock);
555 * Creates an event of name 'fullname' and returns it or NULL on error.
557 struct afb_evtid *afb_evt_evtid_create(const char *fullname)
560 struct afb_evtid *evtid, *oevt;
563 /* allocates the event */
564 len = strlen(fullname);
565 evtid = malloc(len + 1 + sizeof * evtid);
569 /* allocates the id */
570 pthread_rwlock_wrlock(&events_rwlock);
571 if (event_count == UINT16_MAX) {
572 pthread_rwlock_unlock(&events_rwlock);
574 ERROR("Can't create more events");
579 /* TODO add a guard (counting number of event created) */
582 id = event_genid = 1;
584 while(oevt != NULL && oevt->id != id)
586 } while (oevt != NULL);
588 /* initialize the event */
589 memcpy(evtid->fullname, fullname, len + 1);
590 evtid->next = evtids;
592 evtid->watchs = NULL;
594 pthread_rwlock_init(&evtid->rwlock, NULL);
597 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
598 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_event_x2_itf : &afb_evt_event_x2_itf;
599 if (evtid->hookflags & afb_hook_flag_evt_create)
600 afb_hook_evt_create(evtid->fullname, evtid->id);
602 evtid->eventid.itf = &afb_evt_event_x2_itf;
604 pthread_rwlock_unlock(&events_rwlock);
606 /* returns the event */
613 * Creates an event of name 'prefix'/'name' and returns it or NULL on error.
615 struct afb_evtid *afb_evt_evtid_create2(const char *prefix, const char *name)
617 size_t prelen, postlen;
620 /* makes the event fullname */
621 prelen = strlen(prefix);
622 postlen = strlen(name);
623 fullname = alloca(prelen + postlen + 2);
624 memcpy(fullname, prefix, prelen);
625 fullname[prelen] = '/';
626 memcpy(fullname + prelen + 1, name, postlen + 1);
628 /* create the event */
629 return afb_evt_evtid_create(fullname);
633 * increment the reference count of the event 'evtid'
635 struct afb_evtid *afb_evt_evtid_addref(struct afb_evtid *evtid)
637 __atomic_add_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED);
643 * increment the reference count of the event 'evtid'
645 struct afb_evtid *afb_evt_evtid_hooked_addref(struct afb_evtid *evtid)
647 if (evtid->hookflags & afb_hook_flag_evt_addref)
648 afb_hook_evt_addref(evtid->fullname, evtid->id);
649 return afb_evt_evtid_addref(evtid);
654 * decrement the reference count of the event 'evtid'
655 * and destroy it when the count reachs zero
657 void afb_evt_evtid_unref(struct afb_evtid *evtid)
659 struct afb_evtid **prv, *oev;
660 struct afb_evt_watch *watch, *nwatch;
662 if (!__atomic_sub_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED)) {
663 /* unlinks the event if valid! */
664 pthread_rwlock_wrlock(&events_rwlock);
671 ERROR("unexpected event");
672 pthread_rwlock_unlock(&events_rwlock);
679 pthread_rwlock_unlock(&events_rwlock);
681 /* removes all watchers */
682 pthread_rwlock_wrlock(&evtid->rwlock);
683 watch = evtid->watchs;
684 evtid->watchs = NULL;
685 pthread_rwlock_unlock(&evtid->rwlock);
687 nwatch = watch->next_by_evtid;
688 evtid_unwatch(evtid, watch->listener, watch, 1);
693 pthread_rwlock_destroy(&evtid->rwlock);
700 * decrement the reference count of the event 'evtid'
701 * and destroy it when the count reachs zero
703 void afb_evt_evtid_hooked_unref(struct afb_evtid *evtid)
705 if (evtid->hookflags & afb_hook_flag_evt_unref)
706 afb_hook_evt_unref(evtid->fullname, evtid->id);
707 afb_evt_evtid_unref(evtid);
712 * Returns the true name of the 'event'
714 const char *afb_evt_evtid_fullname(struct afb_evtid *evtid)
716 return evtid->fullname;
720 * Returns the name of the 'event'
722 const char *afb_evt_evtid_name(struct afb_evtid *evtid)
724 const char *name = strchr(evtid->fullname, '/');
725 return name ? name + 1 : evtid->fullname;
730 * Returns the name associated to the event 'evtid'.
732 const char *afb_evt_evtid_hooked_name(struct afb_evtid *evtid)
734 const char *result = afb_evt_evtid_name(evtid);
735 if (evtid->hookflags & afb_hook_flag_evt_name)
736 afb_hook_evt_name(evtid->fullname, evtid->id, result);
742 * Returns the id of the 'event'
744 uint16_t afb_evt_evtid_id(struct afb_evtid *evtid)
750 * Returns an instance of the listener defined by the 'send' callback
752 * Returns NULL in case of memory depletion.
754 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
756 struct afb_evt_listener *listener;
758 /* search if an instance already exists */
759 pthread_rwlock_wrlock(&listeners_rwlock);
760 listener = listeners;
761 while (listener != NULL) {
762 if (listener->itf == itf && listener->closure == closure) {
763 listener = afb_evt_listener_addref(listener);
766 listener = listener->next;
770 listener = calloc(1, sizeof *listener);
771 if (listener != NULL) {
774 listener->closure = closure;
775 listener->watchs = NULL;
776 listener->refcount = 1;
777 pthread_rwlock_init(&listener->rwlock, NULL);
778 listener->next = listeners;
779 listeners = listener;
782 pthread_rwlock_unlock(&listeners_rwlock);
787 * Increases the reference count of 'listener' and returns it
789 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
791 __atomic_add_fetch(&listener->refcount, 1, __ATOMIC_RELAXED);
796 * Decreases the reference count of the 'listener' and destroys it
799 void afb_evt_listener_unref(struct afb_evt_listener *listener)
801 struct afb_evt_listener **prv, *olis;
803 if (listener && !__atomic_sub_fetch(&listener->refcount, 1, __ATOMIC_RELAXED)) {
805 /* unlink the listener */
806 pthread_rwlock_wrlock(&listeners_rwlock);
810 if (olis == listener)
813 ERROR("unexpected listener");
814 pthread_rwlock_unlock(&listeners_rwlock);
819 *prv = listener->next;
820 pthread_rwlock_unlock(&listeners_rwlock);
822 /* remove the watchers */
823 afb_evt_listener_unwatch_all(listener, 0);
825 /* free the listener */
826 pthread_rwlock_destroy(&listener->rwlock);
832 * Makes the 'listener' watching 'evtid'
833 * Returns 0 in case of success or else -1.
835 int afb_evt_listener_watch_evt(struct afb_evt_listener *listener, struct afb_evtid *evtid)
837 struct afb_evt_watch *watch;
839 /* check parameter */
840 if (listener->itf->push == NULL) {
845 /* search the existing watch for the listener */
846 pthread_rwlock_wrlock(&listener->rwlock);
847 watch = listener->watchs;
848 while(watch != NULL) {
849 if (watch->evtid == evtid)
851 watch = watch->next_by_listener;
854 /* not found, allocate a new */
855 watch = malloc(sizeof *watch);
857 pthread_rwlock_unlock(&listener->rwlock);
862 /* initialise and link */
863 watch->evtid = evtid;
864 watch->listener = listener;
865 watch->next_by_listener = listener->watchs;
866 listener->watchs = watch;
867 pthread_rwlock_wrlock(&evtid->rwlock);
868 watch->next_by_evtid = evtid->watchs;
869 evtid->watchs = watch;
870 pthread_rwlock_unlock(&evtid->rwlock);
872 if (listener->itf->add != NULL)
873 listener->itf->add(listener->closure, evtid->fullname, evtid->id);
875 pthread_rwlock_unlock(&listener->rwlock);
880 * Avoids the 'listener' to watch 'evtid'
881 * Returns 0 in case of success or else -1.
883 int afb_evt_listener_unwatch_evt(struct afb_evt_listener *listener, struct afb_evtid *evtid)
885 struct afb_evt_watch *watch, **pwatch;
887 /* search the existing watch */
888 pthread_rwlock_wrlock(&listener->rwlock);
889 pwatch = &listener->watchs;
893 pthread_rwlock_unlock(&listener->rwlock);
897 if (evtid == watch->evtid) {
898 *pwatch = watch->next_by_listener;
899 pthread_rwlock_unlock(&listener->rwlock);
900 listener_unwatch(listener, evtid, watch, 1);
903 pwatch = &watch->next_by_listener;
908 * Avoids the 'listener' to watch 'eventid'
909 * Returns 0 in case of success or else -1.
911 int afb_evt_listener_unwatch_id(struct afb_evt_listener *listener, uint16_t eventid)
913 struct afb_evt_watch *watch, **pwatch;
914 struct afb_evtid *evtid;
916 /* search the existing watch */
917 pthread_rwlock_wrlock(&listener->rwlock);
918 pwatch = &listener->watchs;
922 pthread_rwlock_unlock(&listener->rwlock);
926 evtid = watch->evtid;
927 if (evtid->id == eventid) {
928 *pwatch = watch->next_by_listener;
929 pthread_rwlock_unlock(&listener->rwlock);
930 listener_unwatch(listener, evtid, watch, 1);
933 pwatch = &watch->next_by_listener;
938 * Avoids the 'listener' to watch any event, calling the callback
939 * 'remove' of the interface if 'remoe' is not zero.
941 void afb_evt_listener_unwatch_all(struct afb_evt_listener *listener, int remove)
943 struct afb_evt_watch *watch, *nwatch;
945 /* search the existing watch */
946 pthread_rwlock_wrlock(&listener->rwlock);
947 watch = listener->watchs;
948 listener->watchs = NULL;
949 pthread_rwlock_unlock(&listener->rwlock);
951 nwatch = watch->next_by_listener;
952 listener_unwatch(listener, watch->evtid, watch, remove);
959 * update the hooks for events
961 void afb_evt_update_hooks()
963 struct afb_evtid *evtid;
965 pthread_rwlock_rdlock(&events_rwlock);
966 for (evtid = evtids ; evtid ; evtid = evtid->next) {
967 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
968 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_event_x2_itf : &afb_evt_event_x2_itf;
970 pthread_rwlock_unlock(&events_rwlock);
974 inline struct afb_evtid *afb_evt_event_x2_to_evtid(struct afb_event_x2 *eventid)
976 return (struct afb_evtid*)eventid;
979 inline struct afb_event_x2 *afb_evt_event_x2_from_evtid(struct afb_evtid *evtid)
981 return &evtid->eventid;
985 * Creates an event of 'fullname' and returns it.
986 * Returns an event with closure==NULL in case of error.
988 struct afb_event_x2 *afb_evt_event_x2_create(const char *fullname)
990 return afb_evt_event_x2_from_evtid(afb_evt_evtid_create(fullname));
994 * Creates an event of name 'prefix'/'name' and returns it.
995 * Returns an event with closure==NULL in case of error.
997 struct afb_event_x2 *afb_evt_event_x2_create2(const char *prefix, const char *name)
999 return afb_evt_event_x2_from_evtid(afb_evt_evtid_create2(prefix, name));
1003 * Returns the fullname of the 'eventid'
1005 const char *afb_evt_event_x2_fullname(struct afb_event_x2 *eventid)
1007 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1008 return evtid ? evtid->fullname : NULL;
1012 * Returns the id of the 'eventid'
1014 uint16_t afb_evt_event_x2_id(struct afb_event_x2 *eventid)
1016 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1017 return evtid ? evtid->id : 0;
1021 * Makes the 'listener' watching 'eventid'
1022 * Returns 0 in case of success or else -1.
1024 int afb_evt_listener_watch_x2(struct afb_evt_listener *listener, struct afb_event_x2 *eventid)
1026 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1028 /* check parameter */
1034 /* search the existing watch for the listener */
1035 return afb_evt_listener_watch_evt(listener, evtid);
1039 * Avoids the 'listener' to watch 'eventid'
1040 * Returns 0 in case of success or else -1.
1042 int afb_evt_listener_unwatch_x2(struct afb_evt_listener *listener, struct afb_event_x2 *eventid)
1044 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1046 /* check parameter */
1052 /* search the existing watch */
1053 return afb_evt_listener_unwatch_evt(listener, evtid);
1056 int afb_evt_event_x2_push(struct afb_event_x2 *eventid, struct json_object *object)
1059 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1061 return afb_evt_evtid_hooked_push(evtid, object);
1062 json_object_put(object);
1066 __attribute__((alias("afb_evt_event_x2_unhooked_push")));
1069 int afb_evt_event_x2_unhooked_push(struct afb_event_x2 *eventid, struct json_object *object)
1071 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1073 return afb_evt_evtid_push(evtid, object);
1074 json_object_put(object);
1078 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
1079 struct afb_event_x1 afb_evt_event_from_evtid(struct afb_evtid *evtid)
1083 ? (struct afb_event_x1){ .itf = &afb_evt_hooked_event_x2_itf, .closure = &evtid->eventid }
1085 ? (struct afb_event_x1){ .itf = &afb_evt_event_x2_itf, .closure = &evtid->eventid }
1087 : (struct afb_event_x1){ .itf = NULL, .closure = NULL };
1091 void afb_evt_event_x2_unref(struct afb_event_x2 *eventid)
1093 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1095 afb_evt_evtid_unref(evtid);
1098 struct afb_event_x2 *afb_evt_event_x2_addref(struct afb_event_x2 *eventid)
1100 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
1102 afb_evt_evtid_addref(evtid);