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