Make status common
authorJosé Bollo <jose.bollo@iot.bzh>
Mon, 3 Jul 2017 11:57:19 +0000 (13:57 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Mon, 3 Jul 2017 12:13:46 +0000 (14:13 +0200)
Make all error status for bindings use
a common convention: a negative value
means an error.

Change-Id: Id09610051295810f04f00477d7ec1d9771bf7975
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
12 files changed:
bindings/samples/HelloWorld.c
docs/afb-binding-references.md
include/afb/afb-req-common.h
include/afb/afb-service-v1.h
include/afb/afb-service-v2.h
src/afb-stub-ws.c
src/afb-subcall.c
src/afb-svc.c
src/afb-ws-json1.c
src/afb-xreq.c
src/afb-xreq.h
src/main.c

index 89ed829..731ea53 100644 (file)
@@ -169,10 +169,10 @@ static void pingJson (afb_req request) {
     ping(request, jresp, "pingJson");
 }
 
-static void subcallcb (void *prequest, int iserror, json_object *object)
+static void subcallcb (void *prequest, int status, json_object *object)
 {
        afb_req request = afb_req_unstore(prequest);
-       if (iserror)
+       if (status < 0)
                afb_req_fail(request, "failed", json_object_to_json_string(object));
        else
                afb_req_success(request, json_object_get(object), NULL);
@@ -204,7 +204,7 @@ static void subcallsync (afb_req request)
                afb_req_fail(request, "failed", "bad arguments");
        else {
                rc = afb_req_subcall_sync(request, api, verb, object, &result);
-               if (rc)
+               if (rc >= 0)
                        afb_req_success(request, result, NULL);
                else {
                        afb_req_fail(request, "failed", json_object_to_json_string(result));
@@ -287,10 +287,10 @@ static void eventpush (afb_req request)
        json_object_put(object);
 }
 
-static void callcb (void *prequest, int iserror, json_object *object)
+static void callcb (void *prequest, int status, json_object *object)
 {
        afb_req request = afb_req_unstore(prequest);
-       if (iserror)
+       if (status < 0)
                afb_req_fail(request, "failed", json_object_to_json_string(object));
        else
                afb_req_success(request, json_object_get(object), NULL);
@@ -322,7 +322,7 @@ static void callsync (afb_req request)
                afb_req_fail(request, "failed", "bad arguments");
        else {
                rc = afb_service_call_sync(api, verb, object, &result);
-               if (rc)
+               if (rc >= 0)
                        afb_req_success(request, result, NULL);
                else {
                        afb_req_fail(request, "failed", json_object_to_json_string(result));
index e8b9629..7cc5ba8 100644 (file)
@@ -245,7 +245,7 @@ bindings for themselves.
  *
  * The 'callback' receives 3 arguments:
  *  1. 'closure' the user defined closure pointer 'callback_closure',
- *  2. 'iserror' a boolean status being true (not null) when an error occured,
+ *  2. 'status' a status being 0 on success or negative when an error occured,
  *  2. 'result' the resulting data as a JSON object.
  *
  * @param api      The api name of the method to call
@@ -260,7 +260,7 @@ void afb_service_call(
        const char *api,
        const char *verb,
        struct json_object *args,
-       void (*callback)(void*closure, int iserror, struct json_object *result),
+       void (*callback)(void*closure, int status, struct json_object *result),
        void *callback_closure);
 
 /**
@@ -276,7 +276,7 @@ void afb_service_call(
  * @param args     The arguments to pass to the method
  * @param result   Where to store the result - should call json_object_put on it -
  *
- * @returns 1 in case of success or 0 in case of error.
+ * @returns 0 in case of success or a negative value in case of error.
  *
  * @see also 'afb_req_subcall'
  */
@@ -578,7 +578,7 @@ client (with its permissions).
  * This call is made in the context of the request 'req'.
  * 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.
+ * 'status' is 0 on success or negative when on an error reply.
  * 'result' is the json object of the reply, you must not call json_object_put
  * on the result.
  *
@@ -591,14 +591,14 @@ void afb_req_subcall(
                 const char *api,
                 const char *verb,
                 struct json_object *args,
-                void (*callback)(void *closure, int iserror, struct json_object *result),
+                void (*callback)(void *closure, int status, struct json_object *result),
                 void *closure);
 
 /*
  * 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.
+ * It returns 0 on success or a negative value on error answer.
  * The object pointed by 'result' is filled and must be released by the caller
  * after its use by calling 'json_object_put'.
  *
index df37739..9fce668 100644 (file)
@@ -336,7 +336,7 @@ static inline int afb_req_unsubscribe(struct afb_req req, struct afb_event event
  * This call is made in the context of the request 'req'.
  * 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.
+ * 'status' is 0 on success or negative when on an error reply.
  * 'result' is the json object of the reply, you must not call json_object_put
  * on the result.
  *
@@ -353,7 +353,7 @@ static inline void afb_req_subcall(struct afb_req req, const char *api, const ch
  * 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.
+ * It returns 0 on success or a negative value on error answer.
  * The object pointed by 'result' is filled and must be released by the caller
  * after its use by calling 'json_object_put'.
  *
index ee207ab..db79838 100644 (file)
@@ -29,7 +29,7 @@
  *
  * The 'callback' receives 3 arguments:
  *  1. 'closure' the user defined closure pointer 'callback_closure',
- *  2. 'iserror' a boolean status being true (not null) when an error occured,
+ *  2. 'status' a status being 0 on success or negative when an error occured,
  *  2. 'result' the resulting data as a JSON object.
  *
  * @param service  The service as received during initialisation
@@ -46,7 +46,7 @@ static inline void afb_service_call_v1(
        const char *api,
        const char *verb,
        struct json_object *args,
-       void (*callback)(void*closure, int iserror, struct json_object *result),
+       void (*callback)(void*closure, int status, struct json_object *result),
        void *callback_closure)
 {
        service.itf->call(service.closure, api, verb, args, callback, callback_closure);
@@ -66,7 +66,7 @@ static inline void afb_service_call_v1(
  * @param args     The arguments to pass to the method
  * @param result   Where to store the result - should call json_object_put on it -
  *
- * @returns 1 in case of success or 0 in case of error.
+ * @returns 0 in case of success or a negative value in case of error.
  *
  * @see also 'afb_req_subcall'
  */
index 0e92036..08974c9 100644 (file)
@@ -29,7 +29,7 @@
  *
  * The 'callback' receives 3 arguments:
  *  1. 'closure' the user defined closure pointer 'callback_closure',
- *  2. 'iserror' a boolean status being true (not null) when an error occured,
+ *  2. 'status' a status being 0 on success or negative when an error occured,
  *  2. 'result' the resulting data as a JSON object.
  *
  * @param api      The api name of the method to call
@@ -44,7 +44,7 @@ static inline void afb_service_call_v2(
        const char *api,
        const char *verb,
        struct json_object *args,
-       void (*callback)(void*closure, int iserror, struct json_object *result),
+       void (*callback)(void*closure, int status, struct json_object *result),
        void *callback_closure)
 {
        afb_get_service_v2().itf->call(afb_get_service_v2().closure, api, verb, args, callback, callback_closure);
@@ -63,7 +63,7 @@ static inline void afb_service_call_v2(
  * @param args     The arguments to pass to the method
  * @param result   Where to store the result - should call json_object_put on it -
  *
- * @returns 1 in case of success or 0 in case of error.
+ * @returns 0 in case of success or a negative value in case of error.
  *
  * @see also 'afb_req_subcall'
  */
index 1dc19fb..fa7e489 100644 (file)
@@ -740,11 +740,11 @@ static void client_reply_fail(struct afb_stub_ws *stubws, struct readbuf *rb)
 }
 
 /* send a subcall reply */
-static void client_send_subcall_reply(struct client_subcall *subcall, int iserror, json_object *object)
+static void client_send_subcall_reply(struct client_subcall *subcall, int status, json_object *object)
 {
        int rc;
        struct writebuf wb = { .count = 0 };
-       char ie = (char)!!iserror;
+       char ie = status < 0;
 
        if (!writebuf_char(&wb, CHAR_FOR_SUBCALL_REPLY)
         || !writebuf_uint32(&wb, subcall->subcallid)
@@ -761,9 +761,9 @@ static void client_send_subcall_reply(struct client_subcall *subcall, int iserro
 }
 
 /* callback for subcall reply */
-static void client_subcall_reply_cb(void *closure, int iserror, json_object *object)
+static void client_subcall_reply_cb(void *closure, int status, json_object *object)
 {
-       client_send_subcall_reply(closure, iserror, object);
+       client_send_subcall_reply(closure, status, object);
        free(closure);
 }
 
@@ -1040,14 +1040,14 @@ overflow:
 /* on subcall reply */
 static void server_on_subcall_reply(struct afb_stub_ws *stubws, struct readbuf *rb)
 {
-       char iserror;
+       char ie;
        uint32_t subcallid;
        struct json_object *object;
        struct server_subcall *sc, **psc;
 
        /* reads the call message data */
        if (!readbuf_uint32(rb, &subcallid)
-        || !readbuf_char(rb, &iserror)
+        || !readbuf_char(rb, &ie)
         || !readbuf_object(rb, &object)) {
                /* TODO bad protocol */
                return;
@@ -1064,7 +1064,7 @@ static void server_on_subcall_reply(struct afb_stub_ws *stubws, struct readbuf *
        } else {
                *psc = sc->next;
                pthread_mutex_unlock(&stubws->mutex);
-               sc->callback(sc->closure, (int)iserror, object);
+               sc->callback(sc->closure, -(int)ie, object);
                free(sc);
        }
        json_object_put(object);
index 3c75b17..4af6274 100644 (file)
@@ -49,11 +49,11 @@ static void subcall_destroy(struct afb_xreq *xreq)
        free(subcall);
 }
 
-static void subcall_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj)
+static void subcall_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
 {
        struct subcall *subcall = CONTAINER_OF_XREQ(struct subcall, xreq);
 
-       subcall->callback(subcall->closure, iserror, obj);
+       subcall->callback(subcall->closure, status, obj);
        json_object_put(obj);
 }
 
index 97312e6..2a8153e 100644 (file)
@@ -57,7 +57,7 @@ struct svc_req
        /* sync */
        struct jobloop *jobloop;
        struct json_object *result;
-       int iserror;
+       int status;
 };
 
 /* functions for services */
@@ -81,7 +81,7 @@ static const struct afb_evt_itf evt_itf = {
 
 /* functions for requests of services */
 static void svcreq_destroy(struct afb_xreq *xreq);
-static void svcreq_reply(struct afb_xreq *xreq, int iserror, json_object *obj);
+static void svcreq_reply(struct afb_xreq *xreq, int status, json_object *obj);
 
 /* interface for requests of services */
 const struct afb_xreq_query_itf afb_svc_xreq_itf = {
@@ -316,16 +316,16 @@ static void svcreq_sync_leave(struct svc_req *svcreq)
        }
 }
 
-static void svcreq_reply(struct afb_xreq *xreq, int iserror, json_object *obj)
+static void svcreq_reply(struct afb_xreq *xreq, int status, json_object *obj)
 {
        struct svc_req *svcreq = CONTAINER_OF_XREQ(struct svc_req, xreq);
        if (svcreq->callback) {
                struct afb_svc *svc = svcreq->svc;
-               svcreq->callback(svcreq->closure, iserror, obj);
-               HOOK(call_result, svc, iserror, obj);
+               svcreq->callback(svcreq->closure, status, obj);
+               HOOK(call_result, svc, status, obj);
                json_object_put(obj);
        } else {
-               svcreq->iserror = iserror;
+               svcreq->status = status;
                svcreq->result = obj;
                svcreq_sync_leave(svcreq);
        }
@@ -340,7 +340,7 @@ static void svcreq_sync_enter(int signum, void *closure, struct jobloop *jobloop
                afb_xreq_process(&svcreq->xreq, svcreq->svc->apiset);
        } else {
                svcreq->result = afb_msg_json_internal_error();
-               svcreq->iserror = 1;
+               svcreq->status = -1;
                svcreq_sync_leave(svcreq);
        }
 }
@@ -362,8 +362,8 @@ static void svc_call(void *closure, const char *api, const char *verb, struct js
                ERROR("out of memory");
                json_object_put(args);
                ierr = afb_msg_json_internal_error();
-               callback(cbclosure, 1, ierr);
-               HOOK(call_result, svc, 1, ierr);
+               callback(cbclosure, -1, ierr);
+               HOOK(call_result, svc, -1, ierr);
                json_object_put(ierr);
                return;
        }
@@ -393,17 +393,18 @@ static int svc_call_sync(void *closure, const char *api, const char *verb, struc
                errno = ENOMEM;
                json_object_put(args);
                *result = afb_msg_json_internal_error();
-               rc = 0;
+               rc = -1;
        } else {
                /* initialises the request */
                svcreq->jobloop = NULL;
                svcreq->callback = NULL;
                svcreq->result = NULL;
-               svcreq->iserror = 1;
+               svcreq->status = 0;
                afb_xreq_addref(&svcreq->xreq);
                rc = jobs_enter(NULL, 0, svcreq_sync_enter, svcreq);
-               rc = rc >= 0 && !svcreq->iserror;
-               *result = (rc || svcreq->result) ? svcreq->result : afb_msg_json_internal_error();
+               if (rc >= 0)
+                       rc = svcreq->status;
+               *result = (rc >= 0 || svcreq->result) ? svcreq->result : afb_msg_json_internal_error();
                afb_xreq_unref(&svcreq->xreq);
        }
        HOOK(callsync_result, svc, rc, *result);
index a0cdb8a..7c05cf7 100644 (file)
@@ -50,7 +50,7 @@ static void aws_on_event(struct afb_ws_json1 *ws, const char *event, int eventid
 
 /* predeclaration of wsreq callbacks */
 static void wsreq_destroy(struct afb_xreq *xreq);
-static void wsreq_reply(struct afb_xreq *xreq, int iserror, json_object *obj);
+static void wsreq_reply(struct afb_xreq *xreq, int status, json_object *obj);
 
 /* declaration of websocket structure */
 struct afb_ws_json1
@@ -229,12 +229,12 @@ static void wsreq_destroy(struct afb_xreq *xreq)
        free(wsreq);
 }
 
-static void wsreq_reply(struct afb_xreq *xreq, int iserror, json_object *obj)
+static void wsreq_reply(struct afb_xreq *xreq, int status, json_object *obj)
 {
        struct afb_wsreq *wsreq = CONTAINER_OF_XREQ(struct afb_wsreq, xreq);
        int rc;
 
-       rc = (iserror ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
+       rc = (status < 0 ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
                        wsreq->msgj1, obj, afb_context_sent_token(&wsreq->xreq.context));
        if (rc)
                ERROR("Can't send reply: %m");
index 80fa963..cd0b086 100644 (file)
@@ -109,7 +109,7 @@ static void xreq_fail_cb(void *closure, const char *status, const char *info)
                if (xreq->queryitf->fail)
                        xreq->queryitf->fail(xreq, status, info);
                else
-                       xreq->queryitf->reply(xreq, 1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
+                       xreq->queryitf->reply(xreq, -1, afb_msg_json_reply_error(status, info, &xreq->context, NULL));
        }
 }
 
@@ -215,7 +215,7 @@ struct xreq_sync
        struct json_object *args;
        struct jobloop *jobloop;
        struct json_object *result;
-       int iserror;
+       int status;
 };
 
 static void xreq_sync_leave(struct xreq_sync *sync)
@@ -227,11 +227,11 @@ static void xreq_sync_leave(struct xreq_sync *sync)
        }
 }
 
-static void xreq_sync_reply(void *closure, int iserror, struct json_object *obj)
+static void xreq_sync_reply(void *closure, int status, struct json_object *obj)
 {
        struct xreq_sync *sync = closure;
 
-       sync->iserror = iserror;
+       sync->status = status;
        sync->result = json_object_get(obj);
        xreq_sync_leave(sync);
 }
@@ -244,7 +244,7 @@ static void xreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
                sync->jobloop = jobloop;
                xreq_subcall_cb(sync->caller, sync->api, sync->verb, json_object_get(sync->args), xreq_sync_reply, sync);
        } else {
-               sync->iserror = 1;
+               sync->status = -1;
                xreq_sync_leave(sync);
        }
 }
@@ -261,16 +261,16 @@ static int xreq_subcallsync_cb(void *closure, const char *api, const char *verb,
        sync.args = args;
        sync.jobloop = NULL;
        sync.result = NULL;
-       sync.iserror = 1;
+       sync.status = 0;
 
        rc = jobs_enter(NULL, 0, xreq_sync_enter, &sync);
        json_object_put(args);
-       if (rc < 0 || sync.iserror) {
+       if (rc < 0 || sync.status < 0) {
                *result = sync.result ? : afb_msg_json_internal_error();
-               return 0;
+               return -1;
        }
        *result = sync.result;
-       return 1;
+       return 0;
 }
 
 static void xreq_vverbose_cb(void*closure, int level, const char *file, int line, const char *func, const char *fmt, va_list args)
@@ -391,12 +391,12 @@ struct reply
        void *closure;
 };
 
-static void xreq_hooked_subcall_reply_cb(void *closure, int iserror, struct json_object *result)
+static void xreq_hooked_subcall_reply_cb(void *closure, int status, struct json_object *result)
 {
        struct reply *reply = closure;
        
-       afb_hook_xreq_subcall_result(reply->xreq, iserror, result);
-       reply->callback(reply->closure, iserror, result);
+       afb_hook_xreq_subcall_result(reply->xreq, status, result);
+       reply->callback(reply->closure, status, result);
        free(reply);
 }
 
index 4230774..77f843f 100644 (file)
@@ -35,7 +35,7 @@ struct afb_xreq_query_itf {
        struct afb_arg (*get)(struct afb_xreq *xreq, const char *name);
        void (*success)(struct afb_xreq *xreq, struct json_object *obj, const char *info);
        void (*fail)(struct afb_xreq *xreq, const char *status, const char *info);
-       void (*reply)(struct afb_xreq *xreq, int iserror, struct json_object *obj);
+       void (*reply)(struct afb_xreq *xreq, int status, struct json_object *obj);
        void (*unref)(struct afb_xreq *xreq);
        int (*subscribe)(struct afb_xreq *xreq, struct afb_event event);
        int (*unsubscribe)(struct afb_xreq *xreq, struct afb_event event);
index 6b89aef..b932868 100644 (file)
@@ -447,11 +447,11 @@ struct startup_req
        struct afb_session *session;
 };
 
-static void startup_call_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj)
+static void startup_call_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
 {
        struct startup_req *sreq = CONTAINER_OF_XREQ(struct startup_req, xreq);
 
-       if (!iserror)
+       if (status >= 0)
                NOTICE("startup call %s returned %s", sreq->current->value, json_object_get_string(obj));
        else {
                ERROR("startup call %s ERROR! %s", sreq->current->value, json_object_get_string(obj));