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
26 #include <afb/afb-binding-v2.h>
27 #include <json-c/json.h>
30 #include "afb-api-so-v2.h"
31 #include "afb-apiset.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
45 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
46 static const char afb_api_so_v2_data[] = "afbBindingV2data";
49 * Description of a binding
52 const struct afb_binding_v2 *binding; /* descriptor */
53 struct afb_binding_data_v2 *data; /* data */
54 void *handle; /* context of dlopen */
55 struct afb_svc *service; /* handler for service started */
56 struct afb_ditf ditf; /* daemon interface */
59 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *name)
61 const struct afb_verb_v2 *verb;
63 verb = desc->binding->verbs;
64 while (verb->verb && strcasecmp(verb->verb, name))
66 return verb->verb ? verb : NULL;
70 static void call_cb(void *closure, struct afb_xreq *xreq)
72 struct api_so_v2 *desc = closure;
73 const struct afb_verb_v2 *verb;
75 verb = search(desc, xreq->verb);
76 afb_xreq_call_verb_v2(xreq, verb);
79 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
83 void (*onevent)(const char *event, struct json_object *object);
85 struct api_so_v2 *desc = closure;
88 if (desc->service != NULL) {
89 /* not an error when onneed */
93 /* already started: it is an error */
94 ERROR("Service %s already started", desc->binding->api);
98 /* get the initialisation */
99 start = desc->binding->init;
100 onevent = desc->binding->onevent;
101 if (start == NULL && onevent == NULL) {
102 /* not an error when onneed */
106 /* no initialisation method */
107 ERROR("Binding %s is not a service", desc->binding->api);
111 /* get the event handler if any */
112 desc->service = afb_svc_create(desc->binding->api, apiset, share_session, onevent, &desc->data->service);
113 if (desc->service == NULL) {
115 ERROR("Starting service %s failed", desc->binding->api);
119 /* Starts the service */
120 desc->ditf.state = Daemon_Init;
121 rc = afb_svc_start_v2(desc->service, start);
123 /* initialisation error */
124 ERROR("Initialisation of service %s failed (%d): %m", desc->binding->api, rc);
125 afb_svc_destroy(desc->service, &desc->data->service);
126 desc->service = NULL;
131 desc->ditf.state = Daemon_Run;
135 static void update_hooks_cb(void *closure)
137 struct api_so_v2 *desc = closure;
138 afb_ditf_update_hook(&desc->ditf);
140 afb_svc_update_hook(desc->service);
143 static int get_verbosity_cb(void *closure)
145 struct api_so_v2 *desc = closure;
146 return desc->data->verbosity;
149 static void set_verbosity_cb(void *closure, int level)
151 struct api_so_v2 *desc = closure;
152 desc->data->verbosity = level;
155 static struct json_object *addperm(struct json_object *o, struct json_object *x)
157 struct json_object *a;
162 if (!json_object_object_get_ex(o, "allOf", &a)) {
163 a = json_object_new_array();
164 json_object_array_add(a, o);
165 o = json_object_new_object();
166 json_object_object_add(o, "allOf", a);
168 json_object_array_add(a, x);
172 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
174 struct json_object *x = json_object_new_object();
175 json_object_object_add(x, key, val);
176 return addperm(o, x);
179 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
181 return addperm_key_val(o, key, json_object_new_string(val));
184 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
186 return addperm_key_val(o, key, json_object_new_int(val));
189 static struct json_object *make_description_openAPIv3(struct api_so_v2 *desc)
192 const struct afb_verb_v2 *verb;
193 struct json_object *r, *f, *a, *i, *p, *g;
195 r = json_object_new_object();
196 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
198 i = json_object_new_object();
199 json_object_object_add(r, "info", i);
200 json_object_object_add(i, "title", json_object_new_string(desc->binding->api));
201 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
202 json_object_object_add(i, "description", json_object_new_string(desc->binding->info ?: desc->binding->api));
204 p = json_object_new_object();
205 json_object_object_add(r, "paths", p);
206 verb = desc->binding->verbs;
209 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
210 buffer[sizeof buffer - 1] = 0;
211 f = json_object_new_object();
212 json_object_object_add(p, buffer, f);
213 g = json_object_new_object();
214 json_object_object_add(f, "get", g);
217 if (verb->session & AFB_SESSION_CLOSE_V2)
218 a = addperm_key_valstr(a, "session", "close");
219 if (verb->session & AFB_SESSION_CHECK_V2)
220 a = addperm_key_valstr(a, "session", "check");
221 if (verb->session & AFB_SESSION_REFRESH_V2)
222 a = addperm_key_valstr(a, "token", "refresh");
223 if (verb->session & AFB_SESSION_LOA_MASK_V2)
224 a = addperm_key_valint(a, "LOA", verb->session & AFB_SESSION_LOA_MASK_V2);
230 json_object_object_add(g, "x-permissions", a);
232 a = json_object_new_object();
233 json_object_object_add(g, "responses", a);
234 f = json_object_new_object();
235 json_object_object_add(a, "200", f);
236 json_object_object_add(f, "description", json_object_new_string(verb->info?:verb->verb));
242 static struct json_object *describe_cb(void *closure)
244 struct api_so_v2 *desc = closure;
245 struct json_object *r = desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
247 r = make_description_openAPIv3(desc);
251 static struct afb_api_itf so_v2_api_itf = {
253 .service_start = service_start_cb,
254 .update_hooks = update_hooks_cb,
255 .get_verbosity = get_verbosity_cb,
256 .set_verbosity = set_verbosity_cb,
257 .describe = describe_cb
260 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)
263 struct api_so_v2 *desc;
264 struct afb_api afb_api;
268 assert(binding->api);
269 assert(binding->verbs);
272 /* allocates the description */
273 desc = calloc(1, sizeof *desc);
275 ERROR("out of memory");
278 desc->binding = binding;
280 desc->handle = handle;
281 desc->service = NULL;
283 /* init the interface */
284 desc->data->verbosity = verbosity;
285 afb_ditf_init_v2(&desc->ditf, binding->api, data);
287 /* init the binding */
288 if (binding->preinit) {
289 INFO("binding %s calling preinit function", binding->api);
290 rc = binding->preinit();
292 ERROR("binding %s preinit function failed...", binding->api);
297 /* records the binding */
298 afb_api.closure = desc;
299 afb_api.itf = &so_v2_api_itf;
300 afb_api.noconcurrency = binding->noconcurrency;
301 if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
302 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
305 INFO("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
314 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
316 const struct afb_binding_v2 *binding;
317 struct afb_binding_data_v2 *data;
319 /* retrieves the register function */
320 binding = dlsym(handle, afb_api_so_v2_descriptor);
321 data = dlsym(handle, afb_api_so_v2_data);
322 if (!binding && !data)
325 INFO("binding [%s] looks like an AFB binding V2", path);
328 if (!binding || !data) {
329 ERROR("binding [%s] incomplete symbol set: %s is missing",
330 path, binding ? afb_api_so_v2_data : afb_api_so_v2_descriptor);
333 if (binding->api == NULL || *binding->api == 0) {
334 ERROR("binding [%s] bad api name...", path);
337 if (!afb_api_is_valid_name(binding->api)) {
338 ERROR("binding [%s] invalid api name...", path);
342 if (binding->specification == NULL || *binding->specification == 0) {
343 ERROR("binding [%s] bad specification...", path);
347 if (binding->verbs == NULL) {
348 ERROR("binding [%s] no verbs...", path);
352 return afb_api_so_v2_add_binding(binding, handle, apiset, data);