1/*
2 * Copyright (c) 2022 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_0_CAMERAHOSTPROXY_H
17#define OHOS_HDI_CAMERA_V1_0_CAMERAHOSTPROXY_H
18
19#include "v1_0/icamera_host.h"
20#include <unistd.h>
21#include <iproxy_broker.h>
22
23namespace OHOS {
24namespace HDI {
25namespace Camera {
26namespace V1_0 {
27
28class CameraHostProxy : public IProxyBroker<OHOS::HDI::Camera::V1_0::ICameraHost> {
29public:
30    class IServiceManagerDeathRecipient : public IRemoteObject::DeathRecipient {
31    public:
32        IServiceManagerDeathRecipient(wptr<OHOS::HDI::Camera::V1_0::CameraHostProxy> proxy) : proxy_(proxy) {}
33        ~IServiceManagerDeathRecipient() override = default;
34        void OnRemoteDied(const wptr<IRemoteObject> &remote) override
35        {
36            int32_t result = HDF_FAILURE;
37            const int sleepInterval = 500000;
38            const int waitTimes = 10;
39            int currentTime = waitTimes;
40            do {
41                usleep(sleepInterval);
42                auto proxy = proxy_.promote();
43                if (proxy != nullptr) {
44                    result = OHOS::HDI::Camera::V1_0::CameraHostProxy::Reconnect(proxy);
45                }
46                --currentTime;
47            } while (result != HDF_SUCCESS && currentTime >0);
48        }
49    private:
50        wptr<OHOS::HDI::Camera::V1_0::CameraHostProxy> proxy_;
51    };
52
53    explicit CameraHostProxy(const sptr<IRemoteObject>& remote) : IProxyBroker<OHOS::HDI::Camera::V1_0::ICameraHost>(remote) {
54        reconnectRemote_ = nullptr;
55        servMgr_ = nullptr;
56        deathRecipient_ = nullptr;
57        isReconnected_ = false;
58    }
59    virtual ~CameraHostProxy() {
60        if (servMgr_ != nullptr && deathRecipient_ != nullptr) {
61            servMgr_->RemoveDeathRecipient(deathRecipient_);
62        }
63    }
64
65    inline bool IsProxy() override
66    {
67        return true;
68    }
69
70    int32_t SetCallback(const sptr<OHOS::HDI::Camera::V1_0::ICameraHostCallback>& callbackObj) override;
71
72    int32_t GetCameraIds(std::vector<std::string>& cameraIds) override;
73
74    int32_t GetCameraAbility(const std::string& cameraId, std::vector<uint8_t>& cameraAbility) override;
75
76    int32_t OpenCamera(const std::string& cameraId,
77         const sptr<OHOS::HDI::Camera::V1_0::ICameraDeviceCallback>& callbackObj, sptr<OHOS::HDI::Camera::V1_0::ICameraDevice>& device) override;
78
79    int32_t SetFlashlight(const std::string& cameraId, bool isEnable) override;
80
81    int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) override;
82
83    static int32_t SetCallback_(const sptr<OHOS::HDI::Camera::V1_0::ICameraHostCallback>& callbackObj,
84         const sptr<IRemoteObject> remote);
85
86    static int32_t GetCameraIds_(std::vector<std::string>& cameraIds, const sptr<IRemoteObject> remote);
87
88    static int32_t GetCameraAbility_(const std::string& cameraId, std::vector<uint8_t>& cameraAbility,
89         const sptr<IRemoteObject> remote);
90
91    static int32_t OpenCamera_(const std::string& cameraId,
92         const sptr<OHOS::HDI::Camera::V1_0::ICameraDeviceCallback>& callbackObj, sptr<OHOS::HDI::Camera::V1_0::ICameraDevice>& device, const sptr<IRemoteObject> remote);
93
94    static int32_t SetFlashlight_(const std::string& cameraId, bool isEnable, const sptr<IRemoteObject> remote);
95
96    static int32_t GetVersion_(uint32_t& majorVer, uint32_t& minorVer, const sptr<IRemoteObject> remote);
97
98    static int32_t Reconnect(sptr<OHOS::HDI::Camera::V1_0::CameraHostProxy> proxy);
99
100    sptr<IRemoteObject> GetCurrentRemote() {
101        return isReconnected_ ? reconnectRemote_ : Remote();
102    }
103
104    bool isReconnected_;
105    std::string serviceName_;
106    sptr<IRemoteObject> servMgr_;
107    sptr<OHOS::HDI::Camera::V1_0::CameraHostProxy::IServiceManagerDeathRecipient> deathRecipient_;
108    sptr<IRemoteObject> reconnectRemote_;
109private:
110    static inline BrokerDelegator<OHOS::HDI::Camera::V1_0::CameraHostProxy> delegator_;
111};
112
113} // V1_0
114} // Camera
115} // HDI
116} // OHOS
117
118#endif // OHOS_HDI_CAMERA_V1_0_CAMERAHOSTPROXY_H
119
120