• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 HARDWARE_GOOGLE_PIXEL_HEALTH_BATTERYDEFENDER_H
18 #define HARDWARE_GOOGLE_PIXEL_HEALTH_BATTERYDEFENDER_H
19 
20 #include <batteryservice/BatteryService.h>
21 
22 #include <stdbool.h>
23 #include <time.h>
24 #include <string>
25 
26 namespace hardware {
27 namespace google {
28 namespace pixel {
29 namespace health {
30 
31 const uint32_t ONE_MIN_IN_SECONDS = 60;
32 const uint32_t ONE_HOUR_IN_MINUTES = 60;
33 const uint32_t ONE_DAY_IN_HOURS = 24;
34 const uint32_t ONE_DAY_IN_SECONDS = ONE_DAY_IN_HOURS * ONE_HOUR_IN_MINUTES * ONE_MIN_IN_SECONDS;
35 
36 const uint32_t DEFAULT_TIME_TO_ACTIVATE_SECONDS = (4 * ONE_DAY_IN_SECONDS);
37 const uint32_t DEFAULT_TIME_TO_CLEAR_SECONDS = (5 * ONE_MIN_IN_SECONDS);
38 const int DEFAULT_CHARGE_LEVEL_START = 0;
39 const int DEFAULT_CHARGE_LEVEL_STOP = 100;
40 const int DEFAULT_CHARGE_LEVEL_DEFENDER_START = 70;
41 const int DEFAULT_CHARGE_LEVEL_DEFENDER_STOP = 80;
42 const int DEFAULT_CAPACITY_LEVEL = 100;
43 
44 class BatteryDefender {
45   public:
46     // Set default google charger paths - can be overridden for other devices
47     BatteryDefender(const char *pathWirelessPresent = "/sys/class/power_supply/wireless/present",
48                     const char *pathChargeLevelStart =
49                             "/sys/devices/platform/soc/soc:google,charger/charge_start_level",
50                     const char *pathChargeLevelStop =
51                             "/sys/devices/platform/soc/soc:google,charger/charge_stop_level",
52                     const int32_t timeToActivateSecs = DEFAULT_TIME_TO_ACTIVATE_SECONDS,
53                     const int32_t timeToClearTimerSecs = DEFAULT_TIME_TO_CLEAR_SECONDS);
54 
55     // This function shall be called periodically in HealthService
56     void update(struct android::BatteryProperties *props);
57 
58   private:
59     enum state_E {
60         STATE_INIT,
61         STATE_DISABLED,
62         STATE_DISCONNECTED,
63         STATE_CONNECTED,
64         STATE_ACTIVE,
65         STATE_COUNT,
66     };
67     const char *const stateStringMap[STATE_COUNT] = {
68             [STATE_INIT] = "INIT",
69             [STATE_DISABLED] = "DISABLED",
70             [STATE_DISCONNECTED] = "DISCONNECTED",
71             [STATE_CONNECTED] = "CONNECTED",
72             [STATE_ACTIVE] = "ACTIVE",
73     };
74 
75     const char *const kPathWirelessPresent;
76     const char *const kPathChargeLevelStart;
77     const char *const kPathChargeLevelStop;
78     const int32_t kTimeToActivateSecs;
79     const int32_t kTimeToClearTimerSecs;
80 
81     // Sysfs
82     const char *const kPathUSBChargerPresent = "/sys/class/power_supply/usb/present";
83     const char *const kPathPersistChargerPresentTime =
84             "/mnt/vendor/persist/battery/defender_charger_time";
85     const char *const kPathPersistDefenderActiveTime =
86             "/mnt/vendor/persist/battery/defender_active_time";
87 
88     // Properties
89     const char *const kPropChargeLevelVendorStart = "persist.vendor.charge.start.level";
90     const char *const kPropChargeLevelVendorStop = "persist.vendor.charge.stop.level";
91     const char *const kPropBatteryDefenderState = "vendor.battery.defender.state";
92     const char *const kPropBatteryDefenderDisable = "vendor.battery.defender.disable";
93     const char *const kPropBatteryDefenderThreshold = "vendor.battery.defender.threshold";
94     const char *const kPropBootmode = "ro.bootmode";
95     const char *const kPropBatteryDefenderCtrlEnable = "vendor.battery.defender.ctrl.enable";
96     const char *const kPropBatteryDefenderCtrlActivateTime =
97             "vendor.battery.defender.ctrl.trigger_time";
98     const char *const kPropBatteryDefenderCtrlResumeTime =
99             "vendor.battery.defender.ctrl.resume_time";
100     const char *const kPropBatteryDefenderCtrlStartSOC =
101             "vendor.battery.defender.ctrl.recharge_soc_start";
102     const char *const kPropBatteryDefenderCtrlStopSOC =
103             "vendor.battery.defender.ctrl.recharge_soc_stop";
104     const char *const kPropBatteryDefenderCtrlTriggerSOC =
105             "vendor.battery.defender.ctrl.trigger_soc";
106 
107     // Default thresholds
108     const bool kDefaultEnable = true;
109     const int kChargeLevelDefaultStart = DEFAULT_CHARGE_LEVEL_START;
110     const int kChargeLevelDefaultStop = DEFAULT_CHARGE_LEVEL_STOP;
111     const int kChargeLevelDefenderStart = DEFAULT_CHARGE_LEVEL_DEFENDER_START;
112     const int kChargeLevelDefenderStop = DEFAULT_CHARGE_LEVEL_DEFENDER_STOP;
113     const int kChargeHighCapacityLevel = DEFAULT_CAPACITY_LEVEL;
114 
115     // Inputs
116     int64_t mTimeBetweenUpdateCalls = 0;
117     int64_t mTimePreviousSecs;
118     bool mIsUsbPresent = false;
119     bool mIsWirelessPresent = false;
120     bool mIsPowerAvailable = false;
121     bool mIsDefenderDisabled = false;
122     int32_t mTimeToActivateSecsModified;
123 
124     // State
125     state_E mCurrentState = STATE_INIT;
126     int64_t mTimeChargerPresentSecs = 0;
127     int64_t mTimeChargerPresentSecsPrevious = -1;
128     int64_t mTimeChargerNotPresentSecs = 0;
129     int64_t mTimeActiveSecs = 0;
130     int64_t mTimeActiveSecsPrevious = -1;
131     int mChargeLevelStartPrevious = DEFAULT_CHARGE_LEVEL_START;
132     int mChargeLevelStopPrevious = DEFAULT_CHARGE_LEVEL_STOP;
133     bool mHasReachedHighCapacityLevel = false;
134     bool mWasAcOnline = false;
135     bool mWasUsbOnline = true; /* Default; in case neither AC/USB online becomes 1 */
136     bool mIgnoreWirelessFileError = false;
137 
138     // Process state actions
139     void stateMachine_runAction(const state_E state,
140                                 const struct android::BatteryProperties *props);
141 
142     // Check state transitions
143     state_E stateMachine_getNextState(const state_E state);
144 
145     // Process state entry actions
146     void stateMachine_firstAction(const state_E state);
147 
148     void updateDefenderProperties(struct android::BatteryProperties *props);
149     void clearStateData(void);
150     void loadPersistentStorage(void);
151     int64_t getTime(void);
152     int64_t getDeltaTimeSeconds(int64_t *timeStartSecs);
153     int32_t getTimeToActivate(void);
154     void removeLineEndings(std::string *str);
155     int readFileToInt(const char *path, const bool optionalFile = false);
156     bool writeIntToFile(const char *path, const int value);
157     void writeTimeToFile(const char *path, const int value, int64_t *previous);
158     void writeChargeLevelsToFile(const int vendorStart, const int vendorStop);
159     bool isChargePowerAvailable(void);
160     bool isDefaultChargeLevel(const int start, const int stop);
161     bool isBatteryDefenderDisabled(const int vendorStart, const int vendorStop);
162     void addTimeToChargeTimers(void);
163 };
164 
165 }  // namespace health
166 }  // namespace pixel
167 }  // namespace google
168 }  // namespace hardware
169 
170 #endif /* HARDWARE_GOOGLE_PIXEL_HEALTH_BATTERYDEFENDER_H */
171