daf88d6b663ea2b864aca2382f65ec576129cdea
[src/app-framework-binder.git] / include / afb / afb-event-x2.h
1 /*
2  * Copyright (C) 2016-2019 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #pragma once
19
20 #include "afb-event-x2-itf.h"
21
22 /** @defgroup AFB_EVENT
23  *  @{ */
24
25 /**
26  * Checks whether the 'event' is valid or not.
27  *
28  * @param event the event to check
29  *
30  * @return 0 if not valid or 1 if valid.
31  */
32 static inline int afb_event_x2_is_valid(struct afb_event_x2 *event)
33 {
34         return !!event;
35 }
36
37 /**
38  * Broadcasts widely an event of 'event' with the data 'object'.
39  * 'object' can be NULL.
40  *
41  * For convenience, the function calls 'json_object_put' for 'object'.
42  * Thus, in the case where 'object' should remain available after
43  * the function returns, the function 'json_object_get' shall be used.
44  *
45  * @param event the event to broadcast
46  * @param object the companion object to associate to the broadcasted event (can be NULL)
47  *
48  * @return 0 in case of success or -1 in case of error
49  */
50 static inline int afb_event_x2_broadcast(
51                         struct afb_event_x2 *event,
52                         struct json_object *object)
53 {
54         return event->itf->broadcast(event, object);
55 }
56
57 /**
58  * Pushes an event of 'event' with the data 'object' to its observers.
59  * 'object' can be NULL.
60  *
61  * For convenience, the function calls 'json_object_put' for 'object'.
62  * Thus, in the case where 'object' should remain available after
63  * the function returns, the function 'json_object_get' shall be used.
64  *
65  * @param event the event to push
66  * @param object the companion object to associate to the pushed event (can be NULL)
67  *
68  * @Return
69  *   *  1 if at least one client listen for the event
70  *   *  0 if no more client listen for the event
71  *   * -1 in case of error (the event can't be delivered)
72  */
73 static inline int afb_event_x2_push(
74                         struct afb_event_x2 *event,
75                         struct json_object *object)
76 {
77         return event->itf->push(event, object);
78 }
79
80 /**
81  * Gets the name associated to 'event'.
82  *
83  * @param event the event whose name is requested
84  *
85  * @return the name of the event
86  *
87  * The returned name can be used until call to 'afb_event_x2_unref'.
88  * It shouldn't be freed.
89  */
90 static inline const char *afb_event_x2_name(struct afb_event_x2 *event)
91 {
92         return event->itf->name(event);
93 }
94
95 /**
96  * Decrease the count of references to 'event'.
97  * Call this function when the evenid is no more used.
98  * It destroys the event_x2 when the reference count falls to zero.
99  *
100  * @param event the event
101  */
102 static inline void afb_event_x2_unref(struct afb_event_x2 *event)
103 {
104         event->itf->unref(event);
105 }
106
107 /**
108  * Increases the count of references to 'event'
109  *
110  * @param event the event
111  *
112  * @return the event
113  */
114 static inline struct afb_event_x2 *afb_event_x2_addref(
115                                         struct afb_event_x2 *event)
116 {
117         return event->itf->addref(event);
118 }
119
120 /** @} */