Merge "Add more logs for checking wakeup process" into tizen
[platform/core/uifw/multi-assistant-service.git] / src / multi_assistant_service_plugin.c
1 /*
2  * Copyright 2018  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <tizen.h>
18 #include <service_app.h>
19 #include <app_manager.h>
20 #include <app.h>
21 #include <malloc.h>
22 #include <Ecore.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <dlfcn.h>
29
30 #include "multi_wakeup_recognizer.h"
31 #include "multi_assistant_main.h"
32 #include "multi_assistant_service.h"
33 #include "multi_assistant_service_plugin.h"
34 #include "multi_assistant_dbus.h"
35 #include "multi_assistant_common.h"
36
37 /* Sound buf save for test */
38 #if 1
39 #define BUF_SAVE_MODE
40 #endif
41
42 #ifdef BUF_SAVE_MODE
43 static char g_temp_file_name[128] = {'\0',};
44
45 static FILE* g_pFile = NULL;
46
47 static int g_count = 1;
48 #endif
49
50 static void *g_handle = NULL;
51
52 static wakeup_manager_interface _wakeup_manager_interface = { NULL, };
53 static ma_plugin_settings* g_plugin_settings = NULL;
54
55 static bool is_ui_panel_enabled()
56 {
57         /* By default we assume the ui panel is always enabled unless explicitly turned off */
58         bool ret = true;
59         if (g_plugin_settings) {
60                 ret = g_plugin_settings->ui_panel_enabled;
61         }
62         MAS_LOGD("UI Panel Enabled : %d", ret);
63         return ret;
64 }
65
66 #if 0 /* + TEST_CODE */
67 Eina_Bool __send_asr_result(void *data)
68 {
69         MAS_LOGD("[ENTER]");
70
71         if (!strcmp((char*)data, "Today's")) {
72                 masc_ui_dbus_send_asr_result(-1, 1, "Today's");
73         }
74         if (!strcmp((char*)data, "weather.")) {
75                 masc_ui_dbus_send_asr_result(-1, 0, "Today's weather.");
76         }
77
78         MAS_LOGD("END");
79         return EINA_FALSE;
80 }
81
82 Eina_Bool __send_result(void *data)
83 {
84         MAS_LOGD("[ENTER]");
85
86         int ret = masc_ui_dbus_send_result(-1, (const char*)data, (const char*)data, "test");
87         if (0 != ret) {
88                 MAS_LOGE("[ERROR] Fail to stop recording(%d)", ret);
89                 return EINA_TRUE;
90         }
91
92         MAS_LOGD("END");
93         return EINA_FALSE;
94 }
95 #endif /* -TEST_CODE */
96
97 Eina_Bool __launch_assistant_by_wakeup_appid_timer(char* appid)
98 {
99         MAS_LOGD("[ENTER] appid(%s)", appid);
100
101         int pid = -1;
102         if (!appid) return ECORE_CALLBACK_CANCEL;
103
104         bool use_custom_ui = mas_get_client_custom_ui_option_by_appid(appid);
105         bool ui_panel_enabled = is_ui_panel_enabled();
106         if (ui_panel_enabled) masc_ui_dbus_enable_common_ui(!use_custom_ui);
107
108         mas_set_current_client_by_appid(appid);
109         if (ui_panel_enabled) masc_ui_dbus_change_assistant(appid);
110         if ((pid = mas_get_client_pid_by_appid(appid)) != -1) {
111                 masc_dbus_active_state_change(pid, MA_ACTIVE_STATE_ACTIVE);
112                 /* Bring MA client to foreground - is there a better way instead of launching? */
113                 mas_launch_client_by_appid(appid);
114         } else {
115                 // Appropriate MA Client not available, trying to launch new one
116                 MAS_LOGD("MA Client with appid %s does not exist, launching client", appid);
117                 mas_launch_client_by_appid(appid);
118         }
119
120         if (appid) free(appid);
121
122         MAS_LOGD("END");
123         return ECORE_CALLBACK_CANCEL;
124 }
125
126 Eina_Bool __launch_assistant_by_wakeup_word_timer(char* wakeup_word)
127 {
128         MAS_LOGD("[ENTER]");
129
130         if (!wakeup_word) return EINA_FALSE;
131
132         const char* appid = mas_get_client_appid_by_wakeup_word(wakeup_word);
133         __launch_assistant_by_wakeup_appid_timer(strdup(appid));
134
135         if (wakeup_word) free(wakeup_word);
136
137         MAS_LOGD("END");
138         return ECORE_CALLBACK_CANCEL;
139 }
140
141 static void __wakeup_event_cb(wakeup_event_info event, void* user_data)
142 {
143         MAS_LOGD("[SUCCESS] __wakeup_event_cb is called, wakeup_word(%s)", event.wakeup_word);
144         int ret = -1;
145
146         if (is_ui_panel_enabled()) {
147                 int retry_cnt = 0;
148                 while (0 != ret) {
149                         ret = masc_ui_dbus_send_hello();
150                         retry_cnt++;
151                         if (5 < retry_cnt) {
152                                 MAS_LOGE("[ERROR] Fail to receive reply for hello, ret(%d)", ret);
153                                 break;
154                         }
155                 }
156         }
157
158 #ifdef BUF_SAVE_MODE
159         if (g_pFile) {
160                 fclose(g_pFile);
161                 g_pFile = NULL;
162         } else {
163                 MAS_LOGD("[Recorder Info] File not found!");
164         }
165
166         while (1) {
167                 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/ma_service_%d_%d", getpid(), g_count);
168                 int ret = access(g_temp_file_name, 0);
169
170                 if (0 == ret) {
171                         MAS_LOGD("[Recorder ERROR] File is already exist");
172                         if (0 == remove(g_temp_file_name)) {
173                                 MAS_LOGD("[Recorder] Remove file");
174                                 break;
175                         } else {
176                                 g_count++;
177                         }
178                 } else {
179                         break;
180                 }
181         }
182
183         MAS_LOGD("[Recorder] Temp file name=[%s]", g_temp_file_name);
184
185         /* open test file */
186         g_pFile = fopen(g_temp_file_name, "wb+x");
187         if (!g_pFile) {
188                 MAS_LOGD("[Recorder ERROR] File not found!");
189                 return;
190         }
191         g_count++;
192 #endif
193
194 #if 0 /* + TEST_CODE */
195         if (WAKEUP_EVENT_SUCCESS == event) {
196                 ecore_thread_main_loop_begin();
197                 ecore_timer_add(1.0, __send_asr_result, "Today's");
198                 ecore_thread_main_loop_end();
199
200                 ecore_thread_main_loop_begin();
201                 ecore_timer_add(2.0, __send_asr_result, "weather.");
202                 ecore_thread_main_loop_end();
203
204                 ecore_thread_main_loop_begin();
205                 ecore_timer_add(3.0, __send_result, "Partly cloudy with temperatures from 75 to 88");
206                 ecore_thread_main_loop_end();
207         }
208 #endif /* - TEST_CODE */
209         if (event.wakeup_appid) {
210                 ecore_thread_main_loop_begin();
211                 ecore_timer_add(0.0f, __launch_assistant_by_wakeup_appid_timer, (void*)strdup(event.wakeup_appid));
212                 ecore_thread_main_loop_end();
213         } else if (event.wakeup_word) {
214                 ecore_thread_main_loop_begin();
215                 ecore_timer_add(0.0f, __launch_assistant_by_wakeup_word_timer, (void*)strdup(event.wakeup_word));
216                 ecore_thread_main_loop_end();
217         }
218
219 }
220
221 static bool __validate_streaming_event_order(int pid, wakeup_speech_streaming_event_e *event)
222 {
223         bool ret = false;
224
225         static int previous_pid = -1;
226         static wakeup_speech_streaming_event_e previous_event = WAKEUP_SPEECH_STREAMING_EVENT_FINISH;
227
228         if (NULL == event) return false;
229
230         wakeup_speech_streaming_event_e expected_sequence [][2] = {
231                 {WAKEUP_SPEECH_STREAMING_EVENT_START, WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE},
232                 {WAKEUP_SPEECH_STREAMING_EVENT_START, WAKEUP_SPEECH_STREAMING_EVENT_FINISH},
233                 {WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE, WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE},
234                 {WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE, WAKEUP_SPEECH_STREAMING_EVENT_FINISH},
235                 {WAKEUP_SPEECH_STREAMING_EVENT_FINISH, WAKEUP_SPEECH_STREAMING_EVENT_START},
236         };
237
238         if (pid != previous_pid) {
239                 /* When sending streaming event to a new client, it always sends START message first */
240                 previous_event = WAKEUP_SPEECH_STREAMING_EVENT_FINISH;
241         }
242
243         for (int loop = 0;loop < sizeof(expected_sequence) / sizeof(expected_sequence[0]);loop++) {
244                 if (previous_event == expected_sequence[loop][0] &&
245                         *event == expected_sequence[loop][1]) {
246                         ret = true;
247                 }
248         }
249         if (!ret) {
250                 /* In case of FINISH -> CONTINUE without START, simply modify current event value */
251                 if (WAKEUP_SPEECH_STREAMING_EVENT_FINISH == previous_event &&
252                         WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE == *event) {
253                         *event = WAKEUP_SPEECH_STREAMING_EVENT_START;
254                         ret = true;
255
256                         MAS_LOGD("[WARNING] forcibly changed CONTINUE to START : %d -> %d (PID %d -> %d)",
257                                 previous_event, *event, previous_pid, pid);
258                 }
259         }
260
261         if (ret) {
262                 previous_pid = pid;
263                 previous_event = *event;
264         } else {
265                 MAS_LOGE("[ERROR] State sequence validation failed : %d -> %d (PID %d -> %d)",
266                         previous_event, *event, previous_pid, pid);
267         }
268         return ret;
269 }
270
271 static Eina_Bool handle_speech_streaming_event_failure(void *data)
272 {
273         mas_client_send_recognition_result(0, MA_RECOGNITION_RESULT_EVENT_ERROR);
274     return ECORE_CALLBACK_CANCEL;
275 }
276
277 static void __audio_streaming_cb(wakeup_speech_streaming_event_e event, unsigned char* buffer, int len, void *user_data)
278 {
279         if (event == WAKEUP_SPEECH_STREAMING_EVENT_FAIL) {
280                 ecore_thread_main_loop_begin();
281                 ecore_timer_add(0.0f, handle_speech_streaming_event_failure, NULL);
282                 ecore_thread_main_loop_begin();
283                 return;
284         }
285         static int count = 0;
286         if (event != WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE || count % 100 == 0) {
287                 MAS_LOGD( "[SUCCESS] __audio_streaming_cb is called, event(%d), buffer(%p), len(%d)", event, buffer, len);
288         }
289         ++count;
290
291         int pid = mas_get_current_client_pid();
292         if (pid == -1) {
293                 MAS_LOGE("[ERROR] Fail to retrieve pid of current MA client");
294         } else {
295                 if (__validate_streaming_event_order(pid, &event)) {
296                         int ret = masc_dbus_send_streaming_audio_data(pid, event, buffer, len);
297                         if (0 != ret) {
298                                 MAS_LOGE("[ERROR] Fail to send speech data, ret(%d)", ret);
299                         }
300                 }
301         }
302
303 #ifdef BUF_SAVE_MODE
304         /* write pcm buffer */
305         if (g_pFile)
306                 fwrite(buffer, 1, len, g_pFile);
307
308         if (WAKEUP_SPEECH_STREAMING_EVENT_FINISH == event) {
309                 if (g_pFile) {
310                         MAS_LOGE("[Recorder SUCCESS] File Close");
311                         fclose(g_pFile);
312                         g_pFile = NULL;
313                 } else {
314                         MAS_LOGE("[Recorder ERROR] File not found!");
315                 }
316         }
317 #endif
318 }
319
320 static void __speech_status_cb(wakeup_speech_status_e status, void *user_data)
321 {
322         MAS_LOGD( "[SUCCESS] __speech_status_cb is called, status(%d)", status);
323 }
324
325 static void __error_cb(int error, const char* err_msg, void* user_data)
326 {
327         MAS_LOGD( "[SUCCESS] __error_cb is called, error(%d), err_msg(%s)", error, err_msg);
328
329         if (is_ui_panel_enabled()) {
330                 int ret = masc_ui_dbus_send_error_message(error, err_msg);
331                 if (0 != ret) {
332                         MAS_LOGE("[ERROR] Fail to send error message, ret(%d)", ret);
333                 }
334         }
335 }
336
337 int multi_assistant_service_plugin_initialize(void)
338 {
339         MAS_LOGD( "[Enter]");
340
341         char filepath[512] = {'\0',};
342         const char *default_engine_path = MA_WAKEUP_MANAGER_PATH;
343         snprintf(filepath, 512, "%s/%s", default_engine_path, MA_DEFAULT_WAKEUP_MANAGER_FILENAME);
344
345         char *error;
346         g_handle = NULL;
347         g_handle = dlopen(filepath, RTLD_LAZY);
348         if (NULL != (error = dlerror())) {
349                 MAS_LOGE("[ERROR] Fail to dlopen(%s), error(%s)", filepath, error);
350                 return -1; //MAS_ERROR_OPERATION_FAILED;
351         }
352
353         _wakeup_manager_interface.initialize =
354                 (wakeup_manager_initialize)dlsym(g_handle,
355                 MA_WAKEUP_MANAGER_FUNC_INITIALIZE);
356         _wakeup_manager_interface.deinitialize =
357                 (wakeup_manager_deinitialize)dlsym(g_handle,
358                 MA_WAKEUP_MANAGER_FUNC_DEINITIALIZE);
359         _wakeup_manager_interface.get_settings =
360                 (wakeup_manager_get_settings)dlsym(g_handle,
361                 MA_WAKEUP_MANAGER_FUNC_GET_SETTINGS);
362         _wakeup_manager_interface.add_assistant_wakeup_word =
363                 (wakeup_manager_add_assistant_wakeup_word)dlsym(g_handle,
364                 MA_WAKEUP_MANAGER_FUNC_ADD_ASSISTANT_WAKEUP_WORD);
365         _wakeup_manager_interface.add_assistant_language =
366                 (wakeup_manager_add_assistant_language)dlsym(g_handle,
367                 MA_WAKEUP_MANAGER_FUNC_ADD_ASSISTANT_LANGUAGE);
368         _wakeup_manager_interface.set_assistant_wakeup_engine =
369                 (wakeup_manager_set_assistant_wakeup_engine)dlsym(g_handle,
370                 MA_WAKEUP_MANAGER_FUNC_SET_ASSISTANT_WAKEUP_ENGINE);
371         _wakeup_manager_interface.set_language =
372                 (wakeup_manager_set_language)dlsym(g_handle,
373                 MA_WAKEUP_MANAGER_FUNC_SET_LANGUAGE);
374         _wakeup_manager_interface.activate =
375                 (wakeup_manager_activate)dlsym(g_handle,
376                 MA_WAKEUP_MANAGER_FUNC_ACTIVATE);
377         _wakeup_manager_interface.deactivate =
378                 (wakeup_manager_deactivate)dlsym(g_handle,
379                 MA_WAKEUP_MANAGER_FUNC_DEACTIVATE);
380         _wakeup_manager_interface.update_voice_feedback_state =
381                 (wakeup_manager_update_voice_feedback_state)dlsym(g_handle,
382                 MA_WAKEUP_MANAGER_FUNC_UPDATE_VOICE_FEEDBACK_STATE);
383         _wakeup_manager_interface.send_assistant_specific_command =
384                 (wakeup_manager_send_assistant_specific_command)dlsym(g_handle,
385                 MA_WAKEUP_MANAGER_FUNC_SEND_ASSISTANT_SPECIFIC_COMMAND);
386         _wakeup_manager_interface.change_system_volume =
387                 (wakeup_manager_change_system_volume)dlsym(g_handle,
388                 MA_WAKEUP_MANAGER_FUNC_CHANGE_SYSTEM_VOLUME);
389         _wakeup_manager_interface.update_result_state =
390                 (wakeup_manager_update_result_state)dlsym(g_handle,
391                 MA_WAKEUP_MANAGER_FUNC_UPDATE_RESULT_STATE);
392         _wakeup_manager_interface.process_event =
393                 (wakeup_manager_process_event)dlsym(g_handle,
394                 MA_WAKEUP_MANAGER_FUNC_PROCESS_EVENT);
395         _wakeup_manager_interface.start_streaming_utterance_data =
396                 (wakeup_manager_start_streaming_utterance_data)dlsym(g_handle,
397                 MA_WAKEUP_MANAGER_FUNC_START_STREAMING_UTTERANCE_DATA);
398         _wakeup_manager_interface.stop_streaming_utterance_data =
399                 (wakeup_manager_stop_streaming_utterance_data)dlsym(g_handle,
400                 MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_UTTERANCE_DATA);
401         _wakeup_manager_interface.start_streaming_previous_utterance_data =
402                 (wakeup_manager_start_streaming_previous_utterance_data)dlsym(g_handle,
403                 MA_WAKEUP_MANAGER_FUNC_START_STREAMING_PREVIOUS_UTTERANCE_DATA);
404         _wakeup_manager_interface.stop_streaming_previous_utterance_data =
405                 (wakeup_manager_stop_streaming_previous_utterance_data)dlsym(g_handle,
406                 MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_PREVIOUS_UTTERANCE_DATA);
407         _wakeup_manager_interface.start_streaming_follow_up_data =
408                 (wakeup_manager_start_streaming_follow_up_data)dlsym(g_handle,
409                 MA_WAKEUP_MANAGER_FUNC_START_STREAMING_FOLLOW_UP_DATA);
410         _wakeup_manager_interface.stop_streaming_follow_up_data =
411                 (wakeup_manager_stop_streaming_follow_up_data)dlsym(g_handle,
412                 MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_FOLLOW_UP_DATA);
413         _wakeup_manager_interface.get_audio_format =
414                 (wakeup_manager_get_audio_format)dlsym(g_handle,
415                 MA_WAKEUP_MANAGER_FUNC_GET_AUDIO_FORMAT);
416         _wakeup_manager_interface.get_audio_source_type =
417                 (wakeup_manager_get_audio_source_type)dlsym(g_handle,
418                 MA_WAKEUP_MANAGER_FUNC_GET_AUDIO_SOURCE_TYPE);
419         _wakeup_manager_interface.set_wakeup_event_callback =
420                 (wakeup_manager_set_wakeup_event_callback)dlsym(g_handle,
421                 MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_EVENT_CALLBACK);
422         _wakeup_manager_interface.set_utterance_streaming_callback =
423                 (wakeup_manager_set_utterance_streaming_callback)dlsym(g_handle,
424                 MA_WAKEUP_MANAGER_FUNC_SET_UTTERANCE_STREAMING_CALLBACK);
425         _wakeup_manager_interface.set_previous_utterance_streaming_callback =
426                 (wakeup_manager_set_previous_utterance_streaming_callback)dlsym(g_handle,
427                 MA_WAKEUP_MANAGER_FUNC_SET_PREVIOUS_UTTERANCE_STREAMING_CALLBACK);
428         _wakeup_manager_interface.set_follow_up_streaming_callback =
429                 (wakeup_manager_set_follow_up_streaming_callback)dlsym(g_handle,
430                 MA_WAKEUP_MANAGER_FUNC_SET_FOLLOW_UP_STREAMING_CALLBACK);
431         _wakeup_manager_interface.set_speech_status_callback =
432                 (wakeup_manager_set_speech_status_callback)dlsym(g_handle,
433                 MA_WAKEUP_MANAGER_FUNC_SET_SPEECH_STATUS_CALLBACK);
434         _wakeup_manager_interface.set_error_callback =
435                 (wakeup_manager_set_error_callback)dlsym(g_handle,
436                 MA_WAKEUP_MANAGER_FUNC_SET_ERROR_CALLBACK);
437
438         int ret = -1;
439         if (NULL != g_handle) {
440                 wakeup_manager_initialize func = _wakeup_manager_interface.initialize;
441
442                 if (NULL == func) {
443                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_INITIALIZE);
444                 } else {
445                         ret = func();
446                         if (0 != ret) {
447                                 MAS_LOGE("[ERROR] Fail to initialize, ret(%d)", ret);
448                         }
449                 }
450
451                 wakeup_manager_get_settings get_settings_func = _wakeup_manager_interface.get_settings;
452
453                 if (NULL == get_settings_func) {
454                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_GET_SETTINGS);
455                 } else {
456                         size_t struct_size;
457                         ret = get_settings_func(&g_plugin_settings, &struct_size);
458                         if (0 != ret || struct_size != sizeof(ma_plugin_settings)) {
459                                 MAS_LOGE("[ERROR] Fail to get settings, ret(%d), size %zu", ret, struct_size);
460                                 g_plugin_settings = NULL;
461                         }
462                 }
463         } else {
464                 MAS_LOGE("[ERROR] g_handle is not valid");
465         }
466         return ret;
467 }
468
469 int multi_assistant_service_plugin_deinitialize(void)
470 {
471 #ifdef BUF_SAVE_MODE
472         if (g_pFile) {
473                 fclose(g_pFile);
474                 g_pFile = NULL;
475         } else {
476                 MAS_LOGD("[Recorder ERROR] File not found!");
477         }
478 #endif
479         int ret = -1;
480         if (NULL != g_handle) {
481                 wakeup_manager_deinitialize func = _wakeup_manager_interface.deinitialize;
482                 if (NULL == func) {
483                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_DEINITIALIZE);
484                 } else {
485                         ret = func();
486                         if (0 != ret) {
487                                 MAS_LOGE("[ERROR] Fail to deinitialize, ret(%d)", ret);
488                         }
489                 }
490
491                 dlclose(g_handle);
492                 g_handle = NULL;
493         } else {
494                 MAS_LOGE("[ERROR] g_handle is not valid");
495         }
496
497         return ret;
498 }
499
500 int multi_assistant_service_plugin_get_settings(ma_plugin_settings **settings, size_t *struct_size)
501 {
502         int ret = -1;
503         if (NULL != g_handle) {
504                 wakeup_manager_get_settings func = _wakeup_manager_interface.get_settings;
505                 if (NULL == func) {
506                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_GET_SETTINGS);
507                 } else {
508                         ret = func(settings, struct_size);
509                         if (0 != ret) {
510                                 MAS_LOGE("[ERROR] Fail to get settings, ret(%d)", ret);
511                         }
512                 }
513         } else {
514                 MAS_LOGE("[ERROR] g_handle is not valid");
515         }
516         return ret;
517 }
518
519 int multi_assistant_service_plugin_set_language(const char* language)
520 {
521         int ret = -1;
522         if (NULL != g_handle) {
523                 wakeup_manager_set_language func = _wakeup_manager_interface.set_language;
524                 if (NULL == func) {
525                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_LANGUAGE);
526                 } else {
527                         ret = func(language);
528                         if (0 != ret) {
529                                 MAS_LOGE("[ERROR] Fail to set langauge(%s), ret(%d)", language, ret);
530                         }
531                 }
532         } else {
533                 MAS_LOGE("[ERROR] g_handle is not valid");
534         }
535         return ret;
536 }
537
538 int multi_assistant_service_plugin_add_assistant_wakeup_word(const char* appid, const char* wakeup_word, const char* language)
539 {
540         int ret = -1;
541         if (NULL != g_handle) {
542                 wakeup_manager_add_assistant_wakeup_word func = _wakeup_manager_interface.add_assistant_wakeup_word;
543                 if (NULL == func) {
544                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_ADD_ASSISTANT_WAKEUP_WORD);
545                 } else {
546                         ret = func(appid, wakeup_word, language);
547                         if (0 != ret) {
548                                 MAS_LOGE("[ERROR] Fail to set wakeup word(%s)(%s)(%s), ret(%d)", appid, wakeup_word, language, ret);
549                         }
550                 }
551         } else {
552                 MAS_LOGE("[ERROR] g_handle is not valid");
553         }
554         return ret;
555 }
556
557 int multi_assistant_service_plugin_add_assistant_language(const char* appid, const char* language)
558 {
559         int ret = -1;
560         if (NULL != g_handle) {
561                 wakeup_manager_add_assistant_language func = _wakeup_manager_interface.add_assistant_language;
562                 if (NULL == func) {
563                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_ADD_ASSISTANT_LANGUAGE);
564                 } else {
565                         ret = func(appid, language);
566                         if (0 != ret) {
567                                 MAS_LOGE("[ERROR] Fail to set wakeup word(%s)(%s), ret(%d)", appid, language, ret);
568                         }
569                 }
570         } else {
571                 MAS_LOGE("[ERROR] g_handle is not valid");
572         }
573         return ret;
574 }
575
576 int multi_assistant_service_plugin_set_assistant_wakeup_engine(const char* appid, const char* engine)
577 {
578         int ret = -1;
579         if (NULL != g_handle) {
580                 wakeup_manager_set_assistant_wakeup_engine func = _wakeup_manager_interface.set_assistant_wakeup_engine;
581                 if (NULL == func) {
582                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_ASSISTANT_WAKEUP_ENGINE);
583                 } else {
584                         ret = func(appid, engine);
585                         if (0 != ret) {
586                                 MAS_LOGE("[ERROR] Fail to set wakeup engine(%s)(%s), ret(%d)", appid, engine, ret);
587                         }
588                 }
589         } else {
590                 MAS_LOGE("[ERROR] g_handle is not valid");
591         }
592         return ret;
593 }
594
595 int multi_assistant_service_plugin_activate(void)
596 {
597         int ret = -1;
598         if (NULL != g_handle) {
599                 wakeup_manager_activate func = _wakeup_manager_interface.activate;
600                  if (NULL == func) {
601                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_ACTIVATE);
602                 } else {
603                         ret = func();
604                         if (0 != ret) {
605                                 MAS_LOGE("[ERROR] Fail to start recording, ret(%d)", ret);
606                         }
607                 }
608         } else {
609                 MAS_LOGE("[ERROR] g_handle is not valid");
610         }
611         return ret;
612 }
613
614 int multi_assistant_service_plugin_deactivate(void)
615 {
616         int ret = -1;
617         if (NULL != g_handle) {
618                 wakeup_manager_deactivate func = _wakeup_manager_interface.deactivate;
619                  if (NULL == func) {
620                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_DEACTIVATE);
621                 } else {
622                         ret = func();
623                         if (0 != ret) {
624                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
625                         }
626                 }
627         } else {
628                 MAS_LOGE("[ERROR] g_handle is not valid");
629         }
630         return ret;
631 }
632
633 int multi_assistant_service_plugin_update_voice_feedback_state(const char* appid, int state)
634 {
635         int ret = -1;
636         if (NULL != g_handle) {
637                 wakeup_manager_update_voice_feedback_state func = _wakeup_manager_interface.update_voice_feedback_state;
638                  if (NULL == func) {
639                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_UPDATE_VOICE_FEEDBACK_STATE);
640                 } else {
641                         ret = func(appid, state);
642                         if (0 != ret) {
643                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
644                         }
645                 }
646         } else {
647                 MAS_LOGE("[ERROR] g_handle is not valid");
648         }
649         return ret;
650 }
651
652 int multi_assistant_service_plugin_send_assistant_specific_command(const char* appid, const char* command)
653 {
654         int ret = -1;
655         if (NULL != g_handle) {
656                 wakeup_manager_send_assistant_specific_command func = _wakeup_manager_interface.send_assistant_specific_command;
657                  if (NULL == func) {
658                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SEND_ASSISTANT_SPECIFIC_COMMAND);
659                 } else {
660                         ret = func(appid, command);
661                         if (0 != ret) {
662                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
663                         }
664                 }
665         } else {
666                 MAS_LOGE("[ERROR] g_handle is not valid");
667         }
668         return ret;
669 }
670
671 int multi_assistant_service_plugin_change_system_volume(const char* appid, int event)
672 {
673         int ret = -1;
674         if (NULL != g_handle) {
675                 wakeup_manager_change_system_volume func = _wakeup_manager_interface.change_system_volume;
676                  if (NULL == func) {
677                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_CHANGE_SYSTEM_VOLUME);
678                 } else {
679                         ret = func(appid, event);
680                         if (0 != ret) {
681                                 MAS_LOGE("[ERROR] Fail to change system volume, ret(%d)", ret);
682                         }
683                 }
684         } else {
685                 MAS_LOGE("[ERROR] g_handle is not valid");
686         }
687         return ret;
688 }
689
690 int multi_assistant_service_plugin_update_result_state(const char* appid, int state)
691 {
692         int ret = -1;
693         if (NULL != g_handle) {
694                 wakeup_manager_update_result_state func = _wakeup_manager_interface.update_result_state;
695                  if (NULL == func) {
696                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_UPDATE_RESULT_STATE);
697                 } else {
698                         ret = func(appid, state);
699                         if (0 != ret) {
700                                 MAS_LOGE("[ERROR] Fail to update result state, ret(%d)", ret);
701                         }
702                 }
703         } else {
704                 MAS_LOGE("[ERROR] g_handle is not valid");
705         }
706         return ret;
707 }
708
709 int multi_assistant_service_plugin_process_event(int event, void *data, int len)
710 {
711         int ret = -1;
712         if (NULL != g_handle) {
713                 wakeup_manager_process_event func = _wakeup_manager_interface.process_event;
714                  if (NULL == func) {
715                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_PROCESS_EVENT);
716                 } else {
717                         ret = func(event, data, len);
718                         if (0 != ret) {
719                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
720                         }
721                 }
722         } else {
723                 MAS_LOGE("[ERROR] g_handle is not valid");
724         }
725         return ret;
726 }
727
728 int multi_assistant_service_plugin_start_streaming_utterance_data(void)
729 {
730         int ret = -1;
731         if (NULL != g_handle) {
732                 wakeup_manager_start_streaming_utterance_data func = _wakeup_manager_interface.start_streaming_utterance_data;
733                 if (NULL == func) {
734                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_START_STREAMING_UTTERANCE_DATA);
735                 } else {
736                         ret = func();
737                         if (0 != ret) {
738                                 MAS_LOGE("[ERROR] Fail to request speech data, ret(%d)", ret);
739                         }
740                 }
741         } else {
742                 MAS_LOGE("[ERROR] g_handle is not valid");
743         }
744         return ret;
745 }
746
747 int multi_assistant_service_plugin_stop_streaming_utterance_data(void)
748 {
749         int ret = -1;
750         if (NULL != g_handle) {
751                 wakeup_manager_stop_streaming_utterance_data func = _wakeup_manager_interface.stop_streaming_utterance_data;
752                 if (NULL == func) {
753                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_UTTERANCE_DATA);
754                 } else {
755                         ret = func();
756                         if (0 != ret) {
757                                 MAS_LOGE("[ERROR] Fail to request speech data, ret(%d)", ret);
758                         }
759                 }
760         } else {
761                 MAS_LOGE("[ERROR] g_handle is not valid");
762         }
763         return ret;
764 }
765
766 int multi_assistant_service_plugin_start_streaming_follow_up_data(void)
767 {
768         int ret = -1;
769         if (NULL != g_handle) {
770                 wakeup_manager_start_streaming_follow_up_data func = _wakeup_manager_interface.start_streaming_follow_up_data;
771                 if (NULL == func) {
772                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_START_STREAMING_FOLLOW_UP_DATA);
773                 } else {
774                         ret = func();
775                         if (0 != ret) {
776                                 MAS_LOGE("[ERROR] Fail to request speech data, ret(%d)", ret);
777                         }
778                 }
779         } else {
780                 MAS_LOGE("[ERROR] g_handle is not valid");
781         }
782         return ret;
783 }
784
785 int multi_assistant_service_plugin_stop_streaming_follow_up_data(void)
786 {
787         int ret = -1;
788         if (NULL != g_handle) {
789                 wakeup_manager_stop_streaming_follow_up_data func = _wakeup_manager_interface.stop_streaming_follow_up_data;
790                 if (NULL == func) {
791                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_FOLLOW_UP_DATA);
792                 } else {
793                         ret = func();
794                         if (0 != ret) {
795                                 MAS_LOGE("[ERROR] Fail to request speech data, ret(%d)", ret);
796                         }
797                 }
798         } else {
799                 MAS_LOGE("[ERROR] g_handle is not valid");
800         }
801         return ret;
802 }
803
804 int multi_assistant_service_plugin_get_recording_audio_format(int *rate, int *channel, int *audio_type)
805 {
806         int ret = -1;
807         if (NULL != g_handle) {
808                 wakeup_manager_get_audio_format func = _wakeup_manager_interface.get_audio_format;
809                 if (NULL == func) {
810                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_GET_AUDIO_FORMAT);
811                 } else {
812                         ret = func(rate, channel, audio_type);
813                         if (0 != ret) {
814                                 MAS_LOGE("[ERROR] Fail to get recording audio format, ret(%d)", ret);
815                         }
816                 }
817         } else {
818                 MAS_LOGE("[ERROR] g_handle is not valid");
819         }
820         return ret;
821 }
822
823 int multi_assistant_service_plugin_get_recording_audio_source_type(char** type)
824 {
825         int ret = -1;
826         if (NULL != g_handle) {
827                 wakeup_manager_get_audio_source_type func = _wakeup_manager_interface.get_audio_source_type;
828                 if (NULL == func) {
829                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_GET_AUDIO_SOURCE_TYPE);
830                 } else {
831                         ret = func(type);
832                         if (0 != ret) {
833                                 MAS_LOGE("[ERROR] Fail to get recording audio source type, ret(%d)", ret);
834                         }
835                 }
836         } else {
837                 MAS_LOGE("[ERROR] g_handle is not valid");
838         }
839         return ret;
840 }
841
842 int multi_assistant_service_plugin_set_callbacks(void)
843 {
844         int ret = multi_assistant_service_plugin_set_wakeup_event_callback(__wakeup_event_cb, NULL);
845         if (0 != ret) {
846                 MAS_LOGE("Fail to set wakeup event cb");
847                 return ret;
848         }
849
850         ret = multi_assistant_service_plugin_set_utterance_streaming_callback(__audio_streaming_cb, NULL);
851         if (0 != ret) {
852                 MAS_LOGE("Fail to set utterance streaming cb");
853                 return ret;
854         }
855
856         ret = multi_assistant_service_plugin_set_previous_utterance_streaming_callback(__audio_streaming_cb, NULL);
857         if (0 != ret) {
858                 MAS_LOGE("Fail to set previous utterance streaming cb");
859                 return ret;
860         }
861
862         ret = multi_assistant_service_plugin_set_follow_up_streaming_callback(__audio_streaming_cb, NULL);
863         if (0 != ret) {
864                 MAS_LOGE("Fail to set follow-up streaming cb");
865                 return ret;
866         }
867
868         ret = multi_assistant_service_plugin_set_speech_status_callback(__speech_status_cb, NULL);
869         if (0 != ret) {
870                 MAS_LOGE("Fail to set speech status changed cb");
871                 return ret;
872         }
873
874         ret = multi_assistant_service_plugin_set_error_callback(__error_cb, NULL);
875         if (0 != ret) {
876                 MAS_LOGE("Fail to set error cb");
877                 return ret;
878         }
879         return 0;
880 }
881
882 int multi_assistant_service_plugin_set_wakeup_event_callback(wakeup_service_wakeup_event_cb callback, void* user_data)
883 {
884         int ret = -1;
885         if (NULL != g_handle) {
886                 wakeup_manager_set_wakeup_event_callback func = _wakeup_manager_interface.set_wakeup_event_callback;
887                 if (NULL == func) {
888                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_EVENT_CALLBACK);
889                 } else {
890                         ret = func(callback, user_data);
891                         if (0 != ret) {
892                                 MAS_LOGE("[ERROR] Fail to set wakeup event callback, ret(%d)", ret);
893                         }
894                 }
895         } else {
896                 MAS_LOGE("[ERROR] g_handle is not valid");
897         }
898         return ret;
899 }
900
901 int multi_assistant_service_plugin_set_utterance_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
902 {
903         int ret = -1;
904         if (NULL != g_handle) {
905                 wakeup_manager_set_utterance_streaming_callback func = _wakeup_manager_interface.set_utterance_streaming_callback;
906                 if (NULL == func) {
907                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_UTTERANCE_STREAMING_CALLBACK);
908                 } else {
909                         ret = func(callback, user_data);
910                         if (0 != ret) {
911                                 MAS_LOGE("[ERROR] Fail to set utterance streaming callback, ret(%d)", ret);
912                         }
913                 }
914         } else {
915                 MAS_LOGE("[ERROR] g_handle is not valid");
916         }
917         return ret;
918 }
919
920 int multi_assistant_service_plugin_set_previous_utterance_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
921 {
922         int ret = -1;
923         if (NULL != g_handle) {
924                 wakeup_manager_set_previous_utterance_streaming_callback func = _wakeup_manager_interface.set_previous_utterance_streaming_callback;
925                 if (NULL == func) {
926                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_PREVIOUS_UTTERANCE_STREAMING_CALLBACK);
927                 } else {
928                         ret = func(callback, user_data);
929                         if (0 != ret) {
930                                 MAS_LOGE("[ERROR] Fail to set utterance streaming callback, ret(%d)", ret);
931                         }
932                 }
933         } else {
934                 MAS_LOGE("[ERROR] g_handle is not valid");
935         }
936         return ret;
937 }
938
939 int multi_assistant_service_plugin_set_follow_up_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
940 {
941         int ret = -1;
942         if (NULL != g_handle) {
943                 wakeup_manager_set_follow_up_streaming_callback func = _wakeup_manager_interface.set_follow_up_streaming_callback;
944                 if (NULL == func) {
945                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_FOLLOW_UP_STREAMING_CALLBACK);
946                 } else {
947                         ret = func(callback, user_data);
948                         if (0 != ret) {
949                                 MAS_LOGE("[ERROR] Fail to set follow-up streaming callback, ret(%d)", ret);
950                         }
951                 }
952         } else {
953                 MAS_LOGE("[ERROR] g_handle is not valid");
954         }
955         return ret;
956 }
957
958 int multi_assistant_service_plugin_set_speech_status_callback(wakeup_service_speech_status_cb callback, void* user_data)
959 {
960         int ret = -1;
961         if (NULL != g_handle) {
962                 wakeup_manager_set_speech_status_callback func = _wakeup_manager_interface.set_speech_status_callback;
963                 if (NULL == func) {
964                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_SPEECH_STATUS_CALLBACK);
965                 } else {
966                         ret = func(callback, user_data);
967                         if (0 != ret) {
968                                 MAS_LOGE("[ERROR] Fail to set speech status callback, ret(%d)", ret);
969                         }
970                 }
971         }
972         return ret;
973 }
974
975 int multi_assistant_service_plugin_set_error_callback(wakeup_service_error_cb callback, void* user_data)
976 {
977         int ret = -1;
978         if (NULL != g_handle) {
979                 wakeup_manager_set_error_callback func = _wakeup_manager_interface.set_error_callback;
980                 if (NULL == func) {
981                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_ERROR_CALLBACK);
982                 } else {
983                         ret = func(callback, user_data);
984                         if (0 != ret) {
985                                 MAS_LOGE("[ERROR] Fail to set error callback, ret(%d)", ret);
986                         }
987                 }
988         }
989         return ret;
990 }