Extract function from open_connection 03/260003/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Thu, 6 May 2021 07:26:45 +0000 (16:26 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Tue, 22 Jun 2021 08:11:29 +0000 (17:11 +0900)
This patch extracts the unit functions from open_connection to enhance readability

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

index fbfb3a20b2198d9de8a7b0ec655760833249e7d2..bc3dc9e6c4483c849a6ffc7067959ac60120351b 100644 (file)
@@ -235,12 +235,38 @@ static void __on_rejected(rpc_port_proxy_tts_h h, void *user_data)
        SLOG(LOG_DEBUG, TAG_TTSC, "Rejected from server");
 }
 
-int tts_tidl_open_connection(int uid)
+static void __get_engine_app_id(int mode, int size, char* app_id)
 {
-       SLOG(LOG_DEBUG, TAG_TTSC, "[TIDL] tts_tidl_open_connection");
+       if (NULL == app_id) {
+               SLOG(LOG_ERROR, TAG_TTSC, "app_id is NULL");
+               return;
+       }
+
+       char* engine_name = vconf_get_str(TTS_ENGINE_DB_DEFAULT);
+       if (NULL == engine_name) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get engine name");
+               engine_name = strdup(TTS_SERVER_ENGINE_DEFAULT);
+       }
 
-       char engine_mode[256] = {0, };
-       char* engine_name = NULL;
+       char* mode_str = "";
+       if (TTS_MODE_NOTIFICATION == mode) {
+               mode_str = TTS_NOTI_SERVER_MODE;
+       } else if (TTS_MODE_SCREEN_READER == mode) {
+               mode_str = TTS_SR_SERVER_MODE;
+       } else if (TTS_MODE_INTERRUPT == mode) {
+               mode_str = TTS_INTERRUPT_SERVER_MODE;
+       }
+
+       snprintf(app_id, size, "%s%s", engine_name, mode_str);
+       free(engine_name);
+
+       SLOG(LOG_INFO, TAG_TTSC, "engine app id : %s", app_id);
+}
+
+static rpc_port_proxy_tts_h __open_rpc_port(int uid, int mode)
+{
+       char engine_app_id[256] = {0, };
+       __get_engine_app_id(mode, 256, engine_app_id);
 
        rpc_port_proxy_tts_callback_s rpc_callback = {
                .connected = __on_connected,
@@ -248,6 +274,26 @@ int tts_tidl_open_connection(int uid)
                .rejected = __on_rejected
        };
 
+       rpc_port_proxy_tts_h handle = NULL;
+       intptr_t ptr_uid = uid;
+       if (0 != rpc_port_proxy_tts_create(engine_app_id, &rpc_callback, (void*)ptr_uid, &handle)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create proxy");
+               return NULL;
+       }
+
+       if (0 != rpc_port_proxy_tts_connect(handle)) {
+               rpc_port_proxy_tts_destroy(handle);
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to connect the service");
+               return NULL;
+       }
+
+       return handle;
+}
+
+int tts_tidl_open_connection(int uid)
+{
+       SLOG(LOG_DEBUG, TAG_TTSC, "[TIDL] tts_tidl_open_connection");
+
        tts_client_s* client = tts_client_get_by_uid(uid);
        if (NULL == client) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get client");
@@ -260,49 +306,17 @@ int tts_tidl_open_connection(int uid)
                return TTS_ERROR_OUT_OF_MEMORY;
        }
 
-       engine_name = vconf_get_str(TTS_ENGINE_DB_DEFAULT);
-       if (NULL == engine_name) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get engine name");
-               engine_name = strdup(TTS_SERVER_ENGINE_DEFAULT);
-       }
-
-       if (TTS_ERROR_SERVICE_RESET != client->reason) {
-               tts_mode_e mode = tts_client_get_mode(client);
-               if (TTS_MODE_DEFAULT == mode) {
-                       strcat(engine_mode, engine_name);
-               } else if (TTS_MODE_NOTIFICATION == mode) {
-                       strcat(engine_mode, engine_name);
-                       strcat(engine_mode, TTS_NOTI_SERVER_MODE);
-               } else if (TTS_MODE_SCREEN_READER == mode) {
-                       strcat(engine_mode, engine_name);
-                       strcat(engine_mode, TTS_SR_SERVER_MODE);
-               } else if (TTS_MODE_INTERRUPT == mode) {
-                       strcat(engine_mode, engine_name);
-                       strcat(engine_mode, TTS_INTERRUPT_SERVER_MODE);
-               }
-               client->reason = 0; // default value
-       }
-       free(engine_name);
-
-       intptr_t ptr_uid = uid;
-       if (0 != rpc_port_proxy_tts_create(engine_mode, &rpc_callback, (void*)ptr_uid, &info->rpc_h)) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to create proxy");
+       info->rpc_h = __open_rpc_port(uid, tts_client_get_mode(client));
+       if (NULL == info->rpc_h) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to open proxy");
                free(info);
                return TTS_ERROR_OPERATION_FAILED;
        }
 
-       if (0 != rpc_port_proxy_tts_connect(info->rpc_h)) {
-               rpc_port_proxy_tts_destroy(info->rpc_h);
-               info->rpc_h = NULL;
-               free(info);
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to connect the service");
-               return TTS_ERROR_OPERATION_FAILED;
-       }
-
        info->uid = uid;
        g_tidl_infos = g_list_append(g_tidl_infos, info);
 
-       SLOG(LOG_ERROR, TAG_TTSC, "[INFO] uid(%d) rpc_h(%p) engine(%s)", uid, info->rpc_h, engine_mode);
+       SLOG(LOG_ERROR, TAG_TTSC, "[INFO] uid(%d) rpc_h(%p)", uid, info->rpc_h);
        SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
        return TTS_ERROR_NONE;
 }