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 *make_description_openAPIv3(struct api_so_v2 *desc)
191 const struct afb_verb_v2 *verb;
192 struct json_object *r, *f, *a, *i, *p, *g;
194 r = json_object_new_object();
195 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
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->ditf.api));
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->info ?: desc->ditf.api));
203 p = json_object_new_object();
204 json_object_object_add(r, "paths", p);
205 verb = desc->binding->verbs;
208 strncpy(buffer + 1, verb->verb, 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);
216 if (verb->session & AFB_SESSION_CLOSE_V2)
217 a = addperm_key_valstr(a, "session", "close");
218 if (verb->session & AFB_SESSION_CHECK_V2)
219 a = addperm_key_valstr(a, "session", "check");
220 if (verb->session & AFB_SESSION_REFRESH_V2)
221 a = addperm_key_valstr(a, "token", "refresh");
222 if (verb->session & AFB_SESSION_LOA_MASK_V2)
223 a = addperm_key_valint(a, "LOA", verb->session & AFB_SESSION_LOA_MASK_V2);
229 json_object_object_add(g, "x-permissions", a);
231 a = json_object_new_object();
232 json_object_object_add(g, "responses", a);
233 f = json_object_new_object();
234 json_object_object_add(a, "200", f);
235 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
241 static struct json_object *describe_cb(void *closure)
243 struct api_so_v2 *desc = closure;
244 struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
246 r = make_description_openAPIv3(desc);
250 static struct afb_api_itf so_v2_api_itf = {
252 .service_start = service_start_cb,
253 .update_hooks = update_hooks_cb,
254 .get_verbosity = get_verbosity_cb,
255 .set_verbosity = set_verbosity_cb,
256 .describe = describe_cb
259 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)
262 struct api_so_v2 *desc;
263 struct afb_api afb_api;
267 assert(binding->api);
268 assert(binding->verbs);
271 /* allocates the description */
272 desc = calloc(1, sizeof *desc);
274 ERROR("out of memory");
277 desc->binding = binding;
279 desc->handle = handle;
280 desc->service = NULL;
282 /* init the interface */
283 desc->data->verbosity = verbosity;
284 afb_ditf_init_v2(&desc->ditf, binding->api, data);
286 /* init the binding */
287 if (binding->preinit) {
288 INFO("binding %s calling preinit function", binding->api);
289 rc = binding->preinit();
291 ERROR("binding %s preinit function failed...", desc->ditf.api);
296 /* records the binding */
297 afb_api.closure = desc;
298 afb_api.itf = &so_v2_api_itf;
299 afb_api.noconcurrency = binding->noconcurrency;
300 if (afb_apiset_add(apiset, desc->ditf.api, afb_api) < 0) {
301 ERROR("binding %s can't be registered to set %s...", desc->ditf.api, afb_apiset_name(apiset));
304 INFO("binding %s added to set %s", desc->ditf.api, afb_apiset_name(apiset));
313 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
315 const struct afb_binding_v2 *binding;
316 struct afb_binding_data_v2 *data;
318 /* retrieves the register function */
319 binding = dlsym(handle, afb_api_so_v2_descriptor);
320 data = dlsym(handle, afb_api_so_v2_data);
321 if (!binding && !data)
324 INFO("binding [%s] looks like an AFB binding V2", path);
327 if (!binding || !data) {
328 ERROR("binding [%s] incomplete symbol set: %s is missing",
329 path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
332 if (binding->api == NULL || *binding->api == 0) {
333 ERROR("binding [%s] bad api name...", path);
336 if (!afb_api_is_valid_name(binding->api)) {
337 ERROR("binding [%s] invalid api name...", path);
341 if (binding->specification == NULL || *binding->specification == 0) {
342 ERROR("binding [%s] bad specification...", path);
346 if (binding->verbs == NULL) {
347 ERROR("binding [%s] no verbs...", path);
351 return afb_api_so_v2_add_binding(binding, handle, apiset, data);