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