Fix detection of error in call synchronous
[src/app-framework-binder.git] / src / afb-calls.c
index 73b89bf..084250b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
+ * Copyright (C) 2016-2019 "IoT.bzh"
  * Author: José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -55,10 +55,13 @@ struct modes
 #define mode_async  ((struct modes){ .hooked=0, .sync=0, .legacy=0 })
 #define mode_legacy_sync  ((struct modes){ .hooked=0, .sync=1, .legacy=1 })
 #define mode_legacy_async  ((struct modes){ .hooked=0, .sync=0, .legacy=1 })
+
+#if WITH_AFB_HOOK
 #define mode_hooked_sync  ((struct modes){ .hooked=1, .sync=1, .legacy=0 })
 #define mode_hooked_async  ((struct modes){ .hooked=1, .sync=0, .legacy=0 })
 #define mode_hooked_legacy_sync  ((struct modes){ .hooked=1, .sync=1, .legacy=1 })
 #define mode_hooked_legacy_async  ((struct modes){ .hooked=1, .sync=0, .legacy=1 })
+#endif
 
 union callback {
        void *any;
@@ -107,6 +110,10 @@ struct callreq
 
 /******************************************************************************/
 
+static const char _internal_error_[] = "internal-error";
+
+/******************************************************************************/
+
 static int store_reply(
                struct json_object *iobject, const char *ierror, const char *iinfo,
                struct json_object **sobject, char **serror, char **sinfo)
@@ -153,7 +160,7 @@ static void sync_enter(int signum, void *closure, struct jobloop *jobloop)
                callreq->jobloop = jobloop;
                afb_export_process_xreq(callreq->export, &callreq->xreq);
        } else {
-               afb_xreq_reply(&callreq->xreq, NULL, "internal-error", NULL);
+               afb_xreq_reply(&callreq->xreq, NULL, _internal_error_, NULL);
        }
 }
 
