• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30 #include "system_ability_load_callback_stub.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 namespace {
35 constexpr int32_t WAIT_TIME_MILL = 3000;
36 }
37 
38 class DFWKLoadCallback : public SystemAbilityLoadCallbackStub {
39 public:
40     void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject);
41     void OnLoadSystemAbilityFail(int32_t systemAbilityId);
42     bool WaitLoadComplete();
43 private:
44     bool isNotified_ = false;
45     bool isLoadSuccess_ = false;
46     std::mutex waitLoadCompleteMutex_;
47     std::condition_variable waitLoadCompleteCondVar_;
48 };
49 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)50 void DFWKLoadCallback::OnLoadSystemAbilitySuccess(
51     int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
52 {
53     std::unique_lock<std::mutex> locker(waitLoadCompleteMutex_);
54     isNotified_ = true;
55     isLoadSuccess_ = true;
56     waitLoadCompleteCondVar_.notify_all();
57 }
58 
OnLoadSystemAbilityFail(int32_t systemAbilityId)59 void DFWKLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
60 {
61     std::unique_lock<std::mutex> locker(waitLoadCompleteMutex_);
62     isNotified_ = true;
63     isLoadSuccess_ = false;
64     waitLoadCompleteCondVar_.notify_all();
65 }
66 
WaitLoadComplete()67 bool DFWKLoadCallback::WaitLoadComplete()
68 {
69     std::unique_lock<std::mutex> locker(waitLoadCompleteMutex_);
70     auto waitStatus = waitLoadCompleteCondVar_.wait_for(locker, std::chrono::milliseconds(WAIT_TIME_MILL),
71         [this]() { return isNotified_; });
72     if (!waitStatus) {
73         DHLOGE("Load distributed hardware SA timeout.");
74         return false;
75     }
76     return isLoadSuccess_;
77 }
78 
DumpDescriptors(const std::vector<DHDescriptor> & descriptors)79 std::string DumpDescriptors(const std::vector<DHDescriptor> &descriptors)
80 {
81     std::string descriptorsInfo = "[";
82     for (auto& descriptor : descriptors) {
83         descriptorsInfo += "[";
84         descriptorsInfo += "dhType:";
85         descriptorsInfo += std::to_string((uint32_t)descriptor.dhType);
86         descriptorsInfo += ",";
87         descriptorsInfo += "id:";
88         descriptorsInfo += descriptor.id;
89         descriptorsInfo += "]";
90     }
91     descriptorsInfo += "]";
92     return descriptorsInfo;
93 }
94 
DistributedHardwareFwkKit()95 DistributedHardwareFwkKit::DistributedHardwareFwkKit() : isDHFWKOnLine_(false)
96 {
97     DHLOGI("Ctor DistributedHardwareFwkKit");
98     DHFWKSAManager::GetInstance().RegisterSAStateCallback([this](bool isOnLine) { this->OnDHFWKOnLine(isOnLine); });
99     DHFWKSAManager::GetInstance().RegisterAbilityListener();
100 }
101 
~DistributedHardwareFwkKit()102 DistributedHardwareFwkKit::~DistributedHardwareFwkKit()
103 {
104     DHLOGI("Dtor DistributedHardwareFwkKit");
105 }
106 
RegisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)107 int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
108 {
109     DHLOGI("Register publisher listener, topic: %{public}" PRIu32 ", is DHFWK online: %{public}s",
110         (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
111     if (!IsDHTopicValid(topic)) {
112         DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic);
113         return ERR_DH_FWK_PARA_INVALID;
114     }
115 
116     int32_t ret = DH_FWK_SUCCESS;
117     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
118         ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener);
119         DHLOGI("Register publisher listener to DHFWK, ret: %{public}" PRId32, ret);
120         if (ret == DH_FWK_SUCCESS) {
121             return DHFWKSAManager::GetInstance().AddPublisherListenerToCache(topic, listener);
122         }
123     } else {
124         DHLOGI("DHFWK not online, or get proxy failed, save listener temporary");
125         return DHFWKSAManager::GetInstance().AddPublisherListenerToCache(topic, listener);
126     }
127 
128     return ret;
129 }
130 
UnregisterPublisherListener(const DHTopic topic,sptr<IPublisherListener> listener)131 int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic topic, sptr<IPublisherListener> listener)
132 {
133     DHLOGI("Unregister publisher listener, topic: %{public}" PRIu32 ", is DHFWK online: %{public}s",
134         (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false");
135     if (!IsDHTopicValid(topic)) {
136         DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic);
137         return ERR_DH_FWK_PARA_INVALID;
138     }
139 
140     int32_t ret = DH_FWK_SUCCESS;
141     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) {
142         ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener);
143         DHLOGI("Unregister publisher listener to DHFWK, ret: %{public}" PRId32, ret);
144     }
145 
146     DHFWKSAManager::GetInstance().RemovePublisherListenerFromCache(topic, listener);
147     return ret;
148 }
149 
PublishMessage(const DHTopic topic,const std::string & message)150 int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std::string &message)
151 {
152     DHLOGI("Publish message, topic: %{public}" PRIu32, (uint32_t)topic);
153     if (!IsDHTopicValid(topic)) {
154         DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic);
155         return ERR_DH_FWK_PARA_INVALID;
156     }
157     if (!IsMessageLengthValid(message)) {
158         return ERR_DH_FWK_PARA_INVALID;
159     }
160 
161     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
162         DHLOGI("DHFWK not online, can not publish message");
163         return ERR_DH_FWK_PUBLISH_MSG_FAILED;
164     }
165 
166     int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message);
167     DHLOGI("Publish message to DHFWK, ret: %{public}" PRId32, ret);
168 
169     return ret;
170 }
171 
IsDHTopicValid(DHTopic topic)172 bool DistributedHardwareFwkKit::IsDHTopicValid(DHTopic topic)
173 {
174     return topic > DHTopic::TOPIC_MIN && topic < DHTopic::TOPIC_MAX;
175 }
176 
OnDHFWKOnLine(bool isOnLine)177 void DistributedHardwareFwkKit::OnDHFWKOnLine(bool isOnLine)
178 {
179     DHLOGI("Receive DHFWK online callback, %{public}s", (isOnLine ? "true" : "false"));
180     isDHFWKOnLine_ = isOnLine;
181 }
182 
IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec)183 bool DistributedHardwareFwkKit::IsQueryLocalSysSpecTypeValid(QueryLocalSysSpecType spec)
184 {
185     return spec > QueryLocalSysSpecType::MIN && spec < QueryLocalSysSpecType::MAX;
186 }
187 
LoadDistributedHardwareSA()188 int32_t DistributedHardwareFwkKit::LoadDistributedHardwareSA()
189 {
190     DHLOGI("Load distributed hardware SA begin!");
191     std::unique_lock<std::mutex> locker(dfwkLoadServiceMutex_);
192     sptr<ISystemAbilityManager> saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
193     if (saMgr == nullptr) {
194         DHLOGE("Failed to get system ability mgr.");
195         return ERR_DH_FWK_POINTER_IS_NULL;
196     }
197     sptr<IRemoteObject> remote = saMgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_SA_ID);
198     if (remote != nullptr) {
199         DHLOGI("DHFWK service has already been loaded!");
200         return DH_FWK_SUCCESS;
201     }
202     sptr<DFWKLoadCallback> dfwkLoadCallback(new DFWKLoadCallback);
203     if (dfwkLoadCallback == nullptr) {
204         DHLOGE("Failed to create DFWK load callback.");
205         return ERR_DH_FWK_POINTER_IS_NULL;
206     }
207     int32_t ret = saMgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_SA_ID, dfwkLoadCallback);
208     if (ret != DH_FWK_SUCCESS) {
209         DHLOGE("Failed to load DFWK service, ret: %{public}d", ret);
210         return ret;
211     }
212     if (!dfwkLoadCallback->WaitLoadComplete()) {
213         DHLOGE("Load DFWK service callback failed");
214         return ERR_DH_FWK_LOAD_CALLBACK_FAIL;
215     }
216     DHLOGI("Load distributed hardware SA end!");
217     return DH_FWK_SUCCESS;
218 }
219 
QueryLocalSysSpec(enum QueryLocalSysSpecType spec)220 std::string DistributedHardwareFwkKit::QueryLocalSysSpec(enum QueryLocalSysSpecType spec)
221 {
222     DHLOGI("Query Local Sys Spec, %{public}d", (uint32_t)spec);
223     if (!IsQueryLocalSysSpecTypeValid(spec)) {
224         DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)spec);
225         return "";
226     }
227 
228     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
229         DHLOGI("DHFWK not online, can not publish message");
230         return "";
231     }
232 
233     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->QueryLocalSysSpec(spec);
234 }
235 
InitializeAVCenter(const TransRole & transRole,int32_t & engineId)236 int32_t DistributedHardwareFwkKit::InitializeAVCenter(const TransRole &transRole, int32_t &engineId)
237 {
238     DHLOGI("Initialize av control center, transRole: %{public}" PRIu32, (uint32_t)transRole);
239 
240     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
241         DHLOGI("DHFWK not online or get proxy failed, can not initializeA av control center");
242         return ERR_DH_FWK_POINTER_IS_NULL;
243     }
244 
245     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->InitializeAVCenter(transRole, engineId);
246 }
247 
ReleaseAVCenter(int32_t engineId)248 int32_t DistributedHardwareFwkKit::ReleaseAVCenter(int32_t engineId)
249 {
250     DHLOGI("Release av control center, engineId: %{public}" PRId32, engineId);
251 
252     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
253         DHLOGI("DHFWK not online or get proxy failed, can not release av control center");
254         return ERR_DH_FWK_POINTER_IS_NULL;
255     }
256 
257     DHFWKSAManager::GetInstance().RemoveAVTransControlCenterCbFromCache(engineId);
258     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->ReleaseAVCenter(engineId);
259 }
260 
CreateControlChannel(int32_t engineId,const std::string & peerDevId)261 int32_t DistributedHardwareFwkKit::CreateControlChannel(int32_t engineId, const std::string &peerDevId)
262 {
263     DHLOGI("Create av control center channel, engineId: %{public}" PRId32 ", peerDevId=%{public}s.", engineId,
264         GetAnonyString(peerDevId).c_str());
265 
266     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
267         DHLOGI("DHFWK not online or get proxy failed, can not create av control center channel");
268         return ERR_DH_FWK_POINTER_IS_NULL;
269     }
270 
271     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->CreateControlChannel(engineId, peerDevId);
272 }
273 
NotifyAVCenter(int32_t engineId,const AVTransEvent & event)274 int32_t DistributedHardwareFwkKit::NotifyAVCenter(int32_t engineId, const AVTransEvent &event)
275 {
276     DHLOGI("Notify av control center, engineId: %{public}" PRId32 ", event type=%{public}" PRId32, engineId,
277         event.type);
278 
279     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
280         DHLOGI("DHFWK not online or get proxy failed, can not notity av control center event.");
281         return ERR_DH_FWK_POINTER_IS_NULL;
282     }
283 
284     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->NotifyAVCenter(engineId, event);
285 }
286 
RegisterCtlCenterCallback(int32_t engineId,const sptr<IAvTransControlCenterCallback> callback)287 int32_t DistributedHardwareFwkKit::RegisterCtlCenterCallback(int32_t engineId,
288     const sptr<IAvTransControlCenterCallback> callback)
289 {
290     DHLOGI("Register av control center callback. engineId: %{public}" PRId32, engineId);
291 
292     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
293         DHLOGI("DHFWK not online or get proxy failed, can not register av control center callback.");
294         return ERR_DH_FWK_POINTER_IS_NULL;
295     }
296     DHFWKSAManager::GetInstance().AddAVTransControlCenterCbToCache(engineId, callback);
297     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterCtlCenterCallback(engineId, callback);
298 }
299 
PauseDistributedHardware(DHType dhType,const std::string & networkId)300 int32_t DistributedHardwareFwkKit::PauseDistributedHardware(DHType dhType, const std::string &networkId)
301 {
302     if (!IsIdLengthValid(networkId)) {
303         return ERR_DH_FWK_PARA_INVALID;
304     }
305     DHLOGI("Pause distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType,
306         GetAnonyString(networkId).c_str());
307 
308     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
309         DHLOGI("DHFWK not online or get proxy failed, can not pause distributed hardware.");
310         return ERR_DH_FWK_POINTER_IS_NULL;
311     }
312     HiSysEventWriteMsg(DHFWK_INIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
313         "user pause sink ui.");
314     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->PauseDistributedHardware(dhType, networkId);
315 }
316 
ResumeDistributedHardware(DHType dhType,const std::string & networkId)317 int32_t DistributedHardwareFwkKit::ResumeDistributedHardware(DHType dhType, const std::string &networkId)
318 {
319     if (!IsIdLengthValid(networkId)) {
320         return ERR_DH_FWK_PARA_INVALID;
321     }
322     DHLOGI("Resume distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType,
323         GetAnonyString(networkId).c_str());
324 
325     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
326         DHLOGI("DHFWK not online or get proxy failed, can not resume distributed hardware.");
327         return ERR_DH_FWK_POINTER_IS_NULL;
328     }
329     HiSysEventWriteMsg(DHFWK_INIT_BEGIN, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
330         "user resume sink ui.");
331     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->ResumeDistributedHardware(dhType, networkId);
332 }
333 
StopDistributedHardware(DHType dhType,const std::string & networkId)334 int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const std::string &networkId)
335 {
336     if (!IsIdLengthValid(networkId)) {
337         return ERR_DH_FWK_PARA_INVALID;
338     }
339     DHLOGI("Stop distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType,
340         GetAnonyString(networkId).c_str());
341 
342     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
343         DHLOGI("DHFWK not online or get proxy failed, can not stop distributed hardware.");
344         return ERR_DH_FWK_POINTER_IS_NULL;
345     }
346     HiSysEventWriteMsg(DHFWK_EXIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
347         "user stop sink ui.");
348     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->StopDistributedHardware(dhType, networkId);
349 }
350 
GetDistributedHardware(const std::string & networkId,EnableStep enableStep,const sptr<IGetDhDescriptorsCallback> callback)351 int32_t DistributedHardwareFwkKit::GetDistributedHardware(const std::string &networkId, EnableStep enableStep,
352     const sptr<IGetDhDescriptorsCallback> callback)
353 {
354     if (!IsIdLengthValid(networkId)) {
355         return ERR_DH_FWK_PARA_INVALID;
356     }
357     DHLOGI("Get distributed hardware networkId %{public}s.", GetAnonyString(networkId).c_str());
358     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
359         DHLOGI("DHFWK not online or get proxy failed, can not get distributed hardware.");
360         return ERR_DH_FWK_POINTER_IS_NULL;
361     }
362     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->GetDistributedHardware(networkId, enableStep, callback);
363 }
364 
RegisterDHStatusListener(sptr<IHDSinkStatusListener> listener)365 int32_t DistributedHardwareFwkKit::RegisterDHStatusListener(sptr<IHDSinkStatusListener> listener)
366 {
367     DHLOGI("Register distributed hardware status sink listener.");
368     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
369         DHLOGI("DHFWK not online or get proxy failed, can not register distributed hardware status listener.");
370         return ERR_DH_FWK_POINTER_IS_NULL;
371     }
372     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterDHStatusListener(listener);
373 }
374 
UnregisterDHStatusListener(sptr<IHDSinkStatusListener> listener)375 int32_t DistributedHardwareFwkKit::UnregisterDHStatusListener(sptr<IHDSinkStatusListener> listener)
376 {
377     DHLOGI("Unregister distributed hardware status sink listener.");
378     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
379         DHLOGI("DHFWK not online or get proxy failed, can not unregister distributed hardware status listener.");
380         return ERR_DH_FWK_POINTER_IS_NULL;
381     }
382     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterDHStatusListener(listener);
383 }
384 
RegisterDHStatusListener(const std::string & networkId,sptr<IHDSourceStatusListener> listener)385 int32_t DistributedHardwareFwkKit::RegisterDHStatusListener(
386     const std::string &networkId, sptr<IHDSourceStatusListener> listener)
387 {
388     if (!IsIdLengthValid(networkId)) {
389         return ERR_DH_FWK_PARA_INVALID;
390     }
391     DHLOGI("Register distributed hardware status source listener %{public}s.", GetAnonyString(networkId).c_str());
392     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
393         DHLOGI("DHFWK not online or get proxy failed, can not register distributed hardware status listener.");
394         return ERR_DH_FWK_POINTER_IS_NULL;
395     }
396     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterDHStatusListener(networkId, listener);
397 }
398 
UnregisterDHStatusListener(const std::string & networkId,sptr<IHDSourceStatusListener> listener)399 int32_t DistributedHardwareFwkKit::UnregisterDHStatusListener(
400     const std::string &networkId, sptr<IHDSourceStatusListener> listener)
401 {
402     if (!IsIdLengthValid(networkId)) {
403         return ERR_DH_FWK_PARA_INVALID;
404     }
405     DHLOGI("Unregister distributed hardware status source listener %{public}s.", GetAnonyString(networkId).c_str());
406     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
407         DHLOGI("DHFWK not online or get proxy failed, can not unregister distributed hardware status listener.");
408         return ERR_DH_FWK_POINTER_IS_NULL;
409     }
410     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterDHStatusListener(networkId, listener);
411 }
412 
EnableSink(const std::vector<DHDescriptor> & descriptors)413 int32_t DistributedHardwareFwkKit::EnableSink(const std::vector<DHDescriptor> &descriptors)
414 {
415     DHLOGI("Enable distributed hardware sink descriptors %{public}s.", DumpDescriptors(descriptors).c_str());
416     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
417         DHLOGI("DHFWK not online or get proxy failed, can not enable sink.");
418         return ERR_DH_FWK_POINTER_IS_NULL;
419     }
420     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->EnableSink(descriptors);
421 }
422 
DisableSink(const std::vector<DHDescriptor> & descriptors)423 int32_t DistributedHardwareFwkKit::DisableSink(const std::vector<DHDescriptor> &descriptors)
424 {
425     DHLOGI("Disable distributed hardware sink descriptors %{public}s.", DumpDescriptors(descriptors).c_str());
426     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
427         DHLOGI("DHFWK not online or get proxy failed, can not disable sink.");
428         return ERR_DH_FWK_POINTER_IS_NULL;
429     }
430     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->DisableSink(descriptors);
431 }
432 
EnableSource(const std::string & networkId,const std::vector<DHDescriptor> & descriptors)433 int32_t DistributedHardwareFwkKit::EnableSource(
434     const std::string &networkId, const std::vector<DHDescriptor> &descriptors)
435 {
436     if (!IsIdLengthValid(networkId)) {
437         return ERR_DH_FWK_PARA_INVALID;
438     }
439     DHLOGI("Enable distributed hardware source networkId %{public}s, descriptors %{public}s.",
440         GetAnonyString(networkId).c_str(), DumpDescriptors(descriptors).c_str());
441     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
442         DHLOGI("DHFWK not online or get proxy failed, can not enable source.");
443         return ERR_DH_FWK_POINTER_IS_NULL;
444     }
445     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->EnableSource(networkId, descriptors);
446 }
447 
DisableSource(const std::string & networkId,const std::vector<DHDescriptor> & descriptors)448 int32_t DistributedHardwareFwkKit::DisableSource(
449     const std::string &networkId, const std::vector<DHDescriptor> &descriptors)
450 {
451     if (!IsIdLengthValid(networkId)) {
452         return ERR_DH_FWK_PARA_INVALID;
453     }
454     DHLOGI("Disable distributed hardware source networkId %{public}s, descriptors %{public}s.",
455         GetAnonyString(networkId).c_str(), DumpDescriptors(descriptors).c_str());
456     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
457         DHLOGI("DHFWK not online or get proxy failed, can not disable source.");
458         return ERR_DH_FWK_POINTER_IS_NULL;
459     }
460     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->DisableSource(networkId, descriptors);
461 }
462 
LoadDistributedHDF(const DHType dhType)463 int32_t DistributedHardwareFwkKit::LoadDistributedHDF(const DHType dhType)
464 {
465     DHLOGI("Load distributed HDF, dhType: %{public}u.", dhType);
466     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
467         DHLOGE("DHFWK not online or get proxy failed, try to load DFWK service.");
468         if (LoadDistributedHardwareSA() != DH_FWK_SUCCESS) {
469             DHLOGE("Load distributed hardware SA failed, can not load distributed HDF.");
470             return ERR_DH_FWK_POINTER_IS_NULL;
471         }
472         if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
473             DHLOGE("DHFWK already not online or get proxy failed yet, can not load distributed HDF.");
474             return ERR_DH_FWK_POINTER_IS_NULL;
475         }
476     }
477     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->LoadDistributedHDF(dhType);
478 }
479 
UnLoadDistributedHDF(const DHType dhType)480 int32_t DistributedHardwareFwkKit::UnLoadDistributedHDF(const DHType dhType)
481 {
482     DHLOGI("UnLoad distributed HDF, dhType: %{public}u.", dhType);
483     if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) {
484         DHLOGI("DHFWK not online or get proxy failed, can not unload distributed HDF.");
485         return ERR_DH_FWK_POINTER_IS_NULL;
486     }
487     return DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnLoadDistributedHDF(dhType);
488 }
489 } // DistributedHardware
490 } // OHOS