Add vfail and vsuccess interfaces
[src/app-framework-binder.git] / include / afb / afb-req-itf.h
index c980838..65f2bfe 100644 (file)
 
 #pragma once
 
-#if !defined(_GNU_SOURCE)
-# error "_GNU_SOURCE must be defined for using vasprintf"
-#endif
-
-#include <stdlib.h>
 #include <stdarg.h>
-#include <stdio.h>
 
-#include <afb/afb-event-itf.h>
+#include "afb-event-itf.h"
 
 /* avoid inclusion of <json-c/json.h> */
 struct json_object;
@@ -56,8 +50,8 @@ struct afb_req_itf {
        void (*success)(void *closure, struct json_object *obj, const char *info);
        void (*fail)(void *closure, const char *status, const char *info);
 
-       const char *(*raw)(void *closure, size_t *size);
-       void (*send)(void *closure, const char *buffer, size_t size);
+       void (*vsuccess)(void *closure, struct json_object *obj, const char *fmt, va_list args);
+       void (*vfail)(void *closure, const char *status, const char *fmt, va_list args);
 
        void *(*context_get)(void *closure);
        void (*context_set)(void *closure, void *value, void (*free_value)(void*));
@@ -72,6 +66,7 @@ struct afb_req_itf {
        int (*unsubscribe)(void *closure, struct afb_event event);
 
        void (*subcall)(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cb_closure);
+       int (*subcallsync)(void *closure, const char *api, const char *verb, struct json_object *args, struct json_object **result);
 };
 
 /*
@@ -165,16 +160,26 @@ static inline void afb_req_success(struct afb_req req, struct json_object *obj,
  * Thus, in the case where 'obj' should remain available after
  * the function returns, the function 'json_object_get' shall be used.
  */
+static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...) __attribute__((format(printf, 3, 4)));
 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
 {
-       char *message;
        va_list args;
        va_start(args, info);
-       if (info == NULL || vasprintf(&message, info, args) < 0)
-               message = NULL;
+       req.itf->vsuccess(req.closure, obj, info, args);
        va_end(args);
-       afb_req_success(req, obj, message);
-       free(message);
+}
+
+/*
+ * Same as 'afb_req_success_f' but the arguments to the format 'info'
+ * are given as a variable argument list instance.
+ *
+ * For convenience, the function calls 'json_object_put' for 'obj'.
+ * Thus, in the case where 'obj' should remain available after
+ * the function returns, the function 'json_object_get' shall be used.
+ */
+static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
+{
+       req.itf->vsuccess(req.closure, obj, info, args);
 }
 
 /*
@@ -185,10 +190,6 @@ static inline void afb_req_success_f(struct afb_req req, struct json_object *obj
  * Note that calling afb_req_fail("success", info) is equivalent
  * to call afb_req_success(NULL, info). Thus even if possible it
  * is strongly recommanded to NEVER use "success" for status.
- *
- * For convenience, the function calls 'json_object_put' for 'obj'.
- * Thus, in the case where 'obj' should remain available after
- * the function returns, the function 'json_object_get' shall be used.
  */
 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
 {
@@ -198,21 +199,23 @@ static inline void afb_req_fail(struct afb_req req, const char *status, const ch
 /*
  * Same as 'afb_req_fail' but the 'info' is a formatting
  * string followed by arguments.
- *
- * For convenience, the function calls 'json_object_put' for 'obj'.
- * Thus, in the case where 'obj' should remain available after
- * the function returns, the function 'json_object_get' shall be used.
  */
+static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) __attribute__((format(printf, 3, 4)));
 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
 {
-       char *message;
        va_list args;
        va_start(args, info);
-       if (info == NULL || vasprintf(&message, info, args) < 0)
-               message = NULL;
+       req.itf->vfail(req.closure, status, info, args);
        va_end(args);
-       afb_req_fail(req, status, message);
-       free(message);
+}
+
+/*
+ * Same as 'afb_req_fail_f' but the arguments to the format 'info'
+ * are given as a variable argument list instance.
+ */
+static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
+{
+       req.itf->vfail(req.closure, status, info, args);
 }
 
 /*
@@ -310,7 +313,7 @@ static inline int afb_req_session_set_LOA(struct afb_req req, unsigned level)
  */
 static inline struct afb_req *afb_req_store(struct afb_req req)
 {
-       struct afb_req *result = malloc(sizeof *result);
+       struct afb_req *result = (struct afb_req*)malloc(sizeof *result);
        if (result != NULL) {
                *result = req;
                afb_req_addref(req);
@@ -358,22 +361,32 @@ static inline int afb_req_unsubscribe(struct afb_req req, struct afb_event event
  * On completion, the function 'callback' is invoked with the
  * 'closure' given at call and two other parameters: 'iserror' and 'result'.
  * 'iserror' is a boolean that indicates if the reply is an error reply.
- * 'result' is the json object of the reply.
+ * 'result' is the json object of the reply, you must not call json_object_put
+ * on the result.
+ *
+ * For convenience, the function calls 'json_object_put' for 'args'.
+ * Thus, in the case where 'args' should remain available after
+ * the function returns, the function 'json_object_get' shall be used.
  */
 static inline void afb_req_subcall(struct afb_req req, const char *api, const char *verb, struct json_object *args, void (*callback)(void *closure, int iserror, struct json_object *result), void *closure)
 {
        req.itf->subcall(req.closure, api, verb, args, callback, closure);
 }
 
-/* internal use */
-static inline const char *afb_req_raw(struct afb_req req, size_t *size)
-{
-       return req.itf->raw(req.closure, size);
-}
-
-/* internal use */
-static inline void afb_req_send(struct afb_req req, const char *buffer, size_t size)
+/*
+ * Makes a call to the method of name 'api' / 'verb' with the object 'args'.
+ * This call is made in the context of the request 'req'.
+ * This call is synchronous, it waits untill completion of the request.
+ * It returns 0 on an error answer and returns 1 when no error was detected.
+ * The object pointed by 'result' is filled and must be released by the caller
+ * after its use by calling 'json_object_put'.
+ *
+ * For convenience, the function calls 'json_object_put' for 'args'.
+ * Thus, in the case where 'args' should remain available after
+ * the function returns, the function 'json_object_get' shall be used.
+ */
+static inline int afb_req_subcall_sync(struct afb_req req, const char *api, const char *verb, struct json_object *args, struct json_object **result)
 {
-       req.itf->send(req.closure, buffer, size);
+       return req.itf->subcallsync(req.closure, api, verb, args, result);
 }