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