Correct to check return of fread
[platform/core/uifw/stt.git] / client / stt.c
index fe24360..8a55d34 100644 (file)
@@ -100,15 +100,15 @@ static bool __check_privilege(const char* uid, const char * privilege)
        char smack_label[1024] = {'\0',};
 
        if (!p_cynara) {
-           return false;
+               return false;
        }
 
        fp = fopen(label_path, "r");
        if (fp != NULL) {
-           if (fread(smack_label, 1, sizeof(smack_label), fp) <= 0)
-               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to fread");
+               if (0 >= fread(smack_label, 1, sizeof(smack_label), fp))
+                       SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to fread");
 
-           fclose(fp);
+               fclose(fp);
        }
 
        pid_t pid = getpid();
@@ -121,7 +121,7 @@ static bool __check_privilege(const char* uid, const char * privilege)
        }
 
        if (ret != CYNARA_API_ACCESS_ALLOWED)
-           return false;
+               return false;
        return true;
 }
 
@@ -916,6 +916,11 @@ static Eina_Bool __stt_connect_daemon(void *data)
                return EINA_FALSE;
        }
 
+       if (0 == stt_client_get_size() || NULL == stt_client_get_by_uid(client->uid)) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Client has been already destroyed");
+               return EINA_FALSE;
+       }
+
        /* Check and Set vconfkey of custom engine before sending hello */
        if (1 == g_privilege_applaunch_allowed && NULL != client->current_engine_id) {
                /* Set vconfkey */
@@ -1037,7 +1042,7 @@ int stt_prepare(stt_h stt)
                return STT_ERROR_INVALID_STATE;
        }
 
-       g_connect_timer = ecore_timer_add(0, __stt_connect_daemon, (void*)client);
+       g_connect_timer = ecore_timer_add(0.02, __stt_connect_daemon, (void*)client);
 
        SLOG(LOG_DEBUG, TAG_STTC, "=====");
        SLOG(LOG_DEBUG, TAG_STTC, " ");
@@ -1612,8 +1617,8 @@ int stt_start(stt_h stt, const char* language, const char* type)
        }
 
        int ret = -1;
-       char appid[128] = {0, };
-       ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
+       char appid[1024] = {0, };
+       ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
 
        if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
                SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
@@ -1628,8 +1633,15 @@ int stt_start(stt_h stt, const char* language, const char* type)
                temp = strdup(language);
        }
 
+       if (NULL == temp) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
+               return STT_ERROR_OUT_OF_MEMORY;
+       }
+
        if (true == client->credential_needed && NULL == client->credential) {
                SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
+               free(temp);
+               temp = NULL;
                return STT_ERROR_PERMISSION_DENIED;
        }
 
@@ -1642,7 +1654,8 @@ int stt_start(stt_h stt, const char* language, const char* type)
                SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
        }
 
-       if (NULL != temp)       free(temp);
+       free(temp);
+       temp = NULL;
 
        SLOG(LOG_DEBUG, TAG_STTC, "=====");
        SLOG(LOG_DEBUG, TAG_STTC, " ");
@@ -1998,9 +2011,9 @@ static void __stt_notify_state_changed(void *data)
                client->state_changed_cb(client->stt, client->before_state,
                        client->current_state, client->state_changed_user_data);
                stt_client_not_use_callback(client);
-               SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called");
+               SLOG(LOG_DEBUG, TAG_STTC, "State changed callback is called, State(%d)", client->current_state);
        } else {
-               SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null");
+               SLOG(LOG_WARN, TAG_STTC, "[WARNING] State changed callback is null, State(%d)", client->current_state);
        }
 
        return;
@@ -2081,12 +2094,12 @@ int __stt_cb_result(int uid, int event, char** data, int data_count, const char*
        }
 
        if (NULL != msg)
-               SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result Message = %s", msg);
+               SECURE_SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result Message = %s", msg);
 
        int i = 0;
        for (i = 0; i < data_count; i++) {
                if (NULL != data[i])
-                       SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
+                       SECURE_SLOG(LOG_DEBUG, TAG_STTC, "Recognition Result[%d] = %s", i, data[i]);
        }
 
        if (NULL != client->recognition_result_cb) {
@@ -2490,3 +2503,128 @@ int stt_unset_speech_status_cb(stt_h stt)
 
        return 0;
 }
