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