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>
37 * Structure for event listeners
39 struct afb_evt_listener {
41 /* chaining listeners */
42 struct afb_evt_listener *next;
44 /* interface for callbacks */
45 const struct afb_evt_itf *itf;
47 /* closure for the callback */
50 /* head of the list of events listened */
51 struct afb_evt_watch *watchs;
53 /* rwlock of the listener */
54 pthread_rwlock_t rwlock;
56 /* count of reference to the listener */
61 * Structure for describing events
66 struct afb_event_x2 eventid;
69 struct afb_evtid *next;
71 /* head of the list of listeners watching the event */
72 struct afb_evt_watch *watchs;
74 /* rwlock of the event */
75 pthread_rwlock_t rwlock;
88 /* fullname of the event */
93 * Structure for associating events and listeners
95 struct afb_evt_watch {
98 struct afb_evtid *evtid;
100 /* link to the next watcher for the same evtid */
101 struct afb_evt_watch *next_by_evtid;
104 struct afb_evt_listener *listener;
106 /* link to the next watcher for the same listener */
107 struct afb_evt_watch *next_by_listener;
113 /* the interface for events */
114 static struct afb_event_x2_itf afb_evt_event_x2_itf = {
115 .broadcast = (void*)afb_evt_evtid_broadcast,
116 .push = (void*)afb_evt_evtid_push,
117 .unref = (void*)afb_evt_evtid_unref,
118 .name = (void*)afb_evt_evtid_name,
119 .addref = (void*)afb_evt_evtid_addref
123 /* the interface for events */
124 static struct afb_event_x2_itf afb_evt_hooked_event_x2_itf = {
125 .broadcast = (void*)afb_evt_evtid_hooked_broadcast,
126 .push = (void*)afb_evt_evtid_hooked_push,
127 .unref = (void*)afb_evt_evtid_hooked_unref,
128 .name = (void*)afb_evt_evtid_hooked_name,
129 .addref = (void*)afb_evt_evtid_hooked_addref
133 /* head of the list of listeners */
134 static pthread_rwlock_t listeners_rwlock = PTHREAD_RWLOCK_INITIALIZER;
135 static struct afb_evt_listener *listeners = NULL;
137 /* handling id of events */
138 static pthread_rwlock_t events_rwlock = PTHREAD_RWLOCK_INITIALIZER;
139 static struct afb_evtid *evtids = NULL;
140 static int event_id_counter = 0;
141 static int event_id_wrapped = 0;
144 * Broadcasts the 'event' of 'id' with its 'obj'
145 * 'obj' is released (like json_object_put)
146 * Returns the count of listener having receive the event.
148 static int broadcast(const char *event, struct json_object *obj, int id)
151 struct afb_evt_listener *listener;
155 pthread_rwlock_rdlock(&listeners_rwlock);
156 listener = listeners;
158 if (listener->itf->broadcast != NULL) {
159 listener->itf->broadcast(listener->closure, event, id, json_object_get(obj));
162 listener = listener->next;
164 pthread_rwlock_unlock(&listeners_rwlock);
165 json_object_put(obj);
170 * Broadcasts the event 'evtid' with its 'object'
171 * 'object' is released (like json_object_put)
172 * Returns the count of listener that received the event.
174 int afb_evt_evtid_broadcast(struct afb_evtid *evtid, struct json_object *object)
176 return broadcast(evtid->fullname, object, evtid->id);
181 * Broadcasts the 'event' of 'id' with its 'obj'
182 * 'obj' is released (like json_object_put)
183 * calls hooks if hookflags isn't 0
184 * Returns the count of listener having receive the event.
186 static int hooked_broadcast(const char *event, struct json_object *obj, int id, int hookflags)
190 json_object_get(obj);
192 if (hookflags & afb_hook_flag_evt_broadcast_before)
193 afb_hook_evt_broadcast_before(event, id, obj);
195 result = broadcast(event, obj, id);
197 if (hookflags & afb_hook_flag_evt_broadcast_after)
198 afb_hook_evt_broadcast_after(event, id, obj, result);
200 json_object_put(obj);
206 * Broadcasts the event 'evtid' with its 'object'
207 * 'object' is released (like json_object_put)
208 * Returns the count of listener that received the event.
210 int afb_evt_evtid_hooked_broadcast(struct afb_evtid *evtid, struct json_object *object)
212 return hooked_broadcast(evtid->fullname, object, evtid->id, evtid->hookflags);
217 * Broadcasts the 'event' with its 'object'
218 * 'object' is released (like json_object_put)
219 * Returns the count of listener having receive the event.
221 int afb_evt_broadcast(const char *event, struct json_object *object)
224 return hooked_broadcast(event, object, 0, -1);
226 return broadcast(event, object, 0);
231 * Pushes the event 'evtid' with 'obj' to its listeners
232 * 'obj' is released (like json_object_put)
233 * Returns the count of listener that received the event.
235 int afb_evt_evtid_push(struct afb_evtid *evtid, struct json_object *obj)
238 struct afb_evt_watch *watch;
239 struct afb_evt_listener *listener;
242 pthread_rwlock_rdlock(&evtid->rwlock);
243 watch = evtid->watchs;
245 listener = watch->listener;
246 assert(listener->itf->push != NULL);
247 if (watch->activity != 0) {
248 listener->itf->push(listener->closure, evtid->fullname, evtid->id, json_object_get(obj));
251 watch = watch->next_by_evtid;
253 pthread_rwlock_unlock(&evtid->rwlock);
254 json_object_put(obj);
261 static void remove_watch(struct afb_evt_watch *watch)
263 struct afb_evt_watch **prv;
264 struct afb_evtid *evtid;
265 struct afb_evt_listener *listener;
267 /* notify listener if needed */
268 evtid = watch->evtid;
269 listener = watch->listener;
270 if (watch->activity != 0 && listener->itf->remove != NULL)
271 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
273 /* unlink the watch for its event */
274 prv = &evtid->watchs;
276 prv = &(*prv)->next_by_evtid;
277 *prv = watch->next_by_evtid;
279 /* unlink the watch for its listener */
280 prv = &listener->watchs;
282 prv = &(*prv)->next_by_listener;
283 *prv = watch->next_by_listener;
290 * Creates an event of name 'fullname' and returns it or NULL on error.
292 struct afb_evtid *afb_evt_evtid_create(const char *fullname)
295 struct afb_evtid *evtid, *oevt;
297 /* allocates the event */
298 len = strlen(fullname);
299 evtid = malloc(len + sizeof * evtid);
303 /* allocates the id */
304 pthread_rwlock_wrlock(&events_rwlock);
306 if (++event_id_counter < 0) {
307 event_id_wrapped = 1;
308 event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
310 if (!event_id_wrapped)
313 while(oevt != NULL && oevt->id != event_id_counter)
315 } while (oevt != NULL);
317 /* initialize the event */
318 memcpy(evtid->fullname, fullname, len + 1);
319 evtid->next = evtids;
321 evtid->watchs = NULL;
322 evtid->id = event_id_counter;
323 pthread_rwlock_init(&evtid->rwlock, NULL);
326 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
327 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_event_x2_itf : &afb_evt_event_x2_itf;
328 if (evtid->hookflags & afb_hook_flag_evt_create)
329 afb_hook_evt_create(evtid->fullname, evtid->id);
331 evtid->eventid.itf = &afb_evt_event_x2_itf;
333 pthread_rwlock_unlock(&events_rwlock);
335 /* returns the event */
342 * Creates an event of name 'prefix'/'name' and returns it or NULL on error.
344 struct afb_evtid *afb_evt_evtid_create2(const char *prefix, const char *name)
346 size_t prelen, postlen;
349 /* makes the event fullname */
350 prelen = strlen(prefix);
351 postlen = strlen(name);
352 fullname = alloca(prelen + postlen + 2);
353 memcpy(fullname, prefix, prelen);
354 fullname[prelen] = '/';
355 memcpy(fullname + prelen + 1, name, postlen + 1);
357 /* create the event */
358 return afb_evt_evtid_create(fullname);
362 * increment the reference count of the event 'evtid'
364 struct afb_evtid *afb_evt_evtid_addref(struct afb_evtid *evtid)
366 __atomic_add_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED);
371 * decrement the reference count of the event 'evtid'
372 * and destroy it when the count reachs zero
374 void afb_evt_evtid_unref(struct afb_evtid *evtid)
377 struct afb_evtid **prv;
378 struct afb_evt_listener *listener;
380 if (!__atomic_sub_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED)) {
381 /* unlinks the event if valid! */
382 pthread_rwlock_wrlock(&events_rwlock);
385 while (*prv && !(found = (*prv == evtid)))
389 pthread_rwlock_unlock(&events_rwlock);
391 /* destroys the event */
393 ERROR("event not found");
395 /* removes all watchers */
396 while(evtid->watchs != NULL) {
397 listener = evtid->watchs->listener;
398 pthread_rwlock_wrlock(&listener->rwlock);
399 pthread_rwlock_wrlock(&evtid->rwlock);
400 remove_watch(evtid->watchs);
401 pthread_rwlock_unlock(&evtid->rwlock);
402 pthread_rwlock_unlock(&listener->rwlock);
406 pthread_rwlock_destroy(&evtid->rwlock);
413 * Returns the true name of the 'event'
415 const char *afb_evt_evtid_fullname(struct afb_evtid *evtid)
417 return evtid->fullname;
421 * Returns the name of the 'event'
423 const char *afb_evt_evtid_name(struct afb_evtid *evtid)
425 const char *name = strchr(evtid->fullname, '/');
426 return name ? name + 1 : evtid->fullname;
430 * Returns the id of the 'event'
432 int afb_evt_evtid_id(struct afb_evtid *evtid)
438 * Returns an instance of the listener defined by the 'send' callback
440 * Returns NULL in case of memory depletion.
442 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
444 struct afb_evt_listener *listener;
446 /* search if an instance already exists */
447 pthread_rwlock_wrlock(&listeners_rwlock);
448 listener = listeners;
449 while (listener != NULL) {
450 if (listener->itf == itf && listener->closure == closure) {
451 listener = afb_evt_listener_addref(listener);
454 listener = listener->next;
458 listener = calloc(1, sizeof *listener);
459 if (listener != NULL) {
462 listener->closure = closure;
463 listener->watchs = NULL;
464 listener->refcount = 1;
465 pthread_rwlock_init(&listener->rwlock, NULL);
466 listener->next = listeners;
467 listeners = listener;
470 pthread_rwlock_unlock(&listeners_rwlock);
475 * Increases the reference count of 'listener' and returns it
477 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
479 __atomic_add_fetch(&listener->refcount, 1, __ATOMIC_RELAXED);
484 * Decreases the reference count of the 'listener' and destroys it
487 void afb_evt_listener_unref(struct afb_evt_listener *listener)
489 struct afb_evt_listener **prv;
490 struct afb_evtid *evtid;
492 if (listener && !__atomic_sub_fetch(&listener->refcount, 1, __ATOMIC_RELAXED)) {
494 /* unlink the listener */
495 pthread_rwlock_wrlock(&listeners_rwlock);
497 while (*prv != listener)
499 *prv = listener->next;
500 pthread_rwlock_unlock(&listeners_rwlock);
502 /* remove the watchers */
503 pthread_rwlock_wrlock(&listener->rwlock);
504 while (listener->watchs != NULL) {
505 evtid = listener->watchs->evtid;
506 pthread_rwlock_wrlock(&evtid->rwlock);
507 remove_watch(listener->watchs);
508 pthread_rwlock_unlock(&evtid->rwlock);
510 pthread_rwlock_unlock(&listener->rwlock);
512 /* free the listener */
513 pthread_rwlock_destroy(&listener->rwlock);
519 * Makes the 'listener' watching 'evtid'
520 * Returns 0 in case of success or else -1.
522 int afb_evt_watch_add_evtid(struct afb_evt_listener *listener, struct afb_evtid *evtid)
524 struct afb_evt_watch *watch;
526 /* check parameter */
527 if (listener->itf->push == NULL) {
532 /* search the existing watch for the listener */
533 pthread_rwlock_wrlock(&listener->rwlock);
534 watch = listener->watchs;
535 while(watch != NULL) {
536 if (watch->evtid == evtid)
538 watch = watch->next_by_listener;
541 /* not found, allocate a new */
542 watch = malloc(sizeof *watch);
544 pthread_rwlock_unlock(&listener->rwlock);
549 /* initialise and link */
550 watch->evtid = evtid;
552 watch->listener = listener;
553 watch->next_by_listener = listener->watchs;
554 listener->watchs = watch;
555 pthread_rwlock_wrlock(&evtid->rwlock);
556 watch->next_by_evtid = evtid->watchs;
557 evtid->watchs = watch;
558 pthread_rwlock_unlock(&evtid->rwlock);
561 if (watch->activity == 0 && listener->itf->add != NULL)
562 listener->itf->add(listener->closure, evtid->fullname, evtid->id);
564 pthread_rwlock_unlock(&listener->rwlock);
570 * Avoids the 'listener' to watch 'evtid'
571 * Returns 0 in case of success or else -1.
573 int afb_evt_watch_sub_evtid(struct afb_evt_listener *listener, struct afb_evtid *evtid)
575 struct afb_evt_watch *watch;
577 /* search the existing watch */
578 pthread_rwlock_wrlock(&listener->rwlock);
579 watch = listener->watchs;
580 while(watch != NULL) {
581 if (watch->evtid == evtid) {
582 if (watch->activity != 0) {
584 if (watch->activity == 0 && listener->itf->remove != NULL)
585 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
587 pthread_rwlock_unlock(&listener->rwlock);
590 watch = watch->next_by_listener;
592 pthread_rwlock_unlock(&listener->rwlock);
597 inline struct afb_evtid *afb_evt_event_x2_to_evtid(struct afb_event_x2 *eventid)
599 return (struct afb_evtid*)eventid;
602 inline struct afb_event_x2 *afb_evt_event_x2_from_evtid(struct afb_evtid *evtid)
604 return &evtid->eventid;
608 * Creates an event of 'fullname' and returns it.
609 * Returns an event with closure==NULL in case of error.
611 struct afb_event_x2 *afb_evt_event_x2_create(const char *fullname)
613 return afb_evt_event_x2_from_evtid(afb_evt_evtid_create(fullname));
617 * Creates an event of name 'prefix'/'name' and returns it.
618 * Returns an event with closure==NULL in case of error.
620 struct afb_event_x2 *afb_evt_event_x2_create2(const char *prefix, const char *name)
622 return afb_evt_event_x2_from_evtid(afb_evt_evtid_create2(prefix, name));
626 * Returns the fullname of the 'eventid'
628 const char *afb_evt_event_x2_fullname(struct afb_event_x2 *eventid)
630 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
631 return evtid ? evtid->fullname : NULL;
635 * Returns the id of the 'eventid'
637 int afb_evt_event_x2_id(struct afb_event_x2 *eventid)
639 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
640 return evtid ? evtid->id : 0;
644 * Makes the 'listener' watching 'eventid'
645 * Returns 0 in case of success or else -1.
647 int afb_evt_event_x2_add_watch(struct afb_evt_listener *listener, struct afb_event_x2 *eventid)
649 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
651 /* check parameter */
657 /* search the existing watch for the listener */
658 return afb_evt_watch_add_evtid(listener, evtid);
662 * Avoids the 'listener' to watch 'eventid'
663 * Returns 0 in case of success or else -1.
665 int afb_evt_event_x2_remove_watch(struct afb_evt_listener *listener, struct afb_event_x2 *eventid)
667 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
669 /* check parameter */
675 /* search the existing watch */
676 return afb_evt_watch_sub_evtid(listener, evtid);
679 int afb_evt_event_x2_push(struct afb_event_x2 *eventid, struct json_object *object)
682 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
684 return afb_evt_evtid_hooked_push(evtid, object);
685 json_object_put(object);
689 __attribute__((alias("afb_evt_event_x2_unhooked_push")));
692 int afb_evt_event_x2_unhooked_push(struct afb_event_x2 *eventid, struct json_object *object)
694 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
696 return afb_evt_evtid_push(evtid, object);
697 json_object_put(object);
701 #if WITH_LEGACY_BINDING_V1 || WITH_LEGACY_BINDING_V2
702 struct afb_event_x1 afb_evt_event_from_evtid(struct afb_evtid *evtid)
706 ? (struct afb_event_x1){ .itf = &afb_evt_hooked_event_x2_itf, .closure = &evtid->eventid }
708 ? (struct afb_event_x1){ .itf = &afb_evt_event_x2_itf, .closure = &evtid->eventid }
710 : (struct afb_event_x1){ .itf = NULL, .closure = NULL };
714 void afb_evt_event_x2_unref(struct afb_event_x2 *eventid)
716 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
718 afb_evt_evtid_unref(evtid);
721 struct afb_event_x2 *afb_evt_event_x2_addref(struct afb_event_x2 *eventid)
723 struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
725 afb_evt_evtid_addref(evtid);
731 * Pushes the event 'evtid' with 'obj' to its listeners
732 * 'obj' is released (like json_object_put)
733 * Emits calls to hooks.
734 * Returns the count of listener taht received the event.
736 int afb_evt_evtid_hooked_push(struct afb_evtid *evtid, struct json_object *obj)
741 /* lease the object */
742 json_object_get(obj);
744 /* hook before push */
745 if (evtid->hookflags & afb_hook_flag_evt_push_before)
746 afb_hook_evt_push_before(evtid->fullname, evtid->id, obj);
749 result = afb_evt_evtid_push(evtid, obj);
751 /* hook after push */
752 if (evtid->hookflags & afb_hook_flag_evt_push_after)
753 afb_hook_evt_push_after(evtid->fullname, evtid->id, obj, result);
755 /* release the object */
756 json_object_put(obj);
761 * increment the reference count of the event 'evtid'
763 struct afb_evtid *afb_evt_evtid_hooked_addref(struct afb_evtid *evtid)
765 if (evtid->hookflags & afb_hook_flag_evt_addref)
766 afb_hook_evt_addref(evtid->fullname, evtid->id);
767 return afb_evt_evtid_addref(evtid);
771 * decrement the reference count of the event 'evtid'
772 * and destroy it when the count reachs zero
774 void afb_evt_evtid_hooked_unref(struct afb_evtid *evtid)
776 if (evtid->hookflags & afb_hook_flag_evt_unref)
777 afb_hook_evt_unref(evtid->fullname, evtid->id);
778 afb_evt_evtid_unref(evtid);
782 * Returns the name associated to the event 'evtid'.
784 const char *afb_evt_evtid_hooked_name(struct afb_evtid *evtid)
786 const char *result = afb_evt_evtid_name(evtid);
787 if (evtid->hookflags & afb_hook_flag_evt_name)
788 afb_hook_evt_name(evtid->fullname, evtid->id, result);
793 * update the hooks for events
795 void afb_evt_update_hooks()
797 struct afb_evtid *evtid;
799 pthread_rwlock_rdlock(&events_rwlock);
800 for (evtid = evtids ; evtid ; evtid = evtid->next) {
801 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
802 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_event_x2_itf : &afb_evt_event_x2_itf;
804 pthread_rwlock_unlock(&events_rwlock);