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 (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, "?", fmt, args);
62 static struct afb_event event_make_cb(void *closure, const char *name)
66 struct afb_ditf *ditf = closure;
68 /* makes the event name */
69 plen = strlen(ditf->api);
71 event = alloca(nlen + plen + 2);
72 memcpy(event, ditf->api, plen);
74 memcpy(event + plen + 1, name, nlen + 1);
76 /* create the event */
77 return afb_evt_create_event(event);
80 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
84 struct afb_ditf *ditf = closure;
86 /* makes the event name */
87 plen = strlen(ditf->api);
89 event = alloca(nlen + plen + 2);
90 memcpy(event, ditf->api, plen);
92 memcpy(event + plen + 1, name, nlen + 1);
94 /* broadcast the event */
95 return afb_evt_broadcast(event, object);
98 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
100 return afb_common_rootdir_open_locale(filename, flags, locale);
103 static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
105 return jobs_queue(group, timeout, callback, argument);
108 static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
110 return afb_xreq_unstore(sreq);
113 static int require_api_cb(void *closure, const char *name, int initialized)
116 return (initialized ? afb_apiset_get_started : afb_apiset_get)(main_apiset, name, &a);
119 /**********************************************
121 **********************************************/
122 static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
124 struct afb_ditf *ditf = closure;
127 vverbose_cb(closure, level, file, line, function, fmt, args);
128 afb_hook_ditf_vverbose(ditf, level, file, line, function, fmt, ap);
132 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
134 hooked_vverbose_cb(closure, level, file, line, "?", fmt, args);
137 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
139 struct afb_ditf *ditf = closure;
140 struct afb_event r = event_make_cb(closure, name);
141 return afb_hook_ditf_event_make(ditf, name, r);
144 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
147 struct afb_ditf *ditf = closure;
148 json_object_get(object);
149 afb_hook_ditf_event_broadcast_before(ditf, name, json_object_get(object));
150 r = event_broadcast_cb(closure, name, object);
151 afb_hook_ditf_event_broadcast_after(ditf, name, object, r);
152 json_object_put(object);
156 static struct sd_event *hooked_get_event_loop(void *closure)
158 struct afb_ditf *ditf = closure;
159 struct sd_event *r = afb_common_get_event_loop();
160 return afb_hook_ditf_get_event_loop(ditf, r);
163 static struct sd_bus *hooked_get_user_bus(void *closure)
165 struct afb_ditf *ditf = closure;
166 struct sd_bus *r = afb_common_get_user_bus();
167 return afb_hook_ditf_get_user_bus(ditf, r);
170 static struct sd_bus *hooked_get_system_bus(void *closure)
172 struct afb_ditf *ditf = closure;
173 struct sd_bus *r = afb_common_get_system_bus();
174 return afb_hook_ditf_get_system_bus(ditf, r);
177 static int hooked_rootdir_get_fd(void *closure)
179 struct afb_ditf *ditf = closure;
180 int r = afb_common_rootdir_get_fd();
181 return afb_hook_ditf_rootdir_get_fd(ditf, r);
184 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
186 struct afb_ditf *ditf = closure;
187 int r = rootdir_open_locale_cb(closure, filename, flags, locale);
188 return afb_hook_ditf_rootdir_open_locale(ditf, filename, flags, locale, r);
191 static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
193 struct afb_ditf *ditf = closure;
194 int r = queue_job_cb(closure, callback, argument, group, timeout);
195 return afb_hook_ditf_queue_job(ditf, callback, argument, group, timeout, r);
198 static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req *sreq)
200 struct afb_ditf *ditf = closure;
201 afb_hook_ditf_unstore_req(ditf, sreq);
202 return unstore_req_cb(closure, sreq);
205 static int hooked_require_api_cb(void *closure, const char *name, int initialized)
208 struct afb_ditf *ditf = closure;
209 afb_hook_ditf_require_api(ditf, name, initialized);
210 result = require_api_cb(closure, name, initialized);
211 return afb_hook_ditf_require_api_result(ditf, name, initialized, result);
214 /**********************************************
216 **********************************************/
217 static const struct afb_daemon_itf daemon_itf = {
218 .vverbose_v1 = old_vverbose_cb,
219 .vverbose_v2 = vverbose_cb,
220 .event_make = event_make_cb,
221 .event_broadcast = event_broadcast_cb,
222 .get_event_loop = afb_common_get_event_loop,
223 .get_user_bus = afb_common_get_user_bus,
224 .get_system_bus = afb_common_get_system_bus,
225 .rootdir_get_fd = afb_common_rootdir_get_fd,
226 .rootdir_open_locale = rootdir_open_locale_cb,
227 .queue_job = queue_job_cb,
228 .unstore_req = unstore_req_cb,
229 .require_api = require_api_cb
232 static const struct afb_daemon_itf hooked_daemon_itf = {
233 .vverbose_v1 = hooked_old_vverbose_cb,
234 .vverbose_v2 = hooked_vverbose_cb,
235 .event_make = hooked_event_make_cb,
236 .event_broadcast = hooked_event_broadcast_cb,
237 .get_event_loop = hooked_get_event_loop,
238 .get_user_bus = hooked_get_user_bus,
239 .get_system_bus = hooked_get_system_bus,
240 .rootdir_get_fd = hooked_rootdir_get_fd,
241 .rootdir_open_locale = hooked_rootdir_open_locale_cb,
242 .queue_job = hooked_queue_job_cb,
243 .unstore_req = hooked_unstore_req_cb,
244 .require_api = hooked_require_api_cb
247 void afb_ditf_init_v2(struct afb_ditf *ditf, const char *api, struct afb_binding_data_v2 *data)
251 data->daemon.closure = ditf;
252 afb_ditf_rename(ditf, api);
255 void afb_ditf_init_v1(struct afb_ditf *ditf, const char *api, struct afb_binding_interface_v1 *itf)
259 itf->verbosity = verbosity;
260 itf->mode = AFB_MODE_LOCAL;
261 itf->daemon.closure = ditf;
262 afb_ditf_rename(ditf, api);
265 void afb_ditf_rename(struct afb_ditf *ditf, const char *api)
268 afb_ditf_update_hook(ditf);
271 void afb_ditf_update_hook(struct afb_ditf *ditf)
273 int hooked = !!afb_hook_flags_ditf(ditf->api);
274 switch (ditf->version) {
276 ditf->v1->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;
280 ditf->v2->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;