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