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"
36 /**********************************************
38 **********************************************/
39 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
42 struct afb_ditf *ditf = closure;
44 if (vasprintf(&p, fmt, args) < 0)
45 vverbose(level, file, line, function, fmt, args);
47 verbose(level, file, line, function, "%s {binding %s}", p, ditf->prefix);
52 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
54 vverbose_cb(closure, level, file, line, "?", fmt, args);
57 static struct afb_event event_make_cb(void *closure, const char *name)
61 struct afb_ditf *ditf = closure;
63 /* makes the event name */
64 plen = strlen(ditf->prefix);
66 event = alloca(nlen + plen + 2);
67 memcpy(event, ditf->prefix, plen);
69 memcpy(event + plen + 1, name, nlen + 1);
71 /* create the event */
72 return afb_evt_create_event(event);
75 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
79 struct afb_ditf *ditf = closure;
81 /* makes the event name */
82 plen = strlen(ditf->prefix);
84 event = alloca(nlen + plen + 2);
85 memcpy(event, ditf->prefix, plen);
87 memcpy(event + plen + 1, name, nlen + 1);
89 /* broadcast the event */
90 return afb_evt_broadcast(event, object);
93 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
95 return afb_common_rootdir_open_locale(filename, flags, locale);
98 static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
100 return jobs_queue(group, timeout, callback, argument);
103 /**********************************************
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)
108 struct afb_ditf *ditf = closure;
109 vverbose_cb(closure, level, file, line, function, fmt, args);
110 afb_hook_ditf_vverbose(ditf, level, file, line, function, fmt, args);
113 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
115 hooked_vverbose_cb(closure, level, file, line, "?", fmt, args);
118 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
120 struct afb_ditf *ditf = closure;
121 struct afb_event r = event_make_cb(closure, name);
122 return afb_hook_ditf_event_make(ditf, name, r);
125 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
128 struct afb_ditf *ditf = closure;
129 json_object_get(object);
130 afb_hook_ditf_event_broadcast_before(ditf, name, json_object_get(object));
131 r = event_broadcast_cb(closure, name, object);
132 afb_hook_ditf_event_broadcast_after(ditf, name, object, r);
133 json_object_put(object);
137 static struct sd_event *hooked_get_event_loop(void *closure)
139 struct afb_ditf *ditf = closure;
140 struct sd_event *r = afb_common_get_event_loop();
141 return afb_hook_ditf_get_event_loop(ditf, r);
144 static struct sd_bus *hooked_get_user_bus(void *closure)
146 struct afb_ditf *ditf = closure;
147 struct sd_bus *r = afb_common_get_user_bus();
148 return afb_hook_ditf_get_user_bus(ditf, r);
151 static struct sd_bus *hooked_get_system_bus(void *closure)
153 struct afb_ditf *ditf = closure;
154 struct sd_bus *r = afb_common_get_system_bus();
155 return afb_hook_ditf_get_system_bus(ditf, r);
158 static int hooked_rootdir_get_fd(void *closure)
160 struct afb_ditf *ditf = closure;
161 int r = afb_common_rootdir_get_fd();
162 return afb_hook_ditf_rootdir_get_fd(ditf, r);
165 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
167 struct afb_ditf *ditf = closure;
168 int r = rootdir_open_locale_cb(closure, filename, flags, locale);
169 return afb_hook_ditf_rootdir_open_locale(ditf, filename, flags, locale, r);
172 static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
174 struct afb_ditf *ditf = closure;
175 int r = queue_job_cb(closure, callback, argument, group, timeout);
176 return afb_hook_ditf_queue_job(ditf, callback, argument, group, timeout, r);
179 static const struct afb_daemon_itf daemon_itf = {
180 .vverbose_v1 = old_vverbose_cb,
181 .vverbose_v2 = vverbose_cb,
182 .event_make = event_make_cb,
183 .event_broadcast = event_broadcast_cb,
184 .get_event_loop = afb_common_get_event_loop,
185 .get_user_bus = afb_common_get_user_bus,
186 .get_system_bus = afb_common_get_system_bus,
187 .rootdir_get_fd = afb_common_rootdir_get_fd,
188 .rootdir_open_locale = rootdir_open_locale_cb,
189 .queue_job = queue_job_cb
192 static const struct afb_daemon_itf hooked_daemon_itf = {
193 .vverbose_v1 = hooked_old_vverbose_cb,
194 .vverbose_v2 = hooked_vverbose_cb,
195 .event_make = hooked_event_make_cb,
196 .event_broadcast = hooked_event_broadcast_cb,
197 .get_event_loop = hooked_get_event_loop,
198 .get_user_bus = hooked_get_user_bus,
199 .get_system_bus = hooked_get_system_bus,
200 .rootdir_get_fd = hooked_rootdir_get_fd,
201 .rootdir_open_locale = hooked_rootdir_open_locale_cb,
202 .queue_job = hooked_queue_job_cb
205 void afb_ditf_init_v2(struct afb_ditf *ditf, const char *prefix, struct afb_binding_data_v2 *data)
209 data->daemon.closure = ditf;
210 afb_ditf_rename(ditf, prefix);
213 void afb_ditf_init_v1(struct afb_ditf *ditf, const char *prefix, struct afb_binding_interface_v1 *itf)
217 itf->verbosity = verbosity;
218 itf->mode = AFB_MODE_LOCAL;
219 itf->daemon.closure = ditf;
220 afb_ditf_rename(ditf, prefix);
223 void afb_ditf_rename(struct afb_ditf *ditf, const char *prefix)
225 ditf->prefix = prefix;
226 afb_ditf_update_hook(ditf);
229 void afb_ditf_update_hook(struct afb_ditf *ditf)
231 int hooked = !!afb_hook_flags_ditf(ditf->prefix);
232 switch (ditf->version) {
234 ditf->v1->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;
238 ditf->v2->daemon.itf = hooked ? &hooked_daemon_itf : &daemon_itf;