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