Remove to check if current engine is same as vconf
[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 #if 0
1244 static bool __is_default_engine()
1245 {
1246         char* engine = NULL;
1247         engine = vconf_get_str(VCONFKEY_VC_ENGINE_DEFAULT);
1248         if (NULL == engine) {
1249                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get sting for vc engine");
1250                 return FALSE;
1251         }
1252
1253         char appid[1024] = {'\0', };
1254         if (0 != aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1)) {
1255                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get callee appid by pid");
1256         }
1257
1258         SLOG(LOG_DEBUG, TAG_VCD, "[Server] VC Default Engine(%s), appId(%s)", engine, appid);
1259         if (0 == strncmp(engine, appid, strlen(engine))) {
1260                 free(engine);
1261                 return TRUE;
1262         }
1263         free(engine);
1264         return FALSE;
1265 }
1266 #endif
1267
1268 int vcd_initialize(vce_request_callback_s *callback)
1269 {
1270         int ret = 0;
1271
1272         /* Remove old file */
1273         __vcd_file_clean_up();
1274
1275         /* initialize modules */
1276         ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
1277         if (0 != ret) {
1278                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
1279         }
1280
1281         ret = vc_db_initialize_for_daemon();
1282         if (0 != ret) {
1283                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to initialize DB : %d", ret);
1284                 return ret;
1285         }
1286
1287         /* Remove db data */
1288         ret = __vcd_db_clean_up();
1289         if (0 != ret) {
1290                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
1291         }
1292
1293         vcd_config_set_service_state(VCD_STATE_NONE);
1294
1295         ret = vcd_engine_agent_init();
1296         if (0 != ret) {
1297                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
1298                 return ret;
1299         }
1300
1301         if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
1302                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
1303                 return VCD_ERROR_OPERATION_FAILED;
1304         }
1305
1306         /* Load engine */
1307         if (0 != vcd_engine_agent_load_current_engine(callback)) {
1308                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
1309                 return VCD_ERROR_OPERATION_FAILED;
1310         }
1311
1312         /* Initialize manager info */
1313         vcd_client_manager_unset();
1314
1315 //      if (TRUE == __is_default_engine()) {
1316                 /* Open dbus connection */
1317                 if (0 != vcd_dbus_open_connection()) {
1318                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open connection");
1319                         return VCD_ERROR_OPERATION_FAILED;
1320                 }
1321 //      }
1322
1323         vcd_config_set_service_state(VCD_STATE_READY);
1324 //      vcdc_send_service_state(VCD_STATE_READY);
1325
1326         /* Set timer cleanup client all */
1327         g_check_client_timer = ecore_timer_add(CLIENT_CLEAN_UP_TIME, vcd_cleanup_client_all, NULL);
1328         if (NULL == g_check_client_timer) {
1329                 SLOG(LOG_WARN, TAG_VCD, "[Server Warning] Fail to create timer of client check");
1330         }
1331
1332         g_current_uid = -1;
1333         g_current_utt_id = -1;
1334
1335         SLOG(LOG_ERROR, TAG_VCD, "[Server SUCCESS] initialize");
1336
1337         return 0;
1338 }
1339
1340 bool vcd_finalize()
1341 {
1342         GList *iter = NULL;
1343         if (0 < g_list_length(g_proc_list)) {
1344                 iter = g_list_first(g_proc_list);
1345                 while (NULL != iter) {
1346                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1347                         iter = g_list_first(g_proc_list);
1348                 }
1349         }
1350
1351         if (NULL != g_restart_timer) {
1352                 ecore_timer_del(g_restart_timer);
1353                 g_restart_timer = NULL;
1354         }
1355
1356         if (NULL != g_check_client_timer) {
1357                 ecore_timer_del(g_check_client_timer);
1358                 g_check_client_timer = NULL;
1359         }
1360
1361         vcd_state_e state = vcd_config_get_service_state();
1362         if (VCD_STATE_READY != state) {
1363                 if (VCD_STATE_RECORDING == state) {
1364                         vcd_recorder_stop();
1365                 }
1366                 vcd_engine_recognize_cancel();
1367         }
1368
1369         if (0 != vcd_recorder_destroy()) {
1370                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1371                 return false;
1372         } else {
1373                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1374         }
1375
1376         if (0 != vcd_engine_agent_release()) {
1377                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1378                 return false;
1379         } else {
1380                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1381         }
1382
1383         vcd_client_manager_unset_appid();
1384
1385         int ret = vcd_config_finalize();
1386         if (0 != ret) {
1387                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to finalize config.");
1388         }
1389
1390         ret = vc_db_finalize();
1391         if (0 != ret) {
1392                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to finalize DB : %d", ret);
1393         }
1394
1395         vcd_config_set_service_state(VCD_STATE_NONE);
1396         vcdc_send_service_state(VCD_STATE_NONE);
1397
1398         /* Open dbus connection */
1399         if (0 != vcd_dbus_close_connection()) {
1400                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to close connection");
1401         }
1402
1403         SLOG(LOG_ERROR, TAG_VCD, "[Server] mode finalize");
1404
1405         return true;
1406 }
1407
1408 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1409 {
1410         bool ret = vcd_finalize();
1411         if (false == ret) {
1412                 return EINA_TRUE;
1413         } else {
1414                 ecore_main_loop_quit();
1415                 return EINA_FALSE;
1416         }
1417 }
1418
1419 static void __read_proc()
1420 {
1421         DIR *dp = NULL;
1422         struct dirent *dirp = NULL;
1423         int tmp;
1424
1425         GList *iter = NULL;
1426         if (0 < g_list_length(g_proc_list)) {
1427                 iter = g_list_first(g_proc_list);
1428                 while (NULL != iter) {
1429                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1430                         iter = g_list_first(g_proc_list);
1431                 }
1432         }
1433
1434         dp = opendir("/proc");
1435         if (NULL == dp) {
1436                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1437         } else {
1438                 do {
1439                         dirp = readdir(dp);
1440
1441                         if (NULL != dirp) {
1442                                 tmp = atoi(dirp->d_name);
1443                                 if (0 >= tmp)   continue;
1444                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1445                         }
1446                 } while (NULL != dirp);
1447                 closedir(dp);
1448         }
1449         return;
1450 }
1451
1452 static void __vcd_cleanup_client(vcd_client_type_e type)
1453 {
1454         int* client_list = NULL;
1455         int client_count = 0;
1456         int i = 0;
1457         int j = 0;
1458         bool exist = false;
1459         int mgr_pid = -1;
1460         int ret = -1;
1461
1462         if (VCD_CLIENT_TYPE_NORMAL == type) {
1463                 ret = vcd_client_get_list(&client_list, &client_count);
1464         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1465                 ret = vcd_client_widget_get_list(&client_list, &client_count);
1466         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1467                 mgr_pid = vcd_client_manager_get_pid();
1468                 client_list = &mgr_pid;
1469                 client_count = 1;
1470                 if (-1 == mgr_pid) {
1471                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1472                         return;
1473                 }
1474         }
1475
1476         if (0 == ret || mgr_pid > 0) {
1477                 SLOG(LOG_INFO, TAG_VCD, "@@@ Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1478                 if (NULL != client_list && client_count > 0) {
1479                         for (i = 0; i < client_count; i++) {
1480                                 exist = false;
1481                                 GList *iter = NULL;
1482                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1483                                         iter = g_list_nth(g_proc_list, j);
1484                                         if (NULL != iter) {
1485                                                 if (*(client_list + i) == GPOINTER_TO_INT(iter->data)) {
1486                                                         SLOG(LOG_INFO, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1487                                                         exist = true;
1488                                                         break;
1489                                                 }
1490                                         }
1491                                 }
1492
1493                                 if (false == exist) {
1494                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", *(client_list + i));
1495                                         if (VCD_CLIENT_TYPE_NORMAL == type)
1496                                                 vcd_server_finalize(*(client_list + i));
1497                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
1498                                                 vcd_server_widget_finalize(*(client_list + i));
1499                                         else
1500                                                 vcd_server_mgr_finalize(mgr_pid);
1501                                 }
1502                         }
1503                 }
1504                 SLOG(LOG_INFO, TAG_VCD, "@@@");
1505         }
1506         if (NULL != client_list && -1 == mgr_pid) {
1507                 free(client_list);
1508                 client_list = NULL;
1509         }
1510         return;
1511 }
1512
1513 Eina_Bool vcd_cleanup_client_all(void *data)
1514 {
1515         __read_proc();
1516
1517         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1518         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1519         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1520
1521         if (0 == vcd_client_get_ref_count()) {
1522                 SLOG(LOG_INFO, TAG_VCD, "[Server] Connected client list is empty");
1523                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1524         }
1525 #if 0
1526         if (0 == vcd_client_get_list(&client_list, &client_count)) {
1527                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up client ");
1528                 if (NULL != client_list && client_count > 0) {
1529                         for (i = 0; i < client_count; i++) {
1530                                 exist = false;
1531                                 GList *iter = NULL;
1532                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1533                                         iter = g_list_nth(g_proc_list, j);
1534                                         if (NULL != iter) {
1535                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1536                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1537                                                         exist = true;
1538                                                         break;
1539                                                 }
1540                                         }
1541                                 }
1542
1543                                 if (false == exist) {
1544                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1545                                         vcd_server_finalize(client_list[i]);
1546                                 }
1547 #if 0
1548                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1549
1550                                 if (0 == result) {
1551                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1552                                         vcd_server_finalize(client_list[i]);
1553                                 } else if (-1 == result) {
1554                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1555                                 }
1556 #endif
1557                         }
1558                 }
1559                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1560         }
1561         if (NULL != client_list) {
1562                 free(client_list);
1563                 client_list = NULL;
1564         }
1565
1566         /* If app is in background state, app cannot response message. */
1567         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1568                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ Clean up widget");
1569                 if (NULL != client_list && client_count > 0) {
1570                         for (i = 0; i < client_count; i++) {
1571                                 exist = false;
1572                                 GList *iter = NULL;
1573                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1574                                         iter = g_list_nth(g_proc_list, j);
1575                                         if (NULL != iter) {
1576                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1577                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1578                                                         exist = true;
1579                                                         break;
1580                                                 }
1581                                         }
1582                                 }
1583
1584                                 if (false == exist) {
1585                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1586                                         vcd_server_widget_finalize(client_list[i]);
1587                                 }
1588 #if 0
1589                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1590
1591                                 if (0 == result) {
1592                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1593                                         vcd_server_widget_finalize(client_list[i]);
1594                                 } else if (-1 == result) {
1595                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1596                                 }
1597 #endif
1598                         }
1599                 }
1600                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1601         }
1602
1603         if (NULL != client_list) {
1604                 free(client_list);
1605                 client_list = NULL;
1606         }
1607
1608         /* manager */
1609         exist = false;
1610         GList *iter = NULL;
1611         int mgr_pid = vcd_client_manager_get_pid();
1612         if (0 < mgr_pid) {
1613                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1614                         iter = g_list_nth(g_proc_list, j);
1615                         if (NULL != iter) {
1616                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1617                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1618                                         exist = true;
1619                                         break;
1620                                 }
1621                         }
1622                 }
1623
1624                 if (false == exist) {
1625                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1626                         vcd_server_mgr_finalize(mgr_pid);
1627                 }
1628         }
1629 #endif
1630         return EINA_TRUE;
1631 }
1632
1633 int vcd_server_get_service_state()
1634 {
1635         return vcd_config_get_service_state();
1636 }
1637
1638 int vcd_server_get_foreground()
1639 {
1640         int pid;
1641         vcd_config_get_foreground(&pid);
1642         return pid;
1643 }
1644
1645 static Eina_Bool __vcd_send_service_state(void *data)
1646 {
1647         vcd_config_set_service_state(VCD_STATE_READY);
1648         vcdc_send_service_state(VCD_STATE_READY);
1649
1650         SLOG(LOG_INFO, TAG_VCD, "[Server Success] success to send service status for READY");
1651
1652         return EINA_FALSE;
1653 }
1654
1655 /*
1656 * API for manager
1657 */
1658 int vcd_server_mgr_initialize(int pid)
1659 {
1660         /* check if pid is valid */
1661         if (false == vcd_client_manager_is_valid(pid)) {
1662                 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1663                 vcd_server_mgr_cancel();
1664                 vcd_client_manager_unset();
1665         }
1666
1667         /* Add client information to client manager */
1668         int ret = vcd_client_manager_set(pid);
1669         if (0 != ret) {
1670                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1671                 return VCD_ERROR_OPERATION_FAILED;
1672         }
1673
1674         if (0 != vcdc_send_manager_pid(pid))
1675                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1676
1677         ecore_timer_add(0.05, __vcd_send_service_state, NULL);
1678
1679         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1680
1681         return VCD_ERROR_NONE;
1682 }
1683
1684 int vcd_server_mgr_finalize(int pid)
1685 {
1686         /* check if pid is valid */
1687         if (false == vcd_client_manager_is_valid(pid)) {
1688                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1689                 return VCD_ERROR_INVALID_PARAMETER;
1690         }
1691
1692         /* Cancel recognition */
1693         vcd_server_mgr_cancel();
1694
1695         if (0 != vcdc_send_manager_pid(-1))
1696                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1697
1698         /* Remove manager information */
1699         if (0 != vcd_client_manager_unset()) {
1700                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1701         }
1702
1703         if (0 == vcd_client_get_ref_count()) {
1704                 SLOG(LOG_INFO, TAG_VCD, "[Server] Connected client list is empty");
1705                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1706         }
1707
1708         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1709
1710         return VCD_ERROR_NONE;
1711 }
1712
1713 int vcd_server_mgr_set_command(int pid)
1714 {
1715         if (0 != vcd_client_manager_set_command(pid)) {
1716                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1717                 return VCD_ERROR_INVALID_PARAMETER;
1718         }
1719         return VCD_ERROR_NONE;
1720 }
1721
1722 int vcd_server_mgr_unset_command(int pid)
1723 {
1724         if (0 != vcd_client_manager_unset_command(pid)) {
1725                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1726                 return VCD_ERROR_INVALID_PARAMETER;
1727         }
1728         return VCD_ERROR_NONE;
1729 }
1730
1731 int vcd_server_mgr_set_demandable_client(int pid)
1732 {
1733         /* check if pid is valid */
1734         if (false == vcd_client_manager_is_valid(pid)) {
1735                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1736                 return VCD_ERROR_INVALID_PARAMETER;
1737         }
1738
1739         GSList* client_list = NULL;
1740         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1741                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1742                 return VCD_ERROR_OPERATION_FAILED;
1743         }
1744
1745         /* Save client list */
1746         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1747                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1748                 return VCD_ERROR_OPERATION_FAILED;
1749         }
1750
1751         return VCD_ERROR_NONE;
1752 }
1753
1754 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1755 {
1756         /* check if pid is valid */
1757         if (false == vcd_client_manager_is_valid(pid)) {
1758                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1759                 return VCD_ERROR_INVALID_PARAMETER;
1760         }
1761
1762         int ret = 0;
1763         vce_audio_type_e type = VCE_AUDIO_TYPE_PCM_S16_LE;
1764         int rate = 16000;
1765         int channel = 1;
1766
1767         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1768         if (0 != ret) {
1769                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1770                 return VCD_ERROR_OPERATION_FAILED;
1771         }
1772
1773         ret = vcd_recorder_set(audio_type, type, rate, channel);
1774         if (0 != ret) {
1775                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1776                 return VCD_ERROR_OPERATION_FAILED;
1777         }
1778
1779         return VCD_ERROR_NONE;
1780 }
1781
1782 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1783 {
1784         /* check if pid is valid */
1785         if (false == vcd_client_manager_is_valid(pid)) {
1786                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1787                 return VCD_ERROR_INVALID_PARAMETER;
1788         }
1789
1790         int ret = vcd_recorder_get(audio_type);
1791         if (0 != ret) {
1792                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1793                 return VCD_ERROR_OPERATION_FAILED;
1794         }
1795
1796         return VCD_ERROR_NONE;
1797 }
1798
1799 int vcd_server_mgr_set_client_info(int pid)
1800 {
1801         /* check if pid is valid */
1802         if (false == vcd_client_manager_is_valid(pid)) {
1803                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1804                 return VCD_ERROR_INVALID_PARAMETER;
1805         }
1806
1807         int ret = vcd_client_save_client_info();
1808         if (0 != ret) {
1809                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1810                 return VCD_ERROR_OPERATION_FAILED;
1811         }
1812
1813         return VCD_ERROR_NONE;
1814 }
1815
1816 static int __reset_waiting_for_widget_recording(void)
1817 {
1818         SLOG(LOG_ERROR, TAG_VCD, "[Server] Reset waiting for widget recording");
1819         // Delete timer to check that widget client is terminated
1820         if (g_check_widget_client_timer) {
1821                 ecore_timer_del(g_check_widget_client_timer);
1822                 g_check_widget_client_timer = NULL;
1823         }
1824         // Reset flag to wait for recording from widget client
1825         vcd_client_widget_set_waiting_for_recording(-1, false);
1826         return 0;
1827 }
1828
1829 #if 0
1830 static Eina_Bool __send_waiting_timeout_error_to_manager(void* data)
1831 {
1832         intptr_t ppid = (intptr_t)data;
1833         int pid = (int)ppid;
1834         SLOG(LOG_ERROR, TAG_VCD, "Widget client didn't send to start recording, pid(%d)", pid);
1835         __reset_waiting_for_widget_recording();
1836
1837         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
1838         return EINA_FALSE;
1839 }
1840 #else
1841 static Eina_Bool __send_waiting_timeout_start_recording(void* data)
1842 {
1843         intptr_t ppid = (intptr_t)data;
1844         int pid = (int)ppid;
1845         SLOG(LOG_ERROR, TAG_VCD, "Widget client didn't send to start recording, pid(%d)", pid);
1846
1847         int ret = __start_internal_recognition();
1848         if (0 != ret) {
1849                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : %d", ret);
1850                 return ret;
1851         }
1852         return EINA_FALSE;
1853 }
1854 #endif
1855
1856 static int __set_waiting_for_widget_recording(int pid)
1857 {
1858         SLOG(LOG_ERROR, TAG_VCD, "[Server] Set waiting for widget recording, pid(%d)", pid);
1859
1860         // Check if the app included widget client is terminated or not. If it is terminated, vcd_server_widget_finalize() function will be called
1861         // In that function, it will start recording
1862         intptr_t ppid = (intptr_t)pid;
1863         g_check_widget_client_timer = ecore_timer_add(2.0, __send_waiting_timeout_start_recording, (void*)ppid);
1864
1865         // Set flag to wait for recording from widget client
1866         vcd_client_widget_set_waiting_for_recording(pid, true);
1867         return 0;
1868 }
1869
1870 static int __start_internal_recognition()
1871 {
1872         int ret;
1873         __reset_waiting_for_widget_recording();
1874
1875         if (0 != vcd_client_command_collect_command()) {
1876                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1877                 /* Send error cb to manager */
1878                 int pid = vcd_client_widget_get_foreground_pid();
1879                 if (-1 != pid)
1880                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_framework.error.vcfw.collect_command_fail");
1881                 return VCD_ERROR_OPERATION_FAILED;
1882         }
1883
1884         /* 3. Set command to engine */
1885         ret = vcd_engine_set_commands();
1886         if (0 != ret) {
1887                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set commands : %d", ret);
1888                 /* Send error cb to manager */
1889                 int pid = vcd_client_widget_get_foreground_pid();
1890                 if (-1 != pid)
1891                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_framework.error.engine.set_commands_fail");
1892                 return VCD_ERROR_OPERATION_FAILED;
1893         }
1894
1895         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Set command");
1896
1897         bool stop_by_silence = true;
1898         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1899                 stop_by_silence = false;
1900         }
1901
1902         vcd_client_manager_set_result_text(NULL);
1903
1904         /* 4. start recognition */
1905         ret = vcd_engine_recognize_start(stop_by_silence);
1906         if (0 != ret) {
1907                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1908                 /* Send error cb to manager */
1909                 int pid = vcd_client_widget_get_foreground_pid();
1910                 if (-1 != pid)
1911                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_framework.error.engine.start_fail");
1912                 return VCD_ERROR_OPERATION_FAILED;
1913         }
1914
1915         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start engine");
1916
1917 #if 1
1918         /* 5. recorder start */
1919         ret = vcd_recorder_start();
1920         if (0 != ret) {
1921                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1922                 vcd_engine_recognize_cancel();
1923                 /* Send error cb to manager */
1924                 int pid = vcd_client_widget_get_foreground_pid();
1925                 if (-1 != pid)
1926                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_framework.error.vcfw.send_rc_fail");
1927                 return ret;
1928         }
1929 #endif
1930
1931         vcd_config_set_service_state(VCD_STATE_RECORDING);
1932         vcdc_send_service_state(VCD_STATE_RECORDING);
1933
1934         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1935
1936         return 0;
1937 }
1938
1939 static Eina_Bool __vcd_request_show_tooltip(void *data)
1940 {
1941         int pid = vcd_client_widget_get_foreground_pid();
1942         if (-1 != pid) {
1943                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command, show(%d)", (bool)data);
1944                 vcdc_send_show_tooltip(pid, (bool)data);
1945         }
1946
1947         return EINA_FALSE;
1948 }
1949
1950 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1951 {
1952         /* 1. check current state */
1953         vcd_state_e state = vcd_config_get_service_state();
1954
1955         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
1956                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready and not synthesizine, state(%d)", state);
1957                 return VCD_ERROR_INVALID_STATE;
1958         }
1959         if (-1 == vcd_client_manager_get_pid()) {
1960                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1961                 return VCD_ERROR_OPERATION_FAILED;
1962         }
1963         __reset_waiting_for_widget_recording();
1964
1965         SLOG(LOG_ERROR, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1966         vcd_client_set_recognition_mode(recognition_mode);
1967
1968         if (false == exclusive_cmd) {
1969                 vcd_client_update_foreground_pid();
1970                 /* Notify show tooltip */
1971                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1972                         int pid = vcd_client_widget_get_foreground_pid();
1973                         if (-1 != pid) {
1974                                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Request tooltip show and widget command");
1975                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1976
1977                                 __set_waiting_for_widget_recording(pid);
1978                                 return 0;
1979                         }
1980                 }
1981         } else {
1982                 vcd_client_manager_set_exclusive(exclusive_cmd);
1983         }
1984
1985         int fg_pid = -1;
1986         if (true == start_by_client) {
1987                 /* Get foreground pid */
1988                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1989                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1990                 }
1991
1992                 /* Set client exclusive option */
1993                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1994                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1995                 }
1996         }
1997
1998         SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recognition");
1999
2000         int ret = __start_internal_recognition();
2001         if (0 != ret) {
2002                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : %d", ret);
2003                 return ret;
2004         }
2005
2006         if (true == start_by_client) {
2007                 vcd_client_unset_exclusive_command(fg_pid);
2008         }
2009
2010         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] start internal recognition");
2011         return VCD_ERROR_NONE;
2012 }
2013
2014 int vcd_server_mgr_stop()
2015 {
2016         /* 1. Check current state is recording */
2017         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
2018                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
2019                 return VCD_ERROR_INVALID_STATE;
2020         }
2021         if (-1 == vcd_client_manager_get_pid()) {
2022                 SLOG(LOG_INFO, TAG_VCD, "[Server] Manager is NOT available.");
2023                 return VCD_ERROR_OPERATION_FAILED;
2024         }
2025         SLOG(LOG_ERROR, TAG_VCD, "[Server] stop internal recognition");
2026
2027 #if 1
2028         /* 2. Stop recorder */
2029         vcd_recorder_stop();
2030 #endif
2031
2032         /* 3. Stop engine recognition */
2033         int ret = vcd_engine_recognize_stop();
2034         if (0 != ret) {
2035                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
2036         }
2037
2038         /* 4. Set original mode */
2039         vcd_config_set_service_state(VCD_STATE_PROCESSING);
2040         vcdc_send_service_state(VCD_STATE_PROCESSING);
2041
2042         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] stop internal recognition");
2043         return VCD_ERROR_NONE;
2044 }
2045
2046 int vcd_server_mgr_cancel()
2047 {
2048         if (-1 == vcd_client_manager_get_pid()) {
2049                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Manager is NOT available.");
2050                 return VCD_ERROR_OPERATION_FAILED;
2051         }
2052
2053         if (g_restart_timer != NULL) {
2054                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
2055                 ecore_timer_del(g_restart_timer);
2056                 g_restart_timer = NULL;
2057         }
2058
2059         /* 1. Check current state */
2060         vcd_state_e state = vcd_config_get_service_state();
2061         if (VCD_STATE_READY == state) {
2062                 SLOG(LOG_INFO, TAG_VCD, "[Server] Current state is READY");
2063                 vcd_recorder_stop();
2064
2065                 if (false == vcd_client_manager_get_exclusive()) {
2066                         if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
2067                                 int pid = vcd_client_widget_get_foreground_pid();
2068                                 if (-1 != pid) {
2069                                         SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip hide");
2070                                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2071                                 }
2072                         }
2073                 }
2074
2075                 vcdc_send_service_state(VCD_STATE_READY);
2076                 return VCD_ERROR_NONE;
2077         }
2078         SLOG(LOG_ERROR, TAG_VCD, "[Server] cancel internal recognition");
2079
2080 #if 1
2081         /* 2. Stop recorder */
2082         vcd_recorder_stop();
2083 #endif
2084
2085         /* 3. Cancel engine */
2086         int ret = vcd_engine_recognize_cancel();
2087         if (0 != ret) {
2088                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
2089         }
2090
2091         if (false == vcd_client_manager_get_exclusive()) {
2092                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
2093                         int pid = vcd_client_widget_get_foreground_pid();
2094                         if (-1 != pid) {
2095                                 SLOG(LOG_INFO, TAG_VCD, "[Server] Request tooltip hide");
2096                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2097                         }
2098                 }
2099         } else {
2100                 vcd_client_manager_set_exclusive(false);
2101         }
2102
2103         /* 4. Set state */
2104         vcd_config_set_service_state(VCD_STATE_READY);
2105         vcdc_send_service_state(VCD_STATE_READY);
2106
2107         SLOG(LOG_ERROR, TAG_VCD, "[Server Success] cancel internal recognition");
2108         return VCD_ERROR_NONE;
2109 }
2110
2111
2112 int vcd_server_mgr_result_select()
2113 {
2114         __vcd_send_selected_result(NULL);
2115
2116         return VCD_ERROR_NONE;
2117 }
2118
2119 int vcd_server_mgr_set_domain(int pid, const char* domain)
2120 {
2121         /* check if pid is valid */
2122         if (false == vcd_client_manager_is_valid(pid)) {
2123                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2124                 return VCD_ERROR_INVALID_PARAMETER;
2125         }
2126
2127         vcd_state_e state = vcd_config_get_service_state();
2128         if (VCD_STATE_READY != state) {
2129                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
2130                 return VCD_ERROR_INVALID_STATE;
2131         }
2132
2133         /* Set domain to engine */
2134         int ret = vcd_engine_set_domain(pid, domain);
2135         if (0 != ret) {
2136                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
2137         } else {
2138                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Set domain");
2139         }
2140
2141         return ret;
2142 }
2143
2144 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
2145 {
2146         /* check if pid is valid */
2147         if (false == vcd_client_manager_is_valid(pid)) {
2148                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2149                 return VCD_ERROR_INVALID_PARAMETER;
2150         }
2151
2152         vcd_state_e state = vcd_config_get_service_state();
2153         if (VCD_STATE_READY != state) {
2154                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
2155                 return VCD_ERROR_INVALID_STATE;
2156         }
2157
2158         /* Set private data to engine */
2159         int ret = vcd_engine_set_private_data(pid, key, data);
2160         if (0 != ret) {
2161                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
2162         } else {
2163                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Set private data");
2164         }
2165
2166         return ret;
2167 }
2168
2169 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
2170 {
2171         /* check if pid is valid */
2172         if (false == vcd_client_manager_is_valid(pid)) {
2173                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2174                 return VCD_ERROR_INVALID_PARAMETER;
2175         }
2176         vcd_state_e state = vcd_config_get_service_state();
2177         if (VCD_STATE_READY != state) {
2178                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
2179                 return VCD_ERROR_INVALID_STATE;
2180         }
2181
2182         /* Get private data to engine */
2183         int ret = vcd_engine_get_private_data(pid, key, data);
2184         if (0 != ret) {
2185                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
2186         } else {
2187                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Set private data");
2188         }
2189
2190         return ret;
2191 }
2192
2193 int vcd_server_mgr_send_specific_engine_request(int pid, const char* engine_app_id, const char* event, const char* request)
2194 {
2195         /* check if pid is valid */
2196         if (false == vcd_client_manager_is_valid(pid)) {
2197                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2198                 return VCD_ERROR_INVALID_PARAMETER;
2199         }
2200         vcd_state_e state = vcd_config_get_service_state();
2201         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2202                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready and not synthesizing, state(%d)", state);
2203                 return VCD_ERROR_INVALID_STATE;
2204         }
2205
2206         /* Get private data to engine */
2207         int ret = vcd_engine_send_specific_engine_request(engine_app_id, event, request);
2208         if (0 != ret) {
2209                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set specific engine request : %d", ret);
2210         } else {
2211                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Set specific engine request ");
2212         }
2213
2214         return ret;
2215 }
2216
2217 int vcd_server_mgr_do_action(int pid, int type, const char* action)
2218 {
2219         int ret = -1;
2220
2221         /* check if pid is valid */
2222         if (false == vcd_client_manager_is_valid(pid)) {
2223                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2224                 return VCD_ERROR_INVALID_PARAMETER;
2225         }
2226
2227         vcd_state_e state = vcd_config_get_service_state();
2228         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2229                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready and not synthesizing, state(%d)", state);
2230                 return VCD_ERROR_INVALID_STATE;
2231         }
2232
2233         /* Request do action to engine */
2234         if (VCD_SEND_EVENT_TYPE_TEXT == type)
2235                 ret = vcd_engine_process_text(pid, action);
2236         else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
2237                 ret = vcd_engine_process_list_event(pid, action);
2238         else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
2239                 ret = vcd_engine_process_haptic_event(pid, action);
2240
2241         if (0 != ret) {
2242                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
2243         } else {
2244                 vcd_config_set_service_state(VCD_STATE_PROCESSING);
2245                 vcdc_send_service_state(VCD_STATE_PROCESSING);
2246                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] Process do action");
2247         }
2248
2249         return ret;
2250 }
2251
2252 int vcd_server_mgr_set_disabled_command_type(int pid, int disabled_cmd_type)
2253 {
2254         int ret = -1;
2255
2256         /* check if pid is valid */
2257         if (false == vcd_client_manager_is_valid(pid)) {
2258                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
2259                 return VCD_ERROR_INVALID_PARAMETER;
2260         }
2261
2262         vcd_state_e state = vcd_config_get_service_state();
2263         if (VCD_STATE_READY != state) {
2264                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
2265                 return VCD_ERROR_INVALID_STATE;
2266         }
2267
2268         ret = vcd_config_set_disabled_command_type(disabled_cmd_type);
2269         if (0 != ret) {
2270                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type");
2271         } else {
2272                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Set command type(%d)", disabled_cmd_type);
2273         }
2274
2275         return ret;
2276 }
2277
2278
2279 /* for TTS feedback */
2280 int vcd_server_mgr_start_feedback(void)
2281 {
2282         /* check current state */
2283         /* not Recording??? */
2284
2285         if (-1 == vcd_client_manager_get_pid()) {
2286                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Manager is NOT available");
2287                 return VCD_ERROR_OPERATION_FAILED;
2288         }
2289
2290         SLOG(LOG_INFO, TAG_VCD, "[Server] start TTS feedback");
2291
2292         /* check there is TTS buffer to be spoken */
2293
2294         return VCD_ERROR_NONE;
2295 }
2296
2297 int vcd_server_mgr_stop_feedback(void)
2298 {
2299         /* check current state */
2300         /* not Recording??? */
2301
2302         if (-1 == vcd_client_manager_get_pid()) {
2303                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Manager is NOT available");
2304                 return VCD_ERROR_OPERATION_FAILED;
2305         }
2306
2307         SLOG(LOG_INFO, TAG_VCD, "[Server] stop TTS feedback");
2308
2309         return VCD_ERROR_NONE;
2310 }
2311
2312 int vcd_server_mgr_send_audio_streaming(int pid, int event, unsigned char* buffer, unsigned int len)
2313 {
2314         SLOG(LOG_INFO, TAG_VCD, "[DEBUG] Send Audio Streaming from Multi-assistant. event(%d), buffer(%p), len(%d)", event, &buffer, len);
2315
2316         int ret = 0;
2317         if (VCD_AUDIO_STREAMING_EVENT_START == event) {
2318                 ret = vcd_recorder_start_streaming();
2319                 if (0 != ret) {
2320                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to start streaming, ret(%d)", ret);
2321                         return ret;
2322                 }
2323         }
2324
2325         ret = vcd_recorder_send_streaming((const void*)buffer, (const unsigned int)len);
2326         if (0 != ret) {
2327                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to start streaming, ret(%d)", ret);
2328                 return ret;
2329         }
2330
2331         if (VCD_AUDIO_STREAMING_EVENT_FINISH == event) {
2332                 ret = vcd_recorder_stop_streaming();
2333                 if (0 != ret) {
2334                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to stop streaming, ret(%d)", ret);
2335                         return ret;
2336                 }
2337         }
2338
2339         return VCD_ERROR_NONE;
2340 }
2341
2342 int vcd_server_mgr_change_system_volume(int pid, vcd_system_volume_event_e system_volume_event)
2343 {
2344         SLOG(LOG_INFO, TAG_VCD, "[DEBUG] change system volume, system volume event(%d)", system_volume_event);
2345
2346         int ret = 0;
2347         if (VCD_SYSTEM_VOLUME_EVENT_CHANGE == system_volume_event) {
2348                 ret = vcd_recorder_change_system_volume();
2349                 if (0 != ret) {
2350                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to change system volume, ret(%d)", ret);
2351                         return ret;
2352                 }
2353         } else if (VCD_SYSTEM_VOLUME_EVENT_RECOVER == system_volume_event) {
2354                 ret = vcd_recorder_recover_system_volume();
2355                 if (0 != ret) {
2356                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to recover system volume, ret(%d)", ret);
2357                         return ret;
2358                 }
2359         }
2360
2361         return ret;
2362 }
2363
2364
2365 /*
2366 * VC Server Functions for Client
2367 */
2368 int vcd_server_initialize(int pid)
2369 {
2370         if (false == vcd_engine_is_available_engine()) {
2371                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2372                 return VCD_ERROR_ENGINE_NOT_FOUND;
2373         }
2374
2375         /* check if pid is valid */
2376         if (true == vcd_client_is_available(pid)) {
2377                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2378                 return VCD_ERROR_NONE;
2379         }
2380
2381         /* Add client information to client manager */
2382         if (0 != vcd_client_add(pid)) {
2383                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2384                 return VCD_ERROR_OPERATION_FAILED;
2385         }
2386
2387         SLOG(LOG_INFO, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
2388
2389         return VCD_ERROR_NONE;
2390 }
2391
2392 int vcd_server_finalize(int pid)
2393 {
2394         /* check if pid is valid */
2395         if (false == vcd_client_is_available(pid)) {
2396                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2397                 return VCD_ERROR_INVALID_PARAMETER;
2398         }
2399
2400         /* Remove client information */
2401         if (0 != vcd_client_delete(pid)) {
2402                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2403         }
2404
2405         if (0 == vcd_client_get_ref_count()) {
2406                 SLOG(LOG_INFO, TAG_VCD, "[Server] Connected client list is empty");
2407                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2408         }
2409
2410         SLOG(LOG_INFO, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
2411
2412         return VCD_ERROR_NONE;
2413 }
2414
2415 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
2416 {
2417         /* check if pid is valid */
2418         if (false == vcd_client_is_available(pid)) {
2419                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2420                 return VCD_ERROR_INVALID_PARAMETER;
2421         }
2422
2423         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
2424                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
2425                 return VCD_ERROR_OPERATION_FAILED;
2426         }
2427
2428         return 0;
2429 }
2430
2431 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
2432 {
2433         /* check if pid is valid */
2434         if (false == vcd_client_is_available(pid)) {
2435                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2436                 return VCD_ERROR_INVALID_PARAMETER;
2437         }
2438
2439         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
2440                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
2441                 return VCD_ERROR_OPERATION_FAILED;
2442         }
2443
2444         return 0;
2445 }
2446
2447 int vcd_server_set_foreground(int pid, bool value)
2448 {
2449         /* check if pid is valid */
2450         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2451                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2452                 return VCD_ERROR_INVALID_PARAMETER;
2453         }
2454
2455         if (0 != vcd_config_set_foreground(pid, value)) {
2456                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
2457                 return VCD_ERROR_OPERATION_FAILED;
2458         }
2459
2460         return 0;
2461 }
2462
2463 static int __vcd_server_launch_manager_app()
2464 {
2465         int ret = -1;
2466         bool running = false;
2467         char* appid = NULL;
2468
2469         ret = vcd_client_manager_get_appid(&appid);
2470         if (0 != ret || NULL == appid) {
2471                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
2472                 return VCD_ERROR_OPERATION_FAILED;
2473         }
2474         ret = app_manager_is_running(appid, &running);
2475         if (0 != ret) {
2476                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
2477                 free(appid);
2478                 appid = NULL;
2479                 return VCD_ERROR_OPERATION_FAILED;
2480         }
2481         if (false == running) {
2482                 int tmp_ret = __vcd_is_package_installed(appid);
2483                 if (false == tmp_ret) {
2484                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
2485                 } else {
2486                         ret = __vcd_activate_app_by_appcontrol(appid);
2487                         if (0 != ret) {
2488                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
2489                                 free(appid);
2490                                 appid = NULL;
2491                                 return ret;
2492                         }
2493                         SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
2494                 }
2495         } else {
2496                 ret = __vcd_resume_app(appid);
2497                 if (0 != ret) {
2498                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
2499                         free(appid);
2500                         appid = NULL;
2501                         return ret;
2502                 }
2503                 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
2504         }
2505
2506         free(appid);
2507         appid = NULL;
2508
2509         return VCD_ERROR_NONE;
2510 }
2511
2512 int vcd_server_set_server_dialog(int pid, const char* app_id, const char* credential)
2513 {
2514         /* check if pid is valid */
2515         if (false == vcd_client_is_available(pid)) {
2516                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2517                 return VCD_ERROR_INVALID_PARAMETER;
2518         }
2519
2520         int ret = vcd_engine_set_server_dialog(app_id, credential);
2521         if (0 != ret) {
2522                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set server dialog, pid(%d), app_id(%s), ret(%d)", pid, app_id, ret);
2523                 vcd_client_set_server_dialog(pid, false);
2524                 return ret;
2525         }
2526         SLOG(LOG_ERROR, TAG_VCD, "[Success] Set server dialog, pid(%d), app_id(%s)", pid, app_id);
2527
2528         if (0 != strncmp(credential, "#NULL", strlen(credential))) {
2529                 ret = vcd_client_set_server_dialog(pid, true);
2530                 if (0 != ret)
2531                         SLOG(LOG_INFO, TAG_VCD, "[Server] Set to true for server dialog, app_id(%s)", app_id);
2532         } else {
2533                 ret = vcd_client_set_server_dialog(pid, false);
2534                 if (0 != ret)
2535                         SLOG(LOG_INFO, TAG_VCD, "[Server] Set to false for server dialog, app_id(%s)", app_id);
2536         }
2537
2538         return 0;
2539 }
2540
2541 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
2542 {
2543         /* check if pid is valid */
2544         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2545                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2546                 return VCD_ERROR_INVALID_PARAMETER;
2547         }
2548
2549         bool is_server_dialog = false;
2550         int ret = vcd_client_get_server_dialog(pid, &is_server_dialog);
2551         if (0 != ret) {
2552                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get server dialog, pid(%d), ret(%d)", pid, ret);
2553         }
2554
2555         if (true == is_server_dialog) {
2556                 /* ++ Request tts event to engine */
2557
2558                 /* -- Request tts event to engine */
2559         } else {
2560                 ret = __vcd_server_launch_manager_app();
2561                 if (0 != ret) {
2562                         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);
2563                         return ret;
2564                 }
2565
2566                 ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
2567                 if (0 != ret) {
2568                         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);
2569                         return ret;
2570                 }
2571         }
2572         return 0;
2573 }
2574
2575 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
2576 {
2577         /* check if pid is valid */
2578         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
2579                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2580                 return VCD_ERROR_INVALID_PARAMETER;
2581         }
2582
2583         /* check if system command is valid */
2584         if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2585                 *is_sys_cmd_valid = true;
2586         else
2587                 *is_sys_cmd_valid = false;
2588
2589         return 0;
2590 }
2591
2592 static void __start_tts_request_thread(void* data, Ecore_Thread* thread)
2593 {
2594         SLOG(LOG_INFO, TAG_VCD, "[SUCCESS] Start tts request thread");
2595         vc_tts_text_data_s* tts_text_data = NULL;
2596
2597         while (1) {
2598                 int ret = -1;
2599                 int cnt = 0;
2600
2601                 /* Get tts text data */
2602                 ret = vcd_data_get_first_tts_text_data(&tts_text_data);
2603                 if (0 != ret || NULL == tts_text_data) {
2604                         /* empty queue */
2605                         if (0 >= vcd_data_get_tts_text_data_size()) {
2606                                 SLOG(LOG_INFO, TAG_VCD, "[DEBUG] No tts text data");
2607                                 return;
2608                         }
2609                         SLOG(LOG_INFO, TAG_VCD, "[INFO] tts text data is just incoming");
2610                         continue;
2611                 }
2612
2613                 while (1) {
2614                         vcd_state_e state = vcd_config_get_service_state();
2615                         if (VCD_STATE_READY != state) {
2616                                 if (0 == cnt++ % 10)
2617                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Waiting to request TTS, state(%d)", state);
2618                                 usleep(100000);
2619                                 continue;
2620                         }
2621                         break;
2622                 }
2623
2624                 /* Set service state to synthesizing */
2625                 vcd_config_set_service_state(VCD_STATE_SYNTHESIZING);
2626
2627                 /* Set current uid */
2628                 g_current_uid = tts_text_data->uid;
2629
2630                 /* Request tts to engine */
2631                 ret = vcd_engine_request_tts(tts_text_data->pid, tts_text_data->utt_id, tts_text_data->text, tts_text_data->language);
2632                 if (0 != ret) {
2633                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to request tts : %d", ret);
2634                 } else {
2635                         SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] request tts, uid(%d) pid(%d), text(%s), language(%s), utt_id(%d)",
2636                                 tts_text_data->uid, tts_text_data->pid, tts_text_data->text, tts_text_data->language, tts_text_data->utt_id);
2637                 }
2638                 /* clear tts text data after use */
2639                 vcd_data_clear_tts_text_data(&tts_text_data);
2640
2641         }
2642 }
2643
2644 static void __end_tts_request_thread(void* data, Ecore_Thread* thread)
2645 {
2646         SLOG(LOG_INFO, TAG_VCD, "[SUCCESS] End tts request thread");
2647         g_tts_thread = NULL;
2648 }
2649
2650 int vcd_server_request_tts(int pid, const char* text, const char* language, int to_vcm, int* utt_id)
2651 {
2652         /* check if pid is valid */
2653         if (false == vcd_client_is_available(pid)) {
2654                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2655                 return VCD_ERROR_INVALID_PARAMETER;
2656         }
2657
2658         vcd_state_e state = vcd_config_get_service_state();
2659         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2660                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready, state(%d)", state);
2661                 return VCD_ERROR_INVALID_STATE;
2662         }
2663
2664         int uid = -1;
2665         g_current_utt_id = (g_current_utt_id + 1) % 1000;
2666         *utt_id = g_current_utt_id;
2667         if (0 == to_vcm) {
2668                 uid = pid * 1000 + g_current_utt_id;
2669         } else {
2670                 uid = vcd_client_manager_get_pid() * 1000 + g_current_utt_id;
2671         }
2672         SLOG(LOG_INFO, TAG_VCD, "[Server INFO] pid(%d), text(%s), language(%s), to_vcm(%d), ", pid, text, language, to_vcm);
2673         SLOG(LOG_INFO, TAG_VCD, "[Server INFO] current_uid(%d), current_utt_id(%d)", uid, g_current_utt_id);
2674
2675         vc_tts_text_data_s* tts_text_data;
2676         tts_text_data = (vc_tts_text_data_s*)calloc(1, sizeof(vc_tts_text_data_s));
2677         if (!tts_text_data) {
2678                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to callocate memory ");
2679                 return VCD_ERROR_OUT_OF_MEMORY;
2680         }
2681         tts_text_data->uid = uid;
2682         tts_text_data->pid = pid;
2683         tts_text_data->utt_id = g_current_utt_id;
2684         tts_text_data->text = strdup(text);
2685         tts_text_data->language = strdup(language);
2686
2687         int ret = vcd_data_add_tts_text_data(tts_text_data->uid, tts_text_data);
2688         if (0 != ret) {
2689                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add tts text data : %d", ret);
2690         }
2691
2692         bool is_canceled = ecore_thread_check(g_tts_thread);
2693         if (NULL == g_tts_thread || TRUE == is_canceled) {
2694                 SLOG(LOG_INFO, TAG_VCD, "[Server INFO] ecore thread run : start_tts_request_thread ");
2695                 g_tts_thread = ecore_thread_run(__start_tts_request_thread, __end_tts_request_thread, NULL, NULL);
2696         }
2697
2698         return 0;
2699 }
2700
2701 int vcd_server_cancel_tts(int pid, int utt_id)
2702 {
2703         /* check if pid is valid */
2704         if (false == vcd_client_is_available(pid)) {
2705                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2706                 return VCD_ERROR_INVALID_PARAMETER;
2707         }
2708
2709         vcd_state_e state = vcd_config_get_service_state();
2710         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2711                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready, state(%d)", state);
2712                 return VCD_ERROR_INVALID_STATE;
2713         }
2714
2715         vc_tts_text_data_s* tts_text_data = NULL;
2716
2717         int uid = pid * 1000 + utt_id;
2718         int ret = vcd_data_get_tts_text_data(uid, &tts_text_data);
2719         if (0 != ret) {
2720                 SLOG(LOG_WARN, TAG_VCD, "[Server WARN] No data in vcd tts text queue");
2721         } else {
2722                 SLOG(LOG_INFO, TAG_VCD, "[Server] Clear tts text data, pid(%d), utt_id(%d), text(%s)", pid, utt_id, tts_text_data->text);
2723                 vcd_data_clear_tts_text_data(&tts_text_data);
2724         }
2725
2726         /* Request tts to engine */
2727         ret = vcd_engine_cancel_tts(pid, utt_id);
2728         if (0 != ret) {
2729                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to cancel tts : %d", ret);
2730         } else {
2731                 SLOG(LOG_INFO, TAG_VCD, "[Server SUCCESS] request tts, pid(%d), utt_id(%d)", pid, utt_id);
2732         }
2733
2734         return 0;
2735 }
2736
2737 int vcd_server_get_tts_audio_format(int pid, int* rate, int* channel, int* audio_type)
2738 {
2739         /* check if pid is valid */
2740         if (false == vcd_client_is_available(pid)) {
2741                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2742                 return VCD_ERROR_INVALID_PARAMETER;
2743         }
2744
2745         vcd_state_e state = vcd_config_get_service_state();
2746         if (VCD_STATE_READY != state && VCD_STATE_SYNTHESIZING != state) {
2747                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready and not synthesizing, state(%d)", state);
2748                 return VCD_ERROR_INVALID_STATE;
2749         }
2750
2751         /* Request tts to engine */
2752         int ret = vcd_engine_get_tts_audio_format(rate, channel, audio_type);
2753         if (0 != ret) {
2754                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get tts audio format : %d", ret);
2755         } else {
2756                 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);
2757         }
2758
2759         return ret;
2760 }
2761
2762 #if 0
2763 int vcd_server_set_exclusive_command(int pid, bool value)
2764 {
2765         /* check if pid is valid */
2766         if (false == vcd_client_is_available(pid)) {
2767                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2768                 return VCD_ERROR_INVALID_PARAMETER;
2769         }
2770
2771         if (true == value) {
2772                 if (0 != vcd_client_set_exclusive_command(pid)) {
2773                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2774                         return VCD_ERROR_OPERATION_FAILED;
2775                 }
2776         } else {
2777                 if (0 != vcd_client_unset_exclusive_command(pid)) {
2778                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2779                         return VCD_ERROR_OPERATION_FAILED;
2780                 }
2781         }
2782
2783         return 0;
2784 }
2785
2786 int vcd_server_request_start(int pid, bool stop_by_silence)
2787 {
2788         /* check if pid is valid */
2789         if (false == vcd_client_is_available(pid)) {
2790                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT foreground client", pid);
2791                 return VCD_ERROR_INVALID_PARAMETER;
2792         }
2793
2794         int ret;
2795         /* Check current state */
2796         vcd_state_e state = vcd_config_get_service_state();
2797
2798         /* Service state should be ready */
2799         if (VCD_STATE_READY != state) {
2800                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2801                 return VCD_ERROR_INVALID_STATE;
2802         }
2803
2804         if (-1 != vcd_client_manager_get_pid()) {
2805                 /* Check current pid is valid */
2806                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2807                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2808                         return VCD_ERROR_INVALID_PARAMETER;
2809                 }
2810         }
2811
2812         ret = vcd_server_mgr_start(stop_by_silence, false);
2813         if (0 != ret) {
2814                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2815                 return VCD_ERROR_INVALID_PARAMETER;
2816         }
2817
2818         return 0;
2819 }
2820
2821 int vcd_server_request_stop(int pid)
2822 {
2823         int ret;
2824         /* Check current state */
2825         vcd_state_e state = vcd_config_get_service_state();
2826
2827         /* Service state should be ready */
2828         if (VCD_STATE_RECORDING != state) {
2829                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2830                 return VCD_ERROR_INVALID_STATE;
2831         }
2832
2833         if (-1 != vcd_client_manager_get_pid()) {
2834                 /* Check current pid is valid */
2835                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2836                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2837                         return VCD_ERROR_INVALID_PARAMETER;
2838                 }
2839         }
2840
2841         ret = vcd_server_mgr_stop();
2842         if (0 != ret) {
2843                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2844                 return VCD_ERROR_OPERATION_FAILED;
2845         }
2846
2847         return VCD_ERROR_NONE;
2848 }
2849
2850 int vcd_server_request_cancel(int pid)
2851 {
2852         int ret;
2853         /* Check current state */
2854         vcd_state_e state = vcd_config_get_service_state();
2855
2856         /* Service state should be recording or processing */
2857         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2858                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2859                 return VCD_ERROR_INVALID_STATE;
2860         }
2861
2862         if (-1 != vcd_client_manager_get_pid()) {
2863                 /* Check current pid is valid */
2864                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2865                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2866                         return VCD_ERROR_INVALID_PARAMETER;
2867                 }
2868         }
2869
2870         ret = vcd_server_mgr_cancel();
2871         if (0 != ret) {
2872                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2873                 return VCD_ERROR_OPERATION_FAILED;
2874         }
2875
2876         return VCD_ERROR_NONE;
2877 }
2878 #endif
2879
2880 /*
2881 * VC Server Functions for Widget lib
2882 */
2883 int vcd_server_widget_initialize(int pid)
2884 {
2885         if (false == vcd_engine_is_available_engine()) {
2886                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2887                 return VCD_ERROR_ENGINE_NOT_FOUND;
2888         }
2889
2890         /* check if pid is valid */
2891         if (true == vcd_client_widget_is_available(pid)) {
2892                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2893                 return VCD_ERROR_NONE;
2894         }
2895
2896         /* Add client information to client manager */
2897         if (0 != vcd_client_widget_add(pid)) {
2898                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2899                 return VCD_ERROR_OPERATION_FAILED;
2900         }
2901
2902         SLOG(LOG_INFO, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2903
2904         return VCD_ERROR_NONE;
2905 }
2906
2907 static void __vcd_server_widget_start_recording(void *data)
2908 {
2909         SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] start recording");
2910
2911         if (0 != __start_internal_recognition()) {
2912                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition");
2913         }
2914 }
2915
2916 int vcd_server_widget_finalize(int pid)
2917 {
2918         /* check if pid is valid */
2919         if (false == vcd_client_widget_is_available(pid)) {
2920                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2921                 if (0 == vcd_client_get_ref_count()) {
2922                         SLOG(LOG_ERROR, TAG_VCD, "[Server] connected client list is empty, vc-service will be terminated");
2923                         ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2924                         return 0;
2925                 }
2926                 return VCD_ERROR_INVALID_PARAMETER;
2927         }
2928
2929         /* Remove client information */
2930         if (0 != vcd_client_widget_delete(pid)) {
2931                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2932         }
2933
2934         if (0 == vcd_client_get_ref_count()) {
2935                 SLOG(LOG_ERROR, TAG_VCD, "[Server] connected client list is empty, vc-service will be terminated");
2936                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2937                 return 0;
2938         }
2939
2940         bool is_waiting = false;
2941         if (0 != vcd_client_widget_get_waiting_for_recording(pid, &is_waiting)) {
2942                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get waiting to recording");
2943         }
2944
2945         if (true == is_waiting) {
2946                 SLOG(LOG_ERROR, TAG_VCD, "[Server INFO] invoke to start recording");
2947                 ecore_main_loop_thread_safe_call_async(__vcd_server_widget_start_recording, NULL);
2948         }
2949         return VCD_ERROR_NONE;
2950 }
2951
2952 int vcd_server_widget_start_recording(int pid, bool widget_command)
2953 {
2954         /* check if pid is valid */
2955         if (false == vcd_client_widget_is_available(pid)) {
2956                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2957                 return VCD_ERROR_INVALID_PARAMETER;
2958         }
2959
2960         bool waiting;
2961         if (0 != vcd_client_widget_get_waiting_for_recording(pid, &waiting) || false == waiting) {
2962                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Server is not waiting for recording, pid(%d), waiting(%d)", pid, waiting);
2963                 return 0;
2964         }
2965
2966         if (true == widget_command) {
2967                 vcd_client_widget_set_command(pid);
2968                 SLOG(LOG_INFO, TAG_VCD, "[Server] widget command is available");
2969         } else {
2970                 vcd_client_widget_unset_command(pid);
2971                 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2972         }
2973
2974         SLOG(LOG_ERROR, TAG_VCD, "[Server] start internal recognition : %d", widget_command);
2975         int ret = __start_internal_recognition();
2976         if (0 != ret) {
2977                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : %d", ret);
2978                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2979         }
2980
2981         return 0;
2982 }
2983
2984 int vcd_server_widget_start(int pid, bool stop_by_silence)
2985 {
2986         /* check if pid is valid */
2987         int fore_pid = vcd_client_widget_get_foreground_pid();
2988         if (pid != fore_pid) {
2989                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2990                 return VCD_ERROR_INVALID_PARAMETER;
2991         }
2992
2993         /* Check current state */
2994         vcd_state_e state = vcd_config_get_service_state();
2995
2996         /* Service state should be ready */
2997         if (VCD_STATE_READY != state) {
2998                 SLOG(LOG_INFO, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2999                 return VCD_ERROR_INVALID_STATE;
3000         }
3001
3002         vcd_client_set_slience_detection(stop_by_silence);
3003
3004         /* Notify show tooltip */
3005         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
3006
3007         return 0;
3008 }
3009
3010 int vcd_server_widget_stop(int pid)
3011 {
3012         /* check if pid is valid */
3013         int fore_pid = vcd_client_widget_get_foreground_pid();
3014         if (pid != fore_pid) {
3015                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
3016                 return VCD_ERROR_INVALID_PARAMETER;
3017         }
3018
3019         int ret;
3020         /* Check current state */
3021         vcd_state_e state = vcd_config_get_service_state();
3022
3023         /* Service state should be recording */
3024         if (VCD_STATE_RECORDING != state) {
3025                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
3026                 return VCD_ERROR_INVALID_STATE;
3027         }
3028
3029         ret = vcd_server_mgr_stop();
3030         if (0 != ret) {
3031                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to start recognition");
3032                 return VCD_ERROR_OPERATION_FAILED;
3033         }
3034
3035         return VCD_ERROR_NONE;
3036 }
3037
3038 int vcd_server_widget_cancel(int pid)
3039 {
3040         /* check if pid is valid */
3041         int fore_pid = vcd_client_widget_get_foreground_pid();
3042         if (pid != fore_pid) {
3043                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
3044                 return VCD_ERROR_INVALID_PARAMETER;
3045         }
3046
3047         int ret;
3048         /* Check current state */
3049         vcd_state_e state = vcd_config_get_service_state();
3050
3051         /* Service state should be recording or processing */
3052         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
3053                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
3054                 return VCD_ERROR_INVALID_STATE;
3055         }
3056
3057         ret = vcd_server_mgr_cancel();
3058         if (0 != ret) {
3059                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
3060                 return ret;
3061         }
3062
3063         return VCD_ERROR_NONE;
3064 }
3065
3066 int vcd_server_widget_enable_asr_result(int pid, bool enable)
3067 {
3068         int ret;
3069         ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
3070         if (0 != ret) {
3071                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
3072         }
3073
3074         return ret;
3075 }
3076
3077 int vcd_server_set_language(const char* language)
3078 {
3079         int ret = VCD_ERROR_NONE;
3080
3081         ret = vcd_config_set_default_language(language);
3082         if (0 != ret) {
3083                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to set language : %d", ret);
3084                 return ret;
3085         }
3086
3087         ret = vcd_engine_set_current_language(language);
3088         if (0 != ret) {
3089                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to set language : %d", ret);
3090                 return ret;
3091         }
3092
3093         return ret;
3094 }
3095 /*
3096 * For engine service
3097 */
3098 int vcd_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
3099 {
3100         SLOG(LOG_INFO, TAG_VCD, "[Server] Get foreach command");
3101
3102         if (NULL == callback) {
3103                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] input parameter is NULL");
3104                 return VCD_ERROR_INVALID_PARAMETER;
3105         }
3106
3107         int ret = 0;
3108         ret = vcd_engine_agent_get_foreach_command(vce_command, callback, user_data);
3109         if (0 != ret) {
3110                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreach command : ret(%d)", ret);
3111         }
3112
3113         return ret;
3114 }
3115
3116 int vcd_get_command_count(vce_cmd_h vce_command, int* count)
3117 {
3118         SLOG(LOG_INFO, TAG_VCD, "[Server] Get command count");
3119
3120         int ret = 0;
3121         ret = vcd_engine_agent_get_command_count(vce_command, count);
3122         if (0 != ret) {
3123                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get command count : ret(%d)", ret);
3124         }
3125
3126         return ret;
3127 }
3128
3129 int vcd_get_audio_type(char** audio_type)
3130 {
3131         SLOG(LOG_INFO, TAG_VCD, "[Server] Get audio type");
3132
3133         int ret = 0;
3134         ret = vcd_engine_agent_get_audio_type(audio_type);
3135         if (0 != ret) {
3136                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio type : ret(%d)", ret);
3137         }
3138
3139         return ret;
3140 }
3141
3142 int vcd_set_private_data(const char* key, const char* data)
3143 {
3144         vcd_state_e state = vcd_config_get_service_state();
3145
3146         if (VCD_STATE_READY != state) {
3147                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
3148                 return VCD_ERROR_INVALID_STATE;
3149         }
3150
3151         int ret = vcd_engine_agent_set_private_data(key, data);
3152         if (0 != ret) {
3153                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data to the manager client : ret(%d)", ret);
3154         } else {
3155                 SLOG(LOG_INFO, TAG_VCD, "[Server] Set private data to the manager client, key(%s), data(%s)", key, data);
3156         }
3157
3158         return ret;
3159 }
3160
3161 int vcd_get_private_data(const char* key, char** data)
3162 {
3163         vcd_state_e state = vcd_config_get_service_state();
3164
3165         if (VCD_STATE_READY != state) {
3166                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state(%d) is NOT 'READY'", state);
3167                 return VCD_ERROR_INVALID_STATE;
3168         }
3169
3170         int ret = vcd_engine_agent_get_private_data(key, data);
3171         if (0 != ret) {
3172                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data from the manager client : ret(%d)", ret);
3173         } else {
3174                 SLOG(LOG_INFO, TAG_VCD, "[Server] Get private data from the manager client, key(%s), data(%s)", key, *data);
3175         }
3176
3177         return ret;
3178 }
3179
3180 int vcd_start_recording()
3181 {
3182         SLOG(LOG_INFO, TAG_VCD, "[Server] Start recording");
3183
3184         int ret = 0;
3185         ret = vcd_engine_agent_start_recording();
3186         if (0 != ret) {
3187                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recording : ret(%d)", ret);
3188         }
3189
3190         return ret;
3191 }
3192
3193 int vcd_stop_recording()
3194 {
3195         SLOG(LOG_INFO, TAG_VCD, "[Server] Stop recording");
3196
3197         int ret = 0;
3198         ret = vcd_engine_agent_stop_recording();
3199         if (0 != ret) {
3200                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recording : ret(%d)", ret);
3201         }
3202
3203         return ret;
3204 }
3205
3206 int vcd_send_update_status(vce_update_event_e update_event, const char* msg)
3207 {
3208         SLOG(LOG_INFO, TAG_VCD, "[Server] update status, update event(%d), msg(%s)", update_event, msg);
3209
3210         int ret = 0;
3211         if (VCE_UPDATE_EVENT_START == update_event) {
3212                 if (VCD_STATE_RECORDING == vcd_config_get_service_state() || VCD_STATE_PROCESSING == vcd_config_get_service_state()) {
3213                         ret = vcd_server_mgr_cancel();
3214                         if (0 != ret) {
3215                                 SLOG(LOG_ERROR, TAG_VCD, "[Server Error] Fail to cancel, ret(%d)", ret);
3216                                 return ret;
3217                         }
3218                 }
3219                 vcd_config_set_service_state(VCD_STATE_UPDATING);
3220                 vcdc_send_service_state(VCD_STATE_UPDATING);
3221
3222         } else if (VCE_UPDATE_EVENT_FINISH == update_event) {
3223                 vcd_config_set_service_state(VCD_STATE_READY);
3224                 vcdc_send_service_state(VCD_STATE_READY);
3225
3226         } else if (VCE_UPDATE_EVENT_FAIL == update_event) {
3227                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Update event : Fail - msg(%s)", msg);
3228         }
3229
3230         return 0;
3231 }
3232
3233 int vcd_set_private_data_set_cb(vce_private_data_set_cb callback_func)
3234 {
3235         SLOG(LOG_INFO, TAG_VCD, "[Server] Set private data set cb");
3236
3237         int ret = 0;
3238         ret = vcd_engine_agent_set_private_data_set_cb(callback_func);
3239         if (0 != ret) {
3240                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data set cb : ret(%d)", ret);
3241         }
3242
3243         return ret;
3244 }
3245
3246 int vcd_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
3247 {
3248         SLOG(LOG_INFO, TAG_VCD, "[Server] Set private data requested cb");
3249
3250         int ret = 0;
3251         ret = vcd_engine_agent_set_private_data_requested_cb(callback_func);
3252         if (0 != ret) {
3253                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data requested cb : ret(%d)", ret);
3254         }
3255
3256         return ret;
3257 }
3258
3259 int vcd_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
3260 {
3261         SLOG(LOG_INFO, TAG_VCD, "[Server] Set nlu base info requested cb");
3262
3263         int ret = 0;
3264         ret = vcd_engine_agent_set_nlu_base_info_requested_cb(callback_func);
3265         if (0 != ret) {
3266                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu base info requested cb : ret(%d)", ret);
3267         }
3268
3269         return ret;
3270 }
3271
3272 int vcd_set_specific_engine_request_cb(vce_specific_engine_request_cb callback_func)
3273 {
3274         SLOG(LOG_INFO, TAG_VCD, "[Server] Set specific engine request cb");
3275         int ret = 0;
3276         ret = vcd_engine_agent_set_specific_engine_request_cb(callback_func);
3277         if (0 != ret) {
3278                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set specific engine request cb : ret(%d)", ret);
3279         }
3280
3281         return ret;
3282 }
3283
3284 int vcd_set_request_tts_cb(vce_request_tts_cb callback_func, void* user_data)
3285 {
3286         SLOG(LOG_INFO, TAG_VCD, "[Server] Set request tts cb");
3287         int ret = 0;
3288         ret = vcd_engine_agent_set_request_tts_cb(callback_func, user_data);
3289         if (0 != ret) {
3290                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set request tts cb : ret(%d)", ret);
3291         }
3292
3293         return ret;
3294 }
3295
3296 int vcd_set_cancel_tts_cb(vce_cancel_tts_cb callback_func, void* user_data)
3297 {
3298         SLOG(LOG_INFO, TAG_VCD, "[Server] Set cancel tts cb");
3299         int ret = 0;
3300         ret = vcd_engine_agent_set_cancel_tts_cb(callback_func, user_data);
3301         if (0 != ret) {
3302                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set cancel tts cb : ret(%d)", ret);
3303         }
3304
3305         return ret;
3306 }
3307
3308 int vcd_set_tts_audio_format_request_cb(vce_tts_audio_format_request_cb callback_func, void* user_data)
3309 {
3310         SLOG(LOG_INFO, TAG_VCD, "[Server] Set tts audio format request cb");
3311         int ret = 0;
3312         ret = vcd_engine_agent_set_get_tts_audio_format_cb(callback_func, user_data);
3313         if (0 != ret) {
3314                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set tts audio format request : ret(%d)", ret);
3315         }
3316
3317         return ret;
3318 }