Make daemon interface common
[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 <afb/afb-binding.h>
25
26 #include "afb-ditf.h"
27 #include "afb-evt.h"
28 #include "afb-common.h"
29 #include "verbose.h"
30
31
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);
36
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
46 };
47
48 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
49 {
50         char *p;
51         struct afb_ditf *ditf = closure;
52
53         if (vasprintf(&p, fmt, args) < 0)
54                 vverbose(level, file, line, fmt, args);
55         else {
56                 verbose(level, file, line, "%s {binding %s}", p, ditf->prefix);
57                 free(p);
58         }
59 }
60
61 static struct afb_event event_make_cb(void *closure, const char *name)
62 {
63         size_t plen, nlen;
64         char *event;
65         struct afb_ditf *ditf = closure;
66
67         /* makes the event name */
68         plen = strlen(ditf->prefix);
69         nlen = strlen(name);
70         event = alloca(nlen + plen + 2);
71         memcpy(event, ditf->prefix, plen);
72         event[plen] = '/';
73         memcpy(event + plen + 1, name, nlen + 1);
74
75         /* create the event */
76         return afb_evt_create_event(event);
77 }
78
79 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
80 {
81         size_t plen, nlen;
82         char *event;
83         struct afb_ditf *ditf = closure;
84
85         /* makes the event name */
86         plen = strlen(ditf->prefix);
87         nlen = strlen(name);
88         event = alloca(nlen + plen + 2);
89         memcpy(event, ditf->prefix, plen);
90         event[plen] = '/';
91         memcpy(event + plen + 1, name, nlen + 1);
92
93         /* broadcast the event */
94         return afb_evt_broadcast(event, object);
95 }
96
97 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
98 {
99         return afb_common_rootdir_open_locale(filename, flags, locale);
100 }
101
102 void afb_ditf_init(struct afb_ditf *ditf, const char *prefix)
103 {
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);
109 }
110
111 void afb_ditf_rename(struct afb_ditf *ditf, const char *prefix)
112 {
113         ditf->prefix = prefix;
114 }
115