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_BATTERY_V2_0_BATTERYINTERFACEPROXY_H
17#define OHOS_HDI_BATTERY_V2_0_BATTERYINTERFACEPROXY_H
18
19#include "v2_0/ibattery_interface.h"
20#include <unistd.h>
21#include <iproxy_broker.h>
22
23namespace OHOS {
24namespace HDI {
25namespace Battery {
26namespace V2_0 {
27
28class BatteryInterfaceProxy : public IProxyBroker<OHOS::HDI::Battery::V2_0::IBatteryInterface> {
29public:
30    class IServiceManagerDeathRecipient : public IRemoteObject::DeathRecipient {
31    public:
32        IServiceManagerDeathRecipient(wptr<OHOS::HDI::Battery::V2_0::BatteryInterfaceProxy> 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::Battery::V2_0::BatteryInterfaceProxy::Reconnect(proxy);
45                }
46                --currentTime;
47            } while (result != HDF_SUCCESS && currentTime >0);
48        }
49    private:
50        wptr<OHOS::HDI::Battery::V2_0::BatteryInterfaceProxy> proxy_;
51    };
52
53    explicit BatteryInterfaceProxy(const sptr<IRemoteObject>& remote) : IProxyBroker<OHOS::HDI::Battery::V2_0::IBatteryInterface>(remote) {
54        reconnectRemote_ = nullptr;
55        servMgr_ = nullptr;
56        deathRecipient_ = nullptr;
57        isReconnected_ = false;
58    }
59    virtual ~BatteryInterfaceProxy() {
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 Register(const sptr<OHOS::HDI::Battery::V2_0::IBatteryCallback>& event) override;
71
72    int32_t UnRegister() override;
73
74    int32_t ChangePath(const std::string& path) override;
75
76    int32_t GetCapacity(int32_t& capacity) override;
77
78    int32_t GetVoltage(int32_t& voltage) override;
79
80    int32_t GetTemperature(int32_t& temperature) override;
81
82    int32_t GetHealthState(OHOS::HDI::Battery::V2_0::BatteryHealthState& healthState) override;
83
84    int32_t GetPluggedType(OHOS::HDI::Battery::V2_0::BatteryPluggedType& pluggedType) override;
85
86    int32_t GetChargeState(OHOS::HDI::Battery::V2_0::BatteryChargeState& chargeState) override;
87
88    int32_t GetPresent(bool& present) override;
89
90    int32_t GetTechnology(std::string& technology) override;
91
92    int32_t GetTotalEnergy(int32_t& totalEnergy) override;
93
94    int32_t GetCurrentAverage(int32_t& curAverage) override;
95
96    int32_t GetCurrentNow(int32_t& curNow) override;
97
98    int32_t GetRemainEnergy(int32_t& remainEnergy) override;
99
100    int32_t GetBatteryInfo(OHOS::HDI::Battery::V2_0::BatteryInfo& info) override;
101
102    int32_t SetChargingLimit(const std::vector<OHOS::HDI::Battery::V2_0::ChargingLimit>& chargingLimit) override;
103
104    int32_t GetChargeType(OHOS::HDI::Battery::V2_0::ChargeType& type) override;
105
106    int32_t SetBatteryConfig(const std::string& sceneName, const std::string& value) override;
107
108    int32_t GetBatteryConfig(const std::string& sceneName, std::string& value) override;
109
110    int32_t IsBatteryConfigSupported(const std::string& sceneName, bool& value) override;
111
112    int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) override;
113
114    static int32_t Register_(const sptr<OHOS::HDI::Battery::V2_0::IBatteryCallback>& event,
115         const sptr<IRemoteObject> remote);
116
117    static int32_t UnRegister_(const sptr<IRemoteObject> remote);
118
119    static int32_t ChangePath_(const std::string& path, const sptr<IRemoteObject> remote);
120
121    static int32_t GetCapacity_(int32_t& capacity, const sptr<IRemoteObject> remote);
122
123    static int32_t GetVoltage_(int32_t& voltage, const sptr<IRemoteObject> remote);
124
125    static int32_t GetTemperature_(int32_t& temperature, const sptr<IRemoteObject> remote);
126
127    static int32_t GetHealthState_(OHOS::HDI::Battery::V2_0::BatteryHealthState& healthState,
128         const sptr<IRemoteObject> remote);
129
130    static int32_t GetPluggedType_(OHOS::HDI::Battery::V2_0::BatteryPluggedType& pluggedType,
131         const sptr<IRemoteObject> remote);
132
133    static int32_t GetChargeState_(OHOS::HDI::Battery::V2_0::BatteryChargeState& chargeState,
134         const sptr<IRemoteObject> remote);
135
136    static int32_t GetPresent_(bool& present, const sptr<IRemoteObject> remote);
137
138    static int32_t GetTechnology_(std::string& technology, const sptr<IRemoteObject> remote);
139
140    static int32_t GetTotalEnergy_(int32_t& totalEnergy, const sptr<IRemoteObject> remote);
141
142    static int32_t GetCurrentAverage_(int32_t& curAverage, const sptr<IRemoteObject> remote);
143
144    static int32_t GetCurrentNow_(int32_t& curNow, const sptr<IRemoteObject> remote);
145
146    static int32_t GetRemainEnergy_(int32_t& remainEnergy, const sptr<IRemoteObject> remote);
147
148    static int32_t GetBatteryInfo_(OHOS::HDI::Battery::V2_0::BatteryInfo& info, const sptr<IRemoteObject> remote);
149
150    static int32_t SetChargingLimit_(const std::vector<OHOS::HDI::Battery::V2_0::ChargingLimit>& chargingLimit,
151         const sptr<IRemoteObject> remote);
152
153    static int32_t GetChargeType_(OHOS::HDI::Battery::V2_0::ChargeType& type, const sptr<IRemoteObject> remote);
154
155    static int32_t SetBatteryConfig_(const std::string& sceneName, const std::string& value,
156         const sptr<IRemoteObject> remote);
157
158    static int32_t GetBatteryConfig_(const std::string& sceneName, std::string& value,
159         const sptr<IRemoteObject> remote);
160
161    static int32_t IsBatteryConfigSupported_(const std::string& sceneName, bool& value,
162         const sptr<IRemoteObject> remote);
163
164    static int32_t GetVersion_(uint32_t& majorVer, uint32_t& minorVer, const sptr<IRemoteObject> remote);
165
166    static int32_t Reconnect(sptr<OHOS::HDI::Battery::V2_0::BatteryInterfaceProxy> proxy);
167
168    sptr<IRemoteObject> GetCurrentRemote() {
169        return isReconnected_ ? reconnectRemote_ : Remote();
170    }
171
172    bool isReconnected_;
173    std::string serviceName_;
174    sptr<IRemoteObject> servMgr_;
175    sptr<OHOS::HDI::Battery::V2_0::BatteryInterfaceProxy::IServiceManagerDeathRecipient> deathRecipient_;
176    sptr<IRemoteObject> reconnectRemote_;
177private:
178    static inline BrokerDelegator<OHOS::HDI::Battery::V2_0::BatteryInterfaceProxy> delegator_;
179};
180
181} // V2_0
182} // Battery
183} // HDI
184} // OHOS
185
186#endif // OHOS_HDI_BATTERY_V2_0_BATTERYINTERFACEPROXY_H
187
188