1 #include <multi_assistant_service.h>
3 #include "wakeup_engine_manager.h"
4 #include "wakeup_manager_main.h"
8 #include <pkgmgr-info.h>
11 namespace multiassistant
16 /* Sound buf save for test */
22 static char g_temp_file_name[128] = {'\0', };
24 static FILE* g_pFile = NULL;
26 static int g_count = 1;
29 /* Need to check whether this value needs to be configurable */
30 static int g_speech_pcm_wait_count = 800;
32 /* Utility function for checking if an element exists in a container */
33 template<class C, class T>
34 static auto contains(const C& v, const T& x) -> decltype(end(v), true)
36 return end(v) != find(begin(v), end(v), x);
39 CWakeupEngineManager::CWakeupEngineManager()
43 CWakeupEngineManager::~CWakeupEngineManager()
45 MWR_LOGI("Wakeup Engine Manager is now being destroyed");
48 CWakeupEngineManager::CWakeupEngineManager(IEngineEventObserver *observer) : CWakeupEngineManager()
53 void CWakeupEngineManager::initialize()
55 DIR* dp = opendir(MA_WAKEUP_ENGINE_PATH);
57 MWR_LOGD("Failed opening directory : %s", (const char*)MA_WAKEUP_ENGINE_PATH);
59 struct dirent *dirp = nullptr;
60 char dirpath[_POSIX_PATH_MAX];
64 if (nullptr != dirp) {
65 const string current_directory{"."};
66 const string parent_directory{".."};
67 if (0 == current_directory.compare(dirp->d_name) ||
68 0 == parent_directory.compare(dirp->d_name))
71 if (DT_DIR != dirp->d_type) /* If not a directory */
74 int dirpath_len = strlen(MA_WAKEUP_ENGINE_PATH) + strlen(dirp->d_name) + 1;
75 if (dirpath_len >= _POSIX_PATH_MAX) {
76 MWR_LOGD("File path is too long : %s", dirp->d_name);
81 memset(dirpath, '\0', _POSIX_PATH_MAX);
82 snprintf(dirpath, _POSIX_PATH_MAX, "%s/%s",
83 (const char*)(MA_WAKEUP_ENGINE_PATH), dirp->d_name);
85 add_engine_directory(string{dirp->d_name}, dirpath);
87 } while (nullptr != dirp);
93 void CWakeupEngineManager::deinitialize()
96 if (mStreamingThread.joinable()) {
97 MWR_LOGD("mStreamingThread is joinable, trying join()");
98 mStopStreamingThread.store(true);
99 mStreamingThread.join();
101 mStopStreamingThread.store(false);
103 for (auto& info : mEngineInfo) {
105 if (info.interface.set_wakeup_event_callback) {
106 info.interface.set_wakeup_event_callback(nullptr, nullptr);
108 if (info.interface.set_speech_status_callback) {
109 info.interface.set_speech_status_callback(nullptr, nullptr);
111 if (info.interface.set_error_callback) {
112 info.interface.set_error_callback(nullptr, nullptr);
114 if (info.interface.set_audio_data_require_status_callback) {
115 info.interface.set_audio_data_require_status_callback(nullptr, nullptr);
117 if (info.interface.set_wakeup_engine_command_callback) {
118 info.interface.set_wakeup_engine_command_callback(nullptr, nullptr);
120 MWR_LOGI("Trying to deinitialize engine : %s", info.engine_name.c_str());
121 if (info.interface.deinitialize) {
122 int ret = info.interface.deinitialize();
123 MWR_LOGI("Deinitialization [%s] returned %d", info.engine_name.c_str(), ret);
125 } catch (const std::exception& e) {
126 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
127 info.engine_name.c_str(), e.what());
129 if (info.engine_handle) {
130 int ret = dlclose(info.engine_handle);
131 MWR_LOGI("Closing [%s] returned %d, [%s]", info.engine_name.c_str(), ret,
132 ((0 == ret) ? "" : dlerror()));
133 info.engine_handle = nullptr;
136 mSelectedEngine = nullptr;
141 void CWakeupEngineManager::subscribe(IEngineEventObserver *observer)
143 mObservers.push_back(observer);
144 MWR_LOGD("Added Observer : %p %zu", observer, mObservers.size());
147 void CWakeupEngineManager::unsubscribe(IEngineEventObserver *observer)
149 auto iter = find(mObservers.begin(), mObservers.end(), observer);
150 if (iter != mObservers.end()) {
151 mObservers.erase(iter);
155 bool CWakeupEngineManager::get_audio_data_required()
157 return mAudioDataRequired;
160 void CWakeupEngineManager::set_selected_wakeup_info(mas_wakeup_event_info wakeup_info)
162 mSelectedEngine = nullptr;
163 for (const auto& info : mEngineInfo) {
164 string appid = string{wakeup_info.wakeup_appid};
165 bool found = contains(info.assistant_list, appid);
168 mSelectedEngine = &info;
169 MWR_LOGD("Selected : %s", info.engine_name.c_str());
174 bool CWakeupEngineManager::set_language(string language)
176 for (const auto& info : mEngineInfo) {
177 if (info.interface.set_language) {
179 info.interface.set_language(language.c_str());
180 } catch (const std::exception& e) {
181 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
182 info.engine_name.c_str(), e.what());
189 bool CWakeupEngineManager::set_assistant_language(string appid, string language)
191 for (auto& info : mEngineInfo) {
192 const auto& iter = find_if(info.assistant_list.begin(), info.assistant_list.end(),
193 [appid](const string& assistant) {
194 return (0 == assistant.compare(appid));
197 /* If the appid is in the assistant list */
198 if (info.assistant_list.end() != iter) {
200 int ret = info.interface.set_language(language.c_str());
201 MWR_LOGI("set_language returned %d : %s %s %s",
202 ret, appid.c_str(), info.engine_name.c_str(), language.c_str());
203 } catch (const std::exception& e) {
204 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
205 info.engine_name.c_str(), e.what());
212 void CWakeupEngineManager::set_assistant_activated(string appid, bool activated)
214 MWR_LOGI("[ENTER] : %s %d", appid.c_str(), activated);
215 for (auto& info : mEngineInfo) {
216 const auto& iter = find_if(info.assistant_list.begin(), info.assistant_list.end(),
217 [appid](const string& assistant) {
218 return (0 == assistant.compare(appid));
221 /* If the appid is in the assistant list */
222 if (info.assistant_list.end() != iter) {
223 bool previously_activated = info.activated;
225 info.activated_assistants.insert(appid);
227 info.activated_assistants.erase(appid);
229 info.activated = (info.activated_assistants.size() > 0);
230 if (previously_activated != info.activated) {
231 if (info.activated) {
233 info.interface.activate();
234 } catch (const std::exception& e) {
235 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
236 info.engine_name.c_str(), e.what());
240 info.interface.deactivate();
241 } catch (const std::exception& e) {
242 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
243 info.engine_name.c_str(), e.what());
246 /* Activated status changed, need to update audio_data_require_status too */
247 on_audio_data_require_status(info.engine_name, info.audio_data_require_status);
253 void CWakeupEngineManager::set_wake_word_audio_require_flag(bool require)
256 mWakeWordAudioRequired = require;
257 for (const auto& info : mEngineInfo) {
258 if (info.interface.set_wake_word_audio_require_flag) {
260 info.interface.set_wake_word_audio_require_flag(require);
261 } catch (const std::exception& e) {
262 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
263 info.engine_name.c_str(), e.what());
269 void CWakeupEngineManager::streaming_speech_data_thread_func()
273 if (nullptr == mSelectedEngine)
276 const wakeup_engine_interface *interface = &(mSelectedEngine->interface);
278 if (NULL == interface ||
279 NULL == interface->get_utterance_data ||
280 NULL == interface->get_utterance_data_count)
283 MWR_LOGD("data_count : %d", interface->get_utterance_data_count());
290 MWR_LOGD("[Recorder Info] File not found!");
294 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/ma_wue_%d_%d", getpid(), g_count);
295 int ret = access(g_temp_file_name, 0);
298 MWR_LOGD("[Recorder ERROR] File is already exist");
299 if (0 == remove(g_temp_file_name)) {
300 MWR_LOGD("[Recorder] Remove file");
310 MWR_LOGD("[Recorder] Temp file name=[%s]", g_temp_file_name);
313 g_pFile = fopen(g_temp_file_name, "wb+x");
315 MWR_LOGD("[Recorder ERROR] File not found!");
321 mas_speech_data speech_data;
323 bool finish_event_sent = false;
325 if (mWakeWordAudioRequired &&
326 NULL != interface->get_wake_word_data &&
327 NULL != interface->get_wake_word_data_count) {
328 for (const auto& observer : mObservers) {
330 if (!observer->on_audio_streaming_data_section(MA_AUDIO_STREAMING_DATA_SECTION_WAKE_WORD)) {
331 LOGE("[Recorder WARNING] One of the observer returned false");
335 int count = interface->get_wake_word_data_count();
336 while (!(mStopStreamingThread.load()) && index < count) {
337 int ret = interface->get_wake_word_data(index, &speech_data);
341 fwrite(speech_data.buffer, 1, speech_data.len, g_pFile);
343 for (const auto& observer : mObservers) {
345 if (!observer->on_streaming_audio_data(
346 speech_data.event, speech_data.buffer, speech_data.len)) {
347 LOGE("[Recorder WARNING] One of the observer returned false");
357 for (const auto& observer : mObservers) {
359 if (!observer->on_audio_streaming_data_section(MA_AUDIO_STREAMING_DATA_SECTION_UTTERANCE)) {
360 LOGE("[Recorder WARNING] One of the observer returned false");
367 while (!(mStopStreamingThread.load())) {
371 /* get feedback data */
372 if (interface && interface->get_utterance_data) {
373 ret = interface->get_utterance_data(index, &speech_data);
376 MWR_LOGD("[DEBUG] No feedback data. Waiting mode : %d", ret);
379 while (!(mStopStreamingThread.load())) {
381 this_thread::sleep_for(chrono::milliseconds(10));
382 if (index < interface->get_utterance_data_count()) {
383 MWR_LOGD("[INFO] Resume thread");
386 if (g_speech_pcm_wait_count < cnt) {
387 unsigned char final_buffer[2] = {'\0', };
388 MWR_LOGE("[ERROR] Wrong request, there's no pcm data");
391 fwrite(final_buffer, 1, sizeof(final_buffer), g_pFile);
392 MWR_LOGE("[Recorder SUCCESS] File Close");
397 for (const auto& observer : mObservers) {
399 if (!observer->on_streaming_audio_data(
400 MAS_SPEECH_STREAMING_EVENT_FAIL, NULL, 0)) {
401 LOGE("[Recorder WARNING] One of the observer returned false");
403 if (!observer->on_streaming_audio_data(
404 MAS_SPEECH_STREAMING_EVENT_FINISH, final_buffer, sizeof(final_buffer))) {
405 LOGE("[Recorder WARNING] One of the observer returned false");
413 MWR_LOGD("[INFO] Finish to wait for new feedback data come");
415 /* resume feedback thread */
421 fwrite(speech_data.buffer, 1, speech_data.len, g_pFile);
423 if (MAS_SPEECH_STREAMING_EVENT_FINISH == speech_data.event) {
425 MWR_LOGE("[Recorder SUCCESS] File Close");
429 MWR_LOGE("[Recorder ERROR] File not found!");
433 const int sleep_duration_in_millis = 10;
434 const int max_burst_count = 3;
435 if (++burst_count >= max_burst_count) {
437 this_thread::sleep_for(chrono::milliseconds(sleep_duration_in_millis));
438 MWR_LOGI("[INFO] Streaming data burst transmission detected, forcing sleep");
440 for (const auto& observer : mObservers) {
442 if (!observer->on_streaming_audio_data(
443 speech_data.event, speech_data.buffer, speech_data.len)) {
444 LOGE("[Recorder WARNING] One of the observer returned false");
449 if (MAS_SPEECH_STREAMING_EVENT_FINISH == speech_data.event) {
450 MWR_LOGI("[INFO] Finish to get and send speech data");
451 finish_event_sent = true;
459 if (true != finish_event_sent) {
460 unsigned char final_buffer[2] = {'\0', };
461 for (const auto& observer : mObservers) {
463 if (!observer->on_streaming_audio_data(
464 MAS_SPEECH_STREAMING_EVENT_FINISH, final_buffer, sizeof(final_buffer))) {
465 LOGE("[Recorder WARNING] One of the observer returned false");
471 fwrite(final_buffer, 1, sizeof(final_buffer), g_pFile);
472 MWR_LOGE("[Recorder SUCCESS] File Close");
482 void CWakeupEngineManager::start_streaming_current_utterance_data()
484 if (mStreamingThread.joinable()) {
485 MWR_LOGE("ERROR : mStreamingThread is joinable, will not start a new thread");
488 mStreamingThread = thread(&CWakeupEngineManager::streaming_speech_data_thread_func, this);
491 void CWakeupEngineManager::stop_streaming_current_utterance_data()
493 if (mStreamingThread.joinable()) {
494 MWR_LOGD("mStreamingThread is joinable, trying join()");
495 mStopStreamingThread.store(true);
496 mStreamingThread.join();
498 mStopStreamingThread.store(false);
501 void CWakeupEngineManager::update_manager_state(wakeup_manager_state_e state)
503 for (const auto& info : mEngineInfo) {
504 if (info.interface.update_manager_state) {
506 info.interface.update_manager_state(state);
507 } catch (const std::exception& e) {
508 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
509 info.engine_name.c_str(), e.what());
513 mWakeupManagerState = state;
516 void CWakeupEngineManager::update_recognition_result(string appid, int result)
518 if (mSelectedEngine) {
519 if (mSelectedEngine->interface.update_recognition_result) {
520 mSelectedEngine->interface.update_recognition_result(appid.c_str(), result);
525 void CWakeupEngineManager::engine_add_target_assistant(string engine_name, string appid)
527 const auto& iter = find_if(mEngineInfo.begin(), mEngineInfo.end(),
528 [engine_name](const EngineInfo& info) {
529 return (0 == info.engine_name.compare(engine_name));
532 if (mEngineInfo.end() == iter) {
533 /* Not found, add new library */
534 pkgmgrinfo_appinfo_h handle;
535 int ret = pkgmgrinfo_appinfo_get_appinfo(engine_name.c_str(), &handle);
536 if (PMINFO_R_OK == ret) {
537 char *root_path = nullptr;
538 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root_path);
539 if (PMINFO_R_OK == ret && nullptr != root_path) {
540 string path = root_path;
542 path += MA_WAKEUP_DEDICATED_ENGINE_PATH;
543 add_engine(engine_name, path);
545 pkgmgrinfo_appinfo_destroy_appinfo(handle);
547 /* Find again to add appid to the newly created engine's assistant list */
548 const auto &new_iter = find_if(mEngineInfo.begin(), mEngineInfo.end(),
549 [engine_name](const EngineInfo& info) {
550 return (0 == info.engine_name.compare(engine_name));
552 if (mEngineInfo.end() != new_iter) {
553 new_iter->assistant_list.push_back(appid);
556 /* If the engine already exists, simply add the appid to the assistant list */
557 iter->assistant_list.push_back(appid);
561 void CWakeupEngineManager::engine_add_wakeup_word(string appid, string wakeup_word, string language)
563 for (const auto& info : mEngineInfo) {
564 bool found = contains(info.assistant_list, appid);
566 if (info.interface.add_wakeup_word) {
568 info.interface.add_wakeup_word(appid.c_str(), wakeup_word.c_str(), language.c_str());
569 } catch (const std::exception& e) {
570 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
571 info.engine_name.c_str(), e.what());
574 MWR_LOGE("Wakeup Engine does not provide add_wakeup_word");
580 void CWakeupEngineManager::engine_remove_wakeup_word(string appid, string wakeup_word, string language)
582 for (const auto& info : mEngineInfo) {
583 bool found = contains(info.assistant_list, appid);
585 if (info.interface.remove_wakeup_word) {
587 info.interface.remove_wakeup_word(appid.c_str(), wakeup_word.c_str(), language.c_str());
588 } catch (const std::exception& e) {
589 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
590 info.engine_name.c_str(), e.what());
593 MWR_LOGE("Wakeup Engine does not provide remove_wakeup_word");
599 void CWakeupEngineManager::engine_set_assistant_specific_command(string appid, string command)
601 for (const auto& info : mEngineInfo) {
602 bool found = contains(info.assistant_list, appid);
604 if (info.interface.set_assistant_specific_command) {
606 info.interface.set_assistant_specific_command(appid.c_str(), command.c_str());
607 } catch (const std::exception& e) {
608 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
609 info.engine_name.c_str(), e.what());
616 void CWakeupEngineManager::engine_feed_audio_data(long time, void* data, int len)
618 for (const auto& info : mEngineInfo) {
619 if (info.activated &&
620 info.audio_data_require_status &&
621 info.interface.feed_audio_data) {
623 bool filtered_out = false;
624 /* After a wakeup event, wakeup engines other than the selected one
625 does not need to receive audio data. */
626 if (WAKEUP_MANAGER_STATE_UTTERANCE == mWakeupManagerState) {
627 if (mSelectedEngine && &info != mSelectedEngine) {
632 int ret = info.interface.feed_audio_data(time, data, len);
634 LOGE("[ERROR] Fail to feed speech data, ret(%d) : %s", ret, info.engine_name.c_str());
637 } catch (const std::exception& e) {
638 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
639 info.engine_name.c_str(), e.what());
645 void CWakeupEngineManager::engine_set_dependency_module_command(string engine_name, string command)
647 const auto& iter = find_if(mEngineInfo.begin(), mEngineInfo.end(),
648 [engine_name](const EngineInfo& info) {
649 return (0 == info.engine_name.compare(engine_name));
652 if (mEngineInfo.end() != iter) {
653 if (iter->activated &&
654 iter->interface.set_dependency_module_command) {
656 int ret = iter->interface.set_dependency_module_command(command.c_str());
658 LOGE("[ERROR] Fail to set dependency module command, ret(%d) : %s",
659 ret, iter->engine_name.c_str());
661 } catch (const std::exception& e) {
662 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
663 iter->engine_name.c_str(), e.what());
669 bool CWakeupEngineManager::on_wakeup_event(string engine_name, mas_wakeup_event_info info)
673 for (const auto& observer : mObservers) {
675 if (!observer->on_wakeup_event(engine_name, info)) {
676 LOGE("[Recorder WARNING] One of the observer returned false");
684 bool CWakeupEngineManager::on_speech_status(string engine_name, mas_speech_status_e status)
688 for (const auto& observer : mObservers) {
690 if (!observer->on_speech_status(engine_name, status)) {
691 LOGE("[Recorder WARNING] One of the observer returned false");
699 bool CWakeupEngineManager::on_error(string engine_name, int error_code, string error_message)
703 for (const auto& observer : mObservers) {
705 if (!observer->on_error(engine_name, error_code, error_message)) {
706 LOGE("[Recorder WARNING] One of the observer returned false");
714 bool CWakeupEngineManager::on_audio_data_require_status(string engine_name, bool require)
716 MWR_LOGI("[ENTER] %s, %d", engine_name.c_str(), require);
721 for (auto& info : mEngineInfo) {
722 if (info.engine_name.compare(engine_name) == 0) {
724 info.audio_data_require_status = require;
726 if (info.activated && info.audio_data_require_status) {
730 MWR_LOGD("count : %d", count);
732 mAudioDataRequired = true;
734 mAudioDataRequired = false;
738 for (const auto& observer : mObservers) {
740 if (!observer->on_audio_data_require_status(engine_name, require)) {
741 LOGE("[Recorder WARNING] One of the observer returned false");
750 bool CWakeupEngineManager::on_wakeup_engine_command(string engine_name, mas_wakeup_engine_command_target_e target, string assistant_name, string command)
754 for (const auto& observer : mObservers) {
756 if (MAS_WAKEUP_ENGINE_COMMAND_TARGET_DEPENDENCY_MODULE == target) {
757 if (!observer->on_wakeup_engine_command(target, engine_name, assistant_name, command)) {
758 LOGE("[Recorder WARNING] One of the observer returned false");
761 const auto& iter = find_if(mEngineInfo.begin(), mEngineInfo.end(),
762 [engine_name](const EngineInfo& info) {
763 return (0 == info.engine_name.compare(engine_name));
765 if (mEngineInfo.end() != iter) {
766 for (const auto& assistant : iter->assistant_list) {
767 if (0 == assistant_name.compare(assistant) ||
768 MAS_WAKEUP_ENGINE_COMMAND_TARGET_ALL_ASSISTANTS == target) {
769 if (!observer->on_wakeup_engine_command(target, engine_name, assistant, command)) {
770 LOGE("[Recorder WARNING] One of the observer returned false");
782 void CWakeupEngineManager::add_engine(string name, string path)
784 MWR_LOGD("Name (%s), Filepath(%s)", name.c_str(), path.c_str());
788 info.engine_handle = dlopen(path.c_str(), RTLD_LAZY);
789 if (nullptr != (error = dlerror()) || nullptr == info.engine_handle) {
790 MWR_LOGD("[ERROR] Fail to dlopen(%s), error(%s)", path.c_str(), error);
791 if (info.engine_handle) dlclose(info.engine_handle);
795 /* Interfaces without version information */
796 info.interface.initialize =
797 (wakeup_engine_initialize)dlsym(info.engine_handle,
798 MA_WAKEUP_ENGINE_FUNC_INITIALIZE);
799 info.interface.deinitialize =
800 (wakeup_engine_deinitialize)dlsym(info.engine_handle,
801 MA_WAKEUP_ENGINE_FUNC_DEINITIALIZE);
802 info.interface.activate =
803 (wakeup_engine_activate)dlsym(info.engine_handle,
804 MA_WAKEUP_ENGINE_FUNC_ACTIVATE);
805 info.interface.deactivate =
806 (wakeup_engine_deactivate)dlsym(info.engine_handle,
807 MA_WAKEUP_ENGINE_FUNC_DEACTIVATE);
808 info.interface.add_wakeup_word =
809 (wakeup_engine_add_wakeup_word)dlsym(info.engine_handle,
810 MA_WAKEUP_ENGINE_FUNC_ADD_WAKEUP_WORD);
811 info.interface.remove_wakeup_word =
812 (wakeup_engine_remove_wakeup_word)dlsym(info.engine_handle,
813 MA_WAKEUP_ENGINE_FUNC_REMOVE_WAKEUP_WORD);
814 info.interface.add_language =
815 (wakeup_engine_add_language)dlsym(info.engine_handle,
816 MA_WAKEUP_ENGINE_FUNC_ADD_LANGUAGE);
817 info.interface.set_language =
818 (wakeup_engine_set_language)dlsym(info.engine_handle,
819 MA_WAKEUP_ENGINE_FUNC_SET_LANGUAGE);
820 info.interface.update_manager_state =
821 (wakeup_engine_update_manager_state)dlsym(info.engine_handle,
822 MA_WAKEUP_ENGINE_FUNC_UPDATE_MANAGER_STATE);
823 info.interface.update_recognition_result =
824 (wakeup_engine_update_recognition_result)dlsym(info.engine_handle,
825 MA_WAKEUP_ENGINE_FUNC_UPDATE_RECOGNITION_RESULT);
826 info.interface.set_audio_format =
827 (wakeup_engine_set_audio_format)dlsym(info.engine_handle,
828 MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_FORMAT);
829 info.interface.get_audio_format =
830 (wakeup_engine_get_audio_format)dlsym(info.engine_handle,
831 MA_WAKEUP_ENGINE_FUNC_GET_AUDIO_FORMAT);
832 info.interface.feed_audio_data =
833 (wakeup_engine_feed_audio_data)dlsym(info.engine_handle,
834 MA_WAKEUP_ENGINE_FUNC_FEED_AUDIO_DATA);
835 info.interface.get_utterance_data_count =
836 (wakeup_engine_get_utterance_data_count)dlsym(info.engine_handle,
837 MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA_COUNT);
838 info.interface.get_utterance_data =
839 (wakeup_engine_get_utterance_data)dlsym(info.engine_handle,
840 MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA);
841 info.interface.get_wake_word_data_count =
842 (wakeup_engine_get_wake_word_data_count)dlsym(info.engine_handle,
843 MA_WAKEUP_ENGINE_FUNC_GET_WAKE_WORD_DATA_COUNT);
844 info.interface.get_wake_word_data =
845 (wakeup_engine_get_wake_word_data)dlsym(info.engine_handle,
846 MA_WAKEUP_ENGINE_FUNC_GET_WAKE_WORD_DATA);
847 info.interface.set_assistant_specific_command =
848 (wakeup_engine_set_assistant_specific_command)dlsym(info.engine_handle,
849 MA_WAKEUP_ENGINE_FUNC_SET_ASSISTANT_SPECIFIC_COMMAND);
850 info.interface.set_wake_word_audio_require_flag =
851 (wakeup_engine_set_wake_word_audio_require_flag)dlsym(info.engine_handle,
852 MA_WAKEUP_ENGINE_FUNC_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG);
853 info.interface.set_wakeup_event_callback =
854 (wakeup_engine_set_wakeup_event_callback)dlsym(info.engine_handle,
855 MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_EVENT_CALLBACK);
856 info.interface.set_speech_status_callback =
857 (wakeup_engine_set_speech_status_callback)dlsym(info.engine_handle,
858 MA_WAKEUP_ENGINE_FUNC_SET_SPEECH_STATUS_CALLBACK);
859 info.interface.set_error_callback =
860 (wakeup_engine_set_error_callback)dlsym(info.engine_handle,
861 MA_WAKEUP_ENGINE_FUNC_SET_ERROR_CALLBACK);
862 info.interface.set_audio_data_require_status_callback =
863 (wakeup_engine_set_audio_data_require_status_callback)dlsym(info.engine_handle,
864 MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_DATA_REQUIRE_STATUS_CALLBACK);
865 info.interface.set_wakeup_engine_command_callback =
866 (wakeup_engine_set_wakeup_engine_command_callback)dlsym(info.engine_handle,
867 MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_ENGINE_COMMAND_CALLBACK);
869 /* Interfaces after version 1 */
870 info.interface.get_version =
871 (wakeup_engine_get_version)dlsym(info.engine_handle,
872 MA_WAKEUP_ENGINE_FUNC_GET_VERSION);
873 info.interface.set_dependency_module_command =
874 (wakeup_engine_set_dependency_module_command)dlsym(info.engine_handle,
875 MA_WAKEUP_ENGINE_FUNC_SET_DEPENDENCY_MODULE_COMMAND);
878 info.engine_path = path;
879 info.engine_name = name;
881 info.activated = false;
882 info.audio_data_require_status = false;
884 /* All the necessary information has already been set properly */
885 mEngineInfo.push_back(info);
887 MWR_LOGD("Initializing wakeup engine : %s %p",
888 info.engine_path.c_str(),
889 info.interface.initialize);
891 /* Workaround for registering C-style callbacks */
893 CWakeupEngineManager *manager;
897 static deque<CallbackUserData> callback_user_data;
899 CallbackUserData user_data;
900 user_data.manager = this;
901 user_data.engine_name = info.engine_name;
902 callback_user_data.push_back(user_data);
905 if (info.interface.set_wakeup_event_callback) {
906 info.interface.set_wakeup_event_callback(
907 [](mas_wakeup_event_info info, void* user_data) {
908 CallbackUserData *data = static_cast<CallbackUserData*>(user_data);
909 if (nullptr == data) return;
910 if (nullptr == data->manager) return;
911 info.wakeup_engine = data->engine_name.c_str();
912 data->manager->on_wakeup_event(data->engine_name, info);
913 }, &(callback_user_data.back()));
916 if (info.interface.set_audio_data_require_status_callback) {
917 info.interface.set_audio_data_require_status_callback(
918 [](bool require, void* user_data) {
919 CallbackUserData *data = static_cast<CallbackUserData*>(user_data);
920 if (nullptr == data) return;
921 if (nullptr == data->manager) return;
922 data->manager->on_audio_data_require_status(data->engine_name, require);
923 }, &(callback_user_data.back()));
926 if (info.interface.set_wakeup_engine_command_callback) {
927 info.interface.set_wakeup_engine_command_callback(
928 [](mas_wakeup_engine_command_target_e target,
929 const char* assistant_name, const char* command, void* user_data) {
930 CallbackUserData* data = static_cast<CallbackUserData*>(user_data);
931 if (nullptr == data) return;
932 if (nullptr == data->manager) return;
933 if (nullptr == command) return;
934 data->manager->on_wakeup_engine_command(
935 data->engine_name, target, (assistant_name ? assistant_name : ""), command);
936 }, &(callback_user_data.back()));
939 if (info.interface.initialize) {
940 info.interface.initialize();
942 if (info.interface.get_version) {
944 if (0 == info.interface.get_version(&version)) {
945 info.version = version;
948 } catch (const std::exception& e) {
949 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
950 info.engine_name.c_str(), e.what());
954 void CWakeupEngineManager::add_engine_directory(string name, string path)
956 if (0 == path.size()) return;
958 DIR* dp = opendir(path.c_str());
960 MWR_LOGD("Failed opening directory : %s", path.c_str());
962 struct dirent *dirp = NULL;
968 if (!strcmp(".", dirp->d_name) || !strcmp("..", dirp->d_name))
971 if (DT_REG != dirp->d_type) /* If not a regular file */
976 filepath += dirp->d_name;
978 if (filepath.length() >= _POSIX_PATH_MAX) {
979 MWR_LOGD("File path is too long : %s", filepath.c_str());
983 add_engine(name, filepath);
985 } while (NULL != dirp);