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