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