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