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 <afb/afb-binding-v2.h>
26 #include <json-c/json.h>
29 #include "afb-api-so-v2.h"
30 #include "afb-apiset.h"
34 #include "afb-common.h"
35 #include "afb-context.h"
36 #include "afb-api-so.h"
44 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
45 static const char afb_api_so_v2_data[] = "afbBindingV2data";
48 * Description of a binding
51 const struct afb_binding_v2 *binding; /* descriptor */
52 struct afb_binding_data_v2 *data; /* data */
53 void *handle; /* context of dlopen */
54 struct afb_svc *service; /* handler for service started */
55 struct afb_ditf ditf; /* daemon interface */
58 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
60 const struct afb_verb_v2 *verb;
62 verb = desc->binding->verbs;
63 while (verb->verb && strcasecmp(verb->verb, name))
65 return verb->verb ? verb : NULL;
69 static void call_cb(void *closure, struct afb_xreq *xreq)
71 struct api_so_v2 *desc = closure;
72 const struct afb_verb_v2 *verb;
74 verb = search(desc, xreq->verb);
75 afb_xreq_call_verb_v2(xreq, verb);
78 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
82 void (*onevent)(const char *event, struct json_object *object);
84 struct api_so_v2 *desc = closure;
87 if (desc->service != NULL) {
88 /* not an error when onneed */
92 /* already started: it is an error */
93 ERROR("Service %s already started", desc->ditf.api);
97 /* get the initialisation */
98 start = desc->binding->init;
99 onevent = desc->binding->onevent;
100 if (start == NULL && onevent == NULL) {
101 /* not an error when onneed */
105 /* no initialisation method */
106 ERROR("Binding %s is not a service", desc->ditf.api);
110 /* get the event handler if any */
111 desc->service = afb_svc_create(desc->ditf.api, apiset, share_session, onevent, &desc->data->service);
112 if (desc->service == NULL) {
114 ERROR("Starting service %s failed", desc->ditf.api);
118 /* Starts the service */
119 desc->ditf.state = Daemon_Init;
120 rc = afb_svc_start_v2(desc->service, start);
122 /* initialisation error */
123 ERROR("Initialisation of service %s failed (%d): %m", desc->ditf.api, rc);
124 afb_svc_destroy(desc->service, &desc->data->service);
125 desc->service = NULL;
130 desc->ditf.state = Daemon_Run;
134 static void update_hooks_cb(void *closure)
136 struct api_so_v2 *desc = closure;
137 afb_ditf_update_hook(&desc->ditf);
139 afb_svc_update_hook(desc->service);
142 static int get_verbosity_cb(void *closure)
144 struct api_so_v2 *desc = closure;
145 return desc->data->verbosity;
148 static void set_verbosity_cb(void *closure, int level)
150 struct api_so_v2 *desc = closure;
151 desc->data->verbosity = level;
154 static struct json_object *addperm(struct json_object *o, struct json_object *x)
156 struct json_object *a;
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);
167 json_object_array_add(a, x);
171 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
173 struct json_object *x = json_object_new_object();
174 json_object_object_add(x, key, val);
175 return addperm(o, x);
178 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
180 return addperm_key_val(o, key, json_object_new_string(val));
183 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
185 return addperm_key_val(o, key, json_object_new_int(val));
188 static struct json_object *addauth_or_array(struct json_object *o, const struct afb_auth *auth);
190 static struct json_object *addauth(struct json_object *o, const struct afb_auth *auth)
193 case afb_auth_No: return addperm(o, json_object_new_boolean(0));
194 case afb_auth_Token: return addperm_key_valstr(o, "session", "check");
195 case afb_auth_LOA: return addperm_key_valint(o, "LOA", auth->loa);
196 case afb_auth_Permission: return addperm_key_valstr(o, "permission", auth->text);
197 case afb_auth_Or: return addperm_key_val(o, "anyOf", addauth_or_array(json_object_new_array(), auth));
198 case afb_auth_And: return addauth(addauth(o, auth->first), auth->next);
199 case afb_auth_Not: return addperm_key_val(o, "not", addauth(NULL, auth->first));
200 case afb_auth_Yes: return addperm(o, json_object_new_boolean(1));
205 static struct json_object *addauth_or_array(struct json_object *o, const struct afb_auth *auth)
207 if (auth->type != afb_auth_Or)
208 json_object_array_add(o, addauth(NULL, auth));
210 addauth_or_array(o, auth->first);
211 addauth_or_array(o, auth->next);
217 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
220 const struct afb_verb_v2 *verb;
221 struct json_object *r, *f, *a, *i, *p, *g;
223 r = json_object_new_object();
224 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
226 i = json_object_new_object();
227 json_object_object_add(r, "info", i);
228 json_object_object_add(i, "title", json_object_new_string(desc->ditf.api));
229 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
230 json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: desc->ditf.api));
232 p = json_object_new_object();
233 json_object_object_add(r, "paths", p);
234 verb = desc->binding->verbs;
237 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
238 buffer[sizeof buffer - 1] = 0;
239 f = json_object_new_object();
240 json_object_object_add(p, buffer, f);
241 g = json_object_new_object();
242 json_object_object_add(f, "get", g);
245 if (verb->session & AFB_SESSION_CLOSE_V2)
246 a = addperm_key_valstr(a, "session", "close");
247 if (verb->session & AFB_SESSION_CHECK_V2)
248 a = addperm_key_valstr(a, "session", "check");
249 if (verb->session & AFB_SESSION_REFRESH_V2)
250 a = addperm_key_valstr(a, "token", "refresh");
251 if (verb->session & AFB_SESSION_LOA_MASK_V2)
252 a = addperm_key_valint(a, "LOA", verb->session & AFB_SESSION_LOA_MASK_V2);
254 a = addauth(a, verb->auth);
256 json_object_object_add(g, "x-permissions", a);
258 a = json_object_new_object();
259 json_object_object_add(g, "responses", a);
260 f = json_object_new_object();
261 json_object_object_add(a, "200", f);
262 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
268 static struct json_object *describe_cb(void *closure)
270 struct api_so_v2 *desc = closure;
271 struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
273 r = make_description_openAPIv3(desc);
277 static struct afb_api_itf so_v2_api_itf = {
279 .service_start = service_start_cb,
280 .update_hooks = update_hooks_cb,
281 .get_verbosity = get_verbosity_cb,
282 .set_verbosity = set_verbosity_cb,
283 .describe = describe_cb
286 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)
289 struct api_so_v2 *desc;
290 struct afb_api afb_api;
294 assert(binding->api);
295 assert(binding->verbs);
298 /* allocates the description */
299 desc = calloc(1, sizeof *desc);
301 ERROR("out of memory");
304 desc->binding = binding;
306 desc->handle = handle;
307 desc->service = NULL;
309 /* init the interface */
310 desc->data->verbosity = verbosity;
311 afb_ditf_init_v2(&desc->ditf, binding->api, data);
313 /* init the binding */
314 if (binding->preinit) {
315 INFO("binding %s calling preinit function", binding->api);
316 rc = binding->preinit();
318 ERROR("binding %s preinit function failed...", desc->ditf.api);
323 /* records the binding */
324 afb_api.closure = desc;
325 afb_api.itf = &so_v2_api_itf;
326 afb_api.noconcurrency = binding->noconcurrency;
327 if (afb_apiset_add(apiset, desc->ditf.api, afb_api) < 0) {
328 ERROR("binding %s can't be registered to set %s...", desc->ditf.api, afb_apiset_name(apiset));
331 INFO("binding %s added to set %s", desc->ditf.api, afb_apiset_name(apiset));
340 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
342 const struct afb_binding_v2 *binding;
343 struct afb_binding_data_v2 *data;
345 /* retrieves the register function */
346 binding = dlsym(handle, afb_api_so_v2_descriptor);
347 data = dlsym(handle, afb_api_so_v2_data);
348 if (!binding && !data)
351 INFO("binding [%s] looks like an AFB binding V2", path);
354 if (!binding || !data) {
355 ERROR("binding [%s] incomplete symbol set: %s is missing",
356 path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
359 if (binding->api == NULL || *binding->api == 0) {
360 ERROR("binding [%s] bad api name...", path);
363 if (!afb_api_is_valid_name(binding->api)) {
364 ERROR("binding [%s] invalid api name...", path);
368 if (binding->specification == NULL || *binding->specification == 0) {
369 ERROR("binding [%s] bad specification...", path);
373 if (binding->verbs == NULL) {
374 ERROR("binding [%s] no verbs...", path);
378 return afb_api_so_v2_add_binding(binding, handle, apiset, data);