• 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 /**
181  * Charge type of the connected charger.
182  */
183 enum class ChargeType : uint32_t {
184     /**
185      * Unknown charge type
186      */
187     NONE,
188     /**
189      * Wired normal charge type
190      */
191     WIRED_NORMAL,
192     /**
193      * Wired quick charge type
194      */
195     WIRED_QUICK,
196     /**
197      * Wired super quick charge type
198      */
199     WIRED_SUPER_QUICK,
200     /**
201      * Wireless normal charge type
202      */
203     WIRELESS_NORMAL,
204     /**
205      * Wireless quick charge type
206      */
207     WIRELESS_QUICK,
208     /**
209      * Wireless super quick charge type
210      */
211     WIRELESS_SUPER_QUICK,
212 };
213 
214 class BatteryInfo {
215 public:
216     enum {
217         COMMON_EVENT_CODE_CAPACITY = 0,
218         COMMON_EVENT_CODE_VOLTAGE = 1,
219         COMMON_EVENT_CODE_TEMPERATURE = 2,
220         COMMON_EVENT_CODE_HEALTH_STATE = 3,
221         COMMON_EVENT_CODE_PLUGGED_TYPE = 4,
222         COMMON_EVENT_CODE_PLUGGED_MAX_CURRENT = 5,
223         COMMON_EVENT_CODE_PLUGGED_MAX_VOLTAGE = 6,
224         COMMON_EVENT_CODE_CHARGE_STATE = 7,
225         COMMON_EVENT_CODE_CHARGE_COUNTER = 8,
226         COMMON_EVENT_CODE_PRESENT = 9,
227         COMMON_EVENT_CODE_TECHNOLOGY = 10,
228         COMMON_EVENT_CODE_CAPACITY_LEVEL = 11,
229         COMMON_EVENT_CODE_PLUGGED_NOW_CURRENT = 12,
230     };
231     BatteryInfo() = default;
232     ~BatteryInfo() = default;
233 
SetCapacity(const int32_t capacity)234     void SetCapacity(const int32_t capacity)
235     {
236         capacity_ = capacity;
237     }
238 
SetVoltage(const int32_t voltage)239     void SetVoltage(const int32_t voltage)
240     {
241         voltage_ = voltage;
242     }
243 
SetTemperature(const int32_t temperature)244     void SetTemperature(const int32_t temperature)
245     {
246         temperature_ = temperature;
247     }
248 
SetHealthState(const BatteryHealthState healthState)249     void SetHealthState(const BatteryHealthState healthState)
250     {
251         healthState_ = healthState;
252     }
253 
SetPluggedType(const BatteryPluggedType pluggedType)254     void SetPluggedType(const BatteryPluggedType pluggedType)
255     {
256         pluggedType_ = pluggedType;
257     }
258 
SetPluggedMaxCurrent(const int32_t maxCurrent)259     void SetPluggedMaxCurrent(const int32_t maxCurrent)
260     {
261         pluggedMaxCurrent_ = maxCurrent;
262     }
263 
SetPluggedMaxVoltage(const int32_t maxVoltage)264     void SetPluggedMaxVoltage(const int32_t maxVoltage)
265     {
266         pluggedMaxVoltage_ = maxVoltage;
267     }
268 
SetChargeState(const BatteryChargeState chargeState)269     void SetChargeState(const BatteryChargeState chargeState)
270     {
271         chargeState_ = chargeState;
272     }
273 
SetChargeCounter(const int32_t chargeCounter)274     void SetChargeCounter(const int32_t chargeCounter)
275     {
276         chargeCounter_ = chargeCounter;
277     }
278 
SetTotalEnergy(const int32_t totalEnergy)279     void SetTotalEnergy(const int32_t totalEnergy)
280     {
281         totalEnergy_ = totalEnergy;
282     }
283 
SetCurAverage(const int32_t curAverage)284     void SetCurAverage(const int32_t curAverage)
285     {
286         curAverage_ = curAverage;
287     }
288 
SetNowCurrent(const int32_t nowCurr)289     void SetNowCurrent(const int32_t nowCurr)
290     {
291         nowCurr_ = nowCurr;
292     }
293 
SetRemainEnergy(const int32_t remainEnergy)294     void SetRemainEnergy(const int32_t remainEnergy)
295     {
296         remainEnergy_ = remainEnergy;
297     }
298 
SetPresent(const bool present)299     void SetPresent(const bool present)
300     {
301         present_ = present;
302     }
303 
SetTechnology(const std::string & technology)304     void SetTechnology(const std::string& technology)
305     {
306         technology_ = technology;
307     }
308 
SetChargeType(const ChargeType chargeType)309     void SetChargeType(const ChargeType chargeType)
310     {
311         chargeType_ = chargeType;
312     }
313 
GetCapacity()314     const int32_t& GetCapacity() const
315     {
316         return capacity_;
317     }
318 
GetVoltage()319     const int32_t& GetVoltage() const
320     {
321         return voltage_;
322     }
323 
GetTemperature()324     const int32_t& GetTemperature() const
325     {
326         return temperature_;
327     }
328 
GetHealthState()329     BatteryHealthState GetHealthState() const
330     {
331         return healthState_;
332     }
333 
GetPluggedType()334     BatteryPluggedType GetPluggedType() const
335     {
336         return pluggedType_;
337     }
338 
GetPluggedMaxCurrent()339     const int32_t& GetPluggedMaxCurrent() const
340     {
341         return pluggedMaxCurrent_;
342     }
343 
GetPluggedMaxVoltage()344     const int32_t& GetPluggedMaxVoltage() const
345     {
346         return pluggedMaxVoltage_;
347     }
348 
GetChargeState()349     BatteryChargeState GetChargeState() const
350     {
351         return chargeState_;
352     }
353 
GetTotalEnergy()354     const int32_t& GetTotalEnergy() const
355     {
356         return totalEnergy_;
357     }
358 
GetCurAverage()359     const int32_t& GetCurAverage() const
360     {
361         return curAverage_;
362     }
363 
GetNowCurrent()364     const int32_t& GetNowCurrent() const
365     {
366         return nowCurr_;
367     }
368 
GetRemainEnergy()369     const int32_t& GetRemainEnergy() const
370     {
371         return remainEnergy_;
372     }
373 
GetChargeCounter()374     const int32_t& GetChargeCounter() const
375     {
376         return chargeCounter_;
377     }
378 
IsPresent()379     bool IsPresent() const
380     {
381         return present_;
382     }
383 
GetTechnology()384     const std::string& GetTechnology() const
385     {
386         return technology_;
387     }
388 
GetChargeType()389     ChargeType GetChargeType() const
390     {
391         return chargeType_;
392     }
393 
394     bool operator==(const BatteryInfo& info)
395     {
396         bool eq = (present_ == info.IsPresent()) &&
397             (capacity_ == info.GetCapacity()) &&
398             (voltage_ == info.GetVoltage()) &&
399             (temperature_ == info.GetTemperature()) &&
400             (totalEnergy_ == info.GetTotalEnergy()) &&
401             (curAverage_ == info.GetCurAverage()) &&
402             (nowCurr_ == info.GetNowCurrent()) &&
403             (pluggedMaxCurrent_ == info.GetPluggedMaxCurrent()) &&
404             (pluggedMaxVoltage_ == info.GetPluggedMaxVoltage()) &&
405             (chargeCounter_ == info.GetChargeCounter()) &&
406             (healthState_ == info.GetHealthState()) &&
407             (pluggedType_ == info.GetPluggedType()) &&
408             (remainEnergy_ == info.GetRemainEnergy()) &&
409             (chargeState_ == info.GetChargeState()) &&
410             (technology_ == info.GetTechnology()) &&
411             (chargeType_ == info.GetChargeType());
412         return eq;
413     }
414 
415     bool operator!=(const BatteryInfo& info)
416     {
417         return !(*this == info);
418     }
419 
420     // Used by both napi and native
421     static constexpr const char* COMMON_EVENT_KEY_CAPACITY = "soc";
422     static constexpr const char* COMMON_EVENT_KEY_CHARGE_STATE = "chargeState";
423     static constexpr const char* COMMON_EVENT_KEY_HEALTH_STATE = "healthState";
424     static constexpr const char* COMMON_EVENT_KEY_PLUGGED_TYPE = "pluggedType";
425     static constexpr const char* COMMON_EVENT_KEY_VOLTAGE = "voltage";
426     static constexpr const char* COMMON_EVENT_KEY_TECHNOLOGY = "technology";
427     static constexpr const char* COMMON_EVENT_KEY_TEMPERATURE = "temperature";
428     static constexpr const char* COMMON_EVENT_KEY_PRESENT = "present";
429     static constexpr const char* COMMON_EVENT_KEY_CAPACITY_LEVEL = "capacityLevel";
430 
431     // Used by native only
432     static constexpr const char* COMMON_EVENT_KEY_PLUGGED_MAX_CURRENT = "maxCurrent";
433     static constexpr const char* COMMON_EVENT_KEY_PLUGGED_MAX_VOLTAGE = "maxVoltage";
434     static constexpr const char* COMMON_EVENT_KEY_PLUGGED_NOW_CURRENT = "nowCurrent";
435     static constexpr const char* COMMON_EVENT_KEY_CHARGE_COUNTER = "chargeCounter";
436 
437 private:
438     bool present_ = INVALID_BATT_BOOL_VALUE;
439     int32_t capacity_ = INVALID_BATT_INT_VALUE;
440     int32_t voltage_ = INVALID_BATT_INT_VALUE;
441     int32_t temperature_ = INVALID_BATT_TEMP_VALUE;
442     int32_t totalEnergy_ = INVALID_BATT_INT_VALUE;
443     int32_t curAverage_ = INVALID_BATT_INT_VALUE;
444     int32_t nowCurr_ = INVALID_BATT_INT_VALUE;
445     int32_t pluggedMaxCurrent_ = INVALID_BATT_INT_VALUE;
446     int32_t pluggedMaxVoltage_ = INVALID_BATT_INT_VALUE;
447     int32_t chargeCounter_ = INVALID_BATT_INT_VALUE;
448     int32_t remainEnergy_ = INVALID_BATT_INT_VALUE;
449     ChargeType chargeType_ = ChargeType::NONE;
450     BatteryHealthState healthState_ = BatteryHealthState::HEALTH_STATE_BUTT;
451     BatteryPluggedType pluggedType_ = BatteryPluggedType::PLUGGED_TYPE_BUTT;
452     BatteryChargeState chargeState_ = BatteryChargeState::CHARGE_STATE_BUTT;
453     std::string technology_ = INVALID_STRING_VALUE;
454 };
455 } // namespace PowerMgr
456 } // namespace OHOS
457 
458 #endif // BATTERY_SRV_BATERY_INFO_H
459