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_IMAGEPROCESSSERVICEPROXY_H
17#define OHOS_HDI_CAMERA_V1_2_IMAGEPROCESSSERVICEPROXY_H
18
19#include "v1_2/iimage_process_service.h"
20#include <unistd.h>
21#include <iproxy_broker.h>
22
23namespace OHOS {
24namespace HDI {
25namespace Camera {
26namespace V1_2 {
27
28class ImageProcessServiceProxy : public IProxyBroker<OHOS::HDI::Camera::V1_2::IImageProcessService> {
29public:
30    class IServiceManagerDeathRecipient : public IRemoteObject::DeathRecipient {
31    public:
32        IServiceManagerDeathRecipient(wptr<OHOS::HDI::Camera::V1_2::ImageProcessServiceProxy> 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_2::ImageProcessServiceProxy::Reconnect(proxy);
45                }
46                --currentTime;
47            } while (result != HDF_SUCCESS && currentTime >0);
48        }
49    private:
50        wptr<OHOS::HDI::Camera::V1_2::ImageProcessServiceProxy> proxy_;
51    };
52
53    explicit ImageProcessServiceProxy(const sptr<IRemoteObject>& remote) : IProxyBroker<OHOS::HDI::Camera::V1_2::IImageProcessService>(remote) {
54        reconnectRemote_ = nullptr;
55        servMgr_ = nullptr;
56        deathRecipient_ = nullptr;
57        isReconnected_ = false;
58    }
59    virtual ~ImageProcessServiceProxy() {
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 CreateImageProcessSession(int32_t userId,
71         const sptr<OHOS::HDI::Camera::V1_2::IImageProcessCallback>& imageProcessCallback, sptr<OHOS::HDI::Camera::V1_2::IImageProcessSession>& imageProcessSession) override;
72
73    int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) override;
74
75    static int32_t CreateImageProcessSession_(int32_t userId,
76         const sptr<OHOS::HDI::Camera::V1_2::IImageProcessCallback>& imageProcessCallback, sptr<OHOS::HDI::Camera::V1_2::IImageProcessSession>& imageProcessSession, const sptr<IRemoteObject> remote);
77
78    static int32_t GetVersion_(uint32_t& majorVer, uint32_t& minorVer, const sptr<IRemoteObject> remote);
79
80    static int32_t Reconnect(sptr<OHOS::HDI::Camera::V1_2::ImageProcessServiceProxy> proxy);
81
82    sptr<IRemoteObject> GetCurrentRemote() {
83        return isReconnected_ ? reconnectRemote_ : Remote();
84    }
85
86    bool isReconnected_;
87    std::string serviceName_;
88    sptr<IRemoteObject> servMgr_;
89    sptr<OHOS::HDI::Camera::V1_2::ImageProcessServiceProxy::IServiceManagerDeathRecipient> deathRecipient_;
90    sptr<IRemoteObject> reconnectRemote_;
91private:
92    static inline BrokerDelegator<OHOS::HDI::Camera::V1_2::ImageProcessServiceProxy> delegator_;
93};
94
95} // V1_2
96} // Camera
97} // HDI
98} // OHOS
99
100#endif // OHOS_HDI_CAMERA_V1_2_IMAGEPROCESSSERVICEPROXY_H
101
102