Bindings V2: rename init functions
[src/app-framework-binder.git] / src / afb-api-so-v2.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 <afb/afb-binding.h>
26 #include <json-c/json.h>
27
28 #include "afb-api.h"
29 #include "afb-api-so-v2.h"
30 #include "afb-apiset.h"
31 #include "afb-svc.h"
32 #include "afb-ditf.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 "jobs.h"
39 #include "verbose.h"
40
41 /*
42  * names of symbols
43  */
44 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
45 static const char afb_api_so_v2_verbosity[] = "afbBindingV2verbosity";
46
47 /*
48  * Description of a binding
49  */
50 struct api_so_v2 {
51         const struct afb_binding_v2 *binding;   /* descriptor */
52         int *verbosity;                         /* verbosity */
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_v2 *search(struct api_so_v2 *desc, const char *name)
59 {
60         const struct afb_verb_v2 *verb;
61
62         verb = desc->binding->verbs;
63         while (verb->verb && strcasecmp(verb->verb, name))
64                 verb++;
65         return verb->verb ? verb : NULL;
66         return NULL;
67 }
68
69 static void call_cb(void *closure, struct afb_xreq *xreq)
70 {
71         struct api_so_v2 *desc = closure;
72         const struct afb_verb_v2 *verb;
73
74         verb = search(desc, xreq->verb);
75         afb_xreq_call_verb_v2(xreq, verb);
76 }
77
78 struct call_sync
79 {
80         struct api_so_v2 *desc;
81         struct afb_xreq *xreq;
82 };
83
84 static void call_sync_cb_cb(int signum, void *closure)
85 {
86         struct call_sync *cs = closure;
87         if (!signum)
88                 call_cb(cs->desc, cs->xreq);
89         else  {
90                 if (!cs->xreq->replied)
91                         afb_xreq_fail(cs->xreq, "aborted", "internal error");
92         }
93 }
94
95 static void call_sync_cb(void *closure, struct afb_xreq *xreq)
96 {
97         struct call_sync cs = { .desc = closure, .xreq = xreq };
98
99         if (jobs_call(closure, 0, call_sync_cb_cb, &cs))
100                 call_cb(closure, xreq);
101 }
102
103 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
104 {
105         int (*start)(struct afb_service service);
106         void (*onevent)(struct afb_service service, const char *event, struct json_object *object);
107
108         struct api_so_v2 *desc = closure;
109
110         /* check state */
111         if (desc->service != NULL) {
112                 /* not an error when onneed */
113                 if (onneed != 0)
114                         return 0;
115
116                 /* already started: it is an error */
117                 ERROR("Service %s already started", desc->binding->api);
118                 return -1;
119         }
120
121         /* get the initialisation */
122         start = desc->binding->init;
123         if (start == NULL) {
124                 /* not an error when onneed */
125                 if (onneed != 0)
126                         return 0;
127
128                 /* no initialisation method */
129                 ERROR("Binding %s is not a service", desc->binding->api);
130                 return -1;
131         }
132
133         /* get the event handler if any */
134         onevent = desc->binding->onevent;
135         desc->service = afb_svc_create_v2(apiset, share_session, start, onevent, &desc->ditf);
136         if (desc->service == NULL) {
137                 /* starting error */
138                 ERROR("Starting service %s failed", desc->binding->api);
139                 return -1;
140         }
141
142         return 0;
143 }
144
145 static void update_hooks_cb(void *closure)
146 {
147         struct api_so_v2 *desc = closure;
148         afb_ditf_update_hook(&desc->ditf);
149 }
150
151 static int get_verbosity_cb(void *closure)
152 {
153         struct api_so_v2 *desc = closure;
154         return *desc->verbosity;
155 }
156
157 static void set_verbosity_cb(void *closure, int level)
158 {
159         struct api_so_v2 *desc = closure;
160         *desc->verbosity = level;
161 }
162
163 static struct json_object *describe_cb(void *closure)
164 {
165         struct api_so_v2 *desc = closure;
166         return desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
167 }
168
169 static struct afb_api_itf so_v2_api_itf = {
170         .call = call_cb,
171         .service_start = service_start_cb,
172         .update_hooks = update_hooks_cb,
173         .get_verbosity = get_verbosity_cb,
174         .set_verbosity = set_verbosity_cb,
175         .describe = describe_cb
176 };
177
178 static struct afb_api_itf so_v2_sync_api_itf = {
179         .call = call_sync_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_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset, int *pver)
188 {
189         int rc;
190         struct api_so_v2 *desc;
191         struct afb_api afb_api;
192
193         /* basic checks */
194         assert(binding->api);
195         assert(binding->specification);
196         assert(binding->verbs);
197
198         /* allocates the description */
199         desc = malloc(sizeof *desc);
200         if (desc == NULL) {
201                 ERROR("out of memory");
202                 goto error;
203         }
204         desc->binding = binding;
205         desc->verbosity = pver;
206         desc->handle = handle;
207         desc->service = NULL;
208         memset(&desc->ditf, 0, sizeof desc->ditf);
209
210         /* init the interface */
211         afb_ditf_init_v2(&desc->ditf, binding->api);
212
213         /* init the binding */
214         if (binding->preinit) {
215                 INFO("binding %s calling preinit function", binding->api);
216                 rc = binding->preinit(desc->ditf.daemon);
217                 if (rc < 0) {
218                         ERROR("binding %s preinit function failed...", binding->api);
219                         goto error2;
220                 }
221         }
222
223         /* records the binding */
224         afb_api.closure = desc;
225         afb_api.itf = binding->concurrent ? &so_v2_api_itf : &so_v2_sync_api_itf;
226         if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
227                 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
228                 goto error2;
229         }
230         INFO("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
231         return 1;
232
233 error2:
234         free(desc);
235 error:
236         return -1;
237 }
238
239 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
240 {
241         const struct afb_binding_v2 *binding;
242         int *pver;
243
244         /* retrieves the register function */
245         binding = dlsym(handle, afb_api_so_v2_descriptor);
246         pver = dlsym(handle, afb_api_so_v2_verbosity);
247         if (!binding && !pver)
248                 return 0;
249
250         INFO("binding [%s] looks like an AFB binding V2", path);
251
252         /* basic checks */
253         if (!binding || !pver) {
254                 ERROR("binding [%s] incomplete symbols...", path);
255                 goto error;
256         }
257         if (binding->api == NULL || *binding->api == 0) {
258                 ERROR("binding [%s] bad api name...", path);
259                 goto error;
260         }
261         if (!afb_api_is_valid_name(binding->api)) {
262                 ERROR("binding [%s] invalid api name...", path);
263                 goto error;
264         }
265         if (binding->specification == NULL || *binding->specification == 0) {
266                 ERROR("binding [%s] bad specification...", path);
267                 goto error;
268         }
269         if (binding->verbs == NULL) {
270                 ERROR("binding [%s] no verbs...", path);
271                 goto error;
272         }
273
274         return afb_api_so_v2_add_binding(binding, handle, apiset, pver);
275
276  error:
277         return -1;
278 }
279