Remove unnecessary codes
[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 <dirent.h>
18 #include <sound_manager.h>
19
20 #include "vc_info_parser.h"
21 #include "vcd_main.h"
22 #include "vcd_server.h"
23 #include "vcd_client_data.h"
24
25 #include "vcd_engine_agent.h"
26 #include "vcd_config.h"
27 #include "vcd_recorder.h"
28 #include "vcd_dbus.h"
29
30 #include "voice_control_command_expand.h"
31
32 /*
33 * VC Server static variable
34 */
35 static bool     g_is_engine;
36
37 static GList *g_proc_list = NULL;
38
39 static Ecore_Timer *g_restart_timer = NULL;
40
41 /*
42 * VC Server Internal Functions
43 */
44 static Eina_Bool __stop_by_silence(void *data)
45 {
46         SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
47
48         vcd_server_mgr_stop();
49
50         SLOG(LOG_DEBUG, TAG_VCD, "=====");
51         SLOG(LOG_DEBUG, TAG_VCD, "  ");
52         return EINA_FALSE;
53 }
54
55 static Eina_Bool __cancel_by_interrupt(void *data)
56 {
57         SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
58
59         vcd_server_mgr_cancel();
60
61         SLOG(LOG_DEBUG, TAG_VCD, "=====");
62         SLOG(LOG_DEBUG, TAG_VCD, "  ");
63         return EINA_FALSE;
64 }
65
66 static Eina_Bool __restart_engine(void *data)
67 {
68         SLOG(LOG_DEBUG, TAG_VCD, "===== Restart by no result");
69
70         g_restart_timer = NULL;
71
72         /* Restart recognition */
73         int ret = vcd_engine_recognize_start(true);
74         if (0 != ret) {
75                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
76                 return EINA_FALSE;
77         }
78
79         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
80
81         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
82                 vcd_config_set_service_state(VCD_STATE_RECORDING);
83                 vcdc_send_service_state(VCD_STATE_RECORDING);
84         }
85
86         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
87
88         SLOG(LOG_DEBUG, TAG_VCD, "=====");
89         SLOG(LOG_DEBUG, TAG_VCD, "  ");
90         return EINA_FALSE;
91 }
92
93 static int __server_recorder_callback(const void* data, const unsigned int length)
94 {
95         vcd_state_e state = vcd_config_get_service_state();
96         if (VCD_STATE_RECORDING != state) {
97                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Not recording state, but recording");
98 //              return 0;
99         }
100
101         vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
102         int ret;
103
104         ret = vcd_engine_recognize_audio(data, length, &speech_detected);
105
106         if (0 > ret) {
107                 /* Error */
108                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
109                 ecore_timer_add(0, __cancel_by_interrupt, NULL);
110                 /* Send error cb to manager */
111                 if (VCP_ERROR_OUT_OF_NETWORK == ret) {
112                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_TIMED_OUT, "Engine connection failed");
113                 } else {
114                         vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "Engine recognition failed");
115                 }
116                 return 0;
117         }
118
119         if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
120                 if (-1 != vcd_client_manager_get_pid()) {
121                         /* Manager client is available */
122                         if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
123                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
124                         }
125                 }
126         } else if (VCP_SPEECH_DETECT_END == speech_detected) {
127                 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
128                         /* silence detected */
129                         ecore_timer_add(0, __stop_by_silence, NULL);
130                 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
131                         /* Stop engine recognition */
132                         int ret = vcd_engine_recognize_stop();
133                         if (0 != ret) {
134                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
135                         }
136                         vcd_config_set_service_state(VCD_STATE_PROCESSING);
137                         vcdc_send_service_state(VCD_STATE_PROCESSING);
138
139                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
140                 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
141                         /* Stop engine recognition */
142                         int ret = vcd_engine_recognize_stop();
143                         if (0 != ret) {
144                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
145                         }
146                 }
147         }
148
149         return 0;
150 }
151
152 void __server_recorder_interrupt_callback()
153 {
154         SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
155
156         ecore_timer_add(0, __cancel_by_interrupt, NULL);
157
158         SLOG(LOG_DEBUG, TAG_VCD, "=====");
159         SLOG(LOG_DEBUG, TAG_VCD, "  ");
160 }
161
162 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
163 {
164         SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
165
166         /* Current state is recording */
167         vcd_state_e state = vcd_config_get_service_state();
168         if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
169                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
170                 vcd_server_mgr_cancel();
171         }
172
173         int ret;
174         ret = vcd_engine_set_current_language(current_lang);
175         if (0 != ret) {
176                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
177         }
178
179         SLOG(LOG_DEBUG, TAG_VCD, "=====");
180         SLOG(LOG_DEBUG, TAG_VCD, "  ");
181
182         return;
183 }
184
185 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
186 {
187         SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
188
189         SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
190
191         if (VC_NO_FOREGROUND_PID != current) {
192                 /* Foreground app is changed */
193                 vcd_state_e state = vcd_config_get_service_state();
194                 if (VCD_STATE_RECORDING == state) {
195                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
196                         ecore_timer_add(0, __cancel_by_interrupt, NULL);
197                 }
198         }
199
200         SLOG(LOG_DEBUG, TAG_VCD, "=====");
201         SLOG(LOG_DEBUG, TAG_VCD, "  ");
202
203         return;
204 }
205
206 static Eina_Bool __vcd_send_selected_result(void *data)
207 {
208         GSList* pid_list = NULL;
209         if (0 != vc_info_parser_get_result_pid_list(&pid_list)) {
210                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
211         } else {
212                 if (0 < g_slist_length(pid_list)) {
213                         GSList* iter = NULL;
214                         vc_cmd_s* temp_cmd = NULL;
215                         int ret = 0;
216                         int count = 0;
217
218                         iter = g_slist_nth(pid_list, 0);
219                         while (NULL != iter) {
220                                 temp_cmd = iter->data;
221
222                                 if (NULL != temp_cmd) {
223                                         count = 0;
224                                         do {
225                                                 /* send result noti */
226                                                 ret = vcdc_send_result(temp_cmd->pid, temp_cmd->type);
227                                                 if (0 != ret) {
228                                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
229                                                         if (VCD_ERROR_TIMED_OUT != ret) {
230                                                                 break;
231                                                         }
232                                                 } else {
233                                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
234                                                 }
235                                                 count++;
236
237                                                 if (100 == count)       break;
238                                                 /* While is retry code */
239                                         } while (0 != ret);
240                                         free(temp_cmd);
241                                 }
242
243                                 pid_list = g_slist_remove_link(pid_list, iter);
244                                 iter = g_slist_nth(pid_list, 0);
245                         }
246                 }
247         }
248
249         if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
250                 vcd_config_set_service_state(VCD_STATE_READY);
251                 vcdc_send_service_state(VCD_STATE_READY);
252         }
253
254         return EINA_FALSE;
255 }
256
257 static int __convert_type_to_priority(vc_cmd_type_e type)
258 {
259         switch (type) {
260                 case VC_COMMAND_TYPE_NONE:              return 0;
261                 case VC_COMMAND_TYPE_BACKGROUND:        return 1;
262                 case VC_COMMAND_TYPE_FOREGROUND:        return 2;
263                 case VC_COMMAND_TYPE_WIDGET:            return 2;
264                 case VC_COMMAND_TYPE_SYSTEM:            return 3;
265                 case VC_COMMAND_TYPE_EXCLUSIVE:         return 3;
266                 default:                                return 0;
267         }
268 }
269
270 static void __vcd_server_pre_result_cb(vcp_pre_result_event_e event, const char* pre_result, void* user_data)
271 {
272         if (NULL != pre_result) {
273                 vcdc_send_pre_result_to_manager(vcd_client_manager_get_pid(), event, pre_result);
274         }
275 }
276
277 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
278                                    const char* non_fixed_result, const char* msg, const char* nlp_info, void *user_data)
279 {
280         if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
281                 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
282                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
283                         return;
284                 }
285         }
286
287         // temp 
288         if (NULL != nlp_info) {
289                 if (0 != vc_info_parser_set_nlp_info(nlp_info)) {
290                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save nlp info");
291                 }
292         }
293
294         vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
295
296         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)", 
297                 event, all_result, non_fixed_result, msg, count);
298
299         if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
300                 if (VCP_RESULT_EVENT_REJECTED == event) {
301                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
302                         /* If no result and restart option is ON */
303                         /* Send reject message */
304                         bool temp = vcd_client_manager_get_exclusive();
305                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
306                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
307                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
308                         }
309
310                         g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
311                         return;
312                 }
313                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
314                 vcd_recorder_stop();
315         } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
316                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
317                 /* Restart option is ON */
318                 g_restart_timer = ecore_timer_add(0, __restart_engine, NULL);
319                 if (VCP_RESULT_EVENT_REJECTED == event) {
320                         bool temp = vcd_client_manager_get_exclusive();
321                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
322                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
323                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
324                         }
325                         return;
326                 }
327         }
328
329         /* No result */
330         if (NULL == result_id) {
331                 /* No result */
332                 if (NULL != all_result) {
333                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
334                         bool temp = vcd_client_manager_get_exclusive();
335                         vc_info_parser_set_result(all_result, event, msg, NULL, temp);
336                 }
337
338                 int pid = vcd_client_widget_get_foreground_pid();
339                 if (-1 != pid) {
340                         if (NULL != all_result) {
341                                 /* Send result text to widget */
342                                 vcdc_send_result(pid, VC_COMMAND_TYPE_WIDGET);
343                         }
344
345                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
346                         /* Send to hide tooltip */
347                         vcdc_send_show_tooltip(pid, false);
348                 }
349
350                 if (-1 != vcd_client_manager_get_pid()) {
351                         /* Manager client is available */
352                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
353                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
354                         }
355                 }
356
357                 vcd_client_manager_set_exclusive(false);
358
359                 return;
360         }
361
362         /* Normal result */
363         SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
364
365         int ret = -1;
366         vc_cmd_s* temp_cmd = NULL;
367         vc_cmd_list_h vc_cmd_list = NULL;
368
369         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
370                 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
371                 vcd_client_manager_set_exclusive(false);
372                 vcd_config_set_service_state(VCD_STATE_READY);
373                 vcdc_send_service_state(VCD_STATE_READY);
374                 return;
375         }
376
377         int i = 0;
378         int priority = 0;
379         for (i = 0; i < count; i++) {
380                 SLOG(LOG_DEBUG, TAG_VCD, "[Server]   [%d] Result ID(%d)", i, result_id[i]);
381
382                 if (result_id[i] < 0) {
383                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
384                         continue;
385                 }
386
387                 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
388                 if (0 == ret && NULL != temp_cmd) {
389                         /* Add priority filter */
390                         int temp_priority = __convert_type_to_priority(temp_cmd->type);
391                         if (priority > temp_priority) {
392                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ignore result by priority");
393                                 continue;
394                         } else if (priority < temp_priority) {
395                                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] High priority result!!");
396                                 priority = temp_priority;
397
398                                 if (0 != vc_cmd_list_remove_all(vc_cmd_list, true)) {
399                                         SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to list remove all");
400                                 }
401                         }
402
403                         switch (temp_cmd->format) {
404                         case VC_CMD_FORMAT_FIXED:
405                                 /* Nonfixed result is NOT valid */
406                                 break;
407                         case VC_CMD_FORMAT_FIXED_AND_EXTRA:
408                                 if (NULL == temp_cmd->parameter) {
409                                         if (NULL != non_fixed_result) {
410                                                 temp_cmd->parameter = strdup(non_fixed_result);
411                                         }
412                                 } else {
413                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
414                                 }
415                                 break;
416                         case VC_CMD_FORMAT_EXTRA_AND_FIXED:
417                                 if (NULL == temp_cmd->command) {
418                                         if (NULL != non_fixed_result) {
419                                                 temp_cmd->command = strdup(non_fixed_result);
420                                         }
421                                 } else {
422                                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
423                                 }
424
425                                 break;
426                         default:
427                                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
428                         }
429
430                         temp_cmd->id = i;
431                         if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
432                                 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
433                                 vc_cmd_destroy((vc_cmd_h)temp_cmd);
434                         }
435                 } else {
436                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
437                 }
438         }
439
440         vc_cmd_print_list(vc_cmd_list);
441
442         SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
443
444         int result_count = 0;
445         vc_cmd_list_get_count(vc_cmd_list, &result_count);
446
447         if (false == vcd_client_manager_get_exclusive()) {
448                 int pid = vcd_client_widget_get_foreground_pid();
449                 if (-1 != pid) {
450                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
451                         vcdc_send_show_tooltip(pid, false);
452                 }
453
454                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
455
456                 if (-1 != vcd_client_manager_get_pid()) {
457                         /* Manager client is available */
458                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
459                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
460                         }
461                 } else {
462                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
463                         /* Send result to client */
464                         ecore_timer_add(0, __vcd_send_selected_result, NULL);
465                 }
466         } else {
467                 /* exclusive command */
468                 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
469
470                 if (-1 != vcd_client_manager_get_pid()) {
471                         /* Manager client is available */
472                         if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
473                                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
474                         }
475                 } else {
476                         SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
477                 }
478
479                 vcd_client_manager_set_exclusive(false);
480         }
481
482         vc_cmd_list_destroy(vc_cmd_list, true);
483
484         return;
485 }
486
487 /*
488 * vcd server Interfaces
489 */
490 static void __vcd_file_clean_up()
491 {
492         SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
493
494         DIR *dp = NULL;
495         int ret = -1;
496         struct dirent entry;
497         struct dirent *dirp = NULL;
498
499         dp = opendir(VC_RUNTIME_INFO_ROOT);
500         if (dp == NULL) {
501                 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
502                 return;
503         }
504
505         char remove_path[256] = {0, };
506         do {
507                 ret = readdir_r(dp, &entry, &dirp);
508                 if (0 != ret) {
509                         SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
510                         break;
511                 }
512
513                 if (NULL != dirp) {
514                         if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
515                                 memset(remove_path, 0, 256);
516                                 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
517
518                                 /* Clean up code */
519                                 if (0 != remove(remove_path)) {
520                                         SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
521                                 } else {
522                                         SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
523                                 }
524                         }
525                 }
526         } while (NULL != dirp);
527
528         closedir(dp);
529
530         return;
531 }
532
533 int vcd_initialize()
534 {
535         int ret = 0;
536
537         /* Remove old file */
538         __vcd_file_clean_up();
539         /* Send error signal for notifying that daemon is reset*/
540         vcdc_send_error_signal(VCD_ERROR_SERVICE_RESET, "Daemon reset");
541
542         /* initialize modules */
543         ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
544         if (0 != ret) {
545                 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
546         }
547
548         vcd_config_set_service_state(VCD_STATE_NONE);
549
550         ret = vcd_engine_agent_init(__vcd_server_pre_result_cb, __vcd_server_result_cb);
551         if (0 != ret) {
552                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
553                 return ret;
554         }
555
556         if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
557                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
558                 return VCD_ERROR_OPERATION_FAILED;
559         }
560
561         /* Find engine */
562         ret = vcd_engine_agent_initialize_current_engine();
563         if (0 != ret) {
564                 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
565                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
566                 else
567                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
568
569                 g_is_engine = false;
570         } else {
571                 g_is_engine = true;
572         }
573
574         /* Load engine */
575         if (0 != vcd_engine_agent_load_current_engine()) {
576                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
577                 return VCD_ERROR_OPERATION_FAILED;
578         }
579
580         /* Initialize manager info */
581         vcd_client_manager_unset();
582
583         vcd_config_set_service_state(VCD_STATE_READY);
584         vcdc_send_service_state(VCD_STATE_READY);
585
586         SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
587
588         return 0;
589 }
590
591 void vcd_finalize()
592 {
593         GList *iter = NULL;
594         if (0 < g_list_length(g_proc_list)) {
595                 iter = g_list_first(g_proc_list);
596                 while (NULL != iter) {
597                         g_proc_list = g_list_remove_link(g_proc_list, iter);
598                         iter = g_list_first(g_proc_list);
599                 }
600         }
601
602         if (g_restart_timer != NULL) {
603                 ecore_timer_del(g_restart_timer);
604                 g_restart_timer = NULL;
605         }
606
607         vcd_state_e state = vcd_config_get_service_state();
608         if (VCD_STATE_READY != state) {
609                 if (VCD_STATE_RECORDING == state) {
610                         vcd_recorder_stop();
611                 }
612                 vcd_engine_recognize_cancel();
613         }
614         if (0 != vcd_recorder_destroy()) {
615                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
616         } else {
617                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
618         }
619
620         if (0 != vcd_engine_agent_release()) {
621                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
622         } else {
623                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
624         }
625
626         vcd_config_set_service_state(VCD_STATE_NONE);
627         vcdc_send_service_state(VCD_STATE_NONE);
628
629         SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
630
631         return;
632 }
633
634 static Eina_Bool __finalize_quit_ecore_loop(void *data)
635 {
636         SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
637         ecore_main_loop_quit();
638         return EINA_FALSE;
639 }
640
641 static void __read_proc()
642 {
643         DIR *dp = NULL;
644         struct dirent entry;
645         struct dirent *dirp = NULL;
646         int ret = -1;
647         int tmp;
648
649         GList *iter = NULL;
650         if (0 < g_list_length(g_proc_list)) {
651                 iter = g_list_first(g_proc_list);
652                 while (NULL != iter) {
653                         g_proc_list = g_list_remove_link(g_proc_list, iter);
654                         iter = g_list_first(g_proc_list);
655                 }
656         }
657
658         dp = opendir("/proc");
659         if (NULL == dp) {
660                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
661         } else {
662                 do {
663                         ret = readdir_r(dp, &entry, &dirp);
664                         if (0 != ret) {
665                                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to readdir");
666                                 break;
667                         }
668
669                         if (NULL != dirp) {
670                                 tmp = atoi(dirp->d_name);
671                                 if (0 >= tmp)   continue;
672                                 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
673                         }
674                 } while (NULL != dirp);
675                 closedir(dp);
676         }
677         return;
678 }
679
680 static void __vcd_cleanup_client(vcd_client_type_e type)
681 {
682         int* client_list = NULL;
683         int client_count = 0;
684         int i = 0;
685         int j = 0;
686         bool exist = false;
687         int mgr_pid = -1;
688         int ret = -1;
689
690         if (VCD_CLIENT_TYPE_NORMAL == type) {
691                 ret = vcd_client_get_list(&client_list, &client_count);
692         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
693                 ret = vcd_client_widget_get_list(&client_list, &client_count);
694         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
695                 mgr_pid = vcd_client_manager_get_pid();
696                 client_list = &mgr_pid;
697                 client_count = 1;
698                 if (-1 == mgr_pid) {
699                         SLOG(LOG_WARN, TAG_VCD, "[WARNING] Invalid Manager pid");
700                         return;
701                 }
702         }
703
704         if (0 == ret || mgr_pid > 0) {
705                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
706                 if (NULL != client_list && client_count > 0) {
707                         for (i = 0; i < client_count; i++) {
708                                 exist = false;
709                                 GList *iter = NULL;
710                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
711                                         iter = g_list_nth(g_proc_list, j);
712                                         if (NULL != iter) {
713                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
714                                                         SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
715                                                         exist = true;
716                                                         break;
717                                                 }
718                                         }
719                                 }
720
721                                 if (false == exist) {
722                                         SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
723                                         if (VCD_CLIENT_TYPE_NORMAL == type)
724                                                 vcd_server_finalize(client_list[i]);
725                                         else if (VCD_CLIENT_TYPE_WIDGET == type)
726                                                 vcd_server_widget_finalize(client_list[i]);
727                                         else
728                                                 vcd_server_mgr_finalize(mgr_pid);
729                                 }
730                         }
731                 }
732                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
733                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
734         }
735         if (NULL != client_list && -1 == mgr_pid) {
736                 free(client_list);
737                 client_list = NULL;
738         }
739         return;
740 }
741
742 Eina_Bool vcd_cleanup_client_all(void *data)
743 {
744         __read_proc();
745         
746         __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
747         __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
748         __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
749
750 #if 0
751         if (0 == vcd_client_get_list(&client_list, &client_count)) {
752                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
753                 if (NULL != client_list && client_count > 0) {
754                         for (i = 0; i < client_count; i++) {
755                                 exist = false;
756                                 GList *iter = NULL;
757                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
758                                         iter = g_list_nth(g_proc_list, j);
759                                         if (NULL != iter) {
760                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
761                                                         SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
762                                                         exist = true;
763                                                         break;
764                                                 }
765                                         }
766                                 }
767
768                                 if (false == exist) {
769                                         SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
770                                         vcd_server_finalize(client_list[i]);
771                                 }
772 #if 0
773                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
774
775                                 if (0 == result) {
776                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
777                                         vcd_server_finalize(client_list[i]);
778                                 } else if (-1 == result) {
779                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
780                                 }
781 #endif
782                         }
783                 }
784                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
785                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
786         }
787         if (NULL != client_list) {
788                 free(client_list);
789                 client_list = NULL;
790         }
791
792         /* If app is in background state, app cannot response message. */
793         if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
794                 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
795                 if (NULL != client_list && client_count > 0) {
796                         for (i = 0; i < client_count; i++) {
797                                 exist = false;
798                                 GList *iter = NULL;
799                                 for (j = 0; j < g_list_length(g_proc_list); j++) {
800                                         iter = g_list_nth(g_proc_list, j);
801                                         if (NULL != iter) {
802                                                 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
803                                                         SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
804                                                         exist = true;
805                                                         break;
806                                                 }
807                                         }
808                                 }
809
810                                 if (false == exist) {
811                                         SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
812                                         vcd_server_widget_finalize(client_list[i]);
813                                 }
814 #if 0
815                                 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
816
817                                 if (0 == result) {
818                                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
819                                         vcd_server_widget_finalize(client_list[i]);
820                                 } else if (-1 == result) {
821                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
822                                 }
823 #endif
824                         }
825                 }
826                 SLOG(LOG_DEBUG, TAG_VCD, "=====");
827                 SLOG(LOG_DEBUG, TAG_VCD, "  ");
828         }
829
830         if (NULL != client_list) {
831                 free(client_list);
832                 client_list = NULL;
833         }
834
835         /* manager */
836         exist = false;
837         GList *iter = NULL;
838         int mgr_pid = vcd_client_manager_get_pid();
839         if (0 < mgr_pid) {
840                 for (j = 0; j < g_list_length(g_proc_list); j++) {
841                         iter = g_list_nth(g_proc_list, j);
842                         if (NULL != iter) {
843                                 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
844                                         SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
845                                         exist = true;
846                                         break;
847                                 }
848                         }
849                 }
850
851                 if (false == exist) {
852                         SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
853                         vcd_server_mgr_finalize(mgr_pid);
854                 }
855         }
856 #endif
857         return EINA_TRUE;
858 }
859
860 int vcd_server_get_service_state()
861 {
862         return vcd_config_get_service_state();
863 }
864
865 int vcd_server_get_foreground()
866 {
867         int pid;
868         vcd_config_get_foreground(&pid);
869         return pid;
870 }
871
872
873 /*
874 * API for manager
875 */
876 int vcd_server_mgr_initialize(int pid)
877 {
878         if (false == g_is_engine) {
879                 if (0 != vcd_engine_agent_initialize_current_engine()) {
880                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
881                         g_is_engine = false;
882                         return VCD_ERROR_ENGINE_NOT_FOUND;
883                 } else {
884                         g_is_engine = true;
885                 }
886         }
887
888         /* check if pid is valid */
889         if (false == vcd_client_manager_is_valid(pid)) {
890                 SLOG(LOG_ERROR, TAG_VCD, "[Server] old manager pid(%d) be removed", vcd_client_manager_get_pid());
891                 vcd_server_mgr_cancel();
892                 vcd_client_manager_unset();
893         }
894
895         /* Add client information to client manager */
896         if (0 != vcd_client_manager_set(pid)) {
897                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
898                 return VCD_ERROR_OPERATION_FAILED;
899         }
900
901         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
902
903         return VCD_ERROR_NONE;
904 }
905
906 int vcd_server_mgr_finalize(int pid)
907 {
908         /* check if pid is valid */
909         if (false == vcd_client_manager_is_valid(pid)) {
910                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
911                 return VCD_ERROR_INVALID_PARAMETER;
912         }
913
914         /* Cancel recognition */
915         vcd_server_mgr_cancel();
916
917         /* Remove manager information */
918         if (0 != vcd_client_manager_unset()) {
919                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
920         }
921
922         if (0 == vcd_client_get_ref_count()) {
923                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
924                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
925         }
926
927         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
928
929         return VCD_ERROR_NONE;
930 }
931
932 int vcd_server_mgr_set_command(int pid)
933 {
934         if (0 != vcd_client_manager_set_command(pid)) {
935                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
936                 return VCD_ERROR_INVALID_PARAMETER;
937         }
938         return VCD_ERROR_NONE;
939 }
940
941 int vcd_server_mgr_unset_command(int pid)
942 {
943         if (0 != vcd_client_manager_unset_command(pid)) {
944                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
945                 return VCD_ERROR_INVALID_PARAMETER;
946         }
947         return VCD_ERROR_NONE;
948 }
949
950 int vcd_server_mgr_set_demandable_client(int pid)
951 {
952         /* check if pid is valid */
953         if (false == vcd_client_manager_is_valid(pid)) {
954                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
955                 return VCD_ERROR_INVALID_PARAMETER;
956         }
957
958         GSList* client_list = NULL;
959         if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
960                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
961                 return VCD_ERROR_OPERATION_FAILED;
962         }
963
964         /* Save client list */
965         if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
966                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
967                 return VCD_ERROR_OPERATION_FAILED;
968         }
969
970         return VCD_ERROR_NONE;
971 }
972
973 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
974 {
975         /* check if pid is valid */
976         if (false == vcd_client_manager_is_valid(pid)) {
977                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
978                 return VCD_ERROR_INVALID_PARAMETER;
979         }
980
981         int ret = 0;
982         vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
983         int rate = 16000;
984         int channel = 1;
985
986         ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
987         if (0 != ret) {
988                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
989                 return VCD_ERROR_OPERATION_FAILED;
990         }
991
992         ret = vcd_recorder_set(audio_type, type, rate, channel);
993         if (0 != ret) {
994                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
995                 return VCD_ERROR_OPERATION_FAILED;
996         }
997
998         return VCD_ERROR_NONE;
999 }
1000
1001 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
1002 {
1003         /* check if pid is valid */
1004         if (false == vcd_client_manager_is_valid(pid)) {
1005                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1006                 return VCD_ERROR_INVALID_PARAMETER;
1007         }
1008
1009         int ret = vcd_recorder_get(audio_type);
1010         if (0 != ret) {
1011                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
1012                 return VCD_ERROR_OPERATION_FAILED;
1013         }
1014
1015         return VCD_ERROR_NONE;
1016 }
1017
1018 int vcd_server_mgr_set_client_info(int pid)
1019 {
1020         /* check if pid is valid */
1021         if (false == vcd_client_manager_is_valid(pid)) {
1022                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
1023                 return VCD_ERROR_INVALID_PARAMETER;
1024         }
1025
1026         int ret = vcd_client_save_client_info();
1027         if (0 != ret) {
1028                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
1029                 return VCD_ERROR_OPERATION_FAILED;
1030         }
1031
1032         return VCD_ERROR_NONE;
1033 }
1034
1035 static int __start_internal_recognition()
1036 {
1037         int ret;
1038
1039         /* 2. Get commands */
1040         ret = vcd_client_command_collect_command();
1041         if (0 != ret) {
1042                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1043                 return VCD_ERROR_OPERATION_FAILED;
1044         }
1045
1046         ret = vcd_client_get_length();
1047         if (0 == ret) {
1048                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1049                 return VCD_ERROR_OPERATION_FAILED;
1050         }
1051
1052         /* 3. Set command to engine */
1053         ret = vcd_engine_set_commands();
1054         if (0 != ret) {
1055                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1056                 return VCD_ERROR_OPERATION_FAILED;
1057         }
1058
1059         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1060
1061         bool stop_by_silence = true;
1062         if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1063                 stop_by_silence = false;
1064         }
1065
1066         /* 4. start recognition */
1067         ret = vcd_engine_recognize_start(stop_by_silence);
1068         if (0 != ret) {
1069                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1070                 return VCD_ERROR_OPERATION_FAILED;
1071         }
1072
1073         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1074
1075         /* 5. recorder start */
1076         ret = vcd_recorder_start();
1077         if (0 != ret) {
1078                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1079                 vcd_engine_recognize_cancel();
1080                 return ret;
1081         }
1082
1083         vcd_config_set_service_state(VCD_STATE_RECORDING);
1084         vcdc_send_service_state(VCD_STATE_RECORDING);
1085
1086         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1087
1088         return 0;
1089 }
1090
1091 static Eina_Bool __vcd_request_show_tooltip(void *data)
1092 {
1093         int pid = vcd_client_widget_get_foreground_pid();
1094         if (-1 != pid) {
1095                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1096                 vcdc_send_show_tooltip(pid, (bool)data);
1097         }
1098
1099         return EINA_FALSE;
1100 }
1101
1102 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1103 {
1104         /* 1. check current state */
1105         vcd_state_e state = vcd_config_get_service_state();
1106
1107         if (VCD_STATE_READY != state) {
1108                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1109                 return VCD_ERROR_INVALID_STATE;
1110         }
1111
1112         SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1113         vcd_client_set_recognition_mode(recognition_mode);
1114
1115         if (false == exclusive_cmd) {
1116                 /* Notify show tooltip */
1117                 int pid = vcd_client_widget_get_foreground_pid();
1118                 if (-1 != pid) {
1119                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1120                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1121                         return 0;
1122                 }
1123         } else {
1124                 vcd_client_manager_set_exclusive(exclusive_cmd);
1125         }
1126
1127         int fg_pid = -1;
1128         if (true == start_by_client) {
1129                 /* Get foreground pid */
1130                 if (0 != vcd_config_get_foreground(&fg_pid)) {
1131                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1132                 }
1133
1134                 /* Set client exclusive option */
1135                 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1136                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1137                 }
1138         }
1139
1140         int ret = __start_internal_recognition();
1141         if (0 != ret) {
1142                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1143                 return ret;
1144         }
1145
1146         if (true == start_by_client) {
1147                 vcd_client_unset_exclusive_command(fg_pid);
1148         }
1149
1150         return VCD_ERROR_NONE;
1151 }
1152
1153 int vcd_server_mgr_stop()
1154 {
1155         /* 1. Check current state is recording */
1156         if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1157                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1158                 return VCD_ERROR_INVALID_STATE;
1159         }
1160
1161         /* 2. Stop recorder */
1162         vcd_recorder_stop();
1163
1164         /* 3. Stop engine recognition */
1165         int ret = vcd_engine_recognize_stop();
1166         if (0 != ret) {
1167                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1168         }
1169
1170         /* 4. Set original mode */
1171         vcd_config_set_service_state(VCD_STATE_PROCESSING);
1172         vcdc_send_service_state(VCD_STATE_PROCESSING);
1173
1174         return VCD_ERROR_NONE;
1175 }
1176
1177 int vcd_server_mgr_cancel()
1178 {
1179         /* 1. Check current state */
1180         vcd_state_e state = vcd_config_get_service_state();
1181         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1182                 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1183                 return VCD_ERROR_INVALID_STATE;
1184         }
1185
1186         if (g_restart_timer != NULL) {
1187                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Delete restart engine timer");
1188                 ecore_timer_del(g_restart_timer);
1189                 g_restart_timer = NULL;
1190         }
1191
1192         /* 2. Stop recorder */
1193         vcd_recorder_stop();
1194         /* 3. Cancel engine */
1195         int ret = vcd_engine_recognize_cancel();
1196         if (0 != ret) {
1197                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1198         }
1199
1200         if (false == vcd_client_manager_get_exclusive()) {
1201                 int pid = vcd_client_widget_get_foreground_pid();
1202                 if (-1 != pid) {
1203                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1204                         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1205                 }
1206         } else {
1207                 vcd_client_manager_set_exclusive(false);
1208         }
1209
1210         /* 4. Set state */
1211         vcd_config_set_service_state(VCD_STATE_READY);
1212         vcdc_send_service_state(VCD_STATE_READY);
1213
1214         return VCD_ERROR_NONE;
1215 }
1216
1217
1218 int vcd_server_mgr_result_select()
1219 {
1220         __vcd_send_selected_result(NULL);
1221
1222         return VCD_ERROR_NONE;
1223 }
1224
1225 /*
1226 * VC Server Functions for Client
1227 */
1228 int vcd_server_initialize(int pid)
1229 {
1230         if (false == g_is_engine) {
1231                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1232                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1233                         g_is_engine = false;
1234                         return VCD_ERROR_ENGINE_NOT_FOUND;
1235                 } else {
1236                         g_is_engine = true;
1237                 }
1238         }
1239
1240         if (false == vcd_engine_is_available_engine()) {
1241                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1242                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1243                         return VCD_ERROR_ENGINE_NOT_FOUND;
1244                 }
1245         }
1246
1247         /* check if pid is valid */
1248         if (true == vcd_client_is_available(pid)) {
1249                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1250                 return VCD_ERROR_INVALID_PARAMETER;
1251         }
1252
1253         /* Add client information to client manager */
1254         if (0 != vcd_client_add(pid)) {
1255                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1256                 return VCD_ERROR_OPERATION_FAILED;
1257         }
1258
1259         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1260
1261         return VCD_ERROR_NONE;
1262 }
1263
1264 int vcd_server_finalize(int pid)
1265 {
1266         /* check if pid is valid */
1267         if (false == vcd_client_is_available(pid)) {
1268                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1269                 return VCD_ERROR_INVALID_PARAMETER;
1270         }
1271
1272         /* Remove client information */
1273         if (0 != vcd_client_delete(pid)) {
1274                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1275         }
1276
1277         if (0 == vcd_client_get_ref_count()) {
1278                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1279                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1280         }
1281
1282         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1283
1284         return VCD_ERROR_NONE;
1285 }
1286
1287 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1288 {
1289         /* check if pid is valid */
1290         if (false == vcd_client_is_available(pid)) {
1291                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1292                 return VCD_ERROR_INVALID_PARAMETER;
1293         }
1294
1295         if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1296                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1297                 return VCD_ERROR_OPERATION_FAILED;
1298         }
1299
1300         return 0;
1301 }
1302
1303 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1304 {
1305         /* check if pid is valid */
1306         if (false == vcd_client_is_available(pid)) {
1307                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1308                 return VCD_ERROR_INVALID_PARAMETER;
1309         }
1310
1311         if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1312                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1313                 return VCD_ERROR_OPERATION_FAILED;
1314         }
1315
1316         return 0;
1317 }
1318
1319 int vcd_server_set_foreground(int pid, bool value)
1320 {
1321         /* check if pid is valid */
1322         if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1323                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1324                 return VCD_ERROR_INVALID_PARAMETER;
1325         }
1326
1327         if (0 != vcd_config_set_foreground(pid, value)) {
1328                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1329                 return VCD_ERROR_OPERATION_FAILED;
1330         }
1331
1332         return 0;
1333 }
1334
1335 #if 0
1336 int vcd_server_set_exclusive_command(int pid, bool value)
1337 {
1338         /* check if pid is valid */
1339         if (false == vcd_client_is_available(pid)) {
1340                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1341                 return VCD_ERROR_INVALID_PARAMETER;
1342         }
1343
1344         if (true == value) {
1345                 if (0 != vcd_client_set_exclusive_command(pid)) {
1346                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1347                         return VCD_ERROR_OPERATION_FAILED;
1348                 }
1349         } else {
1350                 if (0 != vcd_client_unset_exclusive_command(pid)) {
1351                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1352                         return VCD_ERROR_OPERATION_FAILED;
1353                 }
1354         }
1355
1356         return 0;
1357 }
1358
1359 int vcd_server_request_start(int pid, bool stop_by_silence)
1360 {
1361         /* check if pid is valid */
1362         if (false == vcd_client_is_available(pid)) {
1363                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1364                 return VCD_ERROR_INVALID_PARAMETER;
1365         }
1366
1367         int ret;
1368         /* Check current state */
1369         vcd_state_e state = vcd_config_get_service_state();
1370
1371         /* Service state should be ready */
1372         if (VCD_STATE_READY != state) {
1373                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1374                 return VCD_ERROR_INVALID_STATE;
1375         }
1376
1377         if (-1 != vcd_client_manager_get_pid()) {
1378                 /* Check current pid is valid */
1379                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1380                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1381                         return VCD_ERROR_INVALID_PARAMETER;
1382                 }
1383         }
1384
1385         ret = vcd_server_mgr_start(stop_by_silence, false);
1386         if (0 != ret) {
1387                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1388                 return VCD_ERROR_INVALID_PARAMETER;
1389         }
1390
1391         return 0;
1392 }
1393
1394 int vcd_server_request_stop(int pid)
1395 {
1396         int ret;
1397         /* Check current state */
1398         vcd_state_e state = vcd_config_get_service_state();
1399
1400         /* Service state should be ready */
1401         if (VCD_STATE_RECORDING != state) {
1402                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1403                 return VCD_ERROR_INVALID_STATE;
1404         }
1405
1406         if (-1 != vcd_client_manager_get_pid()) {
1407                 /* Check current pid is valid */
1408                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1409                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1410                         return VCD_ERROR_INVALID_PARAMETER;
1411                 }
1412         }
1413
1414         ret = vcd_server_mgr_stop();
1415         if (0 != ret) {
1416                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1417                 return VCD_ERROR_OPERATION_FAILED;
1418         }
1419
1420         return VCD_ERROR_NONE;
1421 }
1422
1423 int vcd_server_request_cancel(int pid)
1424 {
1425         int ret;
1426         /* Check current state */
1427         vcd_state_e state = vcd_config_get_service_state();
1428
1429         /* Service state should be recording or processing */
1430         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1431                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1432                 return VCD_ERROR_INVALID_STATE;
1433         }
1434
1435         if (-1 != vcd_client_manager_get_pid()) {
1436                 /* Check current pid is valid */
1437                 if (false == vcd_client_manager_check_demandable_client(pid)) {
1438                         SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1439                         return VCD_ERROR_INVALID_PARAMETER;
1440                 }
1441         }
1442
1443         ret = vcd_server_mgr_cancel();
1444         if (0 != ret) {
1445                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1446                 return VCD_ERROR_OPERATION_FAILED;
1447         }
1448
1449         return VCD_ERROR_NONE;
1450 }
1451 #endif
1452
1453 /*
1454 * VC Server Functions for Widget lib
1455 */
1456 int vcd_server_widget_initialize(int pid)
1457 {
1458         if (false == g_is_engine) {
1459                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1460                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1461                         g_is_engine = false;
1462                         return VCD_ERROR_ENGINE_NOT_FOUND;
1463                 } else {
1464                         g_is_engine = true;
1465                 }
1466         }
1467
1468         if (false == vcd_engine_is_available_engine()) {
1469                 if (0 != vcd_engine_agent_initialize_current_engine()) {
1470                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1471                         return VCD_ERROR_ENGINE_NOT_FOUND;
1472                 }
1473         }
1474
1475         /* check if pid is valid */
1476         if (true == vcd_client_widget_is_available(pid)) {
1477                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1478                 return VCD_ERROR_INVALID_PARAMETER;
1479         }
1480
1481         /* Add client information to client manager */
1482         if (0 != vcd_client_widget_add(pid)) {
1483                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1484                 return VCD_ERROR_OPERATION_FAILED;
1485         }
1486
1487         SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1488
1489         return VCD_ERROR_NONE;
1490 }
1491
1492 int vcd_server_widget_finalize(int pid)
1493 {
1494         /* check if pid is valid */
1495         if (false == vcd_client_widget_is_available(pid)) {
1496                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1497                 return VCD_ERROR_INVALID_PARAMETER;
1498         }
1499
1500         /* Remove client information */
1501         if (0 != vcd_client_widget_delete(pid)) {
1502                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1503         }
1504
1505         if (0 == vcd_client_get_ref_count()) {
1506                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1507                 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1508         }
1509
1510         return VCD_ERROR_NONE;
1511 }
1512
1513 int vcd_server_widget_start_recording(int pid, bool widget_command)
1514 {
1515         /* check if pid is valid */
1516         if (false == vcd_client_widget_is_available(pid)) {
1517                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1518                 return VCD_ERROR_INVALID_PARAMETER;
1519         }
1520
1521         if (true == widget_command) {
1522                 vcd_client_widget_set_command(pid);
1523                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1524         } else {
1525                 vcd_client_widget_unset_command(pid);
1526                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is NOT available");
1527         }
1528
1529         int ret = __start_internal_recognition();
1530         if (0 != ret) {
1531                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1532                 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1533         }
1534
1535         return 0;
1536 }
1537
1538 int vcd_server_widget_start(int pid, bool stop_by_silence)
1539 {
1540         /* check if pid is valid */
1541         int fore_pid = vcd_client_widget_get_foreground_pid();
1542         if (pid != fore_pid) {
1543                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1544                 return VCD_ERROR_INVALID_PARAMETER;
1545         }
1546
1547         /* Check current state */
1548         vcd_state_e state = vcd_config_get_service_state();
1549
1550         /* Service state should be ready */
1551         if (VCD_STATE_READY != state) {
1552                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1553                 return VCD_ERROR_INVALID_STATE;
1554         }
1555
1556         vcd_client_set_slience_detection(stop_by_silence);
1557
1558         /* Notify show tooltip */
1559         ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1560
1561         return 0;
1562 }
1563
1564 int vcd_server_widget_stop(int pid)
1565 {
1566         /* check if pid is valid */
1567         int fore_pid = vcd_client_widget_get_foreground_pid();
1568         if (pid != fore_pid) {
1569                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1570                 return VCD_ERROR_INVALID_PARAMETER;
1571         }
1572
1573         int ret;
1574         /* Check current state */
1575         vcd_state_e state = vcd_config_get_service_state();
1576
1577         /* Service state should be recording */
1578         if (VCD_STATE_RECORDING != state) {
1579                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1580                 return VCD_ERROR_INVALID_STATE;
1581         }
1582
1583         ret = vcd_server_mgr_stop();
1584         if (0 != ret) {
1585                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1586                 return VCD_ERROR_OPERATION_FAILED;
1587         }
1588
1589         return VCD_ERROR_NONE;
1590 }
1591
1592 int vcd_server_widget_cancel(int pid)
1593 {
1594         /* check if pid is valid */
1595         int fore_pid = vcd_client_widget_get_foreground_pid();
1596         if (pid != fore_pid) {
1597                 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1598                 return VCD_ERROR_INVALID_PARAMETER;
1599         }
1600
1601         int ret;
1602         /* Check current state */
1603         vcd_state_e state = vcd_config_get_service_state();
1604
1605         /* Service state should be recording or processing */
1606         if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1607                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1608                 return VCD_ERROR_INVALID_STATE;
1609         }
1610
1611         ret = vcd_server_mgr_cancel();
1612         if (0 != ret) {
1613                 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1614                 return ret;
1615         }
1616
1617         return VCD_ERROR_NONE;
1618 }
1619