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 #ifndef OHOS_HDF_OPERATE_H 17 #define OHOS_HDF_OPERATE_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <map> 22 #include <mutex> 23 24 #include "idistributed_hardware.h" 25 #include "idistributed_hardware_source.h" 26 #include "single_instance.h" 27 28 namespace OHOS { 29 namespace DistributedHardware { 30 class HdfLoadRefRecipient : public IRemoteObject::DeathRecipient { 31 public: HdfLoadRefRecipient(DHType dhType)32 explicit HdfLoadRefRecipient(DHType dhType) : dhType_(dhType) {} 33 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 34 private: 35 DHType dhType_; 36 }; 37 38 class HdfDeathCallbackImpl : public HdfDeathCallback { 39 public: HdfDeathCallbackImpl(DHType dhType)40 explicit HdfDeathCallbackImpl(DHType dhType) : dhType_(dhType) {} 41 virtual ~HdfDeathCallbackImpl() = default; 42 protected: 43 void OnHdfHostDied(); 44 private: 45 DHType dhType_; 46 }; 47 class HdfOperator { 48 public: HdfOperator(DHType dhType,IDistributedHardwareSource * sourcePtr)49 explicit HdfOperator(DHType dhType, IDistributedHardwareSource *sourcePtr) 50 : dhType_(dhType), sourcePtr_(sourcePtr) 51 { 52 hdfDeathCallback_ = std::make_shared<HdfDeathCallbackImpl>(dhType); 53 } 54 int32_t LoadDistributedHDF(); 55 int32_t UnLoadDistributedHDF(); 56 void ResetRefCount(); 57 bool IsNeedErase(); 58 private: 59 DHType dhType_ = DHType::UNKNOWN; 60 int32_t hdfLoadRef_ = 0; 61 std::mutex hdfLoadRefMutex_; 62 IDistributedHardwareSource *sourcePtr_ = nullptr; 63 std::shared_ptr<HdfDeathCallback> hdfDeathCallback_ = nullptr; 64 }; 65 66 class HdfOperateManager { 67 DECLARE_SINGLE_INSTANCE(HdfOperateManager); 68 public: 69 int32_t LoadDistributedHDF(DHType dhType); 70 int32_t UnLoadDistributedHDF(DHType dhType); 71 int32_t AddDeathRecipient(DHType dhType, sptr<IRemoteObject> &remote); 72 int32_t RemoveDeathRecipient(DHType dhType, sptr<IRemoteObject> &remote); 73 void ResetRefCount(DHType dhType); 74 int32_t RigidGetSourcePtr(DHType dhType, IDistributedHardwareSource *&sourcePtr); 75 int32_t RigidReleaseSourcePtr(DHType dhType); 76 bool IsAnyHdfInuse(); 77 78 private: 79 struct SourceHandlerData { 80 int32_t refCount; 81 void *sourceHandler; 82 IDistributedHardwareSource *sourcePtr; 83 }; 84 85 private: 86 std::mutex hdfOperateMapMutex_; 87 std::map<DHType, std::shared_ptr<HdfOperator>> hdfOperateMap_; 88 sptr<HdfLoadRefRecipient> audioHdfLoadRefRecipient_ = sptr(new HdfLoadRefRecipient(DHType::AUDIO)); 89 sptr<HdfLoadRefRecipient> cameraHdfLoadRefRecipient_ = sptr(new HdfLoadRefRecipient(DHType::CAMERA)); 90 std::mutex sourceHandlerDataMapMutex_; 91 std::map<DHType, SourceHandlerData> sourceHandlerDataMap_; 92 int32_t hdfInuseRefCount_ = 0; 93 }; 94 } // namespace DistributedHardware 95 } // namespace OHOS 96 #endif // OHOS_HDF_OPERATE_H