@@ -173,6 +180,7 @@ static void callreq_reply_cb(struct afb_xreq *xreq, struct json_object *object,
 {
        struct callreq *callreq = CONTAINER_OF_XREQ(struct callreq, xreq);
 
+#if WITH_AFB_HOOK
        /* centralized hooking */
        if (callreq->mode.hooked) {
                if (callreq->mode.sync) {
@@ -187,6 +195,7 @@ static void callreq_reply_cb(struct afb_xreq *xreq, struct json_object *object,
                                afb_hook_api_call_result(callreq->export, object, error, info);
                }
        }
+#endif
 
        /* true report of the result */
        if (callreq->mode.sync) {
@@ -341,7 +350,7 @@ static int do_sync(
 
        afb_xreq_unhooked_unref(&callreq->xreq);
 interr:
-       return store_reply(NULL, "internal-error", NULL, object, info, error);
+       return store_reply(NULL, _internal_error_, NULL, object, error, info);
 }
 
 /******************************************************************************/
@@ -363,7 +372,7 @@ static void do_async(
        callreq = callreq_create(export, caller, api, verb, args, flags, mode);
 
        if (!callreq)
-               final(closure, NULL, "internal-error", NULL, (union callback){ .any = callback }, export, caller);
+               final(closure, NULL, _internal_error_, NULL, (union callback){ .any = callback }, export, caller);
        else {
                callreq->callback.any = callback;
                callreq->closure = closure;
@@ -414,18 +423,6 @@ void afb_calls_call(
        do_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_call, mode_async);
 }
 
-void afb_calls_hooked_call(
-               struct afb_export *export,
-               const char *api,
-               const char *verb,
-               struct json_object *args,
-               void (*callback)(void*, struct json_object*, const char *error, const char *info, struct afb_api_x3*),
-               void *closure)
-{
-       afb_hook_api_call(export, api, verb, args);
-       do_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_call, mode_hooked_async);
-}
-
 int afb_calls_call_sync(
                struct afb_export *export,
                const char *api,
@@ -438,19 +435,6 @@ int afb_calls_call_sync(
        return do_sync(export, NULL, api, verb, args, CALLFLAGS, object, error, info, mode_sync);
 }
 
-int afb_calls_hooked_call_sync(
-               struct afb_export *export,
-               const char *api,
-               const char *verb,
-               struct json_object *args,
-               struct json_object **object,
-               char **error,
-               char **info)
-{
-       afb_hook_api_callsync(export, api, verb, args);
-       return do_sync(export, NULL, api, verb, args, CALLFLAGS, object, error, info, mode_hooked_sync);
-}
-
 void afb_calls_subcall(
                        struct afb_xreq *xreq,
                        const char *api,
@@ -463,30 +447,56 @@ void afb_calls_subcall(
        do_async(NULL, xreq, api, verb, args, flags, callback, closure, final_subcall, mode_async);
 }
 
-void afb_calls_hooked_subcall(
+int afb_calls_subcall_sync(
                        struct afb_xreq *xreq,
                        const char *api,
                        const char *verb,
                        struct json_object *args,
                        int flags,
-                       void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
-                       void *closure)
+                       struct json_object **object,
+                       char **error,
+                       char **info)
 {
-       afb_hook_xreq_subcall(xreq, api, verb, args, flags);
-       do_async(NULL, xreq, api, verb, args, flags, callback, closure, final_subcall, mode_hooked_async);
+       return do_sync(NULL, xreq, api, verb, args, flags, object, error, info, mode_sync);
 }
 
-int afb_calls_subcall_sync(
+#if WITH_AFB_HOOK
+void afb_calls_hooked_call(
+               struct afb_export *export,
+               const char *api,
+               const char *verb,
+               struct json_object *args,
+               void (*callback)(void*, struct json_object*, const char *error, const char *info, struct afb_api_x3*),
+               void *closure)
+{
+       afb_hook_api_call(export, api, verb, args);
+       do_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_call, mode_hooked_async);
+}
+
+int afb_calls_hooked_call_sync(
+               struct afb_export *export,
+               const char *api,
+               const char *verb,
+               struct json_object *args,
+               struct json_object **object,
+               char **error,
+               char **info)
+{
+       afb_hook_api_callsync(export, api, verb, args);
+       return do_sync(export, NULL, api, verb, args, CALLFLAGS, object, error, info, mode_hooked_sync);
+}
+
+void afb_calls_hooked_subcall(
                        struct afb_xreq *xreq,
                        const char *api,
                        const char *verb,
                        struct json_object *args,
                        int flags,
-                       struct json_object **object,
-                       char **error,
-                       char **info)
+                       void (*callback)(void *closure, struct json_object *object, const char *error, const char * info, struct afb_req_x2 *req),
+                       void *closure)
 {
-       return do_sync(NULL, xreq, api, verb, args, flags, object, error, info, mode_sync);
+       afb_hook_xreq_subcall(xreq, api, verb, args, flags);
+       do_async(NULL, xreq, api, verb, args, flags, callback, closure, final_subcall, mode_hooked_async);
 }
 
 int afb_calls_hooked_subcall_sync(
@@ -502,6 +512,7 @@ int afb_calls_hooked_subcall_sync(
        afb_hook_xreq_subcallsync(xreq, api, verb, args, flags);
        return do_sync(NULL, xreq, api, verb, args, flags, object, error, info, mode_hooked_sync);
 }
+#endif
 
 /******************************************************************************/
 /******************************************************************************/
@@ -548,7 +559,7 @@ static int do_legacy_sync(
        afb_xreq_unhooked_unref(&callreq->xreq);
 interr:
        if (object)
-               *object = afb_msg_json_internal_error();
+               *object = afb_msg_json_reply(NULL, _internal_error_, NULL, NULL);
        return -1;
 }
 
@@ -572,7 +583,7 @@ static void do_legacy_async(
        callreq = callreq_create(export, caller, api, verb, args, flags, mode);
 
        if (!callreq) {
-               ie = afb_msg_json_internal_error();
+               ie = afb_msg_json_reply(NULL, _internal_error_, NULL, NULL);
                final(closure, -1, ie, (union callback){ .any = callback }, export, caller);
                json_object_put(ie);
        } else {
@@ -623,49 +634,50 @@ void afb_calls_legacy_call_v12(
        do_legacy_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_legacy_call_v12, mode_legacy_async);
 }
 
-void afb_calls_legacy_hooked_call_v12(
+void afb_calls_legacy_call_v3(
                struct afb_export *export,
                const char *api,
                const char *verb,
                struct json_object *args,
-               void (*callback)(void*, int, struct json_object*),
+               void (*callback)(void*, int, struct json_object*, struct afb_api_x3 *),
                void *closure)
 {
-       afb_hook_api_call(export, api, verb, args);
-       do_legacy_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_legacy_call_v12, mode_hooked_legacy_async);
+       do_legacy_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_legacy_call_v3, mode_legacy_async);
 }
 
-void afb_calls_legacy_call_v3(
+int afb_calls_legacy_call_sync(
                struct afb_export *export,
                const char *api,
                const char *verb,
                struct json_object *args,
-               void (*callback)(void*, int, struct json_object*, struct afb_api_x3 *),
-               void *closure)
+               struct json_object **result)
 {
-       do_legacy_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_legacy_call_v3, mode_legacy_async);
+       return do_legacy_sync(export, NULL, api, verb, args, CALLFLAGS, result, mode_legacy_sync);
 }
 
-void afb_calls_legacy_hooked_call_v3(
+#if WITH_AFB_HOOK
+void afb_calls_legacy_hooked_call_v12(
                struct afb_export *export,
                const char *api,
                const char *verb,
                struct json_object *args,
-               void (*callback)(void*, int, struct json_object*, struct afb_api_x3 *),
+               void (*callback)(void*, int, struct json_object*),
                void *closure)
 {
        afb_hook_api_call(export, api, verb, args);
-       do_legacy_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_legacy_call_v3, mode_hooked_legacy_async);
+       do_legacy_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_legacy_call_v12, mode_hooked_legacy_async);
 }
 
-int afb_calls_legacy_call_sync(
+void afb_calls_legacy_hooked_call_v3(
                struct afb_export *export,
                const char *api,
                const char *verb,
                struct json_object *args,
-               struct json_object **result)
+               void (*callback)(void*, int, struct json_object*, struct afb_api_x3 *),
+               void *closure)
 {
-       return do_legacy_sync(export, NULL, api, verb, args, CALLFLAGS, result, mode_legacy_sync);
+       afb_hook_api_call(export, api, verb, args);
+       do_legacy_async(export, NULL, api, verb, args, CALLFLAGS, callback, closure, final_legacy_call_v3, mode_hooked_legacy_async);
 }
 
 int afb_calls_legacy_hooked_call_sync(
@@ -686,6 +698,7 @@ int afb_calls_legacy_hooked_call_sync(
                json_object_put(object);
        return rc;
 }
+#endif
 
 /******************************************************************************/
 
@@ -738,72 +751,73 @@ void afb_calls_legacy_subcall_v1(
        do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v1, mode_legacy_async);
 }
 
-void afb_calls_legacy_hooked_subcall_v1(
+void afb_calls_legacy_subcall_v2(
                struct afb_xreq *caller,
                const char *api,
                const char *verb,
                struct json_object *args,
-               void (*callback)(void*, int, struct json_object*),
+               void (*callback)(void*, int, struct json_object*, struct afb_req_x1),
                void *closure)
 {
-       afb_hook_xreq_subcall(caller, api, verb, args, LEGACY_SUBCALLFLAGS);
-       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v1, mode_hooked_legacy_async);
+       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v2, mode_legacy_async);
 }
 
-void afb_calls_legacy_subcall_v2(
+void afb_calls_legacy_subcall_v3(
                struct afb_xreq *caller,
                const char *api,
                const char *verb,
                struct json_object *args,
-               void (*callback)(void*, int, struct json_object*, struct afb_req_x1),
+               void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *),
                void *closure)
 {
-       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v2, mode_legacy_async);
+       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v3, mode_legacy_async);
 }
 
-void afb_calls_legacy_hooked_subcall_v2(
+int afb_calls_legacy_subcall_sync(
                struct afb_xreq *caller,
                const char *api,
                const char *verb,
                struct json_object *args,
-               void (*callback)(void*, int, struct json_object*, struct afb_req_x1),
-               void *closure)
+               struct json_object **result)
 {
-       afb_hook_xreq_subcall(caller, api, verb, args, LEGACY_SUBCALLFLAGS);
-       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v2, mode_hooked_legacy_async);
+       return do_legacy_sync(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, result, mode_legacy_sync);
 }
 
