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