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 namespace OHOS {
19 namespace PowerMgr {
20 namespace {
21 constexpr uint32_t MS_NS = 1000000;
22 }
23
DumpBatteryHelp(int32_t fd,const std::vector<std::u16string> & args)24 bool BatteryDump::DumpBatteryHelp(int32_t fd, const std::vector<std::u16string> &args)
25 {
26 if ((args.empty()) || (args[0].compare(u"-h") != 0)) {
27 BATTERY_HILOGE(FEATURE_BATT_INFO, "args cannot be empty or invalid");
28 return false;
29 }
30 DumpHelp(fd);
31 return true;
32 }
33
DumpHelp(int32_t fd)34 void BatteryDump::DumpHelp(int32_t fd)
35 {
36 dprintf(fd, "Usage:\n");
37 dprintf(fd, " -h: dump help\n");
38 dprintf(fd, " -i: get battery info\n");
39 }
40
DumpCurrentTime(int32_t fd)41 void BatteryDump::DumpCurrentTime(int32_t fd)
42 {
43 timespec curTime = { 0, 0 };
44 clock_gettime(CLOCK_REALTIME, &curTime);
45 struct tm *timeinfo = localtime(&(curTime.tv_sec));
46 if (timeinfo == nullptr) {
47 BATTERY_HILOGE(FEATURE_BATT_INFO, "timeinfo cannot be null");
48 return;
49 }
50 dprintf(fd, "Current time: %04d-%02d-%02d %02d:%02d:%02d.%03d\n", timeinfo->tm_year, timeinfo->tm_mon,
51 timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
52 int32_t { (curTime.tv_nsec / MS_NS) });
53 }
54
GetBatteryInfo(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)55 bool BatteryDump::GetBatteryInfo(int32_t fd, sptr<BatteryService> &service, const std::vector<std::u16string> &args)
56 {
57 if ((args.empty()) || (args[0].compare(u"-i") != 0)) {
58 BATTERY_HILOGE(FEATURE_BATT_INFO, "args cannot be empty or invalid");
59 return false;
60 }
61 DumpCurrentTime(fd);
62 int32_t capacity = service->GetCapacity();
63 dprintf(fd, "capacity: %d \n", capacity);
64 BatteryChargeState chargingStatus = service->GetChargingStatus();
65 dprintf(fd, "chargingStatus: %d \n", chargingStatus);
66 BatteryHealthState healthState = service->GetHealthStatus();
67 dprintf(fd, "healthState: %d \n", healthState);
68 BatteryPluggedType pluggedType = service->GetPluggedType();
69 dprintf(fd, "pluggedType: %d \n", pluggedType);
70 int32_t voltage = service->GetVoltage();
71 dprintf(fd, "voltage: %d \n", voltage);
72 bool present = service->GetPresent();
73 dprintf(fd, "present: %d \n", present);
74 std::string technology = service->GetTechnology();
75 dprintf(fd, "technology: %s \n", technology.c_str());
76 int32_t temperature = service->GetBatteryTemperature();
77 dprintf(fd, "temperature: %d \n", temperature);
78
79 return true;
80 }
81 } // namespace PowerMgr
82 } // namespace OHOS
83