aboutsummaryrefslogtreecommitdiffstats
path: root/src/afb-xreq.c
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-05-29 15:54:30 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-05-29 15:54:30 +0200
commitdb01090e1f869965c07b12d9480fa6f3d2e7b1b0 (patch)
tree523e4b00bf591c56f671b5b610f6284b8ce64fbd /src/afb-xreq.c
parent6f1126ae2c585afc34d0bb06f3763e3a82ee3d37 (diff)
Add vfail and vsuccess interfaces
This now factorizes code needed to asprintf the arguments in an allocated string. But the most interesting effect is the ability to handle va_list of arguments. It can be used for library of tools. Change-Id: I4ba74c9984786f07abe0c7e53d7ef79dca863735 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/afb-xreq.c')
-rw-r--r--src/afb-xreq.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/afb-xreq.c b/src/afb-xreq.c
index 527692c6..0b828277 100644
--- a/src/afb-xreq.c
+++ b/src/afb-xreq.c
@@ -19,6 +19,7 @@
#define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
#include <errno.h>
@@ -39,6 +40,17 @@
/******************************************************************************/
+static void vinfo(void *first, void *second, const char *fmt, va_list args, void (*fun)(void*,void*,const char*))
+{
+ char *info;
+ if (fmt == NULL || vasprintf(&info, fmt, args) < 0)
+ info = NULL;
+ fun(first, second, info);
+ free(info);
+}
+
+/******************************************************************************/
+
static struct json_object *xreq_json_cb(void *closure)
{
struct afb_xreq *xreq = closure;
@@ -85,6 +97,16 @@ static void xreq_fail_cb(void *closure, const char *status, const char *info)
}
}
+static void xreq_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
+{
+ vinfo(closure, obj, fmt, args, (void*)xreq_success_cb);
+}
+
+static void xreq_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
+{
+ vinfo(closure, (void*)status, fmt, args, (void*)xreq_fail_cb);
+}
+
static void *xreq_context_get_cb(void *closure)
{
struct afb_xreq *xreq = closure;
@@ -265,6 +287,16 @@ static void xreq_hooked_fail_cb(void *closure, const char *status, const char *i
xreq_fail_cb(closure, status, info);
}
+static void xreq_hooked_vsuccess_cb(void *closure, struct json_object *obj, const char *fmt, va_list args)
+{
+ vinfo(closure, obj, fmt, args, (void*)xreq_hooked_success_cb);
+}
+
+static void xreq_hooked_vfail_cb(void *closure, const char *status, const char *fmt, va_list args)
+{
+ vinfo(closure, (void*)status, fmt, args, (void*)xreq_hooked_fail_cb);
+}
+
static void *xreq_hooked_context_get_cb(void *closure)
{
void *r = xreq_context_get_cb(closure);
@@ -372,6 +404,8 @@ const struct afb_req_itf xreq_itf = {
.get = xreq_get_cb,
.success = xreq_success_cb,
.fail = xreq_fail_cb,
+ .vsuccess = xreq_vsuccess_cb,
+ .vfail = xreq_vfail_cb,
.context_get = xreq_context_get_cb,
.context_set = xreq_context_set_cb,
.addref = xreq_addref_cb,
@@ -389,6 +423,8 @@ const struct afb_req_itf xreq_hooked_itf = {
.get = xreq_hooked_get_cb,
.success = xreq_hooked_success_cb,
.fail = xreq_hooked_fail_cb,
+ .vsuccess = xreq_hooked_vsuccess_cb,
+ .vfail = xreq_hooked_vfail_cb,
.context_get = xreq_hooked_context_get_cb,
.context_set = xreq_hooked_context_set_cb,
.addref = xreq_hooked_addref_cb,