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