afb-ditf: track daemon state
[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 AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <dlfcn.h>
24 #include <assert.h>
25
26 #include <json-c/json.h>
27
28 #include <afb/afb-binding-v1.h>
29
30 #include "afb-api.h"
31 #include "afb-api-so-v1.h"
32 #include "afb-apiset.h"
33 #include "afb-svc.h"
34 #include "afb-evt.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
38 #include "afb-xreq.h"
39 #include "afb-ditf.h"
40 #include "verbose.h"
41
42 /*
43  * names of symbols
44  */
45 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
46 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
47 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
48
49 /*
50  * Description of a binding
51  */
52 struct api_so_v1 {
53         struct afb_binding_v1 *binding; /* descriptor */
54         void *handle;                   /* context of dlopen */
55         struct afb_svc *service;        /* handler for service started */
56         struct afb_binding_interface_v1 interface;
57         struct afb_ditf ditf;           /* daemon interface */
58 };
59
60 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
61 {
62         const struct afb_verb_desc_v1 *verb;
63
64         verb = desc->binding->v1.verbs;
65         while (verb->name && strcasecmp(verb->name, name))
66                 verb++;
67         return verb->name ? verb : NULL;
68 }
69
70 static void call_cb(void *closure, struct afb_xreq *xreq)
71 {
72         const struct afb_verb_desc_v1 *verb;
73         struct api_so_v1 *desc = closure;
74
75         verb = search(desc, xreq->verb);
76         afb_xreq_call_verb_v1(xreq, verb);
77 }
78
79 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
80 {
81         int rc;
82         int (*init)(struct afb_service service);
83         void (*onevent)(const char *event, struct json_object *object);
84
85         struct api_so_v1 *desc = closure;
86
87         /* check state */
88         if (desc->service != NULL) {
89                 /* not an error when onneed */
90                 if (onneed != 0)
91                         goto done;
92
93                 /* already started: it is an error */
94                 ERROR("Service %s already started", desc->binding->v1.prefix);
95                 return -1;
96         }
97
98         /* get the initialisation */
99         init = dlsym(desc->handle, afb_api_so_v1_service_init);
100         onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
101         if (init == NULL && onevent == NULL) {
102                 /* not an error when onneed */
103                 if (onneed != 0)
104                         goto done;
105
106                 /* no initialisation method */
107                 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
108                 return -1;
109         }
110
111         /* get the event handler if any */
112         desc->service = afb_svc_create(desc->binding->v1.prefix, apiset, share_session, onevent, NULL);
113         if (desc->service == NULL) {
114                 ERROR("Creation of service %s failed", desc->binding->v1.prefix);
115                 return -1;
116         }
117
118         /* Starts the service */
119         desc->ditf.state = Daemon_Init;
120         rc = afb_svc_start_v1(desc->service, init);
121         if (rc < 0) {
122                 /* initialisation error */
123                 ERROR("Initialisation of service %s failed (%d): %m", desc->binding->v1.prefix, rc);
124                 afb_svc_destroy(desc->service, NULL);
125                 desc->service = NULL;
126                 return rc;
127         }
128
129 done:
130         desc->ditf.state = Daemon_Run;
131         return 0;
132 }
133
134 static void update_hooks_cb(void *closure)
135 {
136         struct api_so_v1 *desc = closure;
137         afb_ditf_update_hook(&desc->ditf);
138         if (desc->service)
139                 afb_svc_update_hook(desc->service);
140 }
141
142 static int get_verbosity_cb(void *closure)
143 {
144         struct api_so_v1 *desc = closure;
145         return desc->interface.verbosity;
146 }
147
148 static void set_verbosity_cb(void *closure, int level)
149 {
150         struct api_so_v1 *desc = closure;
151         desc->interface.verbosity = level;
152 }
153
154 static struct json_object *addperm(struct json_object *o, struct json_object *x)
155 {
156         struct json_object *a;
157
158         if (!o)
159                 return x;
160
161         if (!json_object_object_get_ex(o, "allOf", &a)) {
162                 a = json_object_new_array();
163                 json_object_array_add(a, o);
164                 o = json_object_new_object();
165                 json_object_object_add(o, "allOf", a);
166         }
167         json_object_array_add(a, x);
168         return o;
169 }
170
171 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
172 {
173         struct json_object *x = json_object_new_object();
174         json_object_object_add(x, key, val);
175         return addperm(o, x);
176 }
177
178 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
179 {
180         return addperm_key_val(o, key, json_object_new_string(val));
181 }
182
183 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
184 {
185         return addperm_key_val(o, key, json_object_new_int(val));
186 }
187
188 static struct json_object *make_description_openAPIv3(struct api_so_v1 *desc)
189 {
190         char buffer[256];
191         const struct afb_verb_desc_v1 *verb;
192         struct json_object *r, *f, *a, *i, *p, *g;
193
194         r = json_object_new_object();
195         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
196
197         i = json_object_new_object();
198         json_object_object_add(r, "info", i);
199         json_object_object_add(i, "title", json_object_new_string(desc->binding->v1.prefix));
200         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
201         json_object_object_add(i, "description", json_object_new_string(desc->binding->v1.info ?: desc->binding->v1.prefix));
202
203         p = json_object_new_object();
204         json_object_object_add(r, "paths", p);
205         verb = desc->binding->v1.verbs;
206         while (verb->name) {
207                 buffer[0] = '/';
208                 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
209                 buffer[sizeof buffer - 1] = 0;
210                 f = json_object_new_object();
211                 json_object_object_add(p, buffer, f);
212                 g = json_object_new_object();
213                 json_object_object_add(f, "get", g);
214
215                 a = NULL;
216                 if (verb->session & AFB_SESSION_CLOSE_V1)
217                         a = addperm_key_valstr(a, "session", "close");
218                 if (verb->session & AFB_SESSION_CHECK_V1)
219                         a = addperm_key_valstr(a, "session", "check");
220                 if (verb->session & AFB_SESSION_RENEW_V1)
221                         a = addperm_key_valstr(a, "token", "refresh");
222                 if (verb->session & AFB_SESSION_LOA_MASK_V1)
223                         a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1);
224                 if (a)
225                         json_object_object_add(g, "x-permissions", a);
226
227                 a = json_object_new_object();
228                 json_object_object_add(g, "responses", a);
229                 f = json_object_new_object();
230                 json_object_object_add(a, "200", f);
231                 json_object_object_add(f, "description", json_object_new_string(verb->info));
232                 verb++;
233         }
234         return r;
235 }
236
237 static struct json_object *describe_cb(void *closure)
238 {
239         struct api_so_v1 *desc = closure;
240
241         return make_description_openAPIv3(desc);
242 }
243
244 static struct afb_api_itf so_v1_api_itf = {
245         .call = call_cb,
246         .service_start = service_start_cb,
247         .update_hooks = update_hooks_cb,
248         .get_verbosity = get_verbosity_cb,
249         .set_verbosity = set_verbosity_cb,
250         .describe = describe_cb
251 };
252
253 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
254 {
255         struct api_so_v1 *desc;
256         struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
257         struct afb_api afb_api;
258
259         /* retrieves the register function */
260         register_function = dlsym(handle, afb_api_so_v1_register);
261         if (!register_function)
262                 return 0;
263         INFO("binding [%s] is a valid AFB binding V1", path);
264
265         /* allocates the description */
266         desc = calloc(1, sizeof *desc);
267         if (desc == NULL) {
268                 ERROR("out of memory");
269                 goto error;
270         }
271         desc->handle = handle;
272
273         /* init the interface */
274         afb_ditf_init_v1(&desc->ditf, path, &desc->interface);
275
276         /* init the binding */
277         INFO("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
278         desc->binding = register_function(&desc->interface);
279         if (desc->binding == NULL) {
280                 ERROR("binding [%s] register function failed. continuing...", path);
281                 goto error2;
282         }
283
284         /* check the returned structure */
285         if (desc->binding->type != AFB_BINDING_VERSION_1) {
286                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
287                 goto error2;
288         }
289         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
290                 ERROR("binding [%s] bad prefix...", path);
291                 goto error2;
292         }
293         if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
294                 ERROR("binding [%s] invalid prefix...", path);
295                 goto error2;
296         }
297         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
298                 ERROR("binding [%s] bad description...", path);
299                 goto error2;
300         }
301         if (desc->binding->v1.verbs == NULL) {
302                 ERROR("binding [%s] no APIs...", path);
303                 goto error2;
304         }
305
306         /* records the binding */
307         afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
308         afb_api.closure = desc;
309         afb_api.itf = &so_v1_api_itf;
310         afb_api.noconcurrency = 0;
311         if (afb_apiset_add(apiset, desc->binding->v1.prefix, afb_api) < 0) {
312                 ERROR("binding [%s] can't be registered...", path);
313                 goto error2;
314         }
315         INFO("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
316         return 1;
317
318 error2:
319         free(desc);
320 error:
321         return -1;
322 }
323