1 /*
2 * Copyright (c) 2022-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
16 #include "distributed_hardware_fwk_kit.h"
17
18 #include <cinttypes>
19
20 #include "anonymous_string.h"
21 #include "constants.h"
22 #include "dh_utils_tool.h"
23 #include "dhfwk_sa_manager.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 #include "dh_utils_hisysevent.h"
27 #include "idistributed_hardware.h"
28
29 namespace OHOS {
30 namespace DistributedHardware {
31
DumpDescriptors(const std::vector<DHDescriptor> & descriptors)32 std::string DumpDescriptors(const std::vector<DHDescriptor> &descriptors)
33 {
34 std::string descriptorsInfo = "[";
35 for (auto& descriptor : descriptors) {
36 descriptorsInfo += "[";
37 descriptorsInfo += "dhType:";
38 descriptorsInfo += std::to_string((uint32_t)descriptor.dhType);
39 descriptorsInfo += ",";
40 descriptorsInfo += "id:";
41 descriptorsInfo += descriptor.id;
42 descriptorsInfo += "]";
43 }
44 descriptorsInfo += "]";
45 return descriptorsInfo;
46 }
47
DistributedHardwareFwkKit()48 DistributedHardwareFwkKit::DistributedHardwareFwkKit() : isDHFWKOnLine_(false)
49 {
50 DHLOGI("Ctor DistributedHardwareFwkKit");
51 DHFWKSAManager::GetInstance().RegisterSAStateCallback([this](bool isOnLine) { this->OnDHFWKOnLine(isOnLine); });
52 DHFWKSAManager::GetInstance().RegisterAbilityListener();
53 }
54
~DistributedHardwareFwkKit()55 DistributedHardwareFwkKit::~DistributedHardwareFwkKit()
56 {
57 DHLOGI("Dtor DistributedHardwareFwkKit");
58 }
59
RegisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)60 int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
61 {
62 DHLOGI("Register publisher listener, topic: %{public}" PRIu32 ", is DHFWK online: %{public}s",
63 (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
64 if (!IsDHTopicValid(topic)) {
65 DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic);
66 return ERR_DH_FWK_PARA_INVALID;
67 }
68
69 int32_t ret = DH_FWK_SUCCESS;
70 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
71 ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener);
72 DHLOGI("Register publisher listener to DHFWK, ret: %{public}" PRId32, ret);
73 if (ret == DH_FWK_SUCCESS) {
74 return DHFWKSAManager::GetInstance().AddPublisherListenerToCache(topic, listener);
75 }
76 } else {
77 DHLOGI("DHFWK not online, or get proxy failed, save listener temporary");
78 return DHFWKSAManager::GetInstance().AddPublisherListenerToCache(topic, listener);
79 }
80
81 return ret;
82 }
83
UnregisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)84 int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
85 {
86 DHLOGI("Unregister publisher listener, topic: %{public}" PRIu32 ", is DHFWK online: %{public}s",
87 (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
88 if (!IsDHTopicValid(topic)) {
89 DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic);
90 return ERR_DH_FWK_PARA_INVALID;
91 }
92
93 int32_t ret = DH_FWK_SUCCESS;
94 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
95 ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener);
96 DHLOGI("Unregister publisher listener to DHFWK, ret: %{public}" PRId32, ret);
97 }
98
99 DHFWKSAManager::GetInstance().RemovePublisherListenerFromCache(topic, listener);
100 return ret;
101 }
102
PublishMessage(const DHTopic topic,const std::string & message)103 int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std::string &message)
104 {
105 DHLOGI("Publish message, topic: %{public}" PRIu32, (uint32_t)topic);
106 if (!IsDHTopicValid(topic)) {
107 DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic);
108 return ERR_DH_FWK_PARA_INVALID;
109 }
110 if (!IsMessageLengthValid(message)) {
111 return ERR_DH_FWK_PARA_INVALID;
112 }
113
114 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
115 DHLOGI("DHFWK not online, can not publish message");
116 return ERR_DH_FWK_PUBLISH_MSG_FAILED;
117 }
118
119 int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message);
120 DHLOGI("Publish message to DHFWK, ret: %{public}" PRId32, ret);
121
122 return ret;
123 }
124
IsDHTopicValid(DHTopic topic)125 bool DistributedHardwareFwkKit::IsDHTopicValid(DHTopic topic)
126 {
127 return topic > DHTopic::TOPIC_MIN && topic < DHTopic::TOPIC_MAX;
128 }
129
OnDHFWKOnLine(bool isOnLine)130 void DistributedHardwareFwkKit::OnDHFWKOnLine(bool isOnLine)
131 {
132 DHLOGI("Receive DHFWK online callback, %{public}s", (isOnLine ? "true" : "false"));
133 isDHFWKOnLine_ = isOnLine;
134 }
135
IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec)136 bool DistributedHardwareFwkKit::IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec)
137 {
138 return spec > QueryLocalSysSpecType::MIN && spec < QueryLocalSysSpecType::MAX;
139 }
140
QueryLocalSysSpec(enum QueryLocalSysSpecType spec)141 std::string DistributedHardwareFwkKit::QueryLocalSysSpec(enum QueryLocalSysSpecType spec)
142 {
143 DHLOGI("Query Local Sys Spec, %{public}d", (uint32_t)spec);
144 if (!IsQueryLocalSysSpecTypeValid(spec)) {
145 DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)spec);
146 return "";
147 }
148
149 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
150 DHLOGI("DHFWK not online, can not publish message");
151 return "";
152 }
153
154 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->QueryLocalSysSpec(spec);
155 }
156
InitializeAVCenter(const TransRole & transRole,int32_t & engineId)157 int32_t DistributedHardwareFwkKit::InitializeAVCenter(const TransRole &transRole, int32_t &engineId)
158 {
159 DHLOGI("Initialize av control center, transRole: %{public}" PRIu32, (uint32_t)transRole);
160
161 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
162 DHLOGI("DHFWK not online or get proxy failed, can not initializeA av control center");
163 return ERR_DH_FWK_POINTER_IS_NULL;
164 }
165
166 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->InitializeAVCenter(transRole, engineId);
167 }
168
ReleaseAVCenter(int32_t engineId)169 int32_t DistributedHardwareFwkKit::ReleaseAVCenter(int32_t engineId)
170 {
171 DHLOGI("Release av control center, engineId: %{public}" PRId32, engineId);
172
173 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
174 DHLOGI("DHFWK not online or get proxy failed, can not release av control center");
175 return ERR_DH_FWK_POINTER_IS_NULL;
176 }
177
178 DHFWKSAManager::GetInstance().RemoveAVTransControlCenterCbFromCache(engineId);
179 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->ReleaseAVCenter(engineId);
180 }
181
CreateControlChannel(int32_t engineId,const std::string & peerDevId)182 int32_t DistributedHardwareFwkKit::CreateControlChannel(int32_t engineId, const std::string &peerDevId)
183 {
184 DHLOGI("Create av control center channel, engineId: %{public}" PRId32 ", peerDevId=%{public}s.", engineId,
185 GetAnonyString(peerDevId).c_str());
186
187 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
188 DHLOGI("DHFWK not online or get proxy failed, can not create av control center channel");
189 return ERR_DH_FWK_POINTER_IS_NULL;
190 }
191
192 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->CreateControlChannel(engineId, peerDevId);
193 }
194
NotifyAVCenter(int32_t engineId,const AVTransEvent & event)195 int32_t DistributedHardwareFwkKit::NotifyAVCenter(int32_t engineId, const AVTransEvent &event)
196 {
197 DHLOGI("Notify av control center, engineId: %{public}" PRId32 ", event type=%{public}" PRId32, engineId,
198 event.type);
199
200 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
201 DHLOGI("DHFWK not online or get proxy failed, can not notity av control center event.");
202 return ERR_DH_FWK_POINTER_IS_NULL;
203 }
204
205 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->NotifyAVCenter(engineId, event);
206 }
207
RegisterCtlCenterCallback(int32_t engineId,const sptr<IAVTransControlCenterCallback> callback)208 int32_t DistributedHardwareFwkKit::RegisterCtlCenterCallback(int32_t engineId,
209 const sptr<IAVTransControlCenterCallback> callback)
210 {
211 DHLOGI("Register av control center callback. engineId: %{public}" PRId32, engineId);
212
213 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
214 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
215 return ERR_DH_FWK_POINTER_IS_NULL;
216 }
217 DHFWKSAManager::GetInstance().AddAVTransControlCenterCbToCache(engineId, callback);
218 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(engineId, callback);
219 }
220
PauseDistributedHardware(DHType dhType,const std::string & networkId)221 int32_t DistributedHardwareFwkKit::PauseDistributedHardware(DHType dhType, const std::string &networkId)
222 {
223 if (!IsIdLengthValid(networkId)) {
224 return ERR_DH_FWK_PARA_INVALID;
225 }
226 DHLOGI("Pause distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType,
227 GetAnonyString(networkId).c_str());
228
229 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
230 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
231 return ERR_DH_FWK_POINTER_IS_NULL;
232 }
233 HiSysEventWriteMsg(DHFWK_INIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
234 "user pause sink ui.");
235 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->PauseDistributedHardware(dhType, networkId);
236 }
237
ResumeDistributedHardware(DHType dhType,const std::string & networkId)238 int32_t DistributedHardwareFwkKit::ResumeDistributedHardware(DHType dhType, const std::string &networkId)
239 {
240 if (!IsIdLengthValid(networkId)) {
241 return ERR_DH_FWK_PARA_INVALID;
242 }
243 DHLOGI("Resume distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType,
244 GetAnonyString(networkId).c_str());
245
246 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
247 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
248 return ERR_DH_FWK_POINTER_IS_NULL;
249 }
250 HiSysEventWriteMsg(DHFWK_INIT_BEGIN, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
251 "user resume sink ui.");
252 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->ResumeDistributedHardware(dhType, networkId);
253 }
254
StopDistributedHardware(DHType dhType,const std::string & networkId)255 int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const std::string &networkId)
256 {
257 if (!IsIdLengthValid(networkId)) {
258 return ERR_DH_FWK_PARA_INVALID;
259 }
260 DHLOGI("Stop distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType,
261 GetAnonyString(networkId).c_str());
262
263 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
264 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
265 return ERR_DH_FWK_POINTER_IS_NULL;
266 }
267 HiSysEventWriteMsg(DHFWK_EXIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
268 "user stop sink ui.");
269 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->StopDistributedHardware(dhType, networkId);
270 }
271
GetDistributedHardware(const std::string & networkId,std::vector<DHDescriptor> & descriptors)272 int32_t DistributedHardwareFwkKit::GetDistributedHardware(
273 const std::string &networkId, std::vector<DHDescriptor> &descriptors)
274 {
275 if (!IsIdLengthValid(networkId)) {
276 return ERR_DH_FWK_PARA_INVALID;
277 }
278 DHLOGI("Get distributed hardware networkId %{public}s.", GetAnonyString(networkId).c_str());
279 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
280 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
281 return ERR_DH_FWK_POINTER_IS_NULL;
282 }
283 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->GetDistributedHardware(networkId, descriptors);
284 }
285
RegisterDHStatusListener(sptr<IHDSinkStatusListener> listener)286 int32_t DistributedHardwareFwkKit::RegisterDHStatusListener(sptr<IHDSinkStatusListener> listener)
287 {
288 DHLOGI("Register distributed hardware status sink listener.");
289 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
290 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
291 return ERR_DH_FWK_POINTER_IS_NULL;
292 }
293 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterDHStatusListener(listener);
294 }
295
UnregisterDHStatusListener(sptr<IHDSinkStatusListener> listener)296 int32_t DistributedHardwareFwkKit::UnregisterDHStatusListener(sptr<IHDSinkStatusListener> listener)
297 {
298 DHLOGI("Unregister distributed hardware status sink listener.");
299 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
300 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
301 return ERR_DH_FWK_POINTER_IS_NULL;
302 }
303 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterDHStatusListener(listener);
304 }
305
RegisterDHStatusListener(const std::string & networkId,sptr<IHDSourceStatusListener> listener)306 int32_t DistributedHardwareFwkKit::RegisterDHStatusListener(
307 const std::string &networkId, sptr<IHDSourceStatusListener> listener)
308 {
309 if (!IsIdLengthValid(networkId)) {
310 return ERR_DH_FWK_PARA_INVALID;
311 }
312 DHLOGI("Register distributed hardware status source listener %{public}s.", GetAnonyString(networkId).c_str());
313 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
314 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
315 return ERR_DH_FWK_POINTER_IS_NULL;
316 }
317 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterDHStatusListener(networkId, listener);
318 }
319
UnregisterDHStatusListener(const std::string & networkId,sptr<IHDSourceStatusListener> listener)320 int32_t DistributedHardwareFwkKit::UnregisterDHStatusListener(
321 const std::string &networkId, sptr<IHDSourceStatusListener> listener)
322 {
323 if (!IsIdLengthValid(networkId)) {
324 return ERR_DH_FWK_PARA_INVALID;
325 }
326 DHLOGI("Unregister distributed hardware status source listener %{public}s.", GetAnonyString(networkId).c_str());
327 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
328 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
329 return ERR_DH_FWK_POINTER_IS_NULL;
330 }
331 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterDHStatusListener(networkId, listener);
332 }
333
EnableSink(const std::vector<DHDescriptor> & descriptors)334 int32_t DistributedHardwareFwkKit::EnableSink(const std::vector<DHDescriptor> &descriptors)
335 {
336 DHLOGI("Enable distributed hardware sink descriptors %{public}s.", DumpDescriptors(descriptors).c_str());
337 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
338 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
339 return ERR_DH_FWK_POINTER_IS_NULL;
340 }
341 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->EnableSink(descriptors);
342 }
343
DisableSink(const std::vector<DHDescriptor> & descriptors)344 int32_t DistributedHardwareFwkKit::DisableSink(const std::vector<DHDescriptor> &descriptors)
345 {
346 DHLOGI("Disable distributed hardware sink descriptors %{public}s.", DumpDescriptors(descriptors).c_str());
347 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
348 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
349 return ERR_DH_FWK_POINTER_IS_NULL;
350 }
351 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->DisableSink(descriptors);
352 }
353
EnableSource(const std::string & networkId,const std::vector<DHDescriptor> & descriptors)354 int32_t DistributedHardwareFwkKit::EnableSource(
355 const std::string &networkId, const std::vector<DHDescriptor> &descriptors)
356 {
357 if (!IsIdLengthValid(networkId)) {
358 return ERR_DH_FWK_PARA_INVALID;
359 }
360 DHLOGI("Enable distributed hardware source networkId %{public}s, descriptors %{public}s.",
361 GetAnonyString(networkId).c_str(), DumpDescriptors(descriptors).c_str());
362 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
363 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
364 return ERR_DH_FWK_POINTER_IS_NULL;
365 }
366 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->EnableSource(networkId, descriptors);
367 }
368
DisableSource(const std::string & networkId,const std::vector<DHDescriptor> & descriptors)369 int32_t DistributedHardwareFwkKit::DisableSource(
370 const std::string &networkId, const std::vector<DHDescriptor> &descriptors)
371 {
372 if (!IsIdLengthValid(networkId)) {
373 return ERR_DH_FWK_PARA_INVALID;
374 }
375 DHLOGI("Disable distributed hardware source networkId %{public}s, descriptors %{public}s.",
376 GetAnonyString(networkId).c_str(), DumpDescriptors(descriptors).c_str());
377 if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
378 DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
379 return ERR_DH_FWK_POINTER_IS_NULL;
380 }
381 return DHFWKSAManager::GetInstance().GetDHFWKProxy()->DisableSource(networkId, descriptors);
382 }
383 } // DistributedHardware
384 } // OHOS