56df9a52c3d097a6c87620904cfba5806f9a2bd1
[platform/core/uifw/voice-control.git] / server / vcd_server.c
1 /*
2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <app_control.h>
18 #include <app_manager.h>
19 #include <aul.h>
20 #include <dirent.h>
21 #include <sound_manager.h>
22 #include <vconf.h>
23
24 #include "vc_cmd_db.h"
25 #include "vc_info_parser.h"
26 #include "vcd_main.h"
27 #include "vcd_server.h"
28 #include "vcd_server_data.h"
29 #include "vcd_client_data.h"
30
31 #include "vcd_engine_agent.h"
32 #include "vcd_config.h"
33 #include "vcd_recorder.h"
34 #include "vcd_dbus.h"
35 #include "vce_internal.h"
36
37 #include "voice_control_command_expand.h"
38 #include "voice_control_common.h"
39
40 #define CLIENT_CLEAN_UP_TIME 500
41 /*
42 * VC Server static variable
43 */
44 static GList *g_proc_list = NULL;
45
46 static Ecore_Timer *g_restart_timer = NULL;
47 static Ecore_Timer *g_check_widget_client_timer = NULL;
48 static Ecore_Timer *g_check_client_timer = NULL;
49
50 static Ecore_Thread* g_tts_thread = NULL;
51 static int g_current_uid = -1;
52 static int g_current_utt_id = -1;
53
54 /**
55 * @brief Enumerations of send event type.
56 */
57 typedef enum {
58         VCD_SEND_EVENT_TYPE_TEXT,               /**< send text event to vc engine*/
59         VCD_SEND_EVENT_TYPE_LIST_EVENT,         /**< send list event to vc engine */
60         VCD_SEND_EVENT_TYPE_HAPTIC_EVENT        /**< send haptic event to vc engine */
61 } vcd_send_event_type_e;
62
63 static int __vcd_server_launch_manager_app();
64 static int __start_internal_recognition();
65
66 /*
67 * VC Server Internal Functions
68 */
69 static Eina_Bool __stop_by_silence(void *data)
70 {
71         SLOG(LOG_INFO, TAG_VCD, "@@@ Silence Detected ");
72
73         vcd_server_mgr_stop();
74
75         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
76         return EINA_FALSE;
77 }
78
79 static Eina_Bool __cancel_by_interrupt(void *data)
80 {
81         SLOG(LOG_INFO, TAG_VCD, "@@@ Cancel by interrupt");
82
83         vcd_server_mgr_cancel();
84
85         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
86         return EINA_FALSE;
87 }
88
89 static void __cancel_by_error(void *data)
90 {
91         SLOG(LOG_INFO, TAG_VCD, "@@@ Cancel by error");
92
93         vcd_server_mgr_cancel();
94
95         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
96         return;
97 }
98
99 static Eina_Bool __restart_engine(void *data)
100 {
101         SLOG(LOG_INFO, TAG_VCD, "@@@ Restart by no result");
102
103         g_restart_timer = NULL;
104
105         /* Restart recognition */
106         int ret = vcd_engine_recognize_start(true);
107         if (0 != ret) {
108                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
109                 return EINA_FALSE;
110         }
111
112         SLOG(LOG_INFO, TAG_VCD, "[Server Success] Start engine");
113
114         vcd_recognition_mode_e mode = vcd_client_get_recognition_mode();
115         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == mode || VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == mode) {
116                 vcd_config_set_service_state(VCD_STATE_RECORDING);
117                 vcdc_send_service_state(VCD_STATE_RECORDING);
118         }
119
120         SLOG(LOG_INFO, TAG_VCD, "[Server Success] Restart recognition");
121
122         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
123         return EINA_FALSE;
124 }
125
126 static int __server_recorder_callback(const void* data, const unsigned int length)
127 {
128         vcd_state_e state = vcd_config_get_service_state();
129         if (VCD_STATE_READY == state) {
130                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ready state, but recording");
131         } else if (VCD_STATE_PROCESSING == state) {
132                 return 0;
133         }
134
135         vce_speech_detect_e speech_detected = VCE_SPEECH_DETECT_NONE;
136         int ret;
137
138         ret = vcd_engine_recognize_audio(data, length, &speech_detected);
139
140         if (0 > ret) {
141                 /* Error */
142                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
143                 ecore_timer_add(0, __cancel_by_interrupt, NULL);
144                 /* Send error cb to manager */
145                 if (VCE_ERROR_OUT_OF_NETWORK == ret) {
146                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "voice_framework.error.engine.set_recording_fail");
147                 } else {
148                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_framework.error.engine.set_recording_fail");
149                 }
150                 return 0;
151         }
152
153         if (VCE_SPEECH_DETECT_BEGIN == speech_detected) {
154                 if (-1 != vcd_client_manager_get_pid()) {
155                         /* Manager client is available */
156                         if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
157                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
158                         }
159                 }
160         } else if (VCE_SPEECH_DETECT_END == speech_detected) {
161                 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
162                         /* silence detected */
163                         ecore_timer_add(0, __stop_by_silence, NULL);
164                 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
165                         /* Stop engine recognition */
166                         int ret = vcd_engine_recognize_stop();
167                         if (0 != ret) {
168                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
169                         }
170                         vcd_config_set_service_state(VCD_STATE_PROCESSING);
171                         vcdc_send_service_state(VCD_STATE_PROCESSING);
172
173                         SLOG(LOG_INFO, TAG_VCD, "[Server] Stop engine only by silence");
174                 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
175                         /* Stop engine recognition */
176                         int ret = vcd_engine_recognize_stop();
177                         if (0 != ret) {
178                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
179                         }
180                 }
181         }
182
183         return 0;
184 }
185
186 void __server_recorder_interrupt_callback()
187 {
188         SLOG(LOG_INFO, TAG_VCD, "@@@ Cancel by sound interrupt");
189
190         ecore_timer_add(0, __cancel_by_interrupt, NULL);
191
192         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
193 }
194
195 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
196 {
197         SLOG(LOG_INFO, TAG_VCD, "@@@ Change language ");
198
199         /* Current state is recording */
200         vcd_state_e state = vcd_config_get_service_state();
201         if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
202                 SLOG(LOG_INFO, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
203                 vcd_server_mgr_cancel();
204         }
205
206         int ret;
207         ret = vcd_engine_set_current_language(current_lang);
208         if (0 != ret) {
209                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
210         }
211
212         SLOG(LOG_INFO, TAG_VCD, "@@@");
213
214         return;
215 }
216
217 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
218 {
219         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Change foreground");
220
221         SLOG(LOG_INFO, TAG_VCD, "Foreground pid(%d)", current);
222
223         if (VC_NO_FOREGROUND_PID != current) {
224                 /* Foreground app is changed */
225                 vcd_state_e state = vcd_config_get_service_state();
226                 if (VCD_STATE_RECORDING == state) {
227                         SLOG(LOG_INFO, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
228                         ecore_timer_add(0, __cancel_by_interrupt, NULL);
229                 }
230         }
231
232         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
233
234         return;
235 }
236
237 static int __vcd_activate_app_by_appcontrol(const char* appid)
238 {
239         if (NULL == appid) {
240                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
241                 return VCD_ERROR_INVALID_PARAMETER;
242         }
243
244         int ret = -1;
245         app_control_h app_control = NULL;
246         ret = app_control_create(&app_control);
247         if (APP_CONTROL_ERROR_NONE == ret) {
248                 // Set extra data
249                 ret = app_control_add_extra_data(app_control, "voice_launch", "get_result");
250                 if (APP_CONTROL_ERROR_NONE != ret) {
251                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to add extra data, ret(%d)", ret);
252                         app_control_destroy(app_control);
253                         return VCD_ERROR_OPERATION_FAILED;
254                 }
255                 // Set an app ID.
256                 ret = app_control_set_app_id(app_control, appid);
257                 if (APP_CONTROL_ERROR_NONE != ret) {
258                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set app id, ret(%d)", ret);
259                         app_control_destroy(app_control);
260                         return VCD_ERROR_OPERATION_FAILED;
261                 }
262                 // Sent launch request
263                 ret = app_control_send_launch_request(app_control, NULL, NULL);
264                 if (APP_CONTROL_ERROR_NONE != ret) {
265                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send launch request, ret(%d)", ret);
266                         app_control_destroy(app_control);
267                         return VCD_ERROR_OPERATION_FAILED;
268                 }
269                 // Destroy app control
270                 ret = app_control_destroy(app_control);
271                 if (APP_CONTROL_ERROR_NONE != ret) {
272                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy, ret(%d)", ret);
273                         return VCD_ERROR_OPERATION_FAILED;
274                 }
275         }
276         return VCD_ERROR_NONE;
277 }
278
279 static int __vcd_resume_app(const char* appid)
280 {
281         app_context_h app_context = NULL;
282         int ret = app_manager_get_app_context(appid, &app_context);
283         if (APP_MANAGER_ERROR_NONE != ret) {
284                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
285                 return VCD_ERROR_OPERATION_FAILED;
286         }
287
288         ret = app_manager_resume_app(app_context);
289         if (APP_MANAGER_ERROR_NONE != ret) {
290                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app, ret(%d), appid(%s)", ret, appid);
291                 return VCD_ERROR_OPERATION_FAILED;
292         }
293         return VCD_ERROR_NONE;
294 }
295
296 static bool __vcd_is_package_installed(const char* appid)
297 {
298         app_info_h app_info = NULL;
299         int ret = app_manager_get_app_info(appid, &app_info);
300         if (APP_MANAGER_ERROR_NONE != ret || NULL == app_info)
301                 return false;
302         ret = app_info_destroy(app_info);
303         if (APP_MANAGER_ERROR_NONE != ret)
304                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to destroy app_info, ret(%d)", ret);
305         return true;
306 }
307
308 void __vc_free_deactivated_app(void* data)
309 {
310         vc_deactivated_app_s* d_app = (vc_deactivated_app_s*)data;
311
312         if (NULL != d_app) {
313                 if (NULL != d_app->appid) {
314                         free(d_app->appid);
315                         d_app->appid = NULL;
316                 }
317
318                 free(d_app);
319                 d_app = NULL;
320         }
321 }
322
323 static bool __vcd_launch_app(const char* result)
324 {
325         if (NULL == result) {
326                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
327                 return VCD_ERROR_INVALID_PARAMETER;
328         }
329
330         GSList* app_list = NULL;
331         if (0 != vc_db_get_appid_list(result, &app_list)) {
332                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] result text is NULL");
333                 return VCD_ERROR_INVALID_PARAMETER;
334         }
335
336         if (0 != g_slist_length(app_list)) {
337                 /* release data */
338                 GSList *iter = NULL;
339                 vc_deactivated_app_s* temp_app = NULL;
340                 iter = g_slist_nth(app_list, 0);
341
342                 while (NULL != iter) {
343                         temp_app = iter->data;
344
345                         if (NULL != temp_app && NULL != temp_app->appid) {
346                                 int ret = -1;
347                                 bool running = false;
348                                 ret = app_manager_is_running(temp_app->appid, &running);
349                                 if (APP_MANAGER_ERROR_NONE != ret) {
350                                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", temp_app->appid);
351                                         g_slist_free_full(iter, __vc_free_deactivated_app);
352                                         return VCD_ERROR_OPERATION_FAILED;
353                                 }
354                                 if (false == running) {
355                                         int tmp_ret = __vcd_is_package_installed(temp_app->appid);
356                                         if (false == tmp_ret) {
357                                                 SLOG(LOG_WARN, TAG_VCD, "[WARNING] app is not installed, appid(%s)", temp_app->appid);
358                                         } else {
359                                                 ret = __vcd_activate_app_by_appcontrol(temp_app->appid);
360                                                 if (0 != ret) {
361                                                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
362                                                         g_slist_free_full(iter, __vc_free_deactivated_app);
363                                                         return ret;
364                                                 }
365                                                 SLOG(LOG_ERROR, TAG_VCD, "Launch app: appid(%s) result(%s)", temp_app->appid, result);
366                                         }
367                                 } else {
368                                         ret = __vcd_resume_app(temp_app->appid);
369                                         if (0 != ret) {
370                                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
371                                                 g_slist_free_full(iter, __vc_free_deactivated_app);
372                                                 return ret;
373                                         }
374                                         SLOG(LOG_ERROR, TAG_VCD, "Resume app: appid(%s) result(%s)", temp_app->appid, result);
375                                 }
376                                 __vc_free_deactivated_app((void*)temp_app);
377                                 temp_app = NULL;
378                         }
379                         app_list = g_slist_remove_link(app_list, iter);
380                         g_slist_free(iter);
381                         iter = g_slist_nth(app_list, 0);
382                 }
383                 app_list = NULL;
384         }
385
386         return VCD_ERROR_NONE;
387 }
388
389 static Eina_Bool __vcd_send_selected_result(void *data)
390 {
391         GSList* pid_list = NULL;
392         const char* result = vcd_client_manager_get_result_text();
393
394         if (0 != vc_info_parser_get_result_pid_list(&pid_list, result)) {
395                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
396         } else {
397                 if (0 < g_slist_length(pid_list)) {
398                         GSList* iter = NULL;
399                         vc_cmd_s* temp_cmd = NULL;
400                         int ret = 0;
401                         int pre_pid = -1;
402                         int pre_type = -1;
403
404                         iter = g_slist_nth(pid_list, 0);
405                         while (NULL != iter) {
406                                 temp_cmd = iter->data;
407
408                                 if (NULL != temp_cmd && (pre_pid != temp_cmd->pid || pre_type == VC_COMMAND_TYPE_WIDGET || temp_cmd->type == VC_COMMAND_TYPE_WIDGET)) {
409                                         /* Launch deactivated several apps that is matched with result */
410                                         ret = __vcd_launch_app(result);
411                                         if (0 != ret) {
412                                                 SLOG(LOG_ERROR, TAG_VCD, "Fail to launch or resume app, ret(%d) result(%s)", ret, result);
413                                         } else {
414                                                 /* send result noti */
415                                                 ret = vcdc_send_result(temp_cmd->pid, vcd_client_manager_get_pid(), temp_cmd->type);
416                                                 if (0 != ret) {
417                                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result, ret(%d)", ret);
418                                                         g_slist_free_full(pid_list, free);
419                                                         pid_list = NULL;
420                                                         break;
421                                                 } else {
422                                                         SLOG(LOG_ERROR, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
423                                                         pre_pid = temp_cmd->pid;
424                                                         pre_type = temp_cmd->type;
425                                                 }
426                                         }
427                                         free(temp_cmd);
428                                         temp_cmd = NULL;
429                                 }
430                                 pid_list = g_slist_remove_link(pid_list, iter);
431                                 g_slist_free(iter);
432                                 iter = g_slist_nth(pid_list, 0);
433                         }
434                 }
435         }
436
437         if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
438                 vcd_config_set_service_state(VCD_STATE_READY);
439                 vcdc_send_service_state(VCD_STATE_READY);
440         }
441
442         return EINA_FALSE;
443 }
444
445 int vcd_send_asr_result(vce_asr_result_event_e event, const char* asr_result, void *user_data)
446 {
447         int ret = __vcd_server_launch_manager_app();
448         if (0 != ret) {
449                 SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send ASR result : mgr_pid(%d), asr_result(%s)", vcd_client_manager_get_pid(), asr_result);
450                 return ret;
451         }
452
453         if (NULL != asr_result) {
454                 SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] ASR result - Event(%d), Text(%s)", event, asr_result);
455                 ret = vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, asr_result);
456                 if (0 != ret) {
457                         SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send ASR result : mgr_pid(%d), asr_result(%s)", vcd_client_manager_get_pid(), asr_result);
458                 }
459         }
460
461         return ret;
462 }
463
464 int vcd_send_specific_engine_result(const char* engine_app_id, const char* event, const char* result, void *user_info)
465 {
466         if (NULL != result) {
467                 SLOG(LOG_INFO, TAG_VCD, "[Server] specific engine result - Event(%s), Text(%s)", event, result);
468                 vcdc_send_specific_engine_result_to_manager(vcd_client_manager_get_pid(), engine_app_id, event, result);
469         }
470
471         return VCD_ERROR_NONE;
472 }
473
474 int vcd_send_nlg_result(const char* nlg_result, void *user_data)
475 {
476         int ret = __vcd_server_launch_manager_app();
477         if (0 != ret) {
478                 SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
479                 return ret;
480         }
481
482         SLOG(LOG_ERROR, TAG_VCD, "[Server] send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
483         ret = vcdc_send_dialog(vcd_client_manager_get_pid(), -1, nlg_result, NULL, 0); //0: VC_DIALOG_END
484         if (0 != ret)
485                 SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), nlg_result(%s)", vcd_client_manager_get_pid(), nlg_result);
486         return ret;
487 }
488
489 static void* __recorder_stop(void *data)
490 {
491         vcd_recorder_stop();
492         return NULL;
493 }
494
495 int vcd_send_result(vce_result_event_e event, int* result_id, int count, const char* all_result, const char* non_fixed_result, const char* nlu_result, const char* msg, int* user_info, void *user_data)
496 {
497         int ret = 0;
498         vcd_recognition_mode_e recognition_mode = vcd_client_get_recognition_mode();
499
500         if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
501                 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != recognition_mode) {
502                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
503                         return VCD_ERROR_INVALID_STATE;
504                 }
505         }
506
507         vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
508         vcd_client_manager_set_result_text(all_result);
509
510         SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
511                 event, all_result, non_fixed_result, msg, count);
512
513         SECURE_SLOG(LOG_ERROR, TAG_VCD, "[Server] NLU result(%s)", nlu_result);
514
515 #if 1
516         /* if nlu_result is exist, Add command handle(is_action) into result list */
517         /* Normal result */
518         SLOG(LOG_INFO, TAG_VCD, "[Server] @ Get engine result @");
519
520         vc_cmd_s* temp_cmd = NULL;
521         vc_cmd_list_h vc_cmd_list = NULL;
522
523         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
524                 SLOG(LOG_INFO, TAG_VCD, "[Server] Fail to create command list");
525                 vcd_client_manager_set_exclusive(false);
526                 vcd_config_set_service_state(VCD_STATE_READY);
527                 vcdc_send_service_state(VCD_STATE_READY);
528                 return VCD_ERROR_NONE;
529         }
530
531         /* priority filter */
532         /* system > exclusive > widget > foreground > system_background > widget partial > foreground partial > background */
533         int i = 0;
534         int* filtered_id = (int*)calloc(count, sizeof(int));
535         if (!filtered_id) {
536                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to allocate memory");
537                 vc_cmd_list_destroy(vc_cmd_list, true);
538                 vc_cmd_list = NULL;
539                 return VCD_ERROR_OUT_OF_MEMORY;
540         }
541         int filtered_count = 0;
542         int top_priority = VC_COMMAND_PRIORITY_BACKGROUND;
543         for (i = 0; i < count; i++) {
544                 SLOG(LOG_INFO, TAG_VCD, "[Server]   [%d] Result id(%d)", i, result_id[i]);
545
546                 if (0 > result_id[i]) {
547                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
548                         continue;
549                 }
550
551                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
552                 if (0 == ret && NULL != temp_cmd) {
553                         if (top_priority == temp_cmd->priority) {
554                                 filtered_id[filtered_count] = result_id[i];
555                                 filtered_count++;
556                         } else if (top_priority > temp_cmd->priority) {
557                                 filtered_id[0] = result_id[i];
558                                 filtered_count = 1;
559                                 top_priority = temp_cmd->priority;
560                         }
561                 }
562                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
563                 temp_cmd = NULL;
564         }
565
566         // ASR consume
567         if (top_priority >= VC_COMMAND_PRIORITY_WIDGET) {
568                 int pid = vcd_client_widget_get_foreground_pid();
569                 if (-1 != pid) {
570                         if (NULL != all_result) {
571                                 vc_info_parser_set_result(all_result, event, msg, NULL, false);
572                                 bool enable = false;
573                                 vcd_client_widget_get_asr_result_enabled(pid, &enable);
574                                 if (true == enable) {
575                                         SLOG(LOG_INFO, TAG_VCD, "[Server] Send ASR result to Widget client");
576                                         bool is_consumed = false;
577                                         if (NULL != user_info) {
578                                                 *user_info = 0x00;
579                                         }
580                                         if (0 != vcdc_send_asr_result(pid, event, all_result, VC_COMMAND_TYPE_WIDGET, &is_consumed)) {
581                                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send asr result");
582                                         } else {
583                                                 SLOG(LOG_INFO, TAG_VCD, "[Server] ASR result is consumed(%d)", is_consumed);
584                                                 if (true == is_consumed) {
585                                                         if (NULL != user_info) {
586                                                                 *user_info = 0x01;
587                                                                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Send whether ASR result is consumed or not (%d)", *user_info);
588                                                         }
589                                                         vcdc_send_show_tooltip(pid, false);
590                                                         if (-1 != vcd_client_manager_get_pid()) {
591                                                                 /* Manager client is available */
592                                                                 vc_info_parser_unset_result(false);
593                                                                 vc_info_parser_set_result(all_result, VC_RESULT_EVENT_RESULT_SUCCESS, msg, NULL, false);
594                                                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
595                                                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
596                                                                 }
597                                                         }
598
599                                                         // Set state manually, because the result is already handled in ASR result by client.
600                                                         // So, the daemon does not need to send the result to client.
601                                                         vcd_client_manager_set_exclusive(false);
602
603                                                         vcd_config_set_service_state(VCD_STATE_READY);
604                                                         vcdc_send_service_state(VCD_STATE_READY);
605                                                         vc_cmd_list_destroy(vc_cmd_list, true);
606                                                         vc_cmd_list = NULL;
607                                                         if (NULL != filtered_id) {
608                                                                 free(filtered_id);
609                                                                 filtered_id = NULL;
610                                                         }
611                                                         return VCD_ERROR_NONE;
612                                                 }
613                                         }
614                                 }
615                         }
616                 }
617         }
618
619         int is_action = 0;
620         for (i = 0; i < filtered_count; i++) {
621                 SLOG(LOG_INFO, TAG_VCD, "[Server]   [%d] Filtered Result id(%d)", i, filtered_id[i]);
622
623                 if (filtered_id[i] < 0) {
624                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Filtered ID(%d) is NOT valid", filtered_id[i]);
625                         continue;
626                 }
627
628                 ret = vcd_client_get_cmd_from_result_id(filtered_id[i], &temp_cmd);
629                 if (0 == ret && NULL != temp_cmd) {
630                         switch (temp_cmd->format) {
631                         case VC_CMD_FORMAT_FIXED:
632                         case VC_CMD_FORMAT_FIXED_AND_VFIXED:
633                         case VC_CMD_FORMAT_VFIXED_AND_FIXED:
634                         case VC_CMD_FORMAT_PARTIAL:
635                         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
636                         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
637                                 break;
638                         case VC_CMD_FORMAT_ACTION:
639                                 is_action = 1;
640                                 break;
641                         default:
642                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
643                         }
644
645                         temp_cmd->id = i;
646                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
647                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to add command to list");
648                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
649                                 temp_cmd = NULL;
650                         }
651                 } else {
652                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matched result(%d)", filtered_id[i]);
653                 }
654         }
655
656         if (NULL != filtered_id) {
657                 free(filtered_id);
658                 filtered_id = NULL;
659         }
660
661         if (NULL != nlu_result) {
662                 SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] NLU (%s)", nlu_result);
663                 vc_info_parser_set_nlu_result(nlu_result);
664                 if (0 == is_action) {
665                         vc_cmd_h nlu_cmd;
666                         if (0 != vc_cmd_create(&nlu_cmd)) {
667                                 SLOG(LOG_ERROR, TAG_VCD, "Fail to nlu cmd create");
668                         } else {
669                                 if (0 != vc_cmd_set_type(nlu_cmd, VC_COMMAND_TYPE_SYSTEM)) {
670                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set type");
671                                 }
672                                 if (0 != vc_cmd_set_pid(nlu_cmd, vcd_client_manager_get_pid())) {
673                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set pid");
674                                 }
675                                 if (0 != vc_cmd_set_format(nlu_cmd, VC_CMD_FORMAT_ACTION)) {
676                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to set format");
677                                 }
678                                 if (0 != vc_cmd_list_add(vc_cmd_list, nlu_cmd)) {
679                                         SLOG(LOG_ERROR, TAG_VCD, "Fail to add nlu cmd to list");
680                                         vc_cmd_destroy(nlu_cmd);
681                                 }
682                         }
683                 }
684         }
685
686         vc_cmd_print_list(vc_cmd_list);
687
688         SLOG(LOG_INFO, TAG_VCD, "[Server] @@@@");
689
690         int result_count = 0;
691         vc_cmd_list_get_count(vc_cmd_list, &result_count);
692
693         /* Handle the result list */
694         if (0 == result_count) {
695                 /* No result */
696                 vc_cmd_list_h widget_cmd_list = NULL;
697                 vc_cmd_list_h foreground_cmd_list = NULL;
698                 if (NULL != all_result) {
699                         SECURE_SLOG(LOG_INFO, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
700                         int cnt = 0;
701
702                         if (0 != vc_cmd_list_create(&widget_cmd_list)) {
703                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create widget command list handle");
704                                 vc_cmd_list_destroy(vc_cmd_list, true);
705                                 vc_cmd_list = NULL;
706                                 return VCD_ERROR_OUT_OF_MEMORY;
707                         }
708
709                         /* Get the list of widget type commands */
710                         vcd_client_append_cmd_from_type(VC_COMMAND_TYPE_WIDGET, widget_cmd_list);
711                         vc_cmd_list_get_count(widget_cmd_list, &cnt);
712                         if (0 < cnt) {
713                                 /* Matched with widget command partially */
714                                 vc_cmd_get_partially_matched_cmd_list(all_result, widget_cmd_list, vc_cmd_list, VC_SEARCH_NONE_LEVEL);
715                                 vc_cmd_list_get_count(vc_cmd_list, &cnt);
716                                 if (0 < cnt) {
717                                         top_priority = VC_COMMAND_PRIORITY_WIDGET;
718                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] Partially matched widget command");
719                                 }
720                         } else {
721                                 if (0 != vc_cmd_list_create(&foreground_cmd_list)) {
722                                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create foreground command list handle");
723                                         vc_cmd_list_destroy(vc_cmd_list, true);
724                                         vc_cmd_list = NULL;
725                                         vc_cmd_list_destroy(widget_cmd_list, true);
726                                         widget_cmd_list = NULL;
727                                         return VCD_ERROR_OUT_OF_MEMORY;
728                                 }
729
730                                 /* Get the list of foreground type commands */
731                                 vcd_client_append_cmd_from_type(VC_COMMAND_TYPE_FOREGROUND, foreground_cmd_list);
732                                 vc_cmd_list_get_count(foreground_cmd_list, &cnt);
733                                 if (0 < cnt) {
734                                         /* Matched with foreground command partially */
735                                         vc_cmd_get_partially_matched_cmd_list(all_result, foreground_cmd_list, vc_cmd_list, VC_SEARCH_NONE_LEVEL);
736                                         vc_cmd_list_get_count(vc_cmd_list, &cnt);
737                                         if (0 < cnt) {
738                                                 top_priority = VC_COMMAND_PRIORITY_FOREGROUND;
739                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] Partially matched foreground command");
740                                         }
741                                 }
742                                 vc_cmd_list_destroy(foreground_cmd_list, true);
743                                 foreground_cmd_list = NULL;
744                         }
745                         vc_cmd_list_destroy(widget_cmd_list, true);
746                         widget_cmd_list = NULL;
747                 } else {
748                         SLOG(LOG_INFO, TAG_VCD, "[Server] Engine all result is NULL");
749                 }
750
751                 vc_cmd_list_get_count(vc_cmd_list, &result_count);
752
753                 // After running partial matching algorithm, if there is no result.
754                 if (0 == result_count) {
755                         SLOG(LOG_INFO, TAG_VCD, "[Server] No commands even after partial matching");
756
757                         bool temp = vcd_client_manager_get_exclusive();
758                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
759
760                         int pid = vcd_client_widget_get_foreground_pid();
761                         if (-1 != pid) {
762                                 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip hide");
763                                 /* Send to hide tooltip */
764                                 vcdc_send_show_tooltip(pid, false);
765                         }
766
767                         if (-1 != vcd_client_manager_get_pid()) {
768                                 /* Manager client is available */
769                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
770                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
771                                 }
772                         }
773
774                         /* set service state ready */
775                         vcd_config_set_service_state(VCD_STATE_READY);
776                         vcdc_send_service_state(VCD_STATE_READY);
777
778                         vcd_client_manager_set_exclusive(false);
779                         vc_cmd_list_destroy(vc_cmd_list, true);
780                         vc_cmd_list = NULL;
781
782                         return VCD_ERROR_NONE;
783                 }
784
785                 // Partial matching algorithm find some commands
786                 event = VC_RESULT_EVENT_RESULT_SUCCESS;
787         }
788
789         // There are more than one result.
790         if (false == vcd_client_manager_get_exclusive()) {
791                 vc_cmd_list_h temp_list = NULL;
792
793                 /* Foreground, Widget, Background, System, System-Background */
794                 if (top_priority >= VC_COMMAND_PRIORITY_BACKGROUND) {
795                         vc_cmd_list_h widget_cmd_list = NULL;
796                         vc_cmd_list_h foreground_cmd_list = NULL;
797                         int cnt = 0;
798
799                         if (0 != vc_cmd_list_create(&temp_list)) {
800                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create widget command list handle");
801                                 vc_cmd_list_destroy(vc_cmd_list, true);
802                                 vc_cmd_list = NULL;
803                                 return VCD_ERROR_OUT_OF_MEMORY;
804                         }
805
806                         if (0 != vc_cmd_list_create(&widget_cmd_list)) {
807                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create widget command list handle");
808                                 vc_cmd_list_destroy(vc_cmd_list, true);
809                                 vc_cmd_list = NULL;
810                                 vc_cmd_list_destroy(temp_list, true);
811                                 temp_list = NULL;
812                                 return VCD_ERROR_OUT_OF_MEMORY;
813                         }
814
815                         /* Get the list of widget type commands */
816                         vcd_client_append_cmd_from_type(VC_COMMAND_TYPE_WIDGET, widget_cmd_list);
817                         vc_cmd_list_get_count(widget_cmd_list, &cnt);
818                         if (0 < cnt) {
819                                 /* Matched with widget command partially */
820                                 vc_cmd_get_partially_matched_cmd_list(all_result, widget_cmd_list, temp_list, VC_SEARCH_NONE_LEVEL);
821                                 vc_cmd_list_get_count(temp_list, &cnt);
822                                 if (0 < cnt) {
823                                         if (0 != vc_cmd_list_destroy(vc_cmd_list, true)) {
824                                                 SLOG(LOG_WARN, TAG_VCD, "[WARNING] Fail to destroy list");
825                                         }
826                                         vc_cmd_list = temp_list;
827                                         top_priority = VC_COMMAND_PRIORITY_WIDGET;
828                                         event = VC_RESULT_EVENT_RESULT_SUCCESS;
829                                         SLOG(LOG_INFO, TAG_VCD, "[INFO] Partially matched widget command when background cmd exists");
830                                 }
831                         } else {
832                                 if (0 != vc_cmd_list_create(&foreground_cmd_list)) {
833                                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create foreground command list handle");
834                                         vc_cmd_list_destroy(vc_cmd_list, true);
835                                         vc_cmd_list = NULL;
836                                         vc_cmd_list_destroy(widget_cmd_list, true);
837                                         widget_cmd_list = NULL;
838                                         return VCD_ERROR_OUT_OF_MEMORY;
839                                 }
840
841                                 /* Get the list of foreground type commands */
842                                 vcd_client_append_cmd_from_type(VC_COMMAND_TYPE_FOREGROUND, foreground_cmd_list);
843                                 vc_cmd_list_get_count(foreground_cmd_list, &cnt);
844                                 if (0 < cnt) {
845                                         /* Matched with foreground command partially */
846                                         vc_cmd_get_partially_matched_cmd_list(all_result, foreground_cmd_list, temp_list, VC_SEARCH_NONE_LEVEL);
847                                         vc_cmd_list_get_count(temp_list, &cnt);
848                                         if (0 < cnt) {
849                                                 if (0 != vc_cmd_list_destroy(vc_cmd_list, true)) {
850                                                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] Fail to destroy list");
851                                                 }
852                                                 vc_cmd_list = temp_list;
853                                                 top_priority = VC_COMMAND_PRIORITY_FOREGROUND;
854                                                 event = VC_RESULT_EVENT_RESULT_SUCCESS;
855                                                 SLOG(LOG_INFO, TAG_VCD, "[INFO] Partially matched foreground command when background cmd exists");
856                                         }
857                                 }
858                                 vc_cmd_list_destroy(foreground_cmd_list, true);
859                                 foreground_cmd_list = NULL;
860                         }
861                         vc_cmd_list_destroy(widget_cmd_list, true);
862                         widget_cmd_list = NULL;
863                 }
864
865                 int pid = vcd_client_widget_get_foreground_pid();
866                 if (-1 != pid) {
867                         SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip hide");
868                         vcdc_send_show_tooltip(pid, false);
869                 }
870
871                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
872                 vc_cmd_list_destroy(vc_cmd_list, true);
873                 vc_cmd_list = NULL;
874
875                 if (-1 != vcd_client_manager_get_pid()) {
876                         /* Manager client is available */
877                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
878                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
879                         }
880                 } else {
881                         SLOG(LOG_INFO, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
882                         ecore_timer_add(0, __vcd_send_selected_result, NULL);
883                 }
884         } else {
885                 /* exclusive command */
886                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
887                 vc_cmd_list_destroy(vc_cmd_list, true);
888                 vc_cmd_list = NULL;
889
890                 if (-1 != vcd_client_manager_get_pid()) {
891                         /* Manager client is available */
892                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
893                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
894                         }
895                 } else {
896                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
897                 }
898
899                 vcd_client_manager_set_exclusive(false);
900         }
901
902         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == recognition_mode) {
903                 if (VCE_RESULT_EVENT_REJECTED == event) {
904                         SLOG(LOG_INFO, TAG_VCD, "[Server] Restart by no or rejected result");
905                         /* If no result and restart option is ON */
906                         /* Send reject message */
907                         bool temp = vcd_client_manager_get_exclusive();
908                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
909                         ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
910                         if (0 != ret) {
911                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
912                         }
913
914                         g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
915                         return ret;
916                 }
917                 SLOG(LOG_INFO, TAG_VCD, "[Server] Stop recorder due to success");
918                 //vcd_recorder_stop();
919                 ecore_main_loop_thread_safe_call_sync(__recorder_stop, NULL);
920         } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == recognition_mode) {
921                 SLOG(LOG_INFO, TAG_VCD, "[Server] Restart continuously");
922                 /* Restart option is ON */
923                 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
924                 if (VCE_RESULT_EVENT_REJECTED == event) {
925                         bool temp = vcd_client_manager_get_exclusive();
926                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
927                         ret = vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION);
928                         if (0 != ret) {
929                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
930                         }
931                         return ret;
932                 }
933         }
934
935         return VCD_ERROR_NONE;
936
937 #else
938         /* No result */
939         if (NULL == result_id) {
940                 /* No result */
941                 if (NULL != all_result) {
942                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
943                         bool temp = vcd_client_manager_get_exclusive();
944                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
945                 }
946
947                 int pid = vcd_client_widget_get_foreground_pid();
948                 if (-1 != pid) {
949                         if (NULL != all_result) {
950                                 /* Send result text to widget */
951                                 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
952                         }
953
954                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
955                         /* Send to hide tooltip */
956                         vcdc_send_show_tooltip(pid, false);
957                 }
958
959                 if (-1 != vcd_client_manager_get_pid()) {
960                         /* Manager client is available */
961                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
962                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
963                         }
964                 }
965
966                 vcd_client_manager_set_exclusive(false);
967
968                 return;
969         }
970
971         /* Normal result */
972         SLOG(LOG_DEBUG, TAG_VCD, "[Server] @ Get engine result @");
973
974         int ret = -1;
975         vc_cmd_s* temp_cmd = NULL;
976         vc_cmd_list_h vc_cmd_list = NULL;
977
978         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
979                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
980                 vcd_client_manager_set_exclusive(false);
981                 vcd_config_set_service_state(VCD_STATE_READY);
982                 vcdc_send_service_state(VCD_STATE_READY);
983                 return;
984         }
985
986         int i = 0;
987         for (i = 0; i < count; i++) {
988                 SLOG(LOG_DEBUG, TAG_VCD, "[Server]   [%d] Result ID(%d)", i, result_id[i]);
989
990                 if (result_id[i] < 0) {
991                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
992                         continue;
993                 }
994
995                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
996                 if (0 == ret && NULL != temp_cmd) {
997                         switch (temp_cmd->format) {
998                         case VC_CMD_FORMAT_FIXED:
999                         case VC_CMD_FORMAT_FIXED_AND_VFIXED:
1000                         case VC_CMD_FORMAT_VFIXED_AND_FIXED:
1001                         case VC_CMD_FORMAT_PARTIAL:
1002                                 /* Nonfixed result is NOT valid */
1003                                 break;
1004                         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
1005                                 if (NULL == temp_cmd->parameter) {
1006                                         if (NULL != non_fixed_result) {
1007                                                 temp_cmd->parameter = strdup(non_fixed_result);
1008                                         }
1009                                 } else {
1010                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT valid. Parameter (%s)", temp_cmd->parameter);
1011                                 }
1012                                 break;
1013                         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
1014                                 if (NULL == temp_cmd->command) {
1015                                         if (NULL != non_fixed_result) {
1016                                                 temp_cmd->command = strdup(non_fixed_result);
1017                                         }
1018                                 } else {
1019                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT valid. Command (%s)", temp_cmd->command);
1020                                 }
1021
1022                                 break;
1023                         default:
1024                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
1025                         }
1026
1027                         temp_cmd->id = i;
1028                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
1029                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
1030                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
1031                         }
1032                 } else {
1033                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matched result(%d)", result_id[i]);
1034                 }
1035         }
1036
1037         vc_cmd_print_list(vc_cmd_list);
1038
1039         SLOG(LOG_DEBUG, TAG_VCD, "[Server] @@@@");
1040
1041         int result_count = 0;
1042         vc_cmd_list_get_count(vc_cmd_list, &result_count);
1043
1044         if (false == vcd_client_manager_get_exclusive()) {
1045                 int pid = vcd_client_widget_get_foreground_pid();
1046                 if (-1 != pid) {
1047                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1048                         vcdc_send_show_tooltip(pid, false);
1049                 }
1050
1051                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
1052
1053                 if (-1 != vcd_client_manager_get_pid()) {
1054                         /* Manager client is available */
1055                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
1056                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
1057                         }
1058                 } else {
1059                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
1060                         /* Send result to client */
1061                         ecore_timer_add(0, __vcd_send_selected_result, NULL);
1062                 }
1063         } else {
1064                 /* exclusive command */
1065                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
1066
1067                 if (-1 != vcd_client_manager_get_pid()) {
1068                         /* Manager client is available */
1069                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
1070                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
1071                         }
1072                 } else {
1073                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
1074                 }
1075
1076                 vcd_client_manager_set_exclusive(false);
1077         }
1078
1079         vc_cmd_list_destroy(vc_cmd_list, true);
1080         vc_cmd_list = NULL;
1081
1082         return;
1083 #endif
1084 }
1085
1086 #if 0
1087 static void __vcd_server_nlu_result_cb(vce_result_event_e event, const char* nlu_result, void *user_data)
1088 {
1089         SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
1090         SECURE_SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
1091
1092         int ret = vc_info_parser_set_nlu_result(nlu_result);
1093         if (0 != ret) {
1094                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
1095         }
1096
1097         return;
1098 }
1099 #endif
1100
1101 int vcd_send_error(vce_error_e error, const char* msg, void *user_data)
1102 {
1103         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
1104         ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
1105
1106         char* error_msg = NULL;
1107         if (NULL != msg) {
1108                 error_msg = strdup(msg);
1109                 if (NULL == error_msg) {
1110                         return VCD_ERROR_OUT_OF_MEMORY;
1111                 }
1112         }
1113
1114         int ret = VCD_ERROR_NONE;
1115         ret = vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg);
1116         if (VCD_ERROR_NONE != ret) {
1117                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
1118         }
1119
1120         if (NULL != error_msg) {
1121                 free(error_msg);
1122                 error_msg = NULL;
1123         }
1124
1125         return ret;
1126 }
1127
1128 /* for TTS feedback */
1129 int vcd_send_feedback_audio_format(int rate, vce_audio_channel_e channel, vce_audio_type_e audio_type)
1130 {
1131         SLOG(LOG_INFO, TAG_VCD, "[Server DEBUG] Engine - Send TTS feedback audio format, g_current_uid(%d)", g_current_uid);
1132
1133         /* send TTS feedback audio format to VC manager */
1134         int ret = VCD_ERROR_NONE;
1135         int pid = g_current_uid / 1000;
1136         if (-1 == g_current_uid || vcd_client_manager_get_pid() == pid) {
1137                 ret = vcdc_send_feedback_audio_format_to_manager(vcd_client_manager_get_pid(), rate, channel, audio_type);
1138                 if (VCD_ERROR_NONE != ret) {
1139                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send TTS feedback audio format to VC manager");
1140                 }
1141         } else {
1142                 SLOG(LOG_INFO, TAG_VCD, "[Server INFO] Do not send TTS feedback audio format to VC manager");
1143         }
1144
1145         return ret;
1146 }
1147
1148 int vcd_send_feedback_streaming(vce_feedback_event_e event, char* buffer, int len)
1149 {
1150         if (-1 == g_current_uid && VCE_FEEDBACK_EVENT_START == event) {
1151                 g_current_utt_id = (g_current_utt_id + 1) % 1000;
1152                 g_current_uid = vcd_client_manager_get_pid() * 1000 + g_current_utt_id;
1153                 SLOG(LOG_INFO, TAG_VCD, "[Server info] set current uid and utt_id as manager pid(%d)", vcd_client_manager_get_pid());
1154         }
1155
1156         int ret = VCD_ERROR_NONE;
1157         int pid = g_current_uid / 1000;
1158         int utt_id = g_current_uid % 1000;
1159
1160         SLOG(LOG_INFO, TAG_VCD, "[Server DEBUG] Engine - Send TTS feedback streaming event(%d), uid(%d), is_mgr_client(%d)", event, g_current_uid, (pid == vcd_client_manager_get_pid() ? true : false));
1161
1162         if (pid == vcd_client_manager_get_pid()) {
1163                 /* send TTS feedback streaming to manager client */
1164                 ret = vcdc_send_feedback_streaming_to_manager(vcd_client_manager_get_pid(), pid, utt_id, event, buffer, len);
1165                 if (VCD_ERROR_NONE != ret) {
1166                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send TTS feedback streaming to manager client");
1167                 }
1168         } else {
1169                 /* send TTS feedback streaming to client */
1170                 ret = vcdc_send_feedback_streaming(pid, utt_id, event, buffer, len);
1171                 if (VCD_ERROR_NONE != ret) {
1172                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send TTS feedback streaming to client");
1173                 }
1174         }
1175
1176         if (VCE_FEEDBACK_EVENT_FINISH == event) {
1177                 /* reset current uid */
1178                 g_current_uid = -1;
1179
1180                 /* Set service state to ready if state is synthesizing */
1181                 vcd_state_e state = vcd_config_get_service_state();
1182                 if (VCD_STATE_SYNTHESIZING == state) {
1183                         vcd_config_set_service_state(VCD_STATE_READY);
1184                 }
1185                 SLOG(LOG_INFO, TAG_VCD, "[Server info] feedback streaming finish event, reset current uid & service state(%d)", vcd_config_get_service_state());
1186         }
1187         return ret;
1188 }
1189
1190
1191 /*
1192 * vcd server Interfaces
1193 */
1194 static void __vcd_file_clean_up()
1195 {
1196         SLOG(LOG_INFO, TAG_VCD, "== Old file clean up == ");
1197
1198         DIR *dp = NULL;
1199         struct dirent *dirp = NULL;
1200
1201         dp = opendir(VC_RUNTIME_INFO_ROOT);
1202         if (dp == NULL) {
1203                 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
1204                 return;
1205         }
1206
1207         char remove_path[257] = {0, };
1208         do {
1209                 dirp = readdir(dp);
1210
1211                 if (NULL != dirp) {
1212                         if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
1213                                 memset(remove_path, 0, 257);
1214                                 snprintf(remove_path, 257, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
1215
1216                                 /* Clean up code */
1217                                 if (0 != remove(remove_path)) {
1218                                         SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
1219                                 } else {
1220                                         SLOG(LOG_INFO, TAG_VCD, "[File message] Remove file : %s", remove_path);
1221                                 }
1222                         }
1223                 }
1224         } while (NULL != dirp);
1225
1226         closedir(dp);
1227
1228         return;
1229 }
1230
1231 static int __vcd_db_clean_up()
1232 {
1233         int ret = 0;
1234         int cnt = VC_COMMAND_TYPE_FOREGROUND;
1235         do {
1236                 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
1237                         ret = vc_db_delete_commands(-1, cnt, NULL);
1238         } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
1239
1240         return ret;
1241 }
1242
1243 static bool __is_default_engine()
1244 {
1245         char* engine = NULL;
1246         engine = vconf_get_str(VCONFKEY_VC_ENGINE_DEFAULT);
1247         if (NULL == engine) {
1248                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get sting for vc engine");
1249                 return FALSE;
1250         }
1251
1252         char appid[1024] = {'\0', };
1253         if (0 != aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1)) {
1254                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get callee appid by pid");
1255         }
1256
1257         SLOG(LOG_DEBUG, TAG_VCD, "[Server] VC Default Engine(%s), appId(%s)", engine, appid);
1258         if (0 == strncmp(engine, appid, strlen(engine))) {
1259                 free(engine);
1260                 return TRUE;
1261         }
1262         free(engine);
1263         return FALSE;
1264 }
1265
1266 int vcd_initialize(vce_request_callback_s *callback)
1267 {
1268         int ret = 0;
1269
1270         /* Remove old file */
1271         __vcd_file_clean_up();
1272
1273         /* initialize modules */
1274         ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
1275         if (0 != ret) {
1276                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
1277         }
1278
1279         ret = vc_db_initialize_for_daemon();
1280         if (0 != ret) {
1281                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to initialize DB : %d", ret);
1282                 return ret;
1283         }
1284
1285         /* Remove db data */
1286         ret = __vcd_db_clean_up();
1287         if (0 != ret) {
1288                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
1289         }
1290
1291         vcd_config_set_service_state(VCD_STATE_NONE);
1292
1293         ret = vcd_engine_agent_init();
1294         if (0 != ret) {
1295                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
1296                 return ret;
1297         }
1298
1299         if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
1300                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
1301                 return VCD_ERROR_OPERATION_FAILED;
1302         }
1303
1304         /* Load engine */
1305         if (0 != vcd_engine_agent_load_current_engine(callback)) {
1306                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1307                 return VCD_ERROR_OPERATION_FAILED;
1308         }
1309
1310         /* Initialize manager info */
1311         vcd_client_manager_unset();
1312
1313         if (TRUE == __is_default_engine()) {
1314                 /* Open dbus connection */
1315                 if (0 != vcd_dbus_open_connection()) {
1316                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open connection");
1317                         return VCD_ERROR_OPERATION_FAILED;
1318                 }
1319         }
1320
1321         vcd_config_set_service_state(VCD_STATE_READY);
1322 //      vcdc_send_service_state(VCD_STATE_READY);
1323
1324         /* Set timer cleanup client all */
1325         g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, vcd_cleanup_client_all, NULL);
1326         if (NULL == g_check_client_timer) {
1327                 SLOG(LOG_WARN, TAG_VCD, "[Server Warning] Fail to create timer of client check");
1328         }
1329
1330         g_current_uid = -1;
1331         g_current_utt_id = -1;
1332
1333         SLOG(LOG_ERROR, TAG_VCD, "[Server SUCCESS] initialize");
1334
1335         return 0;
1336 }
1337
1338 bool vcd_finalize()
1339 {
1340         GList *iter = NULL;
1341         if (0 < g_list_length(g_proc_list)) {
1342                 iter = g_list_first(g_proc_list);
1343                 while (NULL != iter) {
1344                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1345                         iter = g_list_first(g_proc_list);
1346                 }
1347         }
1348
1349         if (NULL != g_restart_timer) {
1350                 ecore_timer_del(g_restart_timer);
1351                 g_restart_timer = NULL;
1352         }
1353
1354         if (NULL != g_check_client_timer) {
1355                 ecore_timer_del(g_check_client_timer);
1356                 g_check_client_timer = NULL;
1357         }
1358
1359         vcd_state_e state = vcd_config_get_service_state();
1360         if (VCD_STATE_READY != state) {
1361                 if (VCD_STATE_RECORDING == state) {
1362                         vcd_recorder_stop();
1363                 }
1364                 vcd_engine_recognize_cancel();
1365         }
1366
1367         if (0 != vcd_recorder_destroy()) {
1368                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1369                 return false;
1370         } else {
1371                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1372         }
1373
1374         if (0 != vcd_engine_agent_release()) {
1375                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1376                 return false;
1377         } else {
1378                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1379         }
1380
1381         vcd_client_manager_unset_appid();
1382
1383         int ret = vcd_config_finalize();
1384         if (0 != ret) {
1385                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to finalize config.");
1386         }
1387
1388         ret = vc_db_finalize();
1389         if (0 != ret) {
1390                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to finalize DB : %d", ret);
1391         }
1392
1393         vcd_config_set_service_state(VCD_STATE_NONE);
1394         vcdc_send_service_state(VCD_STATE_NONE);
1395
1396         /* Open dbus connection */
1397         if (0 != vcd_dbus_close_connection()) {
1398                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to close connection");
1399         }
1400
1401         SLOG(LOG_ERROR, TAG_VCD, "[Server] mode finalize");
1402
1403         return true;
1404 }
1405
1406 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1407 {
1408         bool ret = vcd_finalize();
1409         if (false == ret) {
1410                 return EINA_TRUE;
1411         } else {
1412                 ecore_main_loop_quit();
1413                 return EINA_FALSE;
1414         }
1415 }
1416
1417 static void __read_proc()
1418 {
1419         DIR *dp = NULL;
1420         struct dirent *dirp = NULL;
1421         int tmp;
1422
1423         GList *iter = NULL;
1424         if (0 < g_list_length(g_proc_list)) {
1425                 iter = g_list_first(g_proc_list);
1426                 while (NULL != iter) {
1427                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1428                         iter = g_list_first(g_proc_list);
1429                 }
1430         }
1431
1432         dp = opendir("/proc");
1433         if (NULL == dp) {
1434                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1435         } else {
1436                 do {
1437                         dirp = readdir(dp);
1438
1439                         if (NULL != dirp) {
1440                                 tmp = atoi(dirp->d_name);
1441                                 if (0 >= tmp)   continue;
1442                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1443                         }
1444                 } while (NULL != dirp);
1445                 closedir(dp);
1446         }
1447         return;
1448 }
1449
1450 static void __vcd_cleanup_client(vcd_client_type_e type)
1451 {
1452         int* client_list = NULL;
1453         int client_count = 0;
1454         int i = 0;
1455         int j = 0;
1456         bool exist = false;
1457         int mgr_pid = -1;
1458         int ret = -1;
1459
1460         if (VCD_CLIENT_TYPE_NORMAL == type) {
1461                 ret = vcd_client_get_list(&client_list, &client_count);
1462         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1463                 ret = vcd_client_widget_get_list(&client_list, &client_count);
1464         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1465                 mgr_pid = vcd_client_manager_get_pid();
1466                 client_list = &mgr_pid;
1467                 client_count = 1;
1468                 if (-1 == mgr_pid) {
1469                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1470                         return;
1471                 }
1472         }
1473
1474         if (0 == ret || mgr_pid > 0) {
1475                 SLOG(LOG_INFO, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1476                 if (NULL != client_list && client_count > 0) {
1477                         for (i = 0; i < client_count; i++) {
1478                                 exist = false;
1479                                 GList *iter = NULL;
1480                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1481                                         iter = g_list_nth(g_proc_list, j);
1482                                         if (NULL != iter) {
1483                                                 if (*(client_list + i) == GPOINTER_TO_INT(iter->data)) {
1484                                                         SLOG(LOG_INFO, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1485                                                         exist = true;
1486                                                         break;
1487                                                 }
1488                                         }
1489                                 }
1490
1491                                 if (false == exist) {
1492                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1493                                         if (VCD_CLIENT_TYPE_NORMAL == type)
1494                                                 vcd_server_finalize(*(client_list + i));
1495                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
1496                                                 vcd_server_widget_finalize(*(client_list + i));
1497                                         else
1498                                                 vcd_server_mgr_finalize(mgr_pid);
1499                                 }
1500                         }
1501                 }
1502                 SLOG(LOG_INFO, TAG_VCD, "@@@");
1503         }
1504         if (NULL != client_list && -1 == mgr_pid) {
1505                 free(client_list);
1506                 client_list = NULL;
1507         }
1508         return;
1509 }
1510
1511 Eina_Bool vcd_cleanup_client_all(void *data)
1512 {
1513         __read_proc();
1514
1515         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1516         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1517         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1518
1519         if (0 == vcd_client_get_ref_count()) {
1520                 SLOG(LOG_INFO, TAG_VCD, "[Server] Connected client list is empty");
1521                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1522         }
1523 #if 0
1524         if (0 == vcd_client_get_list(&client_list, &client_count)) {
1525                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1526                 if (NULL != client_list && client_count > 0) {
1527                         for (i = 0; i < client_count; i++) {
1528                                 exist = false;
1529                                 GList *iter = NULL;
1530                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1531                                         iter = g_list_nth(g_proc_list, j);
1532                                         if (NULL != iter) {
1533                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1534                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1535                                                         exist = true;
1536                                                         break;
1537                                                 }
1538                                         }
1539                                 }
1540
1541                                 if (false == exist) {
1542                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1543                                         vcd_server_finalize(client_list[i]);
1544                                 }
1545 #if 0
1546                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1547
1548                                 if (0 == result) {
1549                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1550                                         vcd_server_finalize(client_list[i]);
1551                                 } else if (-1 == result) {
1552                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1553                                 }
1554 #endif
1555                         }
1556                 }
1557                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1558         }
1559         if (NULL != client_list) {
1560                 free(client_list);
1561                 client_list = NULL;
1562         }
1563
1564         /* If app is in background state, app cannot response message. */
1565         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1566                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1567                 if (NULL != client_list && client_count > 0) {
1568                         for (i = 0; i < client_count; i++) {
1569                                 exist = false;
1570                                 GList *iter = NULL;
1571                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1572                                         iter = g_list_nth(g_proc_list, j);
1573                                         if (NULL != iter) {
1574                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1575                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1576                                                         exist = true;
1577                                                         break;
1578                                                 }
1579                                         }
1580                                 }
1581
1582                                 if (false == exist) {
1583                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1584                                         vcd_server_widget_finalize(client_list[i]);
1585                                 }
1586 #if 0
1587                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1588
1589                                 if (0 == result) {
1590                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1591                                         vcd_server_widget_finalize(client_list[i]);
1592                                 } else if (-1 == result) {
1593                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1594                                 }
1595 #endif
1596                         }
1597                 }
1598                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1599         }
1600
1601         if (NULL != client_list) {
1602                 free(client_list);
1603                 client_list = NULL;
1604         }
1605
1606         /* manager */
1607         exist = false;
1608         GList *iter = NULL;
1609         int mgr_pid = vcd_client_manager_get_pid();
1610         if (0 < mgr_pid) {
1611                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1612                         iter = g_list_nth(g_proc_list, j);
1613                         if (NULL != iter) {
1614                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1615                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1616                                         exist = true;
1617                                         break;
1618                                 }
1619                         }
1620                 }
1621
1622                 if (false == exist) {
1623                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1624                         vcd_server_mgr_finalize(mgr_pid);
1625                 }
1626         }
1627 #endif
1628         return EINA_TRUE;
1629 }
1630
1631 int vcd_server_get_service_state()
1632 {
1633         return vcd_config_get_service_state();
1634 }
1635
1636 int vcd_server_get_foreground()
1637 {
1638         int pid;
1639         vcd_config_get_foreground(&pid);
1640         return pid;
1641 }
1642
1643 static Eina_Bool __vcd_send_service_state(void *data)
1644 {
1645         vcd_config_set_service_state(VCD_STATE_READY);
1646         vcdc_send_service_state(VCD_STATE_READY);
1647
1648         SLOG(LOG_INFO, TAG_VCD, "[Server Success] success to send service status for READY");
1649
1650         return EINA_FALSE;
1651 }
1652
1653 /*
1654 * API for manager
1655 */
1656 int vcd_server_mgr_initialize(int pid)
1657 {
1658         /* check if pid is valid */
1659         if (false == vcd_client_manager_is_valid(pid)) {
1660                 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1661                 vcd_server_mgr_cancel();
1662                 vcd_client_manager_unset();
1663         }
1664
1665         /* Add client information to client manager */
1666         int ret = vcd_client_manager_set(pid);
1667         if (0 != ret) {
1668                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1669                 return VCD_ERROR_OPERATION_FAILED;
1670         }
1671
1672         if (0 != vcdc_send_manager_pid(pid))
1673                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1674
1675         ecore_timer_add(0.05, __vcd_send_service_state, NULL);
1676
1677         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1678
1679         return VCD_ERROR_NONE;
1680 }
1681
1682 int vcd_server_mgr_finalize(int pid)
1683 {
1684         /* check if pid is valid */
1685         if (false == vcd_client_manager_is_valid(pid)) {
1686                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1687                 return VCD_ERROR_INVALID_PARAMETER;
1688         }
1689
1690         /* Cancel recognition */
1691         vcd_server_mgr_cancel();
1692
1693         if (0 != vcdc_send_manager_pid(-1))
1694                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1695
1696         /* Remove manager information */
1697         if (0 != vcd_client_manager_unset()) {
1698                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1699         }
1700
1701         if (0 == vcd_client_get_ref_count()) {
1702                 SLOG(LOG_INFO, TAG_VCD, "[Server] Connected client list is empty");
1703                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1704         }
1705
1706         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1707
1708         return VCD_ERROR_NONE;
1709 }
1710
1711 int vcd_server_mgr_set_command(int pid)
1712 {
1713         if (0 != vcd_client_manager_set_command(pid)) {
1714                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1715                 return VCD_ERROR_INVALID_PARAMETER;
1716         }
1717         return VCD_ERROR_NONE;
1718 }
1719
1720 int vcd_server_mgr_unset_command(int pid)
1721 {
1722         if (0 != vcd_client_manager_unset_command(pid)) {
1723                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1724                 return VCD_ERROR_INVALID_PARAMETER;
1725         }
1726         return VCD_ERROR_NONE;
1727 }
1728
1729 int vcd_server_mgr_set_demandable_client(int pid)
1730 {
1731         /* check if pid is valid */
1732         if (false == vcd_client_manager_is_valid(pid)) {
1733                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1734                 return VCD_ERROR_INVALID_PARAMETER;
1735         }
1736
1737         GSList* client_list = NULL;
1738         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1739                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1740                 return VCD_ERROR_OPERATION_FAILED;
1741         }
1742
1743         /* Save client list */
1744         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1745                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1746                 return VCD_ERROR_OPERATION_FAILED;
1747         }
1748
1749         return VCD_ERROR_NONE;
1750 }
1751
1752 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1753 {
1754         /* check if pid is valid */
1755         if (false == vcd_client_manager_is_valid(pid)) {
1756                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1757                 return VCD_ERROR_INVALID_PARAMETER;
1758         }
1759
1760         int ret = 0;
1761         vce_audio_type_e type = VCE_AUDIO_TYPE_PCM_S16_LE;
1762         int rate = 16000;
1763         int channel = 1;
1764
1765         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1766         if (0 != ret) {
1767                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1768                 return VCD_ERROR_OPERATION_FAILED;
1769         }
1770
1771         ret = vcd_recorder_set(audio_type, type, rate, channel);
1772         if (0 != ret) {
1773                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1774                 return VCD_ERROR_OPERATION_FAILED;
1775         }
1776
1777         return VCD_ERROR_NONE;
1778 }
1779
1780 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1781 {
1782         /* check if pid is valid */
1783         if (false == vcd_client_manager_is_valid(pid)) {
1784                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1785                 return VCD_ERROR_INVALID_PARAMETER;
1786         }
1787
1788         int ret = vcd_recorder_get(audio_type);
1789         if (0 != ret) {
1790                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1791                 return VCD_ERROR_OPERATION_FAILED;
1792         }
1793
1794         return VCD_ERROR_NONE;
1795 }
1796
1797 int vcd_server_mgr_set_client_info(int pid)
1798 {
1799         /* check if pid is valid */
1800         if (false == vcd_client_manager_is_valid(pid)) {
1801                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1802                 return VCD_ERROR_INVALID_PARAMETER;
1803         }
1804
1805         int ret = vcd_client_save_client_info();
1806         if (0 != ret) {
1807                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1808                 return VCD_ERROR_OPERATION_FAILED;
1809         }
1810
1811         return VCD_ERROR_NONE;
1812 }
1813
1814 static int __reset_waiting_for_widget_recording(void)
1815 {
1816         SLOG(LOG_ERROR, TAG_VCD, "[Server] Reset waiting for widget recording");
1817         // Delete timer to check that widget client is terminated
1818         if (g_check_widget_client_timer) {
1819                 ecore_timer_del(g_check_widget_client_timer);
1820                 g_check_widget_client_timer = NULL;
1821         }
1822         // Reset flag to wait for recording from widget client
1823         vcd_client_widget_set_waiting_for_recording(-1, false);
1824         return 0;
1825 }
1826
1827 #if 0
1828 static Eina_Bool __send_waiting_timeout_error_to_manager(void* data)
1829 {
1830         intptr_t ppid = (intptr_t)data;
1831         int pid = (int)ppid;
1832         SLOG(LOG_ERROR, TAG_VCD, "Widget client didn't send to start recording, pid(%d)", pid);
1833         __reset_waiting_for_widget_recording();
1834
1835         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1836         return EINA_FALSE;
1837 }
1838 #else
1839 static Eina_Bool __send_waiting_timeout_start_recording(void* data)
1840 {
1841         intptr_t ppid = (intptr_t)data;
1842         int pid = (int)ppid;
1843         SLOG(LOG_ERROR, TAG_VCD, "Widget client didn't send to start recording, pid(%d)", pid);
1844
1845         int ret = __start_internal_recognition();
1846         if (0 != ret) {
1847                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : %d", ret);
1848                 return ret;
1849         }
1850         return EINA_FALSE;
1851 }
1852 #endif
1853
1854 static int __set_waiting_for_widget_recording(int pid)
1855 {
1856         SLOG(LOG_ERROR, TAG_VCD, "[Server] Set waiting for widget recording, pid(%d)", pid);
1857
1858         // Check if the app included widget client is terminated or not. If it is terminated, vcd_server_widget_finalize() function will be called
1859         // In that function, it will start recording
1860         intptr_t ppid = (intptr_t)pid;
1861         g_check_widget_client_timer = ecore_timer_add(2.0, __send_waiting_timeout_start_recording, (void*)ppid);
1862
1863         // Set flag to wait for recording from widget client
1864         vcd_client_widget_set_waiting_for_recording(pid, true);
1865         return 0;
1866 }
1867
1868 static int __start_internal_recognition()
1869 {
1870         int ret;
1871         __reset_waiting_for_widget_recording();
1872
1873         if (0 != vcd_client_command_collect_command()) {
1874                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1875                 /* Send error cb to manager */
1876                 int pid = vcd_client_widget_get_foreground_pid();
1877                 if (-1 != pid)
1878                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_framework.error.vcfw.collect_command_fail");
1879                 return VCD_ERROR_OPERATION_FAILED;
1880         }
1881
1882         /* 3. Set command to engine */
1883         ret = vcd_engine_set_commands();
1884         if (0 != ret) {
1885                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1886                 /* Send error cb to manager */
1887                 int pid = vcd_client_widget_get_foreground_pid();
1888                 if (-1 != pid)
1889                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_framework.error.engine.set_commands_fail");
1890                 return VCD_ERROR_OPERATION_FAILED;
1891         }
1892
1893         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Set command");
1894
1895         bool stop_by_silence = true;
1896         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1897                 stop_by_silence = false;
1898         }
1899
1900         vcd_client_manager_set_result_text(NULL);
1901
1902         /* 4. start recognition */
1903         ret = vcd_engine_recognize_start(stop_by_silence);
1904         if (0 != ret) {
1905                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1906                 /* Send error cb to manager */
1907                 int pid = vcd_client_widget_get_foreground_pid();
1908                 if (-1 != pid)
1909                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_framework.error.engine.start_fail");
1910                 return VCD_ERROR_OPERATION_FAILED;
1911         }
1912
1913         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start engine");
1914
1915 #if 1
1916         /* 5. recorder start */
1917         ret = vcd_recorder_start();
1918         if (0 != ret) {
1919                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1920                 vcd_engine_recognize_cancel();
1921                 /* Send error cb to manager */
1922                 int pid = vcd_client_widget_get_foreground_pid();
1923                 if (-1 != pid)
1924                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_framework.error.vcfw.send_rc_fail");
1925                 return ret;
1926         }
1927 #endif
1928
1929         vcd_config_set_service_state(VCD_STATE_RECORDING);
1930         vcdc_send_service_state(VCD_STATE_RECORDING);
1931
1932         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1933
1934         return 0;
1935 }
1936
1937 static Eina_Bool __vcd_request_show_tooltip(void *data)
1938 {
1939         int pid = vcd_client_widget_get_foreground_pid();
1940         if (-1 != pid) {
1941                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command, show(%d)", (bool)data);
1942                 vcdc_send_show_tooltip(pid, (bool)data);
1943         }
1944
1945         return EINA_FALSE;
1946 }
1947
1948 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1949 {
1950         /* 1. check current state */
1951         vcd_state_e state = vcd_config_get_service_state();
1952
1953         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
1954                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready and not synthesizine, state(%d)", state);
1955                 return VCD_ERROR_INVALID_STATE;
1956         }
1957         if (-1 == vcd_client_manager_get_pid()) {
1958                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1959                 return VCD_ERROR_OPERATION_FAILED;
1960         }
1961         __reset_waiting_for_widget_recording();
1962
1963         SLOG(LOG_ERROR, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1964         vcd_client_set_recognition_mode(recognition_mode);
1965
1966         if (false == exclusive_cmd) {
1967                 vcd_client_update_foreground_pid();
1968                 /* Notify show tooltip */
1969                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1970                         int pid = vcd_client_widget_get_foreground_pid();
1971                         if (-1 != pid) {
1972                                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command");
1973                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1974
1975                                 __set_waiting_for_widget_recording(pid);
1976                                 return 0;
1977                         }
1978                 }
1979         } else {
1980                 vcd_client_manager_set_exclusive(exclusive_cmd);
1981         }
1982
1983         int fg_pid = -1;
1984         if (true == start_by_client) {
1985                 /* Get foreground pid */
1986                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1987                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1988                 }
1989
1990                 /* Set client exclusive option */
1991                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1992                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1993                 }
1994         }
1995
1996         SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recognition");
1997
1998         int ret = __start_internal_recognition();
1999         if (0 != ret) {
2000                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : %d", ret);
2001                 return ret;
2002         }
2003
2004         if (true == start_by_client) {
2005                 vcd_client_unset_exclusive_command(fg_pid);
2006         }
2007
2008         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] start internal recognition");
2009         return VCD_ERROR_NONE;
2010 }
2011
2012 int vcd_server_mgr_stop()
2013 {
2014         /* 1. Check current state is recording */
2015         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
2016                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
2017                 return VCD_ERROR_INVALID_STATE;
2018         }
2019         if (-1 == vcd_client_manager_get_pid()) {
2020                 SLOG(LOG_INFO, TAG_VCD, "[Server] Manager is NOT available.");
2021                 return VCD_ERROR_OPERATION_FAILED;
2022         }
2023         SLOG(LOG_ERROR, TAG_VCD, "[Server] stop internal recognition");
2024
2025 #if 1
2026         /* 2. Stop recorder */
2027         vcd_recorder_stop();
2028 #endif
2029
2030         /* 3. Stop engine recognition */
2031         int ret = vcd_engine_recognize_stop();
2032         if (0 != ret) {
2033                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
2034         }
2035
2036         /* 4. Set original mode */
2037         vcd_config_set_service_state(VCD_STATE_PROCESSING);
2038         vcdc_send_service_state(VCD_STATE_PROCESSING);
2039
2040         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] stop internal recognition");
2041         return VCD_ERROR_NONE;
2042 }
2043
2044 int vcd_server_mgr_cancel()
2045 {
2046         if (-1 == vcd_client_manager_get_pid()) {
2047                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Manager is NOT available.");
2048                 return VCD_ERROR_OPERATION_FAILED;
2049         }
2050
2051         if (g_restart_timer != NULL) {
2052                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
2053                 ecore_timer_del(g_restart_timer);
2054                 g_restart_timer = NULL;
2055         }
2056
2057         /* 1. Check current state */
2058         vcd_state_e state = vcd_config_get_service_state();
2059         if (VCD_STATE_READY == state) {
2060                 SLOG(LOG_INFO, TAG_VCD, "[Server] Current state is READY");
2061                 vcd_recorder_stop();
2062
2063                 if (false == vcd_client_manager_get_exclusive()) {
2064                         if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
2065                                 int pid = vcd_client_widget_get_foreground_pid();
2066                                 if (-1 != pid) {
2067                                         SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip hide");
2068                                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2069                                 }
2070                         }
2071                 }
2072
2073                 vcdc_send_service_state(VCD_STATE_READY);
2074                 return VCD_ERROR_NONE;
2075         }
2076         SLOG(LOG_ERROR, TAG_VCD, "[Server] cancel internal recognition");
2077
2078 #if 1
2079         /* 2. Stop recorder */
2080         vcd_recorder_stop();
2081 #endif
2082
2083         /* 3. Cancel engine */
2084         int ret = vcd_engine_recognize_cancel();
2085         if (0 != ret) {
2086                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
2087         }
2088
2089         if (false == vcd_client_manager_get_exclusive()) {
2090                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
2091                         int pid = vcd_client_widget_get_foreground_pid();
2092                         if (-1 != pid) {
2093                                 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip hide");
2094                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2095                         }
2096                 }
2097         } else {
2098                 vcd_client_manager_set_exclusive(false);
2099         }
2100
2101         /* 4. Set state */
2102         vcd_config_set_service_state(VCD_STATE_READY);
2103         vcdc_send_service_state(VCD_STATE_READY);
2104
2105         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] cancel internal recognition");
2106         return VCD_ERROR_NONE;
2107 }
2108
2109
2110 int vcd_server_mgr_result_select()
2111 {
2112         __vcd_send_selected_result(NULL);
2113
2114         return VCD_ERROR_NONE;
2115 }
2116
2117 int vcd_server_mgr_set_domain(int pid, const char* domain)
2118 {
2119         /* check if pid is valid */
2120         if (false == vcd_client_manager_is_valid(pid)) {
2121                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2122                 return VCD_ERROR_INVALID_PARAMETER;
2123         }
2124
2125         vcd_state_e state = vcd_config_get_service_state();
2126         if (VCD_STATE_READY != state) {
2127                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
2128                 return VCD_ERROR_INVALID_STATE;
2129         }
2130
2131         /* Set domain to engine */
2132         int ret = vcd_engine_set_domain(pid, domain);
2133         if (0 != ret) {
2134                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
2135         } else {
2136                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Set domain");
2137         }
2138
2139         return ret;
2140 }
2141
2142 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
2143 {
2144         /* check if pid is valid */
2145         if (false == vcd_client_manager_is_valid(pid)) {
2146                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2147                 return VCD_ERROR_INVALID_PARAMETER;
2148         }
2149
2150         vcd_state_e state = vcd_config_get_service_state();
2151         if (VCD_STATE_READY != state) {
2152                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
2153                 return VCD_ERROR_INVALID_STATE;
2154         }
2155
2156         /* Set private data to engine */
2157         int ret = vcd_engine_set_private_data(pid, key, data);
2158         if (0 != ret) {
2159                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
2160         } else {
2161                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Set private data");
2162         }
2163
2164         return ret;
2165 }
2166
2167 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
2168 {
2169         /* check if pid is valid */
2170         if (false == vcd_client_manager_is_valid(pid)) {
2171                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2172                 return VCD_ERROR_INVALID_PARAMETER;
2173         }
2174         vcd_state_e state = vcd_config_get_service_state();
2175         if (VCD_STATE_READY != state) {
2176                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
2177                 return VCD_ERROR_INVALID_STATE;
2178         }
2179
2180         /* Get private data to engine */
2181         int ret = vcd_engine_get_private_data(pid, key, data);
2182         if (0 != ret) {
2183                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
2184         } else {
2185                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Set private data");
2186         }
2187
2188         return ret;
2189 }
2190
2191 int vcd_server_mgr_send_specific_engine_request(int pid, const char* engine_app_id, const char* event, const char* request)
2192 {
2193         /* check if pid is valid */
2194         if (false == vcd_client_manager_is_valid(pid)) {
2195                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2196                 return VCD_ERROR_INVALID_PARAMETER;
2197         }
2198         vcd_state_e state = vcd_config_get_service_state();
2199         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2200                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready and not synthesizing, state(%d)", state);
2201                 return VCD_ERROR_INVALID_STATE;
2202         }
2203
2204         /* Get private data to engine */
2205         int ret = vcd_engine_send_specific_engine_request(engine_app_id, event, request);
2206         if (0 != ret) {
2207                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set specific engine request : %d", ret);
2208         } else {
2209                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Set specific engine request ");
2210         }
2211
2212         return ret;
2213 }
2214
2215 int vcd_server_mgr_do_action(int pid, int type, const char* action)
2216 {
2217         int ret = -1;
2218
2219         /* check if pid is valid */
2220         if (false == vcd_client_manager_is_valid(pid)) {
2221                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2222                 return VCD_ERROR_INVALID_PARAMETER;
2223         }
2224
2225         vcd_state_e state = vcd_config_get_service_state();
2226         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2227                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready and not synthesizing, state(%d)", state);
2228                 return VCD_ERROR_INVALID_STATE;
2229         }
2230
2231         /* Request do action to engine */
2232         if (VCD_SEND_EVENT_TYPE_TEXT == type)
2233                 ret = vcd_engine_process_text(pid, action);
2234         else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
2235                 ret = vcd_engine_process_list_event(pid, action);
2236         else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
2237                 ret = vcd_engine_process_haptic_event(pid, action);
2238
2239         if (0 != ret) {
2240                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
2241         } else {
2242                 vcd_config_set_service_state(VCD_STATE_PROCESSING);
2243                 vcdc_send_service_state(VCD_STATE_PROCESSING);
2244                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Process do action");
2245         }
2246
2247         return ret;
2248 }
2249
2250 int vcd_server_mgr_set_disabled_command_type(int pid, int disabled_cmd_type)
2251 {
2252         int ret = -1;
2253
2254         /* check if pid is valid */
2255         if (false == vcd_client_manager_is_valid(pid)) {
2256                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2257                 return VCD_ERROR_INVALID_PARAMETER;
2258         }
2259
2260         vcd_state_e state = vcd_config_get_service_state();
2261         if (VCD_STATE_READY != state) {
2262                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
2263                 return VCD_ERROR_INVALID_STATE;
2264         }
2265
2266         ret = vcd_config_set_disabled_command_type(disabled_cmd_type);
2267         if (0 != ret) {
2268                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type");
2269         } else {
2270                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Set command type(%d)", disabled_cmd_type);
2271         }
2272
2273         return ret;
2274 }
2275
2276
2277 /* for TTS feedback */
2278 int vcd_server_mgr_start_feedback(void)
2279 {
2280         /* check current state */
2281         /* not Recording??? */
2282
2283         if (-1 == vcd_client_manager_get_pid()) {
2284                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Manager is NOT available");
2285                 return VCD_ERROR_OPERATION_FAILED;
2286         }
2287
2288         SLOG(LOG_INFO, TAG_VCD, "[Server] start TTS feedback");
2289
2290         /* check there is TTS buffer to be spoken */
2291
2292         return VCD_ERROR_NONE;
2293 }
2294
2295 int vcd_server_mgr_stop_feedback(void)
2296 {
2297         /* check current state */
2298         /* not Recording??? */
2299
2300         if (-1 == vcd_client_manager_get_pid()) {
2301                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Manager is NOT available");
2302                 return VCD_ERROR_OPERATION_FAILED;
2303         }
2304
2305         SLOG(LOG_INFO, TAG_VCD, "[Server] stop TTS feedback");
2306
2307         return VCD_ERROR_NONE;
2308 }
2309
2310 int vcd_server_mgr_send_audio_streaming(int pid, int event, unsigned char* buffer, unsigned int len)
2311 {
2312         SLOG(LOG_INFO, TAG_VCD, "[DEBUG] Send Audio Streaming from Multi-assistant. event(%d), buffer(%p), len(%d)", event, &buffer, len);
2313
2314         int ret = 0;
2315         if (VCD_AUDIO_STREAMING_EVENT_START == event) {
2316                 ret = vcd_recorder_start_streaming();
2317                 if (0 != ret) {
2318                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to start streaming, ret(%d)", ret);
2319                         return ret;
2320                 }
2321         }
2322
2323         ret = vcd_recorder_send_streaming((const void*)buffer, (const unsigned int)len);
2324         if (0 != ret) {
2325                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to start streaming, ret(%d)", ret);
2326                 return ret;
2327         }
2328
2329         if (VCD_AUDIO_STREAMING_EVENT_FINISH == event) {
2330                 ret = vcd_recorder_stop_streaming();
2331                 if (0 != ret) {
2332                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to stop streaming, ret(%d)", ret);
2333                         return ret;
2334                 }
2335         }
2336
2337         return VCD_ERROR_NONE;
2338 }
2339
2340 int vcd_server_mgr_change_system_volume(int pid, vcd_system_volume_event_e system_volume_event)
2341 {
2342         SLOG(LOG_INFO, TAG_VCD, "[DEBUG] change system volume, system volume event(%d)", system_volume_event);
2343
2344         int ret = 0;
2345         if (VCD_SYSTEM_VOLUME_EVENT_CHANGE == system_volume_event) {
2346                 ret = vcd_recorder_change_system_volume();
2347                 if (0 != ret) {
2348                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to change system volume, ret(%d)", ret);
2349                         return ret;
2350                 }
2351         } else if (VCD_SYSTEM_VOLUME_EVENT_RECOVER == system_volume_event) {
2352                 ret = vcd_recorder_recover_system_volume();
2353                 if (0 != ret) {
2354                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to recover system volume, ret(%d)", ret);
2355                         return ret;
2356                 }
2357         }
2358
2359         return ret;
2360 }
2361
2362
2363 /*
2364 * VC Server Functions for Client
2365 */
2366 int vcd_server_initialize(int pid)
2367 {
2368         if (false == vcd_engine_is_available_engine()) {
2369                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2370                 return VCD_ERROR_ENGINE_NOT_FOUND;
2371         }
2372
2373         /* check if pid is valid */
2374         if (true == vcd_client_is_available(pid)) {
2375                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2376                 return VCD_ERROR_NONE;
2377         }
2378
2379         /* Add client information to client manager */
2380         if (0 != vcd_client_add(pid)) {
2381                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2382                 return VCD_ERROR_OPERATION_FAILED;
2383         }
2384
2385         SLOG(LOG_INFO, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
2386
2387         return VCD_ERROR_NONE;
2388 }
2389
2390 int vcd_server_finalize(int pid)
2391 {
2392         /* check if pid is valid */
2393         if (false == vcd_client_is_available(pid)) {
2394                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2395                 return VCD_ERROR_INVALID_PARAMETER;
2396         }
2397
2398         /* Remove client information */
2399         if (0 != vcd_client_delete(pid)) {
2400                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2401         }
2402
2403         if (0 == vcd_client_get_ref_count()) {
2404                 SLOG(LOG_INFO, TAG_VCD, "[Server] Connected client list is empty");
2405                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2406         }
2407
2408         SLOG(LOG_INFO, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
2409
2410         return VCD_ERROR_NONE;
2411 }
2412
2413 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
2414 {
2415         /* check if pid is valid */
2416         if (false == vcd_client_is_available(pid)) {
2417                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2418                 return VCD_ERROR_INVALID_PARAMETER;
2419         }
2420
2421         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
2422                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
2423                 return VCD_ERROR_OPERATION_FAILED;
2424         }
2425
2426         return 0;
2427 }
2428
2429 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
2430 {
2431         /* check if pid is valid */
2432         if (false == vcd_client_is_available(pid)) {
2433                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2434                 return VCD_ERROR_INVALID_PARAMETER;
2435         }
2436
2437         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
2438                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
2439                 return VCD_ERROR_OPERATION_FAILED;
2440         }
2441
2442         return 0;
2443 }
2444
2445 int vcd_server_set_foreground(int pid, bool value)
2446 {
2447         /* check if pid is valid */
2448         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2449                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2450                 return VCD_ERROR_INVALID_PARAMETER;
2451         }
2452
2453         if (0 != vcd_config_set_foreground(pid, value)) {
2454                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
2455                 return VCD_ERROR_OPERATION_FAILED;
2456         }
2457
2458         return 0;
2459 }
2460
2461 static int __vcd_server_launch_manager_app()
2462 {
2463         int ret = -1;
2464         bool running = false;
2465         char* appid = NULL;
2466
2467         ret = vcd_client_manager_get_appid(&appid);
2468         if (0 != ret || NULL == appid) {
2469                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
2470                 return VCD_ERROR_OPERATION_FAILED;
2471         }
2472         ret = app_manager_is_running(appid, &running);
2473         if (0 != ret) {
2474                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
2475                 free(appid);
2476                 appid = NULL;
2477                 return VCD_ERROR_OPERATION_FAILED;
2478         }
2479         if (false == running) {
2480                 int tmp_ret = __vcd_is_package_installed(appid);
2481                 if (false == tmp_ret) {
2482                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
2483                 } else {
2484                         ret = __vcd_activate_app_by_appcontrol(appid);
2485                         if (0 != ret) {
2486                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
2487                                 free(appid);
2488                                 appid = NULL;
2489                                 return ret;
2490                         }
2491                         SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
2492                 }
2493         } else {
2494                 ret = __vcd_resume_app(appid);
2495                 if (0 != ret) {
2496                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2497                         free(appid);
2498                         appid = NULL;
2499                         return ret;
2500                 }
2501                 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2502         }
2503
2504         free(appid);
2505         appid = NULL;
2506
2507         return VCD_ERROR_NONE;
2508 }
2509
2510 int vcd_server_set_server_dialog(int pid, const char* app_id, const char* credential)
2511 {
2512         /* check if pid is valid */
2513         if (false == vcd_client_is_available(pid)) {
2514                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2515                 return VCD_ERROR_INVALID_PARAMETER;
2516         }
2517
2518         int ret = vcd_engine_set_server_dialog(app_id, credential);
2519         if (0 != ret) {
2520                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set server dialog, pid(%d), app_id(%s), ret(%d)", pid, app_id, ret);
2521                 vcd_client_set_server_dialog(pid, false);
2522                 return ret;
2523         }
2524         SLOG(LOG_ERROR, TAG_VCD, "[Success] Set server dialog, pid(%d), app_id(%s)", pid, app_id);
2525
2526         if (0 != strncmp(credential, "#NULL", strlen(credential))) {
2527                 ret = vcd_client_set_server_dialog(pid, true);
2528                 if (0 != ret)
2529                         SLOG(LOG_INFO, TAG_VCD, "[Server] Set to true for server dialog, app_id(%s)", app_id);
2530         } else {
2531                 ret = vcd_client_set_server_dialog(pid, false);
2532                 if (0 != ret)
2533                         SLOG(LOG_INFO, TAG_VCD, "[Server] Set to false for server dialog, app_id(%s)", app_id);
2534         }
2535
2536         return 0;
2537 }
2538
2539 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2540 {
2541         /* check if pid is valid */
2542         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2543                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2544                 return VCD_ERROR_INVALID_PARAMETER;
2545         }
2546
2547         bool is_server_dialog = false;
2548         int ret = vcd_client_get_server_dialog(pid, &is_server_dialog);
2549         if (0 != ret) {
2550                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get server dialog, pid(%d), ret(%d)", pid, ret);
2551         }
2552
2553         if (true == is_server_dialog) {
2554                 /* ++ Request tts event to engine */
2555
2556                 /* -- Request tts event to engine */
2557         } else {
2558                 ret = __vcd_server_launch_manager_app();
2559                 if (0 != ret) {
2560                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), pid(%d), disp_text(%s), utt_text(%s), continue(%d)", vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2561                         return ret;
2562                 }
2563
2564                 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2565                 if (0 != ret) {
2566                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send dialog : mgr_pid(%d), pid(%d), disp_text(%s), utt_text(%s), continue(%d)", vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2567                         return ret;
2568                 }
2569         }
2570         return 0;
2571 }
2572
2573 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2574 {
2575         /* check if pid is valid */
2576         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2577                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2578                 return VCD_ERROR_INVALID_PARAMETER;
2579         }
2580
2581         /* check if system command is valid */
2582         if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2583                 *is_sys_cmd_valid = true;
2584         else
2585                 *is_sys_cmd_valid = false;
2586
2587         return 0;
2588 }
2589
2590 static void __start_tts_request_thread(void* data, Ecore_Thread* thread)
2591 {
2592         SLOG(LOG_INFO, TAG_VCD, "[SUCCESS] Start tts request thread");
2593         vc_tts_text_data_s* tts_text_data = NULL;
2594
2595         while (1) {
2596                 int ret = -1;
2597                 int cnt = 0;
2598
2599                 /* Get tts text data */
2600                 ret = vcd_data_get_first_tts_text_data(&tts_text_data);
2601                 if (0 != ret || NULL == tts_text_data) {
2602                         /* empty queue */
2603                         if (0 >= vcd_data_get_tts_text_data_size()) {
2604                                 SLOG(LOG_INFO, TAG_VCD, "[DEBUG] No tts text data");
2605                                 return;
2606                         }
2607                         SLOG(LOG_INFO, TAG_VCD, "[INFO] tts text data is just incoming");
2608                         continue;
2609                 }
2610
2611                 while (1) {
2612                         vcd_state_e state = vcd_config_get_service_state();
2613                         if (VCD_STATE_READY != state) {
2614                                 if (0 == cnt++ % 10)
2615                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Waiting to request TTS, state(%d)", state);
2616                                 usleep(100000);
2617                                 continue;
2618                         }
2619                         break;
2620                 }
2621
2622                 /* Set service state to synthesizing */
2623                 vcd_config_set_service_state(VCD_STATE_SYNTHESIZING);
2624
2625                 /* Set current uid */
2626                 g_current_uid = tts_text_data->uid;
2627
2628                 /* Request tts to engine */
2629                 ret = vcd_engine_request_tts(tts_text_data->pid, tts_text_data->utt_id, tts_text_data->text, tts_text_data->language);
2630                 if (0 != ret) {
2631                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to request tts : %d", ret);
2632                 } else {
2633                         SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] request tts, uid(%d) pid(%d), text(%s), language(%s), utt_id(%d)",
2634                                 tts_text_data->uid, tts_text_data->pid, tts_text_data->text, tts_text_data->language, tts_text_data->utt_id);
2635                 }
2636                 /* clear tts text data after use */
2637                 vcd_data_clear_tts_text_data(&tts_text_data);
2638
2639         }
2640 }
2641
2642 static void __end_tts_request_thread(void* data, Ecore_Thread* thread)
2643 {
2644         SLOG(LOG_INFO, TAG_VCD, "[SUCCESS] End tts request thread");
2645         g_tts_thread = NULL;
2646 }
2647
2648 int vcd_server_request_tts(int pid, const char* text, const char* language, int to_vcm, int* utt_id)
2649 {
2650         /* check if pid is valid */
2651         if (false == vcd_client_is_available(pid)) {
2652                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2653                 return VCD_ERROR_INVALID_PARAMETER;
2654         }
2655
2656         vcd_state_e state = vcd_config_get_service_state();
2657         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2658                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready, state(%d)", state);
2659                 return VCD_ERROR_INVALID_STATE;
2660         }
2661
2662         int uid = -1;
2663         g_current_utt_id = (g_current_utt_id + 1) % 1000;
2664         *utt_id = g_current_utt_id;
2665         if (0 == to_vcm) {
2666                 uid = pid * 1000 + g_current_utt_id;
2667         } else {
2668                 uid = vcd_client_manager_get_pid() * 1000 + g_current_utt_id;
2669         }
2670         SLOG(LOG_INFO, TAG_VCD, "[Server INFO] pid(%d), text(%s), language(%s), to_vcm(%d), ", pid, text, language, to_vcm);
2671         SLOG(LOG_INFO, TAG_VCD, "[Server INFO] current_uid(%d), current_utt_id(%d)", uid, g_current_utt_id);
2672
2673         vc_tts_text_data_s* tts_text_data;
2674         tts_text_data = (vc_tts_text_data_s*)calloc(1, sizeof(vc_tts_text_data_s));
2675         if (!tts_text_data) {
2676                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to callocate memory ");
2677                 return VCD_ERROR_OUT_OF_MEMORY;
2678         }
2679         tts_text_data->uid = uid;
2680         tts_text_data->pid = pid;
2681         tts_text_data->utt_id = g_current_utt_id;
2682         tts_text_data->text = strdup(text);
2683         tts_text_data->language = strdup(language);
2684
2685         int ret = vcd_data_add_tts_text_data(tts_text_data->uid, tts_text_data);
2686         if (0 != ret) {
2687                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add tts text data : %d", ret);
2688         }
2689
2690         bool is_canceled = ecore_thread_check(g_tts_thread);
2691         if (NULL == g_tts_thread || TRUE == is_canceled) {
2692                 SLOG(LOG_INFO, TAG_VCD, "[Server INFO] ecore thread run : start_tts_request_thread ");
2693                 g_tts_thread = ecore_thread_run(__start_tts_request_thread, __end_tts_request_thread, NULL, NULL);
2694         }
2695
2696         return 0;
2697 }
2698
2699 int vcd_server_cancel_tts(int pid, int utt_id)
2700 {
2701         /* check if pid is valid */
2702         if (false == vcd_client_is_available(pid)) {
2703                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2704                 return VCD_ERROR_INVALID_PARAMETER;
2705         }
2706
2707         vcd_state_e state = vcd_config_get_service_state();
2708         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2709                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready, state(%d)", state);
2710                 return VCD_ERROR_INVALID_STATE;
2711         }
2712
2713         vc_tts_text_data_s* tts_text_data = NULL;
2714
2715         int uid = pid * 1000 + utt_id;
2716         int ret = vcd_data_get_tts_text_data(uid, &tts_text_data);
2717         if (0 != ret) {
2718                 SLOG(LOG_WARN, TAG_VCD, "[Server WARN] No data in vcd tts text queue");
2719         } else {
2720                 SLOG(LOG_INFO, TAG_VCD, "[Server] Clear tts text data, pid(%d), utt_id(%d), text(%s)", pid, utt_id, tts_text_data->text);
2721                 vcd_data_clear_tts_text_data(&tts_text_data);
2722         }
2723
2724         /* Request tts to engine */
2725         ret = vcd_engine_cancel_tts(pid, utt_id);
2726         if (0 != ret) {
2727                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to cancel tts : %d", ret);
2728         } else {
2729                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] request tts, pid(%d), utt_id(%d)", pid, utt_id);
2730         }
2731
2732         return 0;
2733 }
2734
2735 int vcd_server_get_tts_audio_format(int pid, int* rate, int* channel, int* audio_type)
2736 {
2737         /* check if pid is valid */
2738         if (false == vcd_client_is_available(pid)) {
2739                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2740                 return VCD_ERROR_INVALID_PARAMETER;
2741         }
2742
2743         vcd_state_e state = vcd_config_get_service_state();
2744         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2745                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready and not synthesizing, state(%d)", state);
2746                 return VCD_ERROR_INVALID_STATE;
2747         }
2748
2749         /* Request tts to engine */
2750         int ret = vcd_engine_get_tts_audio_format(rate, channel, audio_type);
2751         if (0 != ret) {
2752                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get tts audio format : %d", ret);
2753         } else {
2754                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] get tts audio format, pid(%d), rate(%d), channel(%d), audio_type(%d)", pid, *rate, *channel, *audio_type);
2755         }
2756
2757         return ret;
2758 }
2759
2760 #if 0
2761 int vcd_server_set_exclusive_command(int pid, bool value)
2762 {
2763         /* check if pid is valid */
2764         if (false == vcd_client_is_available(pid)) {
2765                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2766                 return VCD_ERROR_INVALID_PARAMETER;
2767         }
2768
2769         if (true == value) {
2770                 if (0 != vcd_client_set_exclusive_command(pid)) {
2771                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2772                         return VCD_ERROR_OPERATION_FAILED;
2773                 }
2774         } else {
2775                 if (0 != vcd_client_unset_exclusive_command(pid)) {
2776                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2777                         return VCD_ERROR_OPERATION_FAILED;
2778                 }
2779         }
2780
2781         return 0;
2782 }
2783
2784 int vcd_server_request_start(int pid, bool stop_by_silence)
2785 {
2786         /* check if pid is valid */
2787         if (false == vcd_client_is_available(pid)) {
2788                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT foreground client", pid);
2789                 return VCD_ERROR_INVALID_PARAMETER;
2790         }
2791
2792         int ret;
2793         /* Check current state */
2794         vcd_state_e state = vcd_config_get_service_state();
2795
2796         /* Service state should be ready */
2797         if (VCD_STATE_READY != state) {
2798                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2799                 return VCD_ERROR_INVALID_STATE;
2800         }
2801
2802         if (-1 != vcd_client_manager_get_pid()) {
2803                 /* Check current pid is valid */
2804                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2805                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2806                         return VCD_ERROR_INVALID_PARAMETER;
2807                 }
2808         }
2809
2810         ret = vcd_server_mgr_start(stop_by_silence, false);
2811         if (0 != ret) {
2812                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2813                 return VCD_ERROR_INVALID_PARAMETER;
2814         }
2815
2816         return 0;
2817 }
2818
2819 int vcd_server_request_stop(int pid)
2820 {
2821         int ret;
2822         /* Check current state */
2823         vcd_state_e state = vcd_config_get_service_state();
2824
2825         /* Service state should be ready */
2826         if (VCD_STATE_RECORDING != state) {
2827                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2828                 return VCD_ERROR_INVALID_STATE;
2829         }
2830
2831         if (-1 != vcd_client_manager_get_pid()) {
2832                 /* Check current pid is valid */
2833                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2834                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2835                         return VCD_ERROR_INVALID_PARAMETER;
2836                 }
2837         }
2838
2839         ret = vcd_server_mgr_stop();
2840         if (0 != ret) {
2841                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2842                 return VCD_ERROR_OPERATION_FAILED;
2843         }
2844
2845         return VCD_ERROR_NONE;
2846 }
2847
2848 int vcd_server_request_cancel(int pid)
2849 {
2850         int ret;
2851         /* Check current state */
2852         vcd_state_e state = vcd_config_get_service_state();
2853
2854         /* Service state should be recording or processing */
2855         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2856                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2857                 return VCD_ERROR_INVALID_STATE;
2858         }
2859
2860         if (-1 != vcd_client_manager_get_pid()) {
2861                 /* Check current pid is valid */
2862                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2863                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2864                         return VCD_ERROR_INVALID_PARAMETER;
2865                 }
2866         }
2867
2868         ret = vcd_server_mgr_cancel();
2869         if (0 != ret) {
2870                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2871                 return VCD_ERROR_OPERATION_FAILED;
2872         }
2873
2874         return VCD_ERROR_NONE;
2875 }
2876 #endif
2877
2878 /*
2879 * VC Server Functions for Widget lib
2880 */
2881 int vcd_server_widget_initialize(int pid)
2882 {
2883         if (false == vcd_engine_is_available_engine()) {
2884                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2885                 return VCD_ERROR_ENGINE_NOT_FOUND;
2886         }
2887
2888         /* check if pid is valid */
2889         if (true == vcd_client_widget_is_available(pid)) {
2890                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2891                 return VCD_ERROR_NONE;
2892         }
2893
2894         /* Add client information to client manager */
2895         if (0 != vcd_client_widget_add(pid)) {
2896                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2897                 return VCD_ERROR_OPERATION_FAILED;
2898         }
2899
2900         SLOG(LOG_INFO, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2901
2902         return VCD_ERROR_NONE;
2903 }
2904
2905 static void __vcd_server_widget_start_recording(void *data)
2906 {
2907         SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] start recording");
2908
2909         if (0 != __start_internal_recognition()) {
2910                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition");
2911         }
2912 }
2913
2914 int vcd_server_widget_finalize(int pid)
2915 {
2916         /* check if pid is valid */
2917         if (false == vcd_client_widget_is_available(pid)) {
2918                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2919                 if (0 == vcd_client_get_ref_count()) {
2920                         SLOG(LOG_ERROR, TAG_VCD, "[Server] connected client list is empty, vc-service will be terminated");
2921                         ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2922                         return 0;
2923                 }
2924                 return VCD_ERROR_INVALID_PARAMETER;
2925         }
2926
2927         /* Remove client information */
2928         if (0 != vcd_client_widget_delete(pid)) {
2929                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2930         }
2931
2932         if (0 == vcd_client_get_ref_count()) {
2933                 SLOG(LOG_ERROR, TAG_VCD, "[Server] connected client list is empty, vc-service will be terminated");
2934                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2935                 return 0;
2936         }
2937
2938         bool is_waiting = false;
2939         if (0 != vcd_client_widget_get_waiting_for_recording(pid, &is_waiting)) {
2940                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get waiting to recording");
2941         }
2942
2943         if (true == is_waiting) {
2944                 SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] invoke to start recording");
2945                 ecore_main_loop_thread_safe_call_async(__vcd_server_widget_start_recording, NULL);
2946         }
2947         return VCD_ERROR_NONE;
2948 }
2949
2950 int vcd_server_widget_start_recording(int pid, bool widget_command)
2951 {
2952         /* check if pid is valid */
2953         if (false == vcd_client_widget_is_available(pid)) {
2954                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2955                 return VCD_ERROR_INVALID_PARAMETER;
2956         }
2957
2958         bool waiting;
2959         if (0 != vcd_client_widget_get_waiting_for_recording(pid, &waiting) || false == waiting) {
2960                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Server is not waiting for recording, pid(%d), waiting(%d)", pid, waiting);
2961                 return 0;
2962         }
2963
2964         if (true == widget_command) {
2965                 vcd_client_widget_set_command(pid);
2966                 SLOG(LOG_INFO, TAG_VCD, "[Server] widget command is available");
2967         } else {
2968                 vcd_client_widget_unset_command(pid);
2969                 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2970         }
2971
2972         SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recognition : %d", widget_command);
2973         int ret = __start_internal_recognition();
2974         if (0 != ret) {
2975                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : %d", ret);
2976                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2977         }
2978
2979         return 0;
2980 }
2981
2982 int vcd_server_widget_start(int pid, bool stop_by_silence)
2983 {
2984         /* check if pid is valid */
2985         int fore_pid = vcd_client_widget_get_foreground_pid();
2986         if (pid != fore_pid) {
2987                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2988                 return VCD_ERROR_INVALID_PARAMETER;
2989         }
2990
2991         /* Check current state */
2992         vcd_state_e state = vcd_config_get_service_state();
2993
2994         /* Service state should be ready */
2995         if (VCD_STATE_READY != state) {
2996                 SLOG(LOG_INFO, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2997                 return VCD_ERROR_INVALID_STATE;
2998         }
2999
3000         vcd_client_set_slience_detection(stop_by_silence);
3001
3002         /* Notify show tooltip */
3003         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
3004
3005         return 0;
3006 }
3007
3008 int vcd_server_widget_stop(int pid)
3009 {
3010         /* check if pid is valid */
3011         int fore_pid = vcd_client_widget_get_foreground_pid();
3012         if (pid != fore_pid) {
3013                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
3014                 return VCD_ERROR_INVALID_PARAMETER;
3015         }
3016
3017         int ret;
3018         /* Check current state */
3019         vcd_state_e state = vcd_config_get_service_state();
3020
3021         /* Service state should be recording */
3022         if (VCD_STATE_RECORDING != state) {
3023                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
3024                 return VCD_ERROR_INVALID_STATE;
3025         }
3026
3027         ret = vcd_server_mgr_stop();
3028         if (0 != ret) {
3029                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to start recognition");
3030                 return VCD_ERROR_OPERATION_FAILED;
3031         }
3032
3033         return VCD_ERROR_NONE;
3034 }
3035
3036 int vcd_server_widget_cancel(int pid)
3037 {
3038         /* check if pid is valid */
3039         int fore_pid = vcd_client_widget_get_foreground_pid();
3040         if (pid != fore_pid) {
3041                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
3042                 return VCD_ERROR_INVALID_PARAMETER;
3043         }
3044
3045         int ret;
3046         /* Check current state */
3047         vcd_state_e state = vcd_config_get_service_state();
3048
3049         /* Service state should be recording or processing */
3050         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
3051                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
3052                 return VCD_ERROR_INVALID_STATE;
3053         }
3054
3055         ret = vcd_server_mgr_cancel();
3056         if (0 != ret) {
3057                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
3058                 return ret;
3059         }
3060
3061         return VCD_ERROR_NONE;
3062 }
3063
3064 int vcd_server_widget_enable_asr_result(int pid, bool enable)
3065 {
3066         int ret;
3067         ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
3068         if (0 != ret) {
3069                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
3070         }
3071
3072         return ret;
3073 }
3074
3075 int vcd_server_set_language(const char* language)
3076 {
3077         int ret = VCD_ERROR_NONE;
3078
3079         ret = vcd_config_set_default_language(language);
3080         if (0 != ret) {
3081                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to set language : %d", ret);
3082                 return ret;
3083         }
3084
3085         ret = vcd_engine_set_current_language(language);
3086         if (0 != ret) {
3087                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to set language : %d", ret);
3088                 return ret;
3089         }
3090
3091         return ret;
3092 }
3093 /*
3094 * For engine service
3095 */
3096 int vcd_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
3097 {
3098         SLOG(LOG_INFO, TAG_VCD, "[Server] Get foreach command");
3099
3100         if (NULL == callback) {
3101                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] input parameter is NULL");
3102                 return VCD_ERROR_INVALID_PARAMETER;
3103         }
3104
3105         int ret = 0;
3106         ret = vcd_engine_agent_get_foreach_command(vce_command, callback, user_data);
3107         if (0 != ret) {
3108                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreach command : ret(%d)", ret);
3109         }
3110
3111         return ret;
3112 }
3113
3114 int vcd_get_command_count(vce_cmd_h vce_command, int* count)
3115 {
3116         SLOG(LOG_INFO, TAG_VCD, "[Server] Get command count");
3117
3118         int ret = 0;
3119         ret = vcd_engine_agent_get_command_count(vce_command, count);
3120         if (0 != ret) {
3121                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get command count : ret(%d)", ret);
3122         }
3123
3124         return ret;
3125 }
3126
3127 int vcd_get_audio_type(char** audio_type)
3128 {
3129         SLOG(LOG_INFO, TAG_VCD, "[Server] Get audio type");
3130
3131         int ret = 0;
3132         ret = vcd_engine_agent_get_audio_type(audio_type);
3133         if (0 != ret) {
3134                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio type : ret(%d)", ret);
3135         }
3136
3137         return ret;
3138 }
3139
3140 int vcd_set_private_data(const char* key, const char* data)
3141 {
3142         vcd_state_e state = vcd_config_get_service_state();
3143
3144         if (VCD_STATE_READY != state) {
3145                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
3146                 return VCD_ERROR_INVALID_STATE;
3147         }
3148
3149         int ret = vcd_engine_agent_set_private_data(key, data);
3150         if (0 != ret) {
3151                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data to the manager client : ret(%d)", ret);
3152         } else {
3153                 SLOG(LOG_INFO, TAG_VCD, "[Server] Set private data to the manager client, key(%s), data(%s)", key, data);
3154         }
3155
3156         return ret;
3157 }
3158
3159 int vcd_get_private_data(const char* key, char** data)
3160 {
3161         vcd_state_e state = vcd_config_get_service_state();
3162
3163         if (VCD_STATE_READY != state) {
3164                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
3165                 return VCD_ERROR_INVALID_STATE;
3166         }
3167
3168         int ret = vcd_engine_agent_get_private_data(key, data);
3169         if (0 != ret) {
3170                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data from the manager client : ret(%d)", ret);
3171         } else {
3172                 SLOG(LOG_INFO, TAG_VCD, "[Server] Get private data from the manager client, key(%s), data(%s)", key, *data);
3173         }
3174
3175         return ret;
3176 }
3177
3178 int vcd_start_recording()
3179 {
3180         SLOG(LOG_INFO, TAG_VCD, "[Server] Start recording");
3181
3182         int ret = 0;
3183         ret = vcd_engine_agent_start_recording();
3184         if (0 != ret) {
3185                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recording : ret(%d)", ret);
3186         }
3187
3188         return ret;
3189 }
3190
3191 int vcd_stop_recording()
3192 {
3193         SLOG(LOG_INFO, TAG_VCD, "[Server] Stop recording");
3194
3195         int ret = 0;
3196         ret = vcd_engine_agent_stop_recording();
3197         if (0 != ret) {
3198                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recording : ret(%d)", ret);
3199         }
3200
3201         return ret;
3202 }
3203
3204 int vcd_send_update_status(vce_update_event_e update_event, const char* msg)
3205 {
3206         SLOG(LOG_INFO, TAG_VCD, "[Server] update status, update event(%d), msg(%s)", update_event, msg);
3207
3208         int ret = 0;
3209         if (VCE_UPDATE_EVENT_START == update_event) {
3210                 if (VCD_STATE_RECORDING == vcd_config_get_service_state() || VCD_STATE_PROCESSING == vcd_config_get_service_state()) {
3211                         ret = vcd_server_mgr_cancel();
3212                         if (0 != ret) {
3213                                 SLOG(LOG_ERROR, TAG_VCD, "[Server Error] Fail to cancel, ret(%d)", ret);
3214                                 return ret;
3215                         }
3216                 }
3217                 vcd_config_set_service_state(VCD_STATE_UPDATING);
3218                 vcdc_send_service_state(VCD_STATE_UPDATING);
3219
3220         } else if (VCE_UPDATE_EVENT_FINISH == update_event) {
3221                 vcd_config_set_service_state(VCD_STATE_READY);
3222                 vcdc_send_service_state(VCD_STATE_READY);
3223
3224         } else if (VCE_UPDATE_EVENT_FAIL == update_event) {
3225                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Update event : Fail - msg(%s)", msg);
3226         }
3227
3228         return 0;
3229 }
3230
3231 int vcd_set_private_data_set_cb(vce_private_data_set_cb callback_func)
3232 {
3233         SLOG(LOG_INFO, TAG_VCD, "[Server] Set private data set cb");
3234
3235         int ret = 0;
3236         ret = vcd_engine_agent_set_private_data_set_cb(callback_func);
3237         if (0 != ret) {
3238                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
3239         }
3240
3241         return ret;
3242 }
3243
3244 int vcd_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
3245 {
3246         SLOG(LOG_INFO, TAG_VCD, "[Server] Set private data requested cb");
3247
3248         int ret = 0;
3249         ret = vcd_engine_agent_set_private_data_requested_cb(callback_func);
3250         if (0 != ret) {
3251                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
3252         }
3253
3254         return ret;
3255 }
3256
3257 int vcd_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
3258 {
3259         SLOG(LOG_INFO, TAG_VCD, "[Server] Set nlu base info requested cb");
3260
3261         int ret = 0;
3262         ret = vcd_engine_agent_set_nlu_base_info_requested_cb(callback_func);
3263         if (0 != ret) {
3264                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu base info requested cb : ret(%d)", ret);
3265         }
3266
3267         return ret;
3268 }
3269
3270 int vcd_set_specific_engine_request_cb(vce_specific_engine_request_cb callback_func)
3271 {
3272         SLOG(LOG_INFO, TAG_VCD, "[Server] Set specific engine request cb");
3273         int ret = 0;
3274         ret = vcd_engine_agent_set_specific_engine_request_cb(callback_func);
3275         if (0 != ret) {
3276                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set specific engine request cb : ret(%d)", ret);
3277         }
3278
3279         return ret;
3280 }
3281
3282 int vcd_set_request_tts_cb(vce_request_tts_cb callback_func, void* user_data)
3283 {
3284         SLOG(LOG_INFO, TAG_VCD, "[Server] Set request tts cb");
3285         int ret = 0;
3286         ret = vcd_engine_agent_set_request_tts_cb(callback_func, user_data);
3287         if (0 != ret) {
3288                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set request tts cb : ret(%d)", ret);
3289         }
3290
3291         return ret;
3292 }
3293
3294 int vcd_set_cancel_tts_cb(vce_cancel_tts_cb callback_func, void* user_data)
3295 {
3296         SLOG(LOG_INFO, TAG_VCD, "[Server] Set cancel tts cb");
3297         int ret = 0;
3298         ret = vcd_engine_agent_set_cancel_tts_cb(callback_func, user_data);
3299         if (0 != ret) {
3300                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set cancel tts cb : ret(%d)", ret);
3301         }
3302
3303         return ret;
3304 }
3305
3306 int vcd_set_tts_audio_format_request_cb(vce_tts_audio_format_request_cb callback_func, void* user_data)
3307 {
3308         SLOG(LOG_INFO, TAG_VCD, "[Server] Set tts audio format request cb");
3309         int ret = 0;
3310         ret = vcd_engine_agent_set_get_tts_audio_format_cb(callback_func, user_data);
3311         if (0 != ret) {
3312                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set tts audio format request : ret(%d)", ret);
3313         }
3314
3315         return ret;
3316 }