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 <json-c/json.h>
27 #include <afb/afb-binding-v1.h>
30 #include "afb-api-so-v1.h"
31 #include "afb-apiset.h"
32 #include "afb-export.h"
33 #include "afb-common.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
42 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
43 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
44 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
47 * Description of a binding
50 struct afb_binding_v1 *binding; /* descriptor */
51 void *handle; /* context of dlopen */
52 struct afb_export *export; /* export */
55 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
57 const struct afb_verb_desc_v1 *verb;
59 verb = desc->binding->v1.verbs;
60 while (verb->name && strcasecmp(verb->name, name))
62 return verb->name ? verb : NULL;
65 static void call_cb(void *closure, struct afb_xreq *xreq)
67 const struct afb_verb_desc_v1 *verb;
68 struct api_so_v1 *desc = closure;
70 xreq->request.dynapi = (void*)desc->export; /* hack: this avoids to export afb_export structure */
71 verb = search(desc, xreq->request.verb);
72 afb_xreq_call_verb_v1(xreq, verb);
75 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
77 struct api_so_v1 *desc = closure;
78 return afb_export_start(desc->export, share_session, onneed, apiset);
81 static void update_hooks_cb(void *closure)
83 struct api_so_v1 *desc = closure;
84 afb_export_update_hook(desc->export);
87 static int get_verbosity_cb(void *closure)
89 struct api_so_v1 *desc = closure;
90 return afb_export_verbosity_get(desc->export);
93 static void set_verbosity_cb(void *closure, int level)
95 struct api_so_v1 *desc = closure;
96 afb_export_verbosity_set(desc->export, level);
99 static struct json_object *addperm(struct json_object *o, struct json_object *x)
101 struct json_object *a;
106 if (!json_object_object_get_ex(o, "allOf", &a)) {
107 a = json_object_new_array();
108 json_object_array_add(a, o);
109 o = json_object_new_object();
110 json_object_object_add(o, "allOf", a);
112 json_object_array_add(a, x);
116 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
118 struct json_object *x = json_object_new_object();
119 json_object_object_add(x, key, val);
120 return addperm(o, x);
123 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
125 return addperm_key_val(o, key, json_object_new_string(val));
128 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
130 return addperm_key_val(o, key, json_object_new_int(val));
133 static struct json_object *make_description_openAPIv3(struct api_so_v1 *desc)
136 const struct afb_verb_desc_v1 *verb;
137 struct json_object *r, *f, *a, *i, *p, *g;
139 r = json_object_new_object();
140 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
142 i = json_object_new_object();
143 json_object_object_add(r, "info", i);
144 json_object_object_add(i, "title", json_object_new_string(afb_export_apiname(desc->export)));
145 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
146 json_object_object_add(i, "description", json_object_new_string(desc->binding->v1.info ?: afb_export_apiname(desc->export)));
148 p = json_object_new_object();
149 json_object_object_add(r, "paths", p);
150 verb = desc->binding->v1.verbs;
153 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
154 buffer[sizeof buffer - 1] = 0;
155 f = json_object_new_object();
156 json_object_object_add(p, buffer, f);
157 g = json_object_new_object();
158 json_object_object_add(f, "get", g);
161 if (verb->session & AFB_SESSION_CLOSE_V1)
162 a = addperm_key_valstr(a, "session", "close");
163 if (verb->session & AFB_SESSION_CHECK_V1)
164 a = addperm_key_valstr(a, "session", "check");
165 if (verb->session & AFB_SESSION_RENEW_V1)
166 a = addperm_key_valstr(a, "token", "refresh");
167 if (verb->session & AFB_SESSION_LOA_MASK_V1)
168 a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1);
170 json_object_object_add(g, "x-permissions", a);
172 a = json_object_new_object();
173 json_object_object_add(g, "responses", a);
174 f = json_object_new_object();
175 json_object_object_add(a, "200", f);
176 json_object_object_add(f, "description", json_object_new_string(verb->info));
182 static struct json_object *describe_cb(void *closure)
184 struct api_so_v1 *desc = closure;
186 return make_description_openAPIv3(desc);
189 static struct afb_api_itf so_v1_api_itf = {
191 .service_start = service_start_cb,
192 .update_hooks = update_hooks_cb,
193 .get_verbosity = get_verbosity_cb,
194 .set_verbosity = set_verbosity_cb,
195 .describe = describe_cb
198 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
200 struct api_so_v1 *desc;
201 struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
202 int (*init)(struct afb_service service);
203 void (*onevent)(const char *event, struct json_object *object);
204 struct afb_api afb_api;
205 struct afb_export *export;
207 /* retrieves the register function */
208 register_function = dlsym(handle, afb_api_so_v1_register);
209 if (!register_function)
211 INFO("binding [%s] is a valid AFB binding V1", path);
213 /* allocates the description */
214 init = dlsym(handle, afb_api_so_v1_service_init);
215 onevent = dlsym(handle, afb_api_so_v1_service_event);
216 export = afb_export_create_v1(apiset, path, init, onevent);
217 desc = calloc(1, sizeof *desc);
218 if (desc == NULL || export == NULL) {
219 ERROR("out of memory");
222 desc->export = export;
223 desc->handle = handle;
225 /* init the binding */
226 INFO("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
227 desc->binding = afb_export_register_v1(desc->export, register_function);
228 if (desc->binding == NULL) {
229 ERROR("binding [%s] register function failed. continuing...", path);
233 /* check the returned structure */
234 if (desc->binding->type != AFB_BINDING_VERSION_1) {
235 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
238 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
239 ERROR("binding [%s] bad prefix...", path);
242 if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
243 ERROR("binding [%s] invalid prefix...", path);
246 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
247 ERROR("binding [%s] bad description...", path);
250 if (desc->binding->v1.verbs == NULL) {
251 ERROR("binding [%s] no APIs...", path);
255 /* records the binding */
256 if (!strcmp(path, afb_export_apiname(desc->export)))
257 afb_export_rename(desc->export, desc->binding->v1.prefix);
258 afb_api.closure = desc;
259 afb_api.itf = &so_v1_api_itf;
260 afb_api.group = NULL;
261 if (afb_apiset_add(apiset, afb_export_apiname(desc->export), afb_api) < 0) {
262 ERROR("binding [%s] can't be registered...", path);
265 INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(desc->export));
269 afb_export_destroy(export);