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