• 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 }
29 
DumpBatteryHelp(int32_t fd,const std::vector<std::u16string> & args)30 bool BatteryDump::DumpBatteryHelp(int32_t fd, const std::vector<std::u16string> &args)
31 {
32     if ((args.empty()) || (args[0].compare(u"-h") != 0)) {
33         BATTERY_HILOGE(FEATURE_BATT_INFO, "args cannot be empty or invalid");
34         return false;
35     }
36     DumpHelp(fd);
37     return true;
38 }
39 
DumpHelp(int32_t fd)40 void BatteryDump::DumpHelp(int32_t fd)
41 {
42     dprintf(fd, "Usage:\n");
43     dprintf(fd, "      -h: dump help\n");
44     dprintf(fd, "      -i: dump battery info\n");
45     dprintf(fd, "      -d: show low power diaolog\n");
46     dprintf(fd, "      -u: unplug battery charging state\n");
47     dprintf(fd, "      -r: reset battery charging state\n");
48 }
49 
DumpCurrentTime(int32_t fd)50 void BatteryDump::DumpCurrentTime(int32_t fd)
51 {
52     timespec curTime = { 0, 0 };
53     clock_gettime(CLOCK_REALTIME, &curTime);
54     struct tm *timeinfo = localtime(&(curTime.tv_sec));
55     if (timeinfo == nullptr) {
56         BATTERY_HILOGE(FEATURE_BATT_INFO, "timeinfo cannot be null");
57         return;
58     }
59     // Add 1900 to the year, add 1 to the month.
60     dprintf(fd, "Current time: %04d-%02d-%02d %02d:%02d:%02d.%03d\n", timeinfo->tm_year + 1900, timeinfo->tm_mon + 1,
61             timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
62             int32_t { (curTime.tv_nsec / MS_NS) });
63 }
64 
GetBatteryInfo(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)65 bool BatteryDump::GetBatteryInfo(int32_t fd, sptr<BatteryService> &service, const std::vector<std::u16string> &args)
66 {
67     if ((args.empty()) || (args[0].compare(u"-i") != 0)) {
68         BATTERY_HILOGE(FEATURE_BATT_INFO, "args cannot be empty or invalid");
69         return false;
70     }
71     DumpCurrentTime(fd);
72     int32_t capacity = service->GetCapacity();
73     dprintf(fd, "capacity: %u \n", capacity);
74     BatteryCapacityLevel batteryLevel = service->GetCapacityLevel();
75     dprintf(fd, "batteryLevel: %u \n", batteryLevel);
76     BatteryChargeState chargingStatus = service->GetChargingStatus();
77     dprintf(fd, "chargingStatus: %u \n", chargingStatus);
78     BatteryHealthState healthState = service->GetHealthStatus();
79     dprintf(fd, "healthState: %u \n", healthState);
80     BatteryPluggedType pluggedType = service->GetPluggedType();
81     dprintf(fd, "pluggedType: %u \n", pluggedType);
82     int32_t voltage = service->GetVoltage();
83     dprintf(fd, "voltage: %d \n", voltage);
84     bool present = service->GetPresent();
85     dprintf(fd, "present: %d \n", present);
86     std::string technology = service->GetTechnology();
87     dprintf(fd, "technology: %s \n", technology.c_str());
88     int32_t nowCurrent = service->GetNowCurrent();
89     dprintf(fd, "nowCurrent: %d \n", nowCurrent);
90     int32_t currentAverage = service->GetCurrentAverage();
91     dprintf(fd, "currentAverage: %d \n", currentAverage);
92     int32_t totalEnergy = service->GetTotalEnergy();
93     dprintf(fd, "totalEnergy: %d \n", totalEnergy);
94     int32_t remainEnergy = service->GetRemainEnergy();
95     dprintf(fd, "remainingEnergy: %d \n", remainEnergy);
96     int64_t remainingChargeTime = service->GetRemainingChargeTime();
97     dprintf(fd, "remainingChargeTime: %ld \n", remainingChargeTime);
98     int32_t temperature = service->GetBatteryTemperature();
99     dprintf(fd, "temperature: %d \n", temperature);
100     return true;
101 }
102 
MockUnplugged(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)103 bool BatteryDump::MockUnplugged(int32_t fd, sptr<BatteryService>& service, const std::vector<std::u16string>& args)
104 {
105     if ((args.empty()) || (args[0].compare(u"-u") != 0)) {
106         BATTERY_HILOGE(FEATURE_CHARGING, "args cannot be empty or invalid");
107         return false;
108     }
109     service->MockUnplugged(true);
110     dprintf(fd, "unplugged battery charging state \n");
111     return true;
112 }
113 
ResetPlugged(int32_t fd,sptr<BatteryService> & service,const std::vector<std::u16string> & args)114 bool BatteryDump::ResetPlugged(int32_t fd, sptr<BatteryService>& service, const std::vector<std::u16string>& args)
115 {
116     if ((args.empty()) || (args[0].compare(u"-r") != 0)) {
117         BATTERY_HILOGE(FEATURE_CHARGING, "args cannot be empty or invalid");
118         return false;
119     }
120     service->MockUnplugged(false);
121     dprintf(fd, "reset battery charging state \n");
122     return true;
123 }
124 }  // namespace PowerMgr
125 }  // namespace OHOS
126