afb-api: add a 'describe' function
[src/app-framework-binder.git] / src / afb-api-so-v1.c
1 /*
2  * Copyright (C) 2016, 2017 "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 #define NO_BINDING_VERBOSE_MACRO
20
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <json-c/json.h>
26
27 #include <afb/afb-binding.h>
28
29 #include "afb-api.h"
30 #include "afb-apiset.h"
31 #include "afb-svc.h"
32 #include "afb-evt.h"
33 #include "afb-common.h"
34 #include "afb-context.h"
35 #include "afb-api-so.h"
36 #include "afb-xreq.h"
37 #include "afb-ditf.h"
38 #include "verbose.h"
39
40 /*
41  * names of symbols
42  */
43 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
44 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
45 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
46
47 /*
48  * Description of a binding
49  */
50 struct api_so_v1 {
51         struct afb_binding *binding;    /* descriptor */
52         void *handle;                   /* context of dlopen */
53         struct afb_svc *service;        /* handler for service started */
54         struct afb_ditf ditf;           /* daemon interface */
55 };
56
57 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
58 {
59         const struct afb_verb_desc_v1 *verb;
60
61         verb = desc->binding->v1.verbs;
62         while (verb->name && strcasecmp(verb->name, name))
63                 verb++;
64         return verb->name ? verb : NULL;
65 }
66
67 static void call_cb(void *closure, struct afb_xreq *xreq)
68 {
69         const struct afb_verb_desc_v1 *verb;
70         struct api_so_v1 *desc = closure;
71
72         verb = search(desc, xreq->verb);
73         if (!verb)
74                 afb_xreq_fail_unknown_verb(xreq);
75         else
76                 if (!xreq_session_check_apply(xreq, verb->session))
77                         afb_xreq_call(xreq, verb->callback);
78 }
79
80 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
81 {
82         int (*init)(struct afb_service service);
83         void (*onevent)(const char *event, struct json_object *object);
84
85         struct api_so_v1 *desc = closure;
86
87         /* check state */
88         if (desc->service != NULL) {
89                 /* not an error when onneed */
90                 if (onneed != 0)
91                         return 0;
92
93                 /* already started: it is an error */
94                 ERROR("Service %s already started", desc->binding->v1.prefix);
95                 return -1;
96         }
97
98         /* get the initialisation */
99         init = dlsym(desc->handle, afb_api_so_v1_service_init);
100         if (init == NULL) {
101                 /* not an error when onneed */
102                 if (onneed != 0)
103                         return 0;
104
105                 /* no initialisation method */
106                 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
107                 return -1;
108         }
109
110         /* get the event handler if any */
111         onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
112         desc->service = afb_svc_create(apiset, share_session, init, onevent);
113         if (desc->service == NULL) {
114                 /* starting error */
115                 ERROR("Starting service %s failed", desc->binding->v1.prefix);
116                 return -1;
117         }
118
119         return 0;
120 }
121
122 static void update_hooks_cb(void *closure)
123 {
124         struct api_so_v1 *desc = closure;
125         afb_ditf_update_hook(&desc->ditf);
126 }
127
128 static int get_verbosity_cb(void *closure)
129 {
130         struct api_so_v1 *desc = closure;
131         return desc->ditf.interface.verbosity;
132 }
133
134 static void set_verbosity_cb(void *closure, int level)
135 {
136         struct api_so_v1 *desc = closure;
137         desc->ditf.interface.verbosity = level;
138 }
139
140 struct json_object *describe_cb(void *closure)
141 {
142         struct api_so_v1 *desc = closure;
143         const struct afb_verb_desc_v1 *verb;
144         struct json_object *r, *v, *f, *a;
145
146         r = json_object_new_object();
147         json_object_object_add(r, "version", json_object_new_int(1));
148         json_object_object_add(r, "info", json_object_new_string(desc->binding->v1.info));
149         v = json_object_new_object();
150         json_object_object_add(r, "verbs", v);
151         verb = desc->binding->v1.verbs;
152         while (verb->name) {
153                 f = json_object_new_object();
154                 a = json_object_new_array();
155                 json_object_object_add(f, "name", json_object_new_string(verb->name));
156                 json_object_object_add(f, "info", json_object_new_string(verb->info));
157                 if (verb->session & AFB_SESSION_CREATE)
158                         json_object_array_add(a, json_object_new_string("session-create"));
159                 if (verb->session & AFB_SESSION_CLOSE)
160                         json_object_array_add(a, json_object_new_string("session-close"));
161                 if (verb->session & AFB_SESSION_RENEW)
162                         json_object_array_add(a, json_object_new_string("session-renew"));
163                 if (verb->session & AFB_SESSION_CHECK)
164                         json_object_array_add(a, json_object_new_string("session-check"));
165                 if (verb->session & AFB_SESSION_LOA_EQ) {
166                         const char *rel;
167                         char buffer[80];
168                         switch (verb->session & AFB_SESSION_LOA_EQ) {
169                         case AFB_SESSION_LOA_GE: rel = ">="; break;
170                         case AFB_SESSION_LOA_LE: rel = "<="; break;
171                         case AFB_SESSION_LOA_EQ: rel = "=="; break;
172                         }
173                         snprintf(buffer, sizeof buffer, "LOA%s%d", rel, (int)((verb->session >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK));
174                         json_object_array_add(a, json_object_new_string(buffer));
175                 }
176                 json_object_object_add(f, "flags", a);
177                 json_object_object_add(v, verb->name, f);
178                 verb++;
179         }
180         return r;
181 }
182
183 static struct afb_api_itf so_v1_api_itf = {
184         .call = call_cb,
185         .service_start = service_start_cb,
186         .update_hooks = update_hooks_cb,
187         .get_verbosity = get_verbosity_cb,
188         .set_verbosity = set_verbosity_cb,
189         .describe = describe_cb
190 };
191
192 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
193 {
194         struct api_so_v1 *desc;
195         struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
196         struct afb_api afb_api;
197
198         /* retrieves the register function */
199         register_function = dlsym(handle, afb_api_so_v1_register);
200         if (!register_function)
201                 return 0;
202         INFO("binding [%s] is a valid AFB binding V1", path);
203
204         /* allocates the description */
205         desc = calloc(1, sizeof *desc);
206         if (desc == NULL) {
207                 ERROR("out of memory");
208                 goto error;
209         }
210         desc->handle = handle;
211
212         /* init the interface */
213         afb_ditf_init(&desc->ditf, path);
214
215         /* init the binding */
216         NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
217         desc->binding = register_function(&desc->ditf.interface);
218         if (desc->binding == NULL) {
219                 ERROR("binding [%s] register function failed. continuing...", path);
220                 goto error2;
221         }
222
223         /* check the returned structure */
224         if (desc->binding->type != AFB_BINDING_VERSION_1) {
225                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
226                 goto error2;
227         }
228         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
229                 ERROR("binding [%s] bad prefix...", path);
230                 goto error2;
231         }
232         if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
233                 ERROR("binding [%s] invalid prefix...", path);
234                 goto error2;
235         }
236         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
237                 ERROR("binding [%s] bad description...", path);
238                 goto error2;
239         }
240         if (desc->binding->v1.verbs == NULL) {
241                 ERROR("binding [%s] no APIs...", path);
242                 goto error2;
243         }
244
245         /* records the binding */
246         afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
247         afb_api.closure = desc;
248         afb_api.itf = &so_v1_api_itf;
249         if (afb_apiset_add(apiset, desc->binding->v1.prefix, afb_api) < 0) {
250                 ERROR("binding [%s] can't be registered...", path);
251                 goto error2;
252         }
253         NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
254         return 1;
255
256 error2:
257         free(desc);
258 error:
259         return -1;
260 }
261