2 * Copyright (C) 2016, 2017, 2018 "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.
26 #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";
46 static const struct afb_verb_desc_v1 *search(struct afb_binding_v1 *binding, const char *name)
48 const struct afb_verb_desc_v1 *verb;
50 verb = binding->v1.verbs;
51 while (verb->name && strcasecmp(verb->name, name))
53 return verb->name ? verb : NULL;
56 void afb_api_so_v1_process_call(struct afb_binding_v1 *binding, struct afb_xreq *xreq)
58 const struct afb_verb_desc_v1 *verb;
60 verb = search(binding, xreq->request.called_verb);
61 afb_xreq_call_verb_v1(xreq, verb);
64 static struct json_object *addperm(struct json_object *o, struct json_object *x)
66 struct json_object *a;
71 if (!json_object_object_get_ex(o, "allOf", &a)) {
72 a = json_object_new_array();
73 json_object_array_add(a, o);
74 o = json_object_new_object();
75 json_object_object_add(o, "allOf", a);
77 json_object_array_add(a, x);
81 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
83 struct json_object *x = json_object_new_object();
84 json_object_object_add(x, key, val);
88 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
90 return addperm_key_val(o, key, json_object_new_string(val));
93 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
95 return addperm_key_val(o, key, json_object_new_int(val));
98 struct json_object *afb_api_so_v1_make_description_openAPIv3(struct afb_binding_v1 *binding, const char *apiname)
101 const struct afb_verb_desc_v1 *verb;
102 struct json_object *r, *f, *a, *i, *p, *g;
104 r = json_object_new_object();
105 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
107 i = json_object_new_object();
108 json_object_object_add(r, "info", i);
109 json_object_object_add(i, "title", json_object_new_string(apiname));
110 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
111 json_object_object_add(i, "description", json_object_new_string(binding->v1.info ?: apiname));
113 p = json_object_new_object();
114 json_object_object_add(r, "paths", p);
115 verb = binding->v1.verbs;
118 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
119 buffer[sizeof buffer - 1] = 0;
120 f = json_object_new_object();
121 json_object_object_add(p, buffer, f);
122 g = json_object_new_object();
123 json_object_object_add(f, "get", g);
126 if (verb->session & AFB_SESSION_CLOSE_X1)
127 a = addperm_key_valstr(a, "session", "close");
128 if (verb->session & AFB_SESSION_CHECK_X1)
129 a = addperm_key_valstr(a, "session", "check");
130 if (verb->session & AFB_SESSION_RENEW_X1)
131 a = addperm_key_valstr(a, "token", "refresh");
132 if (verb->session & AFB_SESSION_LOA_MASK_X1)
133 a = addperm_key_valint(a, "LOA", (verb->session >> AFB_SESSION_LOA_SHIFT_X1) & AFB_SESSION_LOA_MASK_X1);
135 json_object_object_add(g, "x-permissions", a);
137 a = json_object_new_object();
138 json_object_object_add(g, "responses", a);
139 f = json_object_new_object();
140 json_object_object_add(a, "200", f);
141 json_object_object_add(f, "description", json_object_new_string(verb->info));
147 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
149 struct afb_binding_v1 *binding; /* descriptor */
150 struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
151 int (*init)(struct afb_service_x1 service);
152 void (*onevent)(const char *event, struct json_object *object);
153 struct afb_export *export;
155 /* retrieves the register function */
156 register_function = dlsym(handle, afb_api_so_v1_register);
157 if (!register_function)
160 INFO("binding [%s] is a valid AFB binding V1", path);
162 /* allocates the description */
163 init = dlsym(handle, afb_api_so_v1_service_init);
164 onevent = dlsym(handle, afb_api_so_v1_service_event);
165 export = afb_export_create_v1(declare_set, call_set, path, init, onevent);
166 if (export == NULL) {
167 ERROR("binding [%s] creation failure...", path);
170 binding = afb_export_register_v1(export, register_function);
171 if (binding == NULL) {
172 ERROR("binding [%s] register failure...", path);
176 /* check the returned structure */
177 if (binding->type != AFB_BINDING_VERSION_1) {
178 ERROR("binding [%s] invalid type %d...", path, binding->type);
181 if (binding->v1.prefix == NULL || *binding->v1.prefix == 0) {
182 ERROR("binding [%s] bad prefix...", path);
185 if (!afb_api_is_valid_name(binding->v1.prefix)) {
186 ERROR("binding [%s] invalid prefix...", path);
189 if (binding->v1.info == NULL || *binding->v1.info == 0) {
190 ERROR("binding [%s] bad description...", path);
193 if (binding->v1.verbs == NULL) {
194 ERROR("binding [%s] no verbs...", path);
198 /* records the binding */
199 if (!strcmp(path, afb_export_apiname(export))) {
200 if (afb_export_rename(export, binding->v1.prefix) < 0) {
201 ERROR("binding [%s] can't be renamed to %s", path, binding->v1.prefix);
206 if (afb_export_declare(export, 0) < 0) {
207 ERROR("binding [%s] can't be registered...", path);
210 INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(export));
211 afb_export_unref(export);
215 afb_export_unref(export);