1 /*
2  * Copyright (c) 2021-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 POWERMGR_BATTERY_SRV_CLIENT_H
17 #define POWERMGR_BATTERY_SRV_CLIENT_H
18 
19 #include <singleton.h>
20 #include <memory>
21 #include <mutex>
22 #include "iremote_object.h"
23 #include "ibattery_srv.h"
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 class BatterySrvClient final : public DelayedRefSingleton<BatterySrvClient> {
28     DECLARE_DELAYED_REF_SINGLETON(BatterySrvClient);
29 
30 public:
31     DISALLOW_COPY_AND_MOVE(BatterySrvClient);
32 
33     /**
34      * Return the capacity of the battery, in percent.
35      */
36     int32_t GetCapacity();
37     /**
38      * Return the charging status, such as CHARGE_STATE_NONE, CHARGE_STATE_ENABLE,
39      * CHARGE_STATE_DISABLE, CHARGE_STATE_FULL,...
40      */
41     BatteryChargeState GetChargingStatus();
42     /**
43      * Return the Health state of the battery, such as HEALTH_STATE_UNKNOWN,
44      * HEALTH_STATE_GOOD, HEALTH_STATE_OVERHEAT,....
45      */
46     BatteryHealthState GetHealthStatus();
47     /**
48      * Return the charger type plugged, such as PLUGGED_TYPE_NONE,
49      * PLUGGED_TYPE_AC, PLUGGED_TYPE_USB,....
50      */
51     BatteryPluggedType GetPluggedType();
52     /**
53      * Return the voltage of the battery, in mv.
54      */
55     int32_t GetVoltage();
56     /**
57      * Return the present state of the battery, true or false.
58      */
59     bool GetPresent();
60     /**
61      * Return the technology of the battery, such as Li-ion.
62      */
63     std::string GetTechnology();
64     /**
65      * Return the temperature of the battery, in 0.1℃.
66      */
67     int32_t GetBatteryTemperature();
68      /**
69      * Return the Current of the battery, in mA.
70      */
71     int32_t GetNowCurrent();
72     /**
73      * Return the RemainEnergy of the battery, in mAh.
74      */
75     int32_t GetRemainEnergy();
76     /**
77      * Return the GetTotalEnergy of the battery, in mAh.
78      */
79     int32_t GetTotalEnergy();
80     /**
81      * Return the level of the battery
82      */
83     BatteryCapacityLevel GetCapacityLevel();
84     /**
85      * Return the remaining charge time
86      */
87     int64_t GetRemainingChargeTime();
88     /**
89      * set charge config
90      */
91     BatteryError SetBatteryConfig(const std::string& sceneName, const std::string& value);
92     /**
93      * get charge config
94      */
95     BatteryError GetBatteryConfig(const std::string& sceneName, std::string& result);
96     /**
97      * is support charge config
98      */
99     BatteryError IsBatteryConfigSupported(const std::string& sceneName, bool& result);
100 
101 #ifndef BATTERYMGR_DEATHRECIPIENT_UNITTEST
102 private:
103 #endif
104     class BatterySrvDeathRecipient : public IRemoteObject::DeathRecipient {
105     public:
BatterySrvDeathRecipient(BatterySrvClient & client)106         explicit BatterySrvDeathRecipient(BatterySrvClient& client) : client_(client) {}
107         virtual ~BatterySrvDeathRecipient() = default;
108         void OnRemoteDied(const wptr<IRemoteObject>& remote);
109     private:
110         DISALLOW_COPY_AND_MOVE(BatterySrvDeathRecipient);
111         BatterySrvClient& client_;
112     };
113 
114     sptr<IBatterySrv> Connect();
115     void ResetProxy(const wptr<IRemoteObject>& remote);
116     sptr<IBatterySrv> proxy_ {nullptr};
117     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
118     std::mutex mutex_;
119 };
120 } // namespace PowerMgr
121 } // namespace OHOS
122 
123 #endif // BATTERY_SRV_CLIENT_H
124