2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <json-c/json.h>
27 #include <afb/afb-binding-v1.h>
30 #include "afb-api-so-v1.h"
31 #include "afb-apiset.h"
32 #include "afb-export.h"
33 //#include "afb-evt.h"
34 #include "afb-common.h"
35 #include "afb-context.h"
36 #include "afb-api-so.h"
43 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
44 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
45 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
48 * Description of a binding
51 struct afb_binding_v1 *binding; /* descriptor */
52 void *handle; /* context of dlopen */
53 struct afb_export *export; /* export */
56 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
58 const struct afb_verb_desc_v1 *verb;
60 verb = desc->binding->v1.verbs;
61 while (verb->name && strcasecmp(verb->name, name))
63 return verb->name ? verb : NULL;
66 static void call_cb(void *closure, struct afb_xreq *xreq)
68 const struct afb_verb_desc_v1 *verb;
69 struct api_so_v1 *desc = closure;
71 verb = search(desc, xreq->verb);
72 afb_xreq_call_verb_v1(xreq, verb);
75 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
78 int (*init)(struct afb_service service);
79 void (*onevent)(const char *event, struct json_object *object);
81 struct api_so_v1 *desc = closure;
84 if (afb_export_is_started(desc->export)) {
85 /* not an error when onneed */
89 /* already started: it is an error */
90 ERROR("Service %s already started", afb_export_apiname(desc->export));
94 /* get the initialisation */
95 init = dlsym(desc->handle, afb_api_so_v1_service_init);
96 onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
98 /* unshare the session if asked */
100 rc = afb_export_unshare_session(desc->export);
102 ERROR("Can't unshare the session for %s", afb_export_apiname(desc->export));
107 /* set event handling */
108 rc = afb_export_handle_events(desc->export, onevent);
110 ERROR("Can't set event handler for %s", afb_export_apiname(desc->export));
114 /* Starts the service */
115 rc = afb_export_start_v1(desc->export, init);
117 /* initialisation error */
118 ERROR("Initialisation of service %s failed (%d): %m", afb_export_apiname(desc->export), rc);
126 static void update_hooks_cb(void *closure)
128 struct api_so_v1 *desc = closure;
129 afb_export_update_hook(desc->export);
132 static int get_verbosity_cb(void *closure)
134 struct api_so_v1 *desc = closure;
135 return afb_export_verbosity_get(desc->export);
138 static void set_verbosity_cb(void *closure, int level)
140 struct api_so_v1 *desc = closure;
141 afb_export_verbosity_set(desc->export, level);
144 static struct json_object *addperm(struct json_object *o, struct json_object *x)
146 struct json_object *a;
151 if (!json_object_object_get_ex(o, "allOf", &a)) {
152 a = json_object_new_array();
153 json_object_array_add(a, o);
154 o = json_object_new_object();
155 json_object_object_add(o, "allOf", a);
157 json_object_array_add(a, x);
161 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
163 struct json_object *x = json_object_new_object();
164 json_object_object_add(x, key, val);
165 return addperm(o, x);
168 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
170 return addperm_key_val(o, key, json_object_new_string(val));
173 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
175 return addperm_key_val(o, key, json_object_new_int(val));
178 static struct json_object *make_description_openAPIv3(struct api_so_v1 *desc)
181 const struct afb_verb_desc_v1 *verb;
182 struct json_object *r, *f, *a, *i, *p, *g;
184 r = json_object_new_object();
185 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
187 i = json_object_new_object();
188 json_object_object_add(r, "info", i);
189 json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
190 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
191 json_object_object_add(i, "description", json_object_new_string(desc->binding->v1.info ?: afb_export_apiname(desc->export)));
193 p = json_object_new_object();
194 json_object_object_add(r, "paths", p);
195 verb = desc->binding->v1.verbs;
198 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
199 buffer[sizeof buffer - 1] = 0;
200 f = json_object_new_object();
201 json_object_object_add(p, buffer, f);
202 g = json_object_new_object();
203 json_object_object_add(f, "get", g);
206 if (verb->session & AFB_SESSION_CLOSE_V1)
207 a = addperm_key_valstr(a, "session", "close");
208 if (verb->session & AFB_SESSION_CHECK_V1)
209 a = addperm_key_valstr(a, "session", "check");
210 if (verb->session & AFB_SESSION_RENEW_V1)
211 a = addperm_key_valstr(a, "token", "refresh");
212 if (verb->session & AFB_SESSION_LOA_MASK_V1)
213 a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1);
215 json_object_object_add(g, "x-permissions", a);
217 a = json_object_new_object();
218 json_object_object_add(g, "responses", a);
219 f = json_object_new_object();
220 json_object_object_add(a, "200", f);
221 json_object_object_add(f, "description", json_object_new_string(verb->info));
227 static struct json_object *describe_cb(void *closure)
229 struct api_so_v1 *desc = closure;
231 return make_description_openAPIv3(desc);
234 static struct afb_api_itf so_v1_api_itf = {
236 .service_start = service_start_cb,
237 .update_hooks = update_hooks_cb,
238 .get_verbosity = get_verbosity_cb,
239 .set_verbosity = set_verbosity_cb,
240 .describe = describe_cb
243 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
245 struct api_so_v1 *desc;
246 struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
247 struct afb_api afb_api;
248 struct afb_export *export;
250 /* retrieves the register function */
251 register_function = dlsym(handle, afb_api_so_v1_register);
252 if (!register_function)
254 INFO("binding [%s] is a valid AFB binding V1", path);
256 /* allocates the description */
257 export = afb_export_create_v1(path);
258 desc = calloc(1, sizeof *desc);
259 if (desc == NULL || export == NULL) {
260 ERROR("out of memory");
263 desc->export = export;
264 desc->handle = handle;
266 /* init the binding */
267 INFO("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
268 desc->binding = afb_export_register_v1(desc->export, register_function);
269 if (desc->binding == NULL) {
270 ERROR("binding [%s] register function failed. continuing...", path);
274 /* check the returned structure */
275 if (desc->binding->type != AFB_BINDING_VERSION_1) {
276 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
279 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
280 ERROR("binding [%s] bad prefix...", path);
283 if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
284 ERROR("binding [%s] invalid prefix...", path);
287 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
288 ERROR("binding [%s] bad description...", path);
291 if (desc->binding->v1.verbs == NULL) {
292 ERROR("binding [%s] no APIs...", path);
296 /* records the binding */
297 if (!strcmp(path, afb_export_apiname(desc->export)))
298 afb_export_rename(desc->export, desc->binding->v1.prefix);
299 afb_api.closure = desc;
300 afb_api.itf = &so_v1_api_itf;
301 afb_api.noconcurrency = 0;
302 if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
303 ERROR("binding [%s] can't be registered...", path);
306 INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(desc->export));
310 afb_export_destroy(export);