4b562b39856fc042ebce7698427a9d2afec37701
[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         }
305
306         /* reset current engine data */
307         g_dynamic_engine.is_loaded = false;
308
309         return 0;
310 }
311
312
313 /*
314  * VCS Engine Interfaces for client
315  */
316
317 int vcd_engine_set_commands()
318 {
319         if (false == g_agent_init) {
320                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
321                 return VCD_ERROR_OPERATION_FAILED;
322         }
323
324         int ret = -1;
325
326         if (true == g_dynamic_engine.is_loaded) {
327                 /* Set dynamic command */
328                 ret = g_dynamic_engine.callbacks->set_commands((vce_cmd_h)0);
329                 if (0 != ret) {
330                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent ERROR] Fail to set command of dynamic engine : error(%d)", ret);
331                         g_dynamic_engine.is_command_ready = false;
332                 } else {
333                         g_dynamic_engine.is_command_ready = true;
334                 }
335
336                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] set command");
337         } else {
338                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available");
339         }
340
341         return 0;
342 }
343
344 int vcd_engine_recognize_start(bool silence)
345 {
346         if (false == g_agent_init) {
347                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
348                 return VCD_ERROR_OPERATION_FAILED;
349         }
350
351         int ret = -1;
352         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] silence is %s", silence ? "true" : "false");
353
354         if (true == g_dynamic_engine.is_loaded && true == g_dynamic_engine.is_command_ready) {
355                 ret = g_dynamic_engine.callbacks->start(silence);
356                 if (0 != ret) {
357                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Fail to start engine error(%d)", ret);
358                         return VCD_ERROR_OPERATION_FAILED;
359                 }
360         } else {
361                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Engine is not available (Cannot start)");
362                 return VCD_ERROR_OPERATION_FAILED;
363         }
364
365         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine start");
366         return 0;
367 }
368
369 int vcd_engine_recognize_audio(const void* data, unsigned int length, vce_speech_detect_e* speech_detected)
370 {
371         if (false == g_agent_init) {
372                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
373                 return VCD_ERROR_OPERATION_FAILED;
374         }
375
376         if (NULL == data) {
377                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
378                 return VCD_ERROR_INVALID_PARAMETER;
379         }
380
381         int ret = -1;
382
383         if (true == g_dynamic_engine.is_loaded) {
384                 ret = g_dynamic_engine.callbacks->set_recording(data, length, speech_detected);
385                 if (0 != ret) {
386                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set recording dynamic engine error(%d)", ret);
387                         if (VCE_ERROR_OUT_OF_NETWORK == ret) {
388                                 return VCD_ERROR_TIMED_OUT;
389                         }
390                         return VCD_ERROR_OPERATION_FAILED;
391                 }
392         }
393
394         return 0;
395 }
396
397 int vcd_engine_recognize_stop()
398 {
399         if (false == g_agent_init) {
400                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
401                 return VCD_ERROR_OPERATION_FAILED;
402         }
403
404         if (true == g_dynamic_engine.is_loaded) {
405                 int ret = -1;
406                 ret = g_dynamic_engine.callbacks->stop();
407                 if (0 != ret) {
408                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to stop dynamic engine error(%d)", ret);
409                         return VCD_ERROR_OPERATION_FAILED;
410                 }
411         } else {
412                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Dynamic engine is not recording state");
413                 return VCD_ERROR_OPERATION_FAILED;
414         }
415
416         return 0;
417 }
418
419 int vcd_engine_recognize_cancel()
420 {
421         if (false == g_agent_init) {
422                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
423                 return VCD_ERROR_OPERATION_FAILED;
424         }
425
426         int ret = -1;
427         if (true == g_dynamic_engine.is_loaded) {
428                 ret = g_dynamic_engine.callbacks->cancel();
429                 if (0 != ret) {
430                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to cancel dynamic engine error(%d)", ret);
431                         return VCD_ERROR_OPERATION_FAILED;
432                 }
433         }
434
435         return 0;
436 }
437
438 int vcd_engine_set_audio_type(const char* audio)
439 {
440         if (false == g_agent_init) {
441                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
442                 return VCD_ERROR_OPERATION_FAILED;
443         }
444
445         int ret = -1;
446         if (true == g_dynamic_engine.is_loaded) {
447                 ret = g_dynamic_engine.callbacks->set_audio_type(audio);
448                 if (0 != ret) {
449                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set audio type(%d)", ret);
450                         return VCD_ERROR_OPERATION_FAILED;
451                 }
452         }
453
454         return 0;
455 }
456
457 int vcd_engine_set_domain(int pid, const char* domain)
458 {
459         if (false == g_agent_init) {
460                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
461                 return VCD_ERROR_OPERATION_FAILED;
462         }
463
464         int ret = -1;
465         if (true == g_dynamic_engine.is_loaded) {
466                 ret = g_dynamic_engine.callbacks->set_domain(domain);
467                 if (0 != ret) {
468                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set domain (%d)", ret);
469                         return VCD_ERROR_OPERATION_FAILED;
470                 }
471         }
472
473         return 0;
474 }
475
476 int vcd_engine_get_nlu_base_info(int pid, const char* key, char** value)
477 {
478         if (NULL == value) {
479                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
480                 return VCD_ERROR_INVALID_PARAMETER;
481         }
482
483         if (false == g_agent_init) {
484                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
485                 return VCD_ERROR_OPERATION_FAILED;
486         }
487
488         int ret = -1;
489         if (true == g_dynamic_engine.is_loaded && NULL != g_dynamic_engine.callbacks->nlu_base_info_request) {
490                 ret = g_dynamic_engine.callbacks->nlu_base_info_request(key, value);
491                 if (0 != ret) {
492                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get nlu base info (%d)", ret);
493                         return VCD_ERROR_OPERATION_FAILED;
494                 }
495         } else {
496                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded or There is no nlu_base_info_request callback");
497                 return VCD_ERROR_OPERATION_FAILED;
498         }
499
500         return 0;
501 }
502
503 int vcd_engine_set_private_data(int pid, const char* key, const char* data)
504 {
505         if (false == g_agent_init) {
506                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
507                 return VCD_ERROR_OPERATION_FAILED;
508         }
509
510         int ret = -1;
511         if (true == g_dynamic_engine.is_loaded && NULL != g_dynamic_engine.callbacks->private_data_set) {
512                 ret = g_dynamic_engine.callbacks->private_data_set(key, data);
513                 if (0 != ret) {
514                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set private data (%d)", ret);
515                         return VCD_ERROR_OPERATION_FAILED;
516                 }
517         } else {
518                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded or There is no private_data_set callback");
519                 return VCD_ERROR_OPERATION_FAILED;
520         }
521
522         return 0;
523 }
524
525 int vcd_engine_get_private_data(int pid, const char* key, char** data)
526 {
527         if (NULL == data) {
528                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
529                 return VCD_ERROR_INVALID_PARAMETER;
530         }
531
532         if (false == g_agent_init) {
533                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
534                 return VCD_ERROR_OPERATION_FAILED;
535         }
536
537         int ret = -1;
538         if (true == g_dynamic_engine.is_loaded && NULL != g_dynamic_engine.callbacks->private_data_request) {
539                 ret = g_dynamic_engine.callbacks->private_data_request(key, data);
540                 if (0 != ret) {
541                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get private data (%d)", ret);
542                         return VCD_ERROR_OPERATION_FAILED;
543                 }
544         } else {
545                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded or There is no private_data_request callback");
546                 return VCD_ERROR_OPERATION_FAILED;
547         }
548
549         return 0;
550 }
551
552 int vcd_engine_process_text(int pid, const char* text)
553 {
554         if (false == g_agent_init) {
555                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
556                 return VCD_ERROR_OPERATION_FAILED;
557         }
558
559         int ret = -1;
560         if (true == g_dynamic_engine.is_loaded) {
561                 ret = g_dynamic_engine.callbacks->process_text(text);
562                 if (0 != ret) {
563                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process text (%d)", ret);
564                         return VCD_ERROR_OPERATION_FAILED;
565                 }
566         }
567
568         return 0;
569 }
570
571 int vcd_engine_process_list_event(int pid, const char* event)
572 {
573         if (false == g_agent_init) {
574                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
575                 return VCD_ERROR_OPERATION_FAILED;
576         }
577
578         int ret = -1;
579         if (true == g_dynamic_engine.is_loaded) {
580                 ret = g_dynamic_engine.callbacks->process_list_event(event);
581                 if (0 != ret) {
582                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process list event (%d)", ret);
583                         return VCD_ERROR_OPERATION_FAILED;
584                 }
585         }
586
587         return 0;
588 }
589
590 int vcd_engine_process_haptic_event(int pid, const char* event)
591 {
592         if (false == g_agent_init) {
593                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
594                 return VCD_ERROR_OPERATION_FAILED;
595         }
596
597         int ret = -1;
598         if (true == g_dynamic_engine.is_loaded) {
599                 ret = g_dynamic_engine.callbacks->process_haptic_event(event);
600                 if (0 != ret) {
601                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process haptic event (%d)", ret);
602                         return VCD_ERROR_OPERATION_FAILED;
603                 }
604         }
605
606         return 0;
607 }
608
609 /*
610  * VCS Engine Interfaces for client and setting
611  */
612
613 int vcd_engine_get_audio_format(const char* audio_id, vce_audio_type_e* types, int* rate, int* channels)
614 {
615         if (false == g_agent_init) {
616                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
617                 return VCD_ERROR_OPERATION_FAILED;
618         }
619
620         if (true != g_dynamic_engine.is_loaded) {
621                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
622         }
623
624         int ret = g_dynamic_engine.callbacks->get_recording_format(audio_id, types, rate, channels);
625         if (0 != ret) {
626                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get recording format(%d)", ret);
627                 return VCD_ERROR_OPERATION_FAILED;
628         }
629
630         return 0;
631 }
632
633 bool __supported_language_cb(const char* language, void* user_data)
634 {
635         GList** lang_list = (GList**)user_data;
636
637         if (NULL == language || NULL == lang_list) {
638                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Input parameter is NULL in callback!!!!");
639                 return false;
640         }
641
642         SLOG(LOG_DEBUG, TAG_VCD, "-- Language(%s)", language);
643
644         char* temp_lang = g_strdup(language);
645
646         *lang_list = g_list_append(*lang_list, temp_lang);
647
648         return true;
649 }
650
651 int vcd_engine_supported_langs(GList** lang_list)
652 {
653         if (false == g_agent_init) {
654                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
655                 return VCD_ERROR_OPERATION_FAILED;
656         }
657
658         if (true != g_dynamic_engine.is_loaded) {
659                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
660         }
661
662         int ret = g_dynamic_engine.callbacks->foreach_langs(__supported_language_cb, (void*)lang_list);
663         if (0 != ret) {
664                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get language list error(%d)", ret);
665                 return VCD_ERROR_OPERATION_FAILED;
666         }
667
668         return 0;
669 }
670
671
672 int vcd_engine_get_current_language(char** lang)
673 {
674         if (false == g_agent_init) {
675                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
676                 return VCD_ERROR_OPERATION_FAILED;
677         }
678
679         if (NULL == lang) {
680                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
681                 return VCD_ERROR_INVALID_PARAMETER;
682         }
683
684         /* get default language */
685         *lang = g_strdup(g_default_lang);
686
687         return 0;
688 }
689
690 int vcd_engine_set_current_language(const char* language)
691 {
692         if (false == g_agent_init) {
693                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
694                 return VCD_ERROR_OPERATION_FAILED;
695         }
696
697         if (NULL == language) {
698                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
699                 return VCD_ERROR_INVALID_PARAMETER;
700         }
701
702         int ret;
703
704         if (true == g_dynamic_engine.is_loaded) {
705                 g_dynamic_engine.is_command_ready = false;
706
707                 ret = g_dynamic_engine.callbacks->set_language(language);
708                 if (0 != ret) {
709                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Fail to set language of dynamic engine error(%d, %s)", ret, language);
710                 }
711         } else {
712                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available (Cannot start)");
713         }
714
715         return 0;
716 }
717
718 int vcd_engine_agent_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
719 {
720         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request foreach command from engine");
721         return vcd_client_foreach_command((client_foreach_command_cb)callback, user_data);
722 }
723
724 int vcd_engine_agent_get_command_count(vce_cmd_h vce_command)
725 {
726         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request command length from engine");
727
728         return vcd_client_get_length();
729 }
730
731 int vcd_engine_agent_get_audio_type(char** audio_type)
732 {
733         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request audio type");
734
735         return vcd_recorder_get(audio_type);
736 }
737
738 int vcd_engine_agent_set_private_data(const char* key, const char* data)
739 {
740         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request set private data, key(%s), data(%s)", key, data);
741         vcdc_send_request_set_private_data(vcd_client_manager_get_pid(), key, data);
742
743         return VCD_ERROR_NONE;
744 }
745
746 int vcd_engine_agent_get_private_data(const char* key, char** data)
747 {
748         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request get private data, key(%s)", key);
749         vcdc_send_request_get_private_data(vcd_client_manager_get_pid(), key, data);
750
751         return VCD_ERROR_NONE;
752 }
753
754 int vcd_engine_agent_start_recording()
755 {
756         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request start recording");
757
758         int ret = vcd_recorder_start();
759         if (0 != ret) {
760                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
761                 vcd_engine_recognize_cancel();
762                 /* Send error cb to manager */
763                 vcdc_send_error_signal_to_manager(vcd_client_manager_get_pid(), VCD_ERROR_OPERATION_FAILED, "voice_engine.error.proc_fail");
764                 return ret;
765         }
766
767         return VCD_ERROR_NONE;
768 }
769
770 int vcd_engine_agent_stop_recording()
771 {
772         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request stop recording");
773
774         return vcd_recorder_stop();
775 }
776
777 int vcd_engine_agent_set_private_data_set_cb(vce_private_data_set_cb callback_func)
778 {
779         if (false == g_agent_init) {
780                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
781                 return VCD_ERROR_OPERATION_FAILED;
782         }
783
784         if (false == g_dynamic_engine.is_loaded) {
785                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not loaded engine");
786                 return VCD_ERROR_OPERATION_FAILED;
787         }
788
789         g_dynamic_engine.callbacks->private_data_set = callback_func;
790
791         return VCD_ERROR_NONE;
792 }
793
794 int vcd_engine_agent_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
795 {
796         if (false == g_agent_init) {
797                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
798                 return VCD_ERROR_OPERATION_FAILED;
799         }
800
801         if (false == g_dynamic_engine.is_loaded) {
802                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not loaded engine");
803                 return VCD_ERROR_OPERATION_FAILED;
804         }
805
806         g_dynamic_engine.callbacks->private_data_request = callback_func;
807
808         return VCD_ERROR_NONE;
809 }
810
811 int vcd_engine_agent_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
812 {
813         if (false == g_agent_init) {
814                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
815                 return VCD_ERROR_OPERATION_FAILED;
816         }
817
818         if (false == g_dynamic_engine.is_loaded) {
819                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not loaded engine");
820                 return VCD_ERROR_OPERATION_FAILED;
821         }
822
823         g_dynamic_engine.callbacks->nlu_base_info_request = callback_func;
824
825         return VCD_ERROR_NONE;
826 }
827