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_VIBRATOR_V1_2_VIBRATORINTERFACEPROXY_H
17#define OHOS_HDI_VIBRATOR_V1_2_VIBRATORINTERFACEPROXY_H
18
19#include "v1_1/vibrator_interface_proxy.h"
20#include "v1_2/ivibrator_interface.h"
21#include <unistd.h>
22#include <iproxy_broker.h>
23
24namespace OHOS {
25namespace HDI {
26namespace Vibrator {
27namespace V1_2 {
28
29class VibratorInterfaceProxy : public IProxyBroker<OHOS::HDI::Vibrator::V1_2::IVibratorInterface> {
30public:
31    class IServiceManagerDeathRecipient : public IRemoteObject::DeathRecipient {
32    public:
33        IServiceManagerDeathRecipient(wptr<OHOS::HDI::Vibrator::V1_2::VibratorInterfaceProxy> 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::Vibrator::V1_2::VibratorInterfaceProxy::Reconnect(proxy);
46                }
47                --currentTime;
48            } while (result != HDF_SUCCESS && currentTime >0);
49        }
50    private:
51        wptr<OHOS::HDI::Vibrator::V1_2::VibratorInterfaceProxy> proxy_;
52    };
53
54    explicit VibratorInterfaceProxy(const sptr<IRemoteObject>& remote) : IProxyBroker<OHOS::HDI::Vibrator::V1_2::IVibratorInterface>(remote) {
55        reconnectRemote_ = nullptr;
56        servMgr_ = nullptr;
57        deathRecipient_ = nullptr;
58        isReconnected_ = false;
59    }
60    virtual ~VibratorInterfaceProxy() {
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 PlayHapticPattern(const OHOS::HDI::Vibrator::V1_2::HapticPaket& pkg) override;
72
73    int32_t GetHapticCapacity(OHOS::HDI::Vibrator::V1_2::HapticCapacity& HapticCapacity) override;
74
75    int32_t GetHapticStartUpTime(int32_t mode, int32_t& startUpTime) override;
76
77    int32_t StopV1_2(int32_t mode) override;
78
79    int32_t StartOnce(uint32_t duration) override;
80
81    int32_t Start(const std::string& effectType) override;
82
83    int32_t Stop(OHOS::HDI::Vibrator::V1_1::HdfVibratorMode mode) override;
84
85    int32_t GetVibratorInfo(std::vector<OHOS::HDI::Vibrator::V1_1::HdfVibratorInfo>& vibratorInfo) override;
86
87    int32_t EnableVibratorModulation(uint32_t duration, uint16_t intensity, int16_t frequency) override;
88
89    int32_t EnableCompositeEffect(const OHOS::HDI::Vibrator::V1_1::HdfCompositeEffect& effect) override;
90
91    int32_t GetEffectInfo(const std::string& effectType, OHOS::HDI::Vibrator::V1_1::HdfEffectInfo& effectInfo) override;
92
93    int32_t IsVibratorRunning(bool& state) override;
94
95    int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) override;
96
97    static int32_t PlayHapticPattern_(const OHOS::HDI::Vibrator::V1_2::HapticPaket& pkg,
98         const sptr<IRemoteObject> remote);
99
100    static int32_t GetHapticCapacity_(OHOS::HDI::Vibrator::V1_2::HapticCapacity& HapticCapacity,
101         const sptr<IRemoteObject> remote);
102
103    static int32_t GetHapticStartUpTime_(int32_t mode, int32_t& startUpTime, const sptr<IRemoteObject> remote);
104
105    static int32_t StopV1_2_(int32_t mode, const sptr<IRemoteObject> remote);
106
107    static int32_t Reconnect(sptr<OHOS::HDI::Vibrator::V1_2::VibratorInterfaceProxy> proxy);
108
109    sptr<IRemoteObject> GetCurrentRemote() {
110        return isReconnected_ ? reconnectRemote_ : Remote();
111    }
112
113    bool isReconnected_;
114    std::string serviceName_;
115    sptr<IRemoteObject> servMgr_;
116    sptr<OHOS::HDI::Vibrator::V1_2::VibratorInterfaceProxy::IServiceManagerDeathRecipient> deathRecipient_;
117    sptr<IRemoteObject> reconnectRemote_;
118private:
119    static inline BrokerDelegator<OHOS::HDI::Vibrator::V1_2::VibratorInterfaceProxy> delegator_;
120};
121
122} // V1_2
123} // Vibrator
124} // HDI
125} // OHOS
126
127#endif // OHOS_HDI_VIBRATOR_V1_2_VIBRATORINTERFACEPROXY_H
128
129