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