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