Definitive switch to internal's xreq
[src/app-framework-binder.git] / src / afb-api-so-v1.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 <dlfcn.h>
23 #include <assert.h>
24
25 #include <afb/afb-binding.h>
26
27 #include "afb-apis.h"
28 #include "afb-svc.h"
29 #include "afb-evt.h"
30 #include "afb-common.h"
31 #include "afb-context.h"
32 #include "afb-api-so.h"
33 #include "afb-xreq.h"
34 #include "verbose.h"
35
36 /*
37  * names of symbols
38  */
39 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
40 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
41 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
42
43 /*
44  * Description of a binding
45  */
46 struct api_so_v1 {
47         struct afb_binding *binding;    /* descriptor */
48         size_t apilength;               /* length of the API name */
49         void *handle;                   /* context of dlopen */
50         struct afb_svc *service;        /* handler for service started */
51         struct afb_binding_interface interface; /* interface for the binding */
52 };
53
54 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name);
55 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
56 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
57 static int afb_api_so_rootdir_get_fd(void *closure);
58 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale);
59
60 static const struct afb_daemon_itf daemon_itf = {
61         .event_broadcast = afb_api_so_event_broadcast_cb,
62         .get_event_loop = afb_common_get_event_loop,
63         .get_user_bus = afb_common_get_user_bus,
64         .get_system_bus = afb_common_get_system_bus,
65         .vverbose = afb_api_so_vverbose_cb,
66         .event_make = afb_api_so_event_make_cb,
67         .rootdir_get_fd = afb_api_so_rootdir_get_fd,
68         .rootdir_open_locale = afb_api_so_rootdir_open_locale
69 };
70
71 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name)
72 {
73         size_t length;
74         char *event;
75         struct api_so_v1 *desc = closure;
76
77         /* makes the event name */
78         assert(desc->binding != NULL);
79         length = strlen(name);
80         event = alloca(length + 2 + desc->apilength);
81         memcpy(event, desc->binding->v1.prefix, desc->apilength);
82         event[desc->apilength] = '/';
83         memcpy(event + desc->apilength + 1, name, length + 1);
84
85         /* crate the event */
86         return afb_evt_create_event(event);
87 }
88
89 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
90 {
91         size_t length;
92         char *event;
93         struct api_so_v1 *desc = closure;
94
95         /* makes the event name */
96         assert(desc->binding != NULL);
97         length = strlen(name);
98         event = alloca(length + 2 + desc->apilength);
99         memcpy(event, desc->binding->v1.prefix, desc->apilength);
100         event[desc->apilength] = '/';
101         memcpy(event + desc->apilength + 1, name, length + 1);
102
103         return afb_evt_broadcast(event, object);
104 }
105
106 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
107 {
108         char *p;
109         struct api_so_v1 *desc = closure;
110
111         if (vasprintf(&p, fmt, args) < 0)
112                 vverbose(level, file, line, fmt, args);
113         else {
114                 verbose(level, file, line, "%s {binding %s}", p, desc->binding->v1.prefix);
115                 free(p);
116         }
117 }
118
119 static int afb_api_so_rootdir_get_fd(void *closure)
120 {
121         return afb_common_rootdir_get_fd();
122 }
123
124 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale)
125 {
126         return afb_common_rootdir_open_locale(filename, flags, locale);
127 }
128
129 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
130 {
131         const struct afb_verb_desc_v1 *verb;
132
133         verb = desc->binding->v1.verbs;
134         while (verb->name && strcasecmp(verb->name, name))
135                 verb++;
136         return verb->name ? verb : NULL;
137 }
138
139 static void call_cb(void *closure, struct afb_xreq *xreq)
140 {
141         const struct afb_verb_desc_v1 *verb;
142         struct api_so_v1 *desc = closure;
143
144         verb = search(desc, xreq->verb);
145         if (!verb)
146                 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->v1.prefix);
147         else {
148                 xreq->timeout = afb_api_so_timeout;
149                 xreq->sessionflags = (int)verb->session;
150                 xreq->group = desc;
151                 xreq->callback = verb->callback;
152                 afb_xreq_call(xreq);
153         }
154 }
155
156 static int service_start_cb(void *closure, int share_session, int onneed)
157 {
158         int (*init)(struct afb_service service);
159         void (*onevent)(const char *event, struct json_object *object);
160
161         struct api_so_v1 *desc = closure;
162
163         /* check state */
164         if (desc->service != NULL) {
165                 /* not an error when onneed */
166                 if (onneed != 0)
167                         return 0;
168
169                 /* already started: it is an error */
170                 ERROR("Service %s already started", desc->binding->v1.prefix);
171                 return -1;
172         }
173
174         /* get the initialisation */
175         init = dlsym(desc->handle, afb_api_so_v1_service_init);
176         if (init == NULL) {
177                 /* not an error when onneed */
178                 if (onneed != 0)
179                         return 0;
180
181                 /* no initialisation method */
182                 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
183                 return -1;
184         }
185
186         /* get the event handler if any */
187         onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
188         desc->service = afb_svc_create(share_session, init, onevent);
189         if (desc->service == NULL) {
190                 /* starting error */
191                 ERROR("Starting service %s failed", desc->binding->v1.prefix);
192                 return -1;
193         }
194
195         return 0;
196 }
197
198 int afb_api_so_v1_add(const char *path, void *handle)
199 {
200         struct api_so_v1 *desc;
201         struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
202         struct afb_verb_desc_v1 fake_verb;
203         struct afb_binding fake_binding;
204
205         /* retrieves the register function */
206         register_function = dlsym(handle, afb_api_so_v1_register);
207         if (!register_function)
208                 return 0;
209         INFO("binding [%s] is a valid AFB binding V1", path);
210
211         /* allocates the description */
212         desc = calloc(1, sizeof *desc);
213         if (desc == NULL) {
214                 ERROR("out of memory");
215                 goto error;
216         }
217         desc->handle = handle;
218
219         /* init the interface */
220         desc->interface.verbosity = verbosity;
221         desc->interface.mode = AFB_MODE_LOCAL;
222         desc->interface.daemon.itf = &daemon_itf;
223         desc->interface.daemon.closure = desc;
224
225         /* for log purpose, a fake binding is needed here */
226         desc->binding = &fake_binding;
227         fake_binding.type = AFB_BINDING_VERSION_1;
228         fake_binding.v1.info = path;
229         fake_binding.v1.prefix = path;
230         fake_binding.v1.verbs = &fake_verb;
231         fake_verb.name = NULL;
232
233         /* init the binding */
234         NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
235         desc->binding = register_function(&desc->interface);
236         if (desc->binding == NULL) {
237                 ERROR("binding [%s] register function failed. continuing...", path);
238                 goto error2;
239         }
240
241         /* check the returned structure */
242         if (desc->binding->type != AFB_BINDING_VERSION_1) {
243                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
244                 goto error2;
245         }
246         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
247                 ERROR("binding [%s] bad prefix...", path);
248                 goto error2;
249         }
250         if (!afb_apis_is_valid_api_name(desc->binding->v1.prefix)) {
251                 ERROR("binding [%s] invalid prefix...", path);
252                 goto error2;
253         }
254         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
255                 ERROR("binding [%s] bad description...", path);
256                 goto error2;
257         }
258         if (desc->binding->v1.verbs == NULL) {
259                 ERROR("binding [%s] no APIs...", path);
260                 goto error2;
261         }
262
263         /* records the binding */
264         desc->apilength = strlen(desc->binding->v1.prefix);
265         if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
266                         .closure = desc,
267                         .call = call_cb,
268                         .service_start = service_start_cb }) < 0) {
269                 ERROR("binding [%s] can't be registered...", path);
270                 goto error2;
271         }
272         NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
273         return 1;
274
275 error2:
276         free(desc);
277 error:
278         return -1;
279 }
280