• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_stats_stub.h"
17 
18 #include "battery_stats_ipc_interface_code.h"
19 #include "battery_stats_info.h"
20 #include "errors.h"
21 #include "ipc_object_stub.h"
22 #include "stats_common.h"
23 #include "stats_errors.h"
24 #include "stats_log.h"
25 #include "stats_utils.h"
26 #include "xcollie/xcollie.h"
27 #include "xcollie/xcollie_define.h"
28 
29 namespace OHOS {
30 namespace PowerMgr {
31 namespace {
32 constexpr uint32_t PARAM_MAX_NUM = 10;
33 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int BatteryStatsStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36     STATS_HILOGD(COMP_SVC, "Remote request, cmd = %{public}d, flags = %{public}d",
37         code, option.GetFlags());
38     const uint32_t DFX_DELAY_MS = 10000;
39     int id = HiviewDFX::XCollie::GetInstance().SetTimer("BatteryStatsStub", DFX_DELAY_MS, nullptr, nullptr,
40         HiviewDFX::XCOLLIE_FLAG_NOOP);
41 
42     std::u16string descriptor = BatteryStatsStub::GetDescriptor();
43     std::u16string remoteDescriptor = data.ReadInterfaceToken();
44     if (descriptor != remoteDescriptor) {
45         STATS_HILOGE(COMP_SVC, "Remote request failed, descriptor is not matched");
46         return E_STATS_GET_SERVICE_FAILED;
47     }
48     int ret = ChooseCodeStub(code, data, reply, option);
49     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
50     return ret;
51 }
52 
ChooseCodeStub(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)53 int32_t BatteryStatsStub::ChooseCodeStub(uint32_t code, MessageParcel &data, MessageParcel &reply,
54     MessageOption &option)
55 {
56     int32_t ret;
57     switch (code) {
58         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_GET): {
59             ret = GetBatteryStatsStub(reply);
60             break;
61         }
62         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_GETTIME): {
63             ret = GetTotalTimeSecondStub(data, reply);
64             break;
65         }
66         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_GETDATA): {
67             ret = GetTotalDataBytesStub(data, reply);
68             break;
69         }
70         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_GETAPPMAH): {
71             ret = GetAppStatsMahStub(data, reply);
72             break;
73         }
74         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_GETAPPPER): {
75             ret = GetAppStatsPercentStub(data, reply);
76             break;
77         }
78         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_GETPARTMAH): {
79             ret = GetPartStatsMahStub(data, reply);
80             break;
81         }
82         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_GETPARTPER): {
83             ret = GetPartStatsPercentStub(data, reply);
84             break;
85         }
86         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_RESET): {
87             ret = ResetStub();
88             break;
89         }
90         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_SETONBATT): {
91             ret = SetOnBatteryStub(data);
92             break;
93         }
94         case static_cast<uint32_t>(PowerMgr::BatteryStatsInterfaceCode::BATTERY_STATS_DUMP): {
95             ret = ShellDumpStub(data, reply);
96             break;
97         }
98         default: {
99             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
100             break;
101         }
102     }
103     return ret;
104 }
105 
GetBatteryStatsStub(MessageParcel & reply)106 int32_t BatteryStatsStub::GetBatteryStatsStub(MessageParcel& reply)
107 {
108     STATS_HILOGD(COMP_SVC, "Enter");
109     BatteryStatsInfoList ret = GetBatteryStats();
110 
111     uint32_t size = static_cast<uint32_t>(ret.size());
112     if (!reply.WriteUint32(size)) {
113         STATS_HILOGE(COMP_SVC, "Write size failed");
114         return false;
115     }
116     STATS_HILOGD(COMP_SVC, "Write size: %{public}u", size);
117     for (const auto& templateVal : ret) {
118         if (templateVal == nullptr) {
119             continue;
120         }
121         templateVal->Marshalling(reply);
122     }
123     int32_t error = static_cast<int32_t>(GetLastError());
124     STATS_WRITE_PARCEL_WITH_RET(COMP_SVC, reply, Int32, error, E_STATS_WRITE_PARCEL_ERROR);
125     return ERR_OK;
126 }
127 
GetTotalTimeSecondStub(MessageParcel & data,MessageParcel & reply)128 int32_t BatteryStatsStub::GetTotalTimeSecondStub(MessageParcel &data, MessageParcel& reply)
129 {
130     STATS_HILOGD(COMP_SVC, "Enter");
131     int32_t type = data.ReadInt32();
132     StatsUtils::StatsType statsType = StatsUtils::StatsType(type);
133     int32_t uid = data.ReadInt32();
134     uint64_t ret = GetTotalTimeSecond(statsType, uid);
135     if (!reply.WriteUint64(ret)) {
136         STATS_HILOGE(COMP_SVC, "Write ret failed");
137         return false;
138     }
139     return ERR_OK;
140 }
141 
GetTotalDataBytesStub(MessageParcel & data,MessageParcel & reply)142 int32_t BatteryStatsStub::GetTotalDataBytesStub(MessageParcel &data, MessageParcel& reply)
143 {
144     STATS_HILOGD(COMP_SVC, "Enter");
145     int32_t type = data.ReadInt32();
146     StatsUtils::StatsType statsType = StatsUtils::StatsType(type);
147     int32_t uid = data.ReadInt32();
148     uint64_t ret = GetTotalDataBytes(statsType, uid);
149     if (!reply.WriteUint64(ret)) {
150         STATS_HILOGE(COMP_SVC, "Write ret failed");
151         return false;
152     }
153     return ERR_OK;
154 }
155 
GetAppStatsMahStub(MessageParcel & data,MessageParcel & reply)156 int32_t BatteryStatsStub::GetAppStatsMahStub(MessageParcel &data, MessageParcel& reply)
157 {
158     STATS_HILOGD(COMP_SVC, "Enter");
159     int32_t uid = data.ReadInt32();
160     double ret = GetAppStatsMah(uid);
161     if (!reply.WriteDouble(ret)) {
162         STATS_HILOGE(COMP_SVC, "Write ret failed");
163         return false;
164     }
165     int32_t error = static_cast<int32_t>(GetLastError());
166     STATS_WRITE_PARCEL_WITH_RET(COMP_SVC, reply, Int32, error, E_STATS_WRITE_PARCEL_ERROR);
167     return ERR_OK;
168 }
169 
GetAppStatsPercentStub(MessageParcel & data,MessageParcel & reply)170 int32_t BatteryStatsStub::GetAppStatsPercentStub(MessageParcel &data, MessageParcel& reply)
171 {
172     STATS_HILOGD(COMP_SVC, "Enter");
173     int32_t uid = data.ReadInt32();
174     double ret = GetAppStatsPercent(uid);
175     if (!reply.WriteDouble(ret)) {
176         STATS_HILOGE(COMP_SVC, "Write ret failed");
177         return false;
178     }
179     int32_t error = static_cast<int32_t>(GetLastError());
180     STATS_WRITE_PARCEL_WITH_RET(COMP_SVC, reply, Int32, error, E_STATS_WRITE_PARCEL_ERROR);
181     return ERR_OK;
182 }
183 
GetPartStatsMahStub(MessageParcel & data,MessageParcel & reply)184 int32_t BatteryStatsStub::GetPartStatsMahStub(MessageParcel &data, MessageParcel& reply)
185 {
186     STATS_HILOGD(COMP_SVC, "Enter");
187     int32_t typeProxy = data.ReadInt32();
188     BatteryStatsInfo::ConsumptionType type = BatteryStatsInfo::ConsumptionType(typeProxy);
189     double ret = GetPartStatsMah(type);
190     if (!reply.WriteDouble(ret)) {
191         STATS_HILOGE(COMP_SVC, "Write ret failed");
192         return false;
193     }
194     int32_t error = static_cast<int32_t>(GetLastError());
195     STATS_WRITE_PARCEL_WITH_RET(COMP_SVC, reply, Int32, error, E_STATS_WRITE_PARCEL_ERROR);
196     return ERR_OK;
197 }
198 
GetPartStatsPercentStub(MessageParcel & data,MessageParcel & reply)199 int32_t BatteryStatsStub::GetPartStatsPercentStub(MessageParcel &data, MessageParcel& reply)
200 {
201     STATS_HILOGD(COMP_SVC, "Enter");
202     int32_t typeProxy = data.ReadInt32();
203     BatteryStatsInfo::ConsumptionType type = BatteryStatsInfo::ConsumptionType(typeProxy);
204     double ret = GetPartStatsPercent(type);
205     if (!reply.WriteDouble(ret)) {
206         STATS_HILOGE(COMP_SVC, "Write ret failed");
207         return false;
208     }
209     int32_t error = static_cast<int32_t>(GetLastError());
210     STATS_WRITE_PARCEL_WITH_RET(COMP_SVC, reply, Int32, error, E_STATS_WRITE_PARCEL_ERROR);
211     return ERR_OK;
212 }
213 
ResetStub()214 int32_t BatteryStatsStub::ResetStub()
215 {
216     STATS_HILOGD(COMP_SVC, "Enter");
217     Reset();
218     return ERR_OK;
219 }
220 
SetOnBatteryStub(MessageParcel & data)221 int32_t BatteryStatsStub::SetOnBatteryStub(MessageParcel& data)
222 {
223     STATS_HILOGD(COMP_SVC, "Enter");
224     bool isOnBattery = data.ReadBool();
225     SetOnBattery(isOnBattery);
226     return ERR_OK;
227 }
228 
ShellDumpStub(MessageParcel & data,MessageParcel & reply)229 int32_t BatteryStatsStub::ShellDumpStub(MessageParcel& data, MessageParcel& reply)
230 {
231     uint32_t argc;
232     std::vector<std::string> args;
233 
234     if (!data.ReadUint32(argc)) {
235         STATS_HILOGE(COMP_SVC, "Readback fail");
236         return E_STATS_READ_PARCEL_ERROR;
237     }
238 
239     if (argc >= PARAM_MAX_NUM) {
240         STATS_HILOGW(COMP_SVC, "params exceed limit, argc=%{public}d", argc);
241         return E_STATS_EXCEED_PARAM_LIMIT;
242     }
243 
244     for (uint32_t i = 0; i < argc; i++) {
245         std::string arg = data.ReadString();
246         if (arg.empty()) {
247             STATS_HILOGW(COMP_SVC, "read value fail=%{public}d", i);
248             return E_STATS_READ_PARCEL_ERROR;
249         }
250         args.push_back(arg);
251     }
252 
253     std::string ret = ShellDump(args, argc);
254     if (!reply.WriteString(ret)) {
255         STATS_HILOGE(COMP_SVC, "Dump Writeback Fail");
256         return E_STATS_WRITE_PARCEL_ERROR;
257     }
258     return ERR_OK;
259 }
260 } // namespace PowerMgr
261 } // namespace OHOS
262