change code for config and change init api and add file for sysinfo check
[platform/core/uifw/tts.git] / server / ttsd_engine_agent.c
1 /*
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.
12 */
13
14
15 #include <dlfcn.h>
16 #include <dirent.h>
17
18 #include "ttsd_main.h"
19 #include "ttsd_engine_agent.h"
20 #include "ttsd_config.h"
21
22 #define ENGINE_PATH_SIZE        256
23
24 /*
25 * Internal data structure
26 */
27 typedef struct {
28         /* base info */
29         char*   engine_uuid;
30         char*   engine_name; 
31         char*   engine_path;
32
33         /* info for using engine load*/
34         bool    is_set;
35         bool    is_loaded;              
36         bool    need_network;
37         void    *handle;
38
39         /* engine base setting */
40         char*   default_lang;
41         int     default_vctype;
42         int     default_speed;
43
44         ttspe_funcs_s*  pefuncs;
45         ttspd_funcs_s*  pdfuncs;
46
47         int (*ttsp_load_engine)(const ttspd_funcs_s* pdfuncs, ttspe_funcs_s* pefuncs);
48         int (*ttsp_unload_engine)();
49 } ttsengine_s;
50
51 typedef struct {
52         char*   engine_uuid;
53         char*   engine_path;
54         char*   engine_name;
55         char*   setting_ug_path;
56         bool    use_network;
57 } ttsengine_info_s;
58
59
60 /** Init flag */
61 static bool g_agent_init;
62
63 /** TTS engine list */
64 static GList *g_engine_list;            
65
66 /** Current engine information */
67 static ttsengine_s g_cur_engine;
68
69 /** Result callback function */
70 static synth_result_callback g_result_cb;
71
72
73 /** Set current engine */
74 int __internal_set_current_engine(const char* engine_uuid);
75
76 /** Check engine id */
77 int __internal_check_engine_id(const char* engine_uuid);
78
79 /** Update engine list */
80 int __internal_update_engine_list();
81
82 /** Get engine info */
83 int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info);
84
85 /** Callback function for result */
86 bool __result_cb(ttsp_result_event_e event, const void* data, unsigned int data_size, void *user_data);
87
88 /** Callback function for voice list */
89 bool __supported_voice_cb(const char* language, ttsp_voice_type_e type, void* user_data);
90
91 /** Free voice list */
92 void __free_voice_list(GList* voice_list);
93
94 /** Callback function for engine info */
95 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name, 
96                       bool use_network, void* user_data);
97
98 /** Callback fucntion for engine setting */
99 bool __engine_setting_cb(const char* key, const char* value, void* user_data);
100
101
102 int ttsd_engine_agent_init(synth_result_callback result_cb)
103 {
104         /* initialize static data */
105         if (result_cb == NULL) {
106                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] invalid parameter");
107                 return TTSD_ERROR_INVALID_PARAMETER;
108         }
109
110         g_result_cb = result_cb;
111
112         g_cur_engine.engine_uuid = NULL;
113         g_cur_engine.engine_name = NULL;
114         g_cur_engine.engine_path = NULL;
115
116         g_cur_engine.is_set = false;
117         g_cur_engine.handle = NULL;
118         g_cur_engine.pefuncs = (ttspe_funcs_s*)g_malloc0( sizeof(ttspe_funcs_s) );
119         g_cur_engine.pdfuncs = (ttspd_funcs_s*)g_malloc0( sizeof(ttspd_funcs_s) );
120
121         g_agent_init = true;
122
123         if (0 != ttsd_config_get_default_voice(&(g_cur_engine.default_lang), &(g_cur_engine.default_vctype))) {
124                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default voice in config"); 
125                 /* Set default voice */
126                 g_cur_engine.default_lang = strdup("en_US");
127                 g_cur_engine.default_vctype = TTSP_VOICE_TYPE_FEMALE;
128         }
129
130         if (0 != ttsd_config_get_default_speed(&(g_cur_engine.default_speed))) {
131                 SLOG(LOG_WARN, get_tag(), "[Server WARNING] There is No default speed in config"); 
132                 ttsd_config_set_default_speed((int)TTSP_SPEED_NORMAL);
133                 g_cur_engine.default_speed = TTSP_SPEED_NORMAL;
134         }
135
136         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Initialize Engine Agent");
137
138         return 0;
139 }
140
141 int ttsd_engine_agent_release()
142 {
143         if (false == g_agent_init) {
144                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
145                 return TTSD_ERROR_OPERATION_FAILED;
146         }
147
148         /* unload current engine */
149         ttsd_engine_agent_unload_current_engine();
150
151         /* release engine list */
152         GList *iter = NULL;
153         ttsengine_s *data = NULL;
154
155         if (g_list_length(g_engine_list) > 0) {
156                 /* Get a first item */
157                 iter = g_list_first(g_engine_list);
158                 while (NULL != iter) {
159                         /* Get data from item */
160                         data = iter->data;
161                         iter = g_list_remove(iter, data);
162                 }
163         }
164         g_list_free(iter);
165         /* release current engine data */
166         if (g_cur_engine.pefuncs != NULL)
167                 g_free(g_cur_engine.pefuncs);
168         
169         if (g_cur_engine.pdfuncs != NULL)
170                 g_free(g_cur_engine.pdfuncs);
171
172         g_result_cb = NULL;
173         g_agent_init = false;
174
175         if (g_cur_engine.default_lang != NULL) 
176                 g_free(g_cur_engine.default_lang);
177
178
179         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Release Engine Agent");
180
181         return 0;
182 }
183
184 int ttsd_engine_agent_initialize_current_engine()
185 {
186         if (false == g_agent_init) {
187                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
188                 return TTSD_ERROR_OPERATION_FAILED;
189         }
190
191         /* update engine list */
192         if (0 != __internal_update_engine_list()) {
193                 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] No engine error");
194                 return TTSD_ERROR_OPERATION_FAILED;
195         }
196
197         /* 2. get current engine from config */
198         char* cur_engine_uuid = NULL;
199         bool is_get_engineid_from_config = false;
200
201         if (0 != ttsd_config_get_default_engine(&cur_engine_uuid)) {
202                 /*not set current engine */
203                 /*set system default engine*/
204                 GList *iter = NULL;
205                 ttsengine_info_s *data = NULL;
206
207                 if (g_list_length(g_engine_list) > 0) {
208                         iter = g_list_first(g_engine_list);
209                         char* default_engine = "27F277E9-BBC4-4dca-B553-D9884A3CDAA0";
210
211                         while (NULL != iter) {
212                                 data = iter->data;
213
214                                 if (0 == strncmp(data->engine_uuid, default_engine, strlen(default_engine))) {
215                                         /* current data is default engine */
216                                         break;
217                                 }
218
219                                 iter = g_list_next(iter);
220                         }
221                 } else {
222                         SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail to set a engine of engine list");
223                         return TTSD_ERROR_OPERATION_FAILED;     
224                 }
225
226                 if (NULL != data->engine_uuid) {
227                         cur_engine_uuid = strdup(data->engine_uuid); 
228                 } else {
229                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Data of current engine is corrupt");
230                         return TTSD_ERROR_OPERATION_FAILED;
231                 }
232                 
233                 is_get_engineid_from_config = false;
234         } else {
235                 is_get_engineid_from_config = true;
236         }
237
238         /* check whether cur engine uuid is valid or not. */
239         if (0 != __internal_check_engine_id(cur_engine_uuid)) {
240                 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] It is not valid engine id from config");
241
242                 GList *iter = NULL;
243                 
244                 if (g_list_length(g_engine_list) > 0) 
245                         iter = g_list_first(g_engine_list);
246                 else {
247                         SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] NO TTS Engine !!");
248                         return TTSD_ERROR_OPERATION_FAILED;     
249                 }
250
251                 if (cur_engine_uuid != NULL)    
252                         g_free(cur_engine_uuid);
253
254                 ttsengine_info_s *data = NULL;
255                 data = iter->data;
256
257                 cur_engine_uuid = g_strdup(data->engine_uuid);
258
259                 is_get_engineid_from_config = false;
260         }
261
262         if (NULL != cur_engine_uuid) 
263                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Current Engine Id : %s", cur_engine_uuid);
264         else 
265                 return TTSD_ERROR_OPERATION_FAILED;
266
267         /* set current engine */
268         if (0 != __internal_set_current_engine(cur_engine_uuid)) {
269                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set current engine ");
270                 return TTSD_ERROR_OPERATION_FAILED;
271         } 
272
273         if (false == is_get_engineid_from_config) {
274                 if (0 != ttsd_config_set_default_engine(cur_engine_uuid))
275                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set id to config"); 
276         }
277
278         if (NULL != cur_engine_uuid)    
279                 g_free(cur_engine_uuid);
280
281         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set current engine"); 
282
283         return 0;
284 }
285
286 int __internal_check_engine_id(const char* engine_uuid)
287 {
288         GList *iter = NULL;
289         ttsengine_s *data = NULL;
290
291         if (g_list_length(g_engine_list) > 0) {
292                 iter = g_list_first(g_engine_list);
293
294                 while (NULL != iter) {
295                         data = iter->data;
296
297                         if (0 == strncmp(engine_uuid, data->engine_uuid, strlen(data->engine_uuid))) 
298                                 return 0;
299         
300                         iter = g_list_next(iter);
301                 }
302         }
303
304         return -1;
305 }
306
307 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name, 
308                              bool use_network, void* user_data)
309 {
310         ttsengine_info_s* temp = (ttsengine_info_s*)user_data; 
311
312         temp->engine_uuid = g_strdup(engine_uuid);
313         temp->engine_name = g_strdup(engine_name);
314         temp->setting_ug_path = g_strdup(setting_ug_name);
315         temp->use_network = use_network;
316 }
317
318 int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info)
319 {
320         char *error;
321         void* handle;
322
323         handle = dlopen (filepath, RTLD_LAZY );
324
325         if (!handle) {
326                 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine : %s", filepath); 
327                 return TTSD_ERROR_OPERATION_FAILED;
328         }
329
330         /* link engine to daemon */
331         dlsym(handle, "ttsp_load_engine");
332         if ((error = dlerror()) != NULL) {
333                 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine. Fail to open ttsp_load_engine : %s", filepath);
334                 dlclose(handle);
335                 return TTSD_ERROR_OPERATION_FAILED;
336         }
337
338         dlsym(handle, "ttsp_unload_engine");
339         if ((error = dlerror()) != NULL) {
340                 SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine. Fail to open ttsp_unload_engine : %s", filepath);
341                 dlclose(handle);
342                 return TTSD_ERROR_OPERATION_FAILED;
343         }
344
345         int (*get_engine_info)(ttsp_engine_info_cb callback, void* user_data);
346
347         get_engine_info = (int (*)(ttsp_engine_info_cb, void*))dlsym(handle, "ttsp_get_engine_info");
348         if (NULL != (error = dlerror()) || NULL == get_engine_info) {
349                 SLOG(LOG_WARN, get_tag(), "[Engine Agent] ttsp_get_engine_info() link error");
350                 dlclose(handle);
351                 return TTSD_ERROR_OPERATION_FAILED;
352         }
353
354         ttsengine_info_s* temp;
355         temp = (ttsengine_info_s*)g_malloc0(sizeof(ttsengine_info_s));
356
357         /* get engine info */
358         if (0 != get_engine_info(&__engine_info_cb, (void*)temp)) {
359                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get engine info");
360                 dlclose(handle);
361                 g_free(temp);
362                 return TTSD_ERROR_OPERATION_FAILED;
363         }
364
365         /* close engine */
366         dlclose(handle);
367
368         if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
369                 if (true == temp->use_network) {
370                         free(temp);
371                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent WARNING] %s is invalid because of network based engine.", temp->engine_name);
372                         return TTSD_ERROR_OPERATION_FAILED;
373                 }
374         }
375
376         temp->engine_path = g_strdup(filepath);
377         
378         SLOG(LOG_DEBUG, get_tag(), "----- Valid engine");
379         SLOG(LOG_DEBUG, get_tag(), "Engine uuid : %s", temp->engine_uuid);
380         SLOG(LOG_DEBUG, get_tag(), "Engine name : %s", temp->engine_name);
381         SLOG(LOG_DEBUG, get_tag(), "Setting ug path : %s", temp->setting_ug_path);
382         SLOG(LOG_DEBUG, get_tag(), "Engine path : %s", temp->engine_path);
383         SLOG(LOG_DEBUG, get_tag(), "Use network : %s", temp->use_network ? "true":"false");
384         SLOG(LOG_DEBUG, get_tag(), "-----");
385         SLOG(LOG_DEBUG, get_tag(), "  ");
386
387         *info = temp;
388
389         return 0;
390 }
391
392 int __internal_update_engine_list()
393 {
394         /* relsease engine list */
395         GList *iter = NULL;
396         ttsengine_info_s *data = NULL;
397
398         if (g_list_length(g_engine_list) > 0) {
399                 iter = g_list_first(g_engine_list);
400
401                 while (NULL != iter) {
402                         data = iter->data;
403
404                         if (data != NULL)
405                                 g_free(data);
406
407                         g_engine_list = g_list_remove_link(g_engine_list, iter);
408                         iter = g_list_first(g_engine_list);
409                 }
410         }
411
412         /* get file name from engine directory and get engine information from each filename */
413         DIR *dp;
414         struct dirent *dirp;
415         dp = opendir(ENGINE_DIRECTORY_DEFAULT);
416
417         if (dp != NULL) {
418                 while ((dirp = readdir(dp)) != NULL) {
419                         ttsengine_info_s* info;
420                         char* filepath = NULL;
421                         int file_size;
422
423                         file_size = strlen(ENGINE_DIRECTORY_DEFAULT) + strlen(dirp->d_name) + 5;
424                         filepath = (char*)g_malloc0( sizeof(char) * file_size);
425
426                         if (NULL != filepath) { 
427                                 strncpy(filepath, ENGINE_DIRECTORY_DEFAULT, strlen(ENGINE_DIRECTORY_DEFAULT) );
428                                 strncat(filepath, "/", strlen("/") );
429                                 strncat(filepath, dirp->d_name, strlen(dirp->d_name) );
430                         } else {
431                                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not enough memory!!" );
432                                 continue;       
433                         }
434
435                         /* get its info and update engine list */
436                         if (0 == __internal_get_engine_info(filepath, &info)) {
437                                 /* add engine info to g_engine_list */
438                                 g_engine_list = g_list_append(g_engine_list, info);
439                         }
440
441                         if (NULL != filepath)
442                                 g_free(filepath);
443                 }
444
445                 closedir(dp);
446         }
447
448         dp = opendir(ENGINE_DIRECTORY_DOWNLOAD);
449
450         if (dp != NULL) {
451                 while ((dirp = readdir(dp)) != NULL) {
452                         ttsengine_info_s* info;
453                         char* filepath = NULL;
454                         int file_size;
455
456                         file_size = strlen(ENGINE_DIRECTORY_DOWNLOAD) + strlen(dirp->d_name) + 5;
457                         filepath = (char*)g_malloc0( sizeof(char) * file_size);
458
459                         if (NULL != filepath) { 
460                                 strcpy(filepath, ENGINE_DIRECTORY_DOWNLOAD);
461                                 strcat(filepath, "/");
462                                 strcat(filepath, dirp->d_name);
463                         } else {
464                                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not enough memory!!" );
465                                 continue;       
466                         }
467
468                         /* get its info and update engine list */
469                         if (0 == __internal_get_engine_info(filepath, &info)) {
470                                 /* add engine info to g_engine_list */
471                                 g_engine_list = g_list_append(g_engine_list, info);
472                         }
473
474                         if (NULL != filepath)
475                                 g_free(filepath);
476                 }
477
478                 closedir(dp);
479         }
480
481         if (g_list_length(g_engine_list) <= 0) {
482                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Engine");
483                 return TTSD_ERROR_OPERATION_FAILED;     
484         }
485
486 #ifdef ENGINE_AGENT_DEBUG       
487         ttsd_print_enginelist();
488 #endif
489
490         return 0;
491 }
492
493 int __internal_set_current_engine(const char* engine_uuid)
494 {
495         /* check whether engine id is valid or not. */
496         GList *iter = NULL;
497         ttsengine_info_s *data = NULL;
498
499         bool flag = false;
500         if (g_list_length(g_engine_list) > 0) {
501                 iter = g_list_first(g_engine_list);
502
503                 while (NULL != iter) {
504                         data = iter->data;
505
506                         if (0 == strncmp(data->engine_uuid, engine_uuid, strlen(engine_uuid))) {
507                                 flag = true;
508                                 break;
509                         }
510
511                         /*Get next item*/
512                         iter = g_list_next(iter);
513                 }
514         }
515
516         /* If current engine does not exist, return error */
517         if (false == flag) {
518                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] __internal_set_current_engine : Cannot find engine id");
519                 return TTSD_ERROR_OPERATION_FAILED;
520         } else {
521                 if (g_cur_engine.engine_uuid != NULL) {
522                         /*compare current engine uuid */
523                         if (0 == strncmp(g_cur_engine.engine_uuid, data->engine_uuid, strlen(engine_uuid))) {
524                                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] tts engine has already been set");
525                                 return 0;
526                         }
527                 }
528         }
529
530         /* set data from g_engine_list */
531         if (g_cur_engine.engine_uuid != NULL)   g_free(g_cur_engine.engine_uuid);
532         if (g_cur_engine.engine_name != NULL)   g_free(g_cur_engine.engine_name);
533         if (g_cur_engine.engine_path != NULL)   g_free(g_cur_engine.engine_path);
534
535         if (NULL == data->engine_uuid || NULL == data->engine_name || NULL == data->engine_path) {
536                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] __internal_set_current_engine : Engine data is NULL");
537                 return TTSD_ERROR_OPERATION_FAILED;
538         }
539
540         g_cur_engine.engine_uuid = g_strdup(data->engine_uuid);
541         g_cur_engine.engine_name = g_strdup(data->engine_name);
542         g_cur_engine.engine_path = g_strdup(data->engine_path);
543
544         g_cur_engine.handle = NULL;
545         g_cur_engine.is_loaded = false;
546         g_cur_engine.is_set = true;
547         g_cur_engine.need_network = data->use_network;
548
549         SLOG(LOG_DEBUG, get_tag(), "-----");
550         SLOG(LOG_DEBUG, get_tag(), "Current engine uuid : %s", g_cur_engine.engine_uuid);
551         SLOG(LOG_DEBUG, get_tag(), "Current engine name : %s", g_cur_engine.engine_name);
552         SLOG(LOG_DEBUG, get_tag(), "Current engine path : %s", g_cur_engine.engine_path);
553         SLOG(LOG_DEBUG, get_tag(), "-----");
554
555         return 0;
556 }
557
558 int ttsd_engine_agent_load_current_engine()
559 {
560         if (false == g_agent_init) {
561                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
562                 return TTSD_ERROR_OPERATION_FAILED;
563         }
564
565         if (false == g_cur_engine.is_set) {
566                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ttsd_engine_agent_load_current_engine : No Current Engine ");
567                 return TTSD_ERROR_OPERATION_FAILED;
568         }
569
570         /* check whether current engine is loaded or not */
571         if (true == g_cur_engine.is_loaded ) {
572                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent] ttsd_engine_agent_load_current_engine : Engine has already been loaded " );
573                 return 0;
574         }
575
576         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] current engine path : %s", g_cur_engine.engine_path);
577
578         /* open engine */
579         char *error = NULL;
580         g_cur_engine.handle = dlopen(g_cur_engine.engine_path, RTLD_LAZY); /* RTLD_LAZY RTLD_NOW*/
581
582         if (NULL != (error = dlerror()) || NULL == g_cur_engine.handle) {
583                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get current engine handle : dlopen error ($s)", error);
584                 return -2;
585         }
586
587         g_cur_engine.ttsp_unload_engine = (int (*)())dlsym(g_cur_engine.handle, "ttsp_unload_engine");
588         if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_unload_engine) {
589                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to link daemon to ttsp_unload_engine() of current engine : (%s)", error);
590                 return -3;
591         }
592
593         g_cur_engine.ttsp_load_engine = (int (*)(const ttspd_funcs_s* , ttspe_funcs_s*) )dlsym(g_cur_engine.handle, "ttsp_load_engine");
594         if (NULL != (error = dlerror()) || NULL == g_cur_engine.ttsp_load_engine) {
595                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to link daemon to ttsp_load_engine() of current engine : %s", error);
596                 return -3;
597         }
598
599         /* load engine */
600         g_cur_engine.pdfuncs->version = 1;
601         g_cur_engine.pdfuncs->size = sizeof(ttspd_funcs_s);
602
603         int ret = 0;
604         ret = g_cur_engine.ttsp_load_engine(g_cur_engine.pdfuncs, g_cur_engine.pefuncs); 
605         if (0 != ret) {
606                 printf("Fail load '%s' engine", g_cur_engine.engine_path);
607                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to load engine : result(%d)");
608                 return TTSD_ERROR_OPERATION_FAILED;
609         }
610
611         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] engine info : version(%d), size(%d)",g_cur_engine.pefuncs->version, g_cur_engine.pefuncs->size );
612
613         /* engine error check */
614         if (g_cur_engine.pefuncs->size != sizeof(ttspe_funcs_s)) {
615                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ttsd_engine_agent_load_current_engine : current engine is not valid");
616                 return TTSD_ERROR_OPERATION_FAILED;
617         }
618
619         /* initalize engine */
620         if (NULL == g_cur_engine.pefuncs->initialize) {
621                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] init function of engine is NULL!!");
622                 return TTSD_ERROR_OPERATION_FAILED;
623         }
624
625         ret = g_cur_engine.pefuncs->initialize(__result_cb);
626         if (0 != ret) {
627                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to initialize current engine : result(%d)", ret);
628                 return TTSD_ERROR_OPERATION_FAILED;
629         }
630
631         /* select default voice */
632         bool set_voice = false;
633         if (NULL != g_cur_engine.default_lang) {
634                 if (NULL == g_cur_engine.pefuncs->is_valid_voice) {
635                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] is_valid_voice() of engine is NULL!!");
636                         return TTSD_ERROR_OPERATION_FAILED;
637                 }
638
639                 if (true == g_cur_engine.pefuncs->is_valid_voice(g_cur_engine.default_lang, g_cur_engine.default_vctype)) {
640                         set_voice = true;
641                         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set origin default voice to current engine : lang(%s), type(%d)", 
642                                 g_cur_engine.default_lang,  g_cur_engine.default_vctype);
643                 } else {
644                         SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail set origin default voice : lang(%s), type(%d)",
645                                 g_cur_engine.default_lang, g_cur_engine.default_vctype);
646                 }
647         }
648
649         if (false == set_voice) {
650                 if (NULL == g_cur_engine.pefuncs->foreach_voices) {
651                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] get_voice_list of engine is NULL!!");
652                         return TTSD_ERROR_OPERATION_FAILED;
653                 }
654
655                 /* get language list */
656                 int ret;
657                 GList* voice_list = NULL;
658
659                 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
660
661                 if (0 == ret && 0 < g_list_length(voice_list)) {
662                         GList *iter = NULL;
663                         voice_s* voice;
664                         
665                         iter = g_list_first(voice_list);
666
667                         /* check english */
668                         while (NULL != iter) {
669                                 voice = iter->data;
670
671                                 if (NULL != voice) {
672                                         if (0 == strcmp("en_US", voice->language)) 
673                                                 break;
674                                 }
675
676                                 iter = g_list_next(iter);
677                         }
678         
679                         /* Set selected language and type */
680                         if (true != g_cur_engine.pefuncs->is_valid_voice(voice->language, voice->type)) {
681                                 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail voice is NOT valid");
682                                 return TTSD_ERROR_OPERATION_FAILED;
683                         }
684
685                         ttsd_config_set_default_voice(voice->language, (int)voice->type);
686                         
687                         g_cur_engine.default_lang = g_strdup(voice->language);
688                         g_cur_engine.default_vctype = voice->type;
689
690                         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Select default voice : lang(%s), type(%d)", 
691                                 voice->language,  voice->type);
692
693                         __free_voice_list(voice_list);
694                 } else {
695                         SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail to get language list : result(%d)", ret);
696                         return TTSD_ERROR_OPERATION_FAILED;
697                 }
698         } 
699  
700         g_cur_engine.is_loaded = true;
701
702         return 0;
703 }
704
705 int ttsd_engine_agent_unload_current_engine()
706 {
707         if (false == g_agent_init) {
708                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
709                 return TTSD_ERROR_OPERATION_FAILED;
710         }
711
712         if (false == g_cur_engine.is_set) {
713                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No Current Engine ");
714                 return TTSD_ERROR_OPERATION_FAILED;
715         }
716
717         if (false == g_cur_engine.is_loaded) {
718                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Engine has already been unloaded " );
719                 return 0;
720         }
721
722         /* shutdown engine */
723         if (NULL == g_cur_engine.pefuncs->deinitialize) {
724                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] The deinitialize() of engine is NULL!!");
725         } else {
726                 int ret = 0;
727                 ret = g_cur_engine.pefuncs->deinitialize();
728                 if (0 != ret) {
729                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent] Fail deinitialize() : result(%d)", ret);
730                 }
731         }
732
733         /* unload engine */
734         g_cur_engine.ttsp_unload_engine();
735         
736         dlclose(g_cur_engine.handle);
737
738         /* reset current engine data */
739         g_cur_engine.handle = NULL;
740         g_cur_engine.is_loaded = false;
741
742         return 0;
743 }
744
745 bool ttsd_engine_agent_need_network()
746 {
747         if (false == g_agent_init) {
748                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
749                 return TTSD_ERROR_OPERATION_FAILED;
750         }
751
752         if (false == g_cur_engine.is_loaded) {
753                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
754                 return TTSD_ERROR_OPERATION_FAILED;
755         }
756
757         return g_cur_engine.need_network;
758 }
759
760 bool ttsd_engine_select_valid_voice(const char* lang, int type, char** out_lang, ttsp_voice_type_e* out_type)
761 {
762         if (NULL == lang || NULL == out_lang || NULL == out_type) {
763                 return false;
764         }
765
766         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Select voice : input lang(%s) , input type(%d), default lang(%s), default type(%d)", 
767                 lang, type, g_cur_engine.default_lang, g_cur_engine.default_vctype);
768
769         
770         /* case 1 : Both are default */
771         if (0 == strncmp(lang, "default", strlen("default")) && 0 == type) {
772                 *out_lang = strdup(g_cur_engine.default_lang);
773                 *out_type = g_cur_engine.default_vctype;
774                 return true;
775         } 
776         
777         /* Get voice list */
778         if (NULL == g_cur_engine.pefuncs->foreach_voices) {
779                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] foreach_voices of engine is NULL!!");
780                 return TTSD_ERROR_OPERATION_FAILED;
781         }
782
783         GList* voice_list = NULL;
784         int ret = 0;
785
786         ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
787         if (0 != ret || 0 >= g_list_length(voice_list)) {
788                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get voice list : result(%d)", ret);
789                 return false;
790         }
791
792         bool result;
793         result = false;
794
795         GList *iter = NULL;
796         voice_s* voice;
797
798         /* lang and type are not default type */
799         if (0 != strncmp(lang, "default", strlen("default")) && 0 != type) {
800                 iter = g_list_first(voice_list);
801
802                 while (NULL != iter) {
803                         /* Get handle data from list */
804                         voice = iter->data;
805
806                         if (0 == strncmp(voice->language, lang, strlen(lang)) &&  voice->type == type) {
807                                 *out_lang = strdup(voice->language);
808                                 *out_type = voice->type;
809                                 result = true;
810                                 break;
811                         }
812
813                         iter = g_list_next(iter);
814                 }
815
816         } else if (0 != strncmp(lang, "default", strlen("default")) && 0 == type) {
817                 /* Only type is default */
818                 if (0 == strncmp(lang, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang))) {
819                         *out_lang = strdup(g_cur_engine.default_lang);
820                         *out_type = g_cur_engine.default_vctype;
821                         result = true;
822                 } else {
823                         voice_s* voice_selected = NULL;
824                         iter = g_list_first(voice_list);
825                         while (NULL != iter) {
826                                 /* Get handle data from list */
827                                 voice = iter->data;
828
829                                 if (0 == strncmp(voice->language, lang, strlen(lang))) {
830                                         voice_selected = voice;
831                                         if (voice->type == g_cur_engine.default_vctype) {
832                                                 voice_selected = voice;
833                                                 break;
834                                         }
835                                 }
836                                 iter = g_list_next(iter);
837                         }
838
839                         if (NULL != voice_selected) {
840                                 *out_lang = strdup(voice_selected->language);
841                                 *out_type = voice_selected->type;
842                                 result = true;
843                         }
844                 }
845         } else if (0 == strncmp(lang, "default", strlen("default")) && 0 != type) {
846                 /* Only lang is default */
847                 if (type == g_cur_engine.default_vctype) {
848                         *out_lang = strdup(g_cur_engine.default_lang);
849                         *out_type = g_cur_engine.default_vctype;
850                         result = true;
851                 } else {
852                         voice_s* voice_selected = NULL;
853                         iter = g_list_first(voice_list);
854                         while (NULL != iter) {
855                                 /* Get handle data from list */
856                                 voice = iter->data;
857
858                                 if (0 == strncmp(voice->language, g_cur_engine.default_lang, strlen(g_cur_engine.default_lang)) ) {
859                                         voice_selected = voice;
860                                         if (voice->type == type) {
861                                                 voice_selected = voice;
862                                                 break;
863                                         }
864                                 }
865                                 iter = g_list_next(iter);
866                         }
867
868                         if (NULL != voice_selected) {
869                                 *out_lang = strdup(voice->language);
870                                 *out_type = voice_selected->type;
871                                 result = true;
872                         }
873                 }
874         }
875
876         if (true == result) {
877                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Selected voice : lang(%s), type(%d)", *out_lang, *out_type);
878         }
879
880         __free_voice_list(voice_list);
881
882         return result;
883 }
884
885 bool ttsd_engine_agent_is_same_engine(const char* engine_id)
886 {
887         if (false == g_agent_init) {
888                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
889                 return false;
890         }
891
892         if (false == g_cur_engine.is_loaded) {
893                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
894                 return false;
895         }
896
897         /* compare current engine and engine id.*/
898         if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
899                 return true;
900         }
901
902         return false;
903 }
904
905 int ttsd_engine_agent_set_default_voice(const char* language, ttsp_voice_type_e vctype)
906 {
907         if (false == g_agent_init) {
908                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
909                 return TTSD_ERROR_OPERATION_FAILED;
910         }
911
912         if (false == g_cur_engine.is_loaded) {
913                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
914                 return TTSD_ERROR_OPERATION_FAILED;
915         }
916
917         if (NULL == g_cur_engine.pefuncs->is_valid_voice) {
918                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] The function of engine is NULL!!");
919                 return TTSD_ERROR_OPERATION_FAILED;
920         }
921
922         int ret = -1;
923         if(false == g_cur_engine.pefuncs->is_valid_voice(language, vctype)) {
924                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice is NOT valid !!");
925                 return TTSD_ERROR_INVALID_VOICE;
926         }
927
928         if (NULL != g_cur_engine.default_lang)
929                 free(g_cur_engine.default_lang);
930
931         g_cur_engine.default_lang = strdup(language);
932         g_cur_engine.default_vctype = vctype;
933
934         ret = ttsd_config_set_default_voice(language, (int)vctype);
935         if (0 == ret) {
936                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default voice : lang(%s), type(%d)",
937                         g_cur_engine.default_lang, g_cur_engine.default_vctype); 
938         } else {
939                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to write default voice to config (%d)", ret); 
940                 return TTSD_ERROR_OPERATION_FAILED;
941         }
942
943         return 0;
944 }
945
946 /******************************************************************************************
947 * TTS Engine Interfaces for client
948 *******************************************************************************************/
949
950 int ttsd_engine_start_synthesis(const char* lang, const ttsp_voice_type_e vctype, const char* text, const int speed, void* user_param)
951 {
952         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] start ttsd_engine_start_synthesis()");
953
954         if (false == g_agent_init) {
955                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
956                 return TTSD_ERROR_OPERATION_FAILED;
957         }
958
959         if (false == g_cur_engine.is_loaded) {
960                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
961                 return TTSD_ERROR_OPERATION_FAILED;
962         }
963
964         /* select voice for default */
965         char* temp_lang = NULL;
966         ttsp_voice_type_e temp_type;
967         if (true != ttsd_engine_select_valid_voice(lang, vctype, &temp_lang, &temp_type)) {
968                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to select default voice");
969                 return TTSD_ERROR_INVALID_VOICE;
970         } else {
971                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Start synthesis : language(%s), type(%d), speed(%d), text(%s)", 
972                         temp_lang, temp_type, speed, text);
973         }
974
975         if (NULL == g_cur_engine.pefuncs->start_synth) {
976                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] start_synth() of engine is NULL!!");
977                 return TTSD_ERROR_OPERATION_FAILED;
978         }
979
980         ttsp_speed_e temp_speed;
981
982         if (0 == speed) {
983                 temp_speed = g_cur_engine.default_speed;
984         } else {
985                 temp_speed = speed;
986         }
987
988         /* synthesize text */
989         int ret = 0;
990         ret = g_cur_engine.pefuncs->start_synth(temp_lang, temp_type, text, temp_speed, user_param);
991         if (0 != ret) {
992                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************", ret);
993                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] * synthesize error : result(%6d) *", ret);
994                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] ***************************************", ret);
995                 return TTSD_ERROR_OPERATION_FAILED;
996         }
997
998         if (NULL == temp_lang)
999                 free(temp_lang);
1000
1001         return 0;
1002 }
1003
1004
1005 int ttsd_engine_cancel_synthesis()
1006 {
1007         SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] start ttsd_engine_cancel_synthesis()");
1008         
1009         if (false == g_agent_init) {
1010                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1011                 return TTSD_ERROR_OPERATION_FAILED;
1012         }
1013
1014         if (false == g_cur_engine.is_loaded) {
1015                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1016                 return TTSD_ERROR_OPERATION_FAILED;
1017         }
1018         
1019         if (NULL == g_cur_engine.pefuncs->cancel_synth) {
1020                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] cancel_synth() of engine is NULL!!");
1021                 return TTSD_ERROR_OPERATION_FAILED;
1022         }
1023
1024         /* stop synthesis */
1025         int ret = 0;
1026         ret = g_cur_engine.pefuncs->cancel_synth();
1027         if (0 != ret) {
1028                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail cancel synthesis : result(%d)", ret);
1029                 return TTSD_ERROR_OPERATION_FAILED;
1030         }
1031
1032         return 0;
1033 }
1034
1035 int ttsd_engine_get_audio_format( ttsp_audio_type_e* type, int* rate, int* channels)
1036 {
1037         if (false == g_agent_init) {
1038                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1039                 return TTSD_ERROR_OPERATION_FAILED;
1040         }
1041
1042         if (false == g_cur_engine.is_loaded) {
1043                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1044                 return TTSD_ERROR_OPERATION_FAILED;
1045         }
1046
1047         if (NULL == g_cur_engine.pefuncs->get_audio_format) {
1048                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] get_audio_format() of engine is NULL!!");
1049                 return TTSD_ERROR_OPERATION_FAILED;
1050         }
1051
1052         int ret = 0;
1053         ret = g_cur_engine.pefuncs->get_audio_format(type, rate, channels);
1054         if (0 != ret) {
1055                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get audio format : result(%d)", ret);
1056                 return TTSD_ERROR_OPERATION_FAILED;
1057         } else
1058                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] get audio format : type(%d), rate(%d), channel(%d)", *type, *rate, *channels);
1059         
1060         return 0;
1061 }
1062
1063 bool __supported_voice_cb(const char* language, ttsp_voice_type_e type, void* user_data)
1064 {
1065         GList** voice_list = (GList**)user_data;
1066
1067         if (NULL == language || NULL == voice_list) {
1068                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in voice list callback!!!!");
1069                 return false;
1070         }
1071
1072         SLOG(LOG_DEBUG, get_tag(), "-- Language(%s), Type(%d)", language, type);
1073
1074         voice_s* voice = g_malloc0(sizeof(voice_s));
1075         voice->language = strdup(language);
1076         voice->type = type;
1077
1078         *voice_list = g_list_append(*voice_list, voice);
1079
1080         return true;
1081 }
1082
1083 int ttsd_engine_get_voice_list(GList** voice_list)
1084 {
1085         if (false == g_agent_init) {
1086                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1087                 return TTSD_ERROR_OPERATION_FAILED;
1088         }
1089
1090         if (false == g_cur_engine.is_loaded) {
1091                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1092                 return TTSD_ERROR_OPERATION_FAILED;
1093         }
1094
1095         if (NULL == g_cur_engine.pefuncs->foreach_voices) {
1096                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] The function of engine is NULL!!");
1097                 return TTSD_ERROR_OPERATION_FAILED;
1098         }
1099
1100         int ret = 0;
1101         ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, (void*)voice_list);
1102         if (0 != ret) {
1103                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Fail to get voice list : result(%d)", ret);
1104                 return TTSD_ERROR_OPERATION_FAILED;
1105         }
1106
1107         return 0;
1108 }
1109
1110 int ttsd_engine_get_default_voice( char** lang, ttsp_voice_type_e* vctype )
1111 {
1112         if (false == g_agent_init) {
1113                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1114                 return TTSD_ERROR_OPERATION_FAILED;
1115         }
1116
1117         if (false == g_cur_engine.is_loaded) {
1118                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1119                 return TTSD_ERROR_OPERATION_FAILED;
1120         }
1121
1122         if (NULL == lang || NULL == vctype) {
1123                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] BAD Parameter"); 
1124                 return TTSD_ERROR_INVALID_PARAMETER;
1125         }
1126
1127         if (NULL != g_cur_engine.default_lang) {
1128                 *lang = g_strdup(g_cur_engine.default_lang);
1129                 *vctype = g_cur_engine.default_vctype;
1130
1131                 SLOG(LOG_DEBUG, get_tag(), "[Engine] Get default voice : language(%s), type(%d)", *lang, *vctype);
1132         } else {
1133                 if (NULL == g_cur_engine.pefuncs->foreach_voices) {
1134                         SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] get_voice_list of engine is NULL!!");
1135                         return TTSD_ERROR_OPERATION_FAILED;
1136                 }
1137
1138                 /* get language list */
1139                 int ret;
1140                 GList* voice_list = NULL;
1141
1142                 ret = g_cur_engine.pefuncs->foreach_voices(__supported_voice_cb, &voice_list);
1143
1144                 if (0 == ret && 0 < g_list_length(voice_list)) {
1145                         GList *iter = NULL;
1146                         voice_s* voice;
1147
1148                         iter = g_list_first(voice_list);
1149
1150                         if (NULL != iter) {
1151                                 voice = iter->data;
1152                                 
1153                                 if (true != g_cur_engine.pefuncs->is_valid_voice(voice->language, voice->type)) {
1154                                         SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail voice is NOT valid ");
1155                                         return TTSD_ERROR_OPERATION_FAILED;
1156                                 }
1157                                 
1158                                 ttsd_config_set_default_voice(voice->language, (int)voice->type);
1159                                 
1160                                 if (NULL != g_cur_engine.default_lang)
1161                                         g_free(g_cur_engine.default_lang);
1162
1163                                 g_cur_engine.default_lang = g_strdup(voice->language);
1164                                 g_cur_engine.default_vctype = voice->type;
1165
1166                                 *lang = g_strdup(g_cur_engine.default_lang);
1167                                 *vctype = g_cur_engine.default_vctype = voice->type;
1168
1169                                 SLOG(LOG_DEBUG, get_tag(), "[Engine] Get default voice (New selected) : language(%s), type(%d)", *lang, *vctype);
1170                         } else {
1171                                 SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail to get language list : result(%d)", ret);
1172                                 return TTSD_ERROR_OPERATION_FAILED;
1173                         }
1174
1175                         __free_voice_list(voice_list);
1176                 } else {
1177                         SLOG(LOG_ERROR, get_tag(), "[Engine ERROR] Fail to get language list : result(%d)", ret);
1178                         return TTSD_ERROR_OPERATION_FAILED;
1179                 }
1180         }
1181         
1182         return 0;
1183 }
1184
1185 /*
1186 * TTS Engine Interfaces for setting
1187 */
1188 int ttsd_engine_setting_get_engine_list(GList** engine_list)
1189 {
1190         if (false == g_agent_init) {
1191                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1192                 return TTSD_ERROR_OPERATION_FAILED;
1193         }
1194
1195         if (NULL == engine_list) {
1196                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL" );
1197                 return TTSD_ERROR_INVALID_PARAMETER;
1198         }
1199
1200         /* update engine list */
1201         if (0 != __internal_update_engine_list()) {
1202                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail __internal_update_engine_list()");
1203                 return TTSD_ERROR_OPERATION_FAILED;
1204         }
1205
1206         GList *iter = NULL;
1207         ttsengine_info_s *data = NULL;
1208
1209         iter = g_list_first(g_engine_list);
1210
1211         SLOG(LOG_DEBUG, get_tag(), "----- [Engine Agent] engine list -----");
1212         
1213         while (NULL != iter) {
1214                 engine_s* temp_engine;
1215         
1216                 temp_engine = (engine_s*)g_malloc0(sizeof(engine_s));
1217
1218                 data = iter->data;
1219
1220                 temp_engine->engine_id = strdup(data->engine_uuid);
1221                 temp_engine->engine_name = strdup(data->engine_name);
1222                 temp_engine->ug_name = strdup(data->setting_ug_path);
1223
1224                 *engine_list = g_list_append(*engine_list, temp_engine);
1225
1226                 iter = g_list_next(iter);
1227
1228                 SLOG(LOG_DEBUG, get_tag(), " -- engine id(%s) engine name(%s) ug name(%s)", 
1229                         temp_engine->engine_id, temp_engine->engine_name, temp_engine->ug_name);
1230                 
1231         }
1232
1233         SLOG(LOG_DEBUG, get_tag(), "--------------------------------------");
1234         
1235         return 0;
1236 }
1237
1238 int ttsd_engine_setting_get_engine(char** engine_id)
1239 {
1240         if (false == g_agent_init) {
1241                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1242                 return TTSD_ERROR_OPERATION_FAILED;
1243         }
1244
1245         if (false == g_cur_engine.is_loaded) {
1246                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1247                 return TTSD_ERROR_ENGINE_NOT_FOUND;
1248         }
1249
1250         *engine_id = strdup(g_cur_engine.engine_uuid);
1251
1252         return 0;
1253 }
1254
1255 int ttsd_engine_setting_set_engine(const char* engine_id)
1256 {
1257         if (false == g_agent_init) {
1258                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1259                 return TTSD_ERROR_OPERATION_FAILED;
1260         }
1261
1262         /* compare current engine and new engine.*/
1263         if (0 == strncmp(g_cur_engine.engine_uuid, engine_id, strlen(engine_id))) {
1264                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] new engine(%s) is the same as current engine(%s)", engine_id, g_cur_engine.engine_uuid);
1265                 return 0;
1266         }
1267
1268         char* tmp_uuid = NULL;
1269         tmp_uuid = strdup(g_cur_engine.engine_uuid);
1270
1271         /* unload engine */
1272         if (0 != ttsd_engine_agent_unload_current_engine()) {
1273                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] fail to unload current engine");
1274         } else {
1275                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] unload current engine");
1276         }
1277
1278         /* change current engine */
1279         if (0 != __internal_set_current_engine(engine_id)) {
1280                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail __internal_set_current_engine()");
1281
1282                 /* roll back to old current engine. */
1283                 __internal_set_current_engine(tmp_uuid);
1284                 ttsd_engine_agent_load_current_engine();
1285                 
1286                 if (tmp_uuid != NULL)   
1287                         free(tmp_uuid);
1288
1289                 return TTSD_ERROR_OPERATION_FAILED;
1290         }
1291
1292         /* load engine */
1293         if (0 != ttsd_engine_agent_load_current_engine()) {
1294                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent Error] fail to load new engine");
1295                 
1296                 if( tmp_uuid != NULL )  
1297                         free(tmp_uuid);
1298
1299                 return TTSD_ERROR_OPERATION_FAILED;
1300         } else {
1301                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] load new engine");
1302         }
1303
1304         /* save engine id to config */
1305         if (0 != ttsd_config_set_default_engine(engine_id)) {
1306                 SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail to save engine id to config"); 
1307         }
1308
1309         if (tmp_uuid != NULL)   
1310                 free(tmp_uuid);
1311
1312         return 0;
1313 }
1314
1315 int ttsd_engine_setting_get_voice_list(char** engine_id, GList** voice_list)
1316 {
1317         if (false == g_agent_init) {
1318                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1319                 return TTSD_ERROR_OPERATION_FAILED;
1320         }
1321
1322         if (false == g_cur_engine.is_loaded) {
1323                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1324                 return TTSD_ERROR_OPERATION_FAILED;
1325         }
1326
1327         int ret = 0;
1328
1329         /* get language list from engine*/
1330         ret = ttsd_engine_get_voice_list(voice_list);
1331         if (0 != ret) {
1332                 SLOG(LOG_ERROR, get_tag(), "[Server Error] fail ttsd_engine_get_voice_list() ");
1333                 return ret;
1334         }
1335
1336         *engine_id = strdup(g_cur_engine.engine_uuid);
1337         
1338         return 0;
1339 }
1340
1341 int ttsd_engine_setting_get_default_voice(char** language, ttsp_voice_type_e* vctype)
1342 {
1343         if (false == g_agent_init) {
1344                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1345                 return TTSD_ERROR_OPERATION_FAILED;
1346         }
1347
1348         if (false == g_cur_engine.is_loaded) {
1349                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1350                 return TTSD_ERROR_OPERATION_FAILED;
1351         }
1352
1353         /* get current language from engine*/
1354         int ret = 0;
1355         char* temp_lang;
1356         ttsp_voice_type_e temp_int;
1357
1358         ret = ttsd_engine_get_default_voice(&temp_lang, &temp_int);
1359         if (0 != ret) {
1360                 SLOG(LOG_ERROR, get_tag(), "[Server Error] fail ttsd_engine_get_default_voice()");
1361                 return ret;
1362         } 
1363
1364         if (NULL != temp_lang) {
1365                 *language = strdup(temp_lang);
1366                 *vctype = temp_int;
1367                 free(temp_lang);
1368         } else {
1369                 SLOG(LOG_ERROR, get_tag(), "[Server Error] fail to get default language");
1370                 return TTSD_ERROR_OPERATION_FAILED;
1371         }
1372
1373         return 0;
1374 }
1375
1376 int ttsd_engine_setting_set_default_voice(const char* language, ttsp_voice_type_e vctype)
1377 {
1378         if (false == g_agent_init) {
1379                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1380                 return TTSD_ERROR_OPERATION_FAILED;
1381         }
1382
1383         if (false == g_cur_engine.is_loaded) {
1384                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1385                 return TTSD_ERROR_OPERATION_FAILED;
1386         }
1387
1388         if (NULL == g_cur_engine.pefuncs->is_valid_voice) {
1389                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] The function of engine is NULL!!");
1390                 return TTSD_ERROR_OPERATION_FAILED;
1391         }
1392
1393         int ret = -1;
1394         if(false == g_cur_engine.pefuncs->is_valid_voice(language, vctype)) {
1395                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Voice is NOT valid !!");
1396                 return TTSD_ERROR_INVALID_VOICE;
1397         }
1398
1399         if (NULL != g_cur_engine.default_lang)
1400                 free(g_cur_engine.default_lang);
1401
1402         g_cur_engine.default_lang = strdup(language);
1403         g_cur_engine.default_vctype = vctype;
1404
1405         ret = ttsd_config_set_default_voice(language, (int)vctype);
1406         if (0 == ret) {
1407                 SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set default voice : lang(%s), type(%d)",
1408                                 g_cur_engine.default_lang, g_cur_engine.default_vctype); 
1409         } else {
1410                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to write default voice to config (%d)", ret); 
1411         }
1412
1413         return 0;
1414 }
1415
1416 int ttsd_engine_setting_get_default_speed(ttsp_speed_e* speed)
1417 {
1418         if (false == g_agent_init) {
1419                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1420                 return TTSD_ERROR_OPERATION_FAILED;
1421         }
1422
1423         if (false == g_cur_engine.is_loaded) {
1424                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1425                 return TTSD_ERROR_OPERATION_FAILED;
1426         }
1427         
1428         /* get current language */
1429         *speed = g_cur_engine.default_speed;
1430
1431         return 0;
1432 }
1433
1434 int ttsd_engine_setting_set_default_speed(const ttsp_speed_e speed)
1435 {
1436         if (false == g_agent_init) {
1437                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1438                 return TTSD_ERROR_OPERATION_FAILED;
1439         }
1440
1441         if (false == g_cur_engine.is_loaded) {
1442                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1443                 return TTSD_ERROR_OPERATION_FAILED;
1444         }
1445
1446         g_cur_engine.default_speed = speed;
1447
1448         if (0 != ttsd_config_set_default_speed(speed)) {
1449                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set default speed to config");
1450         }
1451
1452         return 0;
1453 }
1454
1455 bool __engine_setting_cb(const char* key, const char* value, void* user_data)
1456 {
1457         GList** engine_setting_list = (GList**)user_data;
1458
1459         if (NULL == engine_setting_list || NULL == key || NULL == value) {
1460                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL in engine setting callback!!!!");
1461                 return false;
1462         }
1463
1464         engine_setting_s* temp = g_malloc0(sizeof(engine_setting_s));
1465         temp->key = g_strdup(key);
1466         temp->value = g_strdup(value);
1467
1468         *engine_setting_list = g_list_append(*engine_setting_list, temp);
1469
1470         return true;
1471 }
1472
1473
1474 int ttsd_engine_setting_get_engine_setting_info(char** engine_id, GList** setting_list)
1475 {
1476         if (false == g_agent_init) {
1477                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1478                 return TTSD_ERROR_OPERATION_FAILED;
1479         }
1480
1481         if (false == g_cur_engine.is_loaded) {
1482                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1483                 return TTSD_ERROR_OPERATION_FAILED;
1484         }
1485
1486         if (NULL == setting_list) {
1487                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Input parameter is NULL");
1488                 return TTSD_ERROR_INVALID_PARAMETER;
1489         }
1490
1491         if (NULL == g_cur_engine.pefuncs->foreach_engine_setting) {
1492                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] foreach_engine_setting() of engine is NULL!!");
1493                 return TTSD_ERROR_OPERATION_FAILED;
1494         }
1495
1496         /* get setting info and move setting info to input parameter */
1497         int result = 0;
1498
1499         result = g_cur_engine.pefuncs->foreach_engine_setting(__engine_setting_cb, setting_list);
1500
1501         if (0 == result && 0 < g_list_length(*setting_list)) {
1502                 *engine_id = strdup(g_cur_engine.engine_uuid);
1503         } else {
1504                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to get setting info : result(%d)", result);
1505                 result = TTSD_ERROR_OPERATION_FAILED;
1506         }
1507
1508         return result;
1509 }
1510
1511 int ttsd_engine_setting_set_engine_setting(const char* key, const char* value)
1512 {
1513         if (false == g_agent_init) {
1514                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized" );
1515                 return TTSD_ERROR_OPERATION_FAILED;
1516         }
1517
1518         if (false == g_cur_engine.is_loaded) {
1519                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not loaded engine");
1520                 return TTSD_ERROR_OPERATION_FAILED;
1521         }
1522
1523         if (NULL == g_cur_engine.pefuncs->set_engine_setting) {
1524                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] set_setting_info of engine is NULL!!");
1525                 return TTSD_ERROR_OPERATION_FAILED;
1526         }
1527
1528         /* get setting info and move setting info to input parameter */
1529         int ret = 0;
1530         ret = g_cur_engine.pefuncs->set_engine_setting(key, value);
1531
1532         if (0 != ret) {
1533                 SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set engine setting : result(%d)", ret); 
1534                 return TTSD_ERROR_OPERATION_FAILED;
1535         } 
1536
1537         return 0;
1538 }
1539
1540 void __free_voice_list(GList* voice_list)
1541 {
1542         GList *iter = NULL;
1543         voice_s* data = NULL;
1544
1545         /* if list have item */
1546         if (g_list_length(voice_list) > 0) {
1547                 /* Get a first item */
1548                 iter = g_list_first(voice_list);
1549
1550                 while (NULL != iter) {
1551                         data = iter->data;
1552                         
1553                         if (NULL != data) {
1554                                 if (NULL != data->language)
1555                                         g_free(data->language);
1556
1557                                 g_free(data);
1558                         }
1559                         
1560                         voice_list = g_list_remove_link(voice_list, iter);
1561                         
1562                         iter = g_list_first(voice_list);
1563                 }
1564         }
1565 }
1566
1567 /*
1568 * TTS Engine Callback Functions                                                                                 `                                 *
1569 */
1570 bool __result_cb(ttsp_result_event_e event, const void* data, unsigned int data_size, void *user_data)
1571 {
1572         g_result_cb(event, data, data_size, user_data);
1573
1574         return true;
1575 }
1576
1577 /* function for debugging */
1578 int ttsd_print_enginelist()
1579 {
1580         GList *iter = NULL;
1581         ttsengine_info_s *data = NULL;
1582
1583         if (g_list_length(g_engine_list) > 0) {
1584                 iter = g_list_first(g_engine_list);
1585
1586                 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1587
1588                 int i = 1;      
1589                 while (NULL != iter) {
1590                         data = iter->data;
1591
1592                         SLOG(LOG_DEBUG, get_tag(), "[%dth]", i);
1593                         SLOG(LOG_DEBUG, get_tag(), "engine uuid : %s", data->engine_uuid);
1594                         SLOG(LOG_DEBUG, get_tag(), "engine name : %s", data->engine_name);
1595                         SLOG(LOG_DEBUG, get_tag(), "engine path : %s", data->engine_path);
1596                         SLOG(LOG_DEBUG, get_tag(), "setting ug path : %s", data->setting_ug_path);
1597
1598                         iter = g_list_next(iter);
1599                         i++;
1600                 }
1601                 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1602                 SLOG(LOG_DEBUG, get_tag(), "  ");
1603         } else {
1604                 SLOG(LOG_DEBUG, get_tag(), "----- engine list -----");
1605                 SLOG(LOG_DEBUG, get_tag(), "No Engine in directory");
1606                 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
1607         }
1608
1609         return 0;
1610 }
1611
1612
1613
1614
1615
1616
1617