1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "high_power_wakeup_engine.h" 16 #include "idevmgr_hdi.h" 17 #include "ipc_skeleton.h" 18 #include "v1_2/intell_voice_engine_types.h" 19 #include "intell_voice_engine_manager.h" 20 #include "intell_voice_log.h" 21 #include "engine_host_manager.h" 22 #include "adapter_callback_service.h" 23 24 #define LOG_TAG "HighPowerWakeupEngine" 25 26 using namespace OHOS::IntellVoiceUtils; 27 using OHOS::HDI::DeviceManager::V1_0::IDeviceManager; 28 using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0; 29 30 namespace OHOS { 31 namespace IntellVoiceEngine { 32 static const std::string CALLBACK_EXIST = "is_callback_exist"; 33 static const std::string HIGH_POWER_THREAD_NAME = "HighPowerEngThread"; 34 static constexpr uint32_t MAX_WAKEUP_TASK_NUM = 200; 35 HighPowerWakeupEngine()36HighPowerWakeupEngine::HighPowerWakeupEngine() 37 { 38 INTELL_VOICE_LOG_INFO("enter"); 39 adapterListener_ = std::make_shared<HighPowerAdapterListener>(); 40 if (adapterListener_ == nullptr) { 41 INTELL_VOICE_LOG_ERROR("adapterListener_ is nullptr"); 42 } 43 } 44 ~HighPowerWakeupEngine()45HighPowerWakeupEngine::~HighPowerWakeupEngine() 46 { 47 INTELL_VOICE_LOG_INFO("enter"); 48 } 49 Init(const std::string &,bool reEnroll)50bool HighPowerWakeupEngine::Init(const std::string & /* param */, bool reEnroll) 51 { 52 INTELL_VOICE_LOG_INFO("enter"); 53 if (!EngineUtil::CreateAdapterInner(EngineHostManager::GetInstance(), WAKEUP_ADAPTER_TYPE)) { 54 INTELL_VOICE_LOG_ERROR("failed to create adapter"); 55 return -1; 56 } 57 58 if (!SetCallbackInner()) { 59 INTELL_VOICE_LOG_ERROR("failed to set callback"); 60 return -1; 61 } 62 return true; 63 } 64 SetCallback(sptr<IRemoteObject> object)65void HighPowerWakeupEngine::SetCallback(sptr<IRemoteObject> object) 66 { 67 std::unique_lock<std::mutex> lock(mutex_); 68 sptr<IIntelligentVoiceEngineCallback> callback = iface_cast<IIntelligentVoiceEngineCallback>(object); 69 if (callback == nullptr) { 70 INTELL_VOICE_LOG_WARN("clear callback"); 71 } 72 if (adapterListener_ == nullptr) { 73 INTELL_VOICE_LOG_ERROR("adapterListener_ is nullptr"); 74 return; 75 } 76 adapterListener_->SetCallback(callback); 77 } 78 GetParameter(const std::string & key)79std::string HighPowerWakeupEngine::GetParameter(const std::string &key) 80 { 81 if (key == CALLBACK_EXIST) { 82 if (adapterListener_ == nullptr) { 83 INTELL_VOICE_LOG_ERROR("adapterListener_ is nullptr"); 84 return "false"; 85 } else { 86 return adapterListener_->GetCallbackStatus(); 87 } 88 } 89 return ""; 90 } 91 Detach(void)92int32_t HighPowerWakeupEngine::Detach(void) 93 { 94 std::unique_lock<std::mutex> lock(mutex_); 95 if (adapter_ != nullptr) { 96 adapter_->Detach(); 97 ReleaseAdapterInner(EngineHostManager::GetInstance()); 98 } 99 return 0; 100 } 101 SetCallbackInner()102bool HighPowerWakeupEngine::SetCallbackInner() 103 { 104 INTELL_VOICE_LOG_INFO("enter"); 105 if (adapter_ == nullptr) { 106 INTELL_VOICE_LOG_ERROR("adapter is nullptr"); 107 return false; 108 } 109 110 if (adapterListener_ == nullptr) { 111 INTELL_VOICE_LOG_ERROR("adapter listener is nullptr"); 112 return false; 113 } 114 115 callback_ = sptr<IIntellVoiceEngineCallback>(new (std::nothrow) AdapterCallbackService(adapterListener_)); 116 if (callback_ == nullptr) { 117 INTELL_VOICE_LOG_ERROR("callback_ is nullptr"); 118 return false; 119 } 120 121 adapter_->SetCallback(callback_); 122 return true; 123 } 124 } // namespace IntellVoice 125 } // namespace OHOS 126