Invoke register callback once during tts_prepare() 83/265883/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 1 Nov 2021 08:24:55 +0000 (17:24 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Tue, 2 Nov 2021 06:12:51 +0000 (15:12 +0900)
Current code tries to create new notify callback handle every callback register. It is for
matching business logic for dbus. However, server can send message through expired callback handle
by this logic.

This patch makes code create and register callback handle once during tts_prepare().
By this change, server always sends message throught valid callback handle.
However, if registering callback is failed, this logic can not handle the situation.

If the situation occurs, handling code should be required.

Change-Id: I5e88be20ffc84c0ace99e4559440219c21d36089
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
client/tts_tidl.c

index 8a0445d..0c99d38 100644 (file)
@@ -25,6 +25,7 @@ typedef struct {
        int uid;
        bool connected;
        bool connection_requesting;
+       bool register_callback_invoked;
        rpc_port_proxy_tts_h rpc_h;
        rpc_port_proxy_tts_notify_cb_h notify_cb_h;
        char* engine_app_id;
@@ -100,6 +101,13 @@ static void __notify_cb(void *user_data, int pid, int uid, bundle *msg)
                if (NULL != credential_needed && NULL != ret) {
                        tts_client_set_start_listening(uid, true);
                        tts_core_receive_hello(uid, atoi(ret), atoi(credential_needed));
+
+                       tts_tidl_info_s* info = __get_tidl_info_s(uid);
+                       if (NULL == info) {
+                               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get tidl info");
+                               return;
+                       }
+                       info->register_callback_invoked = false;
                } else {
                        SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get message(TTSD_METHOD_HELLO). pid(%d) uid(%d)", pid, uid);
                }
@@ -152,6 +160,7 @@ static void __on_connected(rpc_port_proxy_tts_h h, void *user_data)
 
        info->connected = true;
        info->connection_requesting = false;
+       info->register_callback_invoked = false;
        if (0 != rpc_port_proxy_tts_invoke_set_mode(info->rpc_h, tts_client_get_mode(client))) {
                SLOG(LOG_ERROR, TAG_TTSC, "Failed to set mode");
                return;
@@ -178,6 +187,7 @@ static void __on_disconnected(rpc_port_proxy_tts_h h, void *user_data)
 
        info->connected = false;
        info->connection_requesting = false;
+       info->register_callback_invoked = false;
        if (tts_client_is_listening_started(uid)) {
                tts_core_handle_service_reset();
                SLOG(LOG_DEBUG, TAG_TTSC, "Disconnected from server");
@@ -202,6 +212,7 @@ static void __on_rejected(rpc_port_proxy_tts_h h, void *user_data)
                return;
        }
        info->connection_requesting = false;
+       info->register_callback_invoked = false;
 
        SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Rejected from server(%d)", uid);
 }
@@ -332,7 +343,8 @@ int tts_tidl_stop_listening(int uid)
        return TTS_ERROR_NONE;
 }
 
-static void __request_tidl_connect(tts_tidl_info_s* info) {
+static void __request_tidl_connect(tts_tidl_info_s* info)
+{
        if (info->connection_requesting) {
                return;
        }
@@ -343,6 +355,41 @@ static void __request_tidl_connect(tts_tidl_info_s* info) {
        info->connection_requesting = true;
 }
 
+static int __create_notify_callback_handle(tts_tidl_info_s* info)
+{
+       if (NULL != info->notify_cb_h) {
+               rpc_port_proxy_tts_notify_cb_dispose(info->rpc_h, info->notify_cb_h);
+               info->notify_cb_h = NULL;
+       }
+
+       if (RPC_PORT_ERROR_NONE != rpc_port_proxy_tts_notify_cb_create(&info->notify_cb_h)) {
+               return TTS_ERROR_OUT_OF_MEMORY;
+       }
+
+       rpc_port_proxy_tts_notify_cb_set_callback(info->notify_cb_h, __notify_cb, NULL);
+       rpc_port_proxy_tts_notify_cb_set_once(info->notify_cb_h, false);
+
+       return TTS_ERROR_NONE;
+}
+
+static int __invoke_register_callback(int pid, tts_tidl_info_s* info)
+{
+       if (info->register_callback_invoked) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[INFO] Already register callback is invoked");
+               return TTS_ERROR_NONE;
+       }
+
+       int ret = __create_notify_callback_handle(info);
+       if (TTS_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create callback handle. ret(%d)", ret);
+               return TTS_ERROR_OPERATION_FAILED;
+       }
+
+       rpc_port_proxy_tts_invoke_register_cb(info->rpc_h, pid, info->uid, info->notify_cb_h);
+       info->register_callback_invoked = true;
+       return TTS_ERROR_NONE;
+}
+
 int tts_tidl_request_hello(int uid)
 {
        SLOG(LOG_DEBUG, TAG_TTSC, "[TIDL] tts_tidl_request_hello");
@@ -369,6 +416,8 @@ int tts_tidl_request_hello(int uid)
                        SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to destroy old rpc_port");
                        return TTS_ERROR_OPERATION_FAILED;
                }
+               info->notify_cb_h = NULL;
+               info->register_callback_invoked = false;
 
                info->rpc_h = __create_rpc_port(uid, engine_app_id);
                if (NULL == info->rpc_h) {
@@ -387,22 +436,17 @@ int tts_tidl_request_hello(int uid)
        }
 
        SLOG(LOG_DEBUG, TAG_TTSC, ">>>>> TTS Hello");
-       /* register callback - needed to create new client before adding callback */
-       if (NULL != info->notify_cb_h) {
-               rpc_port_proxy_tts_notify_cb_dispose(info->rpc_h, info->notify_cb_h);
-               info->notify_cb_h = NULL;
+       if (TTS_ERROR_NONE != __invoke_register_callback(client->pid, info)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to invoke register callback");
+               return TTS_ERROR_OPERATION_FAILED;
        }
 
-       rpc_port_proxy_tts_notify_cb_create(&info->notify_cb_h);
-       rpc_port_proxy_tts_notify_cb_set_callback(info->notify_cb_h, __notify_cb, NULL);
-       rpc_port_proxy_tts_notify_cb_set_once(info->notify_cb_h, false);
-       rpc_port_proxy_tts_invoke_register_cb(info->rpc_h, client->pid, uid, info->notify_cb_h);
-
        SLOG(LOG_DEBUG, TAG_TTSC, "<<<<");
        return TTS_ERROR_NONE;
 }
 
-static int __request_tidl_connect_sync(tts_tidl_info_s* info) {
+static int __request_tidl_connect_sync(tts_tidl_info_s* info)
+{
        int ret = rpc_port_proxy_tts_connect_sync(info->rpc_h);
        SLOG(LOG_INFO, TAG_TTSC, "[INFO] Request connection to stub. ret(%d)", ret);
        if (RPC_PORT_ERROR_NONE == ret) {
@@ -412,6 +456,25 @@ static int __request_tidl_connect_sync(tts_tidl_info_s* info) {
        return TTS_ERROR_OPERATION_FAILED;
 }
 
+static int __invoke_register_callback_sync(int pid, tts_tidl_info_s* info)
+{
+       if (info->register_callback_invoked) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[INFO] Already register callback is invoked");
+               return TTS_ERROR_NONE;
+       }
+
+       int ret = RPC_PORT_ERROR_NONE;
+       ret = __create_notify_callback_handle(info);
+       if (RPC_PORT_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create callback handle. ret(%d)", ret);
+               return TTS_ERROR_OPERATION_FAILED;
+       }
+
+       rpc_port_proxy_tts_invoke_register_cb_sync(info->rpc_h, pid, info->uid, info->notify_cb_h);
+       info->register_callback_invoked = true;
+       return TTS_ERROR_NONE;
+}
+
 int tts_tidl_request_hello_sync(int uid)
 {
        SLOG(LOG_DEBUG, TAG_TTSC, "[TIDL] tts_tidl_request_hello");
@@ -440,17 +503,8 @@ int tts_tidl_request_hello_sync(int uid)
        }
 
        SLOG(LOG_DEBUG, TAG_TTSC, ">>>>> TTS Hello");
-       /* register callback - needed to create new client before adding callback */
-       if (NULL != info->notify_cb_h) {
-               rpc_port_proxy_tts_notify_cb_dispose(info->rpc_h, info->notify_cb_h);
-               info->notify_cb_h = NULL;
-       }
-
-       rpc_port_proxy_tts_notify_cb_create(&info->notify_cb_h);
-       rpc_port_proxy_tts_notify_cb_set_callback(info->notify_cb_h, __notify_cb, NULL);
-       rpc_port_proxy_tts_notify_cb_set_once(info->notify_cb_h, false);
-       if (0 != rpc_port_proxy_tts_invoke_register_cb_sync(info->rpc_h, client->pid, uid, info->notify_cb_h)) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Failed to invoke register");
+       if (TTS_ERROR_NONE != __invoke_register_callback_sync(client->pid, info)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to invoke register callback");
                return TTS_ERROR_OPERATION_FAILED;
        }