Update copyright dates
[src/app-framework-binder.git] / src / afb-api-so-v1.c
1 /*
2  * Copyright (C) 2015-2020 "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 #if WITH_LEGACY_BINDING_V1 && WITH_DYNAMIC_BINDING
19
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <dlfcn.h>
25 #include <assert.h>
26 #include <stdarg.h>
27
28 #include <json-c/json.h>
29 #include <afb/afb-binding-v1.h>
30
31 #include "afb-api.h"
32 #include "afb-api-so-v1.h"
33 #include "afb-apiset.h"
34 #include "afb-export.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
38 #include "afb-xreq.h"
39 #include "afb-auth.h"
40 #include "verbose.h"
41
42 /*
43  * names of symbols
44  */
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";
48
49 static const struct afb_verb_desc_v1 *search(struct afb_binding_v1 *binding, const char *name)
50 {
51         const struct afb_verb_desc_v1 *verb;
52
53         verb = binding->v1.verbs;
54         while (verb->name && strcasecmp(verb->name, name))
55                 verb++;
56         return verb->name ? verb : NULL;
57 }
58
59 void afb_api_so_v1_process_call(struct afb_binding_v1 *binding, struct afb_xreq *xreq)
60 {
61         const struct afb_verb_desc_v1 *verb;
62
63         verb = search(binding, xreq->request.called_verb);
64         afb_xreq_call_verb_v1(xreq, verb);
65 }
66
67 struct json_object *afb_api_so_v1_make_description_openAPIv3(struct afb_binding_v1 *binding, const char *apiname)
68 {
69         char buffer[256];
70         const struct afb_verb_desc_v1 *verb;
71         struct json_object *r, *f, *a, *i, *p, *g;
72
73         r = json_object_new_object();
74         json_object_object_add(r, "openapi", json_object_new_string("3.0.0"));
75
76         i = json_object_new_object();
77         json_object_object_add(r, "info", i);
78         json_object_object_add(i, "title", json_object_new_string(apiname));
79         json_object_object_add(i, "version", json_object_new_string("0.0.0"));
80         json_object_object_add(i, "description", json_object_new_string(binding->v1.info ?: apiname));
81
82         p = json_object_new_object();
83         json_object_object_add(r, "paths", p);
84         verb = binding->v1.verbs;
85         while (verb->name) {
86                 buffer[0] = '/';
87                 strncpy(buffer + 1, verb->name, sizeof buffer - 1);
88                 buffer[sizeof buffer - 1] = 0;
89                 f = json_object_new_object();
90                 json_object_object_add(p, buffer, f);
91                 g = json_object_new_object();
92                 json_object_object_add(f, "get", g);
93
94                 a = afb_auth_json_x1(verb->session);
95                 if (a)
96                         json_object_object_add(g, "x-permissions", a);
97
98                 a = json_object_new_object();
99                 json_object_object_add(g, "responses", a);
100                 f = json_object_new_object();
101                 json_object_object_add(a, "200", f);
102                 json_object_object_add(f, "description", json_object_new_string(verb->info));
103                 verb++;
104         }
105         return r;
106 }
107
108 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *declare_set, struct afb_apiset * call_set)
109 {
110         struct afb_binding_v1 *binding; /* descriptor */
111         struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
112         int (*init)(struct afb_service_x1 service);
113         void (*onevent)(const char *event, struct json_object *object);
114         struct afb_export *export;
115
116         /* retrieves the register function */
117         register_function = dlsym(handle, afb_api_so_v1_register);
118         if (!register_function)
119                 return 0;
120
121         INFO("binding [%s] is a valid AFB binding V1", path);
122
123         /* allocates the description */
124         init = dlsym(handle, afb_api_so_v1_service_init);
125         onevent = dlsym(handle, afb_api_so_v1_service_event);
126         export = afb_export_create_v1(declare_set, call_set, path, init, onevent, path);
127         if (export == NULL) {
128                 ERROR("binding [%s] creation failure...", path);
129                 goto error;
130         }
131         binding = afb_export_register_v1(export, register_function);
132         if (binding == NULL) {
133                 ERROR("binding [%s] register failure...", path);
134                 goto error;
135         }
136
137         /* check the returned structure */
138         if (binding->type != AFB_BINDING_VERSION_1) {
139                 ERROR("binding [%s] invalid type %d...", path, binding->type);
140                 goto error;
141         }
142         if (binding->v1.prefix == NULL || *binding->v1.prefix == 0) {
143                 ERROR("binding [%s] bad prefix...", path);
144                 goto error;
145         }
146         if (!afb_api_is_valid_name(binding->v1.prefix)) {
147                 ERROR("binding [%s] invalid prefix...", path);
148                 goto error;
149         }
150         if (binding->v1.info == NULL || *binding->v1.info == 0) {
151                 ERROR("binding [%s] bad description...", path);
152                 goto error;
153         }
154         if (binding->v1.verbs == NULL) {
155                 ERROR("binding [%s] no verbs...", path);
156                 goto error;
157         }
158
159         /* records the binding */
160         if (!strcmp(path, afb_export_apiname(export))) {
161                 if (afb_export_rename(export, binding->v1.prefix) < 0) {
162                         ERROR("binding [%s] can't be renamed to %s", path, binding->v1.prefix);
163                         goto error;
164                 }
165         }
166
167         if (afb_export_declare(export, 0) < 0) {
168                 ERROR("binding [%s] can't be registered...", path);
169                 goto error;
170         }
171         INFO("binding %s loaded with API prefix %s", path, afb_export_apiname(export));
172         afb_export_unref(export);
173         return 1;
174
175 error:
176         afb_export_unref(export);
177
178         return -1;
179 }
180
181 #endif
182