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