• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 BATTERY_SRV_BATERY_INFO_H
17 #define BATTERY_SRV_BATERY_INFO_H
18 
19 #include <string>
20 
21 namespace OHOS {
22 namespace PowerMgr {
23 static constexpr int32_t INVALID_BATT_INT_VALUE = -1;
24 static constexpr int32_t INVALID_BATT_TEMP_VALUE = 100;
25 static constexpr int32_t INVALID_BATT_LEVEL_VALUE = -1;
26 static constexpr int64_t INVALID_REMAINING_CHARGE_TIME_VALUE = -1;
27 static constexpr const char* INVALID_STRING_VALUE = "Invalid";
28 static constexpr bool INVALID_BATT_BOOL_VALUE = true;
29 
30 /**
31  * Type for acquire BatteryChargeState.
32  */
33 enum class BatteryChargeState : uint32_t {
34     /**
35      * Battery is discharge.
36      */
37     CHARGE_STATE_NONE,
38 
39     /**
40      * Battery is charging.
41      */
42     CHARGE_STATE_ENABLE,
43 
44     /**
45      * Battery is not charging.
46      */
47     CHARGE_STATE_DISABLE,
48 
49     /**
50      * Battery charge full.
51      */
52     CHARGE_STATE_FULL,
53 
54     /**
55     * The bottom of the enum.
56     */
57     CHARGE_STATE_BUTT
58 };
59 
60 /**
61  * Type for acquire BatteryHealthState.
62  */
63 enum class BatteryHealthState : uint32_t {
64     /**
65      * Health Status: unknown.
66      */
67     HEALTH_STATE_UNKNOWN,
68 
69     /**
70      * Health Status: good.
71      */
72     HEALTH_STATE_GOOD,
73 
74     /**
75      * Health Status: over heat.
76      */
77     HEALTH_STATE_OVERHEAT,
78 
79     /**
80      * Health Status: over voltage.
81      */
82     HEALTH_STATE_OVERVOLTAGE,
83 
84     /**
85      * Health Status: COLD.
86      */
87     HEALTH_STATE_COLD,
88 
89     /**
90      * Health Status: Dead.
91      */
92     HEALTH_STATE_DEAD,
93 
94     /**
95     * The bottom of the enum.
96     */
97     HEALTH_STATE_BUTT
98 };
99 
100 /**
101  * Type for acquire BatteryPluggedType.
102  */
103 enum class BatteryPluggedType : uint32_t {
104     /**
105      * Power source is unplugged.
106      */
107     PLUGGED_TYPE_NONE,
108 
109     /**
110      * Power source is an AC charger.
111      */
112     PLUGGED_TYPE_AC,
113 
114     /**
115      * Power source is a USB DC charger.
116      */
117     PLUGGED_TYPE_USB,
118 
119     /**
120      * Power source is wireless charger.
121      */
122     PLUGGED_TYPE_WIRELESS,
123 
124     /**
125     * The bottom of the enum.
126     */
127     PLUGGED_TYPE_BUTT
128 };
129 
130 /**
131  * Battery capacity level of a device
132  */
133 enum class BatteryCapacityLevel : uint32_t {
134     /**
135      * The battery is in unknown capacity level
136      */
137     LEVEL_NONE,
138 
139     /**
140      * The battery is in full capacity level
141      */
142     LEVEL_FULL,
143 
144     /**
145      * The battery is in high capacity level
146      */
147     LEVEL_HIGH,
148 
149     /**
150      * The battery is in normal capacity level
151      */
152     LEVEL_NORMAL,
153 
154     /**
155      * The battery is in low capacity level
156      */
157     LEVEL_LOW,
158 
159     /**
160      * The battery is in warning low capacity level
161      */
162     LEVEL_WARNING,
163 
164     /**
165      * The battery is in critical low capacity level
166      */
167     LEVEL_CRITICAL,
168 
169     /**
170      * The battery is in shutdown low capacity level
171      */
172     LEVEL_SHUTDOWN,
173 
174     /**
175     * Reserved
176     */
177     LEVEL_RESERVED
178 };
179 
180 class BatteryInfo {
181 public:
182     enum {
183         COMMON_EVENT_CODE_CAPACITY = 0,
184         COMMON_EVENT_CODE_VOLTAGE = 1,
185         COMMON_EVENT_CODE_TEMPERATURE = 2,
186         COMMON_EVENT_CODE_HEALTH_STATE = 3,
187         COMMON_EVENT_CODE_PLUGGED_TYPE = 4,
188         COMMON_EVENT_CODE_PLUGGED_MAX_CURRENT = 5,
189         COMMON_EVENT_CODE_PLUGGED_MAX_VOLTAGE = 6,
190         COMMON_EVENT_CODE_CHARGE_STATE = 7,
191         COMMON_EVENT_CODE_CHARGE_COUNTER = 8,
192         COMMON_EVENT_CODE_PRESENT = 9,
193         COMMON_EVENT_CODE_TECHNOLOGY = 10,
194         COMMON_EVENT_CODE_CAPACITY_LEVEL = 11,
195         COMMON_EVENT_CODE_PLUGGED_NOW_CURRENT = 12,
196     };
197     BatteryInfo() = default;
198     ~BatteryInfo() = default;
199 
SetCapacity(const int32_t capacity)200     void SetCapacity(const int32_t capacity)
201     {
202         capacity_ = capacity;
203     }
204 
SetVoltage(const int32_t voltage)205     void SetVoltage(const int32_t voltage)
206     {
207         voltage_ = voltage;
208     }
209 
SetTemperature(const int32_t temperature)210     void SetTemperature(const int32_t temperature)
211     {
212         temperature_ = temperature;
213     }
214 
SetHealthState(const BatteryHealthState healthState)215     void SetHealthState(const BatteryHealthState healthState)
216     {
217         healthState_ = healthState;
218     }
219 
SetPluggedType(const BatteryPluggedType pluggedType)220     void SetPluggedType(const BatteryPluggedType pluggedType)
221     {
222         pluggedType_ = pluggedType;
223     }
224 
SetPluggedMaxCurrent(const int32_t maxCurrent)225     void SetPluggedMaxCurrent(const int32_t maxCurrent)
226     {
227         pluggedMaxCurrent_ = maxCurrent;
228     }
229 
SetPluggedMaxVoltage(const int32_t maxVoltage)230     void SetPluggedMaxVoltage(const int32_t maxVoltage)
231     {
232         pluggedMaxVoltage_ = maxVoltage;
233     }
234 
SetChargeState(const BatteryChargeState chargeState)235     void SetChargeState(const BatteryChargeState chargeState)
236     {
237         chargeState_ = chargeState;
238     }
239 
SetChargeCounter(const int32_t chargeCounter)240     void SetChargeCounter(const int32_t chargeCounter)
241     {
242         chargeCounter_ = chargeCounter;
243     }
244 
SetTotalEnergy(const int32_t totalEnergy)245     void SetTotalEnergy(const int32_t totalEnergy)
246     {
247         totalEnergy_ = totalEnergy;
248     }
249 
SetCurAverage(const int32_t curAverage)250     void SetCurAverage(const int32_t curAverage)
251     {
252         curAverage_ = curAverage;
253     }
254 
SetNowCurrent(const int32_t nowCurr)255     void SetNowCurrent(const int32_t nowCurr)
256     {
257         nowCurr_ = nowCurr;
258     }
259 
SetRemainEnergy(const int32_t remainEnergy)260     void SetRemainEnergy(const int32_t remainEnergy)
261     {
262         remainEnergy_ = remainEnergy;
263     }
264 
SetPresent(const bool present)265     void SetPresent(const bool present)
266     {
267         present_ = present;
268     }
269 
SetTechnology(const std::string & technology)270     void SetTechnology(const std::string& technology)
271     {
272         technology_ = technology;
273     }
274 
GetCapacity()275     const int32_t& GetCapacity() const
276     {
277         return capacity_;
278     }
279 
GetVoltage()280     const int32_t& GetVoltage() const
281     {
282         return voltage_;
283     }
284 
GetTemperature()285     const int32_t& GetTemperature() const
286     {
287         return temperature_;
288     }
289 
GetHealthState()290     BatteryHealthState GetHealthState() const
291     {
292         return healthState_;
293     }
294 
GetPluggedType()295     BatteryPluggedType GetPluggedType() const
296     {
297         return pluggedType_;
298     }
299 
GetPluggedMaxCurrent()300     const int32_t& GetPluggedMaxCurrent() const
301     {
302         return pluggedMaxCurrent_;
303     }
304 
GetPluggedMaxVoltage()305     const int32_t& GetPluggedMaxVoltage() const
306     {
307         return pluggedMaxVoltage_;
308     }
309 
GetChargeState()310     BatteryChargeState GetChargeState() const
311     {
312         return chargeState_;
313     }
314 
GetTotalEnergy()315     const int32_t& GetTotalEnergy() const
316     {
317         return totalEnergy_;
318     }
319 
GetCurAverage()320     const int32_t& GetCurAverage() const
321     {
322         return curAverage_;
323     }
324 
GetNowCurrent()325     const int32_t& GetNowCurrent() const
326     {
327         return nowCurr_;
328     }
329 
GetRemainEnergy()330     const int32_t& GetRemainEnergy() const
331     {
332         return remainEnergy_;
333     }
334 
GetChargeCounter()335     const int32_t& GetChargeCounter() const
336     {
337         return chargeCounter_;
338     }
339 
IsPresent()340     bool IsPresent() const
341     {
342         return present_;
343     }
344 
GetTechnology()345     const std::string& GetTechnology() const
346     {
347         return technology_;
348     }
349 
350     bool operator==(const BatteryInfo& info)
351     {
352         return (present_ == info.IsPresent()) &&
353             (capacity_ == info.GetCapacity()) &&
354             (voltage_ == info.GetVoltage()) &&
355             (temperature_ == info.GetTemperature()) &&
356             (totalEnergy_ == info.GetTotalEnergy()) &&
357             (curAverage_ == info.GetCurAverage()) &&
358             (nowCurr_ == info.GetNowCurrent()) &&
359             (pluggedMaxCurrent_ == info.GetPluggedMaxCurrent()) &&
360             (pluggedMaxVoltage_ == info.GetPluggedMaxVoltage()) &&
361             (chargeCounter_ == info.GetChargeCounter()) &&
362             (healthState_ == info.GetHealthState()) &&
363             (pluggedType_ == info.GetPluggedType()) &&
364             (remainEnergy_ == info.GetRemainEnergy()) &&
365             (chargeState_ == info.GetChargeState()) &&
366             (technology_ == info.GetTechnology());
367     }
368 
369     bool operator!=(const BatteryInfo& info)
370     {
371         return !(*this == info);
372     }
373 
374     // Used by both napi and native
375     static constexpr const char* COMMON_EVENT_KEY_CAPACITY = "soc";
376     static constexpr const char* COMMON_EVENT_KEY_CHARGE_STATE = "chargeState";
377     static constexpr const char* COMMON_EVENT_KEY_HEALTH_STATE = "healthState";
378     static constexpr const char* COMMON_EVENT_KEY_PLUGGED_TYPE = "pluggedType";
379     static constexpr const char* COMMON_EVENT_KEY_VOLTAGE = "voltage";
380     static constexpr const char* COMMON_EVENT_KEY_TECHNOLOGY = "technology";
381     static constexpr const char* COMMON_EVENT_KEY_TEMPERATURE = "temperature";
382     static constexpr const char* COMMON_EVENT_KEY_PRESENT = "present";
383     static constexpr const char* COMMON_EVENT_KEY_CAPACITY_LEVEL = "capacityLevel";
384 
385     // Used by native only
386     static constexpr const char* COMMON_EVENT_KEY_PLUGGED_MAX_CURRENT = "maxCurrent";
387     static constexpr const char* COMMON_EVENT_KEY_PLUGGED_MAX_VOLTAGE = "maxVoltage";
388     static constexpr const char* COMMON_EVENT_KEY_PLUGGED_NOW_CURRENT = "nowCurrent";
389     static constexpr const char* COMMON_EVENT_KEY_CHARGE_COUNTER = "chargeCounter";
390 
391 private:
392     bool present_ = INVALID_BATT_BOOL_VALUE;
393     int32_t capacity_ = INVALID_BATT_INT_VALUE;
394     int32_t voltage_ = INVALID_BATT_INT_VALUE;
395     int32_t temperature_ = INVALID_BATT_TEMP_VALUE;
396     int32_t totalEnergy_ = INVALID_BATT_INT_VALUE;
397     int32_t curAverage_ = INVALID_BATT_INT_VALUE;
398     int32_t nowCurr_ = INVALID_BATT_INT_VALUE;
399     int32_t pluggedMaxCurrent_ = INVALID_BATT_INT_VALUE;
400     int32_t pluggedMaxVoltage_ = INVALID_BATT_INT_VALUE;
401     int32_t chargeCounter_ = INVALID_BATT_INT_VALUE;
402     int32_t remainEnergy_ = INVALID_BATT_INT_VALUE;
403     BatteryHealthState healthState_ = BatteryHealthState::HEALTH_STATE_BUTT;
404     BatteryPluggedType pluggedType_ = BatteryPluggedType::PLUGGED_TYPE_BUTT;
405     BatteryChargeState chargeState_ = BatteryChargeState::CHARGE_STATE_BUTT;
406     std::string technology_ = INVALID_STRING_VALUE;
407 };
408 } // namespace PowerMgr
409 } // namespace OHOS
410 
411 #endif // BATTERY_SRV_BATERY_INFO_H
412