Update copyright dates
[src/app-framework-binder.git] / src / afb-calls.c
index 73b89bf..a859cd3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
+ * Copyright (C) 2015-2020 "IoT.bzh"
  * Author: José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
 #include <afb/afb-binding.h>
 
 #include "afb-calls.h"
-#include "afb-cred.h"
 #include "afb-evt.h"
 #include "afb-export.h"
 #include "afb-hook.h"
 #include "afb-msg-json.h"
 #include "afb-session.h"
 #include "afb-xreq.h"
+#include "afb-error-text.h"
 
 #include "jobs.h"
 #include "verbose.h"
@@ -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;
@@ -153,7 +156,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, afb_error_text_internal_error, NULL);
        }
 }
 
@@ -165,7 +168,6 @@ static void callreq_destroy_cb(struct afb_xreq *xreq)
 
        afb_context_disconnect(&callreq->xreq.context);
        json_object_put(callreq->xreq.json);
-       afb_cred_unref(callreq->xreq.cred);
        free(callreq);
 }
 
@@ -173,6 +175,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 +190,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) {
@@ -276,24 +280,25 @@ static struct callreq *callreq_create(
                errno = ENOMEM;
        } else {
                afb_xreq_init(&callreq->xreq, &afb_calls_xreq_itf);
-               callreq->xreq.context.validated = 1;
                api2 = (char*)&callreq[1];
                callreq->xreq.request.called_api = memcpy(api2, api, lenapi);;
                verb2 = &api2[lenapi];
                callreq->xreq.request.called_verb = memcpy(verb2, verb, lenverb);
                callreq->xreq.json = args;
                callreq->mode = mode;
-               if (caller) {
-                       export = afb_export_from_api_x3(caller->request.api);
+               if (!caller)
+                       afb_export_context_init(export, &callreq->xreq.context);
+               else {
+                       if (flags & afb_req_x2_subcall_api_session)
+                               afb_export_context_init(export, &callreq->xreq.context);
+                       else
+                               afb_context_subinit(&callreq->xreq.context, &caller->context);
                        if (flags & afb_req_x2_subcall_on_behalf)
-                               callreq->xreq.cred = afb_cred_addref(caller->cred);
+                               afb_context_on_behalf_other_context(&callreq->xreq.context, &caller->context);
                        callreq->xreq.caller = caller;
                        afb_xreq_unhooked_addref(caller);
+                       export = afb_export_from_api_x3(caller->request.api);
                }
-               if (caller && (flags & afb_req_x2_subcall_api_session))
-                       afb_context_subinit(&callreq->xreq.context, &caller->context);
-               else
-                       afb_export_context_init(export, &callreq->xreq.context);
                callreq->export = export;
                callreq->flags = flags;
        }
@@ -341,7 +346,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, afb_error_text_internal_error, NULL, object, error, info);
 }
 
 /******************************************************************************/
@@ -363,7 +368,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, afb_error_text_internal_error, NULL, (union callback){ .any = callback }, export, caller);
        else {
                callreq->callback.any = callback;
                callreq->closure = closure;
@@ -414,18 +419,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 +431,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 +443,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 +508,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 +555,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, afb_error_text_internal_error, NULL, NULL);
        return -1;
 }
 
@@ -572,7 +579,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, afb_error_text_internal_error, NULL, NULL);
                final(closure, -1, ie, (union callback){ .any = callback }, export, caller);
                json_object_put(ie);
        } else {
@@ -623,49 +630,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 +694,7 @@ int afb_calls_legacy_hooked_call_sync(
                json_object_put(object);
        return rc;
 }
+#endif
 
 /******************************************************************************/
 
@@ -738,72 +747,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 +826,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
+