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 <json-c/json.h>
28 #include <afb/afb-binding-v1.h>
31 #include "afb-api-so-v1.h"
32 #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_v1_register[] = "afbBindingV1Register";
46 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
47 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
50 * Description of a binding
53 struct afb_binding_v1 *binding; /* descriptor */
54 void *handle; /* context of dlopen */
55 struct afb_svc *service; /* handler for service started */
56 struct afb_binding_interface_v1 interface;
57 struct afb_ditf ditf; /* daemon interface */
60 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
62 const struct afb_verb_desc_v1 *verb;
64 verb = desc->binding->v1.verbs;
65 while (verb->name && strcasecmp(verb->name, name))
67 return verb->name ? verb : NULL;
70 static void call_cb(void *closure, struct afb_xreq *xreq)
72 const struct afb_verb_desc_v1 *verb;
73 struct api_so_v1 *desc = closure;
75 verb = search(desc, xreq->verb);
76 afb_xreq_call_verb_v1(xreq, verb);
79 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
81 int (*init)(struct afb_service service);
82 void (*onevent)(const char *event, struct json_object *object);
84 struct api_so_v1 *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->binding->v1.prefix);
97 /* get the initialisation */
98 init = dlsym(desc->handle, afb_api_so_v1_service_init);
99 onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
100 if (init == NULL && onevent == NULL) {
101 /* not an error when onneed */
105 /* no initialisation method */
106 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
110 /* get the event handler if any */
111 desc->service = afb_svc_create_v1(desc->binding->v1.prefix, apiset, share_session, init, onevent);
112 if (desc->service == NULL) {
114 ERROR("Starting service %s failed", desc->binding->v1.prefix);
121 static void update_hooks_cb(void *closure)
123 struct api_so_v1 *desc = closure;
124 afb_ditf_update_hook(&desc->ditf);
126 afb_svc_update_hook(desc->service);
129 static int get_verbosity_cb(void *closure)
131 struct api_so_v1 *desc = closure;
132 return desc->interface.verbosity;
135 static void set_verbosity_cb(void *closure, int level)
137 struct api_so_v1 *desc = closure;
138 desc->interface.verbosity = level;
141 static struct json_object *addperm(struct json_object *o, struct json_object *x)
143 struct json_object *a;
148 if (!json_object_object_get_ex(o, "allOf", &a)) {
149 a = json_object_new_array();
150 json_object_array_add(a, o);
151 o = json_object_new_object();
152 json_object_object_add(o, "allOf", a);
154 json_object_array_add(a, x);
158 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
160 struct json_object *x = json_object_new_object();
161 json_object_object_add(x, key, val);
162 return addperm(o, x);
165 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
167 return addperm_key_val(o, key, json_object_new_string(val));
170 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
172 return addperm_key_val(o, key, json_object_new_int(val));
175 static struct json_object *make_description_openAPIv3(struct api_so_v1 *desc)
178 const struct afb_verb_desc_v1 *verb;
179 struct json_object *r, *f, *a, *i, *p, *g;
181 r = json_object_new_object();
182 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
184 i = json_object_new_object();
185 json_object_object_add(r, "info", i);
186 json_object_object_add(i, "title", json_object_new_string(desc->binding->v1.prefix));
187 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
188 json_object_object_add(i, "description", json_object_new_string(desc->binding->v1.info ?: desc->binding->v1.prefix));
190 p = json_object_new_object();
191 json_object_object_add(r, "paths", p);
192 verb = desc->binding->v1.verbs;
195 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
196 buffer[sizeof buffer - 1] = 0;
197 f = json_object_new_object();
198 json_object_object_add(p, buffer, f);
199 g = json_object_new_object();
200 json_object_object_add(f, "get", g);
203 if (verb->session & AFB_SESSION_CLOSE_V1)
204 a = addperm_key_valstr(a, "session", "close");
205 if (verb->session & AFB_SESSION_CHECK_V1)
206 a = addperm_key_valstr(a, "session", "check");
207 if (verb->session & AFB_SESSION_RENEW_V1)
208 a = addperm_key_valstr(a, "token", "refresh");
209 if (verb->session & AFB_SESSION_LOA_MASK_V1)
210 a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1);
212 json_object_object_add(g, "x-permissions", a);
214 a = json_object_new_object();
215 json_object_object_add(g, "responses", a);
216 f = json_object_new_object();
217 json_object_object_add(a, "200", f);
218 json_object_object_add(f, "description", json_object_new_string(verb->info));
224 static struct json_object *describe_cb(void *closure)
226 struct api_so_v1 *desc = closure;
228 return make_description_openAPIv3(desc);
231 static struct afb_api_itf so_v1_api_itf = {
233 .service_start = service_start_cb,
234 .update_hooks = update_hooks_cb,
235 .get_verbosity = get_verbosity_cb,
236 .set_verbosity = set_verbosity_cb,
237 .describe = describe_cb
240 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
242 struct api_so_v1 *desc;
243 struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
244 struct afb_api afb_api;
246 /* retrieves the register function */
247 register_function = dlsym(handle, afb_api_so_v1_register);
248 if (!register_function)
250 INFO("binding [%s] is a valid AFB binding V1", path);
252 /* allocates the description */
253 desc = calloc(1, sizeof *desc);
255 ERROR("out of memory");
258 desc->handle = handle;
260 /* init the interface */
261 afb_ditf_init_v1(&desc->ditf, path, &desc->interface);
263 /* init the binding */
264 INFO("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
265 desc->binding = register_function(&desc->interface);
266 if (desc->binding == NULL) {
267 ERROR("binding [%s] register function failed. continuing...", path);
271 /* check the returned structure */
272 if (desc->binding->type != AFB_BINDING_VERSION_1) {
273 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
276 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
277 ERROR("binding [%s] bad prefix...", path);
280 if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
281 ERROR("binding [%s] invalid prefix...", path);
284 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
285 ERROR("binding [%s] bad description...", path);
288 if (desc->binding->v1.verbs == NULL) {
289 ERROR("binding [%s] no APIs...", path);
293 /* records the binding */
294 afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
295 afb_api.closure = desc;
296 afb_api.itf = &so_v1_api_itf;
297 if (afb_apiset_add(apiset, desc->binding->v1.prefix, afb_api) < 0) {
298 ERROR("binding [%s] can't be registered...", path);
301 INFO("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);