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