+static int g_feature_enabled = -1;
+
+static int g_privilege_allowed = -1;
+static cynara *p_cynara = NULL;
+
+static bool g_err_callback_status = false;
+
+const char* stt_tag()
+{
+ return "sttc";
+}
+
+static int __stt_get_feature_enabled()
+{
+ if (0 == g_feature_enabled) {
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] STT NOT supported");
+ return STT_ERROR_NOT_SUPPORTED;
+ } else if (-1 == g_feature_enabled) {
+ bool stt_supported = false;
+ bool mic_supported = false;
+ if (0 == system_info_get_platform_bool(STT_FEATURE_PATH, &stt_supported)) {
+ if (0 == system_info_get_platform_bool(STT_MIC_FEATURE_PATH, &mic_supported)) {
+ if (false == stt_supported || false == mic_supported) {
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] STT NOT supported");
+ g_feature_enabled = 0;
+ return STT_ERROR_NOT_SUPPORTED;
+ }
+
+ g_feature_enabled = 1;
+ } else {
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get feature value");
+ return STT_ERROR_NOT_SUPPORTED;
+ }
+ } else {
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get feature value");
+ return STT_ERROR_NOT_SUPPORTED;
+ }
+ }
+
+ return 0;
+}
+
+static int __check_privilege_initialize()
+{
+ int ret = cynara_initialize(&p_cynara, NULL);
+ if (CYNARA_API_SUCCESS != ret)
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] fail to initialize");
+
+ return ret == CYNARA_API_SUCCESS;
+}
+
+static int __check_privilege(const char* uid, const char * privilege)
+{
+ FILE *fp = NULL;
+ char label_path[1024] = "/proc/self/attr/current";
+ char smack_label[1024] = {'\0',};
+
+ if (!p_cynara) {
+ 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");
+
+ fclose(fp);
+ }
+
+ pid_t pid = getpid();
+ char *session = cynara_session_from_pid(pid);
+ int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
+ SLOG(LOG_DEBUG, TAG_STTC, "[Client]cynara_check returned %d(%s)", ret, (CYNARA_API_ACCESS_ALLOWED == ret) ? "Allowed" : "Denied");
+ if (session) {
+ free(session);
+ session = NULL;
+ }
+
+ if (ret != CYNARA_API_ACCESS_ALLOWED)
+ return false;
+ return true;
+}
+
+static void __check_privilege_deinitialize()
+{
+ if (p_cynara)
+ cynara_finish(p_cynara);
+ p_cynara = NULL;
+}
+
+static int __stt_check_privilege()
+{
+ char uid[16];
+
+ if (0 == g_privilege_allowed) {
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied");
+ return STT_ERROR_PERMISSION_DENIED;
+ } else if (-1 == g_privilege_allowed) {
+ if (false == __check_privilege_initialize()) {
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] privilege initialize is failed");
+ return STT_ERROR_PERMISSION_DENIED;
+ }
+ snprintf(uid, 16, "%d", getuid());
+ if (false == __check_privilege(uid, STT_PRIVILEGE)) {
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Permission is denied");
+ g_privilege_allowed = 0;
+ __check_privilege_deinitialize();
+ return STT_ERROR_PERMISSION_DENIED;
+ }
+ __check_privilege_deinitialize();
+ }
+
+ g_privilege_allowed = 1;
+ return STT_ERROR_NONE;
+}
+
+static const char* __stt_get_error_code(stt_error_e err)
+{
+ switch (err) {
+ case STT_ERROR_NONE: return "STT_ERROR_NONE";
+ case STT_ERROR_OUT_OF_MEMORY: return "STT_ERROR_OUT_OF_MEMORY";
+ case STT_ERROR_IO_ERROR: return "STT_ERROR_IO_ERROR";
+ case STT_ERROR_INVALID_PARAMETER: return "STT_ERROR_INVALID_PARAMETER";
+ case STT_ERROR_TIMED_OUT: return "STT_ERROR_TIMED_OUT";
+ case STT_ERROR_RECORDER_BUSY: return "STT_ERROR_RECORDER_BUSY";
+ case STT_ERROR_OUT_OF_NETWORK: return "STT_ERROR_OUT_OF_NETWORK";
+ case STT_ERROR_PERMISSION_DENIED: return "STT_ERROR_PERMISSION_DENIED";
+ case STT_ERROR_NOT_SUPPORTED: return "STT_ERROR_NOT_SUPPORTED";
+ case STT_ERROR_INVALID_STATE: return "STT_ERROR_INVALID_STATE";
+ case STT_ERROR_INVALID_LANGUAGE: return "STT_ERROR_INVALID_LANGUAGE";
+ case STT_ERROR_ENGINE_NOT_FOUND: return "STT_ERROR_ENGINE_NOT_FOUND";
+ case STT_ERROR_OPERATION_FAILED: return "STT_ERROR_OPERATION_FAILED";
+ case STT_ERROR_NOT_SUPPORTED_FEATURE: return "STT_ERROR_NOT_SUPPORTED_FEATURE";
+ case STT_ERROR_SERVICE_RESET: return "STT_ERROR_SERVICE_RESET";
+ default:
+ return "Invalid error code";
+ }
+}
+
+static int __stt_convert_config_error_code(stt_config_error_e code)
+{
+ if (code == STT_CONFIG_ERROR_NONE) return STT_ERROR_NONE;
+ if (code == STT_CONFIG_ERROR_OUT_OF_MEMORY) return STT_ERROR_OUT_OF_MEMORY;
+ if (code == STT_CONFIG_ERROR_IO_ERROR) return STT_ERROR_IO_ERROR;
+ if (code == STT_CONFIG_ERROR_INVALID_PARAMETER) return STT_ERROR_INVALID_PARAMETER;
+ if (code == STT_CONFIG_ERROR_PERMISSION_DENIED) return STT_ERROR_PERMISSION_DENIED;
+ if (code == STT_CONFIG_ERROR_NOT_SUPPORTED) return STT_ERROR_NOT_SUPPORTED;
+ if (code == STT_CONFIG_ERROR_INVALID_STATE) return STT_ERROR_INVALID_STATE;
+ if (code == STT_CONFIG_ERROR_INVALID_LANGUAGE) return STT_ERROR_INVALID_LANGUAGE;
+ if (code == STT_CONFIG_ERROR_ENGINE_NOT_FOUND) return STT_ERROR_ENGINE_NOT_FOUND;
+ if (code == STT_CONFIG_ERROR_OPERATION_FAILED) return STT_ERROR_OPERATION_FAILED;
+
+ return code;
+}
+
+void __stt_config_lang_changed_cb(const char* before_language, const char* current_language, void* user_data)
+{
+ SLOG(LOG_DEBUG, TAG_STTC, "Language changed : Before lang(%s) Current lang(%s)",
+ before_language, current_language);
+
+ if (0 == strcmp(before_language, current_language)) {
+ return;
+ }
+
+ GList* client_list = NULL;
+ client_list = stt_client_get_client_list();
+
+ GList *iter = NULL;
+ stt_client_s *data = NULL;
+
+ if (g_list_length(client_list) > 0) {
+ /* Get a first item */
+ iter = g_list_first(client_list);
+
+ while (NULL != iter) {
+ data = iter->data;
+ if (NULL != data->default_lang_changed_cb) {
+ SLOG(LOG_DEBUG, TAG_STTC, "Call default language changed callback : uid(%d)", data->uid);
+ data->default_lang_changed_cb(data->stt, before_language, current_language,
+ data->default_lang_changed_user_data);
+ }
+
+ /* Next item */
+ iter = g_list_next(iter);
+ }
+ }
+
+ return;
+}
+
+void __stt_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, bool support_silence, bool need_credential, void* user_data)
+{
+ stt_h stt = (stt_h)user_data;
+
+ stt_client_s* client = stt_client_get(stt);
+ if (NULL == client) {
+ SLOG(LOG_ERROR, TAG_STTC, "[WARNING] A handle is not valid");
+ return;
+ }
+
+ if (NULL != engine_id) SLOG(LOG_DEBUG, TAG_STTC, "Engine id(%s)", engine_id);
+ if (NULL != setting) SLOG(LOG_DEBUG, TAG_STTC, "Engine setting(%s)", setting);
+ if (NULL != language) SLOG(LOG_DEBUG, TAG_STTC, "Language(%s)", language);
+ SLOG(LOG_DEBUG, TAG_STTC, "Silence(%s), Credential(%s)", support_silence ? "on" : "off", need_credential ? "need" : "no need");
+
+ /* call callback function */
+ if (NULL != client->engine_changed_cb) {
+ client->engine_changed_cb(stt, engine_id, language, support_silence, need_credential, client->engine_changed_user_data);
+ } else {
+ SLOG(LOG_WARN, TAG_STTC, "No registered callback function for engine change");
+ }
+ return;
+}
+
+static int __stt_check_handle(stt_h stt, stt_client_s** client)
+{
+ if (NULL == stt) {
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input handle is null");
+ return STT_ERROR_INVALID_PARAMETER;
+ }
+
+ stt_client_s* temp = NULL;
+ temp = stt_client_get(stt);
+
+ /* check handle */
+ if (NULL == temp) {
+ SLOG(LOG_ERROR, TAG_STTC, "[ERROR] A handle is not available");
+ return STT_ERROR_INVALID_PARAMETER;
+ }
+ *client = temp;
+
+ return STT_ERROR_NONE;
+}