• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)93 bool UpdateEngine::Init(const std::string &param)
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     SetCallback(nullptr);
101     EngineUtil::SetLanguage();
102     EngineUtil::SetArea();
103     SetDspFeatures();
104 
105     if (!param.empty()) {
106         SetParameter(param);
107     }
108 
109     IntellVoiceEngineInfo info = {
110         .wakeupPhrase = HistoryInfoMgr::GetInstance().GetStringKVPair(KEY_WAKEUP_PHRASE),
111         .minBufSize = 1280,
112         .sampleChannels = 1,
113         .bitsPerSample = 16,
114         .sampleRate = 16000,
115     };
116     int ret = Attach(info);
117     if (ret != 0) {
118         INTELL_VOICE_LOG_ERROR("attach err");
119         EngineUtil::ReleaseAdapterInner(EngineHostManager::GetInstance());
120         return false;
121     }
122 
123     param_= param;
124     return true;
125 }
126 
SetCallback(sptr<IRemoteObject> object)127 void UpdateEngine::SetCallback(sptr<IRemoteObject> object)
128 {
129     std::lock_guard<std::mutex> lock(mutex_);
130 
131     callback_ = sptr<IIntellVoiceEngineCallback>(new (std::nothrow) UpdateAdapterListener(
132         std::bind(&UpdateEngine::OnUpdateEvent, this, std::placeholders::_1, std::placeholders::_2)));
133     if (callback_ == nullptr) {
134         INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
135         return;
136     }
137 
138     adapter_->SetCallback(callback_);
139 }
140 
Attach(const IntellVoiceEngineInfo & info)141 int32_t UpdateEngine::Attach(const IntellVoiceEngineInfo &info)
142 {
143     std::lock_guard<std::mutex> lock(mutex_);
144     INTELL_VOICE_LOG_INFO("attach");
145     if (adapter_ == nullptr) {
146         INTELL_VOICE_LOG_ERROR("adapter is nullptr");
147         return -1;
148     }
149 
150     IntellVoiceEngineAdapterInfo adapterInfo = {
151         .wakeupPhrase = info.wakeupPhrase,
152         .minBufSize = info.minBufSize,
153         .sampleChannels = info.sampleChannels,
154         .bitsPerSample = info.bitsPerSample,
155         .sampleRate = info.sampleRate,
156     };
157     return adapter_->Attach(adapterInfo);
158 }
159 
Detach(void)160 int32_t UpdateEngine::Detach(void)
161 {
162     INTELL_VOICE_LOG_INFO("enter");
163     std::lock_guard<std::mutex> lock(mutex_);
164     if (adapter_ == nullptr) {
165         INTELL_VOICE_LOG_WARN("already detach");
166         return 0;
167     }
168 
169     int ret =  adapter_->Detach();
170     EngineUtil::ReleaseAdapterInner(EngineHostManager::GetInstance());
171 
172     if (updateResult_ == UpdateState::UPDATE_STATE_DEFAULT) {
173         INTELL_VOICE_LOG_WARN("detach defore receive commit enroll msg");
174         EngineCallbackMessage::CallFunc(HANDLE_UPDATE_COMPLETE,
175             static_cast<int32_t>(UpdateState::UPDATE_STATE_DEFAULT), param_);
176     }
177     return ret;
178 }
179 
Start(bool isLast)180 int32_t UpdateEngine::Start(bool isLast)
181 {
182     INTELL_VOICE_LOG_INFO("enter");
183     std::lock_guard<std::mutex> lock(mutex_);
184     return 0;
185 }
186 
Stop()187 int32_t UpdateEngine::Stop()
188 {
189     INTELL_VOICE_LOG_INFO("enter");
190     std::lock_guard<std::mutex> lock(mutex_);
191     return EngineUtil::Stop();
192 }
193 
SetParameter(const std::string & keyValueList)194 int32_t UpdateEngine::SetParameter(const std::string &keyValueList)
195 {
196     INTELL_VOICE_LOG_INFO("enter");
197     std::lock_guard<std::mutex> lock(mutex_);
198     return EngineUtil::SetParameter(keyValueList);
199 }
200 
GetParameter(const std::string & key)201 std::string UpdateEngine::GetParameter(const std::string &key)
202 {
203     INTELL_VOICE_LOG_INFO("enter");
204     std::lock_guard<std::mutex> lock(mutex_);
205     return EngineUtil::GetParameter(key);
206 }
207 }
208 }