cleanup and improvement of xreq
[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 NO_BINDING_VERBOSE_MACRO
20
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <afb/afb-binding.h>
26
27 #include "afb-api.h"
28 #include "afb-apiset.h"
29 #include "afb-svc.h"
30 #include "afb-ditf.h"
31 #include "afb-evt.h"
32 #include "afb-common.h"
33 #include "afb-context.h"
34 #include "afb-api-so.h"
35 #include "afb-xreq.h"
36 #include "verbose.h"
37
38 /*
39  * names of symbols
40  */
41 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
42
43 /*
44  * Description of a binding
45  */
46 struct api_so_v2 {
47         struct afb_binding_v2 *binding; /* descriptor */
48         void *handle;                   /* context of dlopen */
49         struct afb_svc *service;        /* handler for service started */
50         struct afb_ditf ditf;           /* daemon interface */
51 };
52
53 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
54 {
55         const struct afb_verb_v2 *result;
56
57         result = desc->binding->verbs;
58         while (result->verb && strcasecmp(result->verb, verb))
59                 result++;
60         return result->verb ? result : NULL;
61 }
62
63 static void call_cb(void *closure, struct afb_xreq *xreq)
64 {
65         struct api_so_v2 *desc = closure;
66         const struct afb_verb_v2 *verb;
67
68         verb = search(desc, xreq->verb);
69         if (!verb)
70                 afb_xreq_fail_unknown_verb(xreq);
71         else
72                 if (!xreq_session_check_apply(xreq, verb->session))
73                         afb_xreq_call(xreq, verb->callback);
74 }
75
76 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
77 {
78         int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
79         void (*onevent)(const char *event, struct json_object *object);
80
81         struct api_so_v2 *desc = closure;
82
83         /* check state */
84         if (desc->service != NULL) {
85                 /* not an error when onneed */
86                 if (onneed != 0)
87                         return 0;
88
89                 /* already started: it is an error */
90                 ERROR("Service %s already started", desc->binding->api);
91                 return -1;
92         }
93
94         /* get the initialisation */
95         start = desc->binding->start;
96         if (start == NULL) {
97                 /* not an error when onneed */
98                 if (onneed != 0)
99                         return 0;
100
101                 /* no initialisation method */
102                 ERROR("Binding %s is not a service", desc->binding->api);
103                 return -1;
104         }
105
106         /* get the event handler if any */
107         onevent = desc->binding->onevent;
108         desc->service = afb_svc_create_v2(apiset, share_session, onevent, start, &desc->ditf.interface);
109         if (desc->service == NULL) {
110                 /* starting error */
111                 ERROR("Starting service %s failed", desc->binding->api);
112                 return -1;
113         }
114
115         return 0;
116 }
117
118 static void update_hooks_cb(void *closure)
119 {
120         struct api_so_v2 *desc = closure;
121         afb_ditf_update_hook(&desc->ditf);
122 }
123
124 static int get_verbosity_cb(void *closure)
125 {
126         struct api_so_v2 *desc = closure;
127         return desc->ditf.interface.verbosity;
128 }
129
130 static void set_verbosity_cb(void *closure, int level)
131 {
132         struct api_so_v2 *desc = closure;
133         desc->ditf.interface.verbosity = level;
134 }
135
136 static struct afb_api_itf so_v2_api_itf = {
137         .call = call_cb,
138         .service_start = service_start_cb,
139         .update_hooks = update_hooks_cb,
140         .get_verbosity = get_verbosity_cb,
141         .set_verbosity = set_verbosity_cb
142 };
143
144 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
145 {
146         int rc;
147         struct api_so_v2 *desc;
148         struct afb_binding_v2 *binding;
149         struct afb_api afb_api;
150
151         /* retrieves the register function */
152         binding = dlsym(handle, afb_api_so_v2_descriptor);
153         if (!binding)
154                 return 0;
155
156         INFO("binding [%s] looks like an AFB binding V2", path);
157
158         /* basic checks */
159         if (binding->api == NULL || *binding->api == 0) {
160                 ERROR("binding [%s] bad api name...", path);
161                 goto error;
162         }
163         if (!afb_api_is_valid_name(binding->api)) {
164                 ERROR("binding [%s] invalid api name...", path);
165                 goto error;
166         }
167         if (binding->specification == NULL || *binding->specification == 0) {
168                 ERROR("binding [%s] bad specification...", path);
169                 goto error;
170         }
171         if (binding->verbs == NULL) {
172                 ERROR("binding [%s] no verbs...", path);
173                 goto error;
174         }
175
176         /* allocates the description */
177         desc = calloc(1, sizeof *desc);
178         if (desc == NULL) {
179                 ERROR("out of memory");
180                 goto error;
181         }
182         desc->binding = binding;
183         desc->handle = handle;
184
185         /* init the interface */
186         afb_ditf_init(&desc->ditf, binding->api);
187
188         /* for log purpose, a fake binding is needed here */
189
190         /* init the binding */
191         if (binding->init) {
192                 NOTICE("binding %s [%s] calling init function", binding->api, path);
193                 rc = binding->init(&desc->ditf.interface);
194                 if (rc < 0) {
195                         ERROR("binding %s [%s] initialisation function failed...", binding->api, path);
196                         goto error2;
197                 }
198         }
199
200         /* records the binding */
201         afb_api.closure = desc;
202         afb_api.itf = &so_v2_api_itf;
203         if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
204                 ERROR("binding [%s] can't be registered...", path);
205                 goto error2;
206         }
207         NOTICE("binding %s loaded with API prefix %s", path, binding->api);
208         return 1;
209
210 error2:
211         free(desc);
212 error:
213         return -1;
214 }
215