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