1 /* 2 * Copyright (c) 2022 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 #include "battery_dump.h" 17 18 #include <ctime> 19 #include <iosfwd> 20 #include <cstdio> 21 #include "battery_info.h" 22 #include "battery_log.h" 23 24 namespace OHOS { 25 namespace PowerMgr { 26 namespace { 27 constexpr uint32_t MS_NS = 1000000; 28 constexpr int32_t CAPACITY_DUMP_PARAM_SIZE = 2; 29 constexpr int32_t CAPACITY_LIMIT_MIN = 0; 30 constexpr int32_t CAPACITY_LIMIT_MAX = 100; 31 constexpr int32_t UEVENT_DUMP_PARAM_SIZE = 2; 32 } 33 DumpBatteryHelp(int32_t fd)34 void BatteryDump::DumpBatteryHelp(int32_t fd) 35 { 36 dprintf(fd, "Usage:\n"); 37 dprintf(fd, " -h: dump help\n"); 38 dprintf(fd, " -i: dump battery info\n"); 39 #ifndef BATTERY_USER_VERSION 40 dprintf(fd, " -u: unplug battery charging state\n"); 41 dprintf(fd, " -r: reset battery state\n"); 42 dprintf(fd, " --capacity <capacity>: set battery capacity, the capacity range [0, 100]\n"); 43 dprintf(fd, " --uevent <uevent>: set battery uevent\n"); 44 #endif 45 } 46 DumpCurrentTime(int32_t fd)47 void BatteryDump::DumpCurrentTime(int32_t fd) 48 { 49 timespec curTime = { 0, 0 }; 50 clock_gettime(CLOCK_REALTIME, &curTime); 51 struct tm *timeinfo = localtime(&(curTime.tv_sec)); 52 if (timeinfo == nullptr) { 53 BATTERY_HILOGE(FEATURE_BATT_INFO, "timeinfo cannot be null"); 54 return; 55 } 56 // Add 1900 to the year, add 1 to the month. 57 dprintf(fd, "Current time: %04d-%02d-%02d %02d:%02d:%02d.%03d\n", timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, 58 timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, 59 int32_t { (curTime.tv_nsec / MS_NS) }); 60 } 61 GetBatteryInfo(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)62 bool BatteryDump::GetBatteryInfo(int32_t fd, sptr<BatteryService> &service, const std::vector<std::u16string> &args) 63 { 64 if ((args.empty()) || (args[0].compare(u"-i") != 0)) { 65 BATTERY_HILOGW(FEATURE_BATT_INFO, "args cannot be empty or invalid"); 66 return false; 67 } 68 DumpCurrentTime(fd); 69 int32_t capacity = service->GetCapacity(); 70 dprintf(fd, "capacity: %u \n", capacity); 71 BatteryCapacityLevel batteryLevel = service->GetCapacityLevel(); 72 dprintf(fd, "batteryLevel: %u \n", batteryLevel); 73 BatteryChargeState chargingStatus = service->GetChargingStatus(); 74 dprintf(fd, "chargingStatus: %u \n", chargingStatus); 75 BatteryHealthState healthState = service->GetHealthStatus(); 76 dprintf(fd, "healthState: %u \n", healthState); 77 BatteryPluggedType pluggedType = service->GetPluggedType(); 78 dprintf(fd, "pluggedType: %u \n", pluggedType); 79 int32_t voltage = service->GetVoltage(); 80 dprintf(fd, "voltage: %d \n", voltage); 81 bool present = service->GetPresent(); 82 dprintf(fd, "present: %d \n", present); 83 std::string technology = service->GetTechnology(); 84 dprintf(fd, "technology: %s \n", technology.c_str()); 85 int32_t nowCurrent = service->GetNowCurrent(); 86 dprintf(fd, "nowCurrent: %d \n", nowCurrent); 87 int32_t currentAverage = service->GetCurrentAverage(); 88 dprintf(fd, "currentAverage: %d \n", currentAverage); 89 int32_t totalEnergy = service->GetTotalEnergy(); 90 dprintf(fd, "totalEnergy: %d \n", totalEnergy); 91 int32_t remainEnergy = service->GetRemainEnergy(); 92 dprintf(fd, "remainingEnergy: %d \n", remainEnergy); 93 int64_t remainingChargeTime = service->GetRemainingChargeTime(); 94 dprintf(fd, "remainingChargeTime: %ld \n", remainingChargeTime); 95 int32_t temperature = service->GetBatteryTemperature(); 96 dprintf(fd, "temperature: %d \n", temperature); 97 ChargeType chargeType = service->GetChargeType(); 98 dprintf(fd, "chargeType: %u \n", chargeType); 99 return true; 100 } 101 MockUnplugged(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)102 bool BatteryDump::MockUnplugged(int32_t fd, sptr<BatteryService>& service, const std::vector<std::u16string>& args) 103 { 104 if ((args.empty()) || (args[0].compare(u"-u") != 0)) { 105 BATTERY_HILOGW(FEATURE_CHARGING, "args cannot be empty or invalid"); 106 return false; 107 } 108 #ifndef BATTERY_USER_VERSION 109 service->MockUnplugged(); 110 dprintf(fd, "unplugged battery charging state \n"); 111 #else 112 dprintf(fd, "[Failed] User version is not support \n"); 113 #endif 114 return true; 115 } 116 Reset(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)117 bool BatteryDump::Reset(int32_t fd, sptr<BatteryService>& service, const std::vector<std::u16string>& args) 118 { 119 if ((args.empty()) || (args[0].compare(u"-r") != 0)) { 120 BATTERY_HILOGW(FEATURE_CHARGING, "args cannot be empty or invalid"); 121 return false; 122 } 123 #ifndef BATTERY_USER_VERSION 124 service->Reset(); 125 dprintf(fd, "reset battery state \n"); 126 #else 127 dprintf(fd, "[Failed] User version is not support \n"); 128 #endif 129 return true; 130 } 131 MockCapacity(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)132 bool BatteryDump::MockCapacity(int32_t fd, sptr<BatteryService> &service, const std::vector<std::u16string> &args) 133 { 134 if ((args.empty()) || args.size() != CAPACITY_DUMP_PARAM_SIZE || (args[0].compare(u"--capacity") != 0)) { 135 BATTERY_HILOGW(FEATURE_BATT_INFO, "args cannot be empty or invalid"); 136 return false; 137 } 138 #ifndef BATTERY_USER_VERSION 139 int32_t capacity = 0; 140 std::string capacityStr = Str16ToStr8(args[1]); 141 if (!StrToInt(capacityStr, capacity)) { 142 BATTERY_HILOGW(FEATURE_BATT_INFO, "capacity convert failed"); 143 return false; 144 } 145 if (capacity < CAPACITY_LIMIT_MIN || capacity > CAPACITY_LIMIT_MAX) { 146 dprintf(fd, "capacity out of range\n"); 147 return true; 148 } 149 service->MockCapacity(capacity); 150 dprintf(fd, "battery capacity %d \n", capacity); 151 #else 152 dprintf(fd, "[Failed] User version is not support \n"); 153 #endif 154 return true; 155 } 156 MockUevent(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)157 bool BatteryDump::MockUevent(int32_t fd, sptr<BatteryService> &service, const std::vector<std::u16string> &args) 158 { 159 if ((args.empty()) || args.size() != UEVENT_DUMP_PARAM_SIZE || (args[0].compare(u"--uevent") != 0)) { 160 BATTERY_HILOGW(FEATURE_BATT_INFO, "args cannot be empty or invalid"); 161 return false; 162 } 163 #ifndef BATTERY_USER_VERSION 164 std::string uevent = Str16ToStr8(args[1]); 165 service->MockUevent(uevent); 166 dprintf(fd, "battery uevent %s \n", uevent.c_str()); 167 #else 168 dprintf(fd, "[Failed] User version is not support \n"); 169 #endif 170 return true; 171 } 172 } // namespace PowerMgr 173 } // namespace OHOS 174