aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-04-10 11:41:21 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-04-10 11:41:21 +0200
commit5d248158cc380d0a164fa56b46a7bdede4115407 (patch)
treedf9df98654af13f291f79dfa0cc073f2d7ae6de4
parentb529ded85a3e50a1aa6529e870b2e2f43e377ca2 (diff)
Refactor of API interfaces
The new interface has increased functionnality, it allows to set debug flags by API. Change-Id: Ibd35fb02ed645e22cac8e7021b37b96e07e79877 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afb-api-dbus.c9
-rw-r--r--src/afb-api-so-v1.c24
-rw-r--r--src/afb-api-so-v2.c24
-rw-r--r--src/afb-api-ws.c9
-rw-r--r--src/afb-apis.c94
-rw-r--r--src/afb-apis.h18
6 files changed, 154 insertions, 24 deletions
diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c
index 161c0941..ec47f241 100644
--- a/src/afb-api-dbus.c
+++ b/src/afb-api-dbus.c
@@ -576,6 +576,11 @@ static int api_dbus_client_on_manage_event(sd_bus_message *m, void *userdata, sd
return 1;
}
+static struct afb_api_itf dbus_api_itf = {
+ .call = api_dbus_client_call,
+ .service_start = api_dbus_service_start
+};
+
/* adds a afb-dbus-service client api */
int afb_api_dbus_add_client(const char *path)
{
@@ -614,9 +619,7 @@ int afb_api_dbus_add_client(const char *path)
/* record it as an API */
afb_api.closure = api;
- afb_api.call = api_dbus_client_call;
- afb_api.service_start = api_dbus_service_start;
- afb_api.update_hooks = NULL;
+ afb_api.itf = &dbus_api_itf;
if (afb_apis_add(api->api, afb_api) < 0)
goto error2;
diff --git a/src/afb-api-so-v1.c b/src/afb-api-so-v1.c
index cb61019c..4ec378e1 100644
--- a/src/afb-api-so-v1.c
+++ b/src/afb-api-so-v1.c
@@ -121,6 +121,26 @@ static void update_hooks_cb(void *closure)
afb_ditf_update_hook(&desc->ditf);
}
+static int get_verbosity_cb(void *closure)
+{
+ struct api_so_v1 *desc = closure;
+ return desc->ditf.interface.verbosity;
+}
+
+static void set_verbosity_cb(void *closure, int level)
+{
+ struct api_so_v1 *desc = closure;
+ desc->ditf.interface.verbosity = level;
+}
+
+static struct afb_api_itf so_v1_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
+};
+
int afb_api_so_v1_add(const char *path, void *handle)
{
struct api_so_v1 *desc;
@@ -177,9 +197,7 @@ int afb_api_so_v1_add(const char *path, void *handle)
/* records the binding */
afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
afb_api.closure = desc;
- afb_api.call = call_cb;
- afb_api.service_start = service_start_cb;
- afb_api.update_hooks = update_hooks_cb;
+ afb_api.itf = &so_v1_api_itf;
if (afb_apis_add(desc->binding->v1.prefix, afb_api) < 0) {
ERROR("binding [%s] can't be registered...", path);
goto error2;
diff --git a/src/afb-api-so-v2.c b/src/afb-api-so-v2.c
index 12b83aca..714bcd0f 100644
--- a/src/afb-api-so-v2.c
+++ b/src/afb-api-so-v2.c
@@ -119,6 +119,26 @@ static void update_hooks_cb(void *closure)
afb_ditf_update_hook(&desc->ditf);
}
+static int get_verbosity_cb(void *closure)
+{
+ struct api_so_v2 *desc = closure;
+ return desc->ditf.interface.verbosity;
+}
+
+static void set_verbosity_cb(void *closure, int level)
+{
+ struct api_so_v2 *desc = closure;
+ desc->ditf.interface.verbosity = level;
+}
+
+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
+};
+
int afb_api_so_v2_add(const char *path, void *handle)
{
int rc;
@@ -177,9 +197,7 @@ int afb_api_so_v2_add(const char *path, void *handle)
/* records the binding */
afb_api.closure = desc;
- afb_api.call = call_cb;
- afb_api.service_start = service_start_cb;
- afb_api.update_hooks = update_hooks_cb;
+ afb_api.itf = &so_v2_api_itf;
if (afb_apis_add(binding->api, afb_api) < 0) {
ERROR("binding [%s] can't be registered...", path);
goto error2;
diff --git a/src/afb-api-ws.c b/src/afb-api-ws.c
index 6995b2fa..4b870d45 100644
--- a/src/afb-api-ws.c
+++ b/src/afb-api-ws.c
@@ -856,6 +856,11 @@ static int api_ws_client_connect(struct api_ws *api)
return -1;
}
+static struct afb_api_itf ws_api_itf = {
+ .call = api_ws_client_call_cb,
+ .service_start = api_ws_service_start_cb
+};
+
/* adds a afb-ws-service client api */
int afb_api_ws_add_client(const char *path)
{
@@ -877,9 +882,7 @@ int afb_api_ws_add_client(const char *path)
/* record it as an API */
afb_api.closure = api;
- afb_api.call = api_ws_client_call_cb;
- afb_api.service_start = api_ws_service_start_cb;
- afb_api.update_hooks = NULL;
+ afb_api.itf = &ws_api_itf;
if (afb_apis_add(api->api, afb_api) < 0)
goto error3;
diff --git a/src/afb-apis.c b/src/afb-apis.c
index ba8e0ddc..6e093550 100644
--- a/src/afb-apis.c
+++ b/src/afb-apis.c
@@ -205,7 +205,7 @@ int afb_apis_start_service(const char *api, int share_session, int onneed)
for (i = 0 ; i < apis_count ; i++) {
if (!strcasecmp(apis_array[i].name, api))
- return apis_array[i].api.service_start(apis_array[i].api.closure, share_session, onneed);
+ return apis_array[i].api.itf->service_start(apis_array[i].api.closure, share_session, onneed);
}
ERROR("can't find service %s", api);
errno = ENOENT;
@@ -223,7 +223,7 @@ int afb_apis_start_all_services(int share_session)
int i, rc;
for (i = 0 ; i < apis_count ; i++) {
- rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1);
+ rc = apis_array[i].api.itf->service_start(apis_array[i].api.closure, share_session, 1);
if (rc < 0)
return rc;
}
@@ -244,7 +244,7 @@ static void do_call_direct(struct afb_xreq *xreq)
afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
else {
xreq->context.api_key = a->api.closure;
- a->api.call(a->api.closure, xreq);
+ a->api.itf->call(a->api.closure, xreq);
}
}
@@ -294,18 +294,94 @@ void afb_apis_call(struct afb_xreq *xreq)
}
/**
- * Ask to update the hook flags
+ * Ask to update the hook flags of the 'api'
+ * @param api the api to update (NULL updates all)
*/
-void afb_apis_update_hooks()
+void afb_apis_update_hooks(const char *api)
{
const struct api_desc *i, *e;
- i = apis_array;
- e = &apis_array[apis_count];
+ if (!api) {
+ i = apis_array;
+ e = &apis_array[apis_count];
+ } else {
+ i = search(api);
+ e = &i[!!i];
+ }
while (i != e) {
- if (i->api.update_hooks)
- i->api.update_hooks(i->api.closure);
+ if (i->api.itf->update_hooks)
+ i->api.itf->update_hooks(i->api.closure);
i++;
}
}
+/**
+ * Set the verbosity level of the 'api'
+ * @param api the api to set (NULL set all)
+ */
+void afb_apis_set_verbosity(const char *api, int level)
+{
+ const struct api_desc *i, *e;
+
+ if (!api) {
+ i = apis_array;
+ e = &apis_array[apis_count];
+ } else {
+ i = search(api);
+ e = &i[!!i];
+ }
+ while (i != e) {
+ if (i->api.itf->set_verbosity)
+ i->api.itf->set_verbosity(i->api.closure, level);
+ i++;
+ }
+}
+
+/**
+ * Set the verbosity level of the 'api'
+ * @param api the api to set (NULL set all)
+ */
+int afb_apis_get_verbosity(const char *api)
+{
+ const struct api_desc *i;
+
+ i = api ? search(api) : NULL;
+ if (!i) {
+ errno = ENOENT;
+ return -1;
+ }
+ if (!i->api.itf->get_verbosity)
+ return 0;
+
+ return i->api.itf->get_verbosity(i->api.closure);
+}
+
+/**
+ * Get the list of api names
+ * @return a NULL terminated array of api names. Must be freed.
+ */
+const char **afb_apis_get_names()
+{
+ size_t size;
+ char *dest;
+ const char **names;
+ int i;
+
+ size = apis_count * (1 + sizeof(*names)) + sizeof(*names);
+ for (i = 0 ; i < apis_count ; i++)
+ size += strlen(apis_array[i].name);
+
+ names = malloc(size);
+ if (!names)
+ errno = ENOMEM;
+ else {
+ dest = (void*)&names[apis_count+1];
+ for (i = 0 ; i < apis_count ; i++) {
+ names[i] = dest;
+ dest = stpcpy(dest, apis_array[i].name) + 1;
+ }
+ names[i] = NULL;
+ }
+ return names;
+}
+
diff --git a/src/afb-apis.h b/src/afb-apis.h
index ce33de13..02544668 100644
--- a/src/afb-apis.h
+++ b/src/afb-apis.h
@@ -21,12 +21,19 @@ struct afb_req;
struct afb_context;
struct afb_xreq;
-struct afb_api
+struct afb_api_itf
{
- void *closure;
void (*call)(void *closure, struct afb_xreq *xreq);
int (*service_start)(void *closure, int share_session, int onneed);
void (*update_hooks)(void *closure);
+ int (*get_verbosity)(void *closure);
+ void (*set_verbosity)(void *closure, int level);
+};
+
+struct afb_api
+{
+ void *closure;
+ struct afb_api_itf *itf;
};
extern void afb_apis_set_timeout(int to);
@@ -41,4 +48,9 @@ extern int afb_apis_start_service(const char *name, int share_session, int onnee
extern void afb_apis_call(struct afb_xreq *xreq);
extern void afb_apis_call_direct(struct afb_xreq *xreq);
-extern void afb_apis_update_hooks();
+extern void afb_apis_update_hooks(const char *api);
+
+extern void afb_apis_set_verbosity(const char *api, int level);
+extern int afb_apis_get_verbosity(const char *api);
+extern const char **afb_apis_get_names();
+