• 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 "stats_service_test_proxy.h"
17 
18 #include <message_parcel.h>
19 
20 #include "ibattery_stats.h"
21 #include "battery_stats_proxy.h"
22 #include "errors.h"
23 #include "message_option.h"
24 #include "stats_common.h"
25 #include "hilog/log.h"
26 #include "stats_utils.h"
27 
28 using OHOS::HiviewDFX::HiLog;
29 
30 namespace OHOS {
31 namespace PowerMgr {
StatsServiceTestProxy(const sptr<BatteryStatsService> & service)32 StatsServiceTestProxy::StatsServiceTestProxy(const sptr<BatteryStatsService>& service)
33 {
34     if (service != nullptr) {
35         stub_ = static_cast<sptr<BatteryStatsStub>>(service);
36     }
37 }
38 
GetBatteryStatsIpc(ParcelableBatteryStatsList & batteryStats,int32_t & tempError)39 ErrCode StatsServiceTestProxy::GetBatteryStatsIpc(
40     ParcelableBatteryStatsList& batteryStats,
41     int32_t& tempError)
42 {
43     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
44 
45     MessageParcel data;
46     MessageParcel reply;
47     MessageOption option(MessageOption::TF_SYNC);
48 
49     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
50         HiLog::Error(LABEL, "Write interface token failed!");
51         return ERR_INVALID_VALUE;
52     }
53 
54     int32_t result = stub_->SendRequest(
55         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_BATTERY_STATS_IPC), data, reply, option);
56     if (FAILED(result)) {
57         HiLog::Error(LABEL, "Send request failed!");
58         return result;
59     }
60 
61     ErrCode errCode = reply.ReadInt32();
62     if (FAILED(errCode)) {
63         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
64             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_BATTERY_STATS_IPC));
65         return errCode;
66     }
67 
68     std::unique_ptr<ParcelableBatteryStatsList> batteryStatsInfo(reply.ReadParcelable<ParcelableBatteryStatsList>());
69     if (batteryStatsInfo != nullptr) {
70         batteryStats = *batteryStatsInfo;
71     }
72 
73     tempError = reply.ReadInt32();
74     return ERR_OK;
75 }
76 
SetOnBatteryIpc(bool isOnBattery)77 ErrCode StatsServiceTestProxy::SetOnBatteryIpc(
78     bool isOnBattery)
79 {
80     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
81 
82     MessageParcel data;
83     MessageParcel reply;
84     MessageOption option(MessageOption::TF_SYNC);
85 
86     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
87         HiLog::Error(LABEL, "Write interface token failed!");
88         return ERR_INVALID_VALUE;
89     }
90 
91     if (!data.WriteInt32(isOnBattery ? 1 : 0)) {
92         HiLog::Error(LABEL, "Write [isOnBattery] failed!");
93         return ERR_INVALID_DATA;
94     }
95 
96     int32_t result = stub_->SendRequest(
97         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_SET_ON_BATTERY_IPC), data, reply, option);
98     if (FAILED(result)) {
99         HiLog::Error(LABEL, "Send request failed!");
100         return result;
101     }
102 
103     ErrCode errCode = reply.ReadInt32();
104     if (FAILED(errCode)) {
105         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
106             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_SET_ON_BATTERY_IPC));
107         return errCode;
108     }
109 
110     return ERR_OK;
111 }
112 
GetAppStatsMahIpc(int32_t uid,double & appStatsMah,int32_t & tempError)113 ErrCode StatsServiceTestProxy::GetAppStatsMahIpc(
114     int32_t uid,
115     double& appStatsMah,
116     int32_t& tempError)
117 {
118     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
119 
120     MessageParcel data;
121     MessageParcel reply;
122     MessageOption option(MessageOption::TF_SYNC);
123 
124     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
125         HiLog::Error(LABEL, "Write interface token failed!");
126         return ERR_INVALID_VALUE;
127     }
128 
129     if (!data.WriteInt32(uid)) {
130         HiLog::Error(LABEL, "Write [uid] failed!");
131         return ERR_INVALID_DATA;
132     }
133 
134     int32_t result = stub_->SendRequest(
135         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_APP_STATS_MAH_IPC), data, reply, option);
136     if (FAILED(result)) {
137         HiLog::Error(LABEL, "Send request failed!");
138         return result;
139     }
140 
141     ErrCode errCode = reply.ReadInt32();
142     if (FAILED(errCode)) {
143         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
144             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_APP_STATS_MAH_IPC));
145         return errCode;
146     }
147 
148     appStatsMah = reply.ReadDouble();
149     tempError = reply.ReadInt32();
150     return ERR_OK;
151 }
152 
GetAppStatsPercentIpc(int32_t uid,double & appStatsPercent,int32_t & tempError)153 ErrCode StatsServiceTestProxy::GetAppStatsPercentIpc(
154     int32_t uid,
155     double& appStatsPercent,
156     int32_t& tempError)
157 {
158     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
159 
160     MessageParcel data;
161     MessageParcel reply;
162     MessageOption option(MessageOption::TF_SYNC);
163 
164     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
165         HiLog::Error(LABEL, "Write interface token failed!");
166         return ERR_INVALID_VALUE;
167     }
168 
169     if (!data.WriteInt32(uid)) {
170         HiLog::Error(LABEL, "Write [uid] failed!");
171         return ERR_INVALID_DATA;
172     }
173 
174     int32_t result = stub_->SendRequest(
175         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_APP_STATS_PERCENT_IPC), data, reply, option);
176     if (FAILED(result)) {
177         HiLog::Error(LABEL, "Send request failed!");
178         return result;
179     }
180 
181     ErrCode errCode = reply.ReadInt32();
182     if (FAILED(errCode)) {
183         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
184             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_APP_STATS_PERCENT_IPC));
185         return errCode;
186     }
187 
188     appStatsPercent = reply.ReadDouble();
189     tempError = reply.ReadInt32();
190     return ERR_OK;
191 }
192 
GetPartStatsMahIpc(int32_t type,double & partStatsMah,int32_t & tempError)193 ErrCode StatsServiceTestProxy::GetPartStatsMahIpc(
194     int32_t type,
195     double& partStatsMah,
196     int32_t& tempError)
197 {
198     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
199 
200     MessageParcel data;
201     MessageParcel reply;
202     MessageOption option(MessageOption::TF_SYNC);
203 
204     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
205         HiLog::Error(LABEL, "Write interface token failed!");
206         return ERR_INVALID_VALUE;
207     }
208 
209     if (!data.WriteInt32(type)) {
210         HiLog::Error(LABEL, "Write [type] failed!");
211         return ERR_INVALID_DATA;
212     }
213 
214     int32_t result = stub_->SendRequest(
215         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_PART_STATS_MAH_IPC), data, reply, option);
216     if (FAILED(result)) {
217         HiLog::Error(LABEL, "Send request failed!");
218         return result;
219     }
220 
221     ErrCode errCode = reply.ReadInt32();
222     if (FAILED(errCode)) {
223         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
224             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_PART_STATS_MAH_IPC));
225         return errCode;
226     }
227 
228     partStatsMah = reply.ReadDouble();
229     tempError = reply.ReadInt32();
230     return ERR_OK;
231 }
232 
GetPartStatsPercentIpc(int32_t type,double & partStatsPercent,int32_t & tempError)233 ErrCode StatsServiceTestProxy::GetPartStatsPercentIpc(
234     int32_t type,
235     double& partStatsPercent,
236     int32_t& tempError)
237 {
238     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
239 
240     MessageParcel data;
241     MessageParcel reply;
242     MessageOption option(MessageOption::TF_SYNC);
243 
244     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
245         HiLog::Error(LABEL, "Write interface token failed!");
246         return ERR_INVALID_VALUE;
247     }
248 
249     if (!data.WriteInt32(type)) {
250         HiLog::Error(LABEL, "Write [type] failed!");
251         return ERR_INVALID_DATA;
252     }
253 
254     int32_t result = stub_->SendRequest(
255         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_PART_STATS_PERCENT_IPC), data, reply, option);
256     if (FAILED(result)) {
257         HiLog::Error(LABEL, "Send request failed!");
258         return result;
259     }
260 
261     ErrCode errCode = reply.ReadInt32();
262     if (FAILED(errCode)) {
263         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
264             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_PART_STATS_PERCENT_IPC));
265         return errCode;
266     }
267 
268     partStatsPercent = reply.ReadDouble();
269     tempError = reply.ReadInt32();
270     return ERR_OK;
271 }
272 
GetTotalTimeSecondIpc(int32_t statsType,int32_t uid,uint64_t & totalTimeSecond)273 ErrCode StatsServiceTestProxy::GetTotalTimeSecondIpc(
274     int32_t statsType,
275     int32_t uid,
276     uint64_t& totalTimeSecond)
277 {
278     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
279 
280     MessageParcel data;
281     MessageParcel reply;
282     MessageOption option(MessageOption::TF_SYNC);
283 
284     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
285         HiLog::Error(LABEL, "Write interface token failed!");
286         return ERR_INVALID_VALUE;
287     }
288 
289     if (!data.WriteInt32(statsType)) {
290         HiLog::Error(LABEL, "Write [statsType] failed!");
291         return ERR_INVALID_DATA;
292     }
293     if (!data.WriteInt32(uid)) {
294         HiLog::Error(LABEL, "Write [uid] failed!");
295         return ERR_INVALID_DATA;
296     }
297 
298     int32_t result = stub_->SendRequest(
299         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_TOTAL_TIME_SECOND_IPC), data, reply, option);
300     if (FAILED(result)) {
301         HiLog::Error(LABEL, "Send request failed!");
302         return result;
303     }
304 
305     ErrCode errCode = reply.ReadInt32();
306     if (FAILED(errCode)) {
307         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
308             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_TOTAL_TIME_SECOND_IPC));
309         return errCode;
310     }
311 
312     totalTimeSecond = reply.ReadUint64();
313     return ERR_OK;
314 }
315 
GetTotalDataBytesIpc(int32_t statsType,int32_t uid,uint64_t & totalDataBytes)316 ErrCode StatsServiceTestProxy::GetTotalDataBytesIpc(
317     int32_t statsType,
318     int32_t uid,
319     uint64_t& totalDataBytes)
320 {
321     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
322 
323     MessageParcel data;
324     MessageParcel reply;
325     MessageOption option(MessageOption::TF_SYNC);
326 
327     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
328         HiLog::Error(LABEL, "Write interface token failed!");
329         return ERR_INVALID_VALUE;
330     }
331 
332     if (!data.WriteInt32(statsType)) {
333         HiLog::Error(LABEL, "Write [statsType] failed!");
334         return ERR_INVALID_DATA;
335     }
336     if (!data.WriteInt32(uid)) {
337         HiLog::Error(LABEL, "Write [uid] failed!");
338         return ERR_INVALID_DATA;
339     }
340 
341     int32_t result = stub_->SendRequest(
342         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_TOTAL_DATA_BYTES_IPC), data, reply, option);
343     if (FAILED(result)) {
344         HiLog::Error(LABEL, "Send request failed!");
345         return result;
346     }
347 
348     ErrCode errCode = reply.ReadInt32();
349     if (FAILED(errCode)) {
350         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
351             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_GET_TOTAL_DATA_BYTES_IPC));
352         return errCode;
353     }
354 
355     totalDataBytes = reply.ReadUint64();
356     return ERR_OK;
357 }
358 
ResetIpc()359 ErrCode StatsServiceTestProxy::ResetIpc()
360 {
361     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
362 
363     MessageParcel data;
364     MessageParcel reply;
365     MessageOption option(MessageOption::TF_SYNC);
366 
367     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
368         HiLog::Error(LABEL, "Write interface token failed!");
369         return ERR_INVALID_VALUE;
370     }
371 
372     int32_t result = stub_->SendRequest(
373         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_RESET_IPC), data, reply, option);
374     if (FAILED(result)) {
375         HiLog::Error(LABEL, "Send request failed!");
376         return result;
377     }
378 
379     ErrCode errCode = reply.ReadInt32();
380     if (FAILED(errCode)) {
381         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
382             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_RESET_IPC));
383         return errCode;
384     }
385 
386     return ERR_OK;
387 }
388 
ShellDumpIpc(const std::vector<std::string> & args,uint32_t argc,std::string & dumpShell)389 ErrCode StatsServiceTestProxy::ShellDumpIpc(
390     const std::vector<std::string>& args,
391     uint32_t argc,
392     std::string& dumpShell)
393 {
394     STATS_RETURN_IF_WITH_RET(stub_ == nullptr, StatsUtils::DEFAULT_VALUE);
395 
396     MessageParcel data;
397     MessageParcel reply;
398     MessageOption option(MessageOption::TF_SYNC);
399 
400     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
401         HiLog::Error(LABEL, "Write interface token failed!");
402         return ERR_INVALID_VALUE;
403     }
404 
405     if (args.size() > static_cast<size_t>(VECTOR_MAX_SIZE)) {
406         HiLog::Error(LABEL, "The vector/array size exceeds the security limit!");
407         return ERR_INVALID_DATA;
408     }
409     data.WriteInt32(args.size());
410     for (auto it1 = args.begin(); it1 != args.end(); ++it1) {
411         if (!data.WriteString16(Str8ToStr16((*it1)))) {
412             HiLog::Error(LABEL, "Write [(*it1)] failed!");
413             return ERR_INVALID_DATA;
414         }
415     }
416     if (!data.WriteUint32(argc)) {
417         HiLog::Error(LABEL, "Write [argc] failed!");
418         return ERR_INVALID_DATA;
419     }
420 
421     int32_t result = stub_->SendRequest(
422         static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_SHELL_DUMP_IPC), data, reply, option);
423     if (FAILED(result)) {
424         HiLog::Error(LABEL, "Send request failed!");
425         return result;
426     }
427 
428     ErrCode errCode = reply.ReadInt32();
429     if (FAILED(errCode)) {
430         HiLog::Error(LABEL, "Read result failed, code is: %{public}d.",
431             static_cast<uint32_t>(IBatteryStatsIpcCode::COMMAND_SHELL_DUMP_IPC));
432         return errCode;
433     }
434 
435     dumpShell = Str16ToStr8(reply.ReadString16());
436     return ERR_OK;
437 }
438 } // namespace PowerMgr
439 } // namespace OHOS