3fc09e924ecaafae3306ee0866a1b7145999742a
[src/app-framework-binder.git] / src / afb-ditf.c
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 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include <json-c/json.h>
25
26 #include <afb/afb-binding-v1.h>
27 #include <afb/afb-binding-v2.h>
28
29 #include "afb-ditf.h"
30 #include "afb-evt.h"
31 #include "afb-common.h"
32 #include "afb-xreq.h"
33 #include "afb-api.h"
34 #include "afb-apiset.h"
35 #include "afb-hook.h"
36 #include "jobs.h"
37 #include "verbose.h"
38
39 extern struct afb_apiset *main_apiset;
40
41 /**********************************************
42 * normal flow
43 **********************************************/
44 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
45 {
46         char *p;
47         struct afb_ditf *ditf = closure;
48
49         if (vasprintf(&p, fmt, args) < 0)
50                 vverbose(level, file, line, function, fmt, args);
51         else {
52                 verbose(level, file, line, function, "[API %s] %s", ditf->api, p);
53                 free(p);
54         }
55 }
56
57 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
58 {
59         vverbose_cb(closure, level, file, line, "?", fmt, args);
60 }
61
62 static struct afb_event event_make_cb(void *closure, const char *name)
63 {
64         size_t plen, nlen;
65         char *event;
66         struct afb_ditf *ditf = closure;
67
68         /* makes the event name */
69         plen = strlen(ditf->api);
70         nlen = strlen(name);
71         event = alloca(nlen + plen + 2);
72         memcpy(event, ditf->api, plen);
73         event[plen] = '/';
74         memcpy(event + plen + 1, name, nlen + 1);
75
76         /* create the event */
77         return afb_evt_create_event(event);
78 }
79
80 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
81 {
82         size_t plen, nlen;
83         char *event;
84         struct afb_ditf *ditf = closure;
85
86         /* makes the event name */
87         plen = strlen(ditf->api);
88         nlen = strlen(name);
89         event = alloca(nlen + plen + 2);
90         memcpy(event, ditf->api, plen);
91         event[plen] = '/';
92         memcpy(event + plen + 1, name, nlen + 1);
93
94         /* broadcast the event */
95         return afb_evt_broadcast(event, object);
96 }
97
98 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
99 {
100         return afb_common_rootdir_open_locale(filename, flags, locale);
101 }
102
103 static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
104 {
105         return jobs_queue(group, timeout, callback, argument);
106 }
107
108 static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
109 {
110         return afb_xreq_unstore(sreq);
111 }
112
113 static int require_api_cb(void *closure, const char *name, int initialized)
114 {
115         return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(main_apiset, name, 1);
116 }
117
118 /**********************************************
119 * hooked flow
120 **********************************************/
121 static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
122 {
123         struct afb_ditf *ditf = closure;
124         va_list ap;
125         va_copy(ap, args);
126         vverbose_cb(closure, level, file, line, function, fmt, args);
127         afb_hook_ditf_vverbose(ditf, level, file, line, function, fmt, ap);
128         va_end(ap);
129 }
130
131 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
132 {
133         hooked_vverbose_cb(closure, level, file, line, "?", fmt, args);
134 }
135
136 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
137 {
138         struct afb_ditf *ditf = closure;
139         struct afb_event r = event_make_cb(closure, name);
140         return afb_hook_ditf_event_make(ditf, name, r);
141 }
142
143 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
144 {
145         int r;
146         struct afb_ditf *ditf = closure;
147         json_object_get(object);
148         afb_hook_ditf_event_broadcast_before(ditf, name, json_object_get(object));
149         r = event_broadcast_cb(closure, name, object);
150         afb_hook_ditf_event_broadcast_after(ditf, name, object, r);
151         json_object_put(object);
152         return r;
153 }
154
155 static struct sd_event *hooked_get_event_loop(void *closure)
156 {
157         struct afb_ditf *ditf = closure;
158         struct sd_event *r = afb_common_get_event_loop();
159         return afb_hook_ditf_get_event_loop(ditf, r);
160 }
161
162 static struct sd_bus *hooked_get_user_bus(void *closure)
163 {
164         struct afb_ditf *ditf = closure;
165         struct sd_bus *r = afb_common_get_user_bus();
166         return afb_hook_ditf_get_user_bus(ditf, r);
167 }
168
169 static struct sd_bus *hooked_get_system_bus(void *closure)
170 {
171         struct afb_ditf *ditf = closure;
172         struct sd_bus *r = afb_common_get_system_bus();
173         return afb_hook_ditf_get_system_bus(ditf, r);
174 }
175
176 static int hooked_rootdir_get_fd(void *closure)
177 {
178         struct afb_ditf *ditf = closure;
179         int r = afb_common_rootdir_get_fd();
180         return afb_hook_ditf_rootdir_get_fd(ditf, r);
181 }
182
183 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
184 {
185         struct afb_ditf *ditf = closure;
186         int r = rootdir_open_locale_cb(closure, filename, flags, locale);
187         return afb_hook_ditf_rootdir_open_locale(ditf, filename, flags, locale, r);
188 }
189
190 static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
191 {
192         struct afb_ditf *ditf = closure;
193         int r = queue_job_cb(closure, callback, argument, group, timeout);
194         return afb_hook_ditf_queue_job(ditf, callback, argument, group, timeout, r);
195 }
196
197 static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req *sreq)
198 {
199         struct afb_ditf *ditf = closure;
200         afb_hook_ditf_unstore_req(ditf, sreq);
201         return unstore_req_cb(closure, sreq);
202 }
203
204 static int hooked_require_api_cb(void *closure, const char *name, int initialized)
205 {
206         int result;
207         struct afb_ditf *ditf = closure;
208         afb_hook_ditf_require_api(ditf, name, initialized);
209         result = require_api_cb(closure, name, initialized);
210         return afb_hook_ditf_require_api_result(ditf, name, initialized, result);
211 }
212
213 /**********************************************
214 * vectors
215 **********************************************/
216 static const struct afb_daemon_itf daemon_itf = {
217         .vverbose_v1 = old_vverbose_cb,
218         .vverbose_v2 = vverbose_cb,
219         .event_make = event_make_cb,
220         .event_broadcast = event_broadcast_cb,
221         .get_event_loop = afb_common_get_event_loop,
222         .get_user_bus = afb_common_get_user_bus,
223         .get_system_bus = afb_common_get_system_bus,
224         .rootdir_get_fd = afb_common_rootdir_get_fd,
225         .rootdir_open_locale = rootdir_open_locale_cb,
226         .queue_job = queue_job_cb,
227         .unstore_req = unstore_req_cb,
228         .require_api = require_api_cb
229 };
230
231 static const struct afb_daemon_itf hooked_daemon_itf = {
232         .vverbose_v1 = hooked_old_vverbose_cb,
233         .vverbose_v2 = hooked_vverbose_cb,
234         .event_make = hooked_event_make_cb,
235         .event_broadcast = hooked_event_broadcast_cb,
236         .get_event_loop = hooked_get_event_loop,
237         .get_user_bus = hooked_get_user_bus,
238         .get_system_bus = hooked_get_system_bus,
239         .rootdir_get_fd = hooked_rootdir_get_fd,
240         .rootdir_open_locale = hooked_rootdir_open_locale_cb,
241         .queue_job = hooked_queue_job_cb,
242         .unstore_req = hooked_unstore_req_cb,
243         .require_api = hooked_require_api_cb
244 };
245
246 void afb_ditf_init_v2(struct afb_ditf *ditf, const char *api, struct afb_binding_data_v2 *data)
247 {
248         ditf->version = 2;
249         ditf->v2 = data;
250         data->daemon.closure = ditf;
251         afb_ditf_rename(ditf, api);
252 }
253
254 void afb_ditf_init_v1(struct afb_ditf *ditf, const char *api, struct afb_binding_interface_v1 *itf)
255 {
256         ditf->version = 1;
257         ditf->v1 = itf;
258         itf->verbosity = verbosity;
259         itf->mode = AFB_MODE_LOCAL;
260         itf->daemon.closure = ditf;
261         afb_ditf_rename(ditf, api);
262 }
263
264 void afb_ditf_rename(struct afb_ditf *ditf, const char *api)
265 {
266         ditf->api = api;
267         afb_ditf_update_hook(ditf);
268 }
269
270 void afb_ditf_update_hook(struct afb_ditf *ditf)
271 {
272         int hooked = !!afb_hook_flags_ditf(ditf->api);
273         switch (ditf->version) {
274         case 1:
275                 ditf->v1->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;
276                 break;
277         default:
278         case 2:
279                 ditf->v2->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;
280                 break;
281         }
282 }
283