1/*
2 * Copyright (c) 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_HDI_CAMERA_V1_2_CAMERAHOSTPROXY_H
17#define OHOS_HDI_CAMERA_V1_2_CAMERAHOSTPROXY_H
18
19#include "v1_1/camera_host_proxy.h"
20#include "v1_2/icamera_host.h"
21#include <unistd.h>
22#include <iproxy_broker.h>
23
24namespace OHOS {
25namespace HDI {
26namespace Camera {
27namespace V1_2 {
28
29class CameraHostProxy : public IProxyBroker<OHOS::HDI::Camera::V1_2::ICameraHost> {
30public:
31    class IServiceManagerDeathRecipient : public IRemoteObject::DeathRecipient {
32    public:
33        IServiceManagerDeathRecipient(wptr<OHOS::HDI::Camera::V1_2::CameraHostProxy> proxy) : proxy_(proxy) {}
34        ~IServiceManagerDeathRecipient() override = default;
35        void OnRemoteDied(const wptr<IRemoteObject> &remote) override
36        {
37            int32_t result = HDF_FAILURE;
38            const int sleepInterval = 500000;
39            const int waitTimes = 10;
40            int currentTime = waitTimes;
41            do {
42                usleep(sleepInterval);
43                auto proxy = proxy_.promote();
44                if (proxy != nullptr) {
45                    result = OHOS::HDI::Camera::V1_2::CameraHostProxy::Reconnect(proxy);
46                }
47                --currentTime;
48            } while (result != HDF_SUCCESS && currentTime >0);
49        }
50    private:
51        wptr<OHOS::HDI::Camera::V1_2::CameraHostProxy> proxy_;
52    };
53
54    explicit CameraHostProxy(const sptr<IRemoteObject>& remote) : IProxyBroker<OHOS::HDI::Camera::V1_2::ICameraHost>(remote) {
55        reconnectRemote_ = nullptr;
56        servMgr_ = nullptr;
57        deathRecipient_ = nullptr;
58        isReconnected_ = false;
59    }
60    virtual ~CameraHostProxy() {
61        if (servMgr_ != nullptr && deathRecipient_ != nullptr) {
62            servMgr_->RemoveDeathRecipient(deathRecipient_);
63        }
64    }
65
66    inline bool IsProxy() override
67    {
68        return true;
69    }
70
71    int32_t OpenCamera_V1_2(const std::string& cameraId,
72         const sptr<OHOS::HDI::Camera::V1_0::ICameraDeviceCallback>& callbackObj, sptr<OHOS::HDI::Camera::V1_2::ICameraDevice>& device) override;
73
74    int32_t NotifyDeviceStateChangeInfo(int32_t notifyType, int32_t deviceState) override;
75
76    int32_t SetCallback_V1_2(const sptr<OHOS::HDI::Camera::V1_2::ICameraHostCallback>& callbackObj) override;
77
78    int32_t SetFlashlight_V1_2(float level) override;
79
80    int32_t PreCameraSwitch(const std::string& cameraId) override;
81
82    int32_t PrelaunchWithOpMode(const OHOS::HDI::Camera::V1_1::PrelaunchConfig& config, int32_t operationMode) override;
83
84    int32_t OpenCamera_V1_1(const std::string& cameraId,
85         const sptr<OHOS::HDI::Camera::V1_0::ICameraDeviceCallback>& callbackObj, sptr<OHOS::HDI::Camera::V1_1::ICameraDevice>& device) override;
86
87    int32_t Prelaunch(const OHOS::HDI::Camera::V1_1::PrelaunchConfig& config) override;
88
89    int32_t SetCallback(const sptr<OHOS::HDI::Camera::V1_0::ICameraHostCallback>& callbackObj) override;
90
91    int32_t GetCameraIds(std::vector<std::string>& cameraIds) override;
92
93    int32_t GetCameraAbility(const std::string& cameraId, std::vector<uint8_t>& cameraAbility) override;
94
95    int32_t OpenCamera(const std::string& cameraId,
96         const sptr<OHOS::HDI::Camera::V1_0::ICameraDeviceCallback>& callbackObj, sptr<OHOS::HDI::Camera::V1_0::ICameraDevice>& device) override;
97
98    int32_t SetFlashlight(const std::string& cameraId, bool isEnable) override;
99
100    int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) override;
101
102    static int32_t OpenCamera_V1_2_(const std::string& cameraId,
103         const sptr<OHOS::HDI::Camera::V1_0::ICameraDeviceCallback>& callbackObj, sptr<OHOS::HDI::Camera::V1_2::ICameraDevice>& device, const sptr<IRemoteObject> remote);
104
105    static int32_t NotifyDeviceStateChangeInfo_(int32_t notifyType, int32_t deviceState,
106         const sptr<IRemoteObject> remote);
107
108    static int32_t SetCallback_V1_2_(const sptr<OHOS::HDI::Camera::V1_2::ICameraHostCallback>& callbackObj,
109         const sptr<IRemoteObject> remote);
110
111    static int32_t SetFlashlight_V1_2_(float level, const sptr<IRemoteObject> remote);
112
113    static int32_t PreCameraSwitch_(const std::string& cameraId, const sptr<IRemoteObject> remote);
114
115    static int32_t PrelaunchWithOpMode_(const OHOS::HDI::Camera::V1_1::PrelaunchConfig& config, int32_t operationMode,
116         const sptr<IRemoteObject> remote);
117
118    static int32_t Reconnect(sptr<OHOS::HDI::Camera::V1_2::CameraHostProxy> proxy);
119
120    sptr<IRemoteObject> GetCurrentRemote() {
121        return isReconnected_ ? reconnectRemote_ : Remote();
122    }
123
124    bool isReconnected_;
125    std::string serviceName_;
126    sptr<IRemoteObject> servMgr_;
127    sptr<OHOS::HDI::Camera::V1_2::CameraHostProxy::IServiceManagerDeathRecipient> deathRecipient_;
128    sptr<IRemoteObject> reconnectRemote_;
129private:
130    static inline BrokerDelegator<OHOS::HDI::Camera::V1_2::CameraHostProxy> delegator_;
131};
132
133} // V1_2
134} // Camera
135} // HDI
136} // OHOS
137
138#endif // OHOS_HDI_CAMERA_V1_2_CAMERAHOSTPROXY_H
139
140