2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <json-c/json.h>
26 #include <afb/afb-binding-v1.h>
27 #include <afb/afb-binding-v2.h>
31 #include "afb-common.h"
34 #include "afb-apiset.h"
39 extern struct afb_apiset *main_apiset;
41 /**********************************************
43 **********************************************/
44 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
47 struct afb_ditf *ditf = closure;
49 if (!fmt || vasprintf(&p, fmt, args) < 0)
50 vverbose(level, file, line, function, fmt, args);
52 verbose(level, file, line, function, "[API %s] %s", ditf->api, p);
57 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
59 vverbose_cb(closure, level, file, line, NULL, fmt, args);
62 static struct afb_event event_make_cb(void *closure, const char *name)
66 struct afb_ditf *ditf = closure;
68 /* check daemon state */
69 if (ditf->state == Daemon_Pre_Init) {
71 ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", ditf->api, name);
73 return (struct afb_event){ .itf = NULL, .closure = NULL };
76 /* makes the event name */
77 plen = strlen(ditf->api);
79 event = alloca(nlen + plen + 2);
80 memcpy(event, ditf->api, plen);
82 memcpy(event + plen + 1, name, nlen + 1);
84 /* create the event */
85 return afb_evt_create_event(event);
88 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
92 struct afb_ditf *ditf = closure;
94 /* check daemon state */
95 if (ditf->state == Daemon_Pre_Init) {
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));
102 /* makes the event name */
103 plen = strlen(ditf->api);
105 event = alloca(nlen + plen + 2);
106 memcpy(event, ditf->api, plen);
108 memcpy(event + plen + 1, name, nlen + 1);
110 /* broadcast the event */
111 return afb_evt_broadcast(event, object);
114 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
116 return afb_common_rootdir_open_locale(filename, flags, locale);
119 static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
121 return jobs_queue(group, timeout, callback, argument);
124 static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
126 return afb_xreq_unstore(sreq);
129 static int require_api_cb(void *closure, const char *name, int initialized)
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);
137 return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(main_apiset, name, 1);
140 static int rename_api_cb(void *closure, const char *name)
142 struct afb_ditf *ditf = closure;
143 if (ditf->state != Daemon_Pre_Init) {
144 ERROR("[API %s] Bad call to 'afb_daemon_rename(%s)', must be in PreInit", ditf->api, name);
148 if (!afb_api_is_valid_name(name)) {
149 ERROR("[API %s] Can't rename to %s: bad API name", ditf->api, name);
153 NOTICE("[API %s] renamed to [API %s]", ditf->api, name);
154 afb_ditf_rename(ditf, name);
158 /**********************************************
160 **********************************************/
161 static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
163 struct afb_ditf *ditf = closure;
166 vverbose_cb(closure, level, file, line, function, fmt, args);
167 afb_hook_ditf_vverbose(ditf, level, file, line, function, fmt, ap);
171 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
173 hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args);
176 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
178 struct afb_ditf *ditf = closure;
179 struct afb_event r = event_make_cb(closure, name);
180 return afb_hook_ditf_event_make(ditf, name, r);
183 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
186 struct afb_ditf *ditf = closure;
187 json_object_get(object);
188 afb_hook_ditf_event_broadcast_before(ditf, name, json_object_get(object));
189 r = event_broadcast_cb(closure, name, object);
190 afb_hook_ditf_event_broadcast_after(ditf, name, object, r);
191 json_object_put(object);
195 static struct sd_event *hooked_get_event_loop(void *closure)
197 struct afb_ditf *ditf = closure;
198 struct sd_event *r = afb_common_get_event_loop();
199 return afb_hook_ditf_get_event_loop(ditf, r);
202 static struct sd_bus *hooked_get_user_bus(void *closure)
204 struct afb_ditf *ditf = closure;
205 struct sd_bus *r = afb_common_get_user_bus();
206 return afb_hook_ditf_get_user_bus(ditf, r);
209 static struct sd_bus *hooked_get_system_bus(void *closure)
211 struct afb_ditf *ditf = closure;
212 struct sd_bus *r = afb_common_get_system_bus();
213 return afb_hook_ditf_get_system_bus(ditf, r);
216 static int hooked_rootdir_get_fd(void *closure)
218 struct afb_ditf *ditf = closure;
219 int r = afb_common_rootdir_get_fd();
220 return afb_hook_ditf_rootdir_get_fd(ditf, r);
223 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
225 struct afb_ditf *ditf = closure;
226 int r = rootdir_open_locale_cb(closure, filename, flags, locale);
227 return afb_hook_ditf_rootdir_open_locale(ditf, filename, flags, locale, r);
230 static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
232 struct afb_ditf *ditf = closure;
233 int r = queue_job_cb(closure, callback, argument, group, timeout);
234 return afb_hook_ditf_queue_job(ditf, callback, argument, group, timeout, r);
237 static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req *sreq)
239 struct afb_ditf *ditf = closure;
240 afb_hook_ditf_unstore_req(ditf, sreq);
241 return unstore_req_cb(closure, sreq);
244 static int hooked_require_api_cb(void *closure, const char *name, int initialized)
247 struct afb_ditf *ditf = closure;
248 afb_hook_ditf_require_api(ditf, name, initialized);
249 result = require_api_cb(closure, name, initialized);
250 return afb_hook_ditf_require_api_result(ditf, name, initialized, result);
253 static int hooked_rename_api_cb(void *closure, const char *name)
255 struct afb_ditf *ditf = closure;
256 const char *oldname = ditf->api;
257 int result = rename_api_cb(closure, name);
258 return afb_hook_ditf_rename_api(ditf, oldname, name, result);
261 /**********************************************
263 **********************************************/
264 static const struct afb_daemon_itf daemon_itf = {
265 .vverbose_v1 = old_vverbose_cb,
266 .vverbose_v2 = vverbose_cb,
267 .event_make = event_make_cb,
268 .event_broadcast = event_broadcast_cb,
269 .get_event_loop = afb_common_get_event_loop,
270 .get_user_bus = afb_common_get_user_bus,
271 .get_system_bus = afb_common_get_system_bus,
272 .rootdir_get_fd = afb_common_rootdir_get_fd,
273 .rootdir_open_locale = rootdir_open_locale_cb,
274 .queue_job = queue_job_cb,
275 .unstore_req = unstore_req_cb,
276 .require_api = require_api_cb,
277 .rename_api = rename_api_cb
280 static const struct afb_daemon_itf hooked_daemon_itf = {
281 .vverbose_v1 = hooked_old_vverbose_cb,
282 .vverbose_v2 = hooked_vverbose_cb,
283 .event_make = hooked_event_make_cb,
284 .event_broadcast = hooked_event_broadcast_cb,
285 .get_event_loop = hooked_get_event_loop,
286 .get_user_bus = hooked_get_user_bus,
287 .get_system_bus = hooked_get_system_bus,
288 .rootdir_get_fd = hooked_rootdir_get_fd,
289 .rootdir_open_locale = hooked_rootdir_open_locale_cb,
290 .queue_job = hooked_queue_job_cb,
291 .unstore_req = hooked_unstore_req_cb,
292 .require_api = hooked_require_api_cb,
293 .rename_api = hooked_rename_api_cb
296 void afb_ditf_init_v2(struct afb_ditf *ditf, const char *api, struct afb_binding_data_v2 *data)
299 ditf->state = Daemon_Pre_Init;
301 data->daemon.closure = ditf;
302 afb_ditf_rename(ditf, api);
305 void afb_ditf_init_v1(struct afb_ditf *ditf, const char *api, struct afb_binding_interface_v1 *itf)
308 ditf->state = Daemon_Pre_Init;
310 itf->verbosity = verbosity;
311 itf->mode = AFB_MODE_LOCAL;
312 itf->daemon.closure = ditf;
313 afb_ditf_rename(ditf, api);
316 void afb_ditf_rename(struct afb_ditf *ditf, const char *api)
319 afb_ditf_update_hook(ditf);
322 void afb_ditf_update_hook(struct afb_ditf *ditf)
324 int hooked = !!afb_hook_flags_ditf(ditf->api);
325 switch (ditf->version) {
327 ditf->v1->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;
331 ditf->v2->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;