+
+int stt_start_file(stt_h stt, const char* language, const char* type, const char* filepath, stt_audio_type_e audio_type, int sample_rate)
+{
+       stt_client_s* client = NULL;
+       if (0 != __stt_get_feature_enabled()) {
+               return STT_ERROR_NOT_SUPPORTED;
+       }
+       if (0 != __stt_check_privilege()) {
+               return STT_ERROR_PERMISSION_DENIED;
+       }
+       if (0 != __stt_check_handle(stt, &client)) {
+               return STT_ERROR_INVALID_PARAMETER;
+       }
+       if (NULL == filepath) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
+               return STT_ERROR_INVALID_PARAMETER;
+       }
+
+       SLOG(LOG_DEBUG, TAG_STTC, "===== STT START FILE");
+
+       /* check state */
+       if (client->current_state != STT_STATE_READY) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
+               return STT_ERROR_INVALID_STATE;
+       }
+
+       if (STT_INTERNAL_STATE_NONE != client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is NOT none : %d", client->internal_state);
+               return STT_ERROR_IN_PROGRESS_TO_RECORDING;
+       }
+
+       int ret = -1;
+       char appid[1024] = {0, };
+       ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
+
+       if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID");
+       } else {
+               SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
+       }
+
+       char* temp = NULL;
+       if (NULL == language) {
+               temp = strdup("default");
+       } else {
+               temp = strdup(language);
+       }
+
+       if (NULL == temp) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
+               return STT_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (true == client->credential_needed && NULL == client->credential) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id);
+               free(temp);
+               temp = NULL;
+               return STT_ERROR_PERMISSION_DENIED;
+       }
+
+       client->internal_state = STT_INTERNAL_STATE_STARTING;
+       ret = stt_dbus_request_start_file(client->uid, temp, type, client->silence, appid, client->credential, filepath, audio_type, sample_rate);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start file : %s", __stt_get_error_code(ret));
+               client->internal_state = STT_INTERNAL_STATE_NONE;
+       } else {
+               SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Start is successful but not done");
+       }
+
+       free(temp);
+       temp = NULL;
+
+       SLOG(LOG_DEBUG, TAG_STTC, "=====");
+       SLOG(LOG_DEBUG, TAG_STTC, " ");
+
+       return ret;
+}
+
+int stt_cancel_file(stt_h stt)
+{
+       stt_client_s* client = NULL;
+       if (0 != __stt_get_feature_enabled()) {
+               return STT_ERROR_NOT_SUPPORTED;
+       }
+       if (0 != __stt_check_privilege()) {
+               return STT_ERROR_PERMISSION_DENIED;
+       }
+       if (0 != __stt_check_handle(stt, &client)) {
+               return STT_ERROR_INVALID_PARAMETER;
+       }
+
+       SLOG(LOG_DEBUG, TAG_STTC, "===== STT CANCEL FILE");
+
+       /* check state */
+       if (STT_STATE_RECORDING != client->current_state && STT_STATE_PROCESSING != client->current_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid state : Current state(%d) is 'Ready'", client->current_state);
+               return STT_ERROR_INVALID_STATE;
+       }
+
+       if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state);
+               return STT_ERROR_IN_PROGRESS_TO_RECORDING;
+       } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state);
+               return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
+       } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state);
+               return STT_ERROR_IN_PROGRESS_TO_READY;
+       }
+
+       client->internal_state = STT_INTERNAL_STATE_CANCELING;
+       int ret = stt_dbus_request_cancel_file(client->uid);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to cancel file : %s", __stt_get_error_code(ret));
+               client->internal_state = STT_INTERNAL_STATE_NONE;
+       } else {
+               SLOG(LOG_DEBUG, TAG_STTC, "[SUCCESS] Cancel file is successful but not done");
+       }
+
+       SLOG(LOG_DEBUG, TAG_STTC, "=====");
+       SLOG(LOG_DEBUG, TAG_STTC, " ");
+
+       return ret;
+}
+