• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_service.h"
17 
18 #include <cinttypes>
19 
20 #include "constants.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "ipc_types.h"
24 #include "ipublisher_listener.h"
25 #include "iservice_registry.h"
26 #include "cJSON.h"
27 #include "string_ex.h"
28 #include "system_ability_definition.h"
29 
30 #include "access_manager.h"
31 #include "anonymous_string.h"
32 #include "av_trans_control_center.h"
33 #include "capability_info_manager.h"
34 #include "meta_info_manager.h"
35 #include "component_manager.h"
36 #include "hdf_operate.h"
37 #include "dh_context.h"
38 #include "dh_utils_tool.h"
39 #include "dh_utils_hisysevent.h"
40 #include "distributed_hardware_fwk_kit_paras.h"
41 #include "distributed_hardware_errno.h"
42 #include "distributed_hardware_log.h"
43 #include "distributed_hardware_manager_factory.h"
44 #include "publisher.h"
45 #include "task_executor.h"
46 #include "task_factory.h"
47 #include "version_info_manager.h"
48 
49 namespace OHOS {
50 namespace DistributedHardware {
51 #undef DH_LOG_TAG
52 #define DH_LOG_TAG "DistributedHardwareService"
53 REGISTER_SYSTEM_ABILITY_BY_ID(DistributedHardwareService, DISTRIBUTED_HARDWARE_SA_ID, true);
54 namespace {
55     constexpr int32_t SA_READY_TO_UNLOAD = 0;
56     constexpr int32_t SA_REFUSE_TO_UNLOAD = -1;
57     constexpr int32_t INIT_BUSINESS_DELAY_TIME_MS = 5 * 100;
58     const std::string INIT_TASK_ID = "CheckAndInitDH";
59     const std::string LOCAL_NETWORKID_ALIAS = "local";
60 }
61 
DistributedHardwareService(int32_t saId,bool runOnCreate)62 DistributedHardwareService::DistributedHardwareService(int32_t saId, bool runOnCreate)
63     : SystemAbility(saId, runOnCreate)
64 {
65 }
66 
OnStart()67 void DistributedHardwareService::OnStart()
68 {
69     DHLOGI("DistributedHardwareService::OnStart start");
70     HiSysEventWriteMsg(DHFWK_INIT_BEGIN, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
71         "dhfwk sa start on demand.");
72 
73     if (state_ == ServiceRunningState::STATE_RUNNING) {
74         DHLOGI("DistributedHardwareService has already started.");
75         return;
76     }
77     if (!Init()) {
78         DHLOGE("failed to init DistributedHardwareService");
79         return;
80     }
81     state_ = ServiceRunningState::STATE_RUNNING;
82     DistributedHardwareManagerFactory::GetInstance().SetSAProcessState(false);
83     DHLOGI("DistributedHardwareService::OnStart start service success.");
84 }
85 
Init()86 bool DistributedHardwareService::Init()
87 {
88     DHLOGI("DistributedHardwareService::Init ready to init.");
89     if (!registerToService_) {
90         bool ret = Publish(this);
91         if (!ret) {
92             DHLOGE("DistributedHardwareService::Init Publish failed!");
93             HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
94                 "dhfwk sa init publish failed.");
95             return false;
96         }
97         registerToService_ = true;
98     }
99     std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
100     eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
101     auto executeInnerFunc = [this] { DoBusinessInit(); };
102     eventHandler_->PostTask(executeInnerFunc, INIT_TASK_ID, 0);
103     DHLOGI("DistributedHardwareService::Init success.");
104     HiSysEventWriteMsg(DHFWK_INIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
105         "dhfwk sa init success.");
106     return true;
107 }
108 
InitLocalDevInfo()109 void DistributedHardwareService::InitLocalDevInfo()
110 {
111     DHLOGI("Init Local device info in DB");
112     DistributedHardwareManagerFactory::GetInstance().InitLocalDevInfo();
113 }
114 
DoBusinessInit()115 bool DistributedHardwareService::DoBusinessInit()
116 {
117     if (!IsDepSAStart()) {
118         DHLOGW("Depend sa not start");
119         auto executeInnerFunc = [this] { DoBusinessInit(); };
120         eventHandler_->PostTask(executeInnerFunc, INIT_TASK_ID, INIT_BUSINESS_DELAY_TIME_MS);
121         return false;
122     }
123 
124     DHLOGI("Init AccessManager");
125     auto ret = AccessManager::GetInstance()->Init();
126     if (ret != DH_FWK_SUCCESS) {
127         DHLOGE("DistributedHardwareService::Init failed.");
128         HiSysEventWriteErrCodeMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
129             ret, "dhfwk sa AccessManager init fail.");
130     }
131     InitLocalDevInfo();
132     AccessManager::GetInstance()->CheckTrustedDeviceOnline();
133     return true;
134 }
135 
IsDepSAStart()136 bool DistributedHardwareService::IsDepSAStart()
137 {
138     sptr<ISystemAbilityManager> saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
139     if (saMgr == nullptr) {
140         DHLOGE("Get System Ability Manager failed");
141         return false;
142     }
143     DHLOGI("Check DSoftbus sa");
144     sptr<IRemoteObject> remoteObject = saMgr->CheckSystemAbility(SOFTBUS_SERVER_SA_ID);
145     if (remoteObject == nullptr) {
146         DHLOGW("DSoftbus not start");
147         return false;
148     }
149     DHLOGI("Check KVDB sa");
150     remoteObject = saMgr->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
151     if (remoteObject == nullptr) {
152         DHLOGW("KVDB not start");
153         return false;
154     }
155     DHLOGI("Check DM sa");
156     remoteObject = saMgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
157     if (remoteObject == nullptr) {
158         DHLOGW("DM not start");
159         return false;
160     }
161     return true;
162 }
163 
OnStop()164 void DistributedHardwareService::OnStop()
165 {
166     DHLOGI("DistributedHardwareService::OnStop ready to stop service.");
167     state_ = ServiceRunningState::STATE_NOT_START;
168     registerToService_ = false;
169 }
170 
OnIdle(const SystemAbilityOnDemandReason & idleReason)171 int32_t DistributedHardwareService::OnIdle(const SystemAbilityOnDemandReason& idleReason)
172 {
173     bool saState = DistributedHardwareManagerFactory::GetInstance().GetSAProcessState();
174     DHLOGI("OnIdle, idleReason name: %{public}s, id: %{public}d, value: %{public}s, sa process state: %{public}d",
175         idleReason.GetName().c_str(), idleReason.GetId(), idleReason.GetValue().c_str(), saState);
176     return saState ? SA_READY_TO_UNLOAD : SA_REFUSE_TO_UNLOAD;
177 }
178 
RegisterPublisherListener(const DHTopic topic,const sptr<IPublisherListener> listener)179 int32_t DistributedHardwareService::RegisterPublisherListener(const DHTopic topic,
180     const sptr<IPublisherListener> listener)
181 {
182     Publisher::GetInstance().RegisterListener(topic, listener);
183     return DH_FWK_SUCCESS;
184 }
185 
UnregisterPublisherListener(const DHTopic topic,const sptr<IPublisherListener> listener)186 int32_t DistributedHardwareService::UnregisterPublisherListener(const DHTopic topic,
187     const sptr<IPublisherListener> listener)
188 {
189     Publisher::GetInstance().UnregisterListener(topic, listener);
190     return DH_FWK_SUCCESS;
191 }
192 
PublishMessage(const DHTopic topic,const std::string & msg)193 int32_t DistributedHardwareService::PublishMessage(const DHTopic topic, const std::string &msg)
194 {
195     DHContext::GetInstance();
196     Publisher::GetInstance().PublishMessage(topic, msg);
197     return DH_FWK_SUCCESS;
198 }
199 
QueryLocalSysSpec(const QueryLocalSysSpecType spec)200 std::string DistributedHardwareService::QueryLocalSysSpec(const QueryLocalSysSpecType spec)
201 {
202     DeviceInfo localDevInfo = DHContext::GetInstance().GetDeviceInfo();
203     std::vector<std::shared_ptr<CapabilityInfo>> resInfos;
204     CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(localDevInfo.deviceId, resInfos);
205     DHType targetDhType = DHType::UNKNOWN;
206     std::string targetKey = "";
207     switch (spec) {
208         case QueryLocalSysSpecType::HISTREAMER_AUDIO_ENCODER:
209             targetKey = KEY_HISTREAMER_AUDIO_ENCODER;
210             targetDhType = DHType::AUDIO;
211             break;
212         case QueryLocalSysSpecType::HISTREAMER_AUDIO_DECODER:
213             targetKey = KEY_HISTREAMER_AUDIO_DECODER;
214             targetDhType = DHType::AUDIO;
215             break;
216         case QueryLocalSysSpecType::HISTREAMER_VIDEO_ENCODER:
217             targetKey = KEY_HISTREAMER_VIDEO_ENCODER;
218             targetDhType = DHType::SCREEN;
219             break;
220         case QueryLocalSysSpecType::HISTREAMER_VIDEO_DECODER:
221             targetKey = KEY_HISTREAMER_VIDEO_DECODER;
222             targetDhType = DHType::SCREEN;
223             break;
224         default:
225             break;
226     }
227 
228     DHLOGE("QueryLocalSysSpec targetKey: %{public}s, targetDhType: %{public}" PRIu32, targetKey.c_str(),
229         (uint32_t)targetDhType);
230     if (targetDhType == DHType::UNKNOWN) {
231         DHLOGE("Can not find matched dhtype");
232         return "";
233     }
234 
235     std::string attrs = "";
236     for (const auto &cap : resInfos) {
237         if (cap->GetDHType() != targetDhType) {
238             continue;
239         }
240         attrs = cap->GetDHAttrs();
241         break;
242     }
243     if (attrs.empty()) {
244         DHLOGE("Can not find dh attrs");
245         return "";
246     }
247 
248     return QueryDhSysSpec(targetKey, attrs);
249 }
250 
QueryDhSysSpec(const std::string & targetKey,std::string & attrs)251 std::string DistributedHardwareService::QueryDhSysSpec(const std::string &targetKey, std::string &attrs)
252 {
253     cJSON *attrJson = cJSON_Parse(attrs.c_str());
254     if (attrJson == NULL) {
255         DHLOGE("attrs json is invalid, attrs: %{public}s", attrs.c_str());
256         return "";
257     }
258     cJSON *targetKeyJson = cJSON_GetObjectItem(attrJson, targetKey.c_str());
259     if (!IsString(targetKeyJson)) {
260         DHLOGE("Attrs Json not contains key: %{public}s", targetKey.c_str());
261         cJSON_Delete(attrJson);
262         return "";
263     }
264     std::string result = targetKeyJson->valuestring;
265     cJSON_Delete(attrJson);
266     return result;
267 }
268 
InitializeAVCenter(const TransRole & transRole,int32_t & engineId)269 int32_t DistributedHardwareService::InitializeAVCenter(const TransRole &transRole, int32_t &engineId)
270 {
271     return AVTransControlCenter::GetInstance().InitializeAVCenter(transRole, engineId);
272 }
273 
ReleaseAVCenter(int32_t engineId)274 int32_t DistributedHardwareService::ReleaseAVCenter(int32_t engineId)
275 {
276     return AVTransControlCenter::GetInstance().ReleaseAVCenter(engineId);
277 }
278 
CreateControlChannel(int32_t engineId,const std::string & peerDevId)279 int32_t DistributedHardwareService::CreateControlChannel(int32_t engineId, const std::string &peerDevId)
280 {
281     return AVTransControlCenter::GetInstance().CreateControlChannel(engineId, peerDevId);
282 }
283 
NotifyAVCenter(int32_t engineId,const AVTransEvent & event)284 int32_t DistributedHardwareService::NotifyAVCenter(int32_t engineId, const AVTransEvent &event)
285 {
286     return AVTransControlCenter::GetInstance().NotifyAVCenter(engineId, event);
287 }
288 
RegisterCtlCenterCallback(int32_t engineId,const sptr<IAvTransControlCenterCallback> callback)289 int32_t DistributedHardwareService::RegisterCtlCenterCallback(int32_t engineId,
290     const sptr<IAvTransControlCenterCallback> callback)
291 {
292     return AVTransControlCenter::GetInstance().RegisterCtlCenterCallback(engineId, callback);
293 }
294 
NotifySourceRemoteSinkStarted(std::string & deviceId)295 int32_t DistributedHardwareService::NotifySourceRemoteSinkStarted(std::string &deviceId)
296 {
297     DHLOGI("DistributedHardwareService NotifySourceRemoteSinkStarted Init DHMS Ready Start.");
298     Publisher::GetInstance().PublishMessage(DHTopic::TOPIC_INIT_DHMS_READY, deviceId);
299     DHLOGI("DistributedHardwareService NotifySourceRemoteSinkStarted Init DHMS Ready End.");
300     return DH_FWK_SUCCESS;
301 }
302 
Dump(int32_t fd,const std::vector<std::u16string> & args)303 int DistributedHardwareService::Dump(int32_t fd, const std::vector<std::u16string>& args)
304 {
305     DHLOGI("DistributedHardwareService  Dump.");
306 
307     std::vector<std::string> argsStr {};
308     for (auto item : args) {
309         argsStr.emplace_back(Str16ToStr8(item));
310     }
311 
312     std::string result("");
313     int ret = AccessManager::GetInstance()->Dump(argsStr, result);
314     if (ret != DH_FWK_SUCCESS) {
315         DHLOGE("Dump error, ret = %{public}d", ret);
316     }
317 
318     if (dprintf(fd, "%s\n", result.c_str()) < 0) {
319         DHLOGE("Hidump dprintf error");
320         ret = ERR_DH_FWK_HIDUMP_DPRINTF_ERROR;
321     }
322 
323     return ret;
324 }
325 
PauseDistributedHardware(DHType dhType,const std::string & networkId)326 int32_t DistributedHardwareService::PauseDistributedHardware(DHType dhType, const std::string &networkId)
327 {
328     if (!IsIdLengthValid(networkId)) {
329         return ERR_DH_FWK_PARA_INVALID;
330     }
331     std::map<DHType, IDistributedHardwareSink*> sinkMap = ComponentManager::GetInstance().GetDHSinkInstance();
332     if (sinkMap.find(dhType) == sinkMap.end()) {
333         DHLOGE("PauseDistributedHardware for DHType: %{public}u not init sink handler", (uint32_t)dhType);
334         return ERR_DH_FWK_PARA_INVALID;
335     }
336     if (sinkMap[dhType] == nullptr) {
337         DHLOGE("Sinkhandler ptr is null");
338         return ERR_DH_FWK_PARA_INVALID;
339     }
340     int32_t ret = sinkMap[dhType]->PauseDistributedHardware(networkId);
341     if (ret != 0) {
342         DHLOGE("PauseDistributedHardware for DHType: %{public}u failed, ret: %{public}d", (uint32_t)dhType, ret);
343         return ret;
344     }
345     return DH_FWK_SUCCESS;
346 }
347 
ResumeDistributedHardware(DHType dhType,const std::string & networkId)348 int32_t DistributedHardwareService::ResumeDistributedHardware(DHType dhType, const std::string &networkId)
349 {
350     if (!IsIdLengthValid(networkId)) {
351         return ERR_DH_FWK_PARA_INVALID;
352     }
353     std::map<DHType, IDistributedHardwareSink*> sinkMap = ComponentManager::GetInstance().GetDHSinkInstance();
354     if (sinkMap.find(dhType) == sinkMap.end()) {
355         DHLOGE("ResumeDistributedHardware for DHType: %{public}u not init sink handler", (uint32_t)dhType);
356         return ERR_DH_FWK_PARA_INVALID;
357     }
358     if (sinkMap[dhType] == nullptr) {
359         DHLOGE("Sinkhandler ptr is null");
360         return ERR_DH_FWK_PARA_INVALID;
361     }
362     int32_t ret = sinkMap[dhType]->ResumeDistributedHardware(networkId);
363     if (ret != 0) {
364         DHLOGE("ResumeDistributedHardware for DHType: %{public}u failed, ret: %{public}d", (uint32_t)dhType, ret);
365         return ret;
366     }
367     return DH_FWK_SUCCESS;
368 }
369 
StopDistributedHardware(DHType dhType,const std::string & networkId)370 int32_t DistributedHardwareService::StopDistributedHardware(DHType dhType, const std::string &networkId)
371 {
372     if (!IsIdLengthValid(networkId)) {
373         return ERR_DH_FWK_PARA_INVALID;
374     }
375     std::map<DHType, IDistributedHardwareSink*> sinkMap = ComponentManager::GetInstance().GetDHSinkInstance();
376     if (sinkMap.find(dhType) == sinkMap.end()) {
377         DHLOGE("StopDistributedHardware for DHType: %{public}u not init sink handler", (uint32_t)dhType);
378         return ERR_DH_FWK_PARA_INVALID;
379     }
380     if (sinkMap[dhType] == nullptr) {
381         DHLOGE("Sinkhandler ptr is null");
382         return ERR_DH_FWK_PARA_INVALID;
383     }
384     int32_t ret = sinkMap[dhType]->StopDistributedHardware(networkId);
385     if (ret != 0) {
386         DHLOGE("StopDistributedHardware for DHType: %{public}u failed, ret: %{public}d", (uint32_t)dhType, ret);
387         return ret;
388     }
389     return DH_FWK_SUCCESS;
390 }
391 
GetDistributedHardware(const std::string & networkId,EnableStep enableStep,const sptr<IGetDhDescriptorsCallback> callback)392 int32_t DistributedHardwareService::GetDistributedHardware(const std::string &networkId, EnableStep enableStep,
393     const sptr<IGetDhDescriptorsCallback> callback)
394 {
395     if (!IsIdLengthValid(networkId) || callback == nullptr) {
396         DHLOGE("networkId size is invalid or callback ptr is null");
397         return ERR_DH_FWK_PARA_INVALID;
398     }
399     std::string deviceId;
400     std::string realNetworkId;
401     if (networkId == LOCAL_NETWORKID_ALIAS) {
402         deviceId = GetLocalDeviceInfo().deviceId;
403         realNetworkId = GetLocalDeviceInfo().networkId;
404     } else {
405         deviceId = GetDeviceIdByUUID(DHContext::GetInstance().GetUUIDByNetworkId(networkId));
406         realNetworkId = networkId;
407     }
408     std::vector<DHDescriptor> descriptors;
409     std::string udid = DHContext::GetInstance().GetUDIDByNetworkId(realNetworkId);
410     std::string udidHash = Sha256(udid);
411     std::vector<std::shared_ptr<MetaCapabilityInfo>> metaCapInfos;
412     MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos);
413     if (!metaCapInfos.empty()) {
414         for (const auto &metaCapInfo : metaCapInfos) {
415             DHDescriptor descriptor;
416             descriptor.id = metaCapInfo->GetDHId();
417             descriptor.dhType = metaCapInfo->GetDHType();
418             descriptors.push_back(descriptor);
419         }
420         DHLOGI("Get MetacapInfo Success, networkId: %{public}s.", GetAnonyString(realNetworkId).c_str());
421         callback->OnSuccess(realNetworkId, descriptors, enableStep);
422         return DH_FWK_SUCCESS;
423     }
424 
425     std::vector<std::shared_ptr<CapabilityInfo>> capabilities;
426     CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilities);
427     if (!capabilities.empty()) {
428         for (const auto &capabilitie : capabilities) {
429             DHDescriptor descriptor;
430             descriptor.id = capabilitie->GetDHId();
431             descriptor.dhType = capabilitie->GetDHType();
432             descriptors.push_back(descriptor);
433         }
434         DHLOGI("Get CapabilitieInfo Success, deviceId: %{public}s.", GetAnonyString(deviceId).c_str());
435         callback->OnSuccess(realNetworkId, descriptors, enableStep);
436         return DH_FWK_SUCCESS;
437     }
438     VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(deviceId);
439     CapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId);
440     CapabilityInfoManager::GetInstance()->DoAsyncGetDistributedHardware(realNetworkId, enableStep, callback);
441     return DH_FWK_SUCCESS;
442 }
443 
RegisterDHStatusListener(sptr<IHDSinkStatusListener> listener)444 int32_t DistributedHardwareService::RegisterDHStatusListener(sptr<IHDSinkStatusListener> listener)
445 {
446     int32_t callingUid = IPCSkeleton::GetCallingUid();
447     int32_t callingPid = IPCSkeleton::GetCallingPid();
448     int32_t ret = ComponentManager::GetInstance().RegisterDHStatusListener(listener, callingUid, callingPid);
449     if (ret != 0) {
450         DHLOGE("RegisterDHStatusListener failed, ret: %{public}d.", ret);
451         return ret;
452     }
453     return DH_FWK_SUCCESS;
454 }
455 
UnregisterDHStatusListener(sptr<IHDSinkStatusListener> listener)456 int32_t DistributedHardwareService::UnregisterDHStatusListener(sptr<IHDSinkStatusListener> listener)
457 {
458     int32_t callingUid = IPCSkeleton::GetCallingUid();
459     int32_t callingPid = IPCSkeleton::GetCallingPid();
460     int32_t ret = ComponentManager::GetInstance().UnregisterDHStatusListener(listener, callingUid, callingPid);
461     if (ret != 0) {
462         DHLOGE("UnregisterDHStatusListener failed, ret: %{public}d.", ret);
463         return ret;
464     }
465     return DH_FWK_SUCCESS;
466 }
467 
RegisterDHStatusListener(const std::string & networkId,sptr<IHDSourceStatusListener> listener)468 int32_t DistributedHardwareService::RegisterDHStatusListener(
469     const std::string &networkId, sptr<IHDSourceStatusListener> listener)
470 {
471     if (!IsIdLengthValid(networkId)) {
472         return ERR_DH_FWK_PARA_INVALID;
473     }
474     int32_t callingUid = IPCSkeleton::GetCallingUid();
475     int32_t callingPid = IPCSkeleton::GetCallingPid();
476     int32_t ret = ComponentManager::GetInstance().RegisterDHStatusListener(
477         networkId, listener, callingUid, callingPid);
478     if (ret != 0) {
479         DHLOGE("RegisterDHStatusListener failed, ret: %{public}d.", ret);
480         return ret;
481     }
482     return DH_FWK_SUCCESS;
483 }
484 
UnregisterDHStatusListener(const std::string & networkId,sptr<IHDSourceStatusListener> listener)485 int32_t DistributedHardwareService::UnregisterDHStatusListener(
486     const std::string &networkId, sptr<IHDSourceStatusListener> listener)
487 {
488     if (!IsIdLengthValid(networkId)) {
489         return ERR_DH_FWK_PARA_INVALID;
490     }
491     int32_t callingUid = IPCSkeleton::GetCallingUid();
492     int32_t callingPid = IPCSkeleton::GetCallingPid();
493     int32_t ret = ComponentManager::GetInstance().UnregisterDHStatusListener(
494         networkId, listener, callingUid, callingPid);
495     if (ret != 0) {
496         DHLOGE("UnregisterDHStatusListener failed, ret: %{public}d.", ret);
497         return ret;
498     }
499     return DH_FWK_SUCCESS;
500 }
501 
EnableSink(const std::vector<DHDescriptor> & descriptors)502 int32_t DistributedHardwareService::EnableSink(const std::vector<DHDescriptor> &descriptors)
503 {
504     for (const auto &descriptor : descriptors) {
505         TaskParam taskParam = {
506             .dhId = descriptor.id,
507             .dhType = descriptor.dhType,
508             .effectSink = true,
509             .effectSource = false,
510             .callingUid = IPCSkeleton::GetCallingUid(),
511             .callingPid = IPCSkeleton::GetCallingPid()
512         };
513         auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr);
514         TaskExecutor::GetInstance().PushTask(task);
515     }
516     return DH_FWK_SUCCESS;
517 }
518 
DisableSink(const std::vector<DHDescriptor> & descriptors)519 int32_t DistributedHardwareService::DisableSink(const std::vector<DHDescriptor> &descriptors)
520 {
521     for (const auto &descriptor : descriptors) {
522         TaskParam taskParam = {
523             .dhId = descriptor.id,
524             .dhType = descriptor.dhType,
525             .effectSink = true,
526             .effectSource = false,
527             .callingUid = IPCSkeleton::GetCallingUid(),
528             .callingPid = IPCSkeleton::GetCallingPid()
529         };
530         auto task = TaskFactory::GetInstance().CreateTask(TaskType::DISABLE, taskParam, nullptr);
531         TaskExecutor::GetInstance().PushTask(task);
532     }
533     return DH_FWK_SUCCESS;
534 }
535 
EnableSource(const std::string & networkId,const std::vector<DHDescriptor> & descriptors)536 int32_t DistributedHardwareService::EnableSource(
537     const std::string &networkId, const std::vector<DHDescriptor> &descriptors)
538 {
539     if (!IsIdLengthValid(networkId)) {
540         return ERR_DH_FWK_PARA_INVALID;
541     }
542     for (const auto &descriptor : descriptors) {
543         TaskParam taskParam = {
544             .networkId = networkId,
545             .dhId = descriptor.id,
546             .dhType = descriptor.dhType,
547             .effectSink = false,
548             .effectSource = true,
549             .callingUid = IPCSkeleton::GetCallingUid(),
550             .callingPid = IPCSkeleton::GetCallingPid()
551         };
552         auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr);
553         TaskExecutor::GetInstance().PushTask(task);
554     }
555     return DH_FWK_SUCCESS;
556 }
557 
DisableSource(const std::string & networkId,const std::vector<DHDescriptor> & descriptors)558 int32_t DistributedHardwareService::DisableSource(
559     const std::string &networkId, const std::vector<DHDescriptor> &descriptors)
560 {
561     if (!IsIdLengthValid(networkId)) {
562         return ERR_DH_FWK_PARA_INVALID;
563     }
564     for (const auto &descriptor : descriptors) {
565         TaskParam taskParam = {
566             .networkId = networkId,
567             .dhId = descriptor.id,
568             .dhType = descriptor.dhType,
569             .effectSink = false,
570             .effectSource = true,
571             .callingUid = IPCSkeleton::GetCallingUid(),
572             .callingPid = IPCSkeleton::GetCallingPid()
573         };
574         auto task = TaskFactory::GetInstance().CreateTask(TaskType::DISABLE, taskParam, nullptr);
575         TaskExecutor::GetInstance().PushTask(task);
576     }
577     return DH_FWK_SUCCESS;
578 }
579 
LoadDistributedHDF(const DHType dhType)580 int32_t DistributedHardwareService::LoadDistributedHDF(const DHType dhType)
581 {
582     switch (dhType) {
583         case DHType::AUDIO:
584         case DHType::CAMERA:
585             return HdfOperateManager::GetInstance().LoadDistributedHDF(dhType);
586         default:
587             break;
588     }
589     return ERR_DH_FWK_NO_HDF_SUPPORT;
590 }
591 
UnLoadDistributedHDF(const DHType dhType)592 int32_t DistributedHardwareService::UnLoadDistributedHDF(const DHType dhType)
593 {
594     switch (dhType) {
595         case DHType::AUDIO:
596         case DHType::CAMERA:
597             return HdfOperateManager::GetInstance().UnLoadDistributedHDF(dhType);
598         default:
599             break;
600     }
601     return ERR_DH_FWK_NO_HDF_SUPPORT;
602 }
603 } // namespace DistributedHardware
604 } // namespace OHOS
605