2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #define NO_BINDING_VERBOSE_MACRO
25 #include <afb/afb-binding.h>
30 #include "afb-common.h"
31 #include "afb-context.h"
32 #include "afb-api-so.h"
33 #include "afb-thread.h"
40 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
41 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
42 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
45 * Description of a binding
48 struct afb_binding *binding; /* descriptor */
49 size_t apilength; /* length of the API name */
50 void *handle; /* context of dlopen */
51 struct afb_svc *service; /* handler for service started */
52 struct afb_binding_interface interface; /* interface for the binding */
55 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name);
56 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
57 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
58 static int afb_api_so_rootdir_get_fd(void *closure);
59 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale);
61 static const struct afb_daemon_itf daemon_itf = {
62 .event_broadcast = afb_api_so_event_broadcast_cb,
63 .get_event_loop = afb_common_get_event_loop,
64 .get_user_bus = afb_common_get_user_bus,
65 .get_system_bus = afb_common_get_system_bus,
66 .vverbose = afb_api_so_vverbose_cb,
67 .event_make = afb_api_so_event_make_cb,
68 .rootdir_get_fd = afb_api_so_rootdir_get_fd,
69 .rootdir_open_locale = afb_api_so_rootdir_open_locale
72 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name)
76 struct api_so_v1 *desc = closure;
78 /* makes the event name */
79 assert(desc->binding != NULL);
80 length = strlen(name);
81 event = alloca(length + 2 + desc->apilength);
82 memcpy(event, desc->binding->v1.prefix, desc->apilength);
83 event[desc->apilength] = '/';
84 memcpy(event + desc->apilength + 1, name, length + 1);
87 return afb_evt_create_event(event);
90 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
94 struct api_so_v1 *desc = closure;
96 /* makes the event name */
97 assert(desc->binding != NULL);
98 length = strlen(name);
99 event = alloca(length + 2 + desc->apilength);
100 memcpy(event, desc->binding->v1.prefix, desc->apilength);
101 event[desc->apilength] = '/';
102 memcpy(event + desc->apilength + 1, name, length + 1);
104 return afb_evt_broadcast(event, object);
107 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
110 struct api_so_v1 *desc = closure;
112 if (vasprintf(&p, fmt, args) < 0)
113 vverbose(level, file, line, fmt, args);
115 verbose(level, file, line, "%s {binding %s}", p, desc->binding->v1.prefix);
120 static int afb_api_so_rootdir_get_fd(void *closure)
122 return afb_common_rootdir_get_fd();
125 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale)
127 return afb_common_rootdir_open_locale(filename, flags, locale);
130 static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_desc_v1 *verb)
132 int stag = (int)verb->session;
134 if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
135 if (!afb_context_check(context)) {
136 afb_context_close(context);
137 afb_req_fail(req, "failed", "invalid token's identity");
142 if ((stag & AFB_SESSION_CREATE) != 0) {
143 if (afb_context_check_loa(context, 1)) {
144 afb_req_fail(req, "failed", "invalid creation state");
147 afb_context_change_loa(context, 1);
148 afb_context_refresh(context);
151 if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
152 afb_context_refresh(context);
154 if ((stag & AFB_SESSION_CLOSE) != 0) {
155 afb_context_change_loa(context, 0);
156 afb_context_close(context);
159 if ((stag & AFB_SESSION_LOA_GE) != 0) {
160 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
161 if (!afb_context_check_loa(context, loa)) {
162 afb_req_fail(req, "failed", "invalid LOA");
167 if ((stag & AFB_SESSION_LOA_LE) != 0) {
168 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
169 if (afb_context_check_loa(context, loa + 1)) {
170 afb_req_fail(req, "failed", "invalid LOA");
177 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
179 const struct afb_verb_desc_v1 *verb;
181 verb = desc->binding->v1.verbs;
182 while (verb->name && strcasecmp(verb->name, name))
184 return verb->name ? verb : NULL;
187 static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *strverb)
189 const struct afb_verb_desc_v1 *verb;
190 struct api_so_v1 *desc = closure;
192 verb = search(desc, strverb);
194 afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", strverb, desc->binding->v1.prefix);
195 else if (call_check(req, context, verb)) {
196 afb_thread_req_call(req, verb->callback, afb_api_so_timeout, desc);
200 static void xcall_cb(void *closure, struct afb_xreq *xreq)
202 const struct afb_verb_desc_v1 *verb;
203 struct api_so_v1 *desc = closure;
205 verb = search(desc, xreq->verb);
207 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->v1.prefix);
209 xreq->timeout = afb_api_so_timeout;
210 xreq->sessionflags = (int)verb->session;
212 xreq->callback = verb->callback;
217 static int service_start_cb(void *closure, int share_session, int onneed)
219 int (*init)(struct afb_service service);
220 void (*onevent)(const char *event, struct json_object *object);
222 struct api_so_v1 *desc = closure;
225 if (desc->service != NULL) {
226 /* not an error when onneed */
230 /* already started: it is an error */
231 ERROR("Service %s already started", desc->binding->v1.prefix);
235 /* get the initialisation */
236 init = dlsym(desc->handle, afb_api_so_v1_service_init);
238 /* not an error when onneed */
242 /* no initialisation method */
243 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
247 /* get the event handler if any */
248 onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
249 desc->service = afb_svc_create(share_session, init, onevent);
250 if (desc->service == NULL) {
252 ERROR("Starting service %s failed", desc->binding->v1.prefix);
259 int afb_api_so_v1_add(const char *path, void *handle)
261 struct api_so_v1 *desc;
262 struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
263 struct afb_verb_desc_v1 fake_verb;
264 struct afb_binding fake_binding;
266 /* retrieves the register function */
267 register_function = dlsym(handle, afb_api_so_v1_register);
268 if (!register_function)
270 INFO("binding [%s] is a valid AFB binding V1", path);
272 /* allocates the description */
273 desc = calloc(1, sizeof *desc);
275 ERROR("out of memory");
278 desc->handle = handle;
280 /* init the interface */
281 desc->interface.verbosity = verbosity;
282 desc->interface.mode = AFB_MODE_LOCAL;
283 desc->interface.daemon.itf = &daemon_itf;
284 desc->interface.daemon.closure = desc;
286 /* for log purpose, a fake binding is needed here */
287 desc->binding = &fake_binding;
288 fake_binding.type = AFB_BINDING_VERSION_1;
289 fake_binding.v1.info = path;
290 fake_binding.v1.prefix = path;
291 fake_binding.v1.verbs = &fake_verb;
292 fake_verb.name = NULL;
294 /* init the binding */
295 NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
296 desc->binding = register_function(&desc->interface);
297 if (desc->binding == NULL) {
298 ERROR("binding [%s] register function failed. continuing...", path);
302 /* check the returned structure */
303 if (desc->binding->type != AFB_BINDING_VERSION_1) {
304 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
307 if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
308 ERROR("binding [%s] bad prefix...", path);
311 if (!afb_apis_is_valid_api_name(desc->binding->v1.prefix)) {
312 ERROR("binding [%s] invalid prefix...", path);
315 if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
316 ERROR("binding [%s] bad description...", path);
319 if (desc->binding->v1.verbs == NULL) {
320 ERROR("binding [%s] no APIs...", path);
324 /* records the binding */
325 desc->apilength = strlen(desc->binding->v1.prefix);
326 if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
330 .service_start = service_start_cb }) < 0) {
331 ERROR("binding [%s] can't be registered...", path);
334 NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);