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