diff options
-rw-r--r-- | include/afb/afb-binding-v1.h | 47 | ||||
-rw-r--r-- | include/afb/afb-binding-v2.h | 56 | ||||
-rw-r--r-- | include/afb/afb-binding.h | 22 | ||||
-rw-r--r-- | include/afb/afb-service-itf-v1.h | 47 | ||||
-rw-r--r-- | include/afb/afb-service-itf.h | 2 | ||||
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/afb-api-so-v2.c | 306 | ||||
-rw-r--r-- | src/afb-api-so-v2.h | 21 | ||||
-rw-r--r-- | src/afb-api-so.c | 8 | ||||
-rw-r--r-- | src/afb-svc.c | 65 | ||||
-rw-r--r-- | src/afb-svc.h | 6 | ||||
-rw-r--r-- | src/jobs.c | 15 |
12 files changed, 515 insertions, 81 deletions
diff --git a/include/afb/afb-binding-v1.h b/include/afb/afb-binding-v1.h index 4fbe8f9b..6281dd8a 100644 --- a/include/afb/afb-binding-v1.h +++ b/include/afb/afb-binding-v1.h @@ -18,6 +18,8 @@ #pragma once struct afb_binding_interface; +struct json_object; +struct afb_service; /* * Function for registering the binding @@ -45,6 +47,31 @@ struct afb_binding_interface; extern const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *interface); /* + * When a binding have an exported implementation of the + * function 'afbBindingV1ServiceInit', defined below, + * the framework calls it for initialising the service after + * registration of all bindings. + * + * The object 'service' should be recorded. It has functions that + * allows the binding to call features with its own personality. + * + * The function should return 0 in case of success or, else, should return + * a negative value. + */ +extern int afbBindingV1ServiceInit(struct afb_service service); + +/* + * When a binding have an implementation of the function 'afbBindingV1ServiceEvent', + * defined below, the framework calls that function for any broadcasted event or for + * events that the service subscribed to in its name. + * + * It receive the 'event' name and its related data in 'object' (be aware that 'object' + * might be NULL). + */ +extern void afbBindingV1ServiceEvent(const char *event, struct json_object *object); + + +/* * Enum for Session/Token/Assurance middleware. * This enumeration is valid for bindings of type 1 */ @@ -107,3 +134,23 @@ struct afb_binding_desc_v1 const struct afb_verb_desc_v1 *verbs; /* array of descriptions of verbs terminated by a NULL name */ }; +/* + * Definition of the type+versions of the binding. + * The definition uses hashes. + */ +enum afb_binding_type +{ + AFB_BINDING_VERSION_1 = 123456789 +}; + +/* + * Description of a binding + */ +struct afb_binding +{ + enum afb_binding_type type; /* type of the binding */ + union { + struct afb_binding_desc_v1 v1; /* description of the binding of type 1 */ + }; +}; + diff --git a/include/afb/afb-binding-v2.h b/include/afb/afb-binding-v2.h new file mode 100644 index 00000000..f0806a7a --- /dev/null +++ b/include/afb/afb-binding-v2.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2016, 2017 "IoT.bzh" + * Author: José Bollo <jose.bollo@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +struct afb_binding_interface; +struct afb_service; +struct json_object; + +/* + * A binding V2 MUST have an exported symbol of name + * + * afbBindingV2 + * + */ +extern const struct afb_binding_v2 afbBindingV2; + +/* + * Description of one verb of the API provided by the binding + * This enumeration is valid for bindings of type version 2 + */ +struct afb_verb_v2 +{ + const char *verb; /* name of the verb */ + void (*callback)(struct afb_req req); /* callback function implementing the verb */ + const char * const *permissions; /* required permissions */ + enum afb_session_v1 session; /* authorisation and session requirements of the verb */ +}; + +/* + * Description of the bindings of type version 2 + */ +struct afb_binding_v2 +{ + const char *api; /* api name for the binding */ + const char *specification; /* textual specification of the binding */ + const struct afb_verb_v2 *verbs; /* array of descriptions of verbs terminated by a NULL name */ + int (*init)(const struct afb_binding_interface *interface); + int (*start)(const struct afb_binding_interface *interface, struct afb_service service); + void (*onevent)(const char *event, struct json_object *object); +}; + diff --git a/include/afb/afb-binding.h b/include/afb/afb-binding.h index 8df08ded..c8f96a39 100644 --- a/include/afb/afb-binding.h +++ b/include/afb/afb-binding.h @@ -38,27 +38,9 @@ #include "afb-event-itf.h" #include "afb-req-itf.h" +#include "afb-service-itf.h" #include "afb-binding-v1.h" - -/* - * Definition of the type+versions of the binding. - * The definition uses hashes. - */ -enum afb_binding_type -{ - AFB_BINDING_VERSION_1 = 123456789 /* version 1 */ -}; - -/* - * Description of a binding - */ -struct afb_binding -{ - enum afb_binding_type type; /* type of the binding */ - union { - struct afb_binding_desc_v1 v1; /* description of the binding of type 1 */ - }; -}; +#include "afb-binding-v2.h" /* * config mode diff --git a/include/afb/afb-service-itf-v1.h b/include/afb/afb-service-itf-v1.h deleted file mode 100644 index 9319cd3b..00000000 --- a/include/afb/afb-service-itf-v1.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2016, 2017 "IoT.bzh" - * Author: José Bollo <jose.bollo@iot.bzh> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -struct json_object; -struct afb_service; - -/* - * When a binding have an exported implementation of the - * function 'afbBindingV1ServiceInit', defined below, - * the framework calls it for initialising the service after - * registration of all bindings. - * - * The object 'service' should be recorded. It has functions that - * allows the binding to call features with its own personality. - * - * The function should return 0 in case of success or, else, should return - * a negative value. - */ -extern int afbBindingV1ServiceInit(struct afb_service service); - -/* - * When a binding have an implementation of the function 'afbBindingV1ServiceEvent', - * defined below, the framework calls that function for any broadcasted event or for - * events that the service subscribed to in its name. - * - * It receive the 'event' name and its related data in 'object' (be aware that 'object' - * might be NULL). - */ -extern void afbBindingV1ServiceEvent(const char *event, struct json_object *object); - - diff --git a/include/afb/afb-service-itf.h b/include/afb/afb-service-itf.h index 321fa5ac..e2e61dc8 100644 --- a/include/afb/afb-service-itf.h +++ b/include/afb/afb-service-itf.h @@ -43,8 +43,6 @@ struct afb_service void *closure; }; -#include "afb-service-itf-v1.h" - /** * Calls the 'verb' of the 'api' with the arguments 'args' and 'verb' in the name of the binding. * The result of the call is delivered to the 'callback' function with the 'callback_closure'. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a0579cf9..653cf8c9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,6 +57,7 @@ ADD_LIBRARY(afb-lib STATIC afb-api-dbus.c afb-api-so.c afb-api-so-v1.c + afb-api-so-v2.c afb-api-ws.c afb-apis.c afb-common.c diff --git a/src/afb-api-so-v2.c b/src/afb-api-so-v2.c new file mode 100644 index 00000000..0c8079c4 --- /dev/null +++ b/src/afb-api-so-v2.c @@ -0,0 +1,306 @@ +/* + * Copyright (C) 2016, 2017 "IoT.bzh" + * Author José Bollo <jose.bollo@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#define NO_BINDING_VERBOSE_MACRO + +#include <string.h> +#include <dlfcn.h> +#include <assert.h> + +#include <afb/afb-binding.h> + +#include "afb-apis.h" +#include "afb-svc.h" +#include "afb-evt.h" +#include "afb-common.h" +#include "afb-context.h" +#include "afb-api-so.h" +#include "afb-thread.h" +#include "verbose.h" + +/* + * names of symbols + */ +static const char afb_api_so_v2_descriptor[] = "afbBindingV2"; + +/* + * Description of a binding + */ +struct api_so_v2 { + struct afb_binding_v2 *binding; /* descriptor */ + size_t apilength; /* length of the API name */ + void *handle; /* context of dlopen */ + struct afb_svc *service; /* handler for service started */ + struct afb_binding_interface interface; /* interface for the binding */ +}; + +static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name); +static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object); +static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args); +static int afb_api_so_rootdir_get_fd(void *closure); +static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale); + +static const struct afb_daemon_itf daemon_itf = { + .event_broadcast = afb_api_so_event_broadcast_cb, + .get_event_loop = afb_common_get_event_loop, + .get_user_bus = afb_common_get_user_bus, + .get_system_bus = afb_common_get_system_bus, + .vverbose = afb_api_so_vverbose_cb, + .event_make = afb_api_so_event_make_cb, + .rootdir_get_fd = afb_api_so_rootdir_get_fd, + .rootdir_open_locale = afb_api_so_rootdir_open_locale +}; + +static struct afb_event afb_api_so_event_make_cb(void *closure, const char *name) +{ + size_t length; + char *event; + struct api_so_v2 *desc = closure; + + /* makes the event name */ + assert(desc->binding != NULL); + length = strlen(name); + event = alloca(length + 2 + desc->apilength); + memcpy(event, desc->binding->api, desc->apilength); + event[desc->apilength] = '/'; + memcpy(event + desc->apilength + 1, name, length + 1); + + /* crate the event */ + return afb_evt_create_event(event); +} + +static int afb_api_so_event_broadcast_cb(void *closure, const char *name, struct json_object *object) +{ + size_t length; + char *event; + struct api_so_v2 *desc = closure; + + /* makes the event name */ + assert(desc->binding != NULL); + length = strlen(name); + event = alloca(length + 2 + desc->apilength); + memcpy(event, desc->binding->api, desc->apilength); + event[desc->apilength] = '/'; + memcpy(event + desc->apilength + 1, name, length + 1); + + return afb_evt_broadcast(event, object); +} + +static void afb_api_so_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args) +{ + char *p; + struct api_so_v2 *desc = closure; + + if (vasprintf(&p, fmt, args) < 0) + vverbose(level, file, line, fmt, args); + else { + verbose(level, file, line, "%s {binding %s}", p, desc->binding->api); + free(p); + } +} + +static int afb_api_so_rootdir_get_fd(void *closure) +{ + return afb_common_rootdir_get_fd(); +} + +static int afb_api_so_rootdir_open_locale(void *closure, const char *filename, int flags, const char *locale) +{ + return afb_common_rootdir_open_locale(filename, flags, locale); +} + +static int call_check(struct afb_req req, struct afb_context *context, const struct afb_verb_v2 *verb) +{ + int stag = (int)verb->session; + + if ((stag & (AFB_SESSION_CREATE|AFB_SESSION_CLOSE|AFB_SESSION_RENEW|AFB_SESSION_CHECK|AFB_SESSION_LOA_EQ)) != 0) { + if (!afb_context_check(context)) { + afb_context_close(context); + afb_req_fail(req, "failed", "invalid token's identity"); + return 0; + } + } + + if ((stag & AFB_SESSION_CREATE) != 0) { + if (afb_context_check_loa(context, 1)) { + afb_req_fail(req, "failed", "invalid creation state"); + return 0; + } + afb_context_change_loa(context, 1); + afb_context_refresh(context); + } + + if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0) + afb_context_refresh(context); + + if ((stag & AFB_SESSION_CLOSE) != 0) { + afb_context_change_loa(context, 0); + afb_context_close(context); + } + + if ((stag & AFB_SESSION_LOA_GE) != 0) { + int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK; + if (!afb_context_check_loa(context, loa)) { + afb_req_fail(req, "failed", "invalid LOA"); + return 0; + } + } + + if ((stag & AFB_SESSION_LOA_LE) != 0) { + int loa = (stag >> AFB_SESSION_LOA_SHIFT) & AFB_SESSION_LOA_MASK; + if (afb_context_check_loa(context, loa + 1)) { + afb_req_fail(req, "failed", "invalid LOA"); + return 0; + } + } + return 1; +} + +static void call_cb(void *closure, struct afb_req req, struct afb_context *context, const char *strverb) +{ + const struct afb_verb_v2 *verb; + struct api_so_v2 *desc = closure; + + verb = desc->binding->verbs; + while (verb->verb && strcasecmp(verb->verb, strverb)) + verb++; + if (!verb->verb) + afb_req_fail_f(req, "unknown-verb", "verb %s unknown within api %s", strverb, desc->binding->api); + else if (call_check(req, context, verb)) { + afb_thread_req_call(req, verb->callback, afb_api_so_timeout, desc); + } +} + +static int service_start_cb(void *closure, int share_session, int onneed) +{ + int (*start)(const struct afb_binding_interface *interface, struct afb_service service); + void (*onevent)(const char *event, struct json_object *object); + + struct api_so_v2 *desc = closure; + + /* check state */ + if (desc->service != NULL) { + /* not an error when onneed */ + if (onneed != 0) + return 0; + + /* already started: it is an error */ + ERROR("Service %s already started", desc->binding->api); + return -1; + } + + /* get the initialisation */ + start = desc->binding->start; + if (start == NULL) { + /* not an error when onneed */ + if (onneed != 0) + return 0; + + /* no initialisation method */ + ERROR("Binding %s is not a service", desc->binding->api); + return -1; + } + + /* get the event handler if any */ + onevent = desc->binding->onevent; + desc->service = afb_svc_create_v2(share_session, onevent, start, &desc->interface); + if (desc->service == NULL) { + /* starting error */ + ERROR("Starting service %s failed", desc->binding->api); + return -1; + } + + return 0; +} + +int afb_api_so_v2_add(const char *path, void *handle) +{ + int rc; + struct api_so_v2 *desc; + struct afb_binding_v2 *binding; + + /* retrieves the register function */ + binding = dlsym(handle, afb_api_so_v2_descriptor); + if (!binding) + return 0; + + INFO("binding [%s] looks like an AFB binding V2", path); + + /* basic checks */ + if (binding->api == NULL || *binding->api == 0) { + ERROR("binding [%s] bad api name...", path); + goto error; + } + if (!afb_apis_is_valid_api_name(binding->api)) { + ERROR("binding [%s] invalid api name...", path); + goto error; + } + if (binding->specification == NULL || *binding->specification == 0) { + ERROR("binding [%s] bad specification...", path); + goto error; + } + if (binding->verbs == NULL) { + ERROR("binding [%s] no verbs...", path); + goto error; + } + + /* allocates the description */ + desc = calloc(1, sizeof *desc); + if (desc == NULL) { + ERROR("out of memory"); + goto error; + } + desc->binding = binding; + desc->handle = handle; + + /* init the interface */ + desc->interface.verbosity = verbosity; + desc->interface.mode = AFB_MODE_LOCAL; + desc->interface.daemon.itf = &daemon_itf; + desc->interface.daemon.closure = desc; + + /* for log purpose, a fake binding is needed here */ + + /* init the binding */ + if (binding->init) { + NOTICE("binding %s [%s] calling init function", binding->api, path); + rc = binding->init(&desc->interface); + if (rc < 0) { + ERROR("binding %s [%s] initialisation function failed...", binding->api, path); + goto error2; + } + } + + /* records the binding */ + desc->apilength = strlen(binding->api); + if (afb_apis_add(binding->api, (struct afb_api){ + .closure = desc, + .call = call_cb, + .service_start = service_start_cb }) < 0) { + ERROR("binding [%s] can't be registered...", path); + goto error2; + } + NOTICE("binding %s loaded with API prefix %s", path, binding->api); + return 1; + +error2: + free(desc); +error: + return -1; +} + diff --git a/src/afb-api-so-v2.h b/src/afb-api-so-v2.h new file mode 100644 index 00000000..7305db4a --- /dev/null +++ b/src/afb-api-so-v2.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2016, 2017 "IoT.bzh" + * Author: José Bollo <jose.bollo@iot.bzh> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#pragma once + +extern int afb_api_so_v2_add(const char *path, void *handle); diff --git a/src/afb-api-so.c b/src/afb-api-so.c index d673d0eb..4908ba85 100644 --- a/src/afb-api-so.c +++ b/src/afb-api-so.c @@ -27,6 +27,7 @@ #include "afb-api-so.h" #include "afb-api-so-v1.h" +#include "afb-api-so-v2.h" #include "verbose.h" int afb_api_so_timeout = 15; @@ -53,9 +54,14 @@ static int load_binding(const char *path, int force) } /* retrieves the register function */ + rc = afb_api_so_v2_add(path, handle); + if (rc < 0) { + /* error when loading a valid v2 binding */ + goto error2; + } rc = afb_api_so_v1_add(path, handle); if (rc < 0) { - /* error when loading a valid v& binding */ + /* error when loading a valid v1 binding */ goto error2; } if (rc == 0) { diff --git a/src/afb-svc.c b/src/afb-svc.c index 20dcdcfc..11e6872f 100644 --- a/src/afb-svc.c +++ b/src/afb-svc.c @@ -104,11 +104,10 @@ const struct afb_req_itf afb_svc_req_itf = { static struct afb_session *common_session; /* - * Creates a new service + * Allocates a new service */ -struct afb_svc *afb_svc_create(int share_session, int (*init)(struct afb_service service), void (*on_event)(const char *event, struct json_object *object)) +static struct afb_svc *afb_svc_alloc(int share_session, void (*on_event)(const char *event, struct json_object *object)) { - int rc; struct afb_svc *svc; /* allocates the svc handler */ @@ -142,19 +141,73 @@ struct afb_svc *afb_svc_create(int share_session, int (*init)(struct afb_service goto error3; } + return svc; + +error3: + afb_session_unref(svc->session); +error2: + free(svc); +error: + return NULL; +} + +/* + * Creates a new service + */ +struct afb_svc *afb_svc_create(int share_session, int (*init)(struct afb_service service), void (*on_event)(const char *event, struct json_object *object)) +{ + int rc; + struct afb_svc *svc; + + /* allocates the svc handler */ + svc = afb_svc_alloc(share_session, on_event); + if (svc == NULL) + goto error; + /* initialises the svc now */ rc = init((struct afb_service){ .itf = &service_itf, .closure = svc }); if (rc < 0) - goto error4; + goto error2; return svc; -error4: +error2: if (svc->listener != NULL) afb_evt_listener_unref(svc->listener); -error3: afb_session_unref(svc->session); + free(svc); +error: + return NULL; +} + +/* + * Creates a new service + */ +struct afb_svc *afb_svc_create_v2( + int share_session, + void (*on_event)(const char *event, struct json_object *object), + int (*start)(const struct afb_binding_interface *interface, struct afb_service service), + const struct afb_binding_interface *interface) +{ + int rc; + struct afb_svc *svc; + + /* allocates the svc handler */ + svc = afb_svc_alloc(share_session, on_event); + if (svc == NULL) + goto error; + + /* initialises the svc now */ + rc = start(interface, (struct afb_service){ .itf = &service_itf, .closure = svc }); + if (rc < 0) + goto error2; + + return svc; + error2: + if (svc->listener != NULL) + afb_evt_listener_unref(svc->listener); + afb_session_unref(svc->session); free(svc); error: return NULL; diff --git a/src/afb-svc.h b/src/afb-svc.h index be82b161..7a1f7c10 100644 --- a/src/afb-svc.h +++ b/src/afb-svc.h @@ -19,8 +19,14 @@ struct afb_svc; struct afb_service; +struct afb_binding_interface; extern struct afb_svc *afb_svc_create(int share_session, int (*init)(struct afb_service service), void (*onevent)(const char *event, struct json_object *object)); +extern struct afb_svc *afb_svc_create_v2( + int share_session, + void (*on_event)(const char *event, struct json_object *object), + int (*start)(const struct afb_binding_interface *interface, struct afb_service service), + const struct afb_binding_interface *interface); @@ -847,7 +847,10 @@ int jobs_add_me() return 0; } - +/** + * Gets a sd_event item for the current thread. + * @return a sd_event or NULL in case of error + */ struct sd_event *jobs_get_sd_event() { struct events *events; @@ -885,9 +888,10 @@ struct sd_event *jobs_get_sd_event() events = NULL; } } else { - if (!events) + if (!events) { ERROR("out of memory"); - else { + errno = ENOMEM; + } else { free(events); ERROR("creation of sd_event failed: %m"); events = NULL; @@ -912,7 +916,7 @@ struct sd_event *jobs_get_sd_event() } /** - * run the jobs as + * Enter the jobs processing loop. * @param allowed_count Maximum count of thread for jobs including this one * @param start_count Count of thread to start now, must be lower. * @param waiter_count Maximum count of jobs that can be waiting. @@ -941,4 +945,5 @@ int jobs_enter(int allowed_count, int start_count, int waiter_count, void (*star /* turn as processing thread */ return jobs_add_me(); -}; +} + |