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_DCAMERA_HDF_OPERATE_H 17 #define OHOS_DCAMERA_HDF_OPERATE_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <mutex> 22 23 #include "idistributed_hardware_source.h" 24 #include "iservstat_listener_hdi.h" 25 #include "idevmgr_hdi.h" 26 #include "iservmgr_hdi.h" 27 #include "single_instance.h" 28 #include "v1_1/id_camera_provider.h" 29 30 namespace OHOS { 31 namespace DistributedHardware { 32 using OHOS::HDI::DeviceManager::V1_0::IDeviceManager; 33 using OHOS::HDI::ServiceManager::V1_0::IServiceManager; 34 using OHOS::HDI::ServiceManager::V1_0::IServStatListener; 35 using OHOS::HDI::ServiceManager::V1_0::ServiceStatus; 36 using OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub; 37 using OHOS::HDI::DistributedCamera::V1_1::IDCameraProvider; 38 using OHOS::HDI::DistributedCamera::V1_1::IDCameraHdfCallback; 39 using OHOS::HDI::DistributedCamera::V1_1::DCameraHDFEvent; 40 const std::string CAMERA_SERVICE_NAME = "distributed_camera_service"; 41 const std::string PROVIDER_SERVICE_NAME = "distributed_camera_provider_service"; 42 const std::string HDF_LISTENER_SERVICE_NAME = "DHFWK"; 43 constexpr uint16_t CAMERA_INVALID_VALUE = 0xffff; 44 constexpr int32_t CAMERA_WAIT_TIME = 2000; 45 46 class FwkDCameraHdfCallback; 47 class HdfDeathRecipient : public IRemoteObject::DeathRecipient { 48 public: 49 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 50 }; 51 class DCameraHdfOperate { 52 DECLARE_SINGLE_INSTANCE(DCameraHdfOperate); 53 54 public: 55 int32_t LoadDcameraHDFImpl(std::shared_ptr<HdfDeathCallback> callback); 56 int32_t UnLoadDcameraHDFImpl(); 57 void OnHdfHostDied(); 58 59 private: 60 int32_t WaitLoadCameraService(); 61 int32_t WaitLoadProviderService(); 62 OHOS::sptr<IServStatListener> MakeServStatListener(); 63 int32_t LoadDevice(); 64 int32_t UnLoadDevice(); 65 int32_t RegisterHdfListener(); 66 int32_t UnRegisterHdfListener(); 67 int32_t AddHdfDeathBind(); 68 int32_t RemoveHdfDeathBind(); 69 int32_t MakeFwkDCameraHdfCallback(); 70 71 private: 72 OHOS::sptr<IDeviceManager> devmgr_; 73 OHOS::sptr<IServiceManager> servMgr_; 74 sptr<IDCameraProvider> camHdiProvider_; 75 std::mutex fwkDCameraHdfCallbackMutex_; 76 OHOS::sptr<FwkDCameraHdfCallback> fwkDCameraHdfCallback_; 77 std::atomic<uint16_t> cameraServStatus_ = CAMERA_INVALID_VALUE; 78 std::atomic<uint16_t> providerServStatus_ = CAMERA_INVALID_VALUE; 79 std::condition_variable hdfOperateCon_; 80 std::mutex hdfOperateMutex_; 81 std::shared_ptr<HdfDeathCallback> hdfDeathCallback_; 82 sptr<HdfDeathRecipient> hdfDeathRecipient_ = sptr<HdfDeathRecipient>(new HdfDeathRecipient()); 83 }; 84 85 class DCameraHdfServStatListener : public OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub { 86 public: 87 using StatusCallback = std::function<void(const ServiceStatus &)>; DCameraHdfServStatListener(StatusCallback callback)88 explicit DCameraHdfServStatListener(StatusCallback callback) : callback_(std::move(callback)) 89 { 90 } 91 ~DCameraHdfServStatListener() override = default; 92 void OnReceive(const ServiceStatus& status) override; 93 94 private: 95 StatusCallback callback_; 96 }; 97 98 class FwkDCameraHdfCallback : public IDCameraHdfCallback { 99 protected: 100 int32_t NotifyEvent(int32_t devId, const DCameraHDFEvent& event) override; 101 }; 102 } // namespace DistributedHardware 103 } // namespace OHOS 104 #endif // OHOS_DCAMERA_HDF_OPERATE_H