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