afb-ws & websocket: Fix writing very long data
[src/app-framework-binder.git] / src / afb-apiset.c
index c934e8c..ae1d946 100644 (file)
@@ -29,7 +29,6 @@
 #include "afb-apiset.h"
 #include "afb-context.h"
 #include "afb-xreq.h"
-#include "jobs.h"
 
 #define INCR 8         /* CAUTION: must be a power of 2 */
 
@@ -249,7 +248,7 @@ int afb_apiset_add(struct afb_apiset *set, const char *name, struct afb_api api)
        apis->name = name;
        set->count++;
 
-       NOTICE("API %s added", name);
+       INFO("API %s added", name);
 
        return 0;
 
@@ -285,58 +284,35 @@ int afb_apiset_del(struct afb_apiset *set, const char *name)
        return -1;
 }
 
-/**
- * Get from the 'set' the API of 'name' in 'api'
- * @param set the set of API
- * @param name the name of the API to get
- * @param api the structure where to store data about the API of name
- * @return 0 in case of success or -1 in case of error
- */
-int afb_apiset_lookup(struct afb_apiset *set, const char *name, struct afb_api *api)
-{
-       const struct api_desc *i;
-
-       i = search(set, name);
-       if (i) {
-               *api = i->api;
-               return 0;
-       }
-
-       errno = ENOENT;
-       return -1;
-}
-
 /**
  * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
  * @param set the set of API
  * @param name the name of the API to get
- * @param api the structure where to store data about the API of name
- * @return 0 in case of success or -1 in case of error
+ * @param rec if not zero look also recursively in subsets
+ * @return the api pointer in case of success or NULL in case of error
  */
-static struct api_desc *get_api(struct afb_apiset *set, const char *name)
+static struct api_desc *lookup(struct afb_apiset *set, const char *name, int rec)
 {
        struct api_desc *i = search(set, name);
-       return i || !set->subset ? i : get_api(set->subset, name);
+       return i || !rec || !set->subset ? i : lookup(set->subset, name, rec);
 }
 
 /**
- * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
+ * Get from the 'set' the API of 'name' in 'api'
  * @param set the set of API
  * @param name the name of the API to get
- * @param api the structure where to store data about the API of name
- * @return 0 in case of success or -1 in case of error
+ * @param rec if not zero look also recursively in subsets
+ * @return the api pointer in case of success or NULL in case of error
  */
-int afb_apiset_get(struct afb_apiset *set, const char *name, struct afb_api *api)
+const struct afb_api *afb_apiset_lookup(struct afb_apiset *set, const char *name, int rec)
 {
-       const struct api_desc *i;
+       struct api_desc *i;
 
-       i = get_api(set, name);
-       if (!i) {
-               errno = ENOENT;
-               return -1;
-       }
-       *api = i->api;
-       return 0;
+       i = lookup(set, name, rec);
+       if (i)
+               return &i->api;
+       errno = ENOENT;
+       return NULL;
 }
 
 /**
@@ -359,7 +335,7 @@ static int start_api(struct afb_apiset *set, struct api_desc *api, int share_ses
                return -1;
        }
 
-       NOTICE("API %s starting...", api->name);
+       INFO("API %s starting...", api->name);
        if (api->api.itf->service_start) {
                api->status = EBUSY;
                rc = api->api.itf->service_start(api->api.closure, share_session, onneed, set);
@@ -380,23 +356,21 @@ static int start_api(struct afb_apiset *set, struct api_desc *api, int share_ses
 }
 
 /**
- * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
+ * Get from the 'set' the API of 'name' in 'api'
  * @param set the set of API
  * @param name the name of the API to get
- * @param api the structure where to store data about the API of name
+ * @param rec if not zero look also recursively in subsets
  * @return 0 in case of success or -1 in case of error
  */
-int afb_apiset_get_started(struct afb_apiset *set, const char *name, struct afb_api *api)
+const struct afb_api *afb_apiset_lookup_started(struct afb_apiset *set, const char *name, int rec)
 {
        struct api_desc *i;
 
-       i = get_api(set, name);
-       if (!i) {
-               errno = ENOENT;
-               return -1;
-       }
-       *api = i->api;
-       return i->status ? start_api(set, i, 1, 1) : 0;
+       i = lookup(set, name, rec);
+       if (i)
+               return i->status && start_api(set, i, 1, 1) ? NULL : &i->api;
+       errno = ENOENT;
+       return NULL;
 }
 
 /**
@@ -443,7 +417,7 @@ int afb_apiset_start_all_services(struct afb_apiset *set, int share_session)
                        return rc;
                i++;
        }
-       
+
        return set->subset ? afb_apiset_start_all_services(set->subset, share_session) : 0;
 }
 
@@ -494,9 +468,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 +482,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
@@ -549,11 +539,21 @@ const char **afb_apiset_get_names(struct afb_apiset *set)
  * @param callback the function to call for each name
  * @param closure the closure for the callback
  */
-void afb_apiset_enum(struct afb_apiset *set, void (*callback)(struct afb_apiset *set, const char *name, void *closure), void *closure)
+void afb_apiset_enum(struct afb_apiset *set, int rec, void (*callback)(struct afb_apiset *set, const char *name, void *closure), void *closure)
 {
-       int i;
+       struct afb_apiset *iset;
+       struct api_desc *i, *e;
 
-       for (i = 0 ; i < set->count ; i++)
-               callback(set, set->apis[i].name, closure);
+       iset = set;
+       while (iset) {
+               i = iset->apis;
+               e = &i[iset->count];
+               while (i != e) {
+                       if (lookup(set, i->name, 1) == i)
+                               callback(iset, i->name, closure);
+                       i++;
+               }
+               iset = rec ? iset->subset : NULL;
+       }
 }