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_1_CAMERAHOSTPROXY_H
17#define OHOS_HDI_CAMERA_V1_1_CAMERAHOSTPROXY_H
18
19#include "v1_0/camera_host_proxy.h"
20#include "v1_1/icamera_host.h"
21#include <unistd.h>
22#include <iproxy_broker.h>
23
24namespace OHOS {
25namespace HDI {
26namespace Camera {
27namespace V1_1 {
28
29class CameraHostProxy : public IProxyBroker<OHOS::HDI::Camera::V1_1::ICameraHost> {
30public:
31    class IServiceManagerDeathRecipient : public IRemoteObject::DeathRecipient {
32    public:
33        IServiceManagerDeathRecipient(wptr<OHOS::HDI::Camera::V1_1::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_1::CameraHostProxy::Reconnect(proxy);
46                }
47                --currentTime;
48            } while (result != HDF_SUCCESS && currentTime >0);
49        }
50    private:
51        wptr<OHOS::HDI::Camera::V1_1::CameraHostProxy> proxy_;
52    };
53
54    explicit CameraHostProxy(const sptr<IRemoteObject>& remote) : IProxyBroker<OHOS::HDI::Camera::V1_1::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_1(const std::string& cameraId,
72         const sptr<OHOS::HDI::Camera::V1_0::ICameraDeviceCallback>& callbackObj, sptr<OHOS::HDI::Camera::V1_1::ICameraDevice>& device) override;
73
74    int32_t Prelaunch(const OHOS::HDI::Camera::V1_1::PrelaunchConfig& config) override;
75
76    int32_t SetCallback(const sptr<OHOS::HDI::Camera::V1_0::ICameraHostCallback>& callbackObj) override;
77
78    int32_t GetCameraIds(std::vector<std::string>& cameraIds) override;
79
80    int32_t GetCameraAbility(const std::string& cameraId, std::vector<uint8_t>& cameraAbility) override;
81
82    int32_t OpenCamera(const std::string& cameraId,
83         const sptr<OHOS::HDI::Camera::V1_0::ICameraDeviceCallback>& callbackObj, sptr<OHOS::HDI::Camera::V1_0::ICameraDevice>& device) override;
84
85    int32_t SetFlashlight(const std::string& cameraId, bool isEnable) override;
86
87    int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) override;
88
89    static int32_t OpenCamera_V1_1_(const std::string& cameraId,
90         const sptr<OHOS::HDI::Camera::V1_0::ICameraDeviceCallback>& callbackObj, sptr<OHOS::HDI::Camera::V1_1::ICameraDevice>& device, const sptr<IRemoteObject> remote);
91
92    static int32_t Prelaunch_(const OHOS::HDI::Camera::V1_1::PrelaunchConfig& config, const sptr<IRemoteObject> remote);
93
94    static int32_t Reconnect(sptr<OHOS::HDI::Camera::V1_1::CameraHostProxy> proxy);
95
96    sptr<IRemoteObject> GetCurrentRemote() {
97        return isReconnected_ ? reconnectRemote_ : Remote();
98    }
99
100    bool isReconnected_;
101    std::string serviceName_;
102    sptr<IRemoteObject> servMgr_;
103    sptr<OHOS::HDI::Camera::V1_1::CameraHostProxy::IServiceManagerDeathRecipient> deathRecipient_;
104    sptr<IRemoteObject> reconnectRemote_;
105private:
106    static inline BrokerDelegator<OHOS::HDI::Camera::V1_1::CameraHostProxy> delegator_;
107};
108
109} // V1_1
110} // Camera
111} // HDI
112} // OHOS
113
114#endif // OHOS_HDI_CAMERA_V1_1_CAMERAHOSTPROXY_H
115
116