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