-void afb_calls_legacy_subcall_v3(
+#if WITH_AFB_HOOK
+void afb_calls_legacy_hooked_subcall_v1(
                struct afb_xreq *caller,
                const char *api,
                const char *verb,
                struct json_object *args,
-               void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *),
+               void (*callback)(void*, int, struct json_object*),
                void *closure)
 {
-       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v3, mode_legacy_async);
+       afb_hook_xreq_subcall(caller, api, verb, args, LEGACY_SUBCALLFLAGS);
+       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v1, mode_hooked_legacy_async);
 }
 
-void afb_calls_legacy_hooked_subcall_v3(
+void afb_calls_legacy_hooked_subcall_v2(
                struct afb_xreq *caller,
                const char *api,
                const char *verb,
                struct json_object *args,
-               void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *),
+               void (*callback)(void*, int, struct json_object*, struct afb_req_x1),
                void *closure)
 {
        afb_hook_xreq_subcall(caller, api, verb, args, LEGACY_SUBCALLFLAGS);
-       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v3, mode_hooked_legacy_async);
+       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v2, mode_hooked_legacy_async);
 }
 
-int afb_calls_legacy_subcall_sync(
+void afb_calls_legacy_hooked_subcall_v3(
                struct afb_xreq *caller,
                const char *api,
                const char *verb,
                struct json_object *args,
-               struct json_object **result)
+               void (*callback)(void*, int, struct json_object*, struct afb_req_x2 *),
+               void *closure)
 {
-       return do_legacy_sync(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, result, mode_legacy_sync);
+       afb_hook_xreq_subcall(caller, api, verb, args, LEGACY_SUBCALLFLAGS);
+       do_legacy_async(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, callback, closure, final_legacy_subcall_v3, mode_hooked_legacy_async);
 }
 
 int afb_calls_legacy_hooked_subcall_sync(
@@ -816,3 +830,5 @@ int afb_calls_legacy_hooked_subcall_sync(
        afb_hook_xreq_subcallsync(caller, api, verb, args, LEGACY_SUBCALLFLAGS);
        return do_legacy_sync(NULL, caller, api, verb, args, LEGACY_SUBCALLFLAGS, result, mode_hooked_legacy_sync);
 }
+#endif
+