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_v2_descriptor[] = "afbBindingV2";
43 * Description of a binding
46 struct afb_binding_v2 *binding; /* descriptor */
47 size_t apilength; /* length of the API name */
48 void *handle; /* context of dlopen */
49 struct afb_svc *service; /* handler for service started */
50 struct afb_binding_interface interface; /* interface for the binding */
53 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name);
54 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object);
55 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args);
56 static int afb_api_so_rootdir_get_fd(void *closure);
57 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale);
59 static const struct afb_daemon_itf daemon_itf = {
60 .event_broadcast = afb_api_so_event_broadcast_cb,
61 .get_event_loop = afb_common_get_event_loop,
62 .get_user_bus = afb_common_get_user_bus,
63 .get_system_bus = afb_common_get_system_bus,
64 .vverbose = afb_api_so_vverbose_cb,
65 .event_make = afb_api_so_event_make_cb,
66 .rootdir_get_fd = afb_api_so_rootdir_get_fd,
67 .rootdir_open_locale = afb_api_so_rootdir_open_locale
70 static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name)
74 struct api_so_v2 *desc = closure;
76 /* makes the event name */
77 assert(desc->binding != NULL);
78 length = strlen(name);
79 event = alloca(length + 2 + desc->apilength);
80 memcpy(event, desc->binding->api, desc->apilength);
81 event[desc->apilength] = '/';
82 memcpy(event + desc->apilength + 1, name, length + 1);
85 return afb_evt_create_event(event);
88 static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
92 struct api_so_v2 *desc = closure;
94 /* makes the event name */
95 assert(desc->binding != NULL);
96 length = strlen(name);
97 event = alloca(length + 2 + desc->apilength);
98 memcpy(event, desc->binding->api, desc->apilength);
99 event[desc->apilength] = '/';
100 memcpy(event + desc->apilength + 1, name, length + 1);
102 return afb_evt_broadcast(event, object);
105 static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
108 struct api_so_v2 *desc = closure;
110 if (vasprintf(&p, fmt, args) < 0)
111 vverbose(level, file, line, fmt, args);
113 verbose(level, file, line, "%s {binding %s}", p, desc->binding->api);
118 static int afb_api_so_rootdir_get_fd(void *closure)
120 return afb_common_rootdir_get_fd();
123 static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale)
125 return afb_common_rootdir_open_locale(filename, flags, locale);
128 static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_v2 *verb)
130 int stag = (int)verb->session;
132 if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) {
133 if (!afb_context_check(context)) {
134 afb_context_close(context);
135 afb_req_fail(req, "failed", "invalid token's identity");
140 if ((stag & AFB_SESSION_CREATE) != 0) {
141 if (afb_context_check_loa(context, 1)) {
142 afb_req_fail(req, "failed", "invalid creation state");
145 afb_context_change_loa(context, 1);
146 afb_context_refresh(context);
149 if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
150 afb_context_refresh(context);
152 if ((stag & AFB_SESSION_CLOSE) != 0) {
153 afb_context_change_loa(context, 0);
154 afb_context_close(context);
157 if ((stag & AFB_SESSION_LOA_GE) != 0) {
158 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
159 if (!afb_context_check_loa(context, loa)) {
160 afb_req_fail(req, "failed", "invalid LOA");
165 if ((stag & AFB_SESSION_LOA_LE) != 0) {
166 int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK;
167 if (afb_context_check_loa(context, loa + 1)) {
168 afb_req_fail(req, "failed", "invalid LOA");
175 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
177 const struct afb_verb_v2 *result;
179 result = desc->binding->verbs;
180 while (result->verb && strcasecmp(result->verb, verb))
182 return result->verb ? result : NULL;
185 static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *name)
187 struct api_so_v2 *desc = closure;
188 const struct afb_verb_v2 *verb;
190 verb = search(desc, name);
192 afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", name, desc->binding->api);
193 else if (call_check(req, context, verb)) {
194 afb_thread_req_call(req, verb->callback, afb_api_so_timeout, desc);
198 static void xcall_cb(void *closure, struct afb_xreq *xreq)
200 struct api_so_v2 *desc = closure;
201 const struct afb_verb_v2 *verb;
203 verb = search(desc, xreq->verb);
205 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->api);
207 xreq->timeout = afb_api_so_timeout;
208 xreq->sessionflags = (int)verb->session;
210 xreq->callback = verb->callback;
215 static int service_start_cb(void *closure, int share_session, int onneed)
217 int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
218 void (*onevent)(const char *event, struct json_object *object);
220 struct api_so_v2 *desc = closure;
223 if (desc->service != NULL) {
224 /* not an error when onneed */
228 /* already started: it is an error */
229 ERROR("Service %s already started", desc->binding->api);
233 /* get the initialisation */
234 start = desc->binding->start;
236 /* not an error when onneed */
240 /* no initialisation method */
241 ERROR("Binding %s is not a service", desc->binding->api);
245 /* get the event handler if any */
246 onevent = desc->binding->onevent;
247 desc->service = afb_svc_create_v2(share_session, onevent, start, &desc->interface);
248 if (desc->service == NULL) {
250 ERROR("Starting service %s failed", desc->binding->api);
257 int afb_api_so_v2_add(const char *path, void *handle)
260 struct api_so_v2 *desc;
261 struct afb_binding_v2 *binding;
263 /* retrieves the register function */
264 binding = dlsym(handle, afb_api_so_v2_descriptor);
268 INFO("binding [%s] looks like an AFB binding V2", path);
271 if (binding->api == NULL || *binding->api == 0) {
272 ERROR("binding [%s] bad api name...", path);
275 if (!afb_apis_is_valid_api_name(binding->api)) {
276 ERROR("binding [%s] invalid api name...", path);
279 if (binding->specification == NULL || *binding->specification == 0) {
280 ERROR("binding [%s] bad specification...", path);
283 if (binding->verbs == NULL) {
284 ERROR("binding [%s] no verbs...", path);
288 /* allocates the description */
289 desc = calloc(1, sizeof *desc);
291 ERROR("out of memory");
294 desc->binding = binding;
295 desc->handle = handle;
297 /* init the interface */
298 desc->interface.verbosity = verbosity;
299 desc->interface.mode = AFB_MODE_LOCAL;
300 desc->interface.daemon.itf = &daemon_itf;
301 desc->interface.daemon.closure = desc;
303 /* for log purpose, a fake binding is needed here */
305 /* init the binding */
307 NOTICE("binding %s [%s] calling init function", binding->api, path);
308 rc = binding->init(&desc->interface);
310 ERROR("binding %s [%s] initialisation function failed...", binding->api, path);
315 /* records the binding */
316 desc->apilength = strlen(binding->api);
317 if (afb_apis_add(binding->api, (struct afb_api){
321 .service_start = service_start_cb }) < 0) {
322 ERROR("binding [%s] can't be registered...", path);
325 NOTICE("binding %s loaded with API prefix %s", path, binding->api);