Prepare permission for binding version 2
[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 "verbose.h"
39
40 /*
41  * names of symbols
42  */
43 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
44 static const char afb_api_so_v2_verbosity[] = "afbBindingV2verbosity";
45
46 /*
47  * Description of a binding
48  */
49 struct api_so_v2 {
50         const struct afb_binding_v2 *binding;   /* descriptor */
51         int *verbosity;                         /* verbosity */
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_v2 *search(struct api_so_v2 *desc, const char *name)
58 {
59         const struct afb_verb_v2 *verb;
60
61         verb = desc->binding->verbs;
62         while (verb->verb && strcasecmp(verb->verb, name))
63                 verb++;
64         return verb->verb ? verb : NULL;
65         return NULL;
66 }
67
68 static void call_cb(void *closure, struct afb_xreq *xreq)
69 {
70         struct api_so_v2 *desc = closure;
71         const struct afb_verb_v2 *verb;
72
73         verb = search(desc, xreq->verb);
74         afb_xreq_call_verb_v2(xreq, verb);
75 }
76
77 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
78 {
79         int (*start)(struct afb_service service);
80         void (*onevent)(struct afb_service service, const char *event, struct json_object *object);
81
82         struct api_so_v2 *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->api);
92                 return -1;
93         }
94
95         /* get the initialisation */
96         start = desc->binding->start;
97         if (start == 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->api);
104                 return -1;
105         }
106
107         /* get the event handler if any */
108         onevent = desc->binding->onevent;
109         desc->service = afb_svc_create_v2(apiset, share_session, start, onevent, &desc->ditf);
110         if (desc->service == NULL) {
111                 /* starting error */
112                 ERROR("Starting service %s failed", desc->binding->api);
113                 return -1;
114         }
115
116         return 0;
117 }
118
119 static void update_hooks_cb(void *closure)
120 {
121         struct api_so_v2 *desc = closure;
122         afb_ditf_update_hook(&desc->ditf);
123 }
124
125 static int get_verbosity_cb(void *closure)
126 {
127         struct api_so_v2 *desc = closure;
128         return *desc->verbosity;
129 }
130
131 static void set_verbosity_cb(void *closure, int level)
132 {
133         struct api_so_v2 *desc = closure;
134         *desc->verbosity = level;
135 }
136
137 static struct json_object *describe_cb(void *closure)
138 {
139         struct api_so_v2 *desc = closure;
140         return desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL;
141 }
142
143 static struct afb_api_itf so_v2_api_itf = {
144         .call = call_cb,
145         .service_start = service_start_cb,
146         .update_hooks = update_hooks_cb,
147         .get_verbosity = get_verbosity_cb,
148         .set_verbosity = set_verbosity_cb,
149         .describe = describe_cb
150
151 };
152
153 int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset, int *pver)
154 {
155         int rc;
156         struct api_so_v2 *desc;
157         struct afb_api afb_api;
158
159         /* basic checks */
160         assert(binding->api);
161         assert(binding->specification);
162         assert(binding->verbs);
163
164         /* allocates the description */
165         desc = malloc(sizeof *desc);
166         if (desc == NULL) {
167                 ERROR("out of memory");
168                 goto error;
169         }
170         desc->binding = binding;
171         desc->verbosity = pver;
172         desc->handle = handle;
173         desc->service = NULL;
174         memset(&desc->ditf, 0, sizeof desc->ditf);
175
176         /* init the interface */
177         afb_ditf_init_v2(&desc->ditf, binding->api);
178
179         /* init the binding */
180         if (binding->init) {
181                 INFO("binding %s calling init function", binding->api);
182                 rc = binding->init(desc->ditf.daemon);
183                 if (rc < 0) {
184                         ERROR("binding %s initialisation function failed...", binding->api);
185                         goto error2;
186                 }
187         }
188
189         /* records the binding */
190         afb_api.closure = desc;
191         afb_api.itf = &so_v2_api_itf;
192         if (afb_apiset_add(apiset, binding->api, afb_api) < 0) {
193                 ERROR("binding %s can't be registered to set %s...", binding->api, afb_apiset_name(apiset));
194                 goto error2;
195         }
196         NOTICE("binding %s added to set %s", binding->api, afb_apiset_name(apiset));
197         return 1;
198
199 error2:
200         free(desc);
201 error:
202         return -1;
203 }
204
205 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
206 {
207         const struct afb_binding_v2 *binding;
208         int *pver;
209
210         /* retrieves the register function */
211         binding = dlsym(handle, afb_api_so_v2_descriptor);
212         pver = dlsym(handle, afb_api_so_v2_verbosity);
213         if (!binding && !pver)
214                 return 0;
215
216         INFO("binding [%s] looks like an AFB binding V2", path);
217
218         /* basic checks */
219         if (!binding || !pver) {
220                 ERROR("binding [%s] incomplete symbols...", path);
221                 goto error;
222         }
223         if (binding->api == NULL || *binding->api == 0) {
224                 ERROR("binding [%s] bad api name...", path);
225                 goto error;
226         }
227         if (!afb_api_is_valid_name(binding->api)) {
228                 ERROR("binding [%s] invalid api name...", path);
229                 goto error;
230         }
231         if (binding->specification == NULL || *binding->specification == 0) {
232                 ERROR("binding [%s] bad specification...", path);
233                 goto error;
234         }
235         if (binding->verbs == NULL) {
236                 ERROR("binding [%s] no verbs...", path);
237                 goto error;
238         }
239
240         return afb_api_so_v2_add_binding(binding, handle, apiset, pver);
241
242  error:
243         return -1;
244 }
245