api-v3: First draft
[src/app-framework-binder.git] / src / afb-api-so-v1.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24 #include <stdarg.h>
25
26 #include <json-c/json.h>
27 #include <afb/afb-binding-v1.h>
28
29 #include "afb-api.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"
36 #include "afb-xreq.h"
37 #include "verbose.h"
38
39 /*
40  * names of symbols
41  */
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";
45
46 static const struct afb_verb_desc_v1 *search(struct afb_binding_v1 *binding, const char *name)
47 {
48         const struct afb_verb_desc_v1 *verb;
49
50         verb = binding->v1.verbs;
51         while (verb->name && strcasecmp(verb->name, name))
52                 verb++;
53         return verb->name ? verb : NULL;
54 }
55
56 void afb_api_so_v1_process_call(struct afb_binding_v1 *binding, struct afb_xreq *xreq)
57 {
58         const struct afb_verb_desc_v1 *verb;
59
60         verb = search(binding, xreq->request.called_verb);
61         afb_xreq_call_verb_v1(xreq, verb);
62 }
63
64 static struct json_object *addperm(struct json_object *o, struct json_object *x)
65 {
66         struct json_object *a;
67
68         if (!o)
69                 return x;
70
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);
76         }
77         json_object_array_add(a, x);
78         return o;
79 }
80
81 static struct json_object *addperm_key_val(struct json_object *o, const char *key, struct json_object *val)
82 {
83         struct json_object *x = json_object_new_object();
84         json_object_object_add(x, key, val);
85         return addperm(o, x);
86 }
87
88 static struct json_object *addperm_key_valstr(struct json_object *o, const char *key, const char *val)
89 {
90         return addperm_key_val(o, key, json_object_new_string(val));
91 }
92
93 static struct json_object *addperm_key_valint(struct json_object *o, const char *key, int val)
94 {
95         return addperm_key_val(o, key, json_object_new_int(val));
96 }
97
98 struct json_object *afb_api_so_v1_make_description_openAPIv3(struct afb_binding_v1 *binding, const char *apiname)
99 {
100         char buffer[256];
101         const struct afb_verb_desc_v1 *verb;
102         struct json_object *r, *f, *a, *i, *p, *g;
103
104         r = json_object_new_object();
105         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
106
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));
112
113         p = json_object_new_object();
114         json_object_object_add(r, "paths", p);
115         verb = binding->v1.verbs;
116         while (verb->name) {
117                 buffer[0] = '/';
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);
124
125                 a = NULL;
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);
134                 if (a)
135                         json_object_object_add(g, "x-permissions", a);
136
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));
142                 verb++;
143         }
144         return r;
145 }
146
147 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
148 {
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;
154
155         /* retrieves the register function */
156         register_function = dlsym(handle, afb_api_so_v1_register);
157         if (!register_function)
158                 return 0;
159
160         INFO("binding [%s] is a valid AFB binding V1", path);
161
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);
168                 goto error;
169         }
170         binding = afb_export_register_v1(export, register_function);
171         if (binding == NULL) {
172                 ERROR("binding [%s] register failure...", path);
173                 goto error;
174         }
175
176         /* check the returned structure */
177         if (binding->type != AFB_BINDING_VERSION_1) {
178                 ERROR("binding [%s] invalid type %d...", path, binding->type);
179                 goto error;
180         }
181         if (binding->v1.prefix == NULL || *binding->v1.prefix == 0) {
182                 ERROR("binding [%s] bad prefix...", path);
183                 goto error;
184         }
185         if (!afb_api_is_valid_name(binding->v1.prefix)) {
186                 ERROR("binding [%s] invalid prefix...", path);
187                 goto error;
188         }
189         if (binding->v1.info == NULL || *binding->v1.info == 0) {
190                 ERROR("binding [%s] bad description...", path);
191                 goto error;
192         }
193         if (binding->v1.verbs == NULL) {
194                 ERROR("binding [%s] no verbs...", path);
195                 goto error;
196         }
197
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);
202                         goto error;
203                 }
204         }
205
206         if (afb_export_declare(export, 0) < 0) {
207                 ERROR("binding [%s] can't be registered...", path);
208                 goto error;
209         }
210         INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(export));
211         afb_export_unref(export);
212         return 1;
213
214 error:
215         afb_export_unref(export);
216
217         return -1;
218 }
219