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 struct afb_event event_make_cb(void *closure, const char *name);
33 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object);
34 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
35 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale);
37 static const struct afb_daemon_itf daemon_itf = {
38 .vverbose = vverbose_cb,
39 .event_make = event_make_cb,
40 .event_broadcast = event_broadcast_cb,
41 .get_event_loop = afb_common_get_event_loop,
42 .get_user_bus = afb_common_get_user_bus,
43 .get_system_bus = afb_common_get_system_bus,
44 .rootdir_get_fd = afb_common_rootdir_get_fd,
45 .rootdir_open_locale = rootdir_open_locale_cb
48 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
51 struct afb_ditf *ditf = closure;
53 if (vasprintf(&p, fmt, args) < 0)
54 vverbose(level, file, line, fmt, args);
56 verbose(level, file, line, "%s {binding %s}", p, ditf->prefix);
61 static struct afb_event event_make_cb(void *closure, const char *name)
65 struct afb_ditf *ditf = closure;
67 /* makes the event name */
68 plen = strlen(ditf->prefix);
70 event = alloca(nlen + plen + 2);
71 memcpy(event, ditf->prefix, plen);
73 memcpy(event + plen + 1, name, nlen + 1);
75 /* create the event */
76 return afb_evt_create_event(event);
79 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
83 struct afb_ditf *ditf = closure;
85 /* makes the event name */
86 plen = strlen(ditf->prefix);
88 event = alloca(nlen + plen + 2);
89 memcpy(event, ditf->prefix, plen);
91 memcpy(event + plen + 1, name, nlen + 1);
93 /* broadcast the event */
94 return afb_evt_broadcast(event, object);
97 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
99 return afb_common_rootdir_open_locale(filename, flags, locale);
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;