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