Add hooking of daemon interface
[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 #define NO_BINDING_VERBOSE_MACRO
20
21 #include <string.h>
22 #include <errno.h>
23
24 #include <json-c/json.h>
25
26 #include <afb/afb-binding.h>
27
28 #include "afb-ditf.h"
29 #include "afb-evt.h"
30 #include "afb-common.h"
31 #include "afb-hook.h"
32 #include "verbose.h"
33
34 /**********************************************
35 * normal flow
36 **********************************************/
37 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
38 {
39         char *p;
40         struct afb_ditf *ditf = closure;
41
42         if (vasprintf(&p, fmt, args) < 0)
43                 vverbose(level, file, line, function, fmt, args);
44         else {
45                 verbose(level, file, line, function, "%s {binding %s}", p, ditf->prefix);
46                 free(p);
47         }
48 }
49
50 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
51 {
52         vverbose_cb(closure, level, file, line, "?", fmt, args);
53 }
54
55 static struct afb_event event_make_cb(void *closure, const char *name)
56 {
57         size_t plen, nlen;
58         char *event;
59         struct afb_ditf *ditf = closure;
60
61         /* makes the event name */
62         plen = strlen(ditf->prefix);
63         nlen = strlen(name);
64         event = alloca(nlen + plen + 2);
65         memcpy(event, ditf->prefix, plen);
66         event[plen] = '/';
67         memcpy(event + plen + 1, name, nlen + 1);
68
69         /* create the event */
70         return afb_evt_create_event(event);
71 }
72
73 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
74 {
75         size_t plen, nlen;
76         char *event;
77         struct afb_ditf *ditf = closure;
78
79         /* makes the event name */
80         plen = strlen(ditf->prefix);
81         nlen = strlen(name);
82         event = alloca(nlen + plen + 2);
83         memcpy(event, ditf->prefix, plen);
84         event[plen] = '/';
85         memcpy(event + plen + 1, name, nlen + 1);
86
87         /* broadcast the event */
88         return afb_evt_broadcast(event, object);
89 }
90
91 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
92 {
93         return afb_common_rootdir_open_locale(filename, flags, locale);
94 }
95
96 /**********************************************
97 * hooked flow
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)
100 {
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);
105 }
106
107 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
108 {
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);
112 }
113
114 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
115 {
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);
119 }
120
121 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
122 {
123         int r;
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);
130         return r;
131 }
132
133 static struct sd_event *hooked_get_event_loop(void *closure)
134 {
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);
138 }
139
140 static struct sd_bus *hooked_get_user_bus(void *closure)
141 {
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);
145 }
146
147 static struct sd_bus *hooked_get_system_bus(void *closure)
148 {
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);
152 }
153
154 static int hooked_rootdir_get_fd(void *closure)
155 {
156         struct afb_ditf *ditf = closure;
157         int r = afb_common_rootdir_get_fd();
158         return afb_hook_ditf_rootdir_get_fd(ditf, r);
159 }
160
161 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
162 {
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);
166 }
167
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
177 };
178
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
188 };
189
190 void afb_ditf_init(struct afb_ditf *ditf, const char *prefix)
191 {
192         ditf->interface.verbosity = verbosity;
193         ditf->interface.mode = AFB_MODE_LOCAL;
194         ditf->interface.daemon.closure = ditf;
195         afb_ditf_rename(ditf, prefix);
196 }
197
198 void afb_ditf_rename(struct afb_ditf *ditf, const char *prefix)
199 {
200         ditf->prefix = prefix;
201         afb_ditf_update_hook(ditf);
202 }
203
204 void afb_ditf_update_hook(struct afb_ditf *ditf)
205 {
206         if (afb_hook_flags_ditf(ditf->prefix))
207                 ditf->interface.daemon.itf = &hooked_daemon_itf;
208         else
209                 ditf->interface.daemon.itf = &daemon_itf;
210 }
211