2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
15 #include <Ecore_File.h>
16 #include <runtime_info.h>
17 #include <sys/inotify.h>
18 #include "ttsd_main.h"
19 #include "ttsd_config.h"
20 #include "ttsd_engine_agent.h"
21 #include "ttsd_data.h"
23 #define NOTI_ERROR_FILE_NAME CONFIG_DIRECTORY"/ttsd_noti.err"
25 #define ENGINE_ID "ENGINE_ID"
29 #define BUFFER_SIZE 20
31 static char* g_engine_id;
32 static char* g_language;
36 static Ecore_Fd_Handler* g_fd_handler_noti = NULL;
40 static ttsd_config_changed_cb g_callback;
43 int __ttsd_config_compare()
46 char buf_id[256] = {0};
47 char buf_param[256] = {0};
50 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "r");
52 if (NULL == config_fp) {
53 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Not open file(%s)", DEFAULT_CONFIG_FILE_NAME);
59 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
61 SLOG(LOG_ERROR, get_tag(), "[Config WARNING] Fail to read config (engine id)");
64 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
65 if (NULL != g_engine_id) {
66 if (0 != strcmp(g_engine_id, buf_param)) {
68 g_engine_id = strdup(buf_param);
70 SLOG(LOG_DEBUG, get_tag(), "[Config] engine id is changed : %s", g_engine_id);
72 if (NULL != g_callback)
73 g_callback(TTS_CONFIG_TYPE_ENGINE, g_engine_id, 0);
76 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is not available");
77 g_engine_id = strdup(buf_param);
78 if (NULL != g_callback)
79 g_callback(TTS_CONFIG_TYPE_ENGINE, g_engine_id, 0);
83 SLOG(LOG_ERROR, get_tag(), "[Config] Fail to load config (engine id)");
89 if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
91 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
94 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
95 if (NULL != g_language) {
96 if ((0 != strcmp(g_language, buf_param)) || (g_vc_type != int_param)) {
98 g_language = strdup(buf_param);
99 g_vc_type = int_param;
101 SLOG(LOG_DEBUG, get_tag(), "[Config] voice is changed : lang(%s) type(%d)", g_language, g_vc_type);
103 if (NULL != g_callback)
104 g_callback(TTS_CONFIG_TYPE_VOICE, g_language, g_vc_type);
107 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current voice is not available");
108 g_language = strdup(buf_param);
109 g_vc_type = int_param;
111 if (NULL != g_callback)
112 g_callback(TTS_CONFIG_TYPE_VOICE, g_language, g_vc_type);
116 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
122 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
124 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
127 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
128 if (g_speed != int_param) {
131 SLOG(LOG_DEBUG, get_tag(), "[Config] speech rate is changed : %d", g_speed);
133 if (NULL != g_callback)
134 g_callback(TTS_CONFIG_TYPE_SPEED, NULL, g_speed);
138 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
148 int __ttsd_config_save()
150 if (0 != access(DEFAULT_CONFIG_FILE_NAME, R_OK|W_OK)) {
151 if (0 == ecore_file_mkpath(CONFIG_DIRECTORY)) {
152 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to create directory (%s)", CONFIG_DIRECTORY);
154 SLOG(LOG_DEBUG, get_tag(), "[Config] Create directory (%s)", CONFIG_DIRECTORY);
159 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "w+");
161 if (NULL == config_fp) {
162 /* make file and file default */
163 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to open config (%s)", DEFAULT_CONFIG_FILE_NAME);
167 SLOG(LOG_DEBUG, get_tag(), "[Config] Rewrite config file");
169 /* Write engine id */
170 fprintf(config_fp, "%s %s\n", ENGINE_ID, g_engine_id);
173 fprintf(config_fp, "%s %s %d\n", VOICE, g_language, g_vc_type);
176 fprintf(config_fp, "%s %d\n", SPEED, g_speed);
183 int __ttsd_config_load()
186 char buf_id[256] = {0};
187 char buf_param[256] = {0};
189 bool is_default_open = false;
191 config_fp = fopen(DEFAULT_CONFIG_FILE_NAME, "r");
193 if (NULL == config_fp) {
194 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Not open file(%s)", DEFAULT_CONFIG_FILE_NAME);
196 config_fp = fopen(CONFIG_DEFAULT, "r");
197 if (NULL == config_fp) {
198 SLOG(LOG_ERROR, get_tag(), "[Config WARNING] Not open original config file(%s)", CONFIG_DEFAULT);
202 is_default_open = true;
206 if (EOF == fscanf(config_fp, "%s %s", buf_id, buf_param)) {
208 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (engine id)");
211 if (0 == strncmp(ENGINE_ID, buf_id, strlen(ENGINE_ID))) {
212 g_engine_id = strdup(buf_param);
215 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (engine id)");
221 if (EOF == fscanf(config_fp, "%s %s %d", buf_id, buf_param, &int_param)) {
223 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (voice)");
224 __ttsd_config_save();
227 if (0 == strncmp(VOICE, buf_id, strlen(VOICE))) {
228 g_language = strdup(buf_param);
229 g_vc_type = int_param;
232 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (voice)");
238 if (EOF == fscanf(config_fp, "%s %d", buf_id, &int_param)) {
240 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to read config (speed)");
243 if (0 == strncmp(SPEED, buf_id, strlen(SPEED))) {
247 SLOG(LOG_WARN, get_tag(), "[Config WARNING] Fail to load config (speed)");
254 SLOG(LOG_DEBUG, get_tag(), "[Config] Load config : engine(%s), voice(%s,%d), speed(%d)",
255 g_engine_id, g_language, g_vc_type, g_speed);
257 if (true == is_default_open) {
258 if(0 == __ttsd_config_save()) {
259 SLOG(LOG_DEBUG, get_tag(), "[Config] Create config(%s)", DEFAULT_CONFIG_FILE_NAME);
266 static Eina_Bool inotify_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
268 SLOG(LOG_DEBUG, get_tag(), "===== [Config] Inotify event call");
272 char buffer[sizeof(struct inotify_event) * BUFFER_SIZE];
273 memset(buffer, 0, (sizeof(struct inotify_event) * BUFFER_SIZE));
275 length = read(g_fd_noti, buffer, (sizeof(struct inotify_event) * BUFFER_SIZE));
277 SLOG(LOG_ERROR, get_tag(), "[Config] Empty Inotify event");
278 SLOG(LOG_DEBUG, get_tag(), "=====");
279 SLOG(LOG_DEBUG, get_tag(), " ");
280 return ECORE_CALLBACK_RENEW;
291 struct inotify_event *event = (struct inotify_event *)&buffer[i];
292 i = i + sizeof(struct inotify_event) + event->len;
294 if (IN_CLOSE_WRITE == event->mask) {
295 __ttsd_config_compare();
297 SLOG(LOG_ERROR, get_tag(), "[Config] Undefined event");
301 SLOG(LOG_DEBUG, get_tag(), "=====");
302 SLOG(LOG_DEBUG, get_tag(), " ");
304 return ECORE_CALLBACK_PASS_ON;
307 int __config_file_open_connection()
309 /* get file notification handler */
315 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail get inotify_fd");
320 wd = inotify_add_watch(fd, DEFAULT_CONFIG_FILE_NAME, IN_CLOSE_WRITE);
322 g_fd_handler_noti = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)inotify_event_callback, NULL, NULL, NULL);
323 if (NULL == g_fd_handler_noti) {
324 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Fail to get handler_noti");
331 int ttsd_config_initialize(ttsd_config_changed_cb callback)
338 g_callback = callback;
340 ecore_file_mkpath(CONFIG_DIRECTORY);
342 __ttsd_config_load();
344 if (0 != __config_file_open_connection()) {
351 int ttsd_config_finalize()
353 if (NULL != g_language) {
357 if (NULL != g_engine_id) {
361 /* del inotify variable */
362 ecore_main_fd_handler_del(g_fd_handler_noti);
363 inotify_rm_watch(g_fd_noti, g_wd_noti);
369 int ttsd_config_update_language()
371 /* no work in notification mode */
375 int ttsd_config_get_default_engine(char** engine_id)
377 if (NULL == engine_id)
380 if (NULL != g_engine_id) {
381 *engine_id = strdup(g_engine_id);
383 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current engine id is NULL");
390 int ttsd_config_set_default_engine(const char* engine_id)
392 /* Not available in notification mode */
396 int ttsd_config_get_default_voice(char** language, int* type)
398 if (NULL == language || NULL == type)
401 if (NULL != g_language) {
402 *language = strdup(g_language);
405 SLOG(LOG_ERROR, get_tag(), "[Config ERROR] Current language is NULL");
412 int ttsd_config_set_default_voice(const char* language, int type)
414 /* Not available in notification mode */
418 int ttsd_config_get_default_speed(int* speed)
428 int ttsd_config_set_default_speed(int speed)
430 /* Not available in notification mode */
434 int ttsd_config_save_error(int uid, int uttid, const char* lang, int vctype, const char* text,
435 const char* func, int line, const char* message)
438 err_fp = fopen(NOTI_ERROR_FILE_NAME, "a");
439 if (NULL == err_fp) {
440 SLOG(LOG_WARN, get_tag(), "[WARNING] Fail to open error file (%s)", NOTI_ERROR_FILE_NAME);
443 SLOG(LOG_DEBUG, get_tag(), "Save Error File (%s)", NOTI_ERROR_FILE_NAME);
447 fprintf(err_fp, "function - %s\n", func);
451 fprintf(err_fp, "line - %d\n", line);
454 if (NULL != message) {
455 fprintf(err_fp, "message - %s\n", message);
460 fprintf(err_fp, "uid - %d\n", uid);
463 fprintf(err_fp, "uttid - %d\n", uttid);
467 fprintf(err_fp, "language - %s\n", lang);
471 fprintf(err_fp, "vctype - %d\n", vctype);
475 fprintf(err_fp, "text - %s\n", text);
478 /* get current engine */
479 char *engine_id = NULL;
481 ret = ttsd_engine_setting_get_engine(&engine_id);
483 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get current engine");
485 fprintf(err_fp, "current engine - %s", engine_id);
489 ttsd_data_save_error_log(uid, err_fp);