a7f97ddc25ab7354401fee2aab45ec780c66042f
[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 (!fmt || 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, NULL, 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         /* check daemon state */
69         if (ditf->state == Daemon_Pre_Init) {
70                 ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", ditf->api, name);
71                 errno = EINVAL;
72                 return (struct afb_event){ .itf = NULL, .closure = NULL };
73         }
74
75         /* makes the event name */
76         plen = strlen(ditf->api);
77         nlen = strlen(name);
78         event = alloca(nlen + plen + 2);
79         memcpy(event, ditf->api, plen);
80         event[plen] = '/';
81         memcpy(event + plen + 1, name, nlen + 1);
82
83         /* create the event */
84         return afb_evt_create_event(event);
85 }
86
87 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
88 {
89         size_t plen, nlen;
90         char *event;
91         struct afb_ditf *ditf = closure;
92
93         /* check daemon state */
94         if (ditf->state == Daemon_Pre_Init) {
95                 ERROR("[API %s] Bad call to 'afb_daemon_event_broadcast(%s, %s)', must not be in PreInit", ditf->api, name, json_object_to_json_string(object));
96                 errno = EINVAL;
97                 return 0;
98         }
99
100         /* makes the event name */
101         plen = strlen(ditf->api);
102         nlen = strlen(name);
103         event = alloca(nlen + plen + 2);
104         memcpy(event, ditf->api, plen);
105         event[plen] = '/';
106         memcpy(event + plen + 1, name, nlen + 1);
107
108         /* broadcast the event */
109         return afb_evt_broadcast(event, object);
110 }
111
112 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
113 {
114         return afb_common_rootdir_open_locale(filename, flags, locale);
115 }
116
117 static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
118 {
119         return jobs_queue(group, timeout, callback, argument);
120 }
121
122 static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
123 {
124         return afb_xreq_unstore(sreq);
125 }
126
127 static int require_api_cb(void *closure, const char *name, int initialized)
128 {
129         struct afb_ditf *ditf = closure;
130         if (ditf->state != Daemon_Init) {
131                 ERROR("[API %s] Bad call to 'afb_daemon_require(%s, %d)', must be in Init", ditf->api, name, initialized);
132                 errno = EINVAL;
133                 return -1;
134         }
135         return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(main_apiset, name, 1);
136 }
137
138 static int rename_api_cb(void *closure, const char *name)
139 {
140         struct afb_ditf *ditf = closure;
141         if (ditf->state != Daemon_Pre_Init) {
142                 ERROR("[API %s] Bad call to 'afb_daemon_rename(%s)', must be in PreInit", ditf->api, name);
143                 errno = EINVAL;
144                 return -1;
145         }
146         if (!afb_api_is_valid_name(name)) {
147                 ERROR("[API %s] Can't rename to %s: bad API name", ditf->api, name);
148                 errno = EINVAL;
149                 return -1;
150         }
151         NOTICE("[API %s] renamed to [API %s]", ditf->api, name);
152         afb_ditf_rename(ditf, name);
153         return 0;
154 }
155
156 /**********************************************
157 * hooked flow
158 **********************************************/
159 static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
160 {
161         struct afb_ditf *ditf = closure;
162         va_list ap;
163         va_copy(ap, args);
164         vverbose_cb(closure, level, file, line, function, fmt, args);
165         afb_hook_ditf_vverbose(ditf, level, file, line, function, fmt, ap);
166         va_end(ap);
167 }
168
169 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
170 {
171         hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args);
172 }
173
174 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
175 {
176         struct afb_ditf *ditf = closure;
177         struct afb_event r = event_make_cb(closure, name);
178         return afb_hook_ditf_event_make(ditf, name, r);
179 }
180
181 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
182 {
183         int r;
184         struct afb_ditf *ditf = closure;
185         json_object_get(object);
186         afb_hook_ditf_event_broadcast_before(ditf, name, json_object_get(object));
187         r = event_broadcast_cb(closure, name, object);
188         afb_hook_ditf_event_broadcast_after(ditf, name, object, r);
189         json_object_put(object);
190         return r;
191 }
192
193 static struct sd_event *hooked_get_event_loop(void *closure)
194 {
195         struct afb_ditf *ditf = closure;
196         struct sd_event *r = afb_common_get_event_loop();
197         return afb_hook_ditf_get_event_loop(ditf, r);
198 }
199
200 static struct sd_bus *hooked_get_user_bus(void *closure)
201 {
202         struct afb_ditf *ditf = closure;
203         struct sd_bus *r = afb_common_get_user_bus();
204         return afb_hook_ditf_get_user_bus(ditf, r);
205 }
206
207 static struct sd_bus *hooked_get_system_bus(void *closure)
208 {
209         struct afb_ditf *ditf = closure;
210         struct sd_bus *r = afb_common_get_system_bus();
211         return afb_hook_ditf_get_system_bus(ditf, r);
212 }
213
214 static int hooked_rootdir_get_fd(void *closure)
215 {
216         struct afb_ditf *ditf = closure;
217         int r = afb_common_rootdir_get_fd();
218         return afb_hook_ditf_rootdir_get_fd(ditf, r);
219 }
220
221 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
222 {
223         struct afb_ditf *ditf = closure;
224         int r = rootdir_open_locale_cb(closure, filename, flags, locale);
225         return afb_hook_ditf_rootdir_open_locale(ditf, filename, flags, locale, r);
226 }
227
228 static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
229 {
230         struct afb_ditf *ditf = closure;
231         int r = queue_job_cb(closure, callback, argument, group, timeout);
232         return afb_hook_ditf_queue_job(ditf, callback, argument, group, timeout, r);
233 }
234
235 static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req *sreq)
236 {
237         struct afb_ditf *ditf = closure;
238         afb_hook_ditf_unstore_req(ditf, sreq);
239         return unstore_req_cb(closure, sreq);
240 }
241
242 static int hooked_require_api_cb(void *closure, const char *name, int initialized)
243 {
244         int result;
245         struct afb_ditf *ditf = closure;
246         afb_hook_ditf_require_api(ditf, name, initialized);
247         result = require_api_cb(closure, name, initialized);
248         return afb_hook_ditf_require_api_result(ditf, name, initialized, result);
249 }
250
251 static int hooked_rename_api_cb(void *closure, const char *name)
252 {
253         struct afb_ditf *ditf = closure;
254         const char *oldname = ditf->api;
255         int result = rename_api_cb(closure, name);
256         return afb_hook_ditf_rename_api(ditf, oldname, name, result);
257 }
258
259 /**********************************************
260 * vectors
261 **********************************************/
262 static const struct afb_daemon_itf daemon_itf = {
263         .vverbose_v1 = old_vverbose_cb,
264         .vverbose_v2 = vverbose_cb,
265         .event_make = event_make_cb,
266         .event_broadcast = event_broadcast_cb,
267         .get_event_loop = afb_common_get_event_loop,
268         .get_user_bus = afb_common_get_user_bus,
269         .get_system_bus = afb_common_get_system_bus,
270         .rootdir_get_fd = afb_common_rootdir_get_fd,
271         .rootdir_open_locale = rootdir_open_locale_cb,
272         .queue_job = queue_job_cb,
273         .unstore_req = unstore_req_cb,
274         .require_api = require_api_cb,
275         .rename_api = rename_api_cb
276 };
277
278 static const struct afb_daemon_itf hooked_daemon_itf = {
279         .vverbose_v1 = hooked_old_vverbose_cb,
280         .vverbose_v2 = hooked_vverbose_cb,
281         .event_make = hooked_event_make_cb,
282         .event_broadcast = hooked_event_broadcast_cb,
283         .get_event_loop = hooked_get_event_loop,
284         .get_user_bus = hooked_get_user_bus,
285         .get_system_bus = hooked_get_system_bus,
286         .rootdir_get_fd = hooked_rootdir_get_fd,
287         .rootdir_open_locale = hooked_rootdir_open_locale_cb,
288         .queue_job = hooked_queue_job_cb,
289         .unstore_req = hooked_unstore_req_cb,
290         .require_api = hooked_require_api_cb,
291         .rename_api = hooked_rename_api_cb
292 };
293
294 void afb_ditf_init_v2(struct afb_ditf *ditf, const char *api, struct afb_binding_data_v2 *data)
295 {
296         ditf->version = 2;
297         ditf->state = Daemon_Pre_Init;
298         ditf->v2 = data;
299         data->daemon.closure = ditf;
300         afb_ditf_rename(ditf, api);
301 }
302
303 void afb_ditf_init_v1(struct afb_ditf *ditf, const char *api, struct afb_binding_interface_v1 *itf)
304 {
305         ditf->version = 1;
306         ditf->state = Daemon_Pre_Init;
307         ditf->v1 = itf;
308         itf->verbosity = verbosity;
309         itf->mode = AFB_MODE_LOCAL;
310         itf->daemon.closure = ditf;
311         afb_ditf_rename(ditf, api);
312 }
313
314 void afb_ditf_rename(struct afb_ditf *ditf, const char *api)
315 {
316         ditf->api = api;
317         afb_ditf_update_hook(ditf);
318 }
319
320 void afb_ditf_update_hook(struct afb_ditf *ditf)
321 {
322         int hooked = !!afb_hook_flags_ditf(ditf->api);
323         switch (ditf->version) {
324         case 1:
325                 ditf->v1->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;
326                 break;
327         default:
328         case 2:
329                 ditf->v2->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;
330                 break;
331         }
332 }
333