1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd. 2023-2023.
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 "update_engine.h"
16 #include <fstream>
17 #include "securec.h"
18 #include "intell_voice_log.h"
19
20 #include "update_adapter_listener.h"
21 #include "time_util.h"
22 #include "scope_guard.h"
23 #include "adapter_callback_service.h"
24 #include "engine_callback_message.h"
25 #include "history_info_mgr.h"
26 #include <thread>
27 #include "update_engine_utils.h"
28 #include "engine_host_manager.h"
29 #include "intell_voice_definitions.h"
30
31 #define LOG_TAG "UpdateEngine"
32
33 using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
34 using namespace OHOS::IntellVoiceUtils;
35 using namespace OHOS::AudioStandard;
36
37 namespace OHOS {
38 namespace IntellVoiceEngine {
UpdateEngine()39 UpdateEngine::UpdateEngine()
40 {
41 INTELL_VOICE_LOG_INFO("enter");
42 }
43
~UpdateEngine()44 UpdateEngine::~UpdateEngine()
45 {
46 INTELL_VOICE_LOG_INFO("enter");
47 callback_ = nullptr;
48 }
49
OnCommitEnrollComplete(int32_t result)50 void UpdateEngine::OnCommitEnrollComplete(int32_t result)
51 {
52 std::lock_guard<std::mutex> lock(mutex_);
53 INTELL_VOICE_LOG_INFO("commit enroll complete, result %{public}d", result);
54 if (adapter_ == nullptr) {
55 INTELL_VOICE_LOG_INFO("already detach");
56 return;
57 }
58
59 updateResult_ = (result == 0 ? UpdateState::UPDATE_STATE_COMPLETE_SUCCESS :
60 UpdateState::UPDATE_STATE_COMPLETE_FAIL);
61 if (updateResult_ == UpdateState::UPDATE_STATE_COMPLETE_SUCCESS) {
62 ProcDspModel(ReadDspModel(OHOS::HDI::IntelligentVoice::Engine::V1_0::DSP_MODLE));
63 /* save new version number */
64 UpdateEngineUtils::SaveWakeupVesion();
65 std::string wakeupPhrase = EngineUtil::GetParameter("wakeup_phrase");
66 if (wakeupPhrase.empty()) {
67 INTELL_VOICE_LOG_ERROR("wakeup phrase is empty");
68 } else {
69 HistoryInfoMgr::GetInstance().SetStringKVPair(KEY_WAKEUP_PHRASE, wakeupPhrase);
70 }
71
72 INTELL_VOICE_LOG_INFO("update save version");
73 }
74
75 EngineCallbackMessage::CallFunc(HANDLE_UPDATE_COMPLETE, static_cast<int32_t>(updateResult_), param_);
76 }
77
OnUpdateEvent(int32_t msgId,int32_t result)78 void UpdateEngine::OnUpdateEvent(int32_t msgId, int32_t result)
79 {
80 if (msgId == INTELL_VOICE_ENGINE_MSG_COMMIT_ENROLL_COMPLETE) {
81 wptr<UpdateEngine> thisWptr(this);
82 std::thread([thisWptr, result]() {
83 sptr<UpdateEngine> thisSptr = thisWptr.promote();
84 if (thisSptr != nullptr) {
85 thisSptr->OnCommitEnrollComplete(result);
86 } else {
87 INTELL_VOICE_LOG_WARN("updateEngine is null");
88 }
89 }).detach();
90 }
91 }
92
Init(const std::string & param,bool reEnroll)93 bool UpdateEngine::Init(const std::string ¶m, bool reEnroll)
94 {
95 if (!EngineUtil::CreateAdapterInner(EngineHostManager::GetInstance(), UPDATE_ADAPTER_TYPE)) {
96 INTELL_VOICE_LOG_ERROR("failed to create adapter");
97 return false;
98 }
99
100 if (param == "WhisperVprUpdate") {
101 EngineUtil::SetParameter("WhisperVpr=true");
102 }
103 if (reEnroll) {
104 EngineUtil::SetParameter("WhisperReEnroll=true");
105 }
106
107 SetCallback(nullptr);
108 EngineUtil::SetLanguage();
109 EngineUtil::SetArea();
110 SetDspFeatures();
111
112 if (!param.empty()) {
113 EngineUtil::SetImproveParam();
114 SetParameter(param);
115 }
116
117 IntellVoiceEngineInfo info = {
118 .wakeupPhrase = HistoryInfoMgr::GetInstance().GetStringKVPair(KEY_WAKEUP_PHRASE),
119 .minBufSize = 1280,
120 .sampleChannels = 1,
121 .bitsPerSample = 16,
122 .sampleRate = 16000,
123 };
124 int ret = Attach(info);
125 if (ret != 0) {
126 INTELL_VOICE_LOG_ERROR("attach err");
127 EngineUtil::ReleaseAdapterInner(EngineHostManager::GetInstance());
128 return false;
129 }
130
131 param_= param;
132 return true;
133 }
134
SetCallback(sptr<IRemoteObject> object)135 void UpdateEngine::SetCallback(sptr<IRemoteObject> object)
136 {
137 std::lock_guard<std::mutex> lock(mutex_);
138
139 callback_ = sptr<IIntellVoiceEngineCallback>(new (std::nothrow) UpdateAdapterListener(
140 std::bind(&UpdateEngine::OnUpdateEvent, this, std::placeholders::_1, std::placeholders::_2)));
141 if (callback_ == nullptr) {
142 INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
143 return;
144 }
145
146 adapter_->SetCallback(callback_);
147 }
148
Attach(const IntellVoiceEngineInfo & info)149 int32_t UpdateEngine::Attach(const IntellVoiceEngineInfo &info)
150 {
151 std::lock_guard<std::mutex> lock(mutex_);
152 INTELL_VOICE_LOG_INFO("attach");
153 if (adapter_ == nullptr) {
154 INTELL_VOICE_LOG_ERROR("adapter is nullptr");
155 return -1;
156 }
157
158 IntellVoiceEngineAdapterInfo adapterInfo = {
159 .wakeupPhrase = info.wakeupPhrase,
160 .minBufSize = info.minBufSize,
161 .sampleChannels = info.sampleChannels,
162 .bitsPerSample = info.bitsPerSample,
163 .sampleRate = info.sampleRate,
164 };
165 return adapter_->Attach(adapterInfo);
166 }
167
Detach(void)168 int32_t UpdateEngine::Detach(void)
169 {
170 INTELL_VOICE_LOG_INFO("enter");
171 std::lock_guard<std::mutex> lock(mutex_);
172 if (adapter_ == nullptr) {
173 INTELL_VOICE_LOG_WARN("already detach");
174 return 0;
175 }
176
177 int ret = adapter_->Detach();
178 EngineUtil::ReleaseAdapterInner(EngineHostManager::GetInstance());
179
180 if (updateResult_ == UpdateState::UPDATE_STATE_DEFAULT) {
181 INTELL_VOICE_LOG_WARN("detach defore receive commit enroll msg");
182 EngineCallbackMessage::CallFunc(HANDLE_UPDATE_COMPLETE,
183 static_cast<int32_t>(UpdateState::UPDATE_STATE_DEFAULT), param_);
184 }
185 return ret;
186 }
187
Start(bool isLast)188 int32_t UpdateEngine::Start(bool isLast)
189 {
190 INTELL_VOICE_LOG_INFO("enter");
191 std::lock_guard<std::mutex> lock(mutex_);
192 return 0;
193 }
194
Stop()195 int32_t UpdateEngine::Stop()
196 {
197 INTELL_VOICE_LOG_INFO("enter");
198 std::lock_guard<std::mutex> lock(mutex_);
199 return EngineUtil::Stop();
200 }
201
SetParameter(const std::string & keyValueList)202 int32_t UpdateEngine::SetParameter(const std::string &keyValueList)
203 {
204 INTELL_VOICE_LOG_INFO("enter");
205 std::lock_guard<std::mutex> lock(mutex_);
206 return EngineUtil::SetParameter(keyValueList);
207 }
208
GetParameter(const std::string & key)209 std::string UpdateEngine::GetParameter(const std::string &key)
210 {
211 INTELL_VOICE_LOG_INFO("enter");
212 std::lock_guard<std::mutex> lock(mutex_);
213 return EngineUtil::GetParameter(key);
214 }
215 }
216 }