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