Implement stt default engine setting and Fix directories in stt engine parser
[platform/core/uifw/stt.git] / server / sttd_server.c
1 /*
2 *  Copyright (c) 2011-2016 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 #include <pthread.h>
15 #include <sound_manager.h>
16 #include <wav_player.h>
17
18 #include "stt_network.h"
19 #include "sttd_client_data.h"
20 #include "sttd_config.h"
21 #include "sttd_dbus.h"
22 #include "sttd_engine_agent.h"
23 #include "sttd_main.h"
24 #include "sttd_recorder.h"
25 #include "sttd_server.h"
26
27 static pthread_mutex_t stte_result_mutex = PTHREAD_MUTEX_INITIALIZER;
28 static pthread_mutex_t stte_result_time_mutex = PTHREAD_MUTEX_INITIALIZER;
29
30
31 /*
32 * STT Server static variable
33 */
34 static double g_processing_timeout = 30;
35
36 static double g_recording_timeout = 60;
37
38 Ecore_Timer*    g_recording_timer = NULL;
39 Ecore_Timer*    g_processing_timer = NULL;
40
41 static int g_recording_log_count = 0;
42
43 static GList *g_proc_list = NULL;
44
45 /*
46 * STT Server Callback Functions
47 */
48 void __stop_by_silence(void *data)
49 {
50         SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by silence detection");
51
52         int uid = 0;
53
54         uid = stt_client_get_current_recognition();
55
56         int ret;
57         if (0 != uid) {
58                 ret = sttd_server_stop(uid);
59                 if (0 > ret) {
60                         return;
61                 }
62
63                 if (STTD_RESULT_STATE_DONE == ret) {
64                         ret = sttdc_send_set_state(uid, (int)APP_STATE_PROCESSING);
65                         if (0 != ret) {
66                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send state : result(%d)", ret);
67
68                                 /* Remove client */
69                                 sttd_server_finalize(uid);
70                                 stt_client_unset_current_recognition();
71                         }
72                 }
73         } else {
74                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
75         }
76
77         SLOG(LOG_DEBUG, TAG_STTD, "=====");
78         SLOG(LOG_DEBUG, TAG_STTD, "  ");
79
80         return;
81 }
82
83 static void __cancel_recognition_internal()
84 {
85         if (NULL != g_recording_timer)  {
86                 ecore_timer_del(g_recording_timer);
87                 g_recording_timer = NULL;
88         }
89
90         int ret = 0;
91         int uid = 0;
92         uid = stt_client_get_current_recognition();
93
94         app_state_e state = 0;
95         ret = sttd_client_get_state(uid, &state);
96
97         if (0 != ret) {
98                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
99                 return;
100         }
101
102         if (0 != uid && (APP_STATE_PROCESSING == state || APP_STATE_RECORDING == state)) {
103                 /* cancel engine recognition */
104                 ret = sttd_server_cancel(uid);
105                 if (0 != ret) {
106                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
107                 }
108         } else {
109                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid is NOT valid");
110         }
111 }
112
113 static void __cancel_by_error(void *data)
114 {
115         SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by error");
116
117         __cancel_recognition_internal();
118
119         SLOG(LOG_DEBUG, TAG_STTD, "=====");
120         SLOG(LOG_DEBUG, TAG_STTD, "  ");
121
122         return;
123 }
124
125 int __server_audio_recorder_callback(const void* data, const unsigned int length)
126 {
127         int uid = -1;
128         int ret;
129
130         if (NULL == data || 0 == length) {
131                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Recording data is not valid");
132                 ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
133                 return -1;
134         }
135
136         uid = stt_client_get_current_recognition();
137         if (0 != uid) {
138                 ret = sttd_engine_agent_set_recording_data(data, length);
139                 if (ret < 0) {
140                         ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
141                         return -1;
142                 }
143                 g_recording_log_count++;
144                 if (200 <= g_recording_log_count) {
145                         SLOG(LOG_DEBUG, TAG_STTD, "=== Set recording data ===");
146                         g_recording_log_count = 0;
147                 }
148         } else {
149                 if (NULL != g_recording_timer)  {
150                         ecore_timer_del(g_recording_timer);
151                         g_recording_timer = NULL;
152                 }
153                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current uid in recording is is not valid");
154                 return -1;
155         }
156
157         return 0;
158 }
159
160 void __server_audio_interrupt_callback()
161 {
162         SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by sound interrupt");
163
164         __cancel_recognition_internal();
165
166         SLOG(LOG_DEBUG, TAG_STTD, "=====");
167         SLOG(LOG_DEBUG, TAG_STTD, "  ");
168 }
169
170 void __cancel_by_no_record(void *data)
171 {
172         SLOG(LOG_DEBUG, TAG_STTD, "===== Cancel by no record");
173
174         __cancel_recognition_internal();
175
176         SLOG(LOG_DEBUG, TAG_STTD, "=====");
177         SLOG(LOG_DEBUG, TAG_STTD, "  ");
178
179         return;
180 }
181
182 int __server_recognition_result_callback(stte_result_event_e event, const char* type,
183                                         const char** data, int data_count, const char* msg, void *user_data)
184 {
185         // critical section
186         pthread_mutex_lock(&stte_result_mutex);
187
188         SLOG(LOG_DEBUG, TAG_STTD, "===== RESULT event[%d] type[%s] data[%p] data_count[%d]", event, type, data, data_count);
189
190         /* check uid */
191         int uid = stt_client_get_current_recognition();
192
193         app_state_e state;
194         if (0 == uid || 0 != sttd_client_get_state(uid, &state)) {
195                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
196                 SLOG(LOG_DEBUG, TAG_STTD, "=====");
197                 SLOG(LOG_DEBUG, TAG_STTD, "  ");
198                 pthread_mutex_unlock(&stte_result_mutex);
199                 return STTD_ERROR_OPERATION_FAILED;
200         }
201
202         SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event);
203
204         /* send result to client */
205         if (STTE_RESULT_EVENT_FINAL_RESULT == event) {
206                 if (APP_STATE_PROCESSING != state) {
207                         SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is NOT 'Processing'.");
208                 }
209                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] the size of result from engine is '%d'", data_count);
210
211                 /* Delete timer for processing time out */
212                 if (NULL != g_processing_timer) {
213                         ecore_timer_del(g_processing_timer);
214                         g_processing_timer = NULL;
215                 }
216
217                 sttd_config_time_save();
218                 sttd_config_time_reset();
219
220                 sttd_client_set_state(uid, APP_STATE_READY);
221                 stt_client_unset_current_recognition();
222
223                 if (NULL == data || 0 == data_count) {
224                         if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
225                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
226                                 int reason = (int)STTD_ERROR_OPERATION_FAILED;
227
228                                 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
229                                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
230                                 }
231                         }
232                 } else {
233                         if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
234                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
235                                 int reason = (int)STTD_ERROR_OPERATION_FAILED;
236
237                                 if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
238                                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
239                                 }
240                         }
241                 }
242
243                 /* change state of uid */
244 //              sttd_client_set_state(uid, APP_STATE_READY);
245 //              stt_client_unset_current_recognition();
246
247         } else if (STTE_RESULT_EVENT_PARTIAL_RESULT == event) {
248                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The partial result from engine is event[%d] data_count[%d]", event,  data_count);
249
250                 sttd_config_time_save();
251                 sttd_config_time_reset();
252
253                 if (0 != sttdc_send_result(uid, event, data, data_count, msg)) {
254                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");
255                         int reason = (int)STTD_ERROR_OPERATION_FAILED;
256
257                         if (0 != sttdc_send_error_signal(uid, reason, "Fail to send recognition result")) {
258                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data");
259                         }
260                 }
261
262         } else if (STTE_RESULT_EVENT_ERROR == event) {
263                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The event of recognition result is ERROR");
264
265                 /* Delete timer for processing time out */
266                 if (NULL != g_processing_timer) {
267                         ecore_timer_del(g_processing_timer);
268                         g_processing_timer = NULL;
269                 }
270                 sttd_config_time_reset();
271
272                 int ret = 0;
273                 if (APP_STATE_RECORDING == state) {
274                         ret = sttd_engine_agent_recognize_cancel();
275                         if (0 != ret) {
276                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel: result(%d)", ret);
277                         }
278                 }
279
280                 sttd_client_set_state(uid, APP_STATE_READY);
281                 stt_client_unset_current_recognition();
282
283                 if (0 != sttdc_send_result(uid, event, NULL, 0, msg)) {
284                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
285
286                         /* send error msg */
287                         int reason = (int)STTD_ERROR_INVALID_STATE;
288                         if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
289                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
290                         }
291                 }
292
293                 /* change state of uid */
294 //              sttd_client_set_state(uid, APP_STATE_READY);
295 //              stt_client_unset_current_recognition();
296         } else {
297                 /* nothing */
298         }
299
300         SLOG(LOG_DEBUG, TAG_STTD, "=====");
301         SLOG(LOG_DEBUG, TAG_STTD, "  ");
302         pthread_mutex_unlock(&stte_result_mutex);
303
304         return STTD_ERROR_NONE;
305 }
306
307 bool __server_result_time_callback(int index, stte_result_time_event_e event, const char* text, long start_time, long end_time, void* user_data)
308 {
309         pthread_mutex_lock(&stte_result_time_mutex);
310
311         SLOG(LOG_DEBUG, TAG_STTD, "[Server] index(%d) event(%d) text(%s) start(%ld) end(%ld)",
312                 index, event, text, start_time, end_time);
313
314         int ret;
315         ret = sttd_config_time_add(index, (int)event, text, start_time, end_time);
316         if (0 != ret) {
317                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add time info");
318                 pthread_mutex_unlock(&stte_result_time_mutex);
319                 return false;
320         }
321
322         pthread_mutex_unlock(&stte_result_time_mutex);
323
324         return true;
325 }
326
327 int __server_speech_status_callback(stte_speech_status_e status, void *user_param)
328 {
329         SLOG(LOG_DEBUG, TAG_STTD, "===== Speech status detected Callback");
330
331         int uid = stt_client_get_current_recognition();
332         if (0 != uid) {
333                 app_state_e state;
334                 if (0 != sttd_client_get_state(uid, &state)) {
335                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid ");
336                         return STTD_ERROR_OPERATION_FAILED;
337                 }
338
339                 if (APP_STATE_RECORDING != state) {
340                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
341                         return STTD_ERROR_INVALID_STATE;
342                 }
343
344                 if (STTE_SPEECH_STATUS_BEGINNING_POINT_DETECTED == status) {
345                         SLOG(LOG_DEBUG, TAG_STTD, "Begin Speech detected");
346                         sttdc_send_speech_status(uid, status);
347                 } else if (STTE_SPEECH_STATUS_END_POINT_DETECTED == status) {
348                         SLOG(LOG_DEBUG, TAG_STTD, "End Speech detected");
349                         ecore_main_loop_thread_safe_call_async(__stop_by_silence, NULL);
350                 }
351         } else {
352                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current recogntion uid is not valid ");
353         }
354
355         SLOG(LOG_DEBUG, TAG_STTD, "=====");
356         SLOG(LOG_DEBUG, TAG_STTD, "  ");
357
358         return STTD_ERROR_NONE;
359 }
360
361 int __server_error_callback(stte_error_e error, const char* msg)
362 {
363         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Error Callback is called");
364         ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
365
366         return STTD_ERROR_NONE;
367 }
368
369 void __sttd_server_engine_changed_cb(const char* engine_id, const char* language, bool support_silence, bool need_credential, void* user_data)
370 {
371         if (NULL == engine_id) {
372                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Engine id is NULL");
373                 return;
374         } else {
375                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] New default engine : %s", engine_id);
376         }
377
378 #if 0
379         /* need to change state of app to ready */
380         int uid;
381         uid = stt_client_get_current_recognition();
382
383         if (0 != uid) {
384                 SLOG(LOG_ERROR, TAG_STTD, "[Server] Set ready state of uid(%d)", uid);
385
386                 sttd_server_cancel(uid);
387                 sttdc_send_set_state(uid, (int)APP_STATE_READY);
388
389                 stt_client_unset_current_recognition();
390         }
391
392         /* set engine */
393         int ret = sttd_engine_agent_set_default_engine(engine_id);
394         if (0 != ret)
395                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret);
396
397         if (NULL != language) {
398                 ret = sttd_engine_agent_set_default_language(language);
399                 if (0 != ret)
400                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
401         }
402
403         ret = sttd_engine_agent_set_silence_detection(support_silence);
404         if (0 != ret)
405                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
406 #endif
407
408         return;
409 }
410
411 void __sttd_server_language_changed_cb(const char* language, void* user_data)
412 {
413         if (NULL == language) {
414                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL");
415                 return;
416         } else {
417                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get language changed : %s", language);
418         }
419
420         int ret = sttd_engine_agent_set_default_language(language);
421         if (0 != ret)
422                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret);
423
424         return;
425 }
426
427 void __sttd_server_silence_changed_cb(bool value, void* user_data)
428 {
429         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Get silence detection changed : %s", value ? "on" : "off");
430
431         int ret = 0;
432         ret = sttd_engine_agent_set_silence_detection(value);
433         if (0 != ret)
434                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret);
435
436         return;
437 }
438
439 /*
440 * Daemon function
441 */
442 int sttd_initialize(stte_request_callback_s *callback)
443 {
444         int ret = 0;
445
446         if (0 != pthread_mutex_init(&stte_result_mutex, NULL)) {
447                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte result mutex.");
448         }
449
450         if (0 != pthread_mutex_init(&stte_result_time_mutex, NULL)) {
451                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize stte stte_result_time_mutex.");
452         }
453
454         if (sttd_config_initialize(__sttd_server_engine_changed_cb, __sttd_server_language_changed_cb,
455                 __sttd_server_silence_changed_cb, NULL)) {
456                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize config.");
457         }
458
459         ret = sttd_recorder_initialize(__server_audio_recorder_callback, __server_audio_interrupt_callback);
460         if (0 != ret) {
461                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret);
462                 return ret;
463         }
464
465         /* Engine Agent initialize */
466         ret = sttd_engine_agent_init(__server_recognition_result_callback, __server_result_time_callback,
467                 __server_speech_status_callback, __server_error_callback);
468         if (0 != ret) {
469                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
470                 return ret;
471         }
472
473         /* load engine */
474         ret = sttd_engine_agent_load_current_engine(callback);
475         if (0 != ret) {
476                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load default engine");
477                 return ret;
478         }
479
480         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize");
481
482         return 0;
483 }
484
485 int sttd_finalize()
486 {
487         if (0 != pthread_mutex_destroy(&stte_result_mutex)) {
488                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte result mutex.");
489         }
490
491         if (0 != pthread_mutex_destroy(&stte_result_time_mutex)) {
492                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy stte_result_time_mutex.");
493         }
494
495         GList *iter = NULL;
496         if (0 < g_list_length(g_proc_list)) {
497                 iter = g_list_first(g_proc_list);
498                 while (NULL != iter) {
499                         g_proc_list = g_list_remove_link(g_proc_list, iter);
500                         iter = g_list_first(g_proc_list);
501                 }
502         }
503
504         sttd_recorder_deinitialize();
505
506         sttd_config_finalize();
507
508         sttd_engine_agent_release();
509
510         return STTD_ERROR_NONE;
511 }
512
513 static void __read_proc()
514 {
515         DIR *dp = NULL;
516         struct dirent *dirp = NULL;
517         int tmp;
518
519         GList *iter = NULL;
520         if (0 < g_list_length(g_proc_list)) {
521                 iter = g_list_first(g_proc_list);
522                 while (NULL != iter) {
523                         g_proc_list = g_list_remove_link(g_proc_list, iter);
524                         iter = g_list_first(g_proc_list);
525                 }
526         }
527
528         dp = opendir("/proc");
529         if (NULL == dp) {
530                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Fail to open proc");
531         } else {
532                 do {
533                         dirp = readdir(dp);
534
535                         if (NULL != dirp) {
536                                 tmp = atoi(dirp->d_name);
537                                 if (0 >= tmp)   continue;
538                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
539                         }
540                 } while (NULL != dirp);
541                 closedir(dp);
542         }
543         return;
544 }
545
546 Eina_Bool sttd_cleanup_client(void *data)
547 {
548         int* client_list = NULL;
549         int client_count = 0;
550         int i = 0;
551         int j = 0;
552         bool exist = false;
553
554         if (0 != sttd_client_get_list(&client_list, &client_count)) {
555                 if (NULL != client_list)
556                         free(client_list);
557
558                 return EINA_TRUE;
559         }
560
561         if (NULL != client_list) {
562                 SLOG(LOG_DEBUG, TAG_STTD, "===== Clean up client ");
563
564                 __read_proc();
565
566                 for (i = 0; i < client_count; i++) {
567                         int pid = sttd_client_get_pid(client_list[i]);
568                         if (0 > pid) {
569                                 SLOG(LOG_ERROR, TAG_STTD, "[ERROR] Invalid pid");
570                                 continue;
571                         }
572
573                         exist = false;
574                         GList *iter = NULL;
575                         for (j = 0; j < g_list_length(g_proc_list); j++) {
576                                 iter = g_list_nth(g_proc_list, j);
577                                 if (NULL != iter) {
578                                         if (pid == GPOINTER_TO_INT(iter->data)) {
579                                                 SLOG(LOG_DEBUG, TAG_STTD, "uid (%d) is running", client_list[i]);
580                                                 exist = true;
581                                                 break;
582                                         }
583                                 }
584                         }
585
586                         if (false == exist) {
587                                 SLOG(LOG_ERROR, TAG_STTD, "uid (%d) should be removed", client_list[i]);
588                                 sttd_server_finalize(client_list[i]);
589                         }
590 #if 0
591                         result = sttdc_send_hello(client_list[i]);
592
593                         if (0 == result) {
594                                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", client_list[i]);
595                                 sttd_server_finalize(client_list[i]);
596                         } else if (-1 == result) {
597                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Hello result has error");
598                         }
599 #endif
600                 }
601
602                 SLOG(LOG_DEBUG, TAG_STTD, "=====");
603                 SLOG(LOG_DEBUG, TAG_STTD, "  ");
604
605                 free(client_list);
606         }
607
608         return EINA_TRUE;
609 }
610
611 /*
612 * STT Server Functions for Client
613 */
614
615 int sttd_server_initialize(int pid, int uid, bool* silence, bool* credential)
616 {
617         int ret = STTD_ERROR_NONE;
618
619         /* check if uid is valid */
620         app_state_e state;
621         if (0 == sttd_client_get_state(uid, &state)) {
622                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] uid has already been registered");
623                 return STTD_ERROR_NONE;
624         }
625
626         ret = sttd_engine_agent_get_option_supported(silence);
627         if (0 != ret) {
628                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine options supported");
629                 return ret;
630         }
631
632         ret = sttd_engine_agent_is_credential_needed(uid, credential);
633         if (0 != ret) {
634                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
635                 return ret;
636         }
637
638         /* Add client information to client manager */
639         ret = sttd_client_add(pid, uid);
640         if (0 != ret) {
641                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info");
642                 return ret;
643         }
644
645         return STTD_ERROR_NONE;
646 }
647
648 static Eina_Bool __quit_ecore_loop(void *data)
649 {
650         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Quit");
651
652         stt_network_finalize();
653         sttd_finalize();
654         sttd_dbus_close_connection();
655         ecore_main_loop_quit();
656
657         SLOG(LOG_DEBUG, TAG_STTD, "");
658
659         return EINA_FALSE;
660 }
661
662 int sttd_server_finalize(int uid)
663 {
664         /* check if uid is valid */
665         app_state_e state;
666         if (0 != sttd_client_get_state(uid, &state)) {
667                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
668                 return STTD_ERROR_INVALID_PARAMETER;
669         }
670
671         /* release recorder */
672         if (APP_STATE_RECORDING == state || APP_STATE_PROCESSING == state) {
673                 if (APP_STATE_RECORDING == state) {
674                         if (NULL != g_recording_timer)  {
675                                 ecore_timer_del(g_recording_timer);
676                                 g_recording_timer = NULL;
677                         }
678                 }
679
680                 if (APP_STATE_PROCESSING == state) {
681                         if (NULL != g_processing_timer) {
682                                 ecore_timer_del(g_processing_timer);
683                                 g_processing_timer = NULL;
684                         }
685                 }
686
687                 if (0 != sttd_engine_agent_recognize_cancel(uid)) {
688                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognition");
689                 }
690
691                 stt_client_unset_current_recognition();
692         }
693
694         /* Remove client information */
695         if (0 != sttd_client_delete(uid)) {
696                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client");
697         }
698
699         /* unload engine, if ref count of client is 0 */
700         if (0 == sttd_client_get_ref_count()) {
701 //              sttd_dbus_close_connection();
702                 ecore_timer_add(0, __quit_ecore_loop, NULL);
703         }
704
705         return STTD_ERROR_NONE;
706 }
707
708 int sttd_server_get_supported_engines(int uid, GSList** engine_list)
709 {
710         /* Check if uid is valid */
711         app_state_e state;
712         if (0 != sttd_client_get_state(uid, &state)) {
713                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
714                 return STTD_ERROR_INVALID_PARAMETER;
715         }
716
717         /* Check state of uid */
718         if (APP_STATE_READY != state) {
719                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
720                 return STTD_ERROR_INVALID_STATE;
721         }
722
723         int ret;
724         ret = sttd_engine_agent_get_engine_list(engine_list);
725         if (0 != ret) {
726                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list");
727                 return ret;
728         }
729
730         return STTD_ERROR_NONE;
731 }
732
733 int sttd_server_set_current_engine(int uid, const char* engine_id, bool* silence, bool* credential)
734 {
735         /* Check if uid is valid */
736         app_state_e state;
737         if (0 != sttd_client_get_state(uid, &state)) {
738                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
739                 return STTD_ERROR_INVALID_PARAMETER;
740         }
741
742         /* Check state of uid */
743         if (APP_STATE_READY != state) {
744                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
745                 return STTD_ERROR_INVALID_STATE;
746         }
747
748         int ret;
749         ret = sttd_engine_agent_load_current_engine(NULL);
750         if (0 != ret) {
751                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : %d", ret);
752                 return ret;
753         }
754
755         ret = sttd_engine_agent_get_option_supported(silence);
756         if (0 != ret) {
757                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine options : %d", ret);
758                 return ret;
759         }
760
761         if (0 != sttd_engine_agent_is_credential_needed(uid, credential)) {
762                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get credential necessity");
763                 return ret;
764         }
765
766         return STTD_ERROR_NONE;
767 }
768
769 int sttd_server_get_current_engine(int uid, char** engine_id)
770 {
771         /* Check if uid is valid */
772         app_state_e state;
773         if (0 != sttd_client_get_state(uid, &state)) {
774                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
775                 return STTD_ERROR_INVALID_PARAMETER;
776         }
777
778         /* Check state of uid */
779         if (APP_STATE_READY != state) {
780                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
781                 return STTD_ERROR_INVALID_STATE;
782         }
783
784         int ret;
785         ret = sttd_engine_agent_get_current_engine(engine_id);
786         if (0 != ret) {
787                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : %d", ret);
788                 return ret;
789         }
790
791         return STTD_ERROR_NONE;
792 }
793
794 int sttd_server_check_app_agreed(int uid, const char* appid, bool* available)
795 {
796         /* Check if uid is valid */
797         app_state_e state;
798         if (0 != sttd_client_get_state(uid, &state)) {
799                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
800                 return STTD_ERROR_INVALID_PARAMETER;
801         }
802
803         /* Check state of uid */
804         if (APP_STATE_READY != state) {
805                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] The state of uid(%d) is not Ready", uid);
806                 return STTD_ERROR_INVALID_STATE;
807         }
808
809         /* Ask engine available */
810         int ret;
811         bool temp = false;
812         ret = sttd_engine_agent_check_app_agreed(appid, &temp);
813         if (0 != ret) {
814                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
815                 return ret;
816         }
817
818         if (true == temp) {
819                 stt_client_set_app_agreed(uid);
820                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] App(%s) confirmed that engine is available", appid);
821         }
822
823         *available = temp;
824
825         return STTD_ERROR_NONE;
826 }
827
828
829 int sttd_server_get_supported_languages(int uid, GSList** lang_list)
830 {
831         /* check if uid is valid */
832         app_state_e state;
833         if (0 != sttd_client_get_state(uid, &state)) {
834                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
835                 return STTD_ERROR_INVALID_PARAMETER;
836         }
837
838         /* get language list from engine */
839         int ret = sttd_engine_agent_supported_langs(lang_list);
840         if (0 != ret) {
841                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages");
842                 return ret;
843         }
844
845         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages");
846
847         return STTD_ERROR_NONE;
848 }
849
850 int sttd_server_get_current_langauage(int uid, char** current_lang)
851 {
852         /* check if uid is valid */
853         app_state_e state;
854         if (0 != sttd_client_get_state(uid, &state)) {
855                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
856                 return STTD_ERROR_INVALID_PARAMETER;
857         }
858
859         if (NULL == current_lang) {
860                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
861                 return STTD_ERROR_INVALID_PARAMETER;
862         }
863
864         /*get current language from engine */
865         int ret = sttd_engine_agent_get_default_lang(current_lang);
866         if (0 != ret) {
867                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret);
868                 return ret;
869         }
870
871         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get default language");
872
873         return STTD_ERROR_NONE;
874 }
875
876 int sttd_server_set_private_data(int uid, const char* key, const char* data)
877 {
878         /* check if uid is valid */
879         app_state_e state;
880         if (0 != sttd_client_get_state(uid, &state)) {
881                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
882                 return STTD_ERROR_INVALID_PARAMETER;
883         }
884
885         if (NULL == key || NULL == data) {
886                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
887                 return STTD_ERROR_INVALID_PARAMETER;
888         }
889
890         /* set private data to engine */
891         int ret = -1;
892         ret = sttd_engine_agent_set_private_data(key, data);
893         if (0 != ret) {
894                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set private data :result(%d)", ret);
895                 return ret;
896         }
897
898         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Set private data");
899
900         return STTD_ERROR_NONE;
901 }
902
903 int sttd_server_get_private_data(int uid, const char* key, char** data)
904 {
905         /* check if uid is valid */
906         app_state_e state;
907         if (0 != sttd_client_get_state(uid, &state)) {
908                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
909                 return STTD_ERROR_INVALID_PARAMETER;
910         }
911
912         if (NULL == key || NULL == data) {
913                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
914                 return STTD_ERROR_INVALID_PARAMETER;
915         }
916
917         /* get private data to engine */
918         int ret = -1;
919         ret = sttd_engine_agent_get_private_data(key, data);
920         if (0 != ret) {
921                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get private data :result(%d)", ret);
922                 return ret;
923         }
924
925         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Get private data, key(%s), data(%s)", key, *data);
926
927         return STTD_ERROR_NONE;
928 }
929
930 int sttd_server_is_recognition_type_supported(int uid, const char* type, int* support)
931 {
932         /* check if uid is valid */
933         app_state_e state;
934         if (0 != sttd_client_get_state(uid, &state)) {
935                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
936                 return STTD_ERROR_INVALID_PARAMETER;
937         }
938
939         if (NULL == type || NULL == support) {
940                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
941                 return STTD_ERROR_INVALID_PARAMETER;
942         }
943
944         bool temp;
945         int ret = sttd_engine_agent_is_recognition_type_supported(type, &temp);
946         if (0 != ret) {
947                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get recognition type supported : result(%d)", ret);
948                 return ret;
949         }
950
951         *support = (int)temp;
952
953         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] recognition type supporting is %s", *support ? "true" : "false");
954
955         return STTD_ERROR_NONE;
956 }
957
958 int sttd_server_set_start_sound(int uid, const char* file)
959 {
960         /* check if uid is valid */
961         app_state_e state;
962         if (0 != sttd_client_get_state(uid, &state)) {
963                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
964                 return STTD_ERROR_INVALID_PARAMETER;
965         }
966
967         int ret = sttd_client_set_start_sound(uid, file);
968         if (0 != ret) {
969                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
970                 return ret;
971         }
972
973         return 0;
974 }
975
976 int sttd_server_set_stop_sound(int uid, const char* file)
977 {
978         /* check if uid is valid */
979         app_state_e state;
980         if (0 != sttd_client_get_state(uid, &state)) {
981                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
982                 return STTD_ERROR_INVALID_PARAMETER;
983         }
984
985         int ret = sttd_client_set_stop_sound(uid, file);
986         if (0 != ret) {
987                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set start sound file : result(%d)", ret);
988                 return ret;
989         }
990
991         return 0;
992 }
993
994 #if 0
995 Eina_Bool __check_recording_state(void *data)
996 {
997         /* current uid */
998         int uid = stt_client_get_current_recognition();
999         if (0 == uid)
1000                 return EINA_FALSE;
1001
1002         app_state_e state;
1003         if (0 != sttdc_send_get_state(uid, (int*)&state)) {
1004                 /* client is removed */
1005                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid(%d) should be removed.", uid);
1006                 sttd_server_finalize(uid);
1007                 return EINA_FALSE;
1008         }
1009
1010         if (APP_STATE_READY == state) {
1011                 /* Cancel stt */
1012                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Ready'. The STT service should cancel recording", uid);
1013                 sttd_server_cancel(uid);
1014         } else if (APP_STATE_PROCESSING == state) {
1015                 /* Cancel stt and send change state */
1016                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The state of uid(%d) is 'Processing'. The STT service should cancel recording", uid);
1017                 sttd_server_cancel(uid);
1018                 sttdc_send_set_state(uid, (int)APP_STATE_READY);
1019         } else {
1020                 /* Normal state */
1021                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] The states of STT service and client are identical");
1022                 return EINA_TRUE;
1023         }
1024
1025         return EINA_FALSE;
1026 }
1027 #endif
1028
1029 Eina_Bool __stop_by_recording_timeout(void *data)
1030 {
1031         SLOG(LOG_DEBUG, TAG_STTD, "===== Stop by timeout");
1032
1033         g_recording_timer = NULL;
1034
1035         int uid = 0;
1036         uid = stt_client_get_current_recognition();
1037         if (0 != uid) {
1038                 /* cancel engine recognition */
1039                 int ret = sttd_server_stop(uid);
1040                 if (0 != ret) {
1041                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret);
1042                 }
1043         }
1044
1045         SLOG(LOG_DEBUG, TAG_STTD, "=====");
1046         SLOG(LOG_DEBUG, TAG_STTD, "  ");
1047
1048         return EINA_FALSE;
1049 }
1050
1051 void __sttd_server_recorder_start(void* data)
1052 {
1053         intptr_t puid = (intptr_t)data;
1054         int uid = (int)puid;
1055         int current_uid = stt_client_get_current_recognition();
1056
1057         if (uid != current_uid) {
1058                 stt_client_unset_current_recognition();
1059                 return;
1060         }
1061
1062         int ret = sttd_engine_agent_recognize_start_recorder(uid);
1063         if (0 != ret) {
1064                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1065                 return;
1066         }
1067
1068         /* Notify uid state change */
1069         sttdc_send_set_state(uid, APP_STATE_RECORDING);
1070
1071         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1072 }
1073
1074 void __sttd_start_sound_completed_cb(int id, void *user_data)
1075 {
1076         SLOG(LOG_DEBUG, TAG_STTD, "===== Start sound completed");
1077
1078         /* After wav play callback, recorder start */
1079         ecore_main_loop_thread_safe_call_async(__sttd_server_recorder_start, user_data);
1080
1081         SLOG(LOG_DEBUG, TAG_STTD, "=====");
1082         SLOG(LOG_DEBUG, TAG_STTD, "  ");
1083         return;
1084 }
1085
1086 int sttd_server_start(int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
1087 {
1088         if (NULL == lang || NULL == recognition_type) {
1089                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
1090                 return STTD_ERROR_INVALID_PARAMETER;
1091         }
1092
1093         /* check if uid is valid */
1094         app_state_e state;
1095         if (0 != sttd_client_get_state(uid, &state)) {
1096                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1097                 return STTD_ERROR_INVALID_PARAMETER;
1098         }
1099
1100         /* check uid state */
1101         if (APP_STATE_READY != state) {
1102                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
1103                 return STTD_ERROR_INVALID_STATE;
1104         }
1105
1106         int ret = 0;
1107         if (false == stt_client_get_app_agreed(uid)) {
1108                 bool temp = false;
1109                 ret = sttd_engine_agent_check_app_agreed(appid, &temp);
1110                 if (0 != ret) {
1111                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
1112                         return ret;
1113                 }
1114
1115                 if (false == temp) {
1116                         SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
1117                         return STTD_ERROR_PERMISSION_DENIED;
1118                 }
1119
1120                 stt_client_set_app_agreed(uid);
1121         }
1122
1123         /* check if engine use network */
1124         if (true == sttd_engine_agent_need_network()) {
1125                 if (false == stt_network_is_connected()) {
1126                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
1127                         return STTD_ERROR_OUT_OF_NETWORK;
1128                 }
1129         }
1130
1131         char* sound = NULL;
1132         ret = sttd_client_get_start_sound(uid, &sound);
1133         if (0 != ret) {
1134                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1135                 return ret;
1136         }
1137
1138         if (0 != stt_client_set_current_recognition(uid)) {
1139                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT is busy because of recording or processing");
1140                 if (NULL != sound)      free(sound);
1141                 return STTD_ERROR_RECORDER_BUSY;
1142         }
1143
1144         /* engine start recognition */
1145         SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)",
1146                         uid, lang, recognition_type);
1147         if (NULL != sound)
1148                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] start sound : %s", sound);
1149
1150         /* 1. Set audio session */
1151         ret = sttd_recorder_set_audio_session();
1152         if (0 != ret) {
1153                 stt_client_unset_current_recognition();
1154                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set session : %d", ret);
1155                 if (NULL != sound)      free(sound);
1156                 return ret;
1157         }
1158
1159         bool is_sound_done = false;
1160
1161         /* 2. Request wav play */
1162         if (NULL != sound) {
1163                 int id = 0;
1164                 intptr_t puid = (intptr_t)uid;
1165                 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_start_sound_completed_cb, (void*)puid, &id);
1166                 if (WAV_PLAYER_ERROR_NONE != ret) {
1167                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1168                         is_sound_done = true;
1169                 }
1170                 free(sound);
1171                 sound = NULL;
1172         } else {
1173                 is_sound_done = true;
1174         }
1175
1176         /* 3. Create recorder & engine initialize */
1177         ret = sttd_engine_agent_recognize_start_engine(uid, lang, recognition_type, silence, appid, credential, NULL);
1178         if (0 != ret) {
1179                 stt_client_unset_current_recognition();
1180                 sttd_recorder_unset_audio_session();
1181                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1182                 return ret;
1183         }
1184
1185         if (0 != strcmp(STTE_RECOGNITION_TYPE_FREE_PARTIAL, recognition_type)) {
1186                 g_recording_timer = ecore_timer_add(g_recording_timeout, __stop_by_recording_timeout, NULL);
1187         }
1188
1189         /* change uid state */
1190         sttd_client_set_state(uid, APP_STATE_RECORDING);
1191
1192         g_recording_log_count = 0;
1193
1194         if (true == is_sound_done) {
1195                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1196
1197                 ret = sttd_engine_agent_recognize_start_recorder(uid);
1198                 if (0 != ret) {
1199                         stt_client_unset_current_recognition();
1200                         sttd_recorder_unset_audio_session();
1201
1202                         sttd_engine_agent_recognize_cancel();
1203                         ecore_timer_del(g_recording_timer);
1204                         sttd_client_set_state(uid, APP_STATE_READY);
1205
1206                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1207                         return ret;
1208                 }
1209
1210                 /* Notify uid state change */
1211                 sttdc_send_set_state(uid, APP_STATE_RECORDING);
1212
1213                 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Start recognition");
1214                 return STTD_RESULT_STATE_DONE;
1215         }
1216
1217         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Wait sound finish");
1218
1219         return STTD_RESULT_STATE_NOT_DONE;
1220 }
1221
1222 Eina_Bool __time_out_for_processing(void *data)
1223 {
1224         g_processing_timer = NULL;
1225
1226         /* current uid */
1227         int uid = stt_client_get_current_recognition();
1228         if (0 == uid)   return EINA_FALSE;
1229
1230         /* Cancel engine */
1231         int ret = sttd_engine_agent_recognize_cancel();
1232         if (0 != ret) {
1233                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1234         }
1235
1236         if (0 != sttdc_send_result(uid, STTE_RESULT_EVENT_FINAL_RESULT, NULL, 0, "Time out not to receive recognition result.")) {
1237                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result ");
1238
1239                 /* send error msg */
1240                 int reason = (int)STTD_ERROR_TIMED_OUT;
1241                 if (0 != sttdc_send_error_signal(uid, reason, "[ERROR] Fail to send recognition result")) {
1242                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info ");
1243                 }
1244         }
1245
1246         /* Change uid state */
1247         sttd_client_set_state(uid, APP_STATE_READY);
1248
1249         stt_client_unset_current_recognition();
1250
1251         return EINA_FALSE;
1252 }
1253
1254 void __sttd_server_engine_stop(void* data)
1255 {
1256         intptr_t puid = (intptr_t)data;
1257         int uid = (int)puid;
1258         /* change uid state */
1259         sttd_client_set_state(uid, APP_STATE_PROCESSING);
1260
1261         /* Notify uid state change */
1262         sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1263
1264         /* Unset audio session */
1265         int ret = sttd_recorder_unset_audio_session();
1266         if (0 != ret) {
1267                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1268                 return;
1269         }
1270
1271         /* Stop engine */
1272         ret = sttd_engine_agent_recognize_stop_engine();
1273         if (0 != ret) {
1274                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1275                 return;
1276         }
1277
1278         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1279 }
1280
1281 void __sttd_stop_sound_completed_cb(int id, void *user_data)
1282 {
1283         SLOG(LOG_DEBUG, TAG_STTD, "===== Stop sound completed");
1284
1285         /* After wav play callback, engine stop */
1286         ecore_main_loop_thread_safe_call_async(__sttd_server_engine_stop, user_data);
1287
1288         SLOG(LOG_DEBUG, TAG_STTD, "=====");
1289         SLOG(LOG_DEBUG, TAG_STTD, "  ");
1290         return;
1291 }
1292
1293 int sttd_server_stop(int uid)
1294 {
1295         /* check if uid is valid */
1296         app_state_e state;
1297         if (0 != sttd_client_get_state(uid, &state)) {
1298                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1299                 return STTD_ERROR_INVALID_PARAMETER;
1300         }
1301
1302         /* check uid state */
1303         if (APP_STATE_RECORDING != state) {
1304                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording");
1305                 return STTD_ERROR_INVALID_STATE;
1306         }
1307
1308         if (NULL != g_recording_timer)  {
1309                 ecore_timer_del(g_recording_timer);
1310                 g_recording_timer = NULL;
1311         }
1312
1313         char* sound = NULL;
1314         if (0 != sttd_client_get_stop_sound(uid, &sound)) {
1315                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get start beep sound");
1316                 return STTD_ERROR_OPERATION_FAILED;
1317         }
1318
1319         SLOG(LOG_DEBUG, TAG_STTD, "[Server] stop sound path : %s", sound);
1320
1321         int ret;
1322         /* 1. Stop recorder */
1323         ret = sttd_engine_agent_recognize_stop_recorder();
1324         if (0 != ret) {
1325                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop recorder : result(%d)", ret);
1326                 if (0 != sttd_engine_agent_recognize_cancel()) {
1327                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel recognize");
1328                 }
1329                 if (NULL != sound)      free(sound);
1330                 return ret;
1331         }
1332
1333         /* 2. Request wav play */
1334         if (NULL != sound) {
1335                 int id = 0;
1336                 intptr_t puid = (intptr_t)uid;
1337                 ret = wav_player_start(sound, SOUND_TYPE_MEDIA, __sttd_stop_sound_completed_cb, (void*)puid, &id);
1338                 if (WAV_PLAYER_ERROR_NONE != ret) {
1339                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to play wav");
1340                 } else {
1341                         SLOG(LOG_DEBUG, TAG_STTD, "[Server] Play wav : %s", sound);
1342                 }
1343
1344                 free(sound);
1345
1346                 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1347
1348                 return STTD_RESULT_STATE_NOT_DONE;
1349         } else {
1350                 SLOG(LOG_DEBUG, TAG_STTD, "[Server] No sound play");
1351
1352                 /* Unset audio session */
1353                 ret = sttd_recorder_unset_audio_session();
1354                 if (0 != ret) {
1355                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1356                         return ret;
1357                 }
1358
1359                 /* Stop engine */
1360                 ret = sttd_engine_agent_recognize_stop_engine();
1361                 if (0 != ret) {
1362                         stt_client_unset_current_recognition();
1363                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop engine : result(%d)", ret);
1364                         return ret;
1365                 }
1366
1367                 /* change uid state */
1368                 sttd_client_set_state(uid, APP_STATE_PROCESSING);
1369
1370                 /* Notify uid state change */
1371                 sttdc_send_set_state(uid, APP_STATE_PROCESSING);
1372
1373                 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Stop recognition");
1374
1375                 g_processing_timer = ecore_timer_add(g_processing_timeout, __time_out_for_processing, NULL);
1376
1377                 return STTD_RESULT_STATE_DONE;
1378         }
1379
1380         return STTD_ERROR_NONE;
1381 }
1382
1383 int sttd_server_cancel(int uid)
1384 {
1385         /* check if uid is valid */
1386         app_state_e state;
1387         if (0 != sttd_client_get_state(uid, &state)) {
1388                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
1389                 return STTD_ERROR_INVALID_PARAMETER;
1390         }
1391
1392         /* check uid state */
1393         if (APP_STATE_READY == state) {
1394                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
1395                 return STTD_ERROR_NONE;
1396         }
1397
1398         stt_client_unset_current_recognition();
1399
1400         if (NULL != g_recording_timer) {
1401                 ecore_timer_del(g_recording_timer);
1402                 g_recording_timer = NULL;
1403         }
1404
1405         if (NULL != g_processing_timer) {
1406                 ecore_timer_del(g_processing_timer);
1407                 g_processing_timer = NULL;
1408         }
1409
1410         if (APP_STATE_RECORDING == state) {
1411                 /* Unset audio session */
1412                 int ret = sttd_recorder_unset_audio_session();
1413                 if (0 != ret) {
1414                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
1415                         return ret;
1416                 }
1417         }
1418
1419         /* change uid state */
1420         sttd_client_set_state(uid, APP_STATE_READY);
1421
1422         /* cancel engine recognition */
1423         int ret = sttd_engine_agent_recognize_cancel();
1424         if (0 != ret) {
1425                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
1426                 return ret;
1427         }
1428
1429         /* Notify uid state change */
1430         sttdc_send_set_state(uid, APP_STATE_READY);
1431
1432         return STTD_ERROR_NONE;
1433 }