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