Start to implement the bindings V2
[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-thread.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 int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_v2 *verb)
128 {
129         int stag = (int)verb->session;
130
131         if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
132                 if (!afb_context_check(context)) {
133                         afb_context_close(context);
134                         afb_req_fail(req, "failed", "invalid token's identity");
135                         return 0;
136                 }
137         }
138
139         if ((stag & AFB_SESSION_CREATE) != 0) {
140                 if (afb_context_check_loa(context, 1)) {
141                         afb_req_fail(req, "failed", "invalid creation state");
142                         return 0;
143                 }
144                 afb_context_change_loa(context, 1);
145                 afb_context_refresh(context);
146         }
147
148         if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
149                 afb_context_refresh(context);
150
151         if ((stag & AFB_SESSION_CLOSE) != 0) {
152                 afb_context_change_loa(context, 0);
153                 afb_context_close(context);
154         }
155
156         if ((stag & AFB_SESSION_LOA_GE) != 0) {
157                 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
158                 if (!afb_context_check_loa(context, loa)) {
159                         afb_req_fail(req, "failed", "invalid LOA");
160                         return 0;
161                 }
162         }
163
164         if ((stag & AFB_SESSION_LOA_LE) != 0) {
165                 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
166                 if (afb_context_check_loa(context, loa + 1)) {
167                         afb_req_fail(req, "failed", "invalid LOA");
168                         return 0;
169                 }
170         }
171         return 1;
172 }
173
174 static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *strverb)
175 {
176         const struct afb_verb_v2 *verb;
177         struct api_so_v2 *desc = closure;
178
179         verb = desc->binding->verbs;
180         while (verb->verb && strcasecmp(verb->verb, strverb))
181                 verb++;
182         if (!verb->verb)
183                 afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", strverb, desc->binding->api);
184         else if (call_check(req, context, verb)) {
185                 afb_thread_req_call(req, verb->callback, afb_api_so_timeout, desc);
186         }
187 }
188
189 static int service_start_cb(void *closure, int share_session, int onneed)
190 {
191         int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
192         void (*onevent)(const char *event, struct json_object *object);
193
194         struct api_so_v2 *desc = closure;
195
196         /* check state */
197         if (desc->service != NULL) {
198                 /* not an error when onneed */
199                 if (onneed != 0)
200                         return 0;
201
202                 /* already started: it is an error */
203                 ERROR("Service %s already started", desc->binding->api);
204                 return -1;
205         }
206
207         /* get the initialisation */
208         start = desc->binding->start;
209         if (start == NULL) {
210                 /* not an error when onneed */
211                 if (onneed != 0)
212                         return 0;
213
214                 /* no initialisation method */
215                 ERROR("Binding %s is not a service", desc->binding->api);
216                 return -1;
217         }
218
219         /* get the event handler if any */
220         onevent = desc->binding->onevent;
221         desc->service = afb_svc_create_v2(share_session, onevent, start, &desc->interface);
222         if (desc->service == NULL) {
223                 /* starting error */
224                 ERROR("Starting service %s failed", desc->binding->api);
225                 return -1;
226         }
227
228         return 0;
229 }
230
231 int afb_api_so_v2_add(const char *path, void *handle)
232 {
233         int rc;
234         struct api_so_v2 *desc;
235         struct afb_binding_v2 *binding;
236
237         /* retrieves the register function */
238         binding = dlsym(handle, afb_api_so_v2_descriptor);
239         if (!binding)
240                 return 0;
241
242         INFO("binding [%s] looks like an AFB binding V2", path);
243
244         /* basic checks */
245         if (binding->api == NULL || *binding->api == 0) {
246                 ERROR("binding [%s] bad api name...", path);
247                 goto error;
248         }
249         if (!afb_apis_is_valid_api_name(binding->api)) {
250                 ERROR("binding [%s] invalid api name...", path);
251                 goto error;
252         }
253         if (binding->specification == NULL || *binding->specification == 0) {
254                 ERROR("binding [%s] bad specification...", path);
255                 goto error;
256         }
257         if (binding->verbs == NULL) {
258                 ERROR("binding [%s] no verbs...", path);
259                 goto error;
260         }
261
262         /* allocates the description */
263         desc = calloc(1, sizeof *desc);
264         if (desc == NULL) {
265                 ERROR("out of memory");
266                 goto error;
267         }
268         desc->binding = binding;
269         desc->handle = handle;
270
271         /* init the interface */
272         desc->interface.verbosity = verbosity;
273         desc->interface.mode = AFB_MODE_LOCAL;
274         desc->interface.daemon.itf = &daemon_itf;
275         desc->interface.daemon.closure = desc;
276
277         /* for log purpose, a fake binding is needed here */
278
279         /* init the binding */
280         if (binding->init) {
281                 NOTICE("binding %s [%s] calling init function", binding->api, path);
282                 rc = binding->init(&desc->interface);
283                 if (rc < 0) {
284                         ERROR("binding %s [%s] initialisation function failed...", binding->api, path);
285                         goto error2;
286                 }
287         }
288
289         /* records the binding */
290         desc->apilength = strlen(binding->api);
291         if (afb_apis_add(binding->api, (struct afb_api){
292                         .closure = desc,
293                         .call = call_cb,
294                         .service_start = service_start_cb }) < 0) {
295                 ERROR("binding [%s] can't be registered...", path);
296                 goto error2;
297         }
298         NOTICE("binding %s loaded with API prefix %s", path, binding->api);
299         return 1;
300
301 error2:
302         free(desc);
303 error:
304         return -1;
305 }
306