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