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 <aidl/android/hardware/health/HealthInfo.h>
21 #include <batteryservice/BatteryService.h>
22 #include <stdbool.h>
23 #include <time.h>
24 
25 #include <string>
26 
27 namespace hardware {
28 namespace google {
29 namespace pixel {
30 namespace health {
31 
32 const uint32_t ONE_MIN_IN_SECONDS = 60;
33 const uint32_t ONE_HOUR_IN_MINUTES = 60;
34 const uint32_t ONE_DAY_IN_HOURS = 24;
35 const uint32_t ONE_DAY_IN_SECONDS = ONE_DAY_IN_HOURS * ONE_HOUR_IN_MINUTES * ONE_MIN_IN_SECONDS;
36 
37 const uint32_t DEFAULT_TIME_TO_ACTIVATE_SECONDS = (4 * ONE_DAY_IN_SECONDS);
38 const uint32_t DEFAULT_TIME_TO_CLEAR_SECONDS = (5 * ONE_MIN_IN_SECONDS);
39 const int DEFAULT_CHARGE_LEVEL_START = 0;
40 const int DEFAULT_CHARGE_LEVEL_STOP = 100;
41 const int DEFAULT_CHARGE_LEVEL_DEFENDER_START = 70;
42 const int DEFAULT_CHARGE_LEVEL_DEFENDER_STOP = 80;
43 const int DEFAULT_CAPACITY_LEVEL = 100;
44 const int WRITE_DELAY_SECS = 2 * ONE_MIN_IN_SECONDS;
45 
46 const char *const PATH_NOT_SUPPORTED = "";
47 const char *const DEFAULT_START_LEVEL_PATH =
48         "/sys/devices/platform/soc/soc:google,charger/charge_start_level";
49 const char *const DEFAULT_STOP_LEVEL_PATH =
50         "/sys/devices/platform/soc/soc:google,charger/charge_stop_level";
51 
52 class BatteryDefender {
53   public:
54     // Set default google charger paths - can be overridden for other devices
55     BatteryDefender(const std::string pathWirelessPresent = PATH_NOT_SUPPORTED,
56                     const std::string pathChargeLevelStart = DEFAULT_START_LEVEL_PATH,
57                     const std::string pathChargeLevelStop = DEFAULT_STOP_LEVEL_PATH,
58                     const int32_t timeToActivateSecs = DEFAULT_TIME_TO_ACTIVATE_SECONDS,
59                     const int32_t timeToClearTimerSecs = DEFAULT_TIME_TO_CLEAR_SECONDS,
60                     const bool useTypeC = true);
61 
62     // Either of the update() function shall be called periodically in HealthService
63     // Deprecated. Use update(HealthInfo*)
64     void update(struct android::BatteryProperties *props);
65     void update(aidl::android::hardware::health::HealthInfo *health_info);
66 
67     // Set wireless not supported if this is not a device with a wireless charger
68     // (must be checked at runtime)
69     void setWirelessNotSupported(void);
70 
71   private:
72     enum state_E {
73         STATE_INIT,
74         STATE_DISABLED,
75         STATE_DISCONNECTED,
76         STATE_CONNECTED,
77         STATE_ACTIVE,
78         STATE_COUNT,
79     };
80     const char *const kStateStringMap[STATE_COUNT] = {
81             [STATE_INIT] = "INIT",
82             [STATE_DISABLED] = "DISABLED",
83             [STATE_DISCONNECTED] = "DISCONNECTED",
84             [STATE_CONNECTED] = "CONNECTED",
85             [STATE_ACTIVE] = "ACTIVE",
86     };
87 
88     // Constructor
89     std::string mPathWirelessPresent;
90     const std::string kPathChargeLevelStart;
91     const std::string kPathChargeLevelStop;
92     const int32_t kTimeToActivateSecs;
93     const int32_t kTimeToClearTimerSecs;
94     const bool kUseTypeC;
95 
96     // Sysfs
97     const std::string kPathUSBChargerPresent = "/sys/class/power_supply/usb/present";
98     const std::string kPathDOCKChargerPresent = "/sys/class/power_supply/dock/present";
99     const std::string kTypeCPath = "/sys/class/typec/";
100     const std::string kPathPersistChargerPresentTime =
101             "/mnt/vendor/persist/battery/defender_charger_time";
102     const std::string kPathPersistDefenderActiveTime =
103             "/mnt/vendor/persist/battery/defender_active_time";
104     const std::string kPathDockState = "/sys/devices/platform/google,charger/dd_state";
105 
106     // Properties
107     const char *const kPropChargeLevelVendorStart = "persist.vendor.charge.start.level";
108     const char *const kPropChargeLevelVendorStop = "persist.vendor.charge.stop.level";
109     const char *const kPropBatteryDefenderState = "vendor.battery.defender.state";
110     const char *const kPropBatteryDefenderDisable = "vendor.battery.defender.disable";
111     const char *const kPropBatteryDefenderThreshold = "vendor.battery.defender.threshold";
112     const char *const kPropBootmode = "ro.bootmode";
113 
114     const char *const kPropBatteryDefenderCtrlEnable = "vendor.battery.defender.ctrl.enable";
115     const char *const kPropBatteryDefenderCtrlActivateTime =
116             "vendor.battery.defender.ctrl.trigger_time";
117     const char *const kPropBatteryDefenderCtrlResumeTime =
118             "vendor.battery.defender.ctrl.resume_time";
119     const char *const kPropBatteryDefenderCtrlStartSOC =
120             "vendor.battery.defender.ctrl.recharge_soc_start";
121     const char *const kPropBatteryDefenderCtrlStopSOC =
122             "vendor.battery.defender.ctrl.recharge_soc_stop";
123     const char *const kPropBatteryDefenderCtrlTriggerSOC =
124             "vendor.battery.defender.ctrl.trigger_soc";
125     const char *const kPropBatteryDefenderCtrlClear = "vendor.battery.defender.ctrl.clear";
126 
127     // Default thresholds
128     const bool kDefaultEnable = true;
129     const int kChargeLevelDefaultStart = DEFAULT_CHARGE_LEVEL_START;
130     const int kChargeLevelDefaultStop = DEFAULT_CHARGE_LEVEL_STOP;
131     const int kChargeLevelDefenderStart = DEFAULT_CHARGE_LEVEL_DEFENDER_START;
132     const int kChargeLevelDefenderStop = DEFAULT_CHARGE_LEVEL_DEFENDER_STOP;
133     const int kChargeHighCapacityLevel = DEFAULT_CAPACITY_LEVEL;
134     const int kWriteDelaySecs = WRITE_DELAY_SECS;
135 
136     // Inputs
137     int64_t mTimeBetweenUpdateCalls = 0;
138     int64_t mTimePreviousSecs;
139     bool mIsWiredPresent = false;
140     bool mIsWirelessPresent = false;
141     bool mIsDockPresent = false;
142     bool mIsPowerAvailable = false;
143     bool mIsDefenderDisabled = false;
144     bool mIsDockDefendTrigger = false;
145     int32_t mTimeToActivateSecsModified;
146 
147     // State
148     state_E mCurrentState = STATE_INIT;
149     int64_t mTimeChargerPresentSecs = 0;
150     int64_t mTimeChargerPresentSecsPrevious = -1;
151     int64_t mTimeChargerNotPresentSecs = 0;
152     int64_t mTimeActiveSecs = 0;
153     int64_t mTimeActiveSecsPrevious = -1;
154     int mChargeLevelStartPrevious = DEFAULT_CHARGE_LEVEL_START;
155     int mChargeLevelStopPrevious = DEFAULT_CHARGE_LEVEL_STOP;
156     bool mHasReachedHighCapacityLevel = false;
157     bool mWasAcOnline = false;
158     bool mWasUsbOnline = true; /* Default; in case neither AC/USB online becomes 1 */
159 
160     // Process state actions
161     void stateMachine_runAction(const state_E state,
162                                 const aidl::android::hardware::health::HealthInfo &health_info);
163 
164     // Check state transitions
165     state_E stateMachine_getNextState(const state_E state);
166 
167     // Process state entry actions
168     void stateMachine_firstAction(const state_E state);
169 
170     void updateDefenderProperties(aidl::android::hardware::health::HealthInfo *health_info);
171     void clearStateData(void);
172     void loadPersistentStorage(void);
173     int64_t getTime(void);
174     int64_t getDeltaTimeSeconds(int64_t *timeStartSecs);
175     int32_t getTimeToActivate(void);
176     void removeLineEndings(std::string *str);
177     int readFileToInt(const std::string &path, const bool silent = false);
178     bool writeIntToFile(const std::string &path, const int value);
179     void writeTimeToFile(const std::string &path, const int value, int64_t *previous);
180     void writeChargeLevelsToFile(const int vendorStart, const int vendorStop);
181     bool isTypeCSink(const std::string &path);
182     bool isWiredPresent(void);
183     bool isDockPresent(void);
184     bool isChargePowerAvailable(void);
185     bool isDefaultChargeLevel(const int start, const int stop);
186     bool isBatteryDefenderDisabled(const int vendorStart, const int vendorStop);
187     void addTimeToChargeTimers(void);
188     bool isDockDefendTrigger(void);
189 };
190 
191 }  // namespace health
192 }  // namespace pixel
193 }  // namespace google
194 }  // namespace hardware
195 
196 #endif /* HARDWARE_GOOGLE_PIXEL_HEALTH_BATTERYDEFENDER_H */
197