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.
19 #define NO_BINDING_VERBOSE_MACRO
24 #include <json-c/json.h>
26 #include <afb/afb-binding.h>
30 #include "afb-common.h"
34 /**********************************************
36 **********************************************/
37 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
40 struct afb_ditf *ditf = closure;
42 if (vasprintf(&p, fmt, args) < 0)
43 vverbose(level, file, line, function, fmt, args);
45 verbose(level, file, line, function, "%s {binding %s}", p, ditf->prefix);
50 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
52 vverbose_cb(closure, level, file, line, "?", fmt, args);
55 static struct afb_event event_make_cb(void *closure, const char *name)
59 struct afb_ditf *ditf = closure;
61 /* makes the event name */
62 plen = strlen(ditf->prefix);
64 event = alloca(nlen + plen + 2);
65 memcpy(event, ditf->prefix, plen);
67 memcpy(event + plen + 1, name, nlen + 1);
69 /* create the event */
70 return afb_evt_create_event(event);
73 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
77 struct afb_ditf *ditf = closure;
79 /* makes the event name */
80 plen = strlen(ditf->prefix);
82 event = alloca(nlen + plen + 2);
83 memcpy(event, ditf->prefix, plen);
85 memcpy(event + plen + 1, name, nlen + 1);
87 /* broadcast the event */
88 return afb_evt_broadcast(event, object);
91 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
93 return afb_common_rootdir_open_locale(filename, flags, locale);
96 /**********************************************
98 **********************************************/
99 static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
101 struct afb_ditf *ditf = closure;
102 vverbose_cb(closure, level, file, line, function, fmt, args);
103 afb_hook_ditf_vverbose(ditf, level, file, line, function, fmt, args);
104 vverbose_cb(closure, level, file, line, function, fmt, args);
107 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
109 struct afb_ditf *ditf = closure;
110 old_vverbose_cb(closure, level, file, line, fmt, args);
111 afb_hook_ditf_vverbose(ditf, level, file, line, "", fmt, args);
114 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
116 struct afb_ditf *ditf = closure;
117 struct afb_event r = event_make_cb(closure, name);
118 return afb_hook_ditf_event_make(ditf, name, r);
121 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
124 struct afb_ditf *ditf = closure;
125 json_object_get(object);
126 afb_hook_ditf_event_broadcast_before(ditf, name, json_object_get(object));
127 r = event_broadcast_cb(closure, name, object);
128 afb_hook_ditf_event_broadcast_after(ditf, name, object, r);
129 json_object_put(object);
133 static struct sd_event *hooked_get_event_loop(void *closure)
135 struct afb_ditf *ditf = closure;
136 struct sd_event *r = afb_common_get_event_loop();
137 return afb_hook_ditf_get_event_loop(ditf, r);
140 static struct sd_bus *hooked_get_user_bus(void *closure)
142 struct afb_ditf *ditf = closure;
143 struct sd_bus *r = afb_common_get_user_bus();
144 return afb_hook_ditf_get_user_bus(ditf, r);
147 static struct sd_bus *hooked_get_system_bus(void *closure)
149 struct afb_ditf *ditf = closure;
150 struct sd_bus *r = afb_common_get_system_bus();
151 return afb_hook_ditf_get_system_bus(ditf, r);
154 static int hooked_rootdir_get_fd(void *closure)
156 struct afb_ditf *ditf = closure;
157 int r = afb_common_rootdir_get_fd();
158 return afb_hook_ditf_rootdir_get_fd(ditf, r);
161 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
163 struct afb_ditf *ditf = closure;
164 int r = rootdir_open_locale_cb(closure, filename, flags, locale);
165 return afb_hook_ditf_rootdir_open_locale(ditf, filename, flags, locale, r);
168 static const struct afb_daemon_itf daemon_itf = {
169 .vverbose = old_vverbose_cb,
170 .event_make = event_make_cb,
171 .event_broadcast = event_broadcast_cb,
172 .get_event_loop = afb_common_get_event_loop,
173 .get_user_bus = afb_common_get_user_bus,
174 .get_system_bus = afb_common_get_system_bus,
175 .rootdir_get_fd = afb_common_rootdir_get_fd,
176 .rootdir_open_locale = rootdir_open_locale_cb
179 static const struct afb_daemon_itf hooked_daemon_itf = {
180 .vverbose = hooked_old_vverbose_cb,
181 .event_make = hooked_event_make_cb,
182 .event_broadcast = hooked_event_broadcast_cb,
183 .get_event_loop = hooked_get_event_loop,
184 .get_user_bus = hooked_get_user_bus,
185 .get_system_bus = hooked_get_system_bus,
186 .rootdir_get_fd = hooked_rootdir_get_fd,
187 .rootdir_open_locale = hooked_rootdir_open_locale_cb
190 void afb_ditf_init(struct afb_ditf *ditf, const char *prefix)
192 ditf->interface.verbosity = verbosity;
193 ditf->interface.mode = AFB_MODE_LOCAL;
194 ditf->interface.daemon.closure = ditf;
195 afb_ditf_rename(ditf, prefix);
198 void afb_ditf_rename(struct afb_ditf *ditf, const char *prefix)
200 ditf->prefix = prefix;
201 afb_ditf_update_hook(ditf);
204 void afb_ditf_update_hook(struct afb_ditf *ditf)
206 if (afb_hook_flags_ditf(ditf->prefix))
207 ditf->interface.daemon.itf = &hooked_daemon_itf;
209 ditf->interface.daemon.itf = &daemon_itf;