• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
DumpBatteryInfo(sptr<BatteryService> & service,int32_t fd)62 void BatteryDump::DumpBatteryInfo(sptr<BatteryService> &service, int32_t fd)
63 {
64     int32_t capacity = 0;
65     service->GetCapacity(capacity);
66     dprintf(fd, "capacity: %d \n", capacity);
67     uint32_t batteryLevel = static_cast<uint32_t>(BatteryCapacityLevel::LEVEL_NONE);
68     service->GetCapacityLevel(batteryLevel);
69     dprintf(fd, "batteryLevel: %u \n", batteryLevel);
70     uint32_t chargingStatus = static_cast<uint32_t>(BatteryChargeState::CHARGE_STATE_NONE);
71     service->GetChargingStatus(chargingStatus);
72     dprintf(fd, "chargingStatus: %u \n", chargingStatus);
73     uint32_t healthState = static_cast<uint32_t>(BatteryHealthState::HEALTH_STATE_UNKNOWN);
74     service->GetHealthStatus(healthState);
75     dprintf(fd, "healthState: %u \n", healthState);
76     uint32_t pluggedType = static_cast<uint32_t>(BatteryPluggedType::PLUGGED_TYPE_NONE);
77     service->GetPluggedType(pluggedType);
78     dprintf(fd, "pluggedType: %u \n", pluggedType);
79     int32_t voltage = INVALID_BATT_INT_VALUE;
80     service->GetVoltage(voltage);
81     dprintf(fd, "voltage: %d \n", voltage);
82     bool present = false;
83     service->GetPresent(present);
84     dprintf(fd, "present: %d \n", present);
85     std::string technology;
86     service->GetTechnology(technology);
87     dprintf(fd, "technology: %s \n", technology.c_str());
88     int32_t nowCurrent = INVALID_BATT_INT_VALUE;
89     service->GetNowCurrent(nowCurrent);
90     dprintf(fd, "nowCurrent: %d \n", nowCurrent);
91     int32_t currentAverage = INVALID_BATT_INT_VALUE;
92     service->GetCurrentAverage(currentAverage);
93     dprintf(fd, "currentAverage: %d \n", currentAverage);
94     int32_t totalEnergy = INVALID_BATT_INT_VALUE;
95     service->GetTotalEnergy(totalEnergy);
96     dprintf(fd, "totalEnergy: %d \n", totalEnergy);
97     int32_t remainEnergy = INVALID_BATT_INT_VALUE;
98     service->GetRemainEnergy(remainEnergy);
99     dprintf(fd, "remainingEnergy: %d \n", remainEnergy);
100     int64_t remainingChargeTime = INVALID_REMAINING_CHARGE_TIME_VALUE;
101     service->GetRemainingChargeTime(remainingChargeTime);
102     dprintf(fd, "remainingChargeTime: %ld \n", remainingChargeTime);
103     int32_t temperature = INVALID_BATT_INT_VALUE;
104     service->GetBatteryTemperature(temperature);
105     dprintf(fd, "temperature: %d \n", temperature);
106     ChargeType chargeType = service->GetChargeType();
107     dprintf(fd, "chargeType: %u \n", chargeType);
108 }
109 
GetBatteryInfo(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)110 bool BatteryDump::GetBatteryInfo(int32_t fd, sptr<BatteryService> &service, const std::vector<std::u16string> &args)
111 {
112     if ((args.empty()) || (args[0].compare(u"-i") != 0)) {
113         BATTERY_HILOGW(FEATURE_BATT_INFO, "args cannot be empty or invalid");
114         return false;
115     }
116     DumpCurrentTime(fd);
117     DumpBatteryInfo(service, fd);
118     return true;
119 }
120 
MockUnplugged(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)121 bool BatteryDump::MockUnplugged(int32_t fd, sptr<BatteryService>& service, const std::vector<std::u16string>& args)
122 {
123     if ((args.empty()) || (args[0].compare(u"-u") != 0)) {
124         BATTERY_HILOGW(FEATURE_CHARGING, "args cannot be empty or invalid");
125         return false;
126     }
127 #ifndef BATTERY_USER_VERSION
128     service->MockUnplugged();
129     dprintf(fd, "unplugged battery charging state \n");
130 #else
131     dprintf(fd, "[Failed] User version is not support \n");
132 #endif
133     return true;
134 }
135 
Reset(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)136 bool BatteryDump::Reset(int32_t fd, sptr<BatteryService>& service, const std::vector<std::u16string>& args)
137 {
138     if ((args.empty()) || (args[0].compare(u"-r") != 0)) {
139         BATTERY_HILOGW(FEATURE_CHARGING, "args cannot be empty or invalid");
140         return false;
141     }
142 #ifndef BATTERY_USER_VERSION
143     service->Reset();
144     dprintf(fd, "reset battery state \n");
145 #else
146     dprintf(fd, "[Failed] User version is not support \n");
147 #endif
148     return true;
149 }
150 
MockCapacity(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)151 bool BatteryDump::MockCapacity(int32_t fd, sptr<BatteryService> &service, const std::vector<std::u16string> &args)
152 {
153     if ((args.empty()) || args.size() != CAPACITY_DUMP_PARAM_SIZE || (args[0].compare(u"--capacity") != 0)) {
154         BATTERY_HILOGW(FEATURE_BATT_INFO, "args cannot be empty or invalid");
155         return false;
156     }
157 #ifndef BATTERY_USER_VERSION
158     int32_t capacity = 0;
159     std::string capacityStr = Str16ToStr8(args[1]);
160     if (!StrToInt(capacityStr, capacity)) {
161         BATTERY_HILOGW(FEATURE_BATT_INFO, "capacity convert failed");
162         return false;
163     }
164     if (capacity < CAPACITY_LIMIT_MIN || capacity > CAPACITY_LIMIT_MAX) {
165         dprintf(fd, "capacity out of range\n");
166         return true;
167     }
168     service->MockCapacity(capacity);
169     dprintf(fd, "battery capacity %d \n", capacity);
170 #else
171     dprintf(fd, "[Failed] User version is not support \n");
172 #endif
173     return true;
174 }
175 
MockUevent(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)176 bool BatteryDump::MockUevent(int32_t fd, sptr<BatteryService> &service, const std::vector<std::u16string> &args)
177 {
178     if ((args.empty()) || args.size() != UEVENT_DUMP_PARAM_SIZE || (args[0].compare(u"--uevent") != 0)) {
179         BATTERY_HILOGW(FEATURE_BATT_INFO, "args cannot be empty or invalid");
180         return false;
181     }
182 #ifndef BATTERY_USER_VERSION
183     std::string uevent = Str16ToStr8(args[1]);
184     service->MockUevent(uevent);
185     dprintf(fd, "battery uevent %s \n", uevent.c_str());
186 #else
187     dprintf(fd, "[Failed] User version is not support \n");
188 #endif
189     return true;
190 }
191 }  // namespace PowerMgr
192 }  // namespace OHOS
193