2 * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
27 #include <json-c/json.h>
28 #include <afb/afb-eventid-itf.h>
29 #include <afb/afb-event.h>
38 * Structure for event listeners
40 struct afb_evt_listener {
42 /* chaining listeners */
43 struct afb_evt_listener *next;
45 /* interface for callbacks */
46 const struct afb_evt_itf *itf;
48 /* closure for the callback */
51 /* head of the list of events listened */
52 struct afb_evt_watch *watchs;
54 /* mutex of the listener */
55 pthread_mutex_t mutex;
57 /* count of reference to the listener */
62 * Structure for describing events
67 struct afb_eventid eventid;
70 struct afb_evtid *next;
72 /* head of the list of listeners watching the event */
73 struct afb_evt_watch *watchs;
75 /* mutex of the event */
76 pthread_mutex_t mutex;
87 /* fullname of the event */
92 * Structure for associating events and listeners
94 struct afb_evt_watch {
97 struct afb_evtid *evtid;
99 /* link to the next watcher for the same evtid */
100 struct afb_evt_watch *next_by_evtid;
103 struct afb_evt_listener *listener;
105 /* link to the next watcher for the same listener */
106 struct afb_evt_watch *next_by_listener;
112 /* the interface for events */
113 static struct afb_eventid_itf afb_evt_eventid_itf = {
114 .broadcast = (void*)afb_evt_evtid_broadcast,
115 .push = (void*)afb_evt_evtid_push,
116 .unref = (void*)afb_evt_evtid_unref,
117 .name = (void*)afb_evt_evtid_name,
118 .addref = (void*)afb_evt_evtid_addref
121 /* the interface for events */
122 static struct afb_eventid_itf afb_evt_hooked_eventid_itf = {
123 .broadcast = (void*)afb_evt_evtid_hooked_broadcast,
124 .push = (void*)afb_evt_evtid_hooked_push,
125 .unref = (void*)afb_evt_evtid_hooked_unref,
126 .name = (void*)afb_evt_evtid_hooked_name,
127 .addref = (void*)afb_evt_evtid_hooked_addref
130 /* head of the list of listeners */
131 static pthread_mutex_t listeners_mutex = PTHREAD_MUTEX_INITIALIZER;
132 static struct afb_evt_listener *listeners = NULL;
134 /* handling id of events */
135 static pthread_mutex_t events_mutex = PTHREAD_MUTEX_INITIALIZER;
136 static struct afb_evtid *evtids = NULL;
137 static int event_id_counter = 0;
138 static int event_id_wrapped = 0;
141 * Broadcasts the 'event' of 'id' with its 'obj'
142 * 'obj' is released (like json_object_put)
143 * Returns the count of listener having receive the event.
145 static int broadcast(const char *event, struct json_object *obj, int id)
148 struct afb_evt_listener *listener;
151 pthread_mutex_lock(&listeners_mutex);
152 listener = listeners;
154 if (listener->itf->broadcast != NULL) {
155 listener->itf->broadcast(listener->closure, event, id, json_object_get(obj));
158 listener = listener->next;
160 pthread_mutex_unlock(&listeners_mutex);
161 json_object_put(obj);
166 * Broadcasts the 'event' of 'id' with its 'obj'
167 * 'obj' is released (like json_object_put)
168 * calls hooks if hookflags isn't 0
169 * Returns the count of listener having receive the event.
171 static int hooked_broadcast(const char *event, struct json_object *obj, int id, int hookflags)
175 json_object_get(obj);
177 if (hookflags & afb_hook_flag_evt_broadcast_before)
178 afb_hook_evt_broadcast_before(event, id, obj);
180 result = broadcast(event, obj, id);
182 if (hookflags & afb_hook_flag_evt_broadcast_after)
183 afb_hook_evt_broadcast_after(event, id, obj, result);
185 json_object_put(obj);
191 * Broadcasts the event 'evtid' with its 'object'
192 * 'object' is released (like json_object_put)
193 * Returns the count of listener that received the event.
195 int afb_evt_evtid_broadcast(struct afb_evtid *evtid, struct json_object *object)
197 return broadcast(evtid->fullname, object, evtid->id);
201 * Broadcasts the event 'evtid' with its 'object'
202 * 'object' is released (like json_object_put)
203 * Returns the count of listener that received the event.
205 int afb_evt_evtid_hooked_broadcast(struct afb_evtid *evtid, struct json_object *object)
207 return hooked_broadcast(evtid->fullname, object, evtid->id, evtid->hookflags);
211 * Broadcasts the 'event' with its 'object'
212 * 'object' is released (like json_object_put)
213 * Returns the count of listener having receive the event.
215 int afb_evt_broadcast(const char *event, struct json_object *object)
217 return hooked_broadcast(event, object, 0, -1);
221 * Pushes the event 'evtid' with 'obj' to its listeners
222 * 'obj' is released (like json_object_put)
223 * Returns the count of listener that received the event.
225 int afb_evt_evtid_push(struct afb_evtid *evtid, struct json_object *obj)
228 struct afb_evt_watch *watch;
229 struct afb_evt_listener *listener;
232 pthread_mutex_lock(&evtid->mutex);
233 watch = evtid->watchs;
235 listener = watch->listener;
236 assert(listener->itf->push != NULL);
237 if (watch->activity != 0) {
238 listener->itf->push(listener->closure, evtid->fullname, evtid->id, json_object_get(obj));
241 watch = watch->next_by_evtid;
243 pthread_mutex_unlock(&evtid->mutex);
244 json_object_put(obj);
249 * Pushes the event 'evtid' with 'obj' to its listeners
250 * 'obj' is released (like json_object_put)
251 * Emits calls to hooks.
252 * Returns the count of listener taht received the event.
254 int afb_evt_evtid_hooked_push(struct afb_evtid *evtid, struct json_object *obj)
258 /* lease the object */
259 json_object_get(obj);
261 /* hook before push */
262 if (evtid->hookflags & afb_hook_flag_evt_push_before)
263 afb_hook_evt_push_before(evtid->fullname, evtid->id, obj);
266 result = afb_evt_evtid_push(evtid, obj);
268 /* hook after push */
269 if (evtid->hookflags & afb_hook_flag_evt_push_after)
270 afb_hook_evt_push_after(evtid->fullname, evtid->id, obj, result);
272 /* release the object */
273 json_object_put(obj);
280 static void remove_watch(struct afb_evt_watch *watch)
282 struct afb_evt_watch **prv;
283 struct afb_evtid *evtid;
284 struct afb_evt_listener *listener;
286 /* notify listener if needed */
287 evtid = watch->evtid;
288 listener = watch->listener;
289 if (watch->activity != 0 && listener->itf->remove != NULL)
290 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
292 /* unlink the watch for its event */
293 prv = &evtid->watchs;
295 prv = &(*prv)->next_by_evtid;
296 *prv = watch->next_by_evtid;
298 /* unlink the watch for its listener */
299 prv = &listener->watchs;
301 prv = &(*prv)->next_by_listener;
302 *prv = watch->next_by_listener;
309 * Creates an event of name 'fullname' and returns it or NULL on error.
311 struct afb_evtid *afb_evt_evtid_create(const char *fullname)
314 struct afb_evtid *evtid, *oevt;
316 /* allocates the event */
317 len = strlen(fullname);
318 evtid = malloc(len + sizeof * evtid);
322 /* allocates the id */
323 pthread_mutex_lock(&events_mutex);
325 if (++event_id_counter < 0) {
326 event_id_wrapped = 1;
327 event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
329 if (!event_id_wrapped)
332 while(oevt != NULL && oevt->id != event_id_counter)
334 } while (oevt != NULL);
336 /* initialize the event */
337 memcpy(evtid->fullname, fullname, len + 1);
338 evtid->next = evtids;
339 evtid->watchs = NULL;
340 evtid->id = event_id_counter;
341 pthread_mutex_init(&evtid->mutex, NULL);
343 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
344 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_eventid_itf : &afb_evt_eventid_itf;
345 if (evtid->hookflags & afb_hook_flag_evt_create)
346 afb_hook_evt_create(evtid->fullname, evtid->id);
347 pthread_mutex_unlock(&events_mutex);
349 /* returns the event */
356 * increment the reference count of the event 'evtid'
358 struct afb_evtid *afb_evt_evtid_addref(struct afb_evtid *evtid)
360 __atomic_add_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED);
365 * increment the reference count of the event 'evtid'
367 struct afb_evtid *afb_evt_evtid_hooked_addref(struct afb_evtid *evtid)
369 if (evtid->hookflags & afb_hook_flag_evt_addref)
370 afb_hook_evt_addref(evtid->fullname, evtid->id);
371 return afb_evt_evtid_addref(evtid);
375 * decrement the reference count of the event 'evtid'
376 * and destroy it when the count reachs zero
378 void afb_evt_evtid_unref(struct afb_evtid *evtid)
381 struct afb_evtid **prv;
382 struct afb_evt_listener *listener;
384 if (!__atomic_sub_fetch(&evtid->refcount, 1, __ATOMIC_RELAXED)) {
385 /* unlinks the event if valid! */
386 pthread_mutex_lock(&events_mutex);
389 while (*prv && !(found = (*prv == evtid)))
393 pthread_mutex_unlock(&events_mutex);
395 /* destroys the event */
397 ERROR("event not found");
399 /* removes all watchers */
400 while(evtid->watchs != NULL) {
401 listener = evtid->watchs->listener;
402 pthread_mutex_lock(&listener->mutex);
403 pthread_mutex_lock(&evtid->mutex);
404 remove_watch(evtid->watchs);
405 pthread_mutex_unlock(&evtid->mutex);
406 pthread_mutex_unlock(&listener->mutex);
410 pthread_mutex_destroy(&evtid->mutex);
417 * decrement the reference count of the event 'evtid'
418 * and destroy it when the count reachs zero
420 void afb_evt_evtid_hooked_unref(struct afb_evtid *evtid)
422 if (evtid->hookflags & afb_hook_flag_evt_unref)
423 afb_hook_evt_unref(evtid->fullname, evtid->id);
424 afb_evt_evtid_unref(evtid);
428 * Returns the true name of the 'event'
430 const char *afb_evt_evtid_fullname(struct afb_evtid *evtid)
432 return evtid->fullname;
436 * Returns the name of the 'event'
438 const char *afb_evt_evtid_name(struct afb_evtid *evtid)
440 const char *name = strchr(evtid->fullname, '/');
441 return name ? name + 1 : evtid->fullname;
445 * Returns the name associated to the event 'evtid'.
447 const char *afb_evt_evtid_hooked_name(struct afb_evtid *evtid)
449 const char *result = afb_evt_evtid_name(evtid);
450 if (evtid->hookflags & afb_hook_flag_evt_name)
451 afb_hook_evt_name(evtid->fullname, evtid->id, result);
456 * Returns the id of the 'event'
458 int afb_evt_evtid_id(struct afb_evtid *evtid)
464 * Returns an instance of the listener defined by the 'send' callback
466 * Returns NULL in case of memory depletion.
468 struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure)
470 struct afb_evt_listener *listener;
472 /* search if an instance already exists */
473 pthread_mutex_lock(&listeners_mutex);
474 listener = listeners;
475 while (listener != NULL) {
476 if (listener->itf == itf && listener->closure == closure) {
477 listener = afb_evt_listener_addref(listener);
480 listener = listener->next;
484 listener = calloc(1, sizeof *listener);
485 if (listener != NULL) {
488 listener->closure = closure;
489 listener->watchs = NULL;
490 listener->refcount = 1;
491 pthread_mutex_init(&listener->mutex, NULL);
492 listener->next = listeners;
493 listeners = listener;
496 pthread_mutex_unlock(&listeners_mutex);
501 * Increases the reference count of 'listener' and returns it
503 struct afb_evt_listener *afb_evt_listener_addref(struct afb_evt_listener *listener)
505 __atomic_add_fetch(&listener->refcount, 1, __ATOMIC_RELAXED);
510 * Decreases the reference count of the 'listener' and destroys it
513 void afb_evt_listener_unref(struct afb_evt_listener *listener)
515 struct afb_evt_listener **prv;
516 struct afb_evtid *evtid;
518 if (!__atomic_sub_fetch(&listener->refcount, 1, __ATOMIC_RELAXED)) {
520 /* unlink the listener */
521 pthread_mutex_lock(&listeners_mutex);
523 while (*prv != listener)
525 *prv = listener->next;
526 pthread_mutex_unlock(&listeners_mutex);
528 /* remove the watchers */
529 pthread_mutex_lock(&listener->mutex);
530 while (listener->watchs != NULL) {
531 evtid = listener->watchs->evtid;
532 pthread_mutex_lock(&evtid->mutex);
533 remove_watch(listener->watchs);
534 pthread_mutex_unlock(&evtid->mutex);
536 pthread_mutex_unlock(&listener->mutex);
538 /* free the listener */
539 pthread_mutex_destroy(&listener->mutex);
545 * Makes the 'listener' watching 'evtid'
546 * Returns 0 in case of success or else -1.
548 int afb_evt_watch_add_evtid(struct afb_evt_listener *listener, struct afb_evtid *evtid)
550 struct afb_evt_watch *watch;
552 /* check parameter */
553 if (listener->itf->push == NULL) {
558 /* search the existing watch for the listener */
559 pthread_mutex_lock(&listener->mutex);
560 watch = listener->watchs;
561 while(watch != NULL) {
562 if (watch->evtid == evtid)
564 watch = watch->next_by_listener;
567 /* not found, allocate a new */
568 watch = malloc(sizeof *watch);
570 pthread_mutex_unlock(&listener->mutex);
575 /* initialise and link */
576 watch->evtid = evtid;
578 watch->listener = listener;
579 watch->next_by_listener = listener->watchs;
580 listener->watchs = watch;
581 pthread_mutex_lock(&evtid->mutex);
582 watch->next_by_evtid = evtid->watchs;
583 evtid->watchs = watch;
584 pthread_mutex_unlock(&evtid->mutex);
587 if (watch->activity == 0 && listener->itf->add != NULL)
588 listener->itf->add(listener->closure, evtid->fullname, evtid->id);
590 pthread_mutex_unlock(&listener->mutex);
596 * Avoids the 'listener' to watch 'evtid'
597 * Returns 0 in case of success or else -1.
599 int afb_evt_watch_sub_evtid(struct afb_evt_listener *listener, struct afb_evtid *evtid)
601 struct afb_evt_watch *watch;
603 /* search the existing watch */
604 pthread_mutex_lock(&listener->mutex);
605 watch = listener->watchs;
606 while(watch != NULL) {
607 if (watch->evtid == evtid) {
608 if (watch->activity != 0) {
610 if (watch->activity == 0 && listener->itf->remove != NULL)
611 listener->itf->remove(listener->closure, evtid->fullname, evtid->id);
613 pthread_mutex_unlock(&listener->mutex);
616 watch = watch->next_by_listener;
618 pthread_mutex_unlock(&listener->mutex);
624 * update the hooks for events
626 void afb_evt_update_hooks()
628 struct afb_evtid *evtid;
630 pthread_mutex_lock(&events_mutex);
631 for (evtid = evtids ; evtid ; evtid = evtid->next) {
632 evtid->hookflags = afb_hook_flags_evt(evtid->fullname);
633 evtid->eventid.itf = evtid->hookflags ? &afb_evt_hooked_eventid_itf : &afb_evt_eventid_itf;
635 pthread_mutex_unlock(&events_mutex);
638 struct afb_evtid *afb_evt_to_evtid(struct afb_event event)
640 return (struct afb_evtid*)(event.itf == &afb_evt_hooked_eventid_itf ? event.closure : NULL);
643 struct afb_event afb_evt_from_evtid(struct afb_evtid *evtid)
645 return (struct afb_event){ .itf = evtid ? &afb_evt_hooked_eventid_itf : NULL, .closure = &evtid->eventid };
649 * Creates an event of 'fullname' and returns it.
650 * Returns an event with closure==NULL in case of error.
652 struct afb_event afb_evt_create_event(const char *fullname)
654 return afb_evt_from_evtid(afb_evt_evtid_create(fullname));
658 * Returns the fullname of the 'event'
660 const char *afb_evt_event_fullname(struct afb_event event)
662 struct afb_evtid *evtid = afb_evt_to_evtid(event);
663 return evtid ? evtid->fullname : NULL;
667 * Returns the id of the 'event'
669 int afb_evt_event_id(struct afb_event event)
671 struct afb_evtid *evtid = afb_evt_to_evtid(event);
672 return evtid ? evtid->id : 0;
676 * Makes the 'listener' watching 'event'
677 * Returns 0 in case of success or else -1.
679 int afb_evt_add_watch(struct afb_evt_listener *listener, struct afb_event event)
681 struct afb_evtid *evtid = afb_evt_to_evtid(event);
683 /* check parameter */
689 /* search the existing watch for the listener */
690 return afb_evt_watch_add_evtid(listener, evtid);
694 * Avoids the 'listener' to watch 'event'
695 * Returns 0 in case of success or else -1.
697 int afb_evt_remove_watch(struct afb_evt_listener *listener, struct afb_event event)
699 struct afb_evtid *evtid = afb_evt_to_evtid(event);
701 /* check parameter */
707 /* search the existing watch */
708 return afb_evt_watch_sub_evtid(listener, evtid);
711 int afb_evt_push(struct afb_event event, struct json_object *object)
713 struct afb_evtid *evtid = afb_evt_to_evtid(event);
715 return afb_evt_evtid_hooked_push(evtid, object);
716 json_object_put(object);
720 int afb_evt_unhooked_push(struct afb_event event, struct json_object *object)
722 struct afb_evtid *evtid = afb_evt_to_evtid(event);
724 return afb_evt_evtid_push(evtid, object);
725 json_object_put(object);