Add functions to set credential
[platform/core/uifw/stt.git] / server / sttd_engine_agent.c
1 /*
2 *  Copyright (c) 2011-2014 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 "stt_defs.h"
19 #include "stt_engine.h"
20 #include "sttd_main.h"
21 #include "sttd_client_data.h"
22 #include "sttd_config.h"
23 #include "sttd_recorder.h"
24 #include "sttd_engine_agent.h"
25
26
27 #define AUDIO_CREATE_ON_START
28
29 /*
30 * Internal data structure
31 */
32
33 typedef struct {
34         int     uid;
35         int     engine_id;
36         bool    use_default_engine;
37 } sttengine_client_s;
38
39 typedef struct _sttengine_info {
40         int     engine_id;
41
42         char*   engine_uuid;
43         char*   engine_path;
44         char*   engine_name;
45         char*   engine_setting_path;
46         bool    use_network;
47
48         bool    is_loaded;
49
50         /* engine base setting */
51         char*   first_lang;
52         bool    silence_detection;
53         bool    support_silence_detection;
54         bool    need_credential;
55 } sttengine_info_s;
56
57 /** stt engine agent init */
58 static bool     g_agent_init;
59
60 /** list */
61 static GSList*  g_engine_client_list;
62 static GSList*  g_engine_list;
63
64 /** default engine info */
65 static int      g_default_engine_id;
66 static char*    g_default_language;
67 static bool     g_default_silence_detected;
68
69 static int      g_engine_id_count;
70
71 /** current engine id */
72 static int      g_recording_engine_id;
73
74 /** callback functions */
75 static result_callback g_result_cb;
76 static result_time_callback g_result_time_cb;
77 static silence_dectection_callback g_silence_cb;
78
79
80 /** callback functions */
81 void __result_cb(sttp_result_event_e event, const char* type, const char** data, int data_count,
82                  const char* msg, void* time_info, void *user_data);
83
84 bool __result_time_cb(int index, sttp_result_time_event_e event, const char* text,
85                       long start_time, long end_time, void* user_data);
86
87 void __detect_silence_cb(sttp_silence_type_e type, void* user_data);
88
89 bool __supported_language_cb(const char* language, void* user_data);
90
91 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
92                       bool use_network, void* user_data);
93
94 /*
95 * Internal Interfaces
96 */
97
98 /** get engine info */
99 int __internal_get_engine_info(const char* filepath, sttengine_info_s** info);
100
101 int __log_enginelist();
102
103 /*
104 * STT Engine Agent Interfaces
105 */
106 int sttd_engine_agent_init(result_callback result_cb, result_time_callback time_cb,
107                            silence_dectection_callback silence_cb)
108 {
109         /* initialize static data */
110         if (NULL == result_cb || NULL == time_cb || NULL == silence_cb) {
111                 SLOG(LOG_ERROR, TAG_STTD, "[Engine agent ERROR] Invalid parameter");
112                 return STTD_ERROR_INVALID_PARAMETER;
113         }
114
115         g_result_cb = result_cb;
116         g_result_time_cb = time_cb;
117         g_silence_cb = silence_cb;
118
119         g_default_engine_id = -1;
120         g_default_language = NULL;
121         g_engine_id_count = 1;
122         g_recording_engine_id = -1;
123
124         if (0 != sttd_config_get_default_language(&(g_default_language))) {
125                 SLOG(LOG_WARN, TAG_STTD, "[Engine Agent WARNING] There is No default voice in config");
126                 /* Set default voice */
127                 g_default_language = strdup("en_US");
128         } else {
129                 SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Default language is %s", g_default_language);
130         }
131
132         int temp;
133         if (0 != sttd_config_get_default_silence_detection(&temp)) {
134                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] There is no silence detection in config");
135                 g_default_silence_detected = true;
136         } else {
137                 g_default_silence_detected = (bool)temp;
138         }
139
140         g_agent_init = false;
141
142         return 0;
143 }
144
145 int __engine_agent_clear_engine(sttengine_info_s *engine)
146 {
147         if (NULL != engine) {
148                 if (NULL != engine->engine_uuid)        free(engine->engine_uuid);
149                 if (NULL != engine->engine_path)        free(engine->engine_path);
150                 if (NULL != engine->engine_name)        free(engine->engine_name);
151                 if (NULL != engine->engine_setting_path)free(engine->engine_setting_path);
152                 if (NULL != engine->first_lang)         free(engine->first_lang);
153
154                 free(engine);
155         }
156
157         return 0;
158 }
159
160 int sttd_engine_agent_release()
161 {
162         /* Release client list */
163         GSList *iter = NULL;
164         sttengine_client_s *client = NULL;
165
166         if (g_slist_length(g_engine_client_list) > 0) {
167                 /* Get a first item */
168                 iter = g_slist_nth(g_engine_client_list, 0);
169
170                 while (NULL != iter) {
171                         /* Get handle data from list */
172                         client = iter->data;
173                         g_engine_client_list = g_slist_remove_link(g_engine_client_list, iter);
174
175                         if (NULL != client)
176                                 free(client);
177
178                         iter = g_slist_nth(g_engine_client_list, 0);
179                 }
180         }
181
182         g_slist_free(g_engine_client_list);
183
184         /* Release engine list */
185         sttengine_info_s *engine = NULL;
186
187         if (0 < g_slist_length(g_engine_list)) {
188                 /* Get a first item */
189                 iter = g_slist_nth(g_engine_list, 0);
190
191                 while (NULL != iter) {
192                         /* Get handle data from list */
193                         engine = iter->data;
194                         g_engine_list = g_slist_remove_link(g_engine_list, iter);
195
196                         /* Check engine unload */
197                         if (engine->is_loaded) {
198                                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Unload engine id(%d)", engine->engine_id);
199
200                                 if (0 != stt_engine_deinitialize(engine->engine_id))
201                                         SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to deinitialize engine id(%d)", engine->engine_id);
202
203                                 if (0 != stt_engine_unload(engine->engine_id))
204                                         SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to unload engine id(%d)", engine->engine_id);
205
206                                 engine->is_loaded = false;
207                         }
208
209                         __engine_agent_clear_engine(engine);
210
211                         iter = g_slist_nth(g_engine_list, 0);
212                 }
213         }
214
215         g_result_cb = NULL;
216         g_silence_cb = NULL;
217
218         g_agent_init = false;
219         g_default_engine_id = -1;
220
221         return 0;
222 }
223
224 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
225                       bool use_network, void* user_data)
226 {
227         sttengine_info_s* temp = (sttengine_info_s*)user_data;
228
229         temp->engine_uuid = g_strdup(engine_uuid);
230         temp->engine_name = g_strdup(engine_name);
231         temp->engine_setting_path = g_strdup(setting_ug_name);
232         temp->use_network = use_network;
233 }
234
235 int __internal_get_engine_info(const char* filepath, sttengine_info_s** info)
236 {
237         if (NULL == filepath || NULL == info) {
238                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
239                 return STTD_ERROR_INVALID_PARAMETER;
240         }
241
242         /* load engine */
243         char *error;
244         void* handle;
245
246         handle = dlopen(filepath, RTLD_LAZY);
247
248         if (!handle) {
249                 SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Invalid engine : %s", filepath);
250                 return STTD_ERROR_ENGINE_NOT_FOUND;
251         }
252
253         /* link engine to daemon */
254         dlsym(handle, "sttp_load_engine");
255         if ((error = dlerror()) != NULL) {
256                 SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Invalid engine. Fail to open sttp_load_engine : %s", error);
257                 dlclose(handle);
258                 return STTD_ERROR_ENGINE_NOT_FOUND;
259         }
260
261         dlsym(handle, "sttp_unload_engine");
262         if ((error = dlerror()) != NULL) {
263                 SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Invalid engine. Fail to open sttp_unload_engine : %s", error);
264                 dlclose(handle);
265                 return STTD_ERROR_ENGINE_NOT_FOUND;
266         }
267
268         int (*get_engine_info)(sttpe_engine_info_cb callback, void* user_data);
269
270         get_engine_info = (int (*)(sttpe_engine_info_cb, void*))dlsym(handle, "sttp_get_engine_info");
271         if ((error = dlerror()) != NULL || NULL == get_engine_info) {
272                 SLOG(LOG_WARN, TAG_STTD, "[Engine Agent WARNING] Invalid engine. Fail to open sttp_get_engine_info : %s", error);
273                 dlclose(handle);
274                 return STTD_ERROR_ENGINE_NOT_FOUND;
275         }
276
277         sttengine_info_s* temp;
278         temp = (sttengine_info_s*)calloc(1, sizeof(sttengine_info_s));
279         if (NULL == temp) {
280                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to allocate memory");
281                 dlclose(handle);
282                 return STTD_ERROR_OUT_OF_MEMORY;
283         }
284
285         /* get engine info */
286         if (0 != get_engine_info(__engine_info_cb, (void*)temp)) {
287                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get engine info from engine");
288                 dlclose(handle);
289                 free(temp);
290                 return STTD_ERROR_ENGINE_NOT_FOUND;
291         }
292
293         /* close engine */
294         dlclose(handle);
295
296         temp->engine_id = g_engine_id_count;
297         g_engine_id_count++;
298
299         temp->engine_path = g_strdup(filepath);
300         temp->is_loaded = false;
301
302         SLOG(LOG_DEBUG, TAG_STTD, "----- Valid Engine");
303         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "Engine id : %d", temp->engine_id);
304         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "Engine uuid : %s", temp->engine_uuid);
305         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "Engine name : %s", temp->engine_name);
306         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "Engine path : %s", temp->engine_path);
307         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "Engine setting path : %s", temp->engine_setting_path);
308         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "Use network : %s", temp->use_network ? "true" : "false");
309         SLOG(LOG_DEBUG, TAG_STTD, "-----");
310         SLOG(LOG_DEBUG, TAG_STTD, "  ");
311
312         *info = temp;
313
314         return STTD_ERROR_NONE;
315 }
316
317 bool __is_engine(const char* filepath)
318 {
319         GSList *iter = NULL;
320         sttengine_info_s *engine = NULL;
321
322         if (0 < g_slist_length(g_engine_list)) {
323                 /* Get a first item */
324                 iter = g_slist_nth(g_engine_list, 0);
325
326                 while (NULL != iter) {
327                         /* Get handle data from list */
328                         engine = iter->data;
329
330                         if (0 == strcmp(engine->engine_path, filepath)) {
331                                 return true;
332                         }
333
334                         iter = g_slist_next(iter);
335                 }
336         }
337
338         return false;
339 }
340
341 int sttd_engine_agent_initialize_engine_list()
342 {
343         /* Get file name from default engine directory */
344         DIR *dp = NULL;
345         int ret = -1;
346         struct dirent entry;
347         struct dirent *dirp = NULL;
348
349         dp  = opendir(STT_DEFAULT_ENGINE);
350         if (NULL != dp) {
351                 do {
352                         ret = readdir_r(dp, &entry, &dirp);
353                         if (0 != ret) {
354                                 SLOG(LOG_ERROR, TAG_STTD, "[File ERROR] Fail to read directory");
355                                 break;
356                         }
357
358                         if (NULL != dirp) {
359                                 sttengine_info_s* info;
360                                 char* filepath;
361                                 int filesize;
362
363                                 filesize = strlen(STT_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
364                                 filepath = (char*)calloc(filesize, sizeof(char));
365
366                                 if (NULL != filepath) {
367                                         snprintf(filepath, filesize, "%s/%s", STT_DEFAULT_ENGINE, dirp->d_name);
368                                 } else {
369                                         SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Memory not enough!!");
370                                         continue;
371                                 }
372
373                                 if (false  == __is_engine(filepath)) {
374                                         /* get its info and update engine list */
375                                         if (0 == __internal_get_engine_info(filepath, &info)) {
376                                                 /* add engine info to g_engine_list */
377                                                 g_engine_list = g_slist_append(g_engine_list, info);
378                                         }
379                                 }
380
381                                 if (NULL != filepath)
382                                         free(filepath);
383                         }
384                 } while (NULL != dirp);
385
386                 closedir(dp);
387         } else {
388                 SLOG(LOG_WARN, TAG_STTD, "[Engine Agent WARNING] Fail to open default directory");
389         }
390
391         if (0 >= g_slist_length(g_engine_list)) {
392                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] No Engine");
393                 return STTD_ERROR_ENGINE_NOT_FOUND;
394         }
395
396         __log_enginelist();
397
398         /* Set default engine */
399         GSList *iter = NULL;
400         sttengine_info_s *engine = NULL;
401         char* cur_engine_uuid = NULL;
402         bool is_default_engine = false;
403
404         /* get current engine from config */
405         if (0 == sttd_config_get_default_engine(&cur_engine_uuid)) {
406                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] current engine from config : %s", cur_engine_uuid);
407
408                 if (0 < g_slist_length(g_engine_list)) {
409                         /* Get a first item */
410                         iter = g_slist_nth(g_engine_list, 0);
411
412                         while (NULL != iter) {
413                                 /* Get handle data from list */
414                                 engine = iter->data;
415
416                                 if (0 == strcmp(engine->engine_uuid, cur_engine_uuid)) {
417                                         is_default_engine = true;
418                                         g_default_engine_id = engine->engine_id;
419                                         break;
420                                 }
421
422                                 iter = g_slist_next(iter);
423                         }
424                 }
425
426                 if (cur_engine_uuid != NULL)
427                         free(cur_engine_uuid);
428         } else {
429                 SLOG(LOG_WARN, TAG_STTD, "[Engine Agent WARNING] There is not current engine from config");
430         }
431
432         if (false == is_default_engine) {
433                 if (0 < g_slist_length(g_engine_list)) {
434                         /* Get a first item */
435                         iter = g_slist_nth(g_engine_list, 0);
436
437                         /* Get handle data from list */
438                         engine = iter->data;
439
440                         if (NULL != engine) {
441                                 is_default_engine = true;
442                                 g_default_engine_id = engine->engine_id;
443                         }
444                 }
445         }
446
447         if (NULL != engine) {
448                 if (NULL != engine->engine_uuid) {
449                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Default engine Id(%d) uuid(%s)", engine->engine_id, engine->engine_uuid);
450
451                         if (false == is_default_engine) {
452                                 if (0 != sttd_config_set_default_engine(engine->engine_uuid))
453                                         SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to set default engine ");
454                         }
455                 }
456         } else {
457                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Default engine is NULL");
458                 return STTD_ERROR_ENGINE_NOT_FOUND;
459         }
460
461         g_agent_init = true;
462
463         return 0;
464 }
465
466 sttengine_info_s* __engine_agent_get_engine_by_id(int engine_id)
467 {
468         GSList *iter = NULL;
469         sttengine_info_s *data = NULL;
470
471         iter = g_slist_nth(g_engine_list, 0);
472
473         while (NULL != iter) {
474
475                 data = iter->data;
476
477                 if (data->engine_id == engine_id)
478                         return data;
479
480                 iter = g_slist_next(iter);
481         }
482
483         return NULL;
484 }
485
486 sttengine_info_s* __engine_agent_get_engine_by_uuid(const char* engine_uuid)
487 {
488         GSList *iter = NULL;
489         sttengine_info_s *data = NULL;
490
491         iter = g_slist_nth(g_engine_list, 0);
492
493         while (NULL != iter) {
494
495                 data = iter->data;
496
497                 if (0 == strcmp(data->engine_uuid, engine_uuid))
498                         return data;
499
500                 iter = g_slist_next(iter);
501         }
502
503         return NULL;
504 }
505
506 sttengine_client_s* __engine_agent_get_client(int uid)
507 {
508         GSList *iter = NULL;
509         sttengine_client_s *data = NULL;
510
511         if (0 < g_slist_length(g_engine_client_list)) {
512                 iter = g_slist_nth(g_engine_client_list, 0);
513
514                 while (NULL != iter) {
515                         /* Get handle data from list */
516                         data = iter->data;
517
518                         if (uid == data->uid)
519                                 return data;
520
521                         iter = g_slist_next(iter);
522                 }
523         }
524
525         return NULL;
526 }
527
528 sttengine_info_s* __engine_agent_get_engine_by_uid(int uid)
529 {
530         sttengine_client_s *data;
531
532         data = __engine_agent_get_client(uid);
533         if (NULL != data)
534                 return __engine_agent_get_engine_by_id(data->engine_id);
535
536         return NULL;
537 }
538
539 int __engine_agent_check_engine_unload(int engine_id)
540 {
541         /* Check the count of client to use this engine */
542         GSList *iter = NULL;
543         int client_count = 0;
544         sttengine_client_s *data = NULL;
545
546         if (0 < g_slist_length(g_engine_client_list)) {
547                 iter = g_slist_nth(g_engine_client_list, 0);
548
549                 while (NULL != iter) {
550                         /* Get handle data from list */
551                         data = iter->data;
552
553                         if (data->engine_id == engine_id)
554                                 client_count++;
555
556                         iter = g_slist_next(iter);
557                 }
558         }
559
560         if (0 == client_count) {
561                 sttengine_info_s* engine = NULL;
562                 engine = __engine_agent_get_engine_by_id(engine_id);
563                 if (NULL == engine) {
564                         SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get engine from client(%d)", engine_id);
565                 } else {
566                         if (engine->is_loaded) {
567                                 /* unload engine */
568 #ifndef AUDIO_CREATE_ON_START
569                                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Destroy recorder");
570                                 if (0 != sttd_recorder_destroy(engine->engine_id))
571                                         SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to destroy recorder(%d)", engine->engine_id);
572 #endif
573                                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Unload engine id(%d)", engine_id);
574                                 if (0 != stt_engine_deinitialize(engine->engine_id))
575                                         SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to deinitialize engine id(%d)", engine->engine_id);
576
577                                 if (0 != stt_engine_unload(engine->engine_id))
578                                         SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to unload engine id(%d)", engine->engine_id);
579
580                                 engine->is_loaded = false;
581                         }
582                 }
583         }
584
585         return 0;
586 }
587
588 int sttd_engine_agent_load_current_engine(int uid, const char* engine_uuid)
589 {
590         if (false == g_agent_init) {
591                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
592                 return STTD_ERROR_OPERATION_FAILED;
593         }
594
595         sttengine_client_s* client = NULL;
596         sttengine_info_s* engine = NULL;
597         int before_engine = -1;
598
599         client = __engine_agent_get_client(uid);
600
601         if (NULL == client) {
602                 client = (sttengine_client_s*)calloc(1, sizeof(sttengine_client_s));
603                 if (NULL == client) {
604                         SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to allocate memory");
605                         return STTD_ERROR_OUT_OF_MEMORY;
606                 }
607
608                 /* initialize */
609                 client->uid = uid;
610                 client->engine_id = -1;
611
612                 g_engine_client_list = g_slist_append(g_engine_client_list, client);
613
614                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Registered client(%d)", uid);
615         }
616
617         if (NULL == engine_uuid) {
618                 /* Set default engine */
619                 engine = __engine_agent_get_engine_by_id(g_default_engine_id);
620
621                 if (NULL == engine) {
622                         SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get default engine : %d", g_default_engine_id);
623                         return STTD_ERROR_OPERATION_FAILED;
624                 }
625                 before_engine = client->engine_id;
626
627                 client->engine_id = engine->engine_id;
628                 client->use_default_engine = true;
629         } else {
630                 /* Set engine by uid */
631                 engine = __engine_agent_get_engine_by_uuid(engine_uuid);
632
633                 if (NULL == engine) {
634                         SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get engine : %s", engine_uuid);
635                         return STTD_ERROR_OPERATION_FAILED;
636                 }
637                 before_engine = client->engine_id;
638
639                 client->engine_id = engine->engine_id;
640                 client->use_default_engine = false;
641         }
642
643         if (-1 != before_engine) {
644                 /* Unload engine if reference count is 0 */
645                 __engine_agent_check_engine_unload(before_engine);
646         }
647
648         if (true == engine->is_loaded) {
649                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine] engine id(%d) is already loaded", engine->engine_id);
650                 return 0;
651         }
652
653         /* Load engine */
654         int ret;
655         ret = stt_engine_load(engine->engine_id, engine->engine_path);
656         if (0 != ret) {
657                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to load engine : id(%d) path(%s)", engine->engine_id, engine->engine_path);
658                 return STTD_ERROR_OPERATION_FAILED;
659         }
660
661         ret = stt_engine_initialize(engine->engine_id, __result_cb, __detect_silence_cb);
662         if (0 != ret) {
663                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to initialize engine : id(%d) path(%s)", engine->engine_id, engine->engine_path);
664                 return STTD_ERROR_OPERATION_FAILED;
665         }
666
667         ret = stt_engine_set_silence_detection(engine->engine_id, g_default_silence_detected);
668         if (0 != ret) {
669                 SLOG(LOG_WARN, TAG_STTD, "[Engine Agent WARNING] Not support silence detection");
670                 engine->support_silence_detection = false;
671         } else {
672                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Silence detection : %s", g_default_silence_detected ? "true" : "false");
673                 engine->support_silence_detection = true;
674                 engine->silence_detection = g_default_silence_detected;
675         }
676
677         /* Set first language */
678         char* tmp_lang = NULL;
679         ret = stt_engine_get_first_language(engine->engine_id, &tmp_lang);
680         if (0 == ret && NULL != tmp_lang) {
681                 engine->first_lang = strdup(tmp_lang);
682                 free(tmp_lang);
683         } else {
684                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get first language from engine : %d %s", engine->engine_id, engine->engine_name);
685                 return STTD_ERROR_OPERATION_FAILED;
686         }
687
688 #ifndef AUDIO_CREATE_ON_START
689         /* Ready recorder */
690         sttp_audio_type_e atype;
691         int rate;
692         int channels;
693
694         ret = stt_engine_get_audio_type(engine->engine_id, &atype, &rate, &channels);
695         if (0 != ret) {
696                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get audio type : %d %s", engine->engine_id, engine->engine_name);
697                 return STTD_ERROR_OPERATION_FAILED;
698         }
699
700         ret = sttd_recorder_create(engine->engine_id, uid, atype, channels, rate);
701         if (0 != ret) {
702                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to create recorder : %d %s", engine->engine_id, engine->engine_name);
703                 return STTD_ERROR_OPERATION_FAILED;
704         }
705 #endif
706
707         engine->is_loaded = true;
708         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent SUCCESS] The %s(%d) has been loaded !!!", engine->engine_name, engine->engine_id);
709
710         return 0;
711 }
712
713 int sttd_engine_agent_unload_current_engine(int uid)
714 {
715         if (false == g_agent_init) {
716                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized ");
717                 return STTD_ERROR_OPERATION_FAILED;
718         }
719
720         /* Remove client */
721         int engine_id = -1;
722
723         GSList *iter = NULL;
724         sttengine_client_s *data = NULL;
725
726         if (0 < g_slist_length(g_engine_client_list)) {
727                 iter = g_slist_nth(g_engine_client_list, 0);
728
729                 while (NULL != iter) {
730                         /* Get handle data from list */
731                         data = iter->data;
732
733                         if (NULL != data) {
734                                 if (uid == data->uid) {
735                                         g_engine_client_list = g_slist_remove_link(g_engine_client_list, iter);
736                                         engine_id = data->engine_id;
737                                         free(data);
738                                         break;
739                                 }
740                         }
741
742                         iter = g_slist_next(iter);
743                 }
744         }
745
746         if (-1 != engine_id) {
747                 __engine_agent_check_engine_unload(engine_id);
748         }
749
750         return 0;
751 }
752
753 bool sttd_engine_agent_is_default_engine()
754 {
755         if (g_default_engine_id > 0)
756                 return true;
757
758         return false;
759 }
760
761 int sttd_engine_agent_get_engine_list(GSList** engine_list)
762 {
763         if (false == g_agent_init) {
764                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
765                 return STTD_ERROR_OPERATION_FAILED;
766         }
767
768         GSList *iter = NULL;
769         sttengine_info_s *data = NULL;
770
771         iter = g_slist_nth(g_engine_list, 0);
772
773         SLOG(LOG_DEBUG, TAG_STTD, "----- [Engine Agent] engine list -----");
774
775         while (NULL != iter) {
776                 engine_s* temp_engine;
777
778                 temp_engine = (engine_s*)calloc(1, sizeof(engine_s));
779                 if (NULL == temp_engine) {
780                         SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to allocate memory");
781                         return STTD_ERROR_OUT_OF_MEMORY;
782                 }
783
784                 data = iter->data;
785
786                 temp_engine->engine_id = strdup(data->engine_uuid);
787                 temp_engine->engine_name = strdup(data->engine_name);
788                 temp_engine->ug_name = strdup(data->engine_setting_path);
789
790                 *engine_list = g_slist_append(*engine_list, temp_engine);
791
792                 iter = g_slist_next(iter);
793
794                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, " -- Engine id(%s)", temp_engine->engine_id);
795                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "    Engine name(%s)", temp_engine->engine_name);
796                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "    Engine ug name(%s)", temp_engine->ug_name);
797         }
798
799         SLOG(LOG_DEBUG, TAG_STTD, "--------------------------------------");
800
801         return 0;
802 }
803
804 int sttd_engine_agent_get_current_engine(int uid, char** engine_uuid)
805 {
806         if (false == g_agent_init) {
807                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
808                 return STTD_ERROR_OPERATION_FAILED;
809         }
810
811         if (NULL == engine_uuid) {
812                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid parameter");
813                 return STTD_ERROR_INVALID_PARAMETER;
814         }
815
816         sttengine_info_s* engine;
817         engine = __engine_agent_get_engine_by_uid(uid);
818
819         if (NULL == engine) {
820                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
821                 return STTD_ERROR_INVALID_PARAMETER;
822         }
823
824         if (false == engine->is_loaded) {
825                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
826                 return STTD_ERROR_OPERATION_FAILED;
827         }
828
829         *engine_uuid = strdup(engine->engine_uuid);
830
831         return 0;
832 }
833
834 bool sttd_engine_agent_need_network(int uid)
835 {
836         if (false == g_agent_init) {
837                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
838                 return STTD_ERROR_OPERATION_FAILED;
839         }
840
841         sttengine_info_s* engine;
842         engine = __engine_agent_get_engine_by_uid(uid);
843
844         if (NULL != engine)
845                 return engine->use_network;
846
847         return false;
848 }
849
850 int sttd_engine_agent_supported_langs(int uid, GSList** lang_list)
851 {
852         if (false == g_agent_init) {
853                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
854                 return STTD_ERROR_OPERATION_FAILED;
855         }
856
857         if (NULL == lang_list) {
858                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Input parameter is NULL");
859                 return STTD_ERROR_INVALID_PARAMETER;
860         }
861
862         sttengine_info_s* engine = NULL;
863         engine = __engine_agent_get_engine_by_uid(uid);
864
865         if (NULL == engine) {
866                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
867                 return STTD_ERROR_INVALID_PARAMETER;
868         }
869
870         if (false == engine->is_loaded) {
871                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
872                 return STTD_ERROR_OPERATION_FAILED;
873         }
874
875         int ret = stt_engine_get_supported_langs(engine->engine_id, lang_list);
876         if (0 != ret) {
877                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] get language list error(%d)", ret);
878                 return STTD_ERROR_OPERATION_FAILED;
879         }
880
881         return 0;
882 }
883
884 int sttd_engine_agent_get_default_lang(int uid, char** lang)
885 {
886         if (false == g_agent_init) {
887                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
888                 return STTD_ERROR_OPERATION_FAILED;
889         }
890
891         if (NULL == lang) {
892                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
893                 return STTD_ERROR_INVALID_PARAMETER;
894         }
895
896         sttengine_info_s* engine = NULL;
897         engine = __engine_agent_get_engine_by_uid(uid);
898
899         if (NULL == engine) {
900                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
901                 return STTD_ERROR_INVALID_PARAMETER;
902         }
903
904         if (false == engine->is_loaded) {
905                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
906                 return STTD_ERROR_OPERATION_FAILED;
907         }
908
909         /* get default language */
910         bool is_valid = false;
911         if (0 != stt_engine_is_valid_language(engine->engine_id, g_default_language, &is_valid)) {
912                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to check valid language");
913                 return STTD_ERROR_OPERATION_FAILED;
914         }
915
916         if (true == is_valid) {
917                 *lang = strdup(g_default_language);
918         } else
919                 *lang = strdup(engine->first_lang);
920
921         return 0;
922 }
923
924 int sttd_engine_agent_get_option_supported(int uid, bool* silence)
925 {
926         if (false == g_agent_init) {
927                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
928                 return STTD_ERROR_OPERATION_FAILED;
929         }
930
931         if (NULL == silence) {
932                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
933                 return STTD_ERROR_INVALID_PARAMETER;
934         }
935
936         sttengine_info_s* engine = NULL;
937         engine = __engine_agent_get_engine_by_uid(uid);
938
939         if (NULL == engine) {
940                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] uid(%d) is not valid", uid);
941                 return STTD_ERROR_INVALID_PARAMETER;
942         }
943
944         if (false == engine->is_loaded) {
945                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
946                 return STTD_ERROR_OPERATION_FAILED;
947         }
948
949         *silence = engine->support_silence_detection;
950
951         return 0;
952 }
953
954 int sttd_engine_agent_is_credential_needed(int uid, bool* credential)
955 {
956         if (false == g_agent_init) {
957                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
958                 return STTD_ERROR_OPERATION_FAILED;
959         }
960
961         if (NULL == credential) {
962                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
963                 return STTD_ERROR_INVALID_PARAMETER;
964         }
965
966         sttengine_info_s* engine = NULL;
967         engine = __engine_agent_get_engine_by_uid(uid);
968
969         if (NULL == engine) {
970                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] uid(%d) is not valid", uid);
971                 return STTD_ERROR_INVALID_PARAMETER;
972         }
973
974         if (false == engine->is_loaded) {
975                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
976                 return STTD_ERROR_OPERATION_FAILED;
977         }
978
979         bool temp = false;
980         int ret;
981
982         ret = stt_engine_need_app_credential(engine->engine_id, &temp);
983         if (0 != ret) {
984                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get to support recognition type : %d", ret);
985                 return STTD_ERROR_OPERATION_FAILED;
986         }
987
988         *credential = temp;
989         return 0;
990 }
991
992 int sttd_engine_agent_is_recognition_type_supported(int uid, const char* type, bool* support)
993 {
994         if (false == g_agent_init) {
995                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
996                 return STTD_ERROR_OPERATION_FAILED;
997         }
998
999         if (NULL == type || NULL == support) {
1000                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
1001                 return STTD_ERROR_INVALID_PARAMETER;
1002         }
1003
1004         sttengine_info_s* engine = NULL;
1005         engine = __engine_agent_get_engine_by_uid(uid);
1006
1007         if (NULL == engine) {
1008                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
1009                 return STTD_ERROR_INVALID_PARAMETER;
1010         }
1011
1012         if (false == engine->is_loaded) {
1013                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
1014                 return STTD_ERROR_OPERATION_FAILED;
1015         }
1016
1017         bool temp = false;
1018         int ret;
1019
1020         ret = stt_engine_support_recognition_type(engine->engine_id, type, &temp);
1021         if (0 != ret) {
1022                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get to support recognition type : %d", ret);
1023                 return STTD_ERROR_OPERATION_FAILED;
1024         }
1025
1026         *support = temp;
1027
1028         return 0;
1029 }
1030
1031 /*
1032 * STT Engine Interfaces for client
1033 */
1034
1035 int __set_option(sttengine_info_s* engine, int silence)
1036 {
1037         if (NULL == engine)
1038                 return -1;
1039
1040         /* Check silence detection */
1041         if (engine->support_silence_detection) {
1042                 if (2 == silence) {
1043                         /* default option set */
1044                         if (g_default_silence_detected != engine->silence_detection) {
1045                                 if (0 != stt_engine_set_silence_detection(engine->engine_id, g_default_silence_detected)) {
1046                                         SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to set silence detection : %s", g_default_silence_detected ? "true" : "false");
1047                                 } else {
1048                                         engine->silence_detection = g_default_silence_detected;
1049                                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Set silence detection : %s", g_default_silence_detected ? "true" : "false");
1050                                 }
1051                         }
1052                 } else {
1053                         if (silence != engine->silence_detection) {
1054                                 if (0 != stt_engine_set_silence_detection(engine->engine_id, silence)) {
1055                                         SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to set silence detection : %s", silence ? "true" : "false");
1056                                 } else {
1057                                         engine->silence_detection = silence;
1058                                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Set silence detection : %s", silence ? "true" : "false");
1059                                 }
1060                         }
1061                 }
1062         }
1063
1064         return 0;
1065 }
1066
1067 int sttd_engine_agent_recognize_start_engine(int uid, const char* lang, const char* recognition_type,
1068                                       int silence, const char* credential, void* user_param)
1069 {
1070         if (false == g_agent_init) {
1071                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
1072                 return STTD_ERROR_OPERATION_FAILED;
1073         }
1074
1075         if (NULL == lang || NULL == recognition_type) {
1076                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
1077                 return STTD_ERROR_INVALID_PARAMETER;
1078         }
1079
1080         sttengine_info_s* engine = NULL;
1081         engine = __engine_agent_get_engine_by_uid(uid);
1082
1083         if (NULL == engine) {
1084                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
1085                 return STTD_ERROR_INVALID_PARAMETER;
1086         }
1087
1088         if (false == engine->is_loaded) {
1089                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
1090                 return STTD_ERROR_OPERATION_FAILED;
1091         }
1092
1093         if (0 != __set_option(engine, silence)) {
1094                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to set options");
1095                 return STTD_ERROR_OPERATION_FAILED;
1096         }
1097
1098         SLOG(LOG_DEBUG, TAG_STTD, "g_default_language %s", g_default_language);
1099
1100         int ret;
1101         char* temp = NULL;
1102         if (0 == strncmp(lang, "default", strlen("default"))) {
1103                 bool is_valid = false;
1104                 if (0 != stt_engine_is_valid_language(engine->engine_id, g_default_language, &is_valid)) {
1105                         SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to check valid language");
1106                         return STTD_ERROR_OPERATION_FAILED;
1107                 }
1108
1109                 if (true == is_valid) {
1110                         temp = strdup(g_default_language);
1111                         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent DEBUG] Default language is %s", temp);
1112                 } else {
1113                         temp = strdup(engine->first_lang);
1114                         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent DEBUG] Default language is engine first lang : %s", temp);
1115                 }
1116         } else {
1117                 temp = strdup(lang);
1118         }
1119
1120         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Start engine");
1121
1122         ret = stt_engine_recognize_start(engine->engine_id, temp, recognition_type, credential, user_param);
1123         if (NULL != temp)       free(temp);
1124         if (0 != ret) {
1125                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Recognition start error(%d)", ret);
1126                 sttd_recorder_destroy(engine->engine_id);
1127                 return STTD_ERROR_OPERATION_FAILED;
1128         }
1129
1130 #ifdef AUDIO_CREATE_ON_START
1131         /* Ready recorder */
1132         sttp_audio_type_e atype;
1133         int rate;
1134         int channels;
1135
1136         ret = stt_engine_get_audio_type(engine->engine_id, &atype, &rate, &channels);
1137         if (0 != ret) {
1138                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get audio type : %d %s", engine->engine_id, engine->engine_name);
1139                 return STTD_ERROR_OPERATION_FAILED;
1140         }
1141
1142         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Create recorder");
1143
1144         ret = sttd_recorder_create(engine->engine_id, uid, atype, channels, rate);
1145         if (0 != ret) {
1146                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to create recorder : %d %s", engine->engine_id, engine->engine_name);
1147                 return STTD_ERROR_OPERATION_FAILED;
1148         }
1149 #endif
1150
1151 #if 0
1152         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Start recorder");
1153
1154         ret = sttd_recorder_start(engine->engine_id);
1155         if (0 != ret) {
1156                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to start recorder : result(%d)", ret);
1157                 return ret;
1158         }
1159
1160         g_recording_engine_id = engine->engine_id;
1161         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] g_recording_engine_id : %d", g_recording_engine_id);
1162 #endif
1163
1164         return 0;
1165 }
1166
1167 int sttd_engine_agent_recognize_start_recorder(int uid)
1168 {
1169         sttengine_info_s* engine = NULL;
1170         engine = __engine_agent_get_engine_by_uid(uid);
1171
1172         if (NULL == engine) {
1173                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
1174                 return STTD_ERROR_INVALID_PARAMETER;
1175         }
1176
1177         if (false == engine->is_loaded) {
1178                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
1179                 return STTD_ERROR_OPERATION_FAILED;
1180         }
1181
1182         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Start recorder");
1183
1184         int ret;
1185         ret = sttd_recorder_start(engine->engine_id);
1186         if (0 != ret) {
1187                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to start recorder : result(%d)", ret);
1188                 stt_engine_recognize_cancel(engine->engine_id);
1189                 sttd_recorder_stop(engine->engine_id);
1190                 return ret;
1191         }
1192
1193         g_recording_engine_id = engine->engine_id;
1194         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] g_recording_engine_id : %d", g_recording_engine_id);
1195
1196         return 0;
1197 }
1198
1199 int sttd_engine_agent_set_recording_data(int uid, const void* data, unsigned int length)
1200 {
1201         if (false == g_agent_init) {
1202                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
1203                 return STTD_ERROR_OPERATION_FAILED;
1204         }
1205
1206         if (NULL == data || 0 == length) {
1207                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
1208                 return STTD_ERROR_INVALID_PARAMETER;
1209         }
1210
1211         sttengine_info_s* engine = NULL;
1212         engine = __engine_agent_get_engine_by_uid(uid);
1213
1214         if (NULL == engine) {
1215                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
1216                 return STTD_ERROR_INVALID_PARAMETER;
1217         }
1218
1219         if (false == engine->is_loaded) {
1220                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
1221                 return STTD_ERROR_OPERATION_FAILED;
1222         }
1223
1224         int ret = stt_engine_set_recording_data(engine->engine_id, data, length);
1225         if (0 != ret) {
1226                 SLOG(LOG_WARN, TAG_STTD, "[Engine Agent WARNING] set recording error(%d)", ret);
1227                 return STTD_ERROR_OPERATION_FAILED;
1228         }
1229
1230         return 0;
1231 }
1232
1233 int sttd_engine_agent_recognize_stop_recorder(int uid)
1234 {
1235         if (false == g_agent_init) {
1236                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
1237                 return STTD_ERROR_OPERATION_FAILED;
1238         }
1239
1240         sttengine_info_s* engine = NULL;
1241         engine = __engine_agent_get_engine_by_uid(uid);
1242
1243         if (NULL == engine) {
1244                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
1245                 return STTD_ERROR_INVALID_PARAMETER;
1246         }
1247
1248         if (false == engine->is_loaded) {
1249                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
1250                 return STTD_ERROR_OPERATION_FAILED;
1251         }
1252
1253         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Stop recorder");
1254         int ret;
1255         ret = sttd_recorder_stop(engine->engine_id);
1256         if (0 != ret) {
1257                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to stop recorder : result(%d)", ret);
1258                 return STTD_ERROR_OPERATION_FAILED;
1259         }
1260
1261 #ifdef AUDIO_CREATE_ON_START
1262         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Destroy recorder");
1263         if (0 != sttd_recorder_destroy(engine->engine_id))
1264                 SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to destroy recorder(%d)", engine->engine_id);
1265 #endif
1266
1267         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent Success] Stop recorder");
1268         return 0;
1269 }
1270
1271 int sttd_engine_agent_recognize_stop_engine(int uid)
1272 {
1273         if (false == g_agent_init) {
1274                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
1275                 return STTD_ERROR_OPERATION_FAILED;
1276         }
1277
1278         sttengine_info_s* engine = NULL;
1279         engine = __engine_agent_get_engine_by_uid(uid);
1280
1281         if (NULL == engine) {
1282                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
1283                 return STTD_ERROR_INVALID_PARAMETER;
1284         }
1285
1286         if (false == engine->is_loaded) {
1287                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
1288                 return STTD_ERROR_OPERATION_FAILED;
1289         }
1290
1291         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Stop engine");
1292
1293         int ret;
1294         ret = stt_engine_recognize_stop(engine->engine_id);
1295         if (0 != ret) {
1296                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] stop recognition error(%d)", ret);
1297                 return STTD_ERROR_OPERATION_FAILED;
1298         }
1299
1300         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent Success] Stop engine");
1301
1302         return 0;
1303 }
1304
1305 int sttd_engine_agent_recognize_cancel(int uid)
1306 {
1307         if (false == g_agent_init) {
1308                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
1309                 return STTD_ERROR_OPERATION_FAILED;
1310         }
1311
1312         sttengine_info_s* engine = NULL;
1313         engine = __engine_agent_get_engine_by_uid(uid);
1314
1315         if (NULL == engine) {
1316                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
1317                 return STTD_ERROR_INVALID_PARAMETER;
1318         }
1319
1320         if (false == engine->is_loaded) {
1321                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
1322                 return STTD_ERROR_OPERATION_FAILED;
1323         }
1324
1325         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Cancel engine");
1326
1327         int ret;
1328         ret = stt_engine_recognize_cancel(engine->engine_id);
1329         if (0 != ret) {
1330                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] cancel recognition error(%d)", ret);
1331                 return STTD_ERROR_OPERATION_FAILED;
1332         }
1333
1334         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Stop recorder");
1335
1336         ret = sttd_recorder_stop(engine->engine_id);
1337         if (0 != ret) {
1338                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to stop recorder : result(%d)", ret);
1339                 return STTD_ERROR_OPERATION_FAILED;
1340         }
1341
1342 #ifdef AUDIO_CREATE_ON_START
1343         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Destroy recorder");
1344         if (0 != sttd_recorder_destroy(engine->engine_id))
1345                 SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to destroy recorder(%d)", engine->engine_id);
1346 #endif
1347
1348         g_recording_engine_id = -1;
1349
1350         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent Success] Cancel recognition");
1351
1352         return 0;
1353 }
1354
1355
1356 /*
1357 * STT Engine Interfaces for configure
1358 */
1359
1360 int sttd_engine_agent_set_default_engine(const char* engine_uuid)
1361 {
1362         if (false == g_agent_init) {
1363                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
1364                 return STTD_ERROR_OPERATION_FAILED;
1365         }
1366
1367         if (NULL == engine_uuid) {
1368                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
1369                 return STTD_ERROR_INVALID_PARAMETER;
1370         }
1371
1372         __log_enginelist();
1373
1374         sttengine_info_s* engine;
1375         engine = __engine_agent_get_engine_by_uuid(engine_uuid);
1376         if (NULL == engine) {
1377                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Default engine is not valid");
1378                 return STTD_ERROR_ENGINE_NOT_FOUND;
1379         }
1380
1381         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Default engine id(%d) engine uuid(%s)", engine->engine_id, engine->engine_uuid);
1382
1383         g_default_engine_id = engine->engine_id;
1384
1385         /* Update default engine of client */
1386         GSList *iter = NULL;
1387         sttengine_client_s *data = NULL;
1388
1389         if (0 < g_slist_length(g_engine_client_list)) {
1390                 iter = g_slist_nth(g_engine_client_list, 0);
1391
1392                 while (NULL != iter) {
1393                         /* Get handle data from list */
1394                         data = iter->data;
1395
1396                         if (true == data->use_default_engine) {
1397                                 SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] uid(%d) change engine from id(%d) to id(%d)",
1398                                         data->uid, data->engine_id, engine->engine_id);
1399
1400                                 if (0 != sttd_engine_agent_load_current_engine(data->uid, NULL)) {
1401                                         SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to load current engine : uid(%d)", data->uid);
1402                                 }
1403                         }
1404
1405                         iter = g_slist_next(iter);
1406                 }
1407         }
1408
1409         return 0;
1410 }
1411
1412 int sttd_engine_agent_set_default_language(const char* language)
1413 {
1414         if (false == g_agent_init) {
1415                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
1416                 return STTD_ERROR_OPERATION_FAILED;
1417         }
1418
1419         if (NULL == language) {
1420                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
1421                 return STTD_ERROR_INVALID_PARAMETER;
1422         }
1423
1424         if (NULL != g_default_language)
1425                 free(g_default_language);
1426
1427         g_default_language = strdup(language);
1428
1429         return 0;
1430 }
1431
1432 int sttd_engine_agent_set_silence_detection(bool value)
1433 {
1434         if (false == g_agent_init) {
1435                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
1436                 return STTD_ERROR_OPERATION_FAILED;
1437         }
1438
1439         g_default_silence_detected = value;
1440
1441         return 0;
1442 }
1443
1444 int sttd_engine_agent_check_app_agreed(int uid, const char* appid, bool* result)
1445 {
1446         if (false == g_agent_init) {
1447                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
1448                 return STTD_ERROR_OPERATION_FAILED;
1449         }
1450
1451         sttengine_info_s* engine = NULL;
1452         engine = __engine_agent_get_engine_by_uid(uid);
1453
1454         if (NULL == engine) {
1455                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
1456                 return STTD_ERROR_INVALID_PARAMETER;
1457         }
1458
1459         if (false == engine->is_loaded) {
1460                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
1461                 return STTD_ERROR_OPERATION_FAILED;
1462         }
1463
1464         int ret;
1465         ret = stt_engine_check_app_agreed(engine->engine_id, appid, result);
1466         if (0 != ret) {
1467                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] cancel recognition error(%d)", ret);
1468                 return STTD_ERROR_OPERATION_FAILED;
1469         }
1470
1471
1472         SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Get engine right : %s", *result ? "true" : "false");
1473         return 0;
1474 }
1475
1476 /*
1477 * STT Engine Callback Functions                                                                                 `                                 *
1478 */
1479
1480 void __result_cb(sttp_result_event_e event, const char* type, const char** data, int data_count,
1481                  const char* msg, void* time_info, void *user_data)
1482 {
1483         if (false == g_agent_init) {
1484                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Result Callback : Not Initialized");
1485                 return;
1486         }
1487
1488         SLOG(LOG_DEBUG, TAG_STTD, "[Server] === Result time callback ===");
1489
1490         if (NULL != time_info) {
1491                 /* Get the time info */
1492                 int ret = stt_engine_foreach_result_time(g_recording_engine_id, time_info, __result_time_cb, NULL);
1493                 if (0 != ret) {
1494                         SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get time info : %d", ret);
1495                 }
1496         }
1497
1498         SLOG(LOG_DEBUG, TAG_STTD, "[Server] ============================");
1499
1500         g_result_cb(event, type, data, data_count, msg, user_data);
1501
1502 #ifdef AUDIO_CREATE_ON_START
1503         if (event == STTP_RESULT_EVENT_ERROR) {
1504                 SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Destroy recorder");
1505                 if (0 != sttd_recorder_destroy(g_recording_engine_id))
1506                         SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to destroy recorder(%d)", g_recording_engine_id);
1507         }
1508 #endif
1509
1510         if (event == STTP_RESULT_EVENT_FINAL_RESULT || event == STTP_RESULT_EVENT_ERROR) {
1511                 g_recording_engine_id = -1;
1512         }
1513
1514         return;
1515 }
1516
1517 bool __result_time_cb(int index, sttp_result_time_event_e event, const char* text, long start_time, long end_time, void* user_data)
1518 {
1519         return g_result_time_cb(index, event, text, start_time, end_time, user_data);
1520 }
1521
1522 void __detect_silence_cb(sttp_silence_type_e type, void* user_data)
1523 {
1524         if (false == g_agent_init) {
1525                 SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Silence Callback : Not Initialized");
1526                 return;
1527         }
1528
1529         g_silence_cb(type, user_data);
1530         return;
1531 }
1532
1533 /* A function forging */
1534 int __log_enginelist()
1535 {
1536         GSList *iter = NULL;
1537         sttengine_info_s *data = NULL;
1538
1539         if (0 < g_slist_length(g_engine_list)) {
1540
1541                 /* Get a first item */
1542                 iter = g_slist_nth(g_engine_list, 0);
1543
1544                 SLOG(LOG_DEBUG, TAG_STTD, "--------------- engine list -------------------");
1545
1546                 int i = 1;
1547                 while (NULL != iter) {
1548                         /* Get handle data from list */
1549                         data = iter->data;
1550
1551                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[%dth]", i);
1552                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "  engine uuid : %s", data->engine_uuid);
1553                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "  engine name : %s", data->engine_name);
1554                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "  engine path : %s", data->engine_path);
1555                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "  use network : %s", data->use_network ? "true" : "false");
1556                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "  is loaded : %s", data->is_loaded ? "true" : "false");
1557                         if (NULL != data->first_lang)
1558                                 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "  default lang : %s", data->first_lang);
1559
1560                         iter = g_slist_next(iter);
1561                         i++;
1562                 }
1563                 SLOG(LOG_DEBUG, TAG_STTD, "----------------------------------------------");
1564         } else {
1565                 SLOG(LOG_DEBUG, TAG_STTD, "-------------- engine list -------------------");
1566                 SLOG(LOG_DEBUG, TAG_STTD, "  No Engine in engine directory");
1567                 SLOG(LOG_DEBUG, TAG_STTD, "----------------------------------------------");
1568         }
1569
1570         return 0;
1571 }