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