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 <afb/afb-binding.h>
28 #include "afb-common.h"
32 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
35 struct afb_ditf *ditf = closure;
37 if (vasprintf(&p, fmt, args) < 0)
38 vverbose(level, file, line, function, fmt, args);
40 verbose(level, file, line, function, "%s {binding %s}", p, ditf->prefix);
45 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
47 vverbose_cb(closure, level, file, line, "?", fmt, args);
50 static struct afb_event event_make_cb(void *closure, const char *name)
54 struct afb_ditf *ditf = closure;
56 /* makes the event name */
57 plen = strlen(ditf->prefix);
59 event = alloca(nlen + plen + 2);
60 memcpy(event, ditf->prefix, plen);
62 memcpy(event + plen + 1, name, nlen + 1);
64 /* create the event */
65 return afb_evt_create_event(event);
68 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
72 struct afb_ditf *ditf = closure;
74 /* makes the event name */
75 plen = strlen(ditf->prefix);
77 event = alloca(nlen + plen + 2);
78 memcpy(event, ditf->prefix, plen);
80 memcpy(event + plen + 1, name, nlen + 1);
82 /* broadcast the event */
83 return afb_evt_broadcast(event, object);
86 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
88 return afb_common_rootdir_open_locale(filename, flags, locale);
91 static const struct afb_daemon_itf daemon_itf = {
92 .vverbose = old_vverbose_cb,
93 .event_make = event_make_cb,
94 .event_broadcast = event_broadcast_cb,
95 .get_event_loop = afb_common_get_event_loop,
96 .get_user_bus = afb_common_get_user_bus,
97 .get_system_bus = afb_common_get_system_bus,
98 .rootdir_get_fd = afb_common_rootdir_get_fd,
99 .rootdir_open_locale = rootdir_open_locale_cb
102 void afb_ditf_init(struct afb_ditf *ditf, const char *prefix)
104 ditf->interface.verbosity = verbosity;
105 ditf->interface.mode = AFB_MODE_LOCAL;
106 ditf->interface.daemon.itf = &daemon_itf;
107 ditf->interface.daemon.closure = ditf;
108 afb_ditf_rename(ditf, prefix);
111 void afb_ditf_rename(struct afb_ditf *ditf, const char *prefix)
113 ditf->prefix = prefix;