subcall_req: introduce afb_req_subcall_req
[src/app-framework-binder.git] / src / afb-hook.h
1 /*
2  * Copyright (C) 2016, 2017 "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 <stdarg.h>
21
22 struct req;
23 struct afb_context;
24 struct json_object;
25 struct afb_arg;
26 struct afb_event;
27 struct afb_session;
28 struct afb_xreq;
29 struct afb_ditf;
30 struct afb_svc;
31 struct afb_stored_req;
32 struct sd_bus;
33 struct sd_event;
34
35 /*********************************************************
36 * section hooking xreq
37 *********************************************************/
38 struct afb_hook_xreq;
39
40 /* individual flags */
41 #define afb_hook_flag_req_begin                 0x000001
42 #define afb_hook_flag_req_end                   0x000002
43 #define afb_hook_flag_req_json                  0x000004
44 #define afb_hook_flag_req_get                   0x000008
45 #define afb_hook_flag_req_success               0x000010
46 #define afb_hook_flag_req_fail                  0x000020
47 #define afb_hook_flag_req_context_get           0x000040
48 #define afb_hook_flag_req_context_set           0x000080
49 #define afb_hook_flag_req_addref                0x000100
50 #define afb_hook_flag_req_unref                 0x000200
51 #define afb_hook_flag_req_session_close         0x000400
52 #define afb_hook_flag_req_session_set_LOA       0x000800
53 #define afb_hook_flag_req_subscribe             0x001000
54 #define afb_hook_flag_req_unsubscribe           0x002000
55 #define afb_hook_flag_req_subcall               0x004000
56 #define afb_hook_flag_req_subcall_result        0x008000
57 #define afb_hook_flag_req_subcallsync           0x010000
58 #define afb_hook_flag_req_subcallsync_result    0x020000
59 #define afb_hook_flag_req_vverbose              0x040000
60 #define afb_hook_flag_req_store                 0x080000
61 #define afb_hook_flag_req_unstore               0x100000
62 #define afb_hook_flag_req_subcall_req           0x200000
63 #define afb_hook_flag_req_subcall_req_result    0x400000
64
65 /* common flags */
66 #define afb_hook_flags_req_life         (afb_hook_flag_req_begin|afb_hook_flag_req_end)
67 #define afb_hook_flags_req_args         (afb_hook_flag_req_json|afb_hook_flag_req_get)
68 #define afb_hook_flags_req_result       (afb_hook_flag_req_success|afb_hook_flag_req_fail)
69 #define afb_hook_flags_req_session      (afb_hook_flag_req_session_close|afb_hook_flag_req_session_set_LOA)
70 #define afb_hook_flags_req_event        (afb_hook_flag_req_subscribe|afb_hook_flag_req_unsubscribe)
71 #define afb_hook_flags_req_subcalls     (afb_hook_flag_req_subcall|afb_hook_flag_req_subcall_result\
72                                         |afb_hook_flag_req_subcall_req|afb_hook_flag_req_subcall_req_result\
73                                         |afb_hook_flag_req_subcallsync|afb_hook_flag_req_subcallsync_result)
74
75 /* extra flags */
76 #define afb_hook_flags_req_ref          (afb_hook_flag_req_addref|afb_hook_flag_req_unref)
77 #define afb_hook_flags_req_context      (afb_hook_flag_req_context_get|afb_hook_flag_req_context_set)
78 #define afb_hook_flags_req_stores       (afb_hook_flag_req_store|afb_hook_flag_req_unstore)
79
80 /* predefined groups */
81 #define afb_hook_flags_req_common       (afb_hook_flags_req_life|afb_hook_flags_req_args|afb_hook_flags_req_result\
82                                         |afb_hook_flags_req_session|afb_hook_flags_req_event|afb_hook_flags_req_subcalls\
83                                         |afb_hook_flag_req_vverbose)
84 #define afb_hook_flags_req_extra        (afb_hook_flags_req_common|afb_hook_flags_req_ref|afb_hook_flags_req_context\
85                                         |afb_hook_flags_req_stores)
86 #define afb_hook_flags_req_all          (afb_hook_flags_req_extra)
87
88 struct afb_hook_xreq_itf {
89         void (*hook_xreq_begin)(void * closure, const struct afb_xreq *xreq);
90         void (*hook_xreq_end)(void * closure, const struct afb_xreq *xreq);
91         void (*hook_xreq_json)(void * closure, const struct afb_xreq *xreq, struct json_object *obj);
92         void (*hook_xreq_get)(void * closure, const struct afb_xreq *xreq, const char *name, struct afb_arg arg);
93         void (*hook_xreq_success)(void * closure, const struct afb_xreq *xreq, struct json_object *obj, const char *info);
94         void (*hook_xreq_fail)(void * closure, const struct afb_xreq *xreq, const char *status, const char *info);
95         void (*hook_xreq_context_get)(void * closure, const struct afb_xreq *xreq, void *value);
96         void (*hook_xreq_context_set)(void * closure, const struct afb_xreq *xreq, void *value, void (*free_value)(void*));
97         void (*hook_xreq_addref)(void * closure, const struct afb_xreq *xreq);
98         void (*hook_xreq_unref)(void * closure, const struct afb_xreq *xreq);
99         void (*hook_xreq_session_close)(void * closure, const struct afb_xreq *xreq);
100         void (*hook_xreq_session_set_LOA)(void * closure, const struct afb_xreq *xreq, unsigned level, int result);
101         void (*hook_xreq_subscribe)(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result);
102         void (*hook_xreq_unsubscribe)(void * closure, const struct afb_xreq *xreq, struct afb_event event, int result);
103         void (*hook_xreq_subcall)(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
104         void (*hook_xreq_subcall_result)(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result);
105         void (*hook_xreq_subcallsync)(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
106         void (*hook_xreq_subcallsync_result)(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result);
107         void (*hook_xreq_vverbose)(void * closure, const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args);
108         void (*hook_xreq_store)(void * closure, const struct afb_xreq *xreq, struct afb_stored_req *sreq);
109         void (*hook_xreq_unstore)(void * closure, const struct afb_xreq *xreq);
110         void (*hook_xreq_subcall_req)(void * closure, const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
111         void (*hook_xreq_subcall_req_result)(void * closure, const struct afb_xreq *xreq, int status, struct json_object *result);
112 };
113
114 extern void afb_hook_init_xreq(struct afb_xreq *xreq);
115
116 extern struct afb_hook_xreq *afb_hook_create_xreq(const char *api, const char *verb, struct afb_session *session, int flags, struct afb_hook_xreq_itf *itf, void *closure);
117 extern struct afb_hook_xreq *afb_hook_addref_xreq(struct afb_hook_xreq *spec);
118 extern void afb_hook_unref_xreq(struct afb_hook_xreq *spec);
119
120 /* hooks for xreq */
121 extern void afb_hook_xreq_begin(const struct afb_xreq *xreq);
122 extern void afb_hook_xreq_end(const struct afb_xreq *xreq);
123 extern struct json_object *afb_hook_xreq_json(const struct afb_xreq *xreq, struct json_object *obj);
124 extern struct afb_arg afb_hook_xreq_get(const struct afb_xreq *xreq, const char *name, struct afb_arg arg);
125 extern void afb_hook_xreq_success(const struct afb_xreq *xreq, struct json_object *obj, const char *info);
126 extern void afb_hook_xreq_fail(const struct afb_xreq *xreq, const char *status, const char *info);
127 extern void *afb_hook_xreq_context_get(const struct afb_xreq *xreq, void *value);
128 extern void afb_hook_xreq_context_set(const struct afb_xreq *xreq, void *value, void (*free_value)(void*));
129 extern void afb_hook_xreq_addref(const struct afb_xreq *xreq);
130 extern void afb_hook_xreq_unref(const struct afb_xreq *xreq);
131 extern void afb_hook_xreq_session_close(const struct afb_xreq *xreq);
132 extern int afb_hook_xreq_session_set_LOA(const struct afb_xreq *xreq, unsigned level, int result);
133 extern int afb_hook_xreq_subscribe(const struct afb_xreq *xreq, struct afb_event event, int result);
134 extern int afb_hook_xreq_unsubscribe(const struct afb_xreq *xreq, struct afb_event event, int result);
135 extern void afb_hook_xreq_subcall(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
136 extern void afb_hook_xreq_subcall_result(const struct afb_xreq *xreq, int status, struct json_object *result);
137 extern void afb_hook_xreq_subcallsync(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
138 extern int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int status, struct json_object *result);
139 extern void afb_hook_xreq_vverbose(const struct afb_xreq *xreq, int level, const char *file, int line, const char *func, const char *fmt, va_list args);
140 extern void afb_hook_xreq_store(const struct afb_xreq *xreq, struct afb_stored_req *sreq);
141 extern void afb_hook_xreq_unstore(const struct afb_xreq *xreq);
142 extern void afb_hook_xreq_subcall_req(const struct afb_xreq *xreq, const char *api, const char *verb, struct json_object *args);
143 extern void afb_hook_xreq_subcall_req_result(const struct afb_xreq *xreq, int status, struct json_object *result);
144
145 /*********************************************************
146 * section hooking ditf (daemon interface)
147 *********************************************************/
148
149 #define afb_hook_flag_ditf_vverbose                     0x000001
150 #define afb_hook_flag_ditf_event_make                   0x000002
151 #define afb_hook_flag_ditf_event_broadcast_before       0x000004
152 #define afb_hook_flag_ditf_event_broadcast_after        0x000008
153 #define afb_hook_flag_ditf_get_event_loop               0x000010
154 #define afb_hook_flag_ditf_get_user_bus                 0x000020
155 #define afb_hook_flag_ditf_get_system_bus               0x000040
156 #define afb_hook_flag_ditf_rootdir_get_fd               0x000080
157 #define afb_hook_flag_ditf_rootdir_open_locale          0x000100
158 #define afb_hook_flag_ditf_queue_job                    0x000200
159 #define afb_hook_flag_ditf_unstore_req                  0x000400
160 #define afb_hook_flag_ditf_require_api                  0x000800
161 #define afb_hook_flag_ditf_require_api_result           0x001000
162
163 #define afb_hook_flags_ditf_common      (afb_hook_flag_ditf_vverbose\
164                                         |afb_hook_flag_ditf_event_make\
165                                         |afb_hook_flag_ditf_event_broadcast_before\
166                                         |afb_hook_flag_ditf_event_broadcast_after)
167 #define afb_hook_flags_ditf_extra       (afb_hook_flag_ditf_get_event_loop\
168                                         |afb_hook_flag_ditf_get_user_bus\
169                                         |afb_hook_flag_ditf_get_system_bus\
170                                         |afb_hook_flag_ditf_rootdir_get_fd\
171                                         |afb_hook_flag_ditf_rootdir_open_locale\
172                                         |afb_hook_flag_ditf_queue_job\
173                                         |afb_hook_flag_ditf_unstore_req\
174                                         |afb_hook_flag_ditf_require_api\
175                                         |afb_hook_flag_ditf_require_api_result)
176
177 #define afb_hook_flags_ditf_all         (afb_hook_flags_ditf_common|afb_hook_flags_ditf_extra)
178
179 struct afb_hook_ditf_itf {
180         void (*hook_ditf_event_broadcast_before)(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object);
181         void (*hook_ditf_event_broadcast_after)(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result);
182         void (*hook_ditf_get_event_loop)(void *closure, const struct afb_ditf *ditf, struct sd_event *result);
183         void (*hook_ditf_get_user_bus)(void *closure, const struct afb_ditf *ditf, struct sd_bus *result);
184         void (*hook_ditf_get_system_bus)(void *closure, const struct afb_ditf *ditf, struct sd_bus *result);
185         void (*hook_ditf_vverbose)(void*closure, const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args);
186         void (*hook_ditf_event_make)(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result);
187         void (*hook_ditf_rootdir_get_fd)(void *closure, const struct afb_ditf *ditf, int result);
188         void (*hook_ditf_rootdir_open_locale)(void *closure, const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result);
189         void (*hook_ditf_queue_job)(void *closure, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result);
190         void (*hook_ditf_unstore_req)(void *closure, const struct afb_ditf *ditf, struct afb_stored_req *sreq);
191         void (*hook_ditf_require_api)(void *closure, const struct afb_ditf *ditf, const char *name, int initialized);
192         void (*hook_ditf_require_api_result)(void *closure, const struct afb_ditf *ditf, const char *name, int initialized, int result);
193 };
194
195 extern void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object);
196 extern int afb_hook_ditf_event_broadcast_after(const struct afb_ditf *ditf, const char *name, struct json_object *object, int result);
197 extern struct sd_event *afb_hook_ditf_get_event_loop(const struct afb_ditf *ditf, struct sd_event *result);
198 extern struct sd_bus *afb_hook_ditf_get_user_bus(const struct afb_ditf *ditf, struct sd_bus *result);
199 extern struct sd_bus *afb_hook_ditf_get_system_bus(const struct afb_ditf *ditf, struct sd_bus *result);
200 extern void afb_hook_ditf_vverbose(const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args);
201 extern struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result);
202 extern int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result);
203 extern int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result);
204 extern int afb_hook_ditf_queue_job(const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result);
205 extern void afb_hook_ditf_unstore_req(const struct afb_ditf *ditf, struct afb_stored_req *sreq);
206 extern void afb_hook_ditf_require_api(const struct afb_ditf *ditf, const char *name, int initialized);
207 extern int afb_hook_ditf_require_api_result(const struct afb_ditf *ditf, const char *name, int initialized, int result);
208
209 extern int afb_hook_flags_ditf(const char *api);
210 extern struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure);
211 extern struct afb_hook_ditf *afb_hook_addref_ditf(struct afb_hook_ditf *hook);
212 extern void afb_hook_unref_ditf(struct afb_hook_ditf *hook);
213
214 /*********************************************************
215 * section hooking svc (service interface)
216 *********************************************************/
217
218 #define afb_hook_flag_svc_start_before                  0x000001
219 #define afb_hook_flag_svc_start_after                   0x000002
220 #define afb_hook_flag_svc_on_event_before               0x000004
221 #define afb_hook_flag_svc_on_event_after                0x000008
222 #define afb_hook_flag_svc_call                          0x000010
223 #define afb_hook_flag_svc_call_result                   0x000020
224 #define afb_hook_flag_svc_callsync                      0x000040
225 #define afb_hook_flag_svc_callsync_result               0x000080
226
227 #define afb_hook_flags_svc_all          (afb_hook_flag_svc_start_before|afb_hook_flag_svc_start_after\
228                                         |afb_hook_flag_svc_on_event_before|afb_hook_flag_svc_on_event_after\
229                                         |afb_hook_flag_svc_call|afb_hook_flag_svc_call_result\
230                                         |afb_hook_flag_svc_callsync|afb_hook_flag_svc_callsync_result)
231
232 struct afb_hook_svc_itf {
233         void (*hook_svc_start_before)(void *closure, const struct afb_svc *svc);
234         void (*hook_svc_start_after)(void *closure, const struct afb_svc *svc, int status);
235         void (*hook_svc_on_event_before)(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object);
236         void (*hook_svc_on_event_after)(void *closure, const struct afb_svc *svc, const char *event, int eventid, struct json_object *object);
237         void (*hook_svc_call)(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args);
238         void (*hook_svc_call_result)(void *closure, const struct afb_svc *svc, int status, struct json_object *result);
239         void (*hook_svc_callsync)(void *closure, const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args);
240         void (*hook_svc_callsync_result)(void *closure, const struct afb_svc *svc, int status, struct json_object *result);
241 };
242
243 extern void afb_hook_svc_start_before(const struct afb_svc *svc);
244 extern int afb_hook_svc_start_after(const struct afb_svc *svc, int status);
245 extern void afb_hook_svc_on_event_before(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object);
246 extern void afb_hook_svc_on_event_after(const struct afb_svc *svc, const char *event, int eventid, struct json_object *object);
247 extern void afb_hook_svc_call(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args);
248 extern void afb_hook_svc_call_result(const struct afb_svc *svc, int status, struct json_object *result);
249 extern void afb_hook_svc_callsync(const struct afb_svc *svc, const char *api, const char *verb, struct json_object *args);
250 extern int afb_hook_svc_callsync_result(const struct afb_svc *svc, int status, struct json_object *result);
251
252 extern int afb_hook_flags_svc(const char *api);
253 extern struct afb_hook_svc *afb_hook_create_svc(const char *api, int flags, struct afb_hook_svc_itf *itf, void *closure);
254 extern struct afb_hook_svc *afb_hook_addref_svc(struct afb_hook_svc *hook);
255 extern void afb_hook_unref_svc(struct afb_hook_svc *hook);
256
257 /*********************************************************
258 * section hooking evt (event interface)
259 *********************************************************/
260
261 #define afb_hook_flag_evt_create                        0x000001
262 #define afb_hook_flag_evt_push_before                   0x000002
263 #define afb_hook_flag_evt_push_after                    0x000004
264 #define afb_hook_flag_evt_broadcast_before              0x000008
265 #define afb_hook_flag_evt_broadcast_after               0x000010
266 #define afb_hook_flag_evt_name                          0x000020
267 #define afb_hook_flag_evt_drop                          0x000040
268
269 #define afb_hook_flags_evt_common       (afb_hook_flag_evt_push_before|afb_hook_flag_evt_broadcast_before)
270 #define afb_hook_flags_evt_extra        (afb_hook_flags_evt_common\
271                                         |afb_hook_flag_evt_push_after|afb_hook_flag_evt_broadcast_after\
272                                         |afb_hook_flag_evt_create|afb_hook_flag_evt_drop)
273 #define afb_hook_flags_evt_all          (afb_hook_flags_evt_extra|afb_hook_flag_evt_name)
274
275 struct afb_hook_evt_itf {
276         void (*hook_evt_create)(void *closure, const char *evt, int id);
277         void (*hook_evt_push_before)(void *closure, const char *evt, int id, struct json_object *obj);
278         void (*hook_evt_push_after)(void *closure, const char *evt, int id, struct json_object *obj, int result);
279         void (*hook_evt_broadcast_before)(void *closure, const char *evt, int id, struct json_object *obj);
280         void (*hook_evt_broadcast_after)(void *closure, const char *evt, int id, struct json_object *obj, int result);
281         void (*hook_evt_name)(void *closure, const char *evt, int id);
282         void (*hook_evt_drop)(void *closure, const char *evt, int id);
283 };
284
285 extern void afb_hook_evt_create(const char *evt, int id);
286 extern void afb_hook_evt_push_before(const char *evt, int id, struct json_object *obj);
287 extern int afb_hook_evt_push_after(const char *evt, int id, struct json_object *obj, int result);
288 extern void afb_hook_evt_broadcast_before(const char *evt, int id, struct json_object *obj);
289 extern int afb_hook_evt_broadcast_after(const char *evt, int id, struct json_object *obj, int result);
290 extern void afb_hook_evt_name(const char *evt, int id);
291 extern void afb_hook_evt_drop(const char *evt, int id);
292
293 extern int afb_hook_flags_evt(const char *name);
294 extern struct afb_hook_evt *afb_hook_create_evt(const char *name, int flags, struct afb_hook_evt_itf *itf, void *closure);
295 extern struct afb_hook_evt *afb_hook_addref_evt(struct afb_hook_evt *hook);
296 extern void afb_hook_unref_evt(struct afb_hook_evt *hook);
297