1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef DEVICE_GOOGLE_BONITO_HEALTH_BATTERYRECHARGINGCONTROL_H
18 #define DEVICE_GOOGLE_BONITO_HEALTH_BATTERYRECHARGINGCONTROL_H
19 
20 #include <android-base/file.h>
21 #include <android-base/logging.h>
22 #include <android-base/strings.h>
23 #include <batteryservice/BatteryService.h>
24 #include <math.h>
25 #include <time.h>
26 #include <utils/Timers.h>
27 #include <string>
28 
29 namespace device {
30 namespace google {
31 namespace bonito {
32 namespace health {
33 
34 enum RechargeState {
35     WAIT_EOC,          // Wait for the charge done
36     RECHARGING_CYCLE,  // During the recharging cycle state
37     OVER_LOADING,      // When system power draw is higher than what charges the battery
38     NO_POWER_SOURCE,   // No power source detected
39     INACTIVE           // Not active the recharging state checking
40 };
41 
42 struct sysfsStringEnumMap {
43     const char *s;
44     int val;
45 };
46 
47 /**
48  * updateBatteryProperties is called with the active Fuel Gauge properties values.
49  * In bluecross case, the Maxim FG. The charger SOC level should not be considered
50  * and cannot be used to make any assumption on the SOC we should reporting to the
51  * user.
52  *
53  * Once 100% is reached, the class will track the charger status to detect
54  * when reaching charge termination (EOC), then will report 100% as long as
55  * in Full or Charging states.
56  * The power source might be disconnected at any FG level, recharge_soc_ is used
57  * to keep track of that level to be reached within defined transition time.
58  */
59 class BatteryRechargingControl {
60   public:
61     BatteryRechargingControl();
62     void updateBatteryProperties(struct android::BatteryProperties *props);
63 
64   private:
65     enum RechargeState state_;
66     int64_t start_time_;
67     /* Keeps track of the target level to detect OVER_LOADING or 0 when no target is set */
68     int recharge_soc_;
69 
70     int mapSysfsString(const char *str, struct sysfsStringEnumMap map[]);
71     int getBatteryStatus(const char *status);
72     int RemapSOC(int);
73     int64_t getTime();
74 };
75 
76 }  // namespace health
77 }  // namespace bonito
78 }  // namespace google
79 }  // namespace device
80 
81 #endif
82