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