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>
28 #define AFB_BINDING_VERSION 0
29 #include <afb/afb-binding.h>
32 #include "afb-api-v3.h"
33 #include "afb-apiset.h"
35 #include "afb-export.h"
38 #include "sig-monitor.h"
41 * Description of a binding
46 struct afb_verb_v3 **verbs;
47 const struct afb_verb_v2 *verbsv2;
48 const struct afb_verb_v3 *verbsv3;
49 struct afb_export *export;
53 static const char nulchar = 0;
55 static int verb_name_compare(const struct afb_verb_v3 *verb, const char *name)
58 ? fnmatch(verb->verb, name, FNM_NOESCAPE|FNM_PATHNAME|FNM_CASEFOLD|FNM_PERIOD)
59 : strcasecmp(verb->verb, name);
62 static struct afb_verb_v3 *search_dynamic_verb(struct afb_api_v3 *api, const char *name)
64 struct afb_verb_v3 **v, **e, *i;
70 if (!verb_name_compare(i, name))
77 void afb_api_v3_process_call(struct afb_api_v3 *api, struct afb_xreq *xreq)
79 const struct afb_verb_v3 *verbsv3;
80 const struct afb_verb_v2 *verbsv2;
83 name = xreq->request.called_verb;
85 /* look first in dynamic set */
86 verbsv3 = search_dynamic_verb(api, name);
88 /* look then in static set */
89 verbsv3 = api->verbsv3;
93 else if (!verb_name_compare(verbsv3, name))
99 /* is it a v3 verb ? */
102 xreq->request.vcbdata = verbsv3->vcbdata;
103 afb_xreq_call_verb_v3(xreq, verbsv3);
107 /* look in legacy set */
108 verbsv2 = api->verbsv2;
110 while (verbsv2->verb) {
111 if (strcasecmp(verbsv2->verb, name))
114 afb_xreq_call_verb_v2(xreq, verbsv2);
120 afb_xreq_reply_unknown_verb(xreq);
123 static struct json_object *describe_verb_v3(const struct afb_verb_v3 *verb)
125 struct json_object *f, *a, *g;
127 f = json_object_new_object();
129 g = json_object_new_object();
130 json_object_object_add(f, "get", g);
132 a = afb_auth_json_v2(verb->auth, verb->session);
134 json_object_object_add(g, "x-permissions", a);
136 a = json_object_new_object();
137 json_object_object_add(g, "responses", a);
138 g = json_object_new_object();
139 json_object_object_add(a, "200", g);
140 json_object_object_add(g, "description", json_object_new_string(verb->info?:verb->verb));
145 struct json_object *afb_api_v3_make_description_openAPIv3(struct afb_api_v3 *api, const char *apiname)
148 struct afb_verb_v3 **iter, **end;
149 const struct afb_verb_v3 *verb;
150 struct json_object *r, *i, *p;
152 r = json_object_new_object();
153 json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
155 i = json_object_new_object();
156 json_object_object_add(r, "info", i);
157 json_object_object_add(i, "title", json_object_new_string(apiname));
158 json_object_object_add(i, "version", json_object_new_string("0.0.0"));
159 json_object_object_add(i, "description", json_object_new_string(api->info));
161 p = json_object_new_object();
162 json_object_object_add(r, "paths", p);
164 end = iter + api->count;
165 while (iter != end) {
168 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
169 json_object_object_add(p, buffer, describe_verb_v3(verb));
175 strncpy(buffer + 1, verb->verb, sizeof buffer - 1);
176 json_object_object_add(p, buffer, describe_verb_v3(verb));
182 struct afb_api_v3 *afb_api_v3_create(struct afb_apiset *declare_set,
183 struct afb_apiset *call_set,
187 int (*preinit)(void*, struct afb_api_x3 *),
190 struct afb_export* creator,
193 struct afb_api_v3 *api;
195 /* allocates the description */
196 api = calloc(1, sizeof *api + (copy_info && info ? 1 + strlen(info) : 0));
198 ERROR("out of memory");
203 api->info = &nulchar;
205 api->info = strcpy((char*)(api + 1), info);
209 api->export = afb_export_create_v3(declare_set, call_set, apiname, api, creator, path);
213 if (afb_export_declare(api->export, noconcurrency) < 0)
216 if (preinit && afb_export_preinit_x3(api->export, preinit, closure) < 0)
222 afb_export_undeclare(api->export);
224 afb_export_unref(api->export);
231 struct afb_api_v3 *afb_api_v3_addref(struct afb_api_v3 *api)
234 __atomic_add_fetch(&api->refcount, 1, __ATOMIC_RELAXED);
238 void afb_api_v3_unref(struct afb_api_v3 *api)
240 if (api && !__atomic_sub_fetch(&api->refcount, 1, __ATOMIC_RELAXED)) {
241 afb_export_destroy(api->export);
243 free(api->verbs[--api->count]);
249 struct afb_export *afb_api_v3_export(struct afb_api_v3 *api)
254 void afb_api_v3_set_verbs_v2(
255 struct afb_api_v3 *api,
256 const struct afb_verb_v2 *verbs)
258 api->verbsv2 = verbs;
261 void afb_api_v3_set_verbs_v3(
262 struct afb_api_v3 *api,
263 const struct afb_verb_v3 *verbs)
265 api->verbsv3 = verbs;
268 int afb_api_v3_add_verb(
269 struct afb_api_v3 *api,
272 void (*callback)(struct afb_req_x2 *req),
274 const struct afb_auth *auth,
278 struct afb_verb_v3 *v, **vv;
282 for (i = 0 ; i < api->count ; i++) {
284 if (glob == v->glob && !strcasecmp(verb, v->verb)) {
285 /* refuse to redefine a dynamic verb */
291 vv = realloc(api->verbs, (1 + api->count) * sizeof *vv);
296 v = malloc(sizeof *v + (1 + strlen(verb)) + (info ? 1 + strlen(info) : 0));
300 v->callback = callback;
301 v->vcbdata = vcbdata;
303 v->session = session;
306 txt = (char*)(v + 1);
308 txt = stpcpy(txt, verb);
316 api->verbs[api->count++] = v;
323 int afb_api_v3_del_verb(
324 struct afb_api_v3 *api,
328 struct afb_verb_v3 *v;
331 for (i = 0 ; i < api->count ; i++) {
333 if (!strcasecmp(verb, v->verb)) {
334 api->verbs[i] = api->verbs[--api->count];
336 *vcbdata = v->vcbdata;
346 int afb_api_v3_set_binding_fields(const struct afb_binding_v3 *desc, struct afb_api_x3 *api)
350 rc = afb_api_x3_set_verbs_v3(api, desc->verbs);
351 if (!rc && desc->onevent)
352 rc = afb_api_x3_on_event(api, desc->onevent);
353 if (!rc && desc->init)
354 rc = afb_api_x3_on_init(api, desc->init);
355 if (!rc && desc->provide_class)
356 rc = afb_api_x3_provide_class(api, desc->provide_class);
357 if (!rc && desc->require_class)
358 rc = afb_api_x3_require_class(api, desc->require_class);
359 if (!rc && desc->require_api)
360 rc = afb_api_x3_require_api(api, desc->require_api, 1);
364 struct safe_preinit_data
366 int (*preinit)(struct afb_api_x3 *);
367 struct afb_api_x3 *api;
371 static void safe_preinit(int sig, void *closure)
373 struct safe_preinit_data *spd = closure;
375 spd->result = spd->preinit(spd->api);
382 int afb_api_v3_safe_preinit(struct afb_api_x3 *api, int (*preinit)(struct afb_api_x3 *))
384 struct safe_preinit_data spd;
386 spd.preinit = preinit;
388 sig_monitor(60, safe_preinit, &spd);
392 static int init_binding(void *closure, struct afb_api_x3 *api)
394 const struct afb_binding_v3 *desc = closure;
395 int rc = afb_api_v3_set_binding_fields(desc, api);
396 if (!rc && desc->preinit)
397 rc = afb_api_v3_safe_preinit(api, desc->preinit);
401 struct afb_api_v3 *afb_api_v3_from_binding(const struct afb_binding_v3 *desc, struct afb_apiset *declare_set, struct afb_apiset * call_set)
403 return afb_api_v3_create(declare_set, call_set, desc->api, desc->info, desc->noconcurrency, init_binding, (void*)desc, 0, NULL, NULL);