diff options
author | José Bollo <jose.bollo@iot.bzh> | 2017-04-25 16:06:31 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2017-04-25 16:06:31 +0200 |
commit | b3f3b90ebfa55f731393b2096dccfae570f22d6f (patch) | |
tree | f60c4c9de5bdee00c6b3fd273080e779e67d4c43 | |
parent | 3ffd607b9321c9a7f1c483d4a5e5e4f9e5abbc03 (diff) |
Make monitor use the interface version 2
Enforce monitor API to be a kind of standard binding.
Change-Id: I4739f879d5e076eb3505f5fe2cf658e6f9299de6
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | include/afb/afb-binding-v2.h | 2 | ||||
-rw-r--r-- | src/afb-api-so-v2.c | 17 | ||||
-rw-r--r-- | src/afb-api-so-v2.h | 2 | ||||
-rw-r--r-- | src/afb-monitor.c | 91 |
4 files changed, 29 insertions, 83 deletions
diff --git a/include/afb/afb-binding-v2.h b/include/afb/afb-binding-v2.h index 1f47e219..97956411 100644 --- a/include/afb/afb-binding-v2.h +++ b/include/afb/afb-binding-v2.h @@ -37,7 +37,7 @@ 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 */ + const char * permissions; /* required permissions */ enum afb_session_v1 session; /* authorisation and session requirements of the verb */ }; diff --git a/src/afb-api-so-v2.c b/src/afb-api-so-v2.c index 6ac1fa70..d56c9852 100644 --- a/src/afb-api-so-v2.c +++ b/src/afb-api-so-v2.c @@ -23,6 +23,7 @@ #include <assert.h> #include <afb/afb-binding.h> +#include <json-c/json.h> #include "afb-api.h" #include "afb-apiset.h" @@ -44,7 +45,7 @@ static const char afb_api_so_v2_descriptor[] = "afbBindingV2"; * Description of a binding */ struct api_so_v2 { - struct afb_binding_v2 *binding; /* descriptor */ + const struct afb_binding_v2 *binding; /* descriptor */ void *handle; /* context of dlopen */ struct afb_svc *service; /* handler for service started */ struct afb_ditf ditf; /* daemon interface */ @@ -133,15 +134,23 @@ static void set_verbosity_cb(void *closure, int level) desc->ditf.interface.verbosity = level; } +static struct json_object *describe_cb(void *closure) +{ + struct api_so_v2 *desc = closure; + return desc->binding->specification ? json_tokener_parse(desc->binding->specification) : NULL; +} + static struct afb_api_itf so_v2_api_itf = { .call = call_cb, .service_start = service_start_cb, .update_hooks = update_hooks_cb, .get_verbosity = get_verbosity_cb, - .set_verbosity = set_verbosity_cb + .set_verbosity = set_verbosity_cb, + .describe = describe_cb + }; -int afb_api_so_v2_add_binding(struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset) +int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset) { int rc; struct api_so_v2 *desc; @@ -192,7 +201,7 @@ error: int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset) { - struct afb_binding_v2 *binding; + const struct afb_binding_v2 *binding; /* retrieves the register function */ binding = dlsym(handle, afb_api_so_v2_descriptor); diff --git a/src/afb-api-so-v2.h b/src/afb-api-so-v2.h index 180e61ae..9b169cea 100644 --- a/src/afb-api-so-v2.h +++ b/src/afb-api-so-v2.h @@ -22,4 +22,4 @@ struct afb_apiset; struct afb_binding_v2; extern int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset); -extern int afb_api_so_v2_add_binding(struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset); +extern int afb_api_so_v2_add_binding(const struct afb_binding_v2 *binding, void *handle, struct afb_apiset *apiset); diff --git a/src/afb-monitor.c b/src/afb-monitor.c index 5d5c12d1..7f47a1cb 100644 --- a/src/afb-monitor.c +++ b/src/afb-monitor.c @@ -16,90 +16,27 @@ */ #define _GNU_SOURCE +#define NO_BINDING_VERBOSE_MACRO #include <string.h> #include <json-c/json.h> +#include <afb/afb-binding.h> #include "afb-api.h" #include "afb-apiset.h" +#include "afb-api-so-v2.h" #include "afb-ditf.h" #include "afb-xreq.h" #include "verbose.h" -extern struct afb_apiset *main_apiset; - -/* CAUTION! KEEP VERBS SORTED */ -#define VERBS \ - V(get) \ - V(hook) \ - V(set) - -/** - * Declare functions of verbs - */ -#define F(x) static void f_##x(struct afb_xreq *xreq) -#define V(x) F(x); - VERBS -#undef V - -/** - * Name of the known verbs - */ -static const struct { - const char *name; - void (*function)(struct afb_xreq*); -} verbs[] = { -#define V(x) { .name = #x, .function = f_##x }, - VERBS -#undef V -}; - -/** - * get the function of a verb - * @param verb the name of the verb - * @return the function for the verb or NULL when no verb of name exists - */ -static void (*lookfun(const char *verb))(struct afb_xreq*) -{ - int l, u, i, c; - - l = 0; - u = (int)(sizeof verbs / sizeof *verbs); - while (l < u) { - i = (l + u) >> 1; - c = strcmp(verb, verbs[i].name); - if (c == 0) - return verbs[i].function; - if (c < 0) - u = i; - else - l = i + 1; - } - return NULL; -} - -static void call_cb(void *closure, struct afb_xreq *xreq) -{ - void (*fun)(struct afb_xreq*); +#include "monitor-api.inc" - fun = lookfun(xreq->verb); - if (!fun) - afb_xreq_fail_unknown_verb(xreq); - else - fun(xreq); -} - -static struct afb_api_itf monitor_api_itf = { - .call = call_cb -}; +extern struct afb_apiset *main_apiset; int afb_monitor_init() { - struct afb_api api; - api.closure = NULL; - api.itf = &monitor_api_itf; - return afb_apiset_add(main_apiset, "monitor", api); + return afb_api_so_v2_add_binding(&_afb_binding_v2_, NULL, main_apiset); } /****************************************************************************** @@ -348,12 +285,12 @@ static void get_apis(struct json_object *resu, struct json_object *spec) static const char _verbosity_[] = "verbosity"; static const char _apis_[] = "apis"; -static void f_get(struct afb_xreq *xreq) +static void f_get(struct afb_req req) { struct json_object *o, *v, *r, *x; r = json_object_new_object(); - o = afb_xreq_json(xreq); + o = afb_req_json(req); if (json_object_object_get_ex(o, _verbosity_, &v)) { x = json_object_new_object(); @@ -367,24 +304,23 @@ static void f_get(struct afb_xreq *xreq) get_apis(x, v); } - if (!xreq->replied) - afb_xreq_success(xreq, json_object_get(r), NULL); + afb_req_success(req, json_object_get(r), NULL); json_object_put(r); } -static void f_set(struct afb_xreq *xreq) +static void f_set(struct afb_req req) { struct json_object *o, *v; - o = afb_xreq_json(xreq); + o = afb_req_json(req); if (json_object_object_get_ex(o, _verbosity_, &v)) { set_verbosity(v); } - if (!xreq->replied) - afb_xreq_success(xreq, NULL, NULL); + afb_req_success(req, NULL, NULL); } +#if 0 static void f_hook(struct afb_xreq *xreq) { struct json_object *o, *v; @@ -397,4 +333,5 @@ static void f_hook(struct afb_xreq *xreq) if (!xreq->replied) afb_xreq_success(xreq, NULL, NULL); } +#endif |