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