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