1 /*
2 * Copyright (c) 2023 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 "engine_host_manager.h"
16
17 #include "iproxy_broker.h"
18 #include "intell_voice_log.h"
19 #include "data_operation_callback.h"
20 #include "intell_voice_util.h"
21 #include "adapter_host_manager.h"
22
23 #define LOG_TAG "EngineHostManager"
24
25 using namespace OHOS::IntellVoiceUtils;
26
27 namespace OHOS {
28 namespace IntellVoiceEngine {
29 static constexpr uint32_t MINOR_VERSION_2 = 2;
30
~EngineHostManager()31 EngineHostManager::~EngineHostManager()
32 {
33 engineHostProxy1_0_ = nullptr;
34 engineHostProxy1_1_ = nullptr;
35 engineHostProxy1_2_ = nullptr;
36 engineHdiDeathRecipient_ = nullptr;
37 dataOprCb_ = nullptr;
38 }
39
Init()40 bool EngineHostManager::Init()
41 {
42 INTELL_VOICE_LOG_INFO("enter");
43 engineHostProxy1_0_ = OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager::Get();
44 if (engineHostProxy1_0_ == nullptr) {
45 INTELL_VOICE_LOG_ERROR("engineHostProxy1_0_ is nullptr");
46 return false;
47 }
48
49 uint32_t majorVer = 0;
50 uint32_t minorVer = 0;
51 engineHostProxy1_0_->GetVersion(majorVer, minorVer);
52 INTELL_VOICE_LOG_INFO("major ver is %{public}u, minor ver is %{public}u", majorVer, minorVer);
53 if (IntellVoiceUtil::GetHdiVersionId(majorVer, minorVer) == IntellVoiceUtil::GetHdiVersionId(1, 1)) {
54 INTELL_VOICE_LOG_INFO("version is 1.1");
55 auto castResult_V1_1 =
56 OHOS::HDI::IntelligentVoice::Engine::V1_1::IIntellVoiceEngineManager::CastFrom(engineHostProxy1_0_);
57 if (castResult_V1_1 != nullptr) {
58 engineHostProxy1_1_ = castResult_V1_1;
59 }
60 } else if (IntellVoiceUtil::GetHdiVersionId(majorVer, minorVer) ==
61 IntellVoiceUtil::GetHdiVersionId(1, MINOR_VERSION_2)) {
62 INTELL_VOICE_LOG_INFO("version is 1.2");
63 auto castResult_V1_2 =
64 OHOS::HDI::IntelligentVoice::Engine::V1_2::IIntellVoiceEngineManager::CastFrom(engineHostProxy1_0_);
65 if (castResult_V1_2 != nullptr) {
66 engineHostProxy1_2_ = castResult_V1_2;
67 engineHostProxy1_1_ = engineHostProxy1_2_;
68 }
69 }
70
71 return true;
72 }
73
RegisterEngineHDIDeathRecipient()74 bool EngineHostManager::RegisterEngineHDIDeathRecipient()
75 {
76 INTELL_VOICE_LOG_INFO("enter");
77 if (engineHostProxy1_0_ == nullptr) {
78 INTELL_VOICE_LOG_ERROR("failed to get engine manager");
79 return false;
80 }
81 sptr<IRemoteObject> object = OHOS::HDI::hdi_objcast<
82 OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager>(engineHostProxy1_0_);
83 if (object == nullptr) {
84 INTELL_VOICE_LOG_ERROR("object is nullptr");
85 return false;
86 }
87
88 engineHdiDeathRecipient_ = new (std::nothrow) IntellVoiceDeathRecipient(
89 std::bind(&EngineHostManager::OnEngineHDIDiedCallback));
90 if (engineHdiDeathRecipient_ == nullptr) {
91 INTELL_VOICE_LOG_ERROR("create death recipient failed");
92 return false;
93 }
94
95 return object->AddDeathRecipient(engineHdiDeathRecipient_);
96 }
97
DeregisterEngineHDIDeathRecipient()98 void EngineHostManager::DeregisterEngineHDIDeathRecipient()
99 {
100 INTELL_VOICE_LOG_INFO("enter");
101 if (engineHdiDeathRecipient_ == nullptr) {
102 INTELL_VOICE_LOG_ERROR("recipient is nullptr");
103 return;
104 }
105
106 if (engineHostProxy1_0_ == nullptr) {
107 INTELL_VOICE_LOG_ERROR("engineHostProxy1_0_ is nullptr");
108 return;
109 }
110
111 sptr<IRemoteObject> object = OHOS::HDI::hdi_objcast<
112 OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineManager>(engineHostProxy1_0_);
113 if (object == nullptr) {
114 INTELL_VOICE_LOG_ERROR("object is nullptr");
115 return;
116 }
117
118 object->RemoveDeathRecipient(engineHdiDeathRecipient_);
119 }
120
SetDataOprCallback()121 void EngineHostManager::SetDataOprCallback()
122 {
123 INTELL_VOICE_LOG_INFO("enter");
124 if (dataOprCb_ != nullptr) {
125 INTELL_VOICE_LOG_INFO("already set data opr callback");
126 return;
127 }
128
129 if (engineHostProxy1_1_ == nullptr) {
130 INTELL_VOICE_LOG_ERROR("engineHostProxy1_1_ is nullptr");
131 return;
132 }
133
134 dataOprCb_ = sptr<OHOS::HDI::IntelligentVoice::Engine::V1_1::IIntellVoiceDataOprCallback>(
135 new (std::nothrow) DataOperationCallback());
136 if (dataOprCb_ == nullptr) {
137 INTELL_VOICE_LOG_ERROR("create data opr callback failed");
138 return;
139 }
140
141 engineHostProxy1_1_->SetDataOprCallback(dataOprCb_);
142 }
143
CreateEngineAdapter(const IntellVoiceEngineAdapterDescriptor & desc)144 std::shared_ptr<IAdapterHostManager> EngineHostManager::CreateEngineAdapter(
145 const IntellVoiceEngineAdapterDescriptor &desc)
146 {
147 INTELL_VOICE_LOG_INFO("enter");
148 if (engineHostProxy1_0_ == nullptr) {
149 INTELL_VOICE_LOG_ERROR("engineHostProxy1_0_ is nullptr");
150 return nullptr;
151 }
152
153 auto adapter = std::make_shared<AdapterHostManager>();
154 if (adapter == nullptr) {
155 INTELL_VOICE_LOG_ERROR("failed to create engine adapter");
156 return nullptr;
157 }
158
159 if (!adapter->Init(desc)) {
160 return nullptr;
161 }
162
163 return adapter;
164 }
165
ReleaseEngineAdapter(const IntellVoiceEngineAdapterDescriptor & desc)166 void EngineHostManager::ReleaseEngineAdapter(const IntellVoiceEngineAdapterDescriptor &desc)
167 {
168 if (engineHostProxy1_0_ == nullptr) {
169 INTELL_VOICE_LOG_ERROR("engineHostProxy1_0_ is nullptr");
170 return;
171 }
172 engineHostProxy1_0_->ReleaseAdapter(desc);
173 }
174
175
GetUploadFiles(int numMax,std::vector<UploadHdiFile> & files)176 int EngineHostManager::GetUploadFiles(int numMax, std::vector<UploadHdiFile> &files)
177 {
178 INTELL_VOICE_LOG_INFO("enter");
179 if (engineHostProxy1_2_ == nullptr) {
180 INTELL_VOICE_LOG_ERROR("engineHostProxy1_2_ is nullptr");
181 return -1;
182 }
183
184 return engineHostProxy1_2_->GetUploadFiles(numMax, files);
185 }
186
OnEngineHDIDiedCallback()187 void EngineHostManager::OnEngineHDIDiedCallback()
188 {
189 INTELL_VOICE_LOG_INFO("receive engine hdi death recipient");
190 _Exit(0);
191 }
192
GetWakeupSourceFilesList(std::vector<std::string> & cloneFiles)193 int32_t EngineHostManager::GetWakeupSourceFilesList(std::vector<std::string>& cloneFiles)
194 {
195 INTELL_VOICE_LOG_INFO("enter");
196 if (engineHostProxy1_2_ == nullptr) {
197 INTELL_VOICE_LOG_ERROR("engineHostProxy1_2_ is nullptr");
198 return -1;
199 }
200
201 return engineHostProxy1_2_->GetCloneFilesList(cloneFiles);
202 }
203
GetWakeupSourceFile(const std::string & filePath,std::vector<uint8_t> & buffer)204 int32_t EngineHostManager::GetWakeupSourceFile(const std::string &filePath, std::vector<uint8_t> &buffer)
205 {
206 INTELL_VOICE_LOG_INFO("enter");
207 if (engineHostProxy1_2_ == nullptr) {
208 INTELL_VOICE_LOG_ERROR("engineHostProxy1_2_ is nullptr");
209 return -1;
210 }
211
212 return engineHostProxy1_2_->GetCloneFile(filePath, buffer);
213 }
214
SendWakeupFile(const std::string & filePath,const std::vector<uint8_t> & buffer)215 int32_t EngineHostManager::SendWakeupFile(const std::string &filePath, const std::vector<uint8_t> &buffer)
216 {
217 INTELL_VOICE_LOG_INFO("enter");
218 if (engineHostProxy1_2_ == nullptr) {
219 INTELL_VOICE_LOG_ERROR("engineHostProxy1_2_ is nullptr");
220 return -1;
221 }
222
223 return engineHostProxy1_2_->SendCloneFile(filePath, buffer);
224 }
225
ClearUserWakeupData(const std::string & wakeupPhrase)226 int32_t EngineHostManager::ClearUserWakeupData(const std::string &wakeupPhrase)
227 {
228 INTELL_VOICE_LOG_INFO("enter");
229 if (engineHostProxy1_2_ == nullptr) {
230 INTELL_VOICE_LOG_ERROR("engineHostProxy1_2_ is nullptr");
231 return -1;
232 }
233 return engineHostProxy1_2_->ClearUserWakeupData(wakeupPhrase);
234 }
235 }
236 }
237