Merge "Add internal method for asr result" into tizen
[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
27 /*
28 * Internal data structure
29 */
30 typedef struct {
31         /* engine info */
32         char*   engine_uuid;
33         char*   engine_name;
34         char*   engine_path;
35
36         /* engine load info */
37         bool    is_set;
38         bool    is_loaded;
39         bool    is_command_ready;
40         void    *handle;
41
42         vcpe_funcs_s*   pefuncs;
43         vcpd_funcs_s*   pdfuncs;
44
45         int (*vcp_load_engine)(vcpd_funcs_s* pdfuncs, vcpe_funcs_s* pefuncs);
46         int (*vcp_unload_engine)();
47 } vcengine_s;
48
49 typedef struct _vcengine_info {
50         char*   engine_uuid;
51         char*   engine_path;
52         char*   engine_name;
53 } vcengine_info_s;
54
55
56 /*
57 * static data
58 */
59
60 /** vc engine agent init */
61 static bool g_agent_init;
62
63 /** vc engine list */
64 static GList *g_engine_list;
65
66 /** current engine information */
67 static vcengine_s g_dynamic_engine;
68
69 static char* g_default_lang;
70
71 /** callback functions */
72 static result_callback g_result_cb = NULL;
73
74 static asr_result_callback g_asr_result_cb = NULL;
75
76 #if 0
77 static pre_result_callback g_pre_result_cb = NULL;
78
79 static nlu_result_callback g_nlu_result_cb = NULL;
80 #endif
81
82 static error_callback g_error_cb = NULL;
83
84 bool __supported_language_cb(const char* language, void* user_data);
85
86 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* engine_setting, bool use_network, void* user_data);
87
88 bool __engine_setting_cb(const char* key, const char* value, void* user_data);
89
90 /** Free voice list */
91 void __free_language_list(GList* lang_list);
92
93
94 /*
95 * Internal Interfaces
96 */
97
98 /** check engine id */
99 int __internal_check_engine_id(const char* engine_uuid);
100
101 /** update engine list */
102 int __internal_update_engine_list();
103
104 /** get engine info */
105 int __internal_get_engine_info(const char* filepath, vcengine_info_s** info);
106
107 int __log_enginelist();
108
109 /*
110 * VCS Engine Agent Interfaces
111 */
112 //int vcd_engine_agent_init(pre_result_callback pre_result_cb, result_callback result_cb, nlu_result_callback nlu_result_cb, error_callback error_cb)
113 int vcd_engine_agent_init(asr_result_callback asr_result_cb, result_callback result_cb, error_callback error_cb)
114 {
115         if (/*NULL == pre_result_cb*/ NULL == asr_result_cb || NULL == result_cb /*|| NULL == nlu_result_cb*/ || NULL == error_cb) {
116                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Input parameter is NULL");
117                 return VCD_ERROR_OPERATION_FAILED;
118         }
119
120         /* init dynamic engine */
121         g_dynamic_engine.engine_uuid = NULL;
122         g_dynamic_engine.engine_name = NULL;
123         g_dynamic_engine.engine_path = NULL;
124
125         g_dynamic_engine.is_set = false;
126         g_dynamic_engine.is_loaded = false;
127         g_dynamic_engine.handle = NULL;
128         g_dynamic_engine.is_command_ready = false;
129         g_dynamic_engine.pefuncs = (vcpe_funcs_s*)calloc(1, sizeof(vcpe_funcs_s));
130         g_dynamic_engine.pdfuncs = (vcpd_funcs_s*)calloc(1, sizeof(vcpd_funcs_s));
131
132         g_agent_init = true;
133
134         //g_pre_result_cb = pre_result_cb;
135         g_asr_result_cb = asr_result_cb;
136         g_result_cb = result_cb;
137         //g_nlu_result_cb = nlu_result_cb;
138         g_error_cb = error_cb;
139
140         if (0 != vcd_config_get_default_language(&g_default_lang)) {
141                 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] There is No default voice in config");
142                 /* Set default voice */
143                 g_default_lang = strdup(VC_BASE_LANGUAGE);
144         }
145
146         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine Agent Initialize");
147
148         return 0;
149 }
150
151 int vcd_engine_agent_release()
152 {
153         if (false == g_agent_init) {
154                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
155                 return VCD_ERROR_OPERATION_FAILED;
156         }
157
158         /* unload current engine */
159         vcd_engine_agent_unload_current_engine();
160
161         /* release engine list */
162         GList *iter = NULL;
163         vcengine_s *data = NULL;
164
165         if (g_list_length(g_engine_list) > 0) {
166                 /* Get a first item */
167                 iter = g_list_first(g_engine_list);
168
169                 while (NULL != iter) {
170                         /* Get handle data from list */
171                         data = iter->data;
172                         iter = g_list_remove(iter, data);
173                 }
174         }
175
176         g_list_free(iter);
177
178         /* release current engine data */
179         if (NULL != g_dynamic_engine.pefuncs) {
180                 free(g_dynamic_engine.pefuncs);
181                 g_dynamic_engine.pefuncs = NULL;
182         }
183         if (NULL != g_dynamic_engine.pdfuncs) {
184                 free(g_dynamic_engine.pdfuncs);
185                 g_dynamic_engine.pdfuncs = NULL;
186         }
187
188         g_agent_init = false;
189
190         //g_pre_result_cb = NULL;
191         g_asr_result_cb = NULL;
192         g_result_cb = NULL;
193         //g_nlu_result_cb = NULL;
194         g_error_cb = NULL;
195
196         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine Agent release");
197
198         return 0;
199 }
200
201 bool vcd_engine_is_available_engine()
202 {
203         if (true == g_dynamic_engine.is_loaded)
204                 return true;
205
206         return false;
207 }
208
209 int vcd_engine_agent_initialize_current_engine()
210 {
211         /* check agent init */
212         if (false == g_agent_init) {
213                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
214                 return VCD_ERROR_OPERATION_FAILED;
215         }
216
217         /* update engine list */
218         if (0 != __internal_update_engine_list()) {
219                 SLOG(LOG_ERROR, TAG_VCD, "[engine agent] vcd_engine_agent_init : __internal_update_engine_list : no engine error");
220                 return VCD_ERROR_ENGINE_NOT_FOUND;
221         }
222
223         /* check whether engine id is valid or not.*/
224         GList *iter = NULL;
225         vcengine_info_s *dynamic_engine = NULL;
226
227         if (g_list_length(g_engine_list) > 0) {
228                 /*Get a first item*/
229                 iter = g_list_first(g_engine_list);
230
231                 while (NULL != iter) {
232                         /*Get handle data from list*/
233                         dynamic_engine = iter->data;
234                         if (NULL != dynamic_engine) {
235                                 break;
236                         }
237
238                         /*Get next item*/
239                         iter = g_list_next(iter);
240                 }
241         } else {
242                 return VCD_ERROR_ENGINE_NOT_FOUND;
243         }
244
245         if (NULL == dynamic_engine) {
246                 return VCD_ERROR_ENGINE_NOT_FOUND;
247         } else {
248                 if (NULL != g_dynamic_engine.engine_uuid) {
249                         /* set data from g_engine_list */
250                         if (g_dynamic_engine.engine_uuid != NULL)       free(g_dynamic_engine.engine_uuid);
251                         if (g_dynamic_engine.engine_name != NULL)       free(g_dynamic_engine.engine_name);
252                         if (g_dynamic_engine.engine_path != NULL)       free(g_dynamic_engine.engine_path);
253                 }
254
255                 g_dynamic_engine.engine_uuid = g_strdup(dynamic_engine->engine_uuid);
256                 g_dynamic_engine.engine_name = g_strdup(dynamic_engine->engine_name);
257                 g_dynamic_engine.engine_path = g_strdup(dynamic_engine->engine_path);
258
259                 g_dynamic_engine.handle = NULL;
260                 g_dynamic_engine.is_loaded = false;
261                 g_dynamic_engine.is_set = true;
262
263                 SLOG(LOG_DEBUG, TAG_VCD, "-----");
264                 SLOG(LOG_DEBUG, TAG_VCD, " Dynamic engine uuid : %s", g_dynamic_engine.engine_uuid);
265                 SLOG(LOG_DEBUG, TAG_VCD, " Dynamic engine name : %s", g_dynamic_engine.engine_name);
266                 SLOG(LOG_DEBUG, TAG_VCD, " Dynamic engine path : %s", g_dynamic_engine.engine_path);
267                 SLOG(LOG_DEBUG, TAG_VCD, "-----");
268
269         }
270
271         return 0;
272 }
273
274 int __internal_check_engine_id(const char* engine_uuid)
275 {
276         if (NULL == engine_uuid) {
277                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
278                 return VCD_ERROR_INVALID_PARAMETER;
279         }
280
281         GList *iter = NULL;
282         vcengine_s *data = NULL;
283
284         if (0 < g_list_length(g_engine_list)) {
285                 /*Get a first item*/
286                 iter = g_list_first(g_engine_list);
287
288                 while (NULL != iter) {
289                         data = iter->data;
290
291                         if (0 == strncmp(engine_uuid, data->engine_uuid, strlen(data->engine_uuid))) {
292                                 return 0;
293                         }
294
295                         iter = g_list_next(iter);
296                 }
297         }
298
299         return -1;
300 }
301
302 void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* engine_setting, bool use_network, void* user_data)
303 {
304         vcengine_info_s* temp = (vcengine_info_s*)user_data;
305
306         temp->engine_uuid = g_strdup(engine_uuid);
307         temp->engine_name = g_strdup(engine_name);
308 }
309
310
311 int __internal_get_engine_info(const char* filepath, vcengine_info_s** info)
312 {
313         if (NULL == filepath || NULL == info) {
314                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
315                 return VCD_ERROR_INVALID_PARAMETER;
316         }
317
318         /* load engine */
319         char *error;
320         void* handle;
321
322         handle = dlopen(filepath, RTLD_LAZY);
323         if (!handle) {
324                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Invalid engine : %s", filepath);
325                 if ((error = dlerror()) != NULL) {
326                         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] %s", error);
327                 }
328                 return -1;
329         }
330
331         /* link engine to daemon */
332         dlsym(handle, "vcp_load_engine");
333         if ((error = dlerror()) != NULL) {
334                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Invalid engine. Fail to open vcp_load_engine : %s", filepath);
335                 dlclose(handle);
336                 return -1;
337         }
338
339         dlsym(handle, "vcp_unload_engine");
340         if ((error = dlerror()) != NULL) {
341                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Invalid engine. Fail to open vcp_unload_engine : %s", filepath);
342                 dlclose(handle);
343                 return -1;
344         }
345
346         int (*get_engine_info)(vcpe_engine_info_cb callback, void* user_data);
347
348         get_engine_info = (int (*)(vcpe_engine_info_cb, void*))dlsym(handle, "vcp_get_engine_info");
349         if (NULL != (error = dlerror()) || NULL == get_engine_info) {
350                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent WARNING] Invalid engine. Fail to open vcp_get_engine_info : %s", filepath);
351                 dlclose(handle);
352                 return -1;
353         }
354
355         vcengine_info_s* temp;
356         temp = (vcengine_info_s*)calloc(1, sizeof(vcengine_info_s));
357         if (NULL == temp) {
358                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
359                 dlclose(handle);
360                 return VCD_ERROR_OUT_OF_MEMORY;
361         }
362
363         /* get engine info */
364         if (0 != get_engine_info(__engine_info_cb, (void*)temp)) {
365                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get engine info from engine");
366                 dlclose(handle);
367                 free(temp);
368                 return -1;
369         }
370
371         /* close engine */
372         dlclose(handle);
373
374         temp->engine_path = g_strdup(filepath);
375
376         SLOG(LOG_DEBUG, TAG_VCD, "----- Valid Engine");
377         SLOG(LOG_DEBUG, TAG_VCD, "Engine uuid : %s", temp->engine_uuid);
378         SLOG(LOG_DEBUG, TAG_VCD, "Engine name : %s", temp->engine_name);
379         SLOG(LOG_DEBUG, TAG_VCD, "Engine path : %s", temp->engine_path);
380         SLOG(LOG_DEBUG, TAG_VCD, "-----");
381         SLOG(LOG_DEBUG, TAG_VCD, "  ");
382
383         *info = temp;
384
385         return 0;
386 }
387
388 int __internal_update_engine_list()
389 {
390         /* relsease engine list */
391         GList *iter = NULL;
392         vcengine_info_s *data = NULL;
393
394         if (0 < g_list_length(g_engine_list)) {
395                 /* Get a first item */
396                 iter = g_list_first(g_engine_list);
397
398                 while (NULL != iter) {
399                         /* Get handle data from list */
400                         data = iter->data;
401
402                         if (NULL != data) {
403                                 if (NULL != data->engine_uuid)          free(data->engine_uuid);
404                                 if (NULL != data->engine_path)          free(data->engine_path);
405                                 if (NULL != data->engine_name)          free(data->engine_name);
406
407                                 free(data);
408                         }
409
410                         g_engine_list = g_list_remove_link(g_engine_list, iter);
411                         iter = g_list_first(g_engine_list);
412                 }
413         }
414
415         /* Get file name from default engine directory */
416         DIR *dp = NULL;
417         struct dirent *dirp = NULL;
418
419         dp  = opendir(VC_DEFAULT_ENGINE);
420         if (NULL != dp) {
421                 do {
422                         dirp = readdir(dp);
423
424                         if (NULL != dirp) {
425                                 if (!strcmp(".", dirp->d_name) || !strcmp("..", dirp->d_name))
426                                         continue;
427
428                                 vcengine_info_s* info = NULL;
429                                 char* filepath = NULL;
430                                 int filesize = 0;
431
432                                 filesize = strlen(VC_DEFAULT_ENGINE) + strlen(dirp->d_name) + 5;
433                                 filepath = (char*)calloc(filesize, sizeof(char));
434
435                                 if (NULL != filepath) {
436                                         snprintf(filepath, filesize, "%s/%s", VC_DEFAULT_ENGINE, dirp->d_name);
437                                 } else {
438                                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Memory not enough!!");
439                                         continue;
440                                 }
441
442                                 /* get its info and update engine list */
443                                 if (0 == __internal_get_engine_info(filepath, &info)) {
444                                         /* add engine info to g_engine_list */
445                                         g_engine_list = g_list_append(g_engine_list, info);
446                                 }
447
448                                 if (NULL != filepath) {
449                                         free(filepath);
450                                         filepath = NULL;
451                                 }
452                         }
453                 } while (NULL != dirp);
454
455                 closedir(dp);
456         } else {
457                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent WARNING] Fail to open default directory");
458         }
459
460         if (0 >= g_list_length(g_engine_list)) {
461                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] No Engine");
462                 return VCD_ERROR_ENGINE_NOT_FOUND;
463         }
464
465         __log_enginelist();
466
467         return 0;
468 }
469
470
471 int __foreach_command(vcp_cmd_h vc_command, vcpd_foreach_command_cb callback, void* user_data)
472 {
473         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request foreach command from engine");
474         return vcd_client_foreach_command((client_foreach_command_cb)callback, user_data);
475 }
476
477 int __command_get_length(vcp_cmd_h vc_command)
478 {
479         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request command length from engine");
480         return vcd_client_get_length();
481 }
482
483 int __get_audio_type(char** audio_type)
484 {
485         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Request audio type");
486
487         return vcd_recorder_get(audio_type);
488 }
489
490 void __result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result, const char* non_fixed, const char* nlu_result, const char* msg, void *user_data)
491 {
492         SLOG(LOG_DEBUG, TAG_VCD, "[Engine agent] Event(%d), Count(%d) Text(%s) Nonfixed(%s) NLU result(%s) Msg(%s)", event, count, all_result, non_fixed, nlu_result, msg);
493
494         if (NULL != g_result_cb) {
495                 g_result_cb(event, result_id, count, all_result, non_fixed, nlu_result, msg, user_data);
496         } else {
497                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent ERROR] Result callback function is NOT valid");
498         }
499
500         return;
501 }
502
503 void __asr_result_cb(vcp_asr_result_event_e event, const char* asr_result, void *user_data)
504 {
505         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] ASR result -  Event(%d), Result(%s)", event, asr_result);
506
507         if (NULL != g_asr_result_cb) {
508                 g_asr_result_cb(event, asr_result, user_data);
509         } else {
510                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent ERROR] ASR result callback function is NOT valid");
511         }
512
513         return;
514 }
515
516 void __error_cb(vcp_error_e error, const char* msg, void *user_data)
517 {
518         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] ERROR(%d)", error);
519
520         if (NULL != g_error_cb) {
521                 g_error_cb(error, msg, user_data);
522         } else {
523                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent ERROR] Error callback function is NOT vaild");
524         }
525
526         return;
527 }
528
529 int __load_engine(vcengine_s* engine)
530 {
531         /* check whether current engine is loaded or not */
532         if (true == engine->is_loaded) {
533                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Engine has already been loaded ");
534                 return 0;
535         }
536
537         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Current engine path : %s", engine->engine_path);
538
539         /* open engine */
540         char *error;
541         engine->handle = dlopen(engine->engine_path, RTLD_LAZY);
542
543         if ((error = dlerror()) != NULL || !engine->handle) {
544                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get engine handle");
545                 return VCD_ERROR_OPERATION_FAILED;
546         }
547
548         engine->vcp_unload_engine = (int (*)())dlsym(engine->handle, "vcp_unload_engine");
549         if ((error = dlerror()) != NULL) {
550                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to link daemon to vcp_unload_engine()");
551                 dlclose(engine->handle);
552                 return VCD_ERROR_OPERATION_FAILED;
553         }
554
555         engine->vcp_load_engine = (int (*)(vcpd_funcs_s*, vcpe_funcs_s*))dlsym(engine->handle, "vcp_load_engine");
556         if (NULL != (error = dlerror()) || NULL == engine->vcp_load_engine) {
557                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to link daemon to vcp_load_engine()");
558                 dlclose(engine->handle);
559                 return VCD_ERROR_OPERATION_FAILED;
560         }
561
562         /* load engine */
563         engine->pdfuncs->version = 1;
564         engine->pdfuncs->size = sizeof(vcpd_funcs_s);
565
566         engine->pdfuncs->foreach_command = __foreach_command;
567         engine->pdfuncs->get_command_count = __command_get_length;
568         engine->pdfuncs->get_audio_type = __get_audio_type;
569
570         if (0 != engine->vcp_load_engine(engine->pdfuncs, engine->pefuncs)) {
571                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail vcp_load_engine()");
572                 dlclose(engine->handle);
573                 return VCD_ERROR_OPERATION_FAILED;
574         }
575
576         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] engine info : version(%d), size(%d)", engine->pefuncs->version, engine->pefuncs->size);
577
578         /* engine error check */
579         if (engine->pefuncs->size != sizeof(vcpe_funcs_s)) {
580                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not valid");
581                 return VCD_ERROR_OPERATION_FAILED;
582         }
583
584         /* Check all engine functions */
585         if (NULL == engine->pefuncs->initialize ||
586                 NULL == engine->pefuncs->deinitialize ||
587                 NULL == engine->pefuncs->get_recording_format ||
588                 NULL == engine->pefuncs->foreach_langs ||
589                 NULL == engine->pefuncs->is_lang_supported ||
590                 NULL == engine->pefuncs->set_result_cb ||
591                 NULL == engine->pefuncs->set_language ||
592                 NULL == engine->pefuncs->set_commands ||
593                 NULL == engine->pefuncs->unset_commands ||
594                 NULL == engine->pefuncs->start ||
595                 NULL == engine->pefuncs->set_recording ||
596                 NULL == engine->pefuncs->stop ||
597                 NULL == engine->pefuncs->cancel ||
598                 NULL == engine->pefuncs->set_asr_result_cb ||
599                 //NULL == engine->pefuncs->set_pre_result_cb ||
600                 NULL == engine->pefuncs->set_error_cb ||
601                 NULL == engine->pefuncs->set_domain ||
602                 NULL == engine->pefuncs->get_nlu_base_info ||
603                 //NULL == engine->pefuncs->set_nlu_result_cb ||
604                 NULL == engine->pefuncs->set_private_data ||
605                 NULL == engine->pefuncs->get_private_data ||
606                 NULL == engine->pefuncs->process_text ||
607                 NULL == engine->pefuncs->process_list_event ||
608                 NULL == engine->pefuncs->process_haptic_event) {
609                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] The current engine is NOT valid");
610                 return VCD_ERROR_OPERATION_FAILED;
611         }
612
613         /* initalize engine */
614         if (0 != engine->pefuncs->initialize()) {
615                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to initialize vc-engine");
616                 return VCD_ERROR_OPERATION_FAILED;
617         }
618
619         if (0 != engine->pefuncs->set_result_cb(__result_cb, NULL)) {
620                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set result callback of vc-engine");
621                 return VCD_ERROR_OPERATION_FAILED;
622         }
623
624         if (0 != engine->pefuncs->set_asr_result_cb(__asr_result_cb, NULL)) {
625                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set asr result callback of vc-engine");
626                 return VCD_ERROR_OPERATION_FAILED;
627         }
628
629         if (0 != engine->pefuncs->set_error_cb(__error_cb, NULL)) {
630                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set error callback of vc-engine");
631                 return VCD_ERROR_OPERATION_FAILED;
632         }
633
634         /* load engine */
635         if (true == engine->pefuncs->is_lang_supported(g_default_lang)) {
636                 if (0 != engine->pefuncs->set_language(g_default_lang)) {
637                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to load current engine");
638                         return VCD_ERROR_OPERATION_FAILED;
639                 }
640                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] The %s has been loaded !!!", engine->engine_name);
641                 engine->is_loaded = true;
642         } else {
643                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent WARNING] This engine do not support default language : lang(%s)", g_default_lang);
644                 engine->is_loaded = false;
645         }
646
647         return 0;
648 }
649
650 int vcd_engine_agent_load_current_engine()
651 {
652         if (false == g_agent_init) {
653                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
654                 return VCD_ERROR_OPERATION_FAILED;
655         }
656
657         if (true == g_dynamic_engine.is_set) {
658                 if (0 != __load_engine(&g_dynamic_engine)) {
659                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to load dynamic engine");
660
661                         /* need to initialize dynamic engine data */
662                         g_dynamic_engine.is_loaded = false;
663                 } else {
664                         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Load dynamic engine");
665                 }
666         }
667
668         return 0;
669 }
670
671 int vcd_engine_agent_unload_current_engine()
672 {
673         if (false == g_agent_init) {
674                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized ");
675                 return VCD_ERROR_OPERATION_FAILED;
676         }
677
678         if (true == g_dynamic_engine.is_set) {
679                 /* unload dynamic engine */
680                 if (true == g_dynamic_engine.is_loaded) {
681                         /* shutdown engine */
682                         g_dynamic_engine.pefuncs->deinitialize();
683                         g_dynamic_engine.vcp_unload_engine();
684                         dlclose(g_dynamic_engine.handle);
685                         g_dynamic_engine.handle = NULL;
686                         g_dynamic_engine.is_loaded = false;
687                 }
688         }
689         return 0;
690 }
691
692
693 /*
694 * VCS Engine Interfaces for client
695 */
696
697 int vcd_engine_set_commands()
698 {
699         if (false == g_agent_init) {
700                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
701                 return VCD_ERROR_OPERATION_FAILED;
702         }
703
704         int ret = -1;
705
706         if (true == g_dynamic_engine.is_loaded) {
707                 /* Set dynamic command */
708                 ret = g_dynamic_engine.pefuncs->set_commands((vcp_cmd_h)0);
709                 if (0 != ret) {
710                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent ERROR] Fail to set command of dynamic engine : error(%d)", ret);
711                         g_dynamic_engine.is_command_ready = false;
712                 } else {
713                         g_dynamic_engine.is_command_ready = true;
714                 }
715
716                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] set command");
717         } else {
718                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available");
719         }
720
721         return 0;
722 }
723
724 int vcd_engine_recognize_start(bool silence)
725 {
726         if (false == g_agent_init) {
727                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
728                 return VCD_ERROR_OPERATION_FAILED;
729         }
730
731         int ret = -1;
732         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] silence is %s", silence ? "true" : "false");
733
734         if (true == g_dynamic_engine.is_loaded && true == g_dynamic_engine.is_command_ready) {
735                 ret = g_dynamic_engine.pefuncs->start(silence);
736                 if (0 != ret) {
737                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent] Fail to start engine error(%d)", ret);
738                         return VCD_ERROR_OPERATION_FAILED;
739                 }
740         } else {
741                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Engine is not available (Cannot start)");
742                 return VCD_ERROR_OPERATION_FAILED;
743         }
744
745         SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent SUCCESS] Engine start");
746         return 0;
747 }
748
749 int vcd_engine_recognize_audio(const void* data, unsigned int length, vcp_speech_detect_e* speech_detected)
750 {
751         if (false == g_agent_init) {
752                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
753                 return VCD_ERROR_OPERATION_FAILED;
754         }
755
756         if (NULL == data) {
757                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
758                 return VCD_ERROR_INVALID_PARAMETER;
759         }
760
761         int ret = -1;
762
763         if (true == g_dynamic_engine.is_loaded) {
764                 ret = g_dynamic_engine.pefuncs->set_recording(data, length, speech_detected);
765                 if (0 != ret) {
766                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set recording dynamic engine error(%d)", ret);
767                         if (VCP_ERROR_OUT_OF_NETWORK == ret) {
768                                 return VCD_ERROR_TIMED_OUT;
769                         }
770                         return VCD_ERROR_OPERATION_FAILED;
771                 }
772         }
773
774         return 0;
775 }
776
777 int vcd_engine_recognize_stop()
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 (true == g_dynamic_engine.is_loaded) {
785                 int ret = -1;
786                 ret = g_dynamic_engine.pefuncs->stop();
787                 if (0 != ret) {
788                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to stop dynamic engine error(%d)", ret);
789                         return VCD_ERROR_OPERATION_FAILED;
790                 }
791         } else {
792                 SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Dynamic engine is not recording state");
793                 return VCD_ERROR_OPERATION_FAILED;
794         }
795
796         return 0;
797 }
798
799 int vcd_engine_recognize_cancel()
800 {
801         if (false == g_agent_init) {
802                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
803                 return VCD_ERROR_OPERATION_FAILED;
804         }
805
806         int ret = -1;
807         if (true == g_dynamic_engine.is_loaded) {
808                 ret = g_dynamic_engine.pefuncs->cancel();
809                 if (0 != ret) {
810                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to cancel dynamic engine error(%d)", ret);
811                         return VCD_ERROR_OPERATION_FAILED;
812                 }
813         }
814
815         return 0;
816 }
817
818 int vcd_engine_set_domain(int pid, const char* domain)
819 {
820         if (false == g_agent_init) {
821                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
822                 return VCD_ERROR_OPERATION_FAILED;
823         }
824
825         int ret = -1;
826         if (true == g_dynamic_engine.is_loaded) {
827                 ret = g_dynamic_engine.pefuncs->set_domain(domain);
828                 if (0 != ret) {
829                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set domain (%d)", ret);
830                         return VCD_ERROR_OPERATION_FAILED;
831                 }
832         }
833
834         return 0;
835 }
836
837 int vcd_engine_get_nlu_base_info(int pid, const char* key, char** value)
838 {
839         if (NULL == value) {
840                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
841                 return VCD_ERROR_INVALID_PARAMETER;
842         }
843
844         if (false == g_agent_init) {
845                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
846                 return VCD_ERROR_OPERATION_FAILED;
847         }
848
849         int ret = -1;
850         if (true == g_dynamic_engine.is_loaded) {
851                 ret = g_dynamic_engine.pefuncs->get_nlu_base_info(key, value);
852                 if (0 != ret) {
853                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get nlu base info (%d)", ret);
854                         return VCD_ERROR_OPERATION_FAILED;
855                 }
856         }
857
858         return 0;
859 }
860
861 int vcd_engine_set_private_data(int pid, const char* key, const char* data)
862 {
863         if (false == g_agent_init) {
864                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
865                 return VCD_ERROR_OPERATION_FAILED;
866         }
867
868         int ret = -1;
869         if (true == g_dynamic_engine.is_loaded) {
870                 ret = g_dynamic_engine.pefuncs->set_private_data(key, data);
871                 if (0 != ret) {
872                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to set private data (%d)", ret);
873                         return VCD_ERROR_OPERATION_FAILED;
874                 }
875         }
876
877         return 0;
878 }
879
880 int vcd_engine_get_private_data(int pid, const char* key, char** data)
881 {
882         if (NULL == data) {
883                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
884                 return VCD_ERROR_INVALID_PARAMETER;
885         }
886
887         if (false == g_agent_init) {
888                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
889                 return VCD_ERROR_OPERATION_FAILED;
890         }
891
892         int ret = -1;
893         if (true == g_dynamic_engine.is_loaded) {
894                 ret = g_dynamic_engine.pefuncs->get_private_data(key, data);
895                 if (0 != ret) {
896                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to get private data (%d)", ret);
897                         return VCD_ERROR_OPERATION_FAILED;
898                 }
899         }
900
901         return 0;
902 }
903
904 int vcd_engine_process_text(int pid, const char* text)
905 {
906         if (false == g_agent_init) {
907                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
908                 return VCD_ERROR_OPERATION_FAILED;
909         }
910
911         int ret = -1;
912         if (true == g_dynamic_engine.is_loaded) {
913                 ret = g_dynamic_engine.pefuncs->process_text(text);
914                 if (0 != ret) {
915                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process text (%d)", ret);
916                         return VCD_ERROR_OPERATION_FAILED;
917                 }
918         }
919
920         return 0;
921 }
922
923 int vcd_engine_process_list_event(int pid, const char* event)
924 {
925         if (false == g_agent_init) {
926                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
927                 return VCD_ERROR_OPERATION_FAILED;
928         }
929
930         int ret = -1;
931         if (true == g_dynamic_engine.is_loaded) {
932                 ret = g_dynamic_engine.pefuncs->process_list_event(event);
933                 if (0 != ret) {
934                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process list event (%d)", ret);
935                         return VCD_ERROR_OPERATION_FAILED;
936                 }
937         }
938
939         return 0;
940 }
941
942 int vcd_engine_process_haptic_event(int pid, const char* event)
943 {
944         if (false == g_agent_init) {
945                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
946                 return VCD_ERROR_OPERATION_FAILED;
947         }
948
949         int ret = -1;
950         if (true == g_dynamic_engine.is_loaded) {
951                 ret = g_dynamic_engine.pefuncs->process_haptic_event(event);
952                 if (0 != ret) {
953                         SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Fail to process haptic event (%d)", ret);
954                         return VCD_ERROR_OPERATION_FAILED;
955                 }
956         }
957
958         return 0;
959 }
960
961 /*
962 * VCS Engine Interfaces for client and setting
963 */
964
965 int vcd_engine_get_audio_format(const char* audio_id, vcp_audio_type_e* types, int* rate, int* channels)
966 {
967         if (false == g_agent_init) {
968                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
969                 return VCD_ERROR_OPERATION_FAILED;
970         }
971
972         if (true != g_dynamic_engine.is_loaded) {
973                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
974         }
975
976         if (NULL == g_dynamic_engine.pefuncs->get_recording_format) {
977                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] The function of engine is NULL!!");
978                 return VCD_ERROR_OPERATION_FAILED;
979         }
980
981         int ret = g_dynamic_engine.pefuncs->get_recording_format(audio_id, types, rate, channels);
982         if (0 != ret) {
983                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get recording format(%d)", ret);
984                 return VCD_ERROR_OPERATION_FAILED;
985         }
986
987         return 0;
988 }
989
990 bool __supported_language_cb(const char* language, void* user_data)
991 {
992         GList** lang_list = (GList**)user_data;
993
994         if (NULL == language || NULL == lang_list) {
995                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Input parameter is NULL in callback!!!!");
996                 return false;
997         }
998
999         SLOG(LOG_DEBUG, TAG_VCD, "-- Language(%s)", language);
1000
1001         char* temp_lang = g_strdup(language);
1002
1003         *lang_list = g_list_append(*lang_list, temp_lang);
1004
1005         return true;
1006 }
1007
1008 int vcd_engine_supported_langs(GList** lang_list)
1009 {
1010         if (false == g_agent_init) {
1011                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
1012                 return VCD_ERROR_OPERATION_FAILED;
1013         }
1014
1015         if (true != g_dynamic_engine.is_loaded) {
1016                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Engine is not loaded");
1017         }
1018
1019         if (NULL == g_dynamic_engine.pefuncs->foreach_langs) {
1020                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] The function of engine is NULL!!");
1021                 return VCD_ERROR_OPERATION_FAILED;
1022         }
1023
1024         int ret = g_dynamic_engine.pefuncs->foreach_langs(__supported_language_cb, (void*)lang_list);
1025         if (0 != ret) {
1026                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] get language list error(%d)", ret);
1027                 return VCD_ERROR_OPERATION_FAILED;
1028         }
1029
1030         return 0;
1031 }
1032
1033
1034 int vcd_engine_get_current_language(char** lang)
1035 {
1036         if (false == g_agent_init) {
1037                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
1038                 return VCD_ERROR_OPERATION_FAILED;
1039         }
1040
1041         if (NULL == lang) {
1042                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
1043                 return VCD_ERROR_INVALID_PARAMETER;
1044         }
1045
1046         /* get default language */
1047         *lang = g_strdup(g_default_lang);
1048
1049         return 0;
1050 }
1051
1052 int vcd_engine_set_current_language(const char* language)
1053 {
1054         if (false == g_agent_init) {
1055                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Not Initialized");
1056                 return VCD_ERROR_OPERATION_FAILED;
1057         }
1058
1059         if (NULL == language) {
1060                 SLOG(LOG_ERROR, TAG_VCD, "[Engine Agent ERROR] Invalid Parameter");
1061                 return VCD_ERROR_INVALID_PARAMETER;
1062         }
1063
1064         int ret;
1065
1066         if (true == g_dynamic_engine.is_loaded) {
1067                 g_dynamic_engine.is_command_ready = false;
1068
1069                 ret = g_dynamic_engine.pefuncs->set_language(language);
1070                 if (0 != ret) {
1071                         SLOG(LOG_WARN, TAG_VCD, "[Engine Agent] Fail to set language of dynamic engine error(%d, %s)", ret, language);
1072                 }
1073         } else {
1074                 SLOG(LOG_DEBUG, TAG_VCD, "[Engine Agent] Dynamic engine is not available (Cannot start)");
1075         }
1076
1077         return 0;
1078 }
1079
1080 void __free_language_list(GList* lang_list)
1081 {
1082         GList *iter = NULL;
1083         char* data = NULL;
1084
1085         /* if list have item */
1086         if (g_list_length(lang_list) > 0) {
1087                 /* Get a first item */
1088                 iter = g_list_first(lang_list);
1089
1090                 while (NULL != iter) {
1091                         data = iter->data;
1092
1093                         if (NULL != data)
1094                                 free(data);
1095
1096                         lang_list = g_list_remove_link(lang_list, iter);
1097
1098                         iter = g_list_first(lang_list);
1099                 }
1100         }
1101 }
1102
1103 int __log_enginelist()
1104 {
1105         GList *iter = NULL;
1106         vcengine_info_s *data = NULL;
1107
1108         if (0 < g_list_length(g_engine_list)) {
1109
1110                 /* Get a first item */
1111                 iter = g_list_first(g_engine_list);
1112
1113                 SLOG(LOG_DEBUG, TAG_VCD, "--------------- engine list -------------------");
1114
1115                 int i = 1;
1116                 while (NULL != iter) {
1117                         /* Get handle data from list */
1118                         data = iter->data;
1119
1120                         SLOG(LOG_DEBUG, TAG_VCD, "[%dth]", i);
1121                         SLOG(LOG_DEBUG, TAG_VCD, "  engine uuid : %s", data->engine_uuid);
1122                         SLOG(LOG_DEBUG, TAG_VCD, "  engine name : %s", data->engine_name);
1123                         SLOG(LOG_DEBUG, TAG_VCD, "  engine path : %s", data->engine_path);
1124                         iter = g_list_next(iter);
1125                         i++;
1126                 }
1127                 SLOG(LOG_DEBUG, TAG_VCD, "----------------------------------------------");
1128         } else {
1129                 SLOG(LOG_DEBUG, TAG_VCD, "-------------- engine list -------------------");
1130                 SLOG(LOG_DEBUG, TAG_VCD, "  No Engine in engine directory");
1131                 SLOG(LOG_DEBUG, TAG_VCD, "----------------------------------------------");
1132         }
1133
1134         return 0;
1135 }
1136
1137
1138
1139