1 /*
2  * Copyright (c) 2022-2023 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 "iservstat_listener_hdi.h"
24 #include "idevmgr_hdi.h"
25 #include "iservmgr_hdi.h"
26 #include "single_instance.h"
27 
28 using OHOS::HDI::DeviceManager::V1_0::IDeviceManager;
29 using OHOS::HDI::ServiceManager::V1_0::IServiceManager;
30 using OHOS::HDI::ServiceManager::V1_0::IServStatListener;
31 using OHOS::HDI::ServiceManager::V1_0::ServiceStatus;
32 using OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub;
33 const std::string CAMERA_SERVICE_NAME = "distributed_camera_service";
34 const std::string PROVIDER_SERVICE_NAME = "distributed_camera_provider_service";
35 constexpr uint16_t INVALID_VALUE = 0xffff;
36 constexpr int32_t WAIT_TIME = 2000;
37 
38 namespace OHOS {
39 namespace DistributedHardware {
40 class DCameraHdfOperate {
41 DECLARE_SINGLE_INSTANCE(DCameraHdfOperate);
42 
43 public:
44     int32_t LoadDcameraHDFImpl();
45     int32_t UnLoadDcameraHDFImpl();
46 
47 private:
48     int32_t WaitLoadCameraService();
49     int32_t WaitLoadProviderService();
50 
51 private:
52     std::atomic<uint16_t> cameraServStatus_ = INVALID_VALUE;
53     std::atomic<uint16_t> providerServStatus_ = INVALID_VALUE;
54     std::condition_variable hdfOperateCon_;
55     std::mutex hdfOperateMutex_;
56 };
57 
58 class DCameraHdfServStatListener : public OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub {
59 public:
60     using StatusCallback = std::function<void(const ServiceStatus &)>;
DCameraHdfServStatListener(StatusCallback callback)61     explicit DCameraHdfServStatListener(StatusCallback callback) : callback_(std::move(callback))
62     {
63     }
64     ~DCameraHdfServStatListener() override = default;
65     void OnReceive(const ServiceStatus& status) override;
66 
67 private:
68     StatusCallback callback_;
69 };
70 } // namespace DistributedHardware
71 } // namespace OHOS
72 #endif // OHOS_DCAMERA_HDF_OPERATE_H