Make monitor use the interface version 2
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 25 Apr 2017 14:06:31 +0000 (16:06 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 25 Apr 2017 14:06:31 +0000 (16:06 +0200)
Enforce monitor API to be a kind of standard binding.

Change-Id: I4739f879d5e076eb3505f5fe2cf658e6f9299de6
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
include/afb/afb-binding-v2.h
src/afb-api-so-v2.c
src/afb-api-so-v2.h
src/afb-monitor.c

index 1f47e21..9795641 100644 (file)
@@ -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 */
 };
 
index 6ac1fa7..d56c985 100644 (file)
@@ -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);
index 180e61a..9b169ce 100644 (file)
@@ -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);
index 5d5c12d..7f47a1c 100644 (file)
  */
 
 #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