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