• 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_proxy.h"
17 
18 #include <message_parcel.h>
19 #include "errors.h"
20 #include "message_option.h"
21 #include "stats_common.h"
22 #include "stats_log.h"
23 #include "stats_utils.h"
24 
25 namespace OHOS {
26 namespace PowerMgr {
GetBatteryStats()27 BatteryStatsInfoList BatteryStatsProxy::GetBatteryStats()
28 {
29     STATS_HILOGD(COMP_FWK, "Enter");
30     BatteryStatsInfoList infoList;
31     sptr<IRemoteObject> remote = Remote();
32     STATS_RETURN_IF_WITH_RET(remote == nullptr, infoList);
33 
34     MessageParcel data;
35     MessageParcel reply;
36     MessageOption option;
37 
38     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
39         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
40         return infoList;
41     }
42 
43     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_GET),
44         data, reply, option);
45     if (ret != ERR_OK) {
46         STATS_HILOGE(COMP_FWK, "Send request is failed, error code: %{public}d", ret);
47         return infoList;
48     }
49     int32_t size = reply.ReadInt32();
50     for (int32_t i = 0; i < size; ++i) {
51         std::shared_ptr<BatteryStatsInfo> info = std::make_shared<BatteryStatsInfo>();
52         info->ReadFromParcel(reply);
53         infoList.emplace_back(info);
54     }
55     return infoList;
56 }
57 
GetTotalTimeSecond(const StatsUtils::StatsType & statsType,const int32_t & uid)58 uint64_t BatteryStatsProxy::GetTotalTimeSecond(const StatsUtils::StatsType& statsType, const int32_t& uid)
59 {
60     STATS_HILOGD(COMP_FWK, "Enter");
61     sptr<IRemoteObject> remote = Remote();
62     STATS_RETURN_IF_WITH_RET(remote == nullptr, StatsUtils::DEFAULT_VALUE);
63 
64     MessageParcel data;
65     MessageParcel reply;
66     MessageOption option;
67 
68     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
69         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
70         return StatsUtils::DEFAULT_VALUE;
71     }
72 
73     uint64_t time = StatsUtils::DEFAULT_VALUE;
74     STATS_WRITE_PARCEL_WITH_RET(COMP_FWK, data, Int32, static_cast<int32_t>(statsType), StatsUtils::DEFAULT_VALUE);
75     STATS_WRITE_PARCEL_WITH_RET(COMP_FWK, data, Int32, uid, StatsUtils::DEFAULT_VALUE);
76 
77     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_GETTIME), data, reply, option);
78     if (ret != ERR_OK) {
79         STATS_HILOGE(COMP_FWK, "Transact is failed, error code: %{public}d", ret);
80     }
81 
82     STATS_READ_PARCEL_WITH_RET(COMP_FWK, reply, Uint64, time, StatsUtils::DEFAULT_VALUE);
83     return time;
84 }
85 
GetTotalDataBytes(const StatsUtils::StatsType & statsType,const int32_t & uid)86 uint64_t BatteryStatsProxy::GetTotalDataBytes(const StatsUtils::StatsType& statsType, const int32_t& uid)
87 {
88     STATS_HILOGD(COMP_FWK, "Enter");
89     sptr<IRemoteObject> remote = Remote();
90     STATS_RETURN_IF_WITH_RET(remote == nullptr, StatsUtils::DEFAULT_VALUE);
91 
92     MessageParcel data;
93     MessageParcel reply;
94     MessageOption option;
95 
96     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
97         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
98         return StatsUtils::DEFAULT_VALUE;
99     }
100 
101     uint64_t count = StatsUtils::DEFAULT_VALUE;
102     STATS_WRITE_PARCEL_WITH_RET(COMP_FWK, data, Int32, static_cast<int32_t>(statsType), StatsUtils::DEFAULT_VALUE);
103     STATS_WRITE_PARCEL_WITH_RET(COMP_FWK, data, Int32, uid, StatsUtils::DEFAULT_VALUE);
104 
105     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_GETDATA), data, reply, option);
106     if (ret != ERR_OK) {
107         STATS_HILOGE(COMP_FWK, "Transact is failed, error code: %{public}d", ret);
108     }
109 
110     STATS_READ_PARCEL_WITH_RET(COMP_FWK, reply, Uint64, count, StatsUtils::DEFAULT_VALUE);
111     return count;
112 }
113 
GetAppStatsMah(const int32_t & uid)114 double BatteryStatsProxy::GetAppStatsMah(const int32_t& uid)
115 {
116     STATS_HILOGD(COMP_FWK, "Enter");
117     sptr<IRemoteObject> remote = Remote();
118     STATS_RETURN_IF_WITH_RET(remote == nullptr, StatsUtils::DEFAULT_VALUE);
119 
120     MessageParcel data;
121     MessageParcel reply;
122     MessageOption option;
123 
124     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
125         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
126         return StatsUtils::DEFAULT_VALUE;
127     }
128 
129     data.WriteInt32(uid);
130 
131     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_GETAPPMAH), data, reply, option);
132     if (ret != ERR_OK) {
133         STATS_HILOGE(COMP_FWK, "Transact is failed, error code: %{public}d", ret);
134     }
135 
136     double appStatsMah = reply.ReadDouble();
137     STATS_HILOGD(COMP_FWK, "Get stats mah: %{public}lf for uid: %{public}d", appStatsMah, uid);
138     return appStatsMah;
139 }
140 
SetOnBattery(bool isOnBattery)141 void BatteryStatsProxy::SetOnBattery(bool isOnBattery)
142 {
143     STATS_HILOGD(COMP_FWK, "Enter");
144     sptr<IRemoteObject> remote = Remote();
145     STATS_RETURN_IF(remote == nullptr);
146 
147     MessageParcel data;
148     MessageParcel reply;
149     MessageOption option;
150 
151     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
152         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
153         return;
154     }
155 
156     data.WriteBool(isOnBattery);
157 
158     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_SETONBATT), data, reply, option);
159     if (ret != ERR_OK) {
160         STATS_HILOGE(COMP_FWK, "Transact is failed, error code: %{public}d", ret);
161     }
162 }
163 
GetAppStatsPercent(const int32_t & uid)164 double BatteryStatsProxy::GetAppStatsPercent(const int32_t& uid)
165 {
166     STATS_HILOGD(COMP_FWK, "Enter");
167     sptr<IRemoteObject> remote = Remote();
168     STATS_RETURN_IF_WITH_RET(remote == nullptr, StatsUtils::DEFAULT_VALUE);
169 
170     MessageParcel data;
171     MessageParcel reply;
172     MessageOption option;
173 
174     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
175         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
176         return StatsUtils::DEFAULT_VALUE;
177     }
178 
179     data.WriteInt32(uid);
180 
181     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_GETAPPPER), data, reply, option);
182     if (ret != ERR_OK) {
183         STATS_HILOGE(COMP_FWK, "Transact is failed, error code: %{public}d", ret);
184     }
185 
186     double appStatsPercent = reply.ReadDouble();
187     STATS_HILOGD(COMP_FWK, "Get stats percent: %{public}lf for uid: %{public}d", appStatsPercent, uid);
188     return appStatsPercent;
189 }
190 
GetPartStatsMah(const BatteryStatsInfo::ConsumptionType & type)191 double BatteryStatsProxy::GetPartStatsMah(const BatteryStatsInfo::ConsumptionType& type)
192 {
193     STATS_HILOGD(COMP_FWK, "Enter");
194     sptr<IRemoteObject> remote = Remote();
195     STATS_RETURN_IF_WITH_RET(remote == nullptr, StatsUtils::DEFAULT_VALUE);
196 
197     MessageParcel data;
198     MessageParcel reply;
199     MessageOption option;
200 
201     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
202         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
203         return StatsUtils::DEFAULT_VALUE;
204     }
205 
206     data.WriteInt32(type);
207 
208     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_GETPARTMAH), data, reply, option);
209     if (ret != ERR_OK) {
210         STATS_HILOGE(COMP_FWK, "Transact is failed, error code: %{public}d", ret);
211     }
212 
213     double partStatsMah = reply.ReadDouble();
214     STATS_HILOGD(COMP_FWK, "Get stats mah: %{public}lf for type: %{public}d", partStatsMah, type);
215     return partStatsMah;
216 }
217 
GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType & type)218 double BatteryStatsProxy::GetPartStatsPercent(const BatteryStatsInfo::ConsumptionType& type)
219 {
220     STATS_HILOGD(COMP_FWK, "Enter");
221     sptr<IRemoteObject> remote = Remote();
222     STATS_RETURN_IF_WITH_RET(remote == nullptr, StatsUtils::DEFAULT_VALUE);
223 
224     MessageParcel data;
225     MessageParcel reply;
226     MessageOption option;
227 
228     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
229         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
230         return StatsUtils::DEFAULT_VALUE;
231     }
232 
233     data.WriteInt32(type);
234 
235     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_GETPARTPER), data, reply, option);
236     if (ret != ERR_OK) {
237         STATS_HILOGE(COMP_FWK, "Transact is failed, error code: %{public}d", ret);
238     }
239 
240     double partStatsPercent = reply.ReadDouble();
241     STATS_HILOGD(COMP_FWK, "Get stats percent: %{public}lf for type: %{public}d", partStatsPercent, type);
242     return partStatsPercent;
243 }
244 
Reset()245 void BatteryStatsProxy::Reset()
246 {
247     STATS_HILOGD(COMP_FWK, "Enter");
248     sptr<IRemoteObject> remote = Remote();
249     STATS_RETURN_IF(remote == nullptr);
250 
251     MessageParcel data;
252     MessageParcel reply;
253     MessageOption option;
254 
255     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
256         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
257         return;
258     }
259 
260     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_RESET), data, reply, option);
261     if (ret != ERR_OK) {
262         STATS_HILOGE(COMP_FWK, "Transact is failed, error code: %{public}d", ret);
263     }
264 }
265 
ShellDump(const std::vector<std::string> & args,uint32_t argc)266 std::string BatteryStatsProxy::ShellDump(const std::vector<std::string>& args, uint32_t argc)
267 {
268     sptr<IRemoteObject> remote = Remote();
269     std::string result = "remote error";
270     STATS_RETURN_IF_WITH_RET(remote == nullptr, result);
271 
272     MessageParcel data;
273     MessageParcel reply;
274     MessageOption option;
275 
276     if (!data.WriteInterfaceToken(BatteryStatsProxy::GetDescriptor())) {
277         STATS_HILOGE(COMP_FWK, "Write descriptor failed");
278         return result;
279     }
280 
281     data.WriteUint32(argc);
282     for (uint32_t i = 0; i < argc; i++) {
283         data.WriteString(args[i]);
284     }
285     int ret = remote->SendRequest(static_cast<uint32_t>(IBatteryStats::BATTERY_STATS_DUMP),
286         data, reply, option);
287     if (ret != ERR_OK) {
288         STATS_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
289         return result;
290     }
291     result = reply.ReadString();
292 
293     return result;
294 }
295 } // namespace PowerMgr
296 } // namespace OHOS