Update date of copyright notices
[src/app-framework-binder.git] / src / afb-api-so-v2.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "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
20 #include <stdlib.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <afb/afb-binding-v2.h>
26 #include <json-c/json.h>
27
28 #include "afb-api.h"
29 #include "afb-api-so-v2.h"
30 #include "afb-apiset.h"
31 #include "afb-auth.h"
32 #include "afb-export.h"
33 #include "afb-evt.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
36 #include "afb-xreq.h"
37 #include "verbose.h"
38
39 /*
40  * names of symbols
41  */
42 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
43 static const char afb_api_so_v2_data[] = "afbBindingV2data";
44
45 /*
46  * Description of a binding
47  */
48 struct api_so_v2 {
49         const struct afb_binding_v2 *binding;   /* descriptor */
50         void *handle;                           /* context of dlopen */
51         struct afb_export *export;              /* exportations */
52 };
53
54 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
55 {
56         const struct afb_verb_v2 *verb;
57
58         verb = desc->binding->verbs;
59         while (verb->verb && strcasecmp(verb->verb, name))
60                 verb++;
61         return verb->verb ? verb : NULL;
62         return NULL;
63 }
64
65 static void call_cb(void *closure, struct afb_xreq *xreq)
66 {
67         struct api_so_v2 *desc = closure;
68         const struct afb_verb_v2 *verb;
69
70         xreq->request.dynapi = (void*)desc->export; /* hack: this avoids to export afb_export structure */
71         verb = search(desc, xreq->request.verb);
72         afb_xreq_call_verb_v2(xreq, verb);
73 }
74
75 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
76 {
77         struct api_so_v2 *desc = closure;
78         return afb_export_start(desc->export, share_session, onneed, apiset);
79 }
80
81 static void update_hooks_cb(void *closure)
82 {
83         struct api_so_v2 *desc = closure;
84         afb_export_update_hook(desc->export);
85 }
86
87 static int get_verbosity_cb(void *closure)
88 {
89         struct api_so_v2 *desc = closure;
90         return afb_export_verbosity_get(desc->export);
91 }
92
93 static void set_verbosity_cb(void *closure, int level)
94 {
95         struct api_so_v2 *desc = closure;
96         afb_export_verbosity_set(desc->export, level);
97 }
98
99 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
100 {
101         char buffer[256];
102         const struct afb_verb_v2 *verb;
103         struct json_object *r, *f, *a, *i, *p, *g;
104
105         r = json_object_new_object();
106         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
107
108         i = json_object_new_object();
109         json_object_object_add(r, "info", i);
110         json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
111         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
112         json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: afb_export_apiname(desc->export)));
113
114         p = json_object_new_object();
115         json_object_object_add(r, "paths", p);
116         verb = desc->binding->verbs;
117         while (verb->verb) {
118                 buffer[0] = '/';
119                 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
120                 buffer[sizeof buffer - 1] = 0;
121                 f = json_object_new_object();
122                 json_object_object_add(p, buffer, f);
123                 g = json_object_new_object();
124                 json_object_object_add(f, "get", g);
125
126                 a = afb_auth_json_v2(verb->auth, verb->session);
127                 if (a)
128                         json_object_object_add(g, "x-permissions", a);
129
130                 a = json_object_new_object();
131                 json_object_object_add(g, "responses", a);
132                 f = json_object_new_object();
133                 json_object_object_add(a, "200", f);
134                 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
135                 verb++;
136         }
137         return r;
138 }
139
140 static struct json_object *describe_cb(void *closure)
141 {
142         struct api_so_v2 *desc = closure;
143         struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
144         if (!r)
145                 r = make_description_openAPIv3(desc);
146         return r;
147 }
148
149 static struct afb_api_itf so_v2_api_itf = {
150         .call = call_cb,
151         .service_start = service_start_cb,
152         .update_hooks = update_hooks_cb,
153         .get_verbosity = get_verbosity_cb,
154         .set_verbosity = set_verbosity_cb,
155         .describe = describe_cb
156 };
157
158 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset, struct afb_binding_data_v2 *data)
159 {
160         int rc;
161         struct api_so_v2 *desc;
162         struct afb_api afb_api;
163         struct afb_export *export;
164
165         /* basic checks */
166         assert(binding);
167         assert(binding->api);
168         assert(binding->verbs);
169         assert(data);
170
171         /* allocates the description */
172         export = afb_export_create_v2(apiset, binding->api, data, binding->init, binding->onevent);
173         desc = calloc(1, sizeof *desc);
174         if (!desc || !export) {
175                 ERROR("out of memory");
176                 goto error;
177         }
178         desc->binding = binding;
179         desc->handle = handle;
180         desc->export = export;
181
182         /* init the binding */
183         if (binding->preinit) {
184                 INFO("binding %s calling preinit function", binding->api);
185                 rc = binding->preinit();
186                 if (rc < 0) {
187                         ERROR("binding %s preinit function failed...", afb_export_apiname(desc->export));
188                         goto error;
189                 }
190         }
191
192         /* records the binding */
193         afb_api.closure = desc;
194         afb_api.itf = &so_v2_api_itf;
195         afb_api.group = binding->noconcurrency ? export : NULL;
196         if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
197                 ERROR("binding %s can't be registered to set %s...", afb_export_apiname(desc->export), afb_apiset_name(apiset));
198                 goto error;
199         }
200         INFO("binding %s added to set %s", afb_export_apiname(desc->export), afb_apiset_name(apiset));
201         return 1;
202
203 error:
204         afb_export_destroy(export);
205         free(desc);
206
207         return -1;
208 }
209
210 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
211 {
212         const struct afb_binding_v2 *binding;
213         struct afb_binding_data_v2 *data;
214
215         /* retrieves the register function */
216         binding = dlsym(handle, afb_api_so_v2_descriptor);
217         data = dlsym(handle, afb_api_so_v2_data);
218         if (!binding && !data)
219                 return 0;
220
221         INFO("binding [%s] looks like an AFB binding V2", path);
222
223         /* basic checks */
224         if (!binding || !data) {
225                 ERROR("binding [%s] incomplete symbol set: %s is missing",
226                         path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
227                 goto error;
228         }
229         if (binding->api == NULL || *binding->api == 0) {
230                 ERROR("binding [%s] bad api name...", path);
231                 goto error;
232         }
233         if (!afb_api_is_valid_name(binding->api, 1)) {
234                 ERROR("binding [%s] invalid api name...", path);
235                 goto error;
236         }
237 #if 0
238         if (binding->specification == NULL || *binding->specification == 0) {
239                 ERROR("binding [%s] bad specification...", path);
240                 goto error;
241         }
242 #endif
243         if (binding->verbs == NULL) {
244                 ERROR("binding [%s] no verbs...", path);
245                 goto error;
246         }
247
248         return afb_api_so_v2_add_binding(binding, handle, apiset, data);
249
250  error:
251         return -1;
252 }
253