afb-apiset: add a wrapper for 'describe'
authorJosé Bollo <jose.bollo@iot.bzh>
Fri, 11 Aug 2017 09:08:56 +0000 (11:08 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Fri, 11 Aug 2017 14:50:43 +0000 (16:50 +0200)
And use it in monitor.

Change-Id: I9efbd19cfbdc665d1d686831b529ebaa7bd917a2
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-apiset.c
src/afb-apiset.h
src/afb-monitor.c
src/afb-stub-ws.c

index 13d806e..18104a9 100644 (file)
@@ -306,6 +306,17 @@ int afb_apiset_lookup(struct afb_apiset *set, const char *name, struct afb_api *
        return -1;
 }
 
+/**
+ * Check whether the 'set' has the API of 'name'
+ * @param set the set of API
+ * @param name the name of the API to get
+ * @return 1 if the api exist or 0 otherwise
+ */
+int afb_apiset_has(struct afb_apiset *set, const char *name)
+{
+       return !!search(set, name);
+}
+
 /**
  * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
  * @param set the set of API
@@ -494,9 +505,10 @@ void afb_apiset_set_verbosity(struct afb_apiset *set, const char *name, int leve
 }
 
 /**
- * Set the verbosity level of the 'api'
+ * Get the verbosity level of the 'api'
  * @param set the api set
- * @param name the api to set (NULL set all)
+ * @param name the api to get
+ * @return the verbosity level or -1 in case of error
  */
 int afb_apiset_get_verbosity(struct afb_apiset *set, const char *name)
 {
@@ -507,12 +519,27 @@ int afb_apiset_get_verbosity(struct afb_apiset *set, const char *name)
                errno = ENOENT;
                return -1;
        }
+
        if (!i->api.itf->get_verbosity)
                return verbosity;
 
        return i->api.itf->get_verbosity(i->api.closure);
 }
 
+/**
+ * Get the description of the API of 'name'
+ * @param set the api set
+ * @param name the api whose description is required
+ * @return the description or NULL
+ */
+struct json_object *afb_apiset_describe(struct afb_apiset *set, const char *name)
+{
+       const struct api_desc *i;
+
+       i = name ? search(set, name) : NULL;
+       return i && i->api.itf->describe ? i->api.itf->describe(i->api.closure) : NULL;
+}
+
 /**
  * Get the list of api names
  * @param set the api set
index fdc169f..ea1eefc 100644 (file)
@@ -19,6 +19,7 @@
 
 struct afb_api;
 struct afb_apiset;
+struct json_object;
 
 extern struct afb_apiset *afb_apiset_addref(struct afb_apiset *set);
 extern void afb_apiset_unref(struct afb_apiset *set);
@@ -30,6 +31,7 @@ extern void afb_apiset_subset_set(struct afb_apiset *set, struct afb_apiset *sub
 extern struct afb_apiset *afb_apiset_subset_get(struct afb_apiset *set);
 extern int afb_apiset_add(struct afb_apiset *set, const char *name, struct afb_api api);
 extern int afb_apiset_del(struct afb_apiset *set, const char *name);
+extern int afb_apiset_has(struct afb_apiset *set, const char *name);
 extern int afb_apiset_lookup(struct afb_apiset *set, const char *name, struct afb_api *api);
 extern int afb_apiset_get(struct afb_apiset *set, const char *name, struct afb_api *api);
 extern int afb_apiset_get_started(struct afb_apiset *set, const char *name, struct afb_api *api);
@@ -38,6 +40,7 @@ extern int afb_apiset_start_all_services(struct afb_apiset *set, int share_sessi
 extern void afb_apiset_update_hooks(struct afb_apiset *set, const char *name);
 extern void afb_apiset_set_verbosity(struct afb_apiset *set, const char *name, int level);
 extern int afb_apiset_get_verbosity(struct afb_apiset *set, const char *name);
+extern struct json_object *afb_apiset_describe(struct afb_apiset *set, const char *name);
 extern const char **afb_apiset_get_names(struct afb_apiset *set);
 extern void afb_apiset_enum(struct afb_apiset *set, void (*callback)(struct afb_apiset *set, const char *name, void *closure), void *closure);
 
index 7524d24..369c5bf 100644 (file)
@@ -239,14 +239,10 @@ static struct json_object *get_verbosity(struct json_object *spec)
 static void get_one_api(struct json_object *resu, const char *name, struct json_object *spec)
 {
        struct json_object *o;
-       struct afb_api api;
-       int rc;
 
-       rc = afb_apiset_lookup(main_apiset, name, &api);
-       if (!rc) {
-               o = api.itf->describe ? api.itf->describe(api.closure) : NULL;
+       o = afb_apiset_describe(main_apiset, name);
+       if (o || afb_apiset_has(main_apiset, name))
                json_object_object_add(resu, name, o);
-       }
 }
 
 /**
index fa7e489..351b332 100644 (file)
@@ -1083,17 +1083,11 @@ static void server_send_description(struct afb_stub_ws *stubws, uint32_t descid,
 
 static void server_describe_job(int signum, void *closure)
 {
-       struct afb_api api;
        struct json_object *obj;
        struct server_describe *desc = closure;
 
        /* get the description if possible */
-       obj = NULL;
-       if (!signum
-        && !afb_apiset_get(desc->stubws->apiset, desc->stubws->apiname, &api)
-        && api.itf->describe) {
-               obj = api.itf->describe(api.closure);
-       }
+       obj = !signum ? afb_apiset_describe(desc->stubws->apiset, desc->stubws->apiname) : NULL;
 
        /* send it */
        server_send_description(desc->stubws, desc->descid, obj);