f12323e066792e8d49e05f26fc343458d2db3806
[platform/core/uifw/multi-assistant-service.git] / src / service_plugin.cpp
1 /*
2  * Copyright 2020 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 #include <new>
30
31 #include "service_main.h"
32 #include "service_plugin.h"
33 #include "service_ipc_dbus.h"
34
35 /* Sound buf save for test */
36 #if 0
37 #define BUF_SAVE_MODE
38 #endif
39
40 typedef struct {
41         void* data;
42         CServicePlugin* plugin;
43 } AsyncParam;
44
45 bool CServicePlugin::is_ui_panel_enabled()
46 {
47         /* By default we assume the ui panel is always enabled unless explicitly turned off */
48         bool ret = true;
49         if (mPluginSettings) {
50                 ret = mPluginSettings->ui_panel_enabled;
51         }
52         MAS_LOGD("UI Panel Enabled : %d", ret);
53         return ret;
54 }
55
56 #if 0 /* + TEST_CODE */
57 Eina_Bool __send_asr_result(void *data)
58 {
59         MAS_LOGD("[ENTER]");
60
61         if (!strcmp((char*)data, "Today's")) {
62                 masc_ui_dbus_send_asr_result(-1, 1, "Today's");
63         }
64         if (!strcmp((char*)data, "weather.")) {
65                 masc_ui_dbus_send_asr_result(-1, 0, "Today's weather.");
66         }
67
68         MAS_LOGD("END");
69         return EINA_FALSE;
70 }
71
72 Eina_Bool __send_result(void *data)
73 {
74         MAS_LOGD("[ENTER]");
75
76         int ret = masc_ui_dbus_send_result(-1, (const char*)data, (const char*)data, "test");
77         if (0 != ret) {
78                 MAS_LOGE("[ERROR] Fail to stop recording(%d)", ret);
79                 return EINA_TRUE;
80         }
81
82         MAS_LOGD("END");
83         return EINA_FALSE;
84 }
85 #endif /* -TEST_CODE */
86
87 static Eina_Bool process_wakeup_event_by_appid_timer(void* data)
88 {
89         if (NULL == data) return ECORE_CALLBACK_CANCEL;
90         AsyncParam* param = static_cast<AsyncParam*>(data);
91
92         char* appid = static_cast<char*>(param->data);
93         MAS_LOGD("[ENTER] appid(%s)", appid);
94
95         int pid = -1;
96         if (!appid) return ECORE_CALLBACK_CANCEL;
97
98         CServicePlugin* plugin = param->plugin;
99         CServiceIpcDbus* service_ipc = nullptr;
100         CServiceMain* service_main = nullptr;
101         if (plugin) {
102                 service_ipc = plugin->get_service_ipc();
103                 service_main = plugin->get_service_main();
104         }
105         if (service_ipc && service_main) {
106                 bool use_custom_ui = service_main->mas_get_client_custom_ui_option_by_appid(appid);
107                 bool ui_panel_enabled = false;
108                 if (param->plugin) ui_panel_enabled = param->plugin->is_ui_panel_enabled();
109                 if (ui_panel_enabled) {
110                         service_ipc->masc_ui_dbus_enable_common_ui(!use_custom_ui);
111                         service_ipc->masc_ui_dbus_change_assistant(appid);
112                 }
113
114                 service_main->mas_set_current_client_by_appid(appid);
115                 if ((pid = service_main->mas_get_client_pid_by_appid(appid)) != -1) {
116                         service_main->mas_client_send_preprocessing_information(pid);
117                         service_ipc->change_active_state(pid, MA_ACTIVE_STATE_ACTIVE);
118                         service_main->mas_process_preprocessing_state_event(PREPROCESSING_STATE_EVENT_ASSISTANT_ACTIVATED);
119                 } else {
120                         // Appropriate MA Client not available, trying to launch new one
121                         MAS_LOGD("MA Client with appid %s does not exist, launching client", appid);
122                         service_main->mas_launch_client_by_appid(appid, CLIENT_LAUNCH_MODE_ACTIVATION);
123                 }
124         }
125
126         if (appid) free(appid);
127         delete param;
128
129         MAS_LOGD("END");
130         return ECORE_CALLBACK_CANCEL;
131 }
132
133 static Eina_Bool process_wakeup_event_by_word_timer(void* data)
134 {
135         MAS_LOGD("[ENTER]");
136
137         if (NULL == data) return ECORE_CALLBACK_CANCEL;
138         AsyncParam* param = static_cast<AsyncParam*>(data);
139
140         char* wakeup_word = static_cast<char*>(param->data);
141         CServicePlugin* plugin = param->plugin;
142         CServiceMain* service_main = nullptr;
143
144         if (plugin) {
145                 service_main = plugin->get_service_main();
146         }
147
148         delete param;
149         param = nullptr;
150
151         if (service_main) {
152                 const char* appid = service_main->mas_get_client_appid_by_wakeup_word(wakeup_word);
153                 if (appid) {
154                         param = new(std::nothrow) AsyncParam;
155                         if (param) {
156                                 param->data = static_cast<void*>(strdup(appid));
157                                 param->plugin = plugin;
158                                 process_wakeup_event_by_appid_timer(static_cast<void*>(param));
159                         }
160                 }
161         }
162
163         if (wakeup_word) free(wakeup_word);
164
165         MAS_LOGD("END");
166         return ECORE_CALLBACK_CANCEL;
167 }
168
169 static void __wakeup_event_cb(mas_wakeup_event_info wakeup_info, void* user_data)
170 {
171         MAS_LOGD("dalton debug : %p", user_data);
172         MAS_LOGD("[SUCCESS] __wakeup_event_cb is called, wakeup_word(%s)", wakeup_info.wakeup_word);
173         int ret = -1;
174
175         CServicePlugin *plugin = static_cast<CServicePlugin*>(user_data);
176         if (plugin) {
177                 CServiceIpcDbus* service_ipc = plugin->get_service_ipc();
178                 if (plugin->is_ui_panel_enabled() && service_ipc) {
179                         int retry_cnt = 0;
180                         while (0 != ret) {
181                                 ret = service_ipc->masc_ui_dbus_send_hello();
182                                 retry_cnt++;
183                                 if (5 < retry_cnt) {
184                                         MAS_LOGE("[ERROR] Fail to receive reply for hello, ret(%d)", ret);
185                                         break;
186                                 }
187                         }
188                 }
189         }
190
191 #ifdef BUF_SAVE_MODE
192         if (mDumpFile) {
193                 fclose(mDumpFile);
194                 mDumpFile = NULL;
195         } else {
196                 MAS_LOGD("[Recorder Info] File not found!");
197         }
198
199         while (1) {
200                 snprintf(mDumpFilename, sizeof(mDumpFilename), "/tmp/ma_service_%d_%d", getpid(), mDumpCount);
201                 int ret = access(mDumpFilename, 0);
202
203                 if (0 == ret) {
204                         MAS_LOGD("[Recorder ERROR] File is already exist");
205                         if (0 == remove(mDumpFilename)) {
206                                 MAS_LOGD("[Recorder] Remove file");
207                                 break;
208                         } else {
209                                 mDumpCount++;
210                         }
211                 } else {
212                         break;
213                 }
214         }
215
216         MAS_LOGD("[Recorder] Temp file name=[%s]", mDumpFilename);
217
218         /* open test file */
219         mDumpFile = fopen(mDumpFilename, "wb+x");
220         if (!mDumpFile) {
221                 MAS_LOGD("[Recorder ERROR] File not found!");
222                 return;
223         }
224         mDumpCount++;
225 #endif
226
227 #if 0 /* + TEST_CODE */
228         if (WAKEUP_EVENT_SUCCESS == event) {
229                 ecore_thread_main_loop_begin();
230                 ecore_timer_add(1.0, __send_asr_result, "Today's");
231                 ecore_thread_main_loop_end();
232
233                 ecore_thread_main_loop_begin();
234                 ecore_timer_add(2.0, __send_asr_result, "weather.");
235                 ecore_thread_main_loop_end();
236
237                 ecore_thread_main_loop_begin();
238                 ecore_timer_add(3.0, __send_result, "Partly cloudy with temperatures from 75 to 88");
239                 ecore_thread_main_loop_end();
240         }
241 #endif /* - TEST_CODE */
242         if (wakeup_info.wakeup_appid) {
243                 AsyncParam* param = new(std::nothrow) AsyncParam;
244                 if (param) {
245                         param->data = static_cast<void*>(strdup(wakeup_info.wakeup_appid));
246                         param->plugin = static_cast<CServicePlugin*>(user_data);
247                         ecore_thread_main_loop_begin();
248                         ecore_timer_add(0.0f, process_wakeup_event_by_appid_timer,
249                                 static_cast<void*>(param));
250                         ecore_thread_main_loop_end();
251                 }
252         } else if (wakeup_info.wakeup_word) {
253                 AsyncParam* param = new(std::nothrow) AsyncParam;
254                 if (param) {
255                         param->data = static_cast<void*>(strdup(wakeup_info.wakeup_word));
256                         param->plugin = static_cast<CServicePlugin*>(user_data);
257                         ecore_thread_main_loop_begin();
258                         ecore_timer_add(0.0f, process_wakeup_event_by_word_timer,
259                                 static_cast<void*>(param));
260                         ecore_thread_main_loop_end();
261                 }
262         }
263 }
264
265 static bool __validate_streaming_event_order(int pid, mas_speech_streaming_event_e *event)
266 {
267         bool ret = false;
268
269         static int previous_pid = -1;
270         static mas_speech_streaming_event_e previous_event = MAS_SPEECH_STREAMING_EVENT_FINISH;
271
272         if (NULL == event) return false;
273
274         mas_speech_streaming_event_e expected_sequence[][2] = {
275                 {MAS_SPEECH_STREAMING_EVENT_START, MAS_SPEECH_STREAMING_EVENT_CONTINUE},
276                 {MAS_SPEECH_STREAMING_EVENT_START, MAS_SPEECH_STREAMING_EVENT_FINISH},
277                 {MAS_SPEECH_STREAMING_EVENT_CONTINUE, MAS_SPEECH_STREAMING_EVENT_CONTINUE},
278                 {MAS_SPEECH_STREAMING_EVENT_CONTINUE, MAS_SPEECH_STREAMING_EVENT_FINISH},
279                 {MAS_SPEECH_STREAMING_EVENT_FINISH, MAS_SPEECH_STREAMING_EVENT_START},
280                 /* If there is no audio data even after the start streaming request */
281                 {MAS_SPEECH_STREAMING_EVENT_FINISH, MAS_SPEECH_STREAMING_EVENT_FINISH},
282         };
283
284         if (pid != previous_pid) {
285                 /* When sending streaming event to a new client, it always sends START message first */
286                 previous_event = MAS_SPEECH_STREAMING_EVENT_FINISH;
287         }
288
289         for (int loop = 0;loop < sizeof(expected_sequence) / sizeof(expected_sequence[0]);loop++) {
290                 if (previous_event == expected_sequence[loop][0] &&
291                         *event == expected_sequence[loop][1]) {
292                         ret = true;
293                 }
294         }
295         if (!ret) {
296                 /* In case of FINISH -> CONTINUE without START, simply modify current event value */
297                 if (MAS_SPEECH_STREAMING_EVENT_FINISH == previous_event &&
298                         MAS_SPEECH_STREAMING_EVENT_CONTINUE == *event) {
299                         *event = MAS_SPEECH_STREAMING_EVENT_START;
300                         ret = true;
301
302                         MAS_LOGD("[WARNING] forcibly changed CONTINUE to START : %d -> %d (PID %d -> %d)",
303                                 previous_event, *event, previous_pid, pid);
304                 }
305         }
306
307         if (ret) {
308                 previous_pid = pid;
309                 previous_event = *event;
310         } else {
311                 MAS_LOGE("[ERROR] State sequence validation failed : %d -> %d (PID %d -> %d)",
312                         previous_event, *event, previous_pid, pid);
313         }
314         return ret;
315 }
316
317 void handle_speech_streaming_event_failure(void* data)
318 {
319         AsyncParam* param = static_cast<AsyncParam*>(data);
320         if (NULL == param) return;
321
322         CServicePlugin* plugin = param->plugin;
323         CServiceMain* service_main = nullptr;
324         if (plugin) service_main = plugin->get_service_main();
325
326         if (service_main) {
327                 service_main->mas_client_send_recognition_result(0, MA_RECOGNITION_RESULT_EVENT_ERROR);
328         }
329
330         delete param;
331 }
332
333 static void __audio_streaming_cb(mas_speech_streaming_event_e event, void* buffer, int len, void *user_data)
334 {
335         if (event == MAS_SPEECH_STREAMING_EVENT_FAIL) {
336                 ecore_main_loop_thread_safe_call_async(handle_speech_streaming_event_failure, NULL);
337                 return;
338         }
339         static int count = 0;
340         if (event != MAS_SPEECH_STREAMING_EVENT_CONTINUE || count % 100 == 0) {
341                 MAS_LOGD("[SUCCESS] __audio_streaming_cb is called, event(%d), buffer(%p), len(%d)",
342                         event, buffer, len);
343         }
344         ++count;
345
346         CServicePlugin* plugin = static_cast<CServicePlugin*>(user_data);
347         CServiceIpcDbus* service_ipc = nullptr;
348         CServiceMain* service_main = nullptr;
349         if (plugin) {
350                 service_ipc = plugin->get_service_ipc();
351                 service_main = plugin->get_service_main();
352         }
353
354         if (service_ipc && service_main) {
355                 int pid = service_main->mas_get_current_client_pid();
356                 int preprocessing_pid = service_main->mas_get_current_preprocessing_client_pid();
357                 if (pid == -1) {
358                         MAS_LOGE("[ERROR] Fail to retrieve pid of current MA client");
359                 } else {
360                         if (__validate_streaming_event_order(pid, &event)) {
361                                 int ret = service_ipc->send_streaming_audio_data(pid,
362                                         event, buffer, len);
363                                 if (0 != ret) {
364                                         MAS_LOGE("[ERROR] Fail to send speech data, ret(%d)", ret);
365                                 }
366                                 if (pid != preprocessing_pid && -1 != preprocessing_pid) {
367                                         int ret = service_ipc->send_streaming_audio_data(preprocessing_pid,
368                                                 event, buffer, len);
369                                         if (0 != ret) {
370                                                 MAS_LOGE("[ERROR] Fail to send speech data to preprocessing client, ret(%d)", ret);
371                                         }
372                                 }
373                         }
374                 }
375         }
376 #ifdef BUF_SAVE_MODE
377         if (mDumpFile)
378                 fwrite(speech_data->buffer, 1, speech_data->len, mDumpFile);
379
380         if (MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
381                 if (mDumpFile) {
382                         MAS_LOGE("[Recorder SUCCESS] File Close");
383                         fclose(mDumpFile);
384                         mDumpFile = NULL;
385                 } else {
386                         MAS_LOGE("[Recorder ERROR] File not found!");
387                 }
388         }
389 #endif
390 }
391
392 static void __speech_status_cb(mas_speech_status_e status, void *user_data)
393 {
394         MAS_LOGD("[SUCCESS] __speech_status_cb is called, status(%d)", status);
395 }
396
397 static void __error_cb(int error, const char* err_msg, void* user_data)
398 {
399         MAS_LOGD("[SUCCESS] __error_cb is called, error(%d), err_msg(%s)", error, err_msg);
400
401         CServicePlugin *plugin = static_cast<CServicePlugin*>(user_data);
402         if (nullptr == plugin) return;
403
404         CServiceIpcDbus* service_ipc = plugin->get_service_ipc();
405         if (plugin->is_ui_panel_enabled() && service_ipc) {
406                 int ret = service_ipc->masc_ui_dbus_send_error_message(error, err_msg);
407                 if (0 != ret) {
408                         MAS_LOGE("[ERROR] Fail to send error message, ret(%d)", ret);
409                 }
410         }
411 }
412
413 static void __setting_changed_cb(void *user_data)
414 {
415         CServicePlugin *plugin = static_cast<CServicePlugin*>(user_data);
416         if (nullptr == plugin) return;
417
418         CServiceMain* service_main = plugin->get_service_main();
419         if (service_main) {
420                 service_main->mas_prelaunch_default_assistant();
421                 service_main->mas_update_voice_key_support_mode();
422         }
423         MAS_LOGD("[SUCCESS] __setting_changed_cb is called");
424 }
425
426 static void __streaming_section_changed_cb(ma_audio_streaming_data_section_e section, void* user_data)
427 {
428         MAS_LOGD("[SUCCESS] __streaming_section_changed_cb is called, section(%d)", section);
429
430         CServicePlugin *plugin = static_cast<CServicePlugin*>(user_data);
431         if (nullptr == plugin) return;
432
433         CServiceIpcDbus *service_ipc = plugin->get_service_ipc();
434         CServiceMain* service_main = plugin->get_service_main();
435         if (service_ipc && service_main) {
436                 int pid = service_main->mas_get_current_client_pid();
437                 int ret = service_ipc->send_streaming_section_changed(pid, (int)section);
438                 if (0 != ret) {
439                         MAS_LOGE("[ERROR] Fail to send streaming section changed information, ret(%d)", ret);
440                 }
441         }
442 }
443
444 static void __wakeup_engine_command_cb(mas_wakeup_engine_command_target_e target, const char* assistant_name, const char* command, void* user_data)
445 {
446         MAS_LOGD("[SUCCESS] __wakeup_engine_command_cb is called, command(%s)", command);
447
448         CServicePlugin *plugin = static_cast<CServicePlugin*>(user_data);
449         if (nullptr == plugin) return;
450
451         CServiceIpcDbus *service_ipc = plugin->get_service_ipc();
452         CServiceMain* service_main = plugin->get_service_main();
453         if (service_ipc && service_main) {
454                 int pid = service_main->mas_get_client_pid_by_appid(assistant_name);
455                 if (-1 != pid) {
456                         int ret = service_ipc->send_wakeup_engine_command(pid, command);
457                         if (0 != ret) {
458                                 MAS_LOGE("[ERROR] Fail to send wakeup engine command, ret(%d)", ret);
459                         }
460                 }
461         }
462 }
463
464 static void __wakeup_service_state_changed_cb(ma_service_state_e state, void* user_data)
465 {
466         MAS_LOGD("[SUCCESS] __wakeup_service_state_changed_cb is called, state(%d)", state);
467
468         CServicePlugin *plugin = static_cast<CServicePlugin*>(user_data);
469         if (nullptr == plugin) return;
470
471         CServiceMain* service_main = plugin->get_service_main();
472         if (service_main) {
473                 service_main->mas_set_current_service_state(state);
474         }
475 }
476
477 static void __wakeup_service_voice_key_status_changed_cb(ma_voice_key_status_e status, void* user_data)
478 {
479         MAS_LOGD("[SUCCESS] __wakeup_service_voice_key_status_changed_cb is called, state(%d)", status);
480
481         CServicePlugin *plugin = static_cast<CServicePlugin*>(user_data);
482         if (nullptr == plugin) return;
483
484         CServiceIpcDbus *service_ipc = plugin->get_service_ipc();
485         CServiceMain* service_main = plugin->get_service_main();
486         if (service_ipc && service_main) {
487                 int pid = service_main->mas_get_current_client_pid();
488                 int ret = service_ipc->change_voice_key_status(pid, status);
489                 if (0 != ret) {
490                         MAS_LOGE("[ERROR] Fail to send voice key status changed information, ret(%d)", ret);
491                 }
492         }
493 }
494
495 int CServicePlugin::initialize(void)
496 {
497         MAS_LOGD("[Enter]");
498
499         char filepath[512] = {'\0', };
500         const char *default_engine_path = MA_WAKEUP_MANAGER_PATH;
501         snprintf(filepath, 512, "%s/%s", default_engine_path, MA_DEFAULT_WAKEUP_MANAGER_FILENAME);
502
503         char *error;
504         mPluginHandle = NULL;
505         mPluginHandle = dlopen(filepath, RTLD_LAZY);
506         if (NULL != (error = dlerror())) {
507                 MAS_LOGE("[ERROR] Fail to dlopen(%s), error(%s)", filepath, error);
508                 return -1; //MAS_ERROR_OPERATION_FAILED;
509         }
510
511         mWakeupManagerInterface.initialize =
512                 (wakeup_manager_initialize)dlsym(mPluginHandle,
513                 MA_WAKEUP_MANAGER_FUNC_INITIALIZE);
514         mWakeupManagerInterface.deinitialize =
515                 (wakeup_manager_deinitialize)dlsym(mPluginHandle,
516                 MA_WAKEUP_MANAGER_FUNC_DEINITIALIZE);
517         mWakeupManagerInterface.get_settings =
518                 (wakeup_manager_get_settings)dlsym(mPluginHandle,
519                 MA_WAKEUP_MANAGER_FUNC_GET_SETTINGS);
520         mWakeupManagerInterface.add_assistant_wakeup_word =
521                 (wakeup_manager_add_assistant_wakeup_word)dlsym(mPluginHandle,
522                 MA_WAKEUP_MANAGER_FUNC_ADD_ASSISTANT_WAKEUP_WORD);
523         mWakeupManagerInterface.remove_assistant_wakeup_word =
524                 (wakeup_manager_remove_assistant_wakeup_word)dlsym(mPluginHandle,
525                 MA_WAKEUP_MANAGER_FUNC_REMOVE_ASSISTANT_WAKEUP_WORD);
526         mWakeupManagerInterface.add_assistant_language =
527                 (wakeup_manager_add_assistant_language)dlsym(mPluginHandle,
528                 MA_WAKEUP_MANAGER_FUNC_ADD_ASSISTANT_LANGUAGE);
529         mWakeupManagerInterface.set_assistant_wakeup_engine =
530                 (wakeup_manager_set_assistant_wakeup_engine)dlsym(mPluginHandle,
531                 MA_WAKEUP_MANAGER_FUNC_SET_ASSISTANT_WAKEUP_ENGINE);
532         mWakeupManagerInterface.set_default_assistant =
533                 (wakeup_manager_set_default_assistant)dlsym(mPluginHandle,
534                 MA_WAKEUP_MANAGER_FUNC_SET_DEFAULT_ASSISTANT);
535         mWakeupManagerInterface.get_default_assistant =
536                 (wakeup_manager_get_default_assistant)dlsym(mPluginHandle,
537                 MA_WAKEUP_MANAGER_FUNC_GET_DEFAULT_ASSISTANT);
538         mWakeupManagerInterface.set_language =
539                 (wakeup_manager_set_language)dlsym(mPluginHandle,
540                 MA_WAKEUP_MANAGER_FUNC_SET_LANGUAGE);
541         mWakeupManagerInterface.activate =
542                 (wakeup_manager_activate)dlsym(mPluginHandle,
543                 MA_WAKEUP_MANAGER_FUNC_ACTIVATE);
544         mWakeupManagerInterface.deactivate =
545                 (wakeup_manager_deactivate)dlsym(mPluginHandle,
546                 MA_WAKEUP_MANAGER_FUNC_DEACTIVATE);
547         mWakeupManagerInterface.update_voice_feedback_state =
548                 (wakeup_manager_update_voice_feedback_state)dlsym(mPluginHandle,
549                 MA_WAKEUP_MANAGER_FUNC_UPDATE_VOICE_FEEDBACK_STATE);
550         mWakeupManagerInterface.set_assistant_specific_command =
551                 (wakeup_manager_set_assistant_specific_command)dlsym(mPluginHandle,
552                 MA_WAKEUP_MANAGER_FUNC_SET_ASSISTANT_SPECIFIC_COMMAND);
553         mWakeupManagerInterface.set_background_volume =
554                 (wakeup_manager_set_background_volume)dlsym(mPluginHandle,
555                 MA_WAKEUP_MANAGER_FUNC_SET_BACKGROUND_VOLUME);
556         mWakeupManagerInterface.update_recognition_result =
557                 (wakeup_manager_update_recognition_result)dlsym(mPluginHandle,
558                 MA_WAKEUP_MANAGER_FUNC_UPDATE_RECOGNITION_RESULT);
559         mWakeupManagerInterface.process_plugin_event =
560                 (wakeup_manager_process_plugin_event)dlsym(mPluginHandle,
561                 MA_WAKEUP_MANAGER_FUNC_PROCESS_PLUGIN_EVENT);
562         mWakeupManagerInterface.start_streaming_utterance_data =
563                 (wakeup_manager_start_streaming_utterance_data)dlsym(mPluginHandle,
564                 MA_WAKEUP_MANAGER_FUNC_START_STREAMING_UTTERANCE_DATA);
565         mWakeupManagerInterface.stop_streaming_utterance_data =
566                 (wakeup_manager_stop_streaming_utterance_data)dlsym(mPluginHandle,
567                 MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_UTTERANCE_DATA);
568         mWakeupManagerInterface.start_streaming_previous_utterance_data =
569                 (wakeup_manager_start_streaming_previous_utterance_data)dlsym(mPluginHandle,
570                 MA_WAKEUP_MANAGER_FUNC_START_STREAMING_PREVIOUS_UTTERANCE_DATA);
571         mWakeupManagerInterface.stop_streaming_previous_utterance_data =
572                 (wakeup_manager_stop_streaming_previous_utterance_data)dlsym(mPluginHandle,
573                 MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_PREVIOUS_UTTERANCE_DATA);
574         mWakeupManagerInterface.start_streaming_follow_up_data =
575                 (wakeup_manager_start_streaming_follow_up_data)dlsym(mPluginHandle,
576                 MA_WAKEUP_MANAGER_FUNC_START_STREAMING_FOLLOW_UP_DATA);
577         mWakeupManagerInterface.stop_streaming_follow_up_data =
578                 (wakeup_manager_stop_streaming_follow_up_data)dlsym(mPluginHandle,
579                 MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_FOLLOW_UP_DATA);
580         mWakeupManagerInterface.get_audio_format =
581                 (wakeup_manager_get_audio_format)dlsym(mPluginHandle,
582                 MA_WAKEUP_MANAGER_FUNC_GET_AUDIO_FORMAT);
583         mWakeupManagerInterface.get_audio_source_type =
584                 (wakeup_manager_get_audio_source_type)dlsym(mPluginHandle,
585                 MA_WAKEUP_MANAGER_FUNC_GET_AUDIO_SOURCE_TYPE);
586         mWakeupManagerInterface.set_wake_word_audio_require_flag =
587                 (wakeup_manager_set_wake_word_audio_require_flag)dlsym(mPluginHandle,
588                 MA_WAKEUP_MANAGER_FUNC_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG);
589         mWakeupManagerInterface.set_assistant_language =
590                 (wakeup_manager_set_assistant_language)dlsym(mPluginHandle,
591                 MA_WAKEUP_MANAGER_FUNC_SET_ASSISTANT_LANGUAGE);
592         mWakeupManagerInterface.set_voice_key_tap_duration =
593                 (wakeup_manager_set_voice_key_tap_duration)dlsym(mPluginHandle,
594                 MA_WAKEUP_MANAGER_FUNC_SET_VOICE_KEY_TAP_DURATION);
595         mWakeupManagerInterface.unset_voice_key_tap_duration =
596                 (wakeup_manager_unset_voice_key_tap_duration)dlsym(mPluginHandle,
597                 MA_WAKEUP_MANAGER_FUNC_UNSET_VOICE_KEY_TAP_DURATION);
598         mWakeupManagerInterface.set_voice_key_support_mode =
599                 (wakeup_manager_set_voice_key_support_mode)dlsym(mPluginHandle,
600                 MA_WAKEUP_MANAGER_FUNC_SET_VOICE_KEY_SUPPORT_MODE);
601         mWakeupManagerInterface.set_wakeup_event_callback =
602                 (wakeup_manager_set_wakeup_event_callback)dlsym(mPluginHandle,
603                 MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_EVENT_CALLBACK);
604         mWakeupManagerInterface.set_utterance_streaming_callback =
605                 (wakeup_manager_set_utterance_streaming_callback)dlsym(mPluginHandle,
606                 MA_WAKEUP_MANAGER_FUNC_SET_UTTERANCE_STREAMING_CALLBACK);
607         mWakeupManagerInterface.set_previous_utterance_streaming_callback =
608                 (wakeup_manager_set_previous_utterance_streaming_callback)dlsym(mPluginHandle,
609                 MA_WAKEUP_MANAGER_FUNC_SET_PREVIOUS_UTTERANCE_STREAMING_CALLBACK);
610         mWakeupManagerInterface.set_follow_up_streaming_callback =
611                 (wakeup_manager_set_follow_up_streaming_callback)dlsym(mPluginHandle,
612                 MA_WAKEUP_MANAGER_FUNC_SET_FOLLOW_UP_STREAMING_CALLBACK);
613         mWakeupManagerInterface.set_speech_status_callback =
614                 (wakeup_manager_set_speech_status_callback)dlsym(mPluginHandle,
615                 MA_WAKEUP_MANAGER_FUNC_SET_SPEECH_STATUS_CALLBACK);
616         mWakeupManagerInterface.set_setting_changed_callback =
617                 (wakeup_manager_set_setting_changed_callback)dlsym(mPluginHandle,
618                 MA_WAKEUP_MANAGER_FUNC_SET_SETTING_CHANGED_CALLBACK);
619         mWakeupManagerInterface.set_error_callback =
620                 (wakeup_manager_set_error_callback)dlsym(mPluginHandle,
621                 MA_WAKEUP_MANAGER_FUNC_SET_ERROR_CALLBACK);
622         mWakeupManagerInterface.set_streaming_section_changed_callback =
623                 (wakeup_manager_set_streaming_section_changed_callback)dlsym(mPluginHandle,
624                 MA_WAKEUP_MANAGER_FUNC_SET_STREAMING_SECTION_CHANGED_CALLBACK);
625         mWakeupManagerInterface.set_wakeup_engine_command_callback =
626                 (wakeup_manager_set_wakeup_engine_command_callback)dlsym(mPluginHandle,
627                 MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_ENGINE_COMMAND_CALLBACK);
628         mWakeupManagerInterface.set_wakeup_service_state_changed_callback =
629                 (wakeup_manager_set_wakeup_service_state_changed_callback)dlsym(mPluginHandle,
630                 MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_SERVICE_STATE_CHANGED_CALLBACK);
631         mWakeupManagerInterface.set_voice_key_status_changed_callback =
632                 (wakeup_manager_set_voice_key_status_changed_callback)dlsym(mPluginHandle,
633                 MA_WAKEUP_MANAGER_FUNC_SET_VOICE_KEY_STATUS_CHANGED_CALLBACK);
634
635         int ret = -1;
636         if (NULL != mPluginHandle) {
637                 wakeup_manager_initialize func = mWakeupManagerInterface.initialize;
638
639                 if (NULL == func) {
640                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_INITIALIZE);
641                 } else {
642                         ret = func();
643                         if (0 != ret) {
644                                 MAS_LOGE("[ERROR] Fail to initialize, ret(%d)", ret);
645                         }
646                 }
647
648                 wakeup_manager_get_settings get_settings_func = mWakeupManagerInterface.get_settings;
649
650                 if (NULL == get_settings_func) {
651                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_GET_SETTINGS);
652                 } else {
653                         size_t struct_size;
654                         ret = get_settings_func(&mPluginSettings, &struct_size);
655                         if (0 != ret || struct_size != sizeof(ma_plugin_settings)) {
656                                 MAS_LOGE("[ERROR] Fail to get settings, ret(%d), size %zu", ret, struct_size);
657                                 mPluginSettings = NULL;
658                         }
659                 }
660         } else {
661                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
662         }
663         return ret;
664 }
665
666 int CServicePlugin::deinitialize(void)
667 {
668 #ifdef BUF_SAVE_MODE
669         if (mDumpFile) {
670                 fclose(mDumpFile);
671                 mDumpFile = NULL;
672         } else {
673                 MAS_LOGD("[Recorder ERROR] File not found!");
674         }
675 #endif
676
677         int ret = -1;
678         if (NULL != mPluginHandle) {
679                 wakeup_manager_deinitialize func = mWakeupManagerInterface.deinitialize;
680                 if (NULL == func) {
681                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_DEINITIALIZE);
682                 } else {
683                         ret = func();
684                         if (0 != ret) {
685                                 MAS_LOGE("[ERROR] Fail to deinitialize, ret(%d)", ret);
686                         }
687                 }
688
689                 dlclose(mPluginHandle);
690                 mPluginHandle = NULL;
691         } else {
692                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
693         }
694
695         return ret;
696 }
697
698 int CServicePlugin::get_settings(ma_plugin_settings **settings, size_t *struct_size)
699 {
700         int ret = -1;
701         if (NULL != mPluginHandle) {
702                 wakeup_manager_get_settings func = mWakeupManagerInterface.get_settings;
703                 if (NULL == func) {
704                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_GET_SETTINGS);
705                 } else {
706                         ret = func(settings, struct_size);
707                         if (0 != ret) {
708                                 MAS_LOGE("[ERROR] Fail to get settings, ret(%d)", ret);
709                         }
710                 }
711         } else {
712                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
713         }
714         return ret;
715 }
716
717 int CServicePlugin::set_language(const char* language)
718 {
719         int ret = -1;
720         if (NULL != mPluginHandle) {
721                 wakeup_manager_set_language func = mWakeupManagerInterface.set_language;
722                 if (NULL == func) {
723                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_LANGUAGE);
724                 } else {
725                         ret = func(language);
726                         if (0 != ret) {
727                                 MAS_LOGE("[ERROR] Fail to set langauge(%s), ret(%d)", language, ret);
728                         }
729                 }
730         } else {
731                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
732         }
733         return ret;
734 }
735
736 int CServicePlugin::add_assistant_wakeup_word(const char* appid, const char* wakeup_word, const char* language)
737 {
738         int ret = -1;
739         if (NULL != mPluginHandle) {
740                 wakeup_manager_add_assistant_wakeup_word func = mWakeupManagerInterface.add_assistant_wakeup_word;
741                 if (NULL == func) {
742                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_ADD_ASSISTANT_WAKEUP_WORD);
743                 } else {
744                         ret = func(appid, wakeup_word, language);
745                         if (0 != ret) {
746                                 MAS_LOGE("[ERROR] Fail to add wakeup word(%s)(%s)(%s), ret(%d)", appid, wakeup_word, language, ret);
747                         }
748                 }
749         } else {
750                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
751         }
752         return ret;
753 }
754
755 int CServicePlugin::remove_assistant_wakeup_word(const char* appid, const char* wakeup_word, const char* language)
756 {
757         int ret = -1;
758         if (NULL != mPluginHandle) {
759                 wakeup_manager_remove_assistant_wakeup_word func = mWakeupManagerInterface.remove_assistant_wakeup_word;
760                 if (NULL == func) {
761                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_REMOVE_ASSISTANT_WAKEUP_WORD);
762                 } else {
763                         ret = func(appid, wakeup_word, language);
764                         if (0 != ret) {
765                                 MAS_LOGE("[ERROR] Fail to remove wakeup word(%s)(%s)(%s), ret(%d)", appid, wakeup_word, language, ret);
766                         }
767                 }
768         } else {
769                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
770         }
771         return ret;
772 }
773
774 int CServicePlugin::add_assistant_language(const char* appid, const char* language)
775 {
776         int ret = -1;
777         if (NULL != mPluginHandle) {
778                 wakeup_manager_add_assistant_language func = mWakeupManagerInterface.add_assistant_language;
779                 if (NULL == func) {
780                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_ADD_ASSISTANT_LANGUAGE);
781                 } else {
782                         ret = func(appid, language);
783                         if (0 != ret) {
784                                 MAS_LOGE("[ERROR] Fail to set wakeup word(%s)(%s), ret(%d)", appid, language, ret);
785                         }
786                 }
787         } else {
788                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
789         }
790         return ret;
791 }
792
793 int CServicePlugin::set_assistant_wakeup_engine(const char* appid, const char* engine)
794 {
795         int ret = -1;
796         if (NULL != mPluginHandle) {
797                 wakeup_manager_set_assistant_wakeup_engine func = mWakeupManagerInterface.set_assistant_wakeup_engine;
798                 if (NULL == func) {
799                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_ASSISTANT_WAKEUP_ENGINE);
800                 } else {
801                         ret = func(appid, engine);
802                         if (0 != ret) {
803                                 MAS_LOGE("[ERROR] Fail to set wakeup engine(%s)(%s), ret(%d)", appid, engine, ret);
804                         }
805                 }
806         } else {
807                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
808         }
809         return ret;
810 }
811
812 int CServicePlugin::set_default_assistant(const char* appid)
813 {
814         int ret = -1;
815         if (NULL != mPluginHandle) {
816                 wakeup_manager_set_default_assistant func = mWakeupManagerInterface.set_default_assistant;
817                 if (NULL == func) {
818                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_DEFAULT_ASSISTANT);
819                 } else {
820                         ret = func(appid);
821                         if (0 != ret) {
822                                 MAS_LOGE("[ERROR] Fail to set default assistant(%s), ret(%d)", appid, ret);
823                         }
824                 }
825         } else {
826                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
827         }
828         return ret;
829 }
830
831 int CServicePlugin::get_default_assistant(const char** appid)
832 {
833         int ret = -1;
834         if (NULL == appid) {
835                 MAS_LOGE("[ERROR] appid is not valid");
836                 return ret;
837         }
838         if (NULL != mPluginHandle) {
839                 wakeup_manager_get_default_assistant func = mWakeupManagerInterface.get_default_assistant;
840                 if (NULL == func) {
841                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_GET_DEFAULT_ASSISTANT);
842                 } else {
843                         ret = func(appid);
844                         if (0 != ret) {
845                                 MAS_LOGE("[ERROR] Fail to get default assistant, ret(%d)", ret);
846                         }
847                 }
848         } else {
849                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
850         }
851         return ret;
852 }
853
854 int CServicePlugin::activate(void)
855 {
856         int ret = -1;
857         if (NULL != mPluginHandle) {
858                 wakeup_manager_activate func = mWakeupManagerInterface.activate;
859                 if (NULL == func) {
860                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_ACTIVATE);
861                 } else {
862                         ret = func();
863                         if (0 != ret) {
864                                 MAS_LOGE("[ERROR] Fail to start recording, ret(%d)", ret);
865                         }
866                 }
867         } else {
868                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
869         }
870         return ret;
871 }
872
873 int CServicePlugin::deactivate(void)
874 {
875         int ret = -1;
876         if (NULL != mPluginHandle) {
877                 wakeup_manager_deactivate func = mWakeupManagerInterface.deactivate;
878                 if (NULL == func) {
879                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_DEACTIVATE);
880                 } else {
881                         ret = func();
882                         if (0 != ret) {
883                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
884                         }
885                 }
886         } else {
887                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
888         }
889         return ret;
890 }
891
892 int CServicePlugin::update_voice_feedback_state(const char* appid, int state)
893 {
894         int ret = -1;
895         if (NULL != mPluginHandle) {
896                 wakeup_manager_update_voice_feedback_state func = mWakeupManagerInterface.update_voice_feedback_state;
897                 if (NULL == func) {
898                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_UPDATE_VOICE_FEEDBACK_STATE);
899                 } else {
900                         ret = func(appid, state);
901                         if (0 != ret) {
902                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
903                         }
904                 }
905         } else {
906                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
907         }
908         return ret;
909 }
910
911 int CServicePlugin::set_assistant_specific_command(const char* appid, const char* command)
912 {
913         int ret = -1;
914         if (NULL != mPluginHandle) {
915                 wakeup_manager_set_assistant_specific_command func = mWakeupManagerInterface.set_assistant_specific_command;
916                 if (NULL == func) {
917                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_ASSISTANT_SPECIFIC_COMMAND);
918                 } else {
919                         ret = func(appid, command);
920                         if (0 != ret) {
921                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
922                         }
923                 }
924         } else {
925                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
926         }
927         return ret;
928 }
929
930 int CServicePlugin::set_background_volume(const char* appid, double ratio)
931 {
932         int ret = -1;
933         if (NULL != mPluginHandle) {
934                 wakeup_manager_set_background_volume func = mWakeupManagerInterface.set_background_volume;
935                 if (NULL == func) {
936                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_BACKGROUND_VOLUME);
937                 } else {
938                         ret = func(appid, ratio);
939                         if (0 != ret) {
940                                 MAS_LOGE("[ERROR] Fail to set background volume, ret(%d)", ret);
941                         }
942                 }
943         } else {
944                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
945         }
946         return ret;
947 }
948
949 int CServicePlugin::update_recognition_result(const char* appid, int state)
950 {
951         int ret = -1;
952         if (NULL != mPluginHandle) {
953                 wakeup_manager_update_recognition_result func = mWakeupManagerInterface.update_recognition_result;
954                 if (NULL == func) {
955                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_UPDATE_RECOGNITION_RESULT);
956                 } else {
957                         ret = func(appid, state);
958                         if (0 != ret) {
959                                 MAS_LOGE("[ERROR] Fail to update result state, ret(%d)", ret);
960                         }
961                 }
962         } else {
963                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
964         }
965         return ret;
966 }
967
968 int CServicePlugin::process_event(int event, void *data, int len)
969 {
970         int ret = -1;
971         if (NULL != mPluginHandle) {
972                 wakeup_manager_process_plugin_event func = mWakeupManagerInterface.process_plugin_event;
973                 if (NULL == func) {
974                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_PROCESS_PLUGIN_EVENT);
975                 } else {
976                         ret = func((mas_plugin_event_e)event, data, len);
977                         if (0 != ret) {
978                                 MAS_LOGE("[ERROR] Fail to stop recording, ret(%d)", ret);
979                         }
980                 }
981         } else {
982                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
983         }
984         return ret;
985 }
986
987 int CServicePlugin::start_streaming_utterance_data(void)
988 {
989         int ret = -1;
990         if (NULL != mPluginHandle) {
991                 wakeup_manager_start_streaming_utterance_data func = mWakeupManagerInterface.start_streaming_utterance_data;
992                 if (NULL == func) {
993                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_START_STREAMING_UTTERANCE_DATA);
994                 } else {
995                         ret = func();
996                         if (0 != ret) {
997                                 MAS_LOGE("[ERROR] Fail to request speech data, ret(%d)", ret);
998                         }
999                 }
1000         } else {
1001                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1002         }
1003         return ret;
1004 }
1005
1006 int CServicePlugin::stop_streaming_utterance_data(void)
1007 {
1008         int ret = -1;
1009         if (NULL != mPluginHandle) {
1010                 wakeup_manager_stop_streaming_utterance_data func = mWakeupManagerInterface.stop_streaming_utterance_data;
1011                 if (NULL == func) {
1012                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_UTTERANCE_DATA);
1013                 } else {
1014                         ret = func();
1015                         if (0 != ret) {
1016                                 MAS_LOGE("[ERROR] Fail to request speech data, ret(%d)", ret);
1017                         }
1018                 }
1019         } else {
1020                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1021         }
1022         return ret;
1023 }
1024
1025 int CServicePlugin::start_streaming_previous_utterance_data(void)
1026 {
1027         int ret = -1;
1028         if (NULL != mPluginHandle) {
1029                 wakeup_manager_start_streaming_previous_utterance_data func = mWakeupManagerInterface.start_streaming_previous_utterance_data;
1030                 if (NULL == func) {
1031                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_START_STREAMING_PREVIOUS_UTTERANCE_DATA);
1032                 } else {
1033                         ret = func();
1034                         if (0 != ret) {
1035                                 MAS_LOGE("[ERROR] Fail to request previous speech data, ret(%d)", ret);
1036                         }
1037                 }
1038         } else {
1039                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1040         }
1041         return ret;
1042 }
1043
1044 int CServicePlugin::stop_streaming_previous_utterance_data(void)
1045 {
1046         int ret = -1;
1047         if (NULL != mPluginHandle) {
1048                 wakeup_manager_stop_streaming_previous_utterance_data func = mWakeupManagerInterface.stop_streaming_previous_utterance_data;
1049                 if (NULL == func) {
1050                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_PREVIOUS_UTTERANCE_DATA);
1051                 } else {
1052                         ret = func();
1053                         if (0 != ret) {
1054                                 MAS_LOGE("[ERROR] Fail to request previous speech data, ret(%d)", ret);
1055                         }
1056                 }
1057         } else {
1058                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1059         }
1060         return ret;
1061 }
1062
1063 int CServicePlugin::start_streaming_follow_up_data(void)
1064 {
1065         int ret = -1;
1066         if (NULL != mPluginHandle) {
1067                 wakeup_manager_start_streaming_follow_up_data func = mWakeupManagerInterface.start_streaming_follow_up_data;
1068                 if (NULL == func) {
1069                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_START_STREAMING_FOLLOW_UP_DATA);
1070                 } else {
1071                         ret = func();
1072                         if (0 != ret) {
1073                                 MAS_LOGE("[ERROR] Fail to request speech data, ret(%d)", ret);
1074                         }
1075                 }
1076         } else {
1077                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1078         }
1079         return ret;
1080 }
1081
1082 int CServicePlugin::stop_streaming_follow_up_data(void)
1083 {
1084         int ret = -1;
1085         if (NULL != mPluginHandle) {
1086                 wakeup_manager_stop_streaming_follow_up_data func = mWakeupManagerInterface.stop_streaming_follow_up_data;
1087                 if (NULL == func) {
1088                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_STOP_STREAMING_FOLLOW_UP_DATA);
1089                 } else {
1090                         ret = func();
1091                         if (0 != ret) {
1092                                 MAS_LOGE("[ERROR] Fail to request speech data, ret(%d)", ret);
1093                         }
1094                 }
1095         } else {
1096                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1097         }
1098         return ret;
1099 }
1100
1101 int CServicePlugin::get_recording_audio_format(int *rate, int *channel, int *audio_type)
1102 {
1103         int ret = -1;
1104         if (NULL != mPluginHandle) {
1105                 wakeup_manager_get_audio_format func = mWakeupManagerInterface.get_audio_format;
1106                 if (NULL == func) {
1107                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_GET_AUDIO_FORMAT);
1108                 } else {
1109                         ret = func(rate, channel, audio_type);
1110                         if (0 != ret) {
1111                                 MAS_LOGE("[ERROR] Fail to get recording audio format, ret(%d)", ret);
1112                         }
1113                 }
1114         } else {
1115                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1116         }
1117         return ret;
1118 }
1119
1120 int CServicePlugin::get_recording_audio_source_type(char** type)
1121 {
1122         int ret = -1;
1123         if (NULL != mPluginHandle) {
1124                 wakeup_manager_get_audio_source_type func = mWakeupManagerInterface.get_audio_source_type;
1125                 if (NULL == func) {
1126                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_GET_AUDIO_SOURCE_TYPE);
1127                 } else {
1128                         ret = func(type);
1129                         if (0 != ret) {
1130                                 MAS_LOGE("[ERROR] Fail to get recording audio source type, ret(%d)", ret);
1131                         }
1132                 }
1133         } else {
1134                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1135         }
1136         return ret;
1137 }
1138
1139 int CServicePlugin::set_voice_key_tap_duration(float duration)
1140 {
1141         int ret = -1;
1142         if (NULL != mPluginHandle) {
1143                 wakeup_manager_set_voice_key_tap_duration func = mWakeupManagerInterface.set_voice_key_tap_duration;
1144                 if (NULL == func) {
1145                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_VOICE_KEY_TAP_DURATION);
1146                 } else {
1147                         ret = func(duration);
1148                         if (0 != ret) {
1149                                 MAS_LOGE("[ERROR] Fail to set voice key tap duration, ret(%d)", ret);
1150                         }
1151                 }
1152         } else {
1153                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1154         }
1155         return ret;
1156 }
1157
1158 int CServicePlugin::unset_voice_key_tap_duration()
1159 {
1160         int ret = -1;
1161         if (NULL != mPluginHandle) {
1162                 wakeup_manager_unset_voice_key_tap_duration func = mWakeupManagerInterface.unset_voice_key_tap_duration;
1163                 if (NULL == func) {
1164                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_UNSET_VOICE_KEY_TAP_DURATION);
1165                 } else {
1166                         ret = func();
1167                         if (0 != ret) {
1168                                 MAS_LOGE("[ERROR] Fail to unset voice key tap duration, ret(%d)", ret);
1169                         }
1170                 }
1171         } else {
1172                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1173         }
1174         return ret;
1175 }
1176
1177 int CServicePlugin::set_voice_key_support_mode(int mode)
1178 {
1179         int ret = -1;
1180         if (NULL != mPluginHandle) {
1181                 wakeup_manager_set_voice_key_support_mode func = mWakeupManagerInterface.set_voice_key_support_mode;
1182                 if (NULL == func) {
1183                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_VOICE_KEY_SUPPORT_MODE);
1184                 } else {
1185                         ret = func(mode);
1186                         if (0 != ret) {
1187                                 MAS_LOGE("[ERROR] Fail to set voice key support mode, ret(%d)", ret);
1188                         }
1189                 }
1190         } else {
1191                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1192         }
1193         return ret;
1194 }
1195
1196 int CServicePlugin::set_wake_word_audio_require_flag(const char* appid, bool require)
1197 {
1198         int ret = -1;
1199         if (NULL != mPluginHandle) {
1200                 wakeup_manager_set_wake_word_audio_require_flag func = mWakeupManagerInterface.set_wake_word_audio_require_flag;
1201                 if (NULL == func) {
1202                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG);
1203                 } else {
1204                         ret = func(require);
1205                         if (0 != ret) {
1206                                 MAS_LOGE("[ERROR] Fail to set wake word audio require flag, ret(%d)", ret);
1207                         }
1208                 }
1209         } else {
1210                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1211         }
1212         return ret;
1213 }
1214
1215 int CServicePlugin::set_assistant_language(const char* appid, const char* language)
1216 {
1217         int ret = -1;
1218         if (NULL != mPluginHandle) {
1219                 wakeup_manager_set_assistant_language func = mWakeupManagerInterface.set_assistant_language;
1220                 if (NULL == func) {
1221                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_ASSISTANT_LANGUAGE);
1222                 } else {
1223                         ret = func(appid, language);
1224                         if (0 != ret) {
1225                                 MAS_LOGE("[ERROR] Fail to set assistant language, ret(%d)", ret);
1226                         }
1227                 }
1228         } else {
1229                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1230         }
1231         return ret;
1232 }
1233
1234 int CServicePlugin::set_callbacks(void)
1235 {
1236         int ret = set_wakeup_event_callback(__wakeup_event_cb, this);
1237         if (0 != ret) {
1238                 MAS_LOGE("Fail to set wakeup event cb");
1239                 return ret;
1240         }
1241
1242         ret = set_utterance_streaming_callback(__audio_streaming_cb, this);
1243         if (0 != ret) {
1244                 MAS_LOGE("Fail to set utterance streaming cb");
1245                 return ret;
1246         }
1247
1248         ret = set_previous_utterance_streaming_callback(__audio_streaming_cb, this);
1249         if (0 != ret) {
1250                 MAS_LOGE("Fail to set previous utterance streaming cb");
1251                 return ret;
1252         }
1253
1254         ret = set_follow_up_streaming_callback(__audio_streaming_cb, this);
1255         if (0 != ret) {
1256                 MAS_LOGE("Fail to set follow-up streaming cb");
1257                 return ret;
1258         }
1259
1260         ret = set_speech_status_callback(__speech_status_cb, this);
1261         if (0 != ret) {
1262                 MAS_LOGE("Fail to set speech status changed cb");
1263                 return ret;
1264         }
1265
1266         ret = set_setting_changed_callback(__setting_changed_cb, this);
1267         if (0 != ret) {
1268                 MAS_LOGE("Fail to set setting changed cb");
1269                 return ret;
1270         }
1271
1272         ret = set_error_callback(__error_cb, this);
1273         if (0 != ret) {
1274                 MAS_LOGE("Fail to set error cb");
1275                 return ret;
1276         }
1277
1278         ret = set_streaming_section_changed_callback(__streaming_section_changed_cb, this);
1279         if (0 != ret) {
1280                 MAS_LOGE("Fail to set streaming section changed cb");
1281                 return ret;
1282         }
1283
1284         ret = set_wakeup_engine_command_callback(__wakeup_engine_command_cb, this);
1285         if (0 != ret) {
1286                 MAS_LOGE("Fail to set wakeup engine command cb");
1287                 return ret;
1288         }
1289
1290         ret = set_wakeup_service_state_changed_callback(__wakeup_service_state_changed_cb, this);
1291         if (0 != ret) {
1292                 MAS_LOGE("Fail to set wakeup engine command cb");
1293                 return ret;
1294         }
1295
1296         ret = set_voice_key_status_changed_callback(__wakeup_service_voice_key_status_changed_cb, this);
1297         if (0 != ret) {
1298                 MAS_LOGE("Fail to set wakeup engine command cb");
1299                 return ret;
1300         }
1301
1302         return 0;
1303 }
1304
1305 int CServicePlugin::set_wakeup_event_callback(wakeup_service_wakeup_event_cb callback, void* user_data)
1306 {
1307         int ret = -1;
1308         if (NULL != mPluginHandle) {
1309                 wakeup_manager_set_wakeup_event_callback func = mWakeupManagerInterface.set_wakeup_event_callback;
1310                 if (NULL == func) {
1311                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_EVENT_CALLBACK);
1312                 } else {
1313                         ret = func(callback, user_data);
1314                         MAS_LOGD("dalton debug : %p", user_data);
1315                         if (0 != ret) {
1316                                 MAS_LOGE("[ERROR] Fail to set wakeup event callback, ret(%d)", ret);
1317                         }
1318                 }
1319         } else {
1320                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1321         }
1322         return ret;
1323 }
1324
1325 int CServicePlugin::set_utterance_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
1326 {
1327         int ret = -1;
1328         if (NULL != mPluginHandle) {
1329                 wakeup_manager_set_utterance_streaming_callback func = mWakeupManagerInterface.set_utterance_streaming_callback;
1330                 if (NULL == func) {
1331                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_UTTERANCE_STREAMING_CALLBACK);
1332                 } else {
1333                         ret = func(callback, user_data);
1334                         if (0 != ret) {
1335                                 MAS_LOGE("[ERROR] Fail to set utterance streaming callback, ret(%d)", ret);
1336                         }
1337                 }
1338         } else {
1339                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1340         }
1341         return ret;
1342 }
1343
1344 int CServicePlugin::set_previous_utterance_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
1345 {
1346         int ret = -1;
1347         if (NULL != mPluginHandle) {
1348                 wakeup_manager_set_previous_utterance_streaming_callback func = mWakeupManagerInterface.set_previous_utterance_streaming_callback;
1349                 if (NULL == func) {
1350                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_PREVIOUS_UTTERANCE_STREAMING_CALLBACK);
1351                 } else {
1352                         ret = func(callback, user_data);
1353                         if (0 != ret) {
1354                                 MAS_LOGE("[ERROR] Fail to set utterance streaming callback, ret(%d)", ret);
1355                         }
1356                 }
1357         } else {
1358                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1359         }
1360         return ret;
1361 }
1362
1363 int CServicePlugin::set_follow_up_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
1364 {
1365         int ret = -1;
1366         if (NULL != mPluginHandle) {
1367                 wakeup_manager_set_follow_up_streaming_callback func = mWakeupManagerInterface.set_follow_up_streaming_callback;
1368                 if (NULL == func) {
1369                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_FOLLOW_UP_STREAMING_CALLBACK);
1370                 } else {
1371                         ret = func(callback, user_data);
1372                         if (0 != ret) {
1373                                 MAS_LOGE("[ERROR] Fail to set follow-up streaming callback, ret(%d)", ret);
1374                         }
1375                 }
1376         } else {
1377                 MAS_LOGE("[ERROR] mPluginHandle is not valid");
1378         }
1379         return ret;
1380 }
1381
1382 int CServicePlugin::set_speech_status_callback(wakeup_service_speech_status_cb callback, void* user_data)
1383 {
1384         int ret = -1;
1385         if (NULL != mPluginHandle) {
1386                 wakeup_manager_set_speech_status_callback func = mWakeupManagerInterface.set_speech_status_callback;
1387                 if (NULL == func) {
1388                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_SPEECH_STATUS_CALLBACK);
1389                 } else {
1390                         ret = func(callback, user_data);
1391                         if (0 != ret) {
1392                                 MAS_LOGE("[ERROR] Fail to set speech status callback, ret(%d)", ret);
1393                         }
1394                 }
1395         }
1396         return ret;
1397 }
1398
1399 int CServicePlugin::set_setting_changed_callback(wakeup_service_setting_changed_cb callback, void* user_data)
1400 {
1401         int ret = -1;
1402         if (NULL != mPluginHandle) {
1403                 wakeup_manager_set_setting_changed_callback func = mWakeupManagerInterface.set_setting_changed_callback;
1404                 if (NULL == func) {
1405                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_SETTING_CHANGED_CALLBACK);
1406                 } else {
1407                         ret = func(callback, user_data);
1408                         if (0 != ret) {
1409                                 MAS_LOGE("[ERROR] Fail to set setting_changed callback, ret(%d)", ret);
1410                         }
1411                 }
1412         }
1413         return ret;
1414 }
1415
1416 int CServicePlugin::set_error_callback(wakeup_service_error_cb callback, void* user_data)
1417 {
1418         int ret = -1;
1419         if (NULL != mPluginHandle) {
1420                 wakeup_manager_set_error_callback func = mWakeupManagerInterface.set_error_callback;
1421                 if (NULL == func) {
1422                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_ERROR_CALLBACK);
1423                 } else {
1424                         ret = func(callback, user_data);
1425                         if (0 != ret) {
1426                                 MAS_LOGE("[ERROR] Fail to set error callback, ret(%d)", ret);
1427                         }
1428                 }
1429         }
1430         return ret;
1431 }
1432
1433 int CServicePlugin::set_streaming_section_changed_callback(wakeup_service_streaming_section_changed_cb callback, void* user_data)
1434 {
1435         int ret = -1;
1436         if (NULL != mPluginHandle) {
1437                 wakeup_manager_set_streaming_section_changed_callback func = mWakeupManagerInterface.set_streaming_section_changed_callback;
1438                 if (NULL == func) {
1439                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_STREAMING_SECTION_CHANGED_CALLBACK);
1440                 } else {
1441                         ret = func(callback, user_data);
1442                         if (0 != ret) {
1443                                 MAS_LOGE("[ERROR] Fail to set streaming section changed callback, ret(%d)", ret);
1444                         }
1445                 }
1446         }
1447         return ret;
1448 }
1449
1450 int CServicePlugin::set_wakeup_engine_command_callback(wakeup_service_wakeup_engine_command_cb callback, void* user_data)
1451 {
1452         int ret = -1;
1453         if (NULL != mPluginHandle) {
1454                 wakeup_manager_set_wakeup_engine_command_callback func = mWakeupManagerInterface.set_wakeup_engine_command_callback;
1455                 if (NULL == func) {
1456                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_ENGINE_COMMAND_CALLBACK);
1457                 } else {
1458                         ret = func(callback, user_data);
1459                         if (0 != ret) {
1460                                 MAS_LOGE("[ERROR] Fail to set error callback, ret(%d)", ret);
1461                         }
1462                 }
1463         }
1464         return ret;
1465 }
1466
1467 int CServicePlugin::set_wakeup_service_state_changed_callback(wakeup_service_wakeup_service_state_changed_cb callback, void* user_data)
1468 {
1469         int ret = -1;
1470         if (NULL != mPluginHandle) {
1471                 wakeup_manager_set_wakeup_service_state_changed_callback func = mWakeupManagerInterface.set_wakeup_service_state_changed_callback;
1472                 if (NULL == func) {
1473                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_WAKEUP_SERVICE_STATE_CHANGED_CALLBACK);
1474                 } else {
1475                         ret = func(callback, user_data);
1476                         if (0 != ret) {
1477                                 MAS_LOGE("[ERROR] Fail to set error callback, ret(%d)", ret);
1478                         }
1479                 }
1480         }
1481         return ret;
1482 }
1483
1484 int CServicePlugin::set_voice_key_status_changed_callback(wakeup_service_voice_key_status_changed_cb callback, void* user_data)
1485 {
1486         int ret = -1;
1487         if (NULL != mPluginHandle) {
1488                 wakeup_manager_set_voice_key_status_changed_callback func = mWakeupManagerInterface.set_voice_key_status_changed_callback;
1489                 if (NULL == func) {
1490                         MAS_LOGE("[ERROR] symbol lookup failed : %s", MA_WAKEUP_MANAGER_FUNC_SET_VOICE_KEY_STATUS_CHANGED_CALLBACK);
1491                 } else {
1492                         ret = func(callback, user_data);
1493                         if (0 != ret) {
1494                                 MAS_LOGE("[ERROR] Fail to set error callback, ret(%d)", ret);
1495                         }
1496                 }
1497         }
1498         return ret;
1499 }
1500