Fix invalid command collect logic
[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                         bool temp = vcd_client_manager_get_exclusive();
645                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
646                 }
647
648                 int pid = vcd_client_widget_get_foreground_pid();
649                 if (-1 != pid) {
650                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
651                         /* Send to hide tooltip */
652                         vcdc_send_show_tooltip(pid, false);
653                 }
654
655                 if (-1 != vcd_client_manager_get_pid()) {
656                         /* Manager client is available */
657                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
658                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
659                         }
660                 }
661
662                 vcd_client_manager_set_exclusive(false);
663
664                 return;
665         } else {
666                 if (false == vcd_client_manager_get_exclusive()) {
667                         int pid = vcd_client_widget_get_foreground_pid();
668                         if (-1 != pid) {
669                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
670                                 vcdc_send_show_tooltip(pid, false);
671                         }
672
673                         vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
674
675                         if (-1 != vcd_client_manager_get_pid()) {
676                                 /* Manager client is available */
677                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
678                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
679                                 }
680                         } else {
681                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
682                                 /* Send result to client */
683                                 ecore_timer_add(0, __vcd_send_selected_result, NULL);
684                         }
685                 } else {
686                         /* exclusive command */
687                         vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
688
689                         if (-1 != vcd_client_manager_get_pid()) {
690                                 /* Manager client is available */
691                                 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
692                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
693                                 }
694                         } else {
695                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
696                         }
697
698                         vcd_client_manager_set_exclusive(false);
699                 }
700         }
701
702         vc_cmd_list_destroy(vc_cmd_list, true);
703
704         return;
705
706 #else
707         /* No result */
708         if (NULL == result_id) {
709                 /* No result */
710                 if (NULL != all_result) {
711                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
712                         bool temp = vcd_client_manager_get_exclusive();
713                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
714                 }
715
716                 int pid = vcd_client_widget_get_foreground_pid();
717                 if (-1 != pid) {
718                         if (NULL != all_result) {
719                                 /* Send result text to widget */
720                                 vcdc_send_result(pid, vcd_client_manager_get_pid(), VC_COMMAND_TYPE_WIDGET);
721                         }
722
723                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
724                         /* Send to hide tooltip */
725                         vcdc_send_show_tooltip(pid, false);
726                 }
727
728                 if (-1 != vcd_client_manager_get_pid()) {
729                         /* Manager client is available */
730                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
731                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
732                         }
733                 }
734
735                 vcd_client_manager_set_exclusive(false);
736
737                 return;
738         }
739
740         /* Normal result */
741         SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
742
743         int ret = -1;
744         vc_cmd_s* temp_cmd = NULL;
745         vc_cmd_list_h vc_cmd_list = NULL;
746
747         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
748                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
749                 vcd_client_manager_set_exclusive(false);
750                 vcd_config_set_service_state(VCD_STATE_READY);
751                 vcdc_send_service_state(VCD_STATE_READY);
752                 return;
753         }
754
755         int i = 0;
756         for (i = 0; i < count; i++) {
757                 SLOG(LOG_DEBUG, TAG_VCD, "[Server]   [%d] Result ID(%d)", i, result_id[i]);
758
759                 if (result_id[i] < 0) {
760                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
761                         continue;
762                 }
763
764                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
765                 if (0 == ret && NULL != temp_cmd) {
766                         switch (temp_cmd->format) {
767                         case VC_CMD_FORMAT_FIXED:
768                         case VC_CMD_FORMAT_FIXED_AND_VFIXED:
769                         case VC_CMD_FORMAT_VFIXED_AND_FIXED:
770                         case VC_CMD_FORMAT_PARTIAL:
771                                 /* Nonfixed result is NOT valid */
772                                 break;
773                         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:
774                                 if (NULL == temp_cmd->parameter) {
775                                         if (NULL != non_fixed_result) {
776                                                 temp_cmd->parameter = strdup(non_fixed_result);
777                                         }
778                                 } else {
779                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
780                                 }
781                                 break;
782                         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:
783                                 if (NULL == temp_cmd->command) {
784                                         if (NULL != non_fixed_result) {
785                                                 temp_cmd->command = strdup(non_fixed_result);
786                                         }
787                                 } else {
788                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
789                                 }
790
791                                 break;
792                         default:
793                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
794                         }
795
796                         temp_cmd->id = i;
797                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
798                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
799                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
800                         }
801                 } else {
802                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
803                 }
804         }
805
806         vc_cmd_print_list(vc_cmd_list);
807
808         SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
809
810         int result_count = 0;
811         vc_cmd_list_get_count(vc_cmd_list, &result_count);
812
813         if (false == vcd_client_manager_get_exclusive()) {
814                 int pid = vcd_client_widget_get_foreground_pid();
815                 if (-1 != pid) {
816                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
817                         vcdc_send_show_tooltip(pid, false);
818                 }
819
820                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
821
822                 if (-1 != vcd_client_manager_get_pid()) {
823                         /* Manager client is available */
824                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
825                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
826                         }
827                 } else {
828                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
829                         /* Send result to client */
830                         ecore_timer_add(0, __vcd_send_selected_result, NULL);
831                 }
832         } else {
833                 /* exclusive command */
834                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
835
836                 if (-1 != vcd_client_manager_get_pid()) {
837                         /* Manager client is available */
838                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
839                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
840                         }
841                 } else {
842                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
843                 }
844
845                 vcd_client_manager_set_exclusive(false);
846         }
847
848         vc_cmd_list_destroy(vc_cmd_list, true);
849
850         return;
851 #endif
852 }
853
854 #if 0
855 static void __vcd_server_nlu_result_cb(vcp_result_event_e event, const char* nlu_result, void *user_data)
856 {
857         SLOG(LOG_DEBUG, TAG_VCD, "[Server] NLU result cb - event(%d)", event);
858         SLOG(LOG_DEBUG, TAG_VCD, "[Server] result (%s)", nlu_result);
859
860         int ret = vc_info_parser_set_nlu_result(nlu_result);
861         if (0 != ret) {
862                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set nlu result (%d)", ret);
863         }
864
865         return;
866 }
867 #endif
868
869 static void __vcd_server_error_cb(vcp_error_e error, const char* msg, void *user_data)
870 {
871         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Engine Error cb - reason(%d), msg(%s)", error, msg);
872         ecore_main_loop_thread_safe_call_async(__cancel_by_error, NULL);
873
874         char* error_msg = NULL;
875         error_msg = strdup(msg);
876
877         if (0 != vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), error, error_msg)) {
878                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send error signal");
879         }
880
881         if (NULL != error_msg) {
882                 free(error_msg);
883                 error_msg = NULL;
884         }
885
886         return;
887 }
888
889 /*
890 * vcd server Interfaces
891 */
892 static void __vcd_file_clean_up()
893 {
894         SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
895
896         DIR *dp = NULL;
897         struct dirent *dirp = NULL;
898
899         dp = opendir(VC_RUNTIME_INFO_ROOT);
900         if (dp == NULL) {
901                 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
902                 return;
903         }
904
905         char remove_path[256] = {0, };
906         do {
907                 dirp = readdir(dp);
908
909                 if (NULL != dirp) {
910                         if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
911                                 memset(remove_path, 0, 256);
912                                 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
913
914                                 /* Clean up code */
915                                 if (0 != remove(remove_path)) {
916                                         SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
917                                 } else {
918                                         SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
919                                 }
920                         }
921                 }
922         } while (NULL != dirp);
923
924         closedir(dp);
925
926         return;
927 }
928
929 static int __vcd_db_clean_up()
930 {
931         int ret = 0;
932         int cnt = VC_COMMAND_TYPE_FOREGROUND;
933         do {
934                 if (VC_COMMAND_TYPE_BACKGROUND != cnt)
935                         ret = vc_db_delete_commands(-1, cnt, NULL);
936         } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
937
938         return ret;
939 }
940
941 int vcd_initialize()
942 {
943         int ret = 0;
944
945         /* Remove old file */
946         __vcd_file_clean_up();
947
948         /* initialize modules */
949         ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
950         if (0 != ret) {
951                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
952         }
953
954         /* Remove db data */
955         ret = __vcd_db_clean_up();
956         if (0 != ret) {
957                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to remove db data");
958         }
959
960         vcd_config_set_service_state(VCD_STATE_NONE);
961
962         //ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb, __vcd_server_nlu_result_cb, __vcd_server_error_cb);
963         ret = vcd_engine_agent_init(__vcd_server_asr_result_cb, __vcd_server_result_cb, __vcd_server_error_cb);
964         if (0 != ret) {
965                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
966                 return ret;
967         }
968
969         if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
970                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
971                 return VCD_ERROR_OPERATION_FAILED;
972         }
973
974         /* Find engine */
975         ret = vcd_engine_agent_initialize_current_engine();
976         if (0 != ret) {
977                 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
978                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
979                 else
980                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
981
982                 g_is_engine = false;
983         } else {
984                 g_is_engine = true;
985         }
986
987         /* Load engine */
988         if (0 != vcd_engine_agent_load_current_engine()) {
989                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
990                 return VCD_ERROR_OPERATION_FAILED;
991         }
992
993         /* Initialize manager info */
994         vcd_client_manager_unset();
995
996         vcd_config_set_service_state(VCD_STATE_READY);
997         vcdc_send_service_state(VCD_STATE_READY);
998
999         SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
1000
1001         return 0;
1002 }
1003
1004 void vcd_finalize()
1005 {
1006         GList *iter = NULL;
1007         if (0 < g_list_length(g_proc_list)) {
1008                 iter = g_list_first(g_proc_list);
1009                 while (NULL != iter) {
1010                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1011                         iter = g_list_first(g_proc_list);
1012                 }
1013         }
1014
1015         if (g_restart_timer != NULL) {
1016                 ecore_timer_del(g_restart_timer);
1017                 g_restart_timer = NULL;
1018         }
1019
1020         vcd_state_e state = vcd_config_get_service_state();
1021         if (VCD_STATE_READY != state) {
1022                 if (VCD_STATE_RECORDING == state) {
1023                         vcd_recorder_stop();
1024                 }
1025                 vcd_engine_recognize_cancel();
1026         }
1027         if (0 != vcd_recorder_destroy()) {
1028                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
1029         } else {
1030                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
1031         }
1032
1033         if (0 != vcd_engine_agent_release()) {
1034                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
1035         } else {
1036                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
1037         }
1038
1039         vcd_client_manager_unset_appid();
1040
1041         vcd_config_set_service_state(VCD_STATE_NONE);
1042         vcdc_send_service_state(VCD_STATE_NONE);
1043
1044         SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
1045
1046         return;
1047 }
1048
1049 static Eina_Bool __finalize_quit_ecore_loop(void *data)
1050 {
1051         SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
1052         ecore_main_loop_quit();
1053         return EINA_FALSE;
1054 }
1055
1056 static void __read_proc()
1057 {
1058         DIR *dp = NULL;
1059         struct dirent *dirp = NULL;
1060         int tmp;
1061
1062         GList *iter = NULL;
1063         if (0 < g_list_length(g_proc_list)) {
1064                 iter = g_list_first(g_proc_list);
1065                 while (NULL != iter) {
1066                         g_proc_list = g_list_remove_link(g_proc_list, iter);
1067                         iter = g_list_first(g_proc_list);
1068                 }
1069         }
1070
1071         dp = opendir("/proc");
1072         if (NULL == dp) {
1073                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
1074         } else {
1075                 do {
1076                         dirp = readdir(dp);
1077
1078                         if (NULL != dirp) {
1079                                 tmp = atoi(dirp->d_name);
1080                                 if (0 >= tmp)   continue;
1081                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
1082                         }
1083                 } while (NULL != dirp);
1084                 closedir(dp);
1085         }
1086         return;
1087 }
1088
1089 static void __vcd_cleanup_client(vcd_client_type_e type)
1090 {
1091         int* client_list = NULL;
1092         int client_count = 0;
1093         int i = 0;
1094         int j = 0;
1095         bool exist = false;
1096         int mgr_pid = -1;
1097         int ret = -1;
1098
1099         if (VCD_CLIENT_TYPE_NORMAL == type) {
1100                 ret = vcd_client_get_list(&client_list, &client_count);
1101         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
1102                 ret = vcd_client_widget_get_list(&client_list, &client_count);
1103         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
1104                 mgr_pid = vcd_client_manager_get_pid();
1105                 client_list = &mgr_pid;
1106                 client_count = 1;
1107                 if (-1 == mgr_pid) {
1108                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
1109                         return;
1110                 }
1111         }
1112
1113         if (0 == ret || mgr_pid > 0) {
1114                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
1115                 if (NULL != client_list && client_count > 0) {
1116                         for (i = 0; i < client_count; i++) {
1117                                 exist = false;
1118                                 GList *iter = NULL;
1119                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1120                                         iter = g_list_nth(g_proc_list, j);
1121                                         if (NULL != iter) {
1122                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1123                                                         SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1124                                                         exist = true;
1125                                                         break;
1126                                                 }
1127                                         }
1128                                 }
1129
1130                                 if (false == exist) {
1131                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
1132                                         if (VCD_CLIENT_TYPE_NORMAL == type)
1133                                                 vcd_server_finalize(client_list[i]);
1134                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
1135                                                 vcd_server_widget_finalize(client_list[i]);
1136                                         else
1137                                                 vcd_server_mgr_finalize(mgr_pid);
1138                                 }
1139                         }
1140                 }
1141                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1142                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
1143         }
1144         if (NULL != client_list && -1 == mgr_pid) {
1145                 free(client_list);
1146                 client_list = NULL;
1147         }
1148         return;
1149 }
1150
1151 Eina_Bool vcd_cleanup_client_all(void *data)
1152 {
1153         __read_proc();
1154         
1155         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
1156         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
1157         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
1158
1159 #if 0
1160         if (0 == vcd_client_get_list(&client_list, &client_count)) {
1161                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
1162                 if (NULL != client_list && client_count > 0) {
1163                         for (i = 0; i < client_count; i++) {
1164                                 exist = false;
1165                                 GList *iter = NULL;
1166                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1167                                         iter = g_list_nth(g_proc_list, j);
1168                                         if (NULL != iter) {
1169                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1170                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
1171                                                         exist = true;
1172                                                         break;
1173                                                 }
1174                                         }
1175                                 }
1176
1177                                 if (false == exist) {
1178                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
1179                                         vcd_server_finalize(client_list[i]);
1180                                 }
1181 #if 0
1182                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
1183
1184                                 if (0 == result) {
1185                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
1186                                         vcd_server_finalize(client_list[i]);
1187                                 } else if (-1 == result) {
1188                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1189                                 }
1190 #endif
1191                         }
1192                 }
1193                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1194                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
1195         }
1196         if (NULL != client_list) {
1197                 free(client_list);
1198                 client_list = NULL;
1199         }
1200
1201         /* If app is in background state, app cannot response message. */
1202         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
1203                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
1204                 if (NULL != client_list && client_count > 0) {
1205                         for (i = 0; i < client_count; i++) {
1206                                 exist = false;
1207                                 GList *iter = NULL;
1208                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1209                                         iter = g_list_nth(g_proc_list, j);
1210                                         if (NULL != iter) {
1211                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
1212                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
1213                                                         exist = true;
1214                                                         break;
1215                                                 }
1216                                         }
1217                                 }
1218
1219                                 if (false == exist) {
1220                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
1221                                         vcd_server_widget_finalize(client_list[i]);
1222                                 }
1223 #if 0
1224                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
1225
1226                                 if (0 == result) {
1227                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
1228                                         vcd_server_widget_finalize(client_list[i]);
1229                                 } else if (-1 == result) {
1230                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
1231                                 }
1232 #endif
1233                         }
1234                 }
1235                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
1236                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
1237         }
1238
1239         if (NULL != client_list) {
1240                 free(client_list);
1241                 client_list = NULL;
1242         }
1243
1244         /* manager */
1245         exist = false;
1246         GList *iter = NULL;
1247         int mgr_pid = vcd_client_manager_get_pid();
1248         if (0 < mgr_pid) {
1249                 for (j = 0; j < g_list_length(g_proc_list); j++) {
1250                         iter = g_list_nth(g_proc_list, j);
1251                         if (NULL != iter) {
1252                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
1253                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
1254                                         exist = true;
1255                                         break;
1256                                 }
1257                         }
1258                 }
1259
1260                 if (false == exist) {
1261                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
1262                         vcd_server_mgr_finalize(mgr_pid);
1263                 }
1264         }
1265 #endif
1266         return EINA_TRUE;
1267 }
1268
1269 int vcd_server_get_service_state()
1270 {
1271         return vcd_config_get_service_state();
1272 }
1273
1274 int vcd_server_get_foreground()
1275 {
1276         int pid;
1277         vcd_config_get_foreground(&pid);
1278         return pid;
1279 }
1280
1281
1282 /*
1283 * API for manager
1284 */
1285 int vcd_server_mgr_initialize(int pid)
1286 {
1287         if (false == g_is_engine) {
1288                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1289                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1290                         g_is_engine = false;
1291                         return VCD_ERROR_ENGINE_NOT_FOUND;
1292                 } else {
1293                         g_is_engine = true;
1294                 }
1295         }
1296
1297         /* check if pid is valid */
1298         if (false == vcd_client_manager_is_valid(pid)) {
1299                 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
1300                 vcd_server_mgr_cancel();
1301                 vcd_client_manager_unset();
1302         }
1303
1304         /* Add client information to client manager */
1305         if (0 != vcd_client_manager_set(pid)) {
1306                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
1307                 return VCD_ERROR_OPERATION_FAILED;
1308         }
1309
1310         if (0 != vcdc_send_manager_pid(pid))
1311                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1312
1313         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
1314
1315         return VCD_ERROR_NONE;
1316 }
1317
1318 int vcd_server_mgr_finalize(int pid)
1319 {
1320         /* check if pid is valid */
1321         if (false == vcd_client_manager_is_valid(pid)) {
1322                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1323                 return VCD_ERROR_INVALID_PARAMETER;
1324         }
1325
1326         /* Cancel recognition */
1327         vcd_server_mgr_cancel();
1328
1329         if (0 != vcdc_send_manager_pid(-1))
1330                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send manager pid");
1331
1332         /* Remove manager information */
1333         if (0 != vcd_client_manager_unset()) {
1334                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1335         }
1336
1337         if (0 == vcd_client_get_ref_count()) {
1338                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1339                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1340         }
1341
1342         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
1343
1344         return VCD_ERROR_NONE;
1345 }
1346
1347 int vcd_server_mgr_set_command(int pid)
1348 {
1349         if (0 != vcd_client_manager_set_command(pid)) {
1350                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1351                 return VCD_ERROR_INVALID_PARAMETER;
1352         }
1353         return VCD_ERROR_NONE;
1354 }
1355
1356 int vcd_server_mgr_unset_command(int pid)
1357 {
1358         if (0 != vcd_client_manager_unset_command(pid)) {
1359                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1360                 return VCD_ERROR_INVALID_PARAMETER;
1361         }
1362         return VCD_ERROR_NONE;
1363 }
1364
1365 int vcd_server_mgr_set_demandable_client(int pid)
1366 {
1367         /* check if pid is valid */
1368         if (false == vcd_client_manager_is_valid(pid)) {
1369                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1370                 return VCD_ERROR_INVALID_PARAMETER;
1371         }
1372
1373         GSList* client_list = NULL;
1374         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
1375                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
1376                 return VCD_ERROR_OPERATION_FAILED;
1377         }
1378
1379         /* Save client list */
1380         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
1381                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
1382                 return VCD_ERROR_OPERATION_FAILED;
1383         }
1384
1385         return VCD_ERROR_NONE;
1386 }
1387
1388 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
1389 {
1390         /* check if pid is valid */
1391         if (false == vcd_client_manager_is_valid(pid)) {
1392                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1393                 return VCD_ERROR_INVALID_PARAMETER;
1394         }
1395
1396         int ret = 0;
1397         vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
1398         int rate = 16000;
1399         int channel = 1;
1400
1401         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
1402         if (0 != ret) {
1403                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
1404                 return VCD_ERROR_OPERATION_FAILED;
1405         }
1406
1407         ret = vcd_recorder_set(audio_type, type, rate, channel);
1408         if (0 != ret) {
1409                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
1410                 return VCD_ERROR_OPERATION_FAILED;
1411         }
1412
1413         return VCD_ERROR_NONE;
1414 }
1415
1416 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1417 {
1418         /* check if pid is valid */
1419         if (false == vcd_client_manager_is_valid(pid)) {
1420                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1421                 return VCD_ERROR_INVALID_PARAMETER;
1422         }
1423
1424         int ret = vcd_recorder_get(audio_type);
1425         if (0 != ret) {
1426                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1427                 return VCD_ERROR_OPERATION_FAILED;
1428         }
1429
1430         return VCD_ERROR_NONE;
1431 }
1432
1433 int vcd_server_mgr_set_client_info(int pid)
1434 {
1435         /* check if pid is valid */
1436         if (false == vcd_client_manager_is_valid(pid)) {
1437                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1438                 return VCD_ERROR_INVALID_PARAMETER;
1439         }
1440
1441         int ret = vcd_client_save_client_info();
1442         if (0 != ret) {
1443                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1444                 return VCD_ERROR_OPERATION_FAILED;
1445         }
1446
1447         return VCD_ERROR_NONE;
1448 }
1449
1450 static int __start_internal_recognition()
1451 {
1452         int ret;
1453
1454         if (0 != vcd_client_command_collect_command()) {
1455                 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to collect command");
1456                 return VCD_ERROR_OPERATION_FAILED;
1457         }
1458
1459         /* 3. Set command to engine */
1460         ret = vcd_engine_set_commands();
1461         if (0 != ret) {
1462                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1463                 return VCD_ERROR_OPERATION_FAILED;
1464         }
1465
1466         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1467
1468         bool stop_by_silence = true;
1469         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1470                 stop_by_silence = false;
1471         }
1472
1473         vcd_client_manager_set_result_text(NULL);
1474
1475         /* 4. start recognition */
1476         ret = vcd_engine_recognize_start(stop_by_silence);
1477         if (0 != ret) {
1478                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1479                 return VCD_ERROR_OPERATION_FAILED;
1480         }
1481
1482         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1483
1484         /* 5. recorder start */
1485         ret = vcd_recorder_start();
1486         if (0 != ret) {
1487                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1488                 vcd_engine_recognize_cancel();
1489                 return ret;
1490         }
1491
1492         vcd_config_set_service_state(VCD_STATE_RECORDING);
1493         vcdc_send_service_state(VCD_STATE_RECORDING);
1494
1495         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1496
1497         return 0;
1498 }
1499
1500 static Eina_Bool __vcd_request_show_tooltip(void *data)
1501 {
1502         int pid = vcd_client_widget_get_foreground_pid();
1503         if (-1 != pid) {
1504                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1505                 vcdc_send_show_tooltip(pid, (bool)data);
1506         }
1507
1508         return EINA_FALSE;
1509 }
1510
1511 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1512 {
1513         /* 1. check current state */
1514         vcd_state_e state = vcd_config_get_service_state();
1515
1516         if (VCD_STATE_READY != state) {
1517                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1518                 return VCD_ERROR_INVALID_STATE;
1519         }
1520         if (-1 == vcd_client_manager_get_pid()) {
1521                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1522                 return VCD_ERROR_OPERATION_FAILED;
1523         }
1524
1525         SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1526         vcd_client_set_recognition_mode(recognition_mode);
1527
1528         if (false == exclusive_cmd) {
1529                 /* Notify show tooltip */
1530                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1531                         int pid = vcd_client_widget_get_foreground_pid();
1532                         if (-1 != pid) {
1533                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1534                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1535                                 return 0;
1536                         }
1537                 }
1538         } else {
1539                 vcd_client_manager_set_exclusive(exclusive_cmd);
1540         }
1541
1542         int fg_pid = -1;
1543         if (true == start_by_client) {
1544                 /* Get foreground pid */
1545                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1546                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1547                 }
1548
1549                 /* Set client exclusive option */
1550                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1551                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1552                 }
1553         }
1554
1555         int ret = __start_internal_recognition();
1556         if (0 != ret) {
1557                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1558                 return ret;
1559         }
1560
1561         if (true == start_by_client) {
1562                 vcd_client_unset_exclusive_command(fg_pid);
1563         }
1564
1565         return VCD_ERROR_NONE;
1566 }
1567
1568 int vcd_server_mgr_stop()
1569 {
1570         /* 1. Check current state is recording */
1571         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1572                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1573                 return VCD_ERROR_INVALID_STATE;
1574         }
1575         if (-1 == vcd_client_manager_get_pid()) {
1576                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1577                 return VCD_ERROR_OPERATION_FAILED;
1578         }
1579
1580         /* 2. Stop recorder */
1581         vcd_recorder_stop();
1582
1583         /* 3. Stop engine recognition */
1584         int ret = vcd_engine_recognize_stop();
1585         if (0 != ret) {
1586                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1587         }
1588
1589         /* 4. Set original mode */
1590         vcd_config_set_service_state(VCD_STATE_PROCESSING);
1591         vcdc_send_service_state(VCD_STATE_PROCESSING);
1592
1593         return VCD_ERROR_NONE;
1594 }
1595
1596 int vcd_server_mgr_cancel()
1597 {
1598         /* 1. Check current state */
1599         vcd_state_e state = vcd_config_get_service_state();
1600         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1601                 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1602                 return VCD_ERROR_INVALID_STATE;
1603         }
1604         if (-1 == vcd_client_manager_get_pid()) {
1605                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available.");
1606                 return VCD_ERROR_OPERATION_FAILED;
1607         }
1608
1609         if (g_restart_timer != NULL) {
1610                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1611                 ecore_timer_del(g_restart_timer);
1612                 g_restart_timer = NULL;
1613         }
1614
1615         /* 2. Stop recorder */
1616         vcd_recorder_stop();
1617         /* 3. Cancel engine */
1618         int ret = vcd_engine_recognize_cancel();
1619         if (0 != ret) {
1620                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1621         }
1622
1623         if (false == vcd_client_manager_get_exclusive()) {
1624                 if (1 == vcd_config_get_command_type_enabled(VC_COMMAND_TYPE_WIDGET)) {
1625                         int pid = vcd_client_widget_get_foreground_pid();
1626                         if (-1 != pid) {
1627                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1628                                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1629                         }
1630                 }
1631         } else {
1632                 vcd_client_manager_set_exclusive(false);
1633         }
1634
1635         /* 4. Set state */
1636         vcd_config_set_service_state(VCD_STATE_READY);
1637         vcdc_send_service_state(VCD_STATE_READY);
1638
1639         return VCD_ERROR_NONE;
1640 }
1641
1642
1643 int vcd_server_mgr_result_select()
1644 {
1645         __vcd_send_selected_result(NULL);
1646
1647         return VCD_ERROR_NONE;
1648 }
1649
1650 int vcd_server_mgr_set_domain(int pid, const char* domain)
1651 {
1652         /* check if pid is valid */
1653         if (false == vcd_client_manager_is_valid(pid)) {
1654                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1655                 return VCD_ERROR_INVALID_PARAMETER;
1656         }
1657
1658         vcd_state_e state = vcd_config_get_service_state();
1659         if (VCD_STATE_READY != state) {
1660                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1661                 return VCD_ERROR_INVALID_STATE;
1662         }
1663
1664         /* Set domain to engine */
1665         int ret = vcd_engine_set_domain(pid, domain);
1666         if (0 != ret) {
1667                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set domain : %d", ret);
1668         } else {
1669                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set domain");
1670         }
1671
1672         return ret;
1673 }
1674
1675 int vcd_server_mgr_set_private_data(int pid, const char* key, const char* data)
1676 {
1677         /* check if pid is valid */
1678         if (false == vcd_client_manager_is_valid(pid)) {
1679                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1680                 return VCD_ERROR_INVALID_PARAMETER;
1681         }
1682
1683         vcd_state_e state = vcd_config_get_service_state();
1684         if (VCD_STATE_READY != state) {
1685                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1686                 return VCD_ERROR_INVALID_STATE;
1687         }
1688
1689         /* Set private data to engine */
1690         int ret = vcd_engine_set_private_data(pid, key, data);
1691         if (0 != ret) {
1692                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set private data : %d", ret);
1693         } else {
1694                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1695         }
1696
1697         return ret;
1698 }
1699
1700 int vcd_server_mgr_get_private_data(int pid, const char* key, char** data)
1701 {
1702         /* check if pid is valid */
1703         if (false == vcd_client_manager_is_valid(pid)) {
1704                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1705                 return VCD_ERROR_INVALID_PARAMETER;
1706         }
1707         vcd_state_e state = vcd_config_get_service_state();
1708         if (VCD_STATE_READY != state) {
1709                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1710                 return VCD_ERROR_INVALID_STATE;
1711         }
1712
1713         /* Get private data to engine */
1714         int ret = vcd_engine_get_private_data(pid, key, data);
1715         if (0 != ret) {
1716                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get private data : %d", ret);
1717         } else {
1718                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Set private data");
1719         }
1720
1721         return ret;
1722 }
1723
1724 int vcd_server_mgr_do_action(int pid, int type, const char* action)
1725 {
1726         int ret = -1;
1727
1728         /* check if pid is valid */
1729         if (false == vcd_client_manager_is_valid(pid)) {
1730                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1731                 return VCD_ERROR_INVALID_PARAMETER;
1732         }
1733
1734         vcd_state_e state = vcd_config_get_service_state();
1735         if (VCD_STATE_READY != state) {
1736                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1737                 return VCD_ERROR_INVALID_STATE;
1738         }
1739
1740         /* Reqeust do action to engine */
1741         if (VCD_SEND_EVENT_TYPE_TEXT == type)
1742                 ret = vcd_engine_process_text(pid, action);
1743         else if (VCD_SEND_EVENT_TYPE_LIST_EVENT == type)
1744                 ret = vcd_engine_process_list_event(pid, action);
1745         else if (VCD_SEND_EVENT_TYPE_HAPTIC_EVENT == type)
1746                 ret = vcd_engine_process_haptic_event(pid, action);
1747
1748         if (0 != ret) {
1749                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to process do action : %d", ret);
1750         } else {
1751                 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] Process do action");
1752         }
1753
1754         return ret;
1755 }
1756
1757 int vcd_server_mgr_enable_command_type(int pid, int cmd_type)
1758 {
1759         int ret = -1;
1760
1761         /* check if pid is valid */
1762         if (false == vcd_client_manager_is_valid(pid)) {
1763                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1764                 return VCD_ERROR_INVALID_PARAMETER;
1765         }
1766
1767         vcd_state_e state = vcd_config_get_service_state();
1768         if (VCD_STATE_READY != state) {
1769                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1770                 return VCD_ERROR_INVALID_STATE;
1771         }
1772
1773         ret = vcd_config_enable_command_type(cmd_type);
1774         if (0 != ret) {
1775                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to enable command type");
1776         } else {
1777                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Enable command type(%d)", cmd_type);
1778         }
1779
1780         return ret;
1781 }
1782
1783 int vcd_server_mgr_disable_command_type(int pid, int cmd_type)
1784 {
1785         int ret = -1;
1786
1787         /* check if pid is valid */
1788         if (false == vcd_client_manager_is_valid(pid)) {
1789                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1790                 return VCD_ERROR_INVALID_PARAMETER;
1791         }
1792
1793         vcd_state_e state = vcd_config_get_service_state();
1794         if (VCD_STATE_READY != state) {
1795                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1796                 return VCD_ERROR_INVALID_STATE;
1797         }
1798
1799         ret = vcd_config_disable_command_type(cmd_type);
1800         if (0 != ret) {
1801                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to disable command type");
1802         } else {
1803                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Disable command type(%d)", cmd_type);
1804         }
1805
1806         return ret;
1807 }
1808
1809 /*
1810 * VC Server Functions for Client
1811 */
1812 int vcd_server_initialize(int pid)
1813 {
1814         if (false == g_is_engine) {
1815                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1816                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1817                         g_is_engine = false;
1818                         return VCD_ERROR_ENGINE_NOT_FOUND;
1819                 } else {
1820                         g_is_engine = true;
1821                 }
1822         }
1823
1824         if (false == vcd_engine_is_available_engine()) {
1825                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1826                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1827                         return VCD_ERROR_ENGINE_NOT_FOUND;
1828                 }
1829         }
1830
1831         /* check if pid is valid */
1832         if (true == vcd_client_is_available(pid)) {
1833                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1834                 return VCD_ERROR_INVALID_PARAMETER;
1835         }
1836
1837         /* Add client information to client manager */
1838         if (0 != vcd_client_add(pid)) {
1839                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1840                 return VCD_ERROR_OPERATION_FAILED;
1841         }
1842
1843         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1844
1845         return VCD_ERROR_NONE;
1846 }
1847
1848 int vcd_server_finalize(int pid)
1849 {
1850         /* check if pid is valid */
1851         if (false == vcd_client_is_available(pid)) {
1852                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1853                 return VCD_ERROR_INVALID_PARAMETER;
1854         }
1855
1856         /* Remove client information */
1857         if (0 != vcd_client_delete(pid)) {
1858                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1859         }
1860
1861         if (0 == vcd_client_get_ref_count()) {
1862                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1863                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1864         }
1865
1866         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1867
1868         return VCD_ERROR_NONE;
1869 }
1870
1871 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1872 {
1873         /* check if pid is valid */
1874         if (false == vcd_client_is_available(pid)) {
1875                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1876                 return VCD_ERROR_INVALID_PARAMETER;
1877         }
1878
1879         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1880                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1881                 return VCD_ERROR_OPERATION_FAILED;
1882         }
1883
1884         return 0;
1885 }
1886
1887 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1888 {
1889         /* check if pid is valid */
1890         if (false == vcd_client_is_available(pid)) {
1891                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1892                 return VCD_ERROR_INVALID_PARAMETER;
1893         }
1894
1895         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1896                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1897                 return VCD_ERROR_OPERATION_FAILED;
1898         }
1899
1900         return 0;
1901 }
1902
1903 int vcd_server_set_foreground(int pid, bool value)
1904 {
1905         /* check if pid is valid */
1906         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1907                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1908                 return VCD_ERROR_INVALID_PARAMETER;
1909         }
1910
1911         if (0 != vcd_config_set_foreground(pid, value)) {
1912                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1913                 return VCD_ERROR_OPERATION_FAILED;
1914         }
1915
1916         return 0;
1917 }
1918
1919 static int __vcd_server_launch_manager_app()
1920 {
1921         int ret = -1;
1922         bool running = false;
1923         char* appid = NULL;
1924
1925         ret = vcd_client_manager_get_appid(&appid);
1926         if (0 != ret || NULL == appid) {
1927                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get manager appid");
1928                 return VCD_ERROR_OPERATION_FAILED;
1929         }
1930         ret = app_manager_is_running(appid, &running);
1931         if (0 != ret) {
1932                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to check running with appid(%s)", appid);
1933                 free(appid);
1934                 appid = NULL;
1935                 return VCD_ERROR_OPERATION_FAILED;
1936         }
1937         if (false == running) {
1938                 int tmp_ret = __vcd_is_package_installed(appid);
1939                 if (false == tmp_ret) {
1940                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] manager app is not installed, appid(%s)", appid);
1941                 } else {
1942                         ret = __vcd_activate_app_by_appcontrol(appid);
1943                         if (0 != ret) {
1944                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to activate app");
1945                                 free(appid);
1946                                 appid = NULL;
1947                                 return ret;
1948                         }
1949                         SLOG(LOG_ERROR, TAG_VCD, "Launch vc manager app: appid(%s)", appid);
1950                 }
1951         } else {
1952                 ret = __vcd_resume_app(appid);
1953                 if (0 != ret) {
1954                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to resume app");
1955                         free(appid);
1956                         appid = NULL;
1957                         return ret;
1958                 }
1959                 SLOG(LOG_ERROR, TAG_VCD, "Resume vc manager app: appid(%s)", appid);
1960         }
1961
1962         free(appid);
1963         appid = NULL;
1964
1965         return VCD_ERROR_NONE;
1966 }
1967
1968 int vcd_server_dialog(int pid, const char* disp_text, const char* utt_text, int continuous)
1969 {
1970         /* check if pid is valid */
1971         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1972                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1973                 return VCD_ERROR_INVALID_PARAMETER;
1974         }
1975
1976         int ret = __vcd_server_launch_manager_app();
1977         if (0 != ret) {
1978                 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);
1979                 return ret;
1980         }
1981
1982         ret = vcdc_send_dialog(vcd_client_manager_get_pid(), pid, disp_text, utt_text, continuous);
1983         if (0 != ret) {
1984                 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);
1985                 return ret;
1986         }
1987
1988         return 0;
1989 }
1990
1991 int vcd_server_is_system_command_valid(int pid, int* is_sys_cmd_valid)
1992 {
1993         /* check if pid is valid */
1994         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1995                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1996                 return VCD_ERROR_INVALID_PARAMETER;
1997         }
1998
1999         /* check if system command is valid */
2000         if (true == vcd_client_manager_is_system_command_valid(vcd_client_manager_get_pid()))
2001                 *is_sys_cmd_valid = true;
2002         else
2003                 *is_sys_cmd_valid = false;
2004
2005         return 0;
2006 }
2007
2008 #if 0
2009 int vcd_server_set_exclusive_command(int pid, bool value)
2010 {
2011         /* check if pid is valid */
2012         if (false == vcd_client_is_available(pid)) {
2013                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2014                 return VCD_ERROR_INVALID_PARAMETER;
2015         }
2016
2017         if (true == value) {
2018                 if (0 != vcd_client_set_exclusive_command(pid)) {
2019                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
2020                         return VCD_ERROR_OPERATION_FAILED;
2021                 }
2022         } else {
2023                 if (0 != vcd_client_unset_exclusive_command(pid)) {
2024                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
2025                         return VCD_ERROR_OPERATION_FAILED;
2026                 }
2027         }
2028
2029         return 0;
2030 }
2031
2032 int vcd_server_request_start(int pid, bool stop_by_silence)
2033 {
2034         /* check if pid is valid */
2035         if (false == vcd_client_is_available(pid)) {
2036                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
2037                 return VCD_ERROR_INVALID_PARAMETER;
2038         }
2039
2040         int ret;
2041         /* Check current state */
2042         vcd_state_e state = vcd_config_get_service_state();
2043
2044         /* Service state should be ready */
2045         if (VCD_STATE_READY != state) {
2046                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2047                 return VCD_ERROR_INVALID_STATE;
2048         }
2049
2050         if (-1 != vcd_client_manager_get_pid()) {
2051                 /* Check current pid is valid */
2052                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2053                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2054                         return VCD_ERROR_INVALID_PARAMETER;
2055                 }
2056         }
2057
2058         ret = vcd_server_mgr_start(stop_by_silence, false);
2059         if (0 != ret) {
2060                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2061                 return VCD_ERROR_INVALID_PARAMETER;
2062         }
2063
2064         return 0;
2065 }
2066
2067 int vcd_server_request_stop(int pid)
2068 {
2069         int ret;
2070         /* Check current state */
2071         vcd_state_e state = vcd_config_get_service_state();
2072
2073         /* Service state should be ready */
2074         if (VCD_STATE_RECORDING != state) {
2075                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
2076                 return VCD_ERROR_INVALID_STATE;
2077         }
2078
2079         if (-1 != vcd_client_manager_get_pid()) {
2080                 /* Check current pid is valid */
2081                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2082                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2083                         return VCD_ERROR_INVALID_PARAMETER;
2084                 }
2085         }
2086
2087         ret = vcd_server_mgr_stop();
2088         if (0 != ret) {
2089                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2090                 return VCD_ERROR_OPERATION_FAILED;
2091         }
2092
2093         return VCD_ERROR_NONE;
2094 }
2095
2096 int vcd_server_request_cancel(int pid)
2097 {
2098         int ret;
2099         /* Check current state */
2100         vcd_state_e state = vcd_config_get_service_state();
2101
2102         /* Service state should be recording or processing */
2103         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2104                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2105                 return VCD_ERROR_INVALID_STATE;
2106         }
2107
2108         if (-1 != vcd_client_manager_get_pid()) {
2109                 /* Check current pid is valid */
2110                 if (false == vcd_client_manager_check_demandable_client(pid)) {
2111                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
2112                         return VCD_ERROR_INVALID_PARAMETER;
2113                 }
2114         }
2115
2116         ret = vcd_server_mgr_cancel();
2117         if (0 != ret) {
2118                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2119                 return VCD_ERROR_OPERATION_FAILED;
2120         }
2121
2122         return VCD_ERROR_NONE;
2123 }
2124 #endif
2125
2126 /*
2127 * VC Server Functions for Widget lib
2128 */
2129 int vcd_server_widget_initialize(int pid)
2130 {
2131         if (false == g_is_engine) {
2132                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2133                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2134                         g_is_engine = false;
2135                         return VCD_ERROR_ENGINE_NOT_FOUND;
2136                 } else {
2137                         g_is_engine = true;
2138                 }
2139         }
2140
2141         if (false == vcd_engine_is_available_engine()) {
2142                 if (0 != vcd_engine_agent_initialize_current_engine()) {
2143                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
2144                         return VCD_ERROR_ENGINE_NOT_FOUND;
2145                 }
2146         }
2147
2148         /* check if pid is valid */
2149         if (true == vcd_client_widget_is_available(pid)) {
2150                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] The pid is already exist");
2151                 return VCD_ERROR_NONE;
2152         }
2153
2154         /* Add client information to client manager */
2155         if (0 != vcd_client_widget_add(pid)) {
2156                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
2157                 return VCD_ERROR_OPERATION_FAILED;
2158         }
2159
2160         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
2161
2162         return VCD_ERROR_NONE;
2163 }
2164
2165 int vcd_server_widget_finalize(int pid)
2166 {
2167         /* check if pid is valid */
2168         if (false == vcd_client_widget_is_available(pid)) {
2169                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2170                 return VCD_ERROR_INVALID_PARAMETER;
2171         }
2172
2173         /* Remove client information */
2174         if (0 != vcd_client_widget_delete(pid)) {
2175                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
2176         }
2177
2178         if (0 == vcd_client_get_ref_count()) {
2179                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
2180                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
2181         }
2182
2183         return VCD_ERROR_NONE;
2184 }
2185
2186 int vcd_server_widget_start_recording(int pid, bool widget_command)
2187 {
2188         /* check if pid is valid */
2189         if (false == vcd_client_widget_is_available(pid)) {
2190                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
2191                 return VCD_ERROR_INVALID_PARAMETER;
2192         }
2193
2194         if (true == widget_command) {
2195                 vcd_client_widget_set_command(pid);
2196                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
2197         } else {
2198                 vcd_client_widget_unset_command(pid);
2199                 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
2200         }
2201
2202         int ret = __start_internal_recognition();
2203         if (0 != ret) {
2204                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
2205                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
2206         }
2207
2208         return 0;
2209 }
2210
2211 int vcd_server_widget_start(int pid, bool stop_by_silence)
2212 {
2213         /* check if pid is valid */
2214         int fore_pid = vcd_client_widget_get_foreground_pid();
2215         if (pid != fore_pid) {
2216                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2217                 return VCD_ERROR_INVALID_PARAMETER;
2218         }
2219
2220         /* Check current state */
2221         vcd_state_e state = vcd_config_get_service_state();
2222
2223         /* Service state should be ready */
2224         if (VCD_STATE_READY != state) {
2225                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
2226                 return VCD_ERROR_INVALID_STATE;
2227         }
2228
2229         vcd_client_set_slience_detection(stop_by_silence);
2230
2231         /* Notify show tooltip */
2232         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
2233
2234         return 0;
2235 }
2236
2237 int vcd_server_widget_stop(int pid)
2238 {
2239         /* check if pid is valid */
2240         int fore_pid = vcd_client_widget_get_foreground_pid();
2241         if (pid != fore_pid) {
2242                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2243                 return VCD_ERROR_INVALID_PARAMETER;
2244         }
2245
2246         int ret;
2247         /* Check current state */
2248         vcd_state_e state = vcd_config_get_service_state();
2249
2250         /* Service state should be recording */
2251         if (VCD_STATE_RECORDING != state) {
2252                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
2253                 return VCD_ERROR_INVALID_STATE;
2254         }
2255
2256         ret = vcd_server_mgr_stop();
2257         if (0 != ret) {
2258                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
2259                 return VCD_ERROR_OPERATION_FAILED;
2260         }
2261
2262         return VCD_ERROR_NONE;
2263 }
2264
2265 int vcd_server_widget_cancel(int pid)
2266 {
2267         /* check if pid is valid */
2268         int fore_pid = vcd_client_widget_get_foreground_pid();
2269         if (pid != fore_pid) {
2270                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
2271                 return VCD_ERROR_INVALID_PARAMETER;
2272         }
2273
2274         int ret;
2275         /* Check current state */
2276         vcd_state_e state = vcd_config_get_service_state();
2277
2278         /* Service state should be recording or processing */
2279         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
2280                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
2281                 return VCD_ERROR_INVALID_STATE;
2282         }
2283
2284         ret = vcd_server_mgr_cancel();
2285         if (0 != ret) {
2286                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
2287                 return ret;
2288         }
2289
2290         return VCD_ERROR_NONE;
2291 }
2292
2293 int vcd_server_widget_enable_asr_result(int pid, bool enable)
2294 {
2295         int ret;
2296         ret = vcd_client_widget_set_asr_result_enabled(pid, enable);
2297         if (0 != ret) {
2298                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to enable asr result : %d", ret);
2299         }
2300
2301         return ret;
2302 }