Fixed logic for stt unprepare
[platform/core/uifw/voice-control.git] / server / vcd_engine_agent.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
18 #include <dlfcn.h>
19 #include <dirent.h>
20
21 #include "vcd_client_data.h"
22 #include "vcd_config.h"
23 #include "vcd_engine_agent.h"
24 #include "vcd_main.h"
25 #include "vcd_recorder.h"
26 #include "vcd_dbus.h"
27
28 /*
29  * Internal data structure
30  */
31 typedef struct {
32         /* engine info */
33         char*   engine_uuid;
34         char*   engine_name;
35         char*   engine_path;
36         char*   engine_setting_path;
37
38         /* engine load info */
39         bool    is_set;
40         bool    is_loaded;
41         bool    is_command_ready;
42         bool    use_network;
43         void    *handle;
44
45         vc_engine_callback_s* callbacks;
46 } vcengine_s;
47
48 typedef struct _vcengine_info {
49         char*   engine_uuid;
50         char*   engine_path;
51         char*   engine_name;
52 } vcengine_info_s;
53
54
55 /*
56  * static data
57  */
58
59 /** vc engine agent init */
60 static bool g_agent_init;
61
62 /** current engine information */
63 static vcengine_s g_dynamic_engine;
64
65 static char* g_default_lang;
66
67 bool __supported_language_cb(const char* language, void* user_data);
68
69 /*
70  * Internal Interfaces
71  */
72
73 /** get engine info */
74 int __internal_get_engine_info(vce_request_callback_s* callback);
75
76 /*
77  * VCS Engine Agent Interfaces
78  */
79 int vcd_engine_agent_init()
80 {
81         if (true == g_agent_init) {
82                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Already initialized");
83                 return VCD_ERROR_NONE;
84         }
85
86         /* init dynamic engine */
87         g_dynamic_engine.engine_uuid = NULL;
88         g_dynamic_engine.engine_name = NULL;
89         g_dynamic_engine.engine_path = NULL;
90
91         g_dynamic_engine.is_set = false;
92         g_dynamic_engine.is_loaded = false;
93         g_dynamic_engine.handle = NULL;
94         g_dynamic_engine.is_command_ready = false;
95
96         g_dynamic_engine.callbacks = (vc_engine_callback_s*)calloc(1, sizeof(vc_engine_callback_s));
97         if (NULL == g_dynamic_engine.callbacks) {
98                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Fail to allocate memory");
99                 return VCD_ERROR_OUT_OF_MEMORY;
100         }
101
102         g_agent_init = true;
103
104         if (0 != vcd_config_get_default_language(&g_default_lang)) {
105                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] There is No default voice in config");
106                 /* Set default voice */
107                 g_default_lang = strdup(VC_BASE_LANGUAGE);
108         }
109
110         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine Agent Initialize");
111
112         return 0;
113 }
114
115 int vcd_engine_agent_release()
116 {
117         if (false == g_agent_init) {
118                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
119                 return VCD_ERROR_OPERATION_FAILED;
120         }
121
122         /* unload current engine */
123         if (0 != vcd_engine_agent_unload_current_engine()) {
124                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to deinitialize");
125                 return VCD_ERROR_OPERATION_FAILED;
126         }
127
128         /* release current engine data */
129         if (NULL != g_dynamic_engine.callbacks) {
130                 free(g_dynamic_engine.callbacks);
131                 g_dynamic_engine.callbacks = NULL;
132         }
133
134         g_agent_init = false;
135
136         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine Agent release");
137
138         return 0;
139 }
140
141 bool vcd_engine_is_available_engine()
142 {
143         if (true == g_dynamic_engine.is_loaded)
144                 return true;
145
146         return false;
147 }
148
149 int __internal_get_engine_info(vce_request_callback_s* callback)
150 {
151         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] inside __internal_get_engine_info");
152
153         if (NULL == callback) {
154                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid engine");
155                 return VCD_ERROR_ENGINE_NOT_FOUND;
156         }
157
158         if (NULL == callback->get_info) {
159                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid engine");
160                 return VCD_ERROR_ENGINE_NOT_FOUND;
161         }
162
163         if (0 != callback->get_info(&(g_dynamic_engine.engine_uuid), &(g_dynamic_engine.engine_name), &(g_dynamic_engine.engine_setting_path), &(g_dynamic_engine.use_network))) {
164                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get engine info");
165                 return VCD_ERROR_ENGINE_NOT_FOUND;
166         }
167
168         if (NULL != g_dynamic_engine.callbacks) {
169                 free(g_dynamic_engine.callbacks);
170                 g_dynamic_engine.callbacks = NULL;
171         }
172         g_dynamic_engine.callbacks = (vc_engine_callback_s*)calloc(1, sizeof(vc_engine_callback_s));
173         if (NULL == g_dynamic_engine.callbacks) {
174                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to allocate memory");
175                 return VCD_ERROR_OUT_OF_MEMORY;
176         }
177
178         g_dynamic_engine.callbacks->get_info = callback->get_info;
179         g_dynamic_engine.callbacks->get_recording_format = callback->get_recording_format;
180         g_dynamic_engine.callbacks->foreach_langs = callback->foreach_langs;
181         g_dynamic_engine.callbacks->is_lang_supported = callback->is_lang_supported;
182         g_dynamic_engine.callbacks->initialize = callback->initialize;
183         g_dynamic_engine.callbacks->deinitialize = callback->deinitialize;
184         g_dynamic_engine.callbacks->set_language = callback->set_language;
185         g_dynamic_engine.callbacks->set_commands = callback->set_commands;
186         g_dynamic_engine.callbacks->unset_commands = callback->unset_commands;
187
188         g_dynamic_engine.callbacks->start = callback->start;
189         g_dynamic_engine.callbacks->set_recording = callback->set_recording;
190         g_dynamic_engine.callbacks->stop = callback->stop;
191         g_dynamic_engine.callbacks->cancel = callback->cancel;
192         g_dynamic_engine.callbacks->set_domain = callback->set_domain;
193         g_dynamic_engine.callbacks->set_audio_type = callback->set_audio_type;
194         g_dynamic_engine.callbacks->process_text = callback->process_text;
195         g_dynamic_engine.callbacks->process_list_event = callback->process_list_event;
196         g_dynamic_engine.callbacks->process_haptic_event = callback->process_haptic_event;
197
198         g_dynamic_engine.callbacks->private_data_set = NULL;
199         g_dynamic_engine.callbacks->private_data_request = NULL;
200         g_dynamic_engine.callbacks->nlu_base_info_request = NULL;
201
202         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Valid Engine");
203         SLOG(LOG_DEBUG, TAG_VCD, "Engine uuid : %s", g_dynamic_engine.engine_uuid);
204         SLOG(LOG_DEBUG, TAG_VCD, "Engine name : %s", g_dynamic_engine.engine_name);
205         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
206
207         return 0;
208 }
209
210 int vcd_engine_agent_load_current_engine(vce_request_callback_s* callback)
211 {
212         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] load current engine START");
213
214         if (false == g_agent_init) {
215                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
216                 return VCD_ERROR_OPERATION_FAILED;
217         }
218
219         if (true == g_dynamic_engine.is_loaded) {
220                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Engine has already been loaded");
221                 return 0;
222         }
223
224         if (NULL == callback) {
225                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid engine");
226                 return VCD_ERROR_ENGINE_NOT_FOUND;
227         }
228
229         /* Get current engine info */
230         int ret = __internal_get_engine_info(callback);
231         if (0 != ret) {
232                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get engine info");
233                 return ret;
234         }
235
236         /* Check all engine functions */
237         if (NULL == g_dynamic_engine.callbacks->get_info ||
238                         NULL == g_dynamic_engine.callbacks->get_recording_format ||
239                         NULL == g_dynamic_engine.callbacks->foreach_langs ||
240                         NULL == g_dynamic_engine.callbacks->is_lang_supported ||
241                         NULL == g_dynamic_engine.callbacks->initialize ||
242                         NULL == g_dynamic_engine.callbacks->deinitialize ||
243                         NULL == g_dynamic_engine.callbacks->set_language ||
244                         NULL == g_dynamic_engine.callbacks->set_commands ||
245                         NULL == g_dynamic_engine.callbacks->unset_commands ||
246                         NULL == g_dynamic_engine.callbacks->start ||
247                         NULL == g_dynamic_engine.callbacks->set_recording ||
248                         NULL == g_dynamic_engine.callbacks->stop ||
249                         NULL == g_dynamic_engine.callbacks->cancel ||
250                         NULL == g_dynamic_engine.callbacks->set_audio_type ||
251                         NULL == g_dynamic_engine.callbacks->set_domain ||
252                         NULL == g_dynamic_engine.callbacks->process_text ||
253                         NULL == g_dynamic_engine.callbacks->process_list_event ||
254                         NULL == g_dynamic_engine.callbacks->process_haptic_event) {
255                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] The current engine is NOT valid");
256                 return VCD_ERROR_ENGINE_NOT_FOUND;
257         }
258
259         /* initalize engine */
260         ret = g_dynamic_engine.callbacks->initialize();
261         if (0 != ret) {
262                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to initialize vc engine service");
263                 return ret;
264         }
265
266         /* set the supported language */
267         if (true == g_dynamic_engine.callbacks->is_lang_supported(g_default_lang)) {
268                 ret = g_dynamic_engine.callbacks->set_language(g_default_lang);
269                 if (0 != ret) {
270                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set the supported language");
271                         return ret;
272                 }
273
274                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] The %s has been loaded!!!", g_dynamic_engine.engine_name);
275                 g_dynamic_engine.is_loaded = true;
276         } else {
277                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent WARNING] This engine do not support default language : lang(%s)", g_default_lang);
278                 g_dynamic_engine.is_loaded = false;
279                 return VCD_ERROR_OPERATION_FAILED;
280         }
281
282         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] load current engine FINISH");
283
284         return 0;
285 }
286
287 int vcd_engine_agent_unload_current_engine()
288 {
289         if (false == g_agent_init) {
290                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
291                 return VCD_ERROR_OPERATION_FAILED;
292         }
293
294         if (false == g_dynamic_engine.is_loaded) {
295                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Engine has already been unloaded");
296                 return VCD_ERROR_NONE;
297         }
298
299         /* shut down engine */
300         int ret = 0;
301         ret = g_dynamic_engine.callbacks->deinitialize();
302         if (0 != ret) {
303                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to deinitialize");
304                 return VCD_ERROR_OPERATION_FAILED;
305         }
306
307         /* reset current engine data */
308         g_dynamic_engine.is_loaded = false;
309
310         return VCD_ERROR_NONE;
311 }
312
313
314 /*
315  * VCS Engine Interfaces for client
316  */
317
318 int vcd_engine_set_commands()
319 {
320         if (false == g_agent_init) {
321                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
322                 return VCD_ERROR_OPERATION_FAILED;
323         }
324
325         int ret = -1;
326
327         if (true == g_dynamic_engine.is_loaded) {
328                 /* Set dynamic command */
329                 ret = g_dynamic_engine.callbacks->set_commands((vce_cmd_h)0);
330                 if (0 != ret) {
331                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent ERROR] Fail to set command of dynamic engine : error(%d)", ret);
332                         g_dynamic_engine.is_command_ready = false;
333                 } else {
334                         g_dynamic_engine.is_command_ready = true;
335                 }
336
337                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] set command");
338         } else {
339                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available");
340         }
341
342         return 0;
343 }
344
345 int vcd_engine_recognize_start(bool silence)
346 {
347         if (false == g_agent_init) {
348                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
349                 return VCD_ERROR_OPERATION_FAILED;
350         }
351
352         int ret = -1;
353         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] silence is %s", silence ? "true" : "false");
354
355         if (true == g_dynamic_engine.is_loaded && true == g_dynamic_engine.is_command_ready) {
356                 ret = g_dynamic_engine.callbacks->start(silence);
357                 if (0 != ret) {
358                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Fail to start engine error(%d)", ret);
359                         return VCD_ERROR_OPERATION_FAILED;
360                 }
361         } else {
362                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Engine is not available (Cannot start)");
363                 return VCD_ERROR_OPERATION_FAILED;
364         }
365
366         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine start");
367         return 0;
368 }
369
370 int vcd_engine_recognize_audio(const void* data, unsigned int length, vce_speech_detect_e* speech_detected)
371 {
372         if (false == g_agent_init) {
373                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
374                 return VCD_ERROR_OPERATION_FAILED;
375         }
376
377         if (NULL == data) {
378                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
379                 return VCD_ERROR_INVALID_PARAMETER;
380         }
381
382         int ret = -1;
383
384         if (true == g_dynamic_engine.is_loaded) {
385                 ret = g_dynamic_engine.callbacks->set_recording(data, length, speech_detected);
386                 if (0 != ret) {
387                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set recording dynamic engine error(%d)", ret);
388                         if (VCE_ERROR_OUT_OF_NETWORK == ret) {
389                                 return VCD_ERROR_TIMED_OUT;
390                         }
391                         return VCD_ERROR_OPERATION_FAILED;
392                 }
393         }
394
395         return 0;
396 }
397
398 int vcd_engine_recognize_stop()
399 {
400         if (false == g_agent_init) {
401                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
402                 return VCD_ERROR_OPERATION_FAILED;
403         }
404
405         if (true == g_dynamic_engine.is_loaded) {
406                 int ret = -1;
407                 ret = g_dynamic_engine.callbacks->stop();
408                 if (0 != ret) {
409                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to stop dynamic engine error(%d)", ret);
410                         return VCD_ERROR_OPERATION_FAILED;
411                 }
412         } else {
413                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Dynamic engine is not recording state");
414                 return VCD_ERROR_OPERATION_FAILED;
415         }
416
417         return 0;
418 }
419
420 int vcd_engine_recognize_cancel()
421 {
422         if (false == g_agent_init) {
423                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
424                 return VCD_ERROR_OPERATION_FAILED;
425         }
426
427         int ret = -1;
428         if (true == g_dynamic_engine.is_loaded) {
429                 ret = g_dynamic_engine.callbacks->cancel();
430                 if (0 != ret) {
431                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to cancel dynamic engine error(%d)", ret);
432                         return VCD_ERROR_OPERATION_FAILED;
433                 }
434         }
435
436         return 0;
437 }
438
439 int vcd_engine_set_audio_type(const char* audio)
440 {
441         if (false == g_agent_init) {
442                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
443                 return VCD_ERROR_OPERATION_FAILED;
444         }
445
446         int ret = -1;
447         if (true == g_dynamic_engine.is_loaded) {
448                 ret = g_dynamic_engine.callbacks->set_audio_type(audio);
449                 if (0 != ret) {
450                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set audio type(%d)", ret);
451                         return VCD_ERROR_OPERATION_FAILED;
452                 }
453         }
454
455         return 0;
456 }
457
458 int vcd_engine_set_domain(int pid, const char* domain)
459 {
460         if (false == g_agent_init) {
461                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
462                 return VCD_ERROR_OPERATION_FAILED;
463         }
464
465         int ret = -1;
466         if (true == g_dynamic_engine.is_loaded) {
467                 ret = g_dynamic_engine.callbacks->set_domain(domain);
468                 if (0 != ret) {
469                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set domain (%d)", ret);
470                         return VCD_ERROR_OPERATION_FAILED;
471                 }
472         }
473
474         return 0;
475 }
476
477 int vcd_engine_get_nlu_base_info(int pid, const char* key, char** value)
478 {
479         if (NULL == value) {
480                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
481                 return VCD_ERROR_INVALID_PARAMETER;
482         }
483
484         if (false == g_agent_init) {
485                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
486                 return VCD_ERROR_OPERATION_FAILED;
487         }
488
489         int ret = -1;
490         if (true == g_dynamic_engine.is_loaded && NULL != g_dynamic_engine.callbacks->nlu_base_info_request) {
491                 ret = g_dynamic_engine.callbacks->nlu_base_info_request(key, value);
492                 if (0 != ret) {
493                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get nlu base info (%d)", ret);
494                         return VCD_ERROR_OPERATION_FAILED;
495                 }
496         } else {
497                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded or There is no nlu_base_info_request callback");
498                 return VCD_ERROR_OPERATION_FAILED;
499         }
500
501         return 0;
502 }
503
504 int vcd_engine_set_private_data(int pid, const char* key, const char* data)
505 {
506         if (false == g_agent_init) {
507                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
508                 return VCD_ERROR_OPERATION_FAILED;
509         }
510
511         int ret = -1;
512         if (true == g_dynamic_engine.is_loaded && NULL != g_dynamic_engine.callbacks->private_data_set) {
513                 ret = g_dynamic_engine.callbacks->private_data_set(key, data);
514                 if (0 != ret) {
515                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set private data (%d)", ret);
516                         return VCD_ERROR_OPERATION_FAILED;
517                 }
518         } else {
519                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded or There is no private_data_set callback");
520                 return VCD_ERROR_OPERATION_FAILED;
521         }
522
523         return 0;
524 }
525
526 int vcd_engine_get_private_data(int pid, const char* key, char** data)
527 {
528         if (NULL == data) {
529                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
530                 return VCD_ERROR_INVALID_PARAMETER;
531         }
532
533         if (false == g_agent_init) {
534                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
535                 return VCD_ERROR_OPERATION_FAILED;
536         }
537
538         int ret = -1;
539         if (true == g_dynamic_engine.is_loaded && NULL != g_dynamic_engine.callbacks->private_data_request) {
540                 ret = g_dynamic_engine.callbacks->private_data_request(key, data);
541                 if (0 != ret) {
542                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get private data (%d)", ret);
543                         return VCD_ERROR_OPERATION_FAILED;
544                 }
545         } else {
546                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded or There is no private_data_request callback");
547                 return VCD_ERROR_OPERATION_FAILED;
548         }
549
550         return 0;
551 }
552
553 int vcd_engine_process_text(int pid, const char* text)
554 {
555         if (false == g_agent_init) {
556                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
557                 return VCD_ERROR_OPERATION_FAILED;
558         }
559
560         int ret = -1;
561         if (true == g_dynamic_engine.is_loaded) {
562                 ret = g_dynamic_engine.callbacks->process_text(text);
563                 if (0 != ret) {
564                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process text (%d)", ret);
565                         return VCD_ERROR_OPERATION_FAILED;
566                 }
567         }
568
569         return 0;
570 }
571
572 int vcd_engine_process_list_event(int pid, const char* event)
573 {
574         if (false == g_agent_init) {
575                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
576                 return VCD_ERROR_OPERATION_FAILED;
577         }
578
579         int ret = -1;
580         if (true == g_dynamic_engine.is_loaded) {
581                 ret = g_dynamic_engine.callbacks->process_list_event(event);
582                 if (0 != ret) {
583                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process list event (%d)", ret);
584                         return VCD_ERROR_OPERATION_FAILED;
585                 }
586         }
587
588         return 0;
589 }
590
591 int vcd_engine_process_haptic_event(int pid, const char* event)
592 {
593         if (false == g_agent_init) {
594                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
595                 return VCD_ERROR_OPERATION_FAILED;
596         }
597
598         int ret = -1;
599         if (true == g_dynamic_engine.is_loaded) {
600                 ret = g_dynamic_engine.callbacks->process_haptic_event(event);
601                 if (0 != ret) {
602                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process haptic event (%d)", ret);
603                         return VCD_ERROR_OPERATION_FAILED;
604                 }
605         }
606
607         return 0;
608 }
609
610 /*
611  * VCS Engine Interfaces for client and setting
612  */
613
614 int vcd_engine_get_audio_format(const char* audio_id, vce_audio_type_e* types, int* rate, int* channels)
615 {
616         if (false == g_agent_init) {
617                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
618                 return VCD_ERROR_OPERATION_FAILED;
619         }
620
621         if (true != g_dynamic_engine.is_loaded) {
622                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
623         }
624
625         int ret = g_dynamic_engine.callbacks->get_recording_format(audio_id, types, rate, channels);
626         if (0 != ret) {
627                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get recording format(%d)", ret);
628                 return VCD_ERROR_OPERATION_FAILED;
629         }
630
631         return 0;
632 }
633
634 bool __supported_language_cb(const char* language, void* user_data)
635 {
636         GList** lang_list = (GList**)user_data;
637
638         if (NULL == language || NULL == lang_list) {
639                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Input parameter is NULL in callback!!!!");
640                 return false;
641         }
642
643         SLOG(LOG_DEBUG, TAG_VCD, "-- Language(%s)", language);
644
645         char* temp_lang = g_strdup(language);
646
647         *lang_list = g_list_append(*lang_list, temp_lang);
648
649         return true;
650 }
651
652 int vcd_engine_supported_langs(GList** lang_list)
653 {
654         if (false == g_agent_init) {
655                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
656                 return VCD_ERROR_OPERATION_FAILED;
657         }
658
659         if (true != g_dynamic_engine.is_loaded) {
660                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
661         }
662
663         int ret = g_dynamic_engine.callbacks->foreach_langs(__supported_language_cb, (void*)lang_list);
664         if (0 != ret) {
665                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get language list error(%d)", ret);
666                 return VCD_ERROR_OPERATION_FAILED;
667         }
668
669         return 0;
670 }
671
672
673 int vcd_engine_get_current_language(char** lang)
674 {
675         if (false == g_agent_init) {
676                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
677                 return VCD_ERROR_OPERATION_FAILED;
678         }
679
680         if (NULL == lang) {
681                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
682                 return VCD_ERROR_INVALID_PARAMETER;
683         }
684
685         /* get default language */
686         *lang = g_strdup(g_default_lang);
687
688         return 0;
689 }
690
691 int vcd_engine_set_current_language(const char* language)
692 {
693         if (false == g_agent_init) {
694                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
695                 return VCD_ERROR_OPERATION_FAILED;
696         }
697
698         if (NULL == language) {
699                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
700                 return VCD_ERROR_INVALID_PARAMETER;
701         }
702
703         int ret;
704
705         if (true == g_dynamic_engine.is_loaded) {
706                 g_dynamic_engine.is_command_ready = false;
707
708                 ret = g_dynamic_engine.callbacks->set_language(language);
709                 if (0 != ret) {
710                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Fail to set language of dynamic engine error(%d, %s)", ret, language);
711                 }
712         } else {
713                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available (Cannot start)");
714         }
715
716         return 0;
717 }
718
719 int vcd_engine_agent_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
720 {
721         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request foreach command from engine");
722         return vcd_client_foreach_command((client_foreach_command_cb)callback, user_data);
723 }
724
725 int vcd_engine_agent_get_command_count(vce_cmd_h vce_command)
726 {
727         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request command length from engine");
728
729         return vcd_client_get_length();
730 }
731
732 int vcd_engine_agent_get_audio_type(char** audio_type)
733 {
734         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request audio type");
735
736         return vcd_recorder_get(audio_type);
737 }
738
739 int vcd_engine_agent_set_private_data(const char* key, const char* data)
740 {
741         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request set private data, key(%s), data(%s)", key, data);
742         vcdc_send_request_set_private_data(vcd_client_manager_get_pid(), key, data);
743
744         return VCD_ERROR_NONE;
745 }
746
747 int vcd_engine_agent_get_private_data(const char* key, char** data)
748 {
749         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request get private data, key(%s)", key);
750         vcdc_send_request_get_private_data(vcd_client_manager_get_pid(), key, data);
751
752         return VCD_ERROR_NONE;
753 }
754
755 int vcd_engine_agent_start_recording()
756 {
757         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request start recording");
758
759         int ret = vcd_recorder_start();
760         if (0 != ret) {
761                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
762                 vcd_engine_recognize_cancel();
763                 /* Send error cb to manager */
764                 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
765                 return ret;
766         }
767
768         return VCD_ERROR_NONE;
769 }
770
771 int vcd_engine_agent_stop_recording()
772 {
773         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request stop recording");
774
775         return vcd_recorder_stop();
776 }
777
778 int vcd_engine_agent_set_private_data_set_cb(vce_private_data_set_cb callback_func)
779 {
780         if (false == g_agent_init) {
781                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
782                 return VCD_ERROR_OPERATION_FAILED;
783         }
784
785         if (false == g_dynamic_engine.is_loaded) {
786                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not loaded engine");
787                 return VCD_ERROR_OPERATION_FAILED;
788         }
789
790         g_dynamic_engine.callbacks->private_data_set = callback_func;
791
792         return VCD_ERROR_NONE;
793 }
794
795 int vcd_engine_agent_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
796 {
797         if (false == g_agent_init) {
798                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
799                 return VCD_ERROR_OPERATION_FAILED;
800         }
801
802         if (false == g_dynamic_engine.is_loaded) {
803                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not loaded engine");
804                 return VCD_ERROR_OPERATION_FAILED;
805         }
806
807         g_dynamic_engine.callbacks->private_data_request = callback_func;
808
809         return VCD_ERROR_NONE;
810 }
811
812 int vcd_engine_agent_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
813 {
814         if (false == g_agent_init) {
815                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
816                 return VCD_ERROR_OPERATION_FAILED;
817         }
818
819         if (false == g_dynamic_engine.is_loaded) {
820                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not loaded engine");
821                 return VCD_ERROR_OPERATION_FAILED;
822         }
823
824         g_dynamic_engine.callbacks->nlu_base_info_request = callback_func;
825
826         return VCD_ERROR_NONE;
827 }
828