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.
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
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);
80 struct api_so_v2 *desc;
81 struct afb_xreq *xreq;
84 static void call_sync_cb_cb(int signum, void *closure)
86 struct call_sync *cs = closure;
88 call_cb(cs->desc, cs->xreq);
90 if (!cs->xreq->replied)
91 afb_xreq_fail(cs->xreq, "aborted", "internal error");
95 static void call_sync_cb(void *closure, struct afb_xreq *xreq)
97 struct call_sync cs = { .desc = closure, .xreq = xreq };
99 if (jobs_call(closure, 0, call_sync_cb_cb, &cs))
100 call_cb(closure, xreq);
103 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
106 void (*onevent)(const char *event, struct json_object *object);
108 struct api_so_v2 *desc = closure;
111 if (desc->service != NULL) {
112 /* not an error when onneed */
116 /* already started: it is an error */
117 ERROR("Service %s already started", desc->binding->api);
121 /* get the initialisation */
122 start = desc->binding->init;
123 onevent = desc->binding->onevent;
124 if (start == NULL && onevent == NULL) {
125 /* not an error when onneed */
129 /* no initialisation method */
130 ERROR("Binding %s is not a service", desc->binding->api);
134 /* get the event handler if any */
135 desc->service = afb_svc_create_v2(apiset, share_session, start, onevent, desc->data);
136 if (desc->service == NULL) {
138 ERROR("Starting service %s failed", desc->binding->api);
145 static void update_hooks_cb(void *closure)
147 struct api_so_v2 *desc = closure;
148 afb_ditf_update_hook(&desc->ditf);
151 static int get_verbosity_cb(void *closure)
153 struct api_so_v2 *desc = closure;
154 return desc->data->verbosity;
157 static void set_verbosity_cb(void *closure, int level)
159 struct api_so_v2 *desc = closure;
160 desc->data->verbosity = level;
163 static struct json_object *addperm(struct json_object *o, struct json_object *x)
165 struct json_object *a;
170 if (!json_object_object_get_ex(o, "allOf", &a)) {
171 a = json_object_new_array();
172 json_object_array_add(a, o);
173 o = json_object_new_object();
174 json_object_object_add(o, "allOf", a);
176 json_object_array_add(a, x);
180 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
182 struct json_object *x = json_object_new_object();
183 json_object_object_add(x, key, val);
184 return addperm(o, x);
187 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
189 return addperm_key_val(o, key, json_object_new_string(val));
192 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
194 return addperm_key_val(o, key, json_object_new_int(val));
197 static struct json_object *make_description(struct api_so_v2 *desc)
200 const struct afb_verb_v2 *verb;
201 struct json_object *r, *f, *a, *i, *p, *g;
203 r = json_object_new_object();
204 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
206 i = json_object_new_object();
207 json_object_object_add(r, "info", i);
208 json_object_object_add(i, "title", json_object_new_string(desc->binding->api));
209 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
211 p = json_object_new_object();
212 json_object_object_add(r, "paths", p);
213 verb = desc->binding->verbs;
216 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
217 buffer[sizeof buffer - 1] = 0;
218 f = json_object_new_object();
219 json_object_object_add(p, buffer, f);
220 g = json_object_new_object();
221 json_object_object_add(f, "get", g);
224 if (verb->session & AFB_SESSION_CLOSE_V2)
225 a = addperm_key_valstr(a, "session", "close");
226 if (verb->session & AFB_SESSION_CHECK_V2)
227 a = addperm_key_valstr(a, "session", "check");
228 if (verb->session & AFB_SESSION_REFRESH_V2)
229 a = addperm_key_valstr(a, "token", "refresh");
230 if (verb->session & AFB_SESSION_LOA_MASK_V2)
231 a = addperm_key_valint(a, "LOA", verb->session & AFB_SESSION_LOA_MASK_V2);
237 json_object_object_add(g, "x-permissions", a);
239 a = json_object_new_object();
240 json_object_object_add(g, "responses", a);
241 f = json_object_new_object();
242 json_object_object_add(a, "200", f);
243 json_object_object_add(f, "description", json_object_new_string(verb->verb));
249 static struct json_object *describe_cb(void *closure)
251 struct api_so_v2 *desc = closure;
252 struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
254 r = make_description(desc);
258 static struct afb_api_itf so_v2_api_itf = {
260 .service_start = service_start_cb,
261 .update_hooks = update_hooks_cb,
262 .get_verbosity = get_verbosity_cb,
263 .set_verbosity = set_verbosity_cb,
264 .describe = describe_cb
267 static struct afb_api_itf so_v2_sync_api_itf = {
268 .call = call_sync_cb,
269 .service_start = service_start_cb,
270 .update_hooks = update_hooks_cb,
271 .get_verbosity = get_verbosity_cb,
272 .set_verbosity = set_verbosity_cb,
273 .describe = describe_cb
276 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)
279 struct api_so_v2 *desc;
280 struct afb_api afb_api;
284 assert(binding->api);
285 assert(binding->verbs);
288 /* allocates the description */
289 desc = calloc(1, sizeof *desc);
291 ERROR("out of memory");
294 desc->binding = binding;
296 desc->handle = handle;
297 desc->service = NULL;
299 /* init the interface */
300 desc->data->verbosity = verbosity;
301 afb_ditf_init_v2(&desc->ditf, binding->api, data);
303 /* init the binding */
304 if (binding->preinit) {
305 INFO("binding %s calling preinit function", binding->api);
306 rc = binding->preinit();
308 ERROR("binding %s preinit function failed...", binding->api);
313 /* records the binding */
314 afb_api.closure = desc;
315 afb_api.itf = binding->noconcurrency ? &so_v2_sync_api_itf : &so_v2_api_itf;
316 if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
317 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
320 INFO("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
329 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
331 const struct afb_binding_v2 *binding;
332 struct afb_binding_data_v2 *data;
334 /* retrieves the register function */
335 binding = dlsym(handle, afb_api_so_v2_descriptor);
336 data = dlsym(handle, afb_api_so_v2_data);
337 if (!binding && !data)
340 INFO("binding [%s] looks like an AFB binding V2", path);
343 if (!binding || !data) {
344 ERROR("binding [%s] incomplete symbol set: %s is missing",
345 path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
348 if (binding->api == NULL || *binding->api == 0) {
349 ERROR("binding [%s] bad api name...", path);
352 if (!afb_api_is_valid_name(binding->api)) {
353 ERROR("binding [%s] invalid api name...", path);
357 if (binding->specification == NULL || *binding->specification == 0) {
358 ERROR("binding [%s] bad specification...", path);
362 if (binding->verbs == NULL) {
363 ERROR("binding [%s] no verbs...", path);
367 return afb_api_so_v2_add_binding(binding, handle, apiset, data);