1 /*
2  * Copyright (c) 2021-2023 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 "net_stats_service_stub.h"
17 
18 #include "net_all_capabilities.h"
19 #include "net_manager_constants.h"
20 #include "net_mgr_log_wrapper.h"
21 #include "net_stats_network.h"
22 #include "netmanager_base_permission.h"
23 
24 namespace OHOS {
25 namespace NetManagerStandard {
NetStatsServiceStub()26 NetStatsServiceStub::NetStatsServiceStub()
27 {
28     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_NSM_REGISTER_NET_STATS_CALLBACK)] =
29         &NetStatsServiceStub::OnRegisterNetStatsCallback;
30     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_NSM_UNREGISTER_NET_STATS_CALLBACK)] =
31         &NetStatsServiceStub::OnUnregisterNetStatsCallback;
32     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_IFACE_RXBYTES)] =
33         &NetStatsServiceStub::OnGetIfaceRxBytes;
34     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_IFACE_TXBYTES)] =
35         &NetStatsServiceStub::OnGetIfaceTxBytes;
36     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_CELLULAR_RXBYTES)] =
37         &NetStatsServiceStub::OnGetCellularRxBytes;
38     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_CELLULAR_TXBYTES)] =
39         &NetStatsServiceStub::OnGetCellularTxBytes;
40     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_RXBYTES)] =
41         &NetStatsServiceStub::OnGetAllRxBytes;
42     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_TXBYTES)] =
43         &NetStatsServiceStub::OnGetAllTxBytes;
44     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_UID_RXBYTES)] =
45         &NetStatsServiceStub::OnGetUidRxBytes;
46     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_UID_TXBYTES)] =
47         &NetStatsServiceStub::OnGetUidTxBytes;
48     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_IFACE_STATS_DETAIL)] =
49         &NetStatsServiceStub::OnGetIfaceStatsDetail;
50     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_UID_STATS_DETAIL)] =
51         &NetStatsServiceStub::OnGetUidStatsDetail;
52     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_UPDATE_IFACES_STATS)] =
53         &NetStatsServiceStub::OnUpdateIfacesStats;
54     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_UPDATE_STATS_DATA)] =
55         &NetStatsServiceStub::OnUpdateStatsData;
56     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_NSM_RESET_FACTORY)] =
57         &NetStatsServiceStub::OnResetFactory;
58     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_STATS_INFO)] =
59         &NetStatsServiceStub::OnGetAllStatsInfo;
60     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_SIM_STATS_INFO)] =
61         &NetStatsServiceStub::OnGetAllSimStatsInfo;
62     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_TRAFFIC_STATS_BY_NETWORK)] =
63         &NetStatsServiceStub::OnGetTrafficStatsByNetwork;
64     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_TRAFFIC_STATS_BY_UID_NETWORK)] =
65         &NetStatsServiceStub::OnGetTrafficStatsByUidNetwork;
66     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_SET_APP_STATS)] =
67         &NetStatsServiceStub::OnSetAppStats;
68     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_COOKIE_RXBYTES)] =
69         &NetStatsServiceStub::OnGetCookieRxBytes;
70     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_COOKIE_TXBYTES)] =
71         &NetStatsServiceStub::OnGetCookieTxBytes;
72 }
73 
74 NetStatsServiceStub::~NetStatsServiceStub() = default;
75 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)76 int32_t NetStatsServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
77                                              MessageOption &option)
78 {
79     NETMGR_LOG_D("stub call start, code = [%{public}d]", code);
80 
81     std::u16string myDescripters = NetStatsServiceStub::GetDescriptor();
82     std::u16string remoteDescripters = data.ReadInterfaceToken();
83     if (myDescripters != remoteDescripters) {
84         NETMGR_LOG_D("descriptor checked fail");
85         return NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
86     }
87 
88     auto itFunc = memberFuncMap_.find(code);
89     if (itFunc != memberFuncMap_.end()) {
90         auto requestFunc = itFunc->second;
91         if (requestFunc != nullptr) {
92             int32_t ret = (this->*requestFunc)(data, reply);
93             NETMGR_LOG_D("stub call end, code = [%{public}d], ret = [%{public}d]", code, ret);
94             return ret;
95         }
96     }
97     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
98 }
99 
OnRegisterNetStatsCallback(MessageParcel & data,MessageParcel & reply)100 int32_t NetStatsServiceStub::OnRegisterNetStatsCallback(MessageParcel &data, MessageParcel &reply)
101 {
102     int32_t ret = CheckNetManagerAvailable(reply);
103     if (ret != NETMANAGER_SUCCESS) {
104         return ret;
105     }
106     int32_t result = NETMANAGER_ERR_LOCAL_PTR_NULL;
107     sptr<IRemoteObject> remote = data.ReadRemoteObject();
108     if (remote == nullptr) {
109         NETMGR_LOG_E("Callback ptr is nullptr.");
110         reply.WriteInt32(result);
111         return result;
112     }
113 
114     sptr<INetStatsCallback> callback = iface_cast<INetStatsCallback>(remote);
115     result = RegisterNetStatsCallback(callback);
116     NETMGR_LOG_D("OnRegisterNetStatsCallback result = [%{public}d]", result);
117     if (!reply.WriteInt32(result)) {
118         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
119     }
120     return NETMANAGER_SUCCESS;
121 }
122 
OnUnregisterNetStatsCallback(MessageParcel & data,MessageParcel & reply)123 int32_t NetStatsServiceStub::OnUnregisterNetStatsCallback(MessageParcel &data, MessageParcel &reply)
124 {
125     int32_t ret = CheckNetManagerAvailable(reply);
126     if (ret != NETMANAGER_SUCCESS) {
127         return ret;
128     }
129     int32_t result = NETMANAGER_ERR_LOCAL_PTR_NULL;
130     sptr<IRemoteObject> remote = data.ReadRemoteObject();
131     if (remote == nullptr) {
132         NETMGR_LOG_E("callback ptr is nullptr.");
133         reply.WriteInt32(result);
134         return result;
135     }
136     sptr<INetStatsCallback> callback = iface_cast<INetStatsCallback>(remote);
137     result = UnregisterNetStatsCallback(callback);
138     if (!reply.WriteInt32(result)) {
139         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
140     }
141     return result;
142 }
143 
OnGetIfaceRxBytes(MessageParcel & data,MessageParcel & reply)144 int32_t NetStatsServiceStub::OnGetIfaceRxBytes(MessageParcel &data, MessageParcel &reply)
145 {
146     uint64_t stats = 0;
147     std::string iface;
148     if (!data.ReadString(iface)) {
149         NETMGR_LOG_E("Read string failed");
150         return NETMANAGER_ERR_READ_DATA_FAIL;
151     }
152     int32_t result = GetIfaceRxBytes(stats, iface);
153     if (!reply.WriteInt32(result)) {
154         NETMGR_LOG_E("WriteInt32 failed");
155         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
156     }
157     if (result == NETMANAGER_SUCCESS) {
158         if (!reply.WriteUint64(stats)) {
159             NETMGR_LOG_E("WriteUint64 failed");
160             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
161         }
162     }
163     return NETMANAGER_SUCCESS;
164 }
165 
OnGetIfaceTxBytes(MessageParcel & data,MessageParcel & reply)166 int32_t NetStatsServiceStub::OnGetIfaceTxBytes(MessageParcel &data, MessageParcel &reply)
167 {
168     uint64_t stats = 0;
169     std::string iface;
170     if (!data.ReadString(iface)) {
171         NETMGR_LOG_E("Read string failed");
172         return NETMANAGER_ERR_READ_DATA_FAIL;
173     }
174 
175     int32_t result = GetIfaceTxBytes(stats, iface);
176     if (!reply.WriteInt32(result)) {
177         NETMGR_LOG_E("WriteInt32 failed");
178         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
179     }
180     if (result == NETMANAGER_SUCCESS) {
181         if (!reply.WriteUint64(stats)) {
182             NETMGR_LOG_E("WriteUint64 failed");
183             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
184         }
185     }
186     return NETMANAGER_SUCCESS;
187 }
188 
OnGetCellularRxBytes(MessageParcel & data,MessageParcel & reply)189 int32_t NetStatsServiceStub::OnGetCellularRxBytes(MessageParcel &data, MessageParcel &reply)
190 {
191     uint64_t stats = 0;
192     int32_t ret = GetCellularRxBytes(stats);
193     if (!reply.WriteInt32(ret)) {
194         NETMGR_LOG_E("WriteInt32 failed");
195         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
196     }
197     if (ret == NETMANAGER_SUCCESS) {
198         if (!reply.WriteUint64(stats)) {
199             NETMGR_LOG_E("WriteUint64 failed");
200             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
201         }
202     }
203     return NETMANAGER_SUCCESS;
204 }
205 
OnGetCellularTxBytes(MessageParcel & data,MessageParcel & reply)206 int32_t NetStatsServiceStub::OnGetCellularTxBytes(MessageParcel &data, MessageParcel &reply)
207 {
208     uint64_t stats = 0;
209     int32_t ret = GetCellularTxBytes(stats);
210     if (!reply.WriteInt32(ret)) {
211         NETMGR_LOG_E("WriteInt32 failed");
212         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
213     }
214     if (ret == NETMANAGER_SUCCESS) {
215         if (!reply.WriteUint64(stats)) {
216             NETMGR_LOG_E("WriteUint64 failed");
217             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
218         }
219     }
220     return NETMANAGER_SUCCESS;
221 }
222 
OnGetAllRxBytes(MessageParcel & data,MessageParcel & reply)223 int32_t NetStatsServiceStub::OnGetAllRxBytes(MessageParcel &data, MessageParcel &reply)
224 {
225     uint64_t stats = 0;
226     int32_t ret = GetAllRxBytes(stats);
227     if (!reply.WriteInt32(ret)) {
228         NETMGR_LOG_E("WriteInt32 failed");
229         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
230     }
231     if (ret == NETMANAGER_SUCCESS) {
232         if (!reply.WriteUint64(stats)) {
233             NETMGR_LOG_E("WriteUint64 failed");
234             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
235         }
236     }
237 
238     return NETMANAGER_SUCCESS;
239 }
240 
OnGetAllTxBytes(MessageParcel & data,MessageParcel & reply)241 int32_t NetStatsServiceStub::OnGetAllTxBytes(MessageParcel &data, MessageParcel &reply)
242 {
243     uint64_t stats = 0;
244     int32_t ret = GetAllTxBytes(stats);
245     if (!reply.WriteInt32(ret)) {
246         NETMGR_LOG_E("WriteInt32 failed");
247         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
248     }
249     if (ret == NETMANAGER_SUCCESS) {
250         if (!reply.WriteUint64(stats)) {
251             NETMGR_LOG_E("WriteUint64 failed");
252             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
253         }
254     }
255 
256     return NETMANAGER_SUCCESS;
257 }
258 
OnGetUidRxBytes(MessageParcel & data,MessageParcel & reply)259 int32_t NetStatsServiceStub::OnGetUidRxBytes(MessageParcel &data, MessageParcel &reply)
260 {
261     uint32_t uid;
262     uint64_t stats = 0;
263     if (!data.ReadUint32(uid)) {
264         NETMGR_LOG_E("ReadInt32 failed");
265         return NETMANAGER_ERR_READ_DATA_FAIL;
266     }
267 
268     int32_t result = GetUidRxBytes(stats, uid);
269     if (!reply.WriteInt32(result)) {
270         NETMGR_LOG_E("WriteInt32 failed");
271         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
272     }
273     if (result == NETMANAGER_SUCCESS) {
274         if (!reply.WriteUint64(stats)) {
275             NETMGR_LOG_E("WriteUint64 failed");
276             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
277         }
278     }
279     return NETMANAGER_SUCCESS;
280 }
281 
OnGetUidTxBytes(MessageParcel & data,MessageParcel & reply)282 int32_t NetStatsServiceStub::OnGetUidTxBytes(MessageParcel &data, MessageParcel &reply)
283 {
284     uint32_t uid;
285     uint64_t stats = 0;
286     if (!data.ReadUint32(uid)) {
287         NETMGR_LOG_E("ReadInt32 failed");
288         return NETMANAGER_ERR_READ_DATA_FAIL;
289     }
290 
291     int32_t result = GetUidTxBytes(stats, uid);
292     if (!reply.WriteInt32(result)) {
293         NETMGR_LOG_E("WriteInt32 failed");
294         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
295     }
296     if (result == NETMANAGER_SUCCESS) {
297         if (!reply.WriteUint64(stats)) {
298             NETMGR_LOG_E("WriteUint64 failed");
299             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
300         }
301     }
302     return NETMANAGER_SUCCESS;
303 }
304 
OnGetIfaceStatsDetail(MessageParcel & data,MessageParcel & reply)305 int32_t NetStatsServiceStub::OnGetIfaceStatsDetail(MessageParcel &data, MessageParcel &reply)
306 {
307     int32_t res = CheckNetManagerAvailable(reply);
308     if (res != NETMANAGER_SUCCESS) {
309         return res;
310     }
311     std::string iface;
312     uint64_t start = 0;
313     uint64_t end = 0;
314     if (!(data.ReadString(iface) && data.ReadUint64(start) && data.ReadUint64(end))) {
315         return NETMANAGER_ERR_READ_DATA_FAIL;
316     }
317     NetStatsInfo info;
318     int32_t ret = GetIfaceStatsDetail(iface, start, end, info);
319     if (!reply.WriteInt32(ret)) {
320         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
321     }
322     if (ret == NETMANAGER_SUCCESS) {
323         if (!info.Marshalling(reply)) {
324             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
325         }
326     }
327     return NETMANAGER_SUCCESS;
328 }
329 
OnGetUidStatsDetail(MessageParcel & data,MessageParcel & reply)330 int32_t NetStatsServiceStub::OnGetUidStatsDetail(MessageParcel &data, MessageParcel &reply)
331 {
332     int32_t res = CheckNetManagerAvailable(reply);
333     if (res != NETMANAGER_SUCCESS) {
334         return res;
335     }
336 
337     std::string iface;
338     uint32_t uid = 0;
339     uint64_t start = 0;
340     uint64_t end = 0;
341     if (!(data.ReadString(iface) && data.ReadUint32(uid) && data.ReadUint64(start) && data.ReadUint64(end))) {
342         return NETMANAGER_ERR_READ_DATA_FAIL;
343     }
344     NetStatsInfo info;
345     int32_t ret = GetUidStatsDetail(iface, uid, start, end, info);
346     if (!reply.WriteInt32(ret)) {
347         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
348     }
349     if (ret == NETMANAGER_SUCCESS) {
350         if (!info.Marshalling(reply)) {
351             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
352         }
353     }
354     return NETMANAGER_SUCCESS;
355 }
356 
OnUpdateIfacesStats(MessageParcel & data,MessageParcel & reply)357 int32_t NetStatsServiceStub::OnUpdateIfacesStats(MessageParcel &data, MessageParcel &reply)
358 {
359     int32_t ret = CheckNetManagerAvailable(reply);
360     if (ret != NETMANAGER_SUCCESS) {
361         return ret;
362     }
363 
364     std::string iface;
365     uint64_t start = 0;
366     uint64_t end = 0;
367     if (!(data.ReadString(iface) && data.ReadUint64(start) && data.ReadUint64(end))) {
368         return NETMANAGER_ERR_READ_DATA_FAIL;
369     }
370 
371     NetStatsInfo infos;
372     if (!NetStatsInfo::Unmarshalling(data, infos)) {
373         return NETMANAGER_ERR_READ_DATA_FAIL;
374     }
375 
376     ret = UpdateIfacesStats(iface, start, end, infos);
377     if (!reply.WriteInt32(ret)) {
378         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
379     }
380     return NETMANAGER_SUCCESS;
381 }
382 
OnUpdateStatsData(MessageParcel & data,MessageParcel & reply)383 int32_t NetStatsServiceStub::OnUpdateStatsData(MessageParcel &data, MessageParcel &reply)
384 {
385     if (!NetManagerPermission::IsSystemCaller()) {
386         NETMGR_LOG_E("Permission check failed.");
387         if (!reply.WriteInt32(NETMANAGER_ERR_NOT_SYSTEM_CALL)) {
388             return IPC_STUB_WRITE_PARCEL_ERR;
389         }
390         return NETMANAGER_ERR_NOT_SYSTEM_CALL;
391     }
392     if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
393         if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
394             return IPC_STUB_WRITE_PARCEL_ERR;
395         }
396         return NETMANAGER_ERR_PERMISSION_DENIED;
397     }
398 
399     int32_t ret = UpdateStatsData();
400     if (!reply.WriteInt32(ret)) {
401         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
402     }
403     return NETMANAGER_SUCCESS;
404 }
405 
OnResetFactory(MessageParcel & data,MessageParcel & reply)406 int32_t NetStatsServiceStub::OnResetFactory(MessageParcel &data, MessageParcel &reply)
407 {
408     int32_t res = CheckNetManagerAvailable(reply);
409     if (res != NETMANAGER_SUCCESS) {
410         return res;
411     }
412 
413     int32_t ret = ResetFactory();
414     if (!reply.WriteInt32(ret)) {
415         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
416     }
417     return NETMANAGER_SUCCESS;
418 }
419 
OnGetAllStatsInfo(MessageParcel & data,MessageParcel & reply)420 int32_t NetStatsServiceStub::OnGetAllStatsInfo(MessageParcel &data, MessageParcel &reply)
421 {
422     int32_t ret = CheckNetManagerAvailable(reply);
423     if (ret != NETMANAGER_SUCCESS) {
424         return ret;
425     }
426     std::vector<NetStatsInfo> infos;
427     int32_t result = GetAllStatsInfo(infos);
428     if (!reply.WriteInt32(result)) {
429         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
430     }
431     if (result == NETMANAGER_SUCCESS) {
432         if (!NetStatsInfo::Marshalling(reply, infos)) {
433             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
434         }
435     }
436     return NETMANAGER_SUCCESS;
437 }
438 
OnGetAllSimStatsInfo(MessageParcel & data,MessageParcel & reply)439 int32_t NetStatsServiceStub::OnGetAllSimStatsInfo(MessageParcel &data, MessageParcel &reply)
440 {
441     int32_t ret = CheckNetManagerAvailable(reply);
442     if (ret != NETMANAGER_SUCCESS) {
443         return ret;
444     }
445     std::vector<NetStatsInfo> infos;
446     int32_t result = GetAllSimStatsInfo(infos);
447     if (!reply.WriteInt32(result)) {
448         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
449     }
450     if (result == NETMANAGER_SUCCESS) {
451         if (!NetStatsInfo::Marshalling(reply, infos)) {
452             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
453         }
454     }
455     return NETMANAGER_SUCCESS;
456 }
457 
OnGetTrafficStatsByNetwork(MessageParcel & data,MessageParcel & reply)458 int32_t NetStatsServiceStub::OnGetTrafficStatsByNetwork(MessageParcel &data, MessageParcel &reply)
459 {
460     int32_t ret = CheckNetManagerAvailable(reply);
461     if (ret != NETMANAGER_SUCCESS) {
462         return ret;
463     }
464     sptr<NetStatsNetwork> network = NetStatsNetwork::Unmarshalling(data);
465     if (network == nullptr) {
466         NETMGR_LOG_E("network is null");
467         return NETMANAGER_ERR_INVALID_PARAMETER;
468     }
469     if (network->startTime_ > network->endTime_) {
470         NETMGR_LOG_E("network is invalid");
471         return NETMANAGER_ERR_INVALID_PARAMETER;
472     }
473     if (network->type_ > static_cast<uint32_t>(BEARER_DEFAULT)) {
474         NETMGR_LOG_E("network is invalid");
475         return NETMANAGER_ERR_INVALID_PARAMETER;
476     }
477     std::unordered_map<uint32_t, NetStatsInfo> infos;
478     int32_t result = GetTrafficStatsByNetwork(infos, network);
479     if (!reply.WriteInt32(result)) {
480         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
481     }
482     if (result == NETMANAGER_SUCCESS) {
483         if (!NetStatsInfo::Marshalling(reply, infos)) {
484             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
485         }
486     }
487     return NETMANAGER_SUCCESS;
488 }
489 
OnGetTrafficStatsByUidNetwork(MessageParcel & data,MessageParcel & reply)490 int32_t NetStatsServiceStub::OnGetTrafficStatsByUidNetwork(MessageParcel &data, MessageParcel &reply)
491 {
492     int32_t ret = CheckNetManagerAvailable(reply);
493     if (ret != NETMANAGER_SUCCESS) {
494         return ret;
495     }
496     uint32_t uid = 0;
497     if (!data.ReadUint32(uid)) {
498         return NETMANAGER_ERR_READ_DATA_FAIL;
499     }
500     sptr<NetStatsNetwork> network = NetStatsNetwork::Unmarshalling(data);
501     if (network == nullptr) {
502         NETMGR_LOG_E("network is null");
503         return NETMANAGER_ERR_INVALID_PARAMETER;
504     }
505     if (network->startTime_ > network->endTime_) {
506         NETMGR_LOG_E("network is invalid");
507         return NETMANAGER_ERR_INVALID_PARAMETER;
508     }
509     if (network->type_ > static_cast<uint32_t>(BEARER_DEFAULT)) {
510         NETMGR_LOG_E("network is invalid");
511         return NETMANAGER_ERR_INVALID_PARAMETER;
512     }
513     std::vector<NetStatsInfoSequence> infos;
514     int32_t result = GetTrafficStatsByUidNetwork(infos, uid, network);
515     if (!reply.WriteInt32(result)) {
516         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
517     }
518     if (result == NETMANAGER_SUCCESS) {
519         if (!NetStatsInfoSequence::Marshalling(reply, infos)) {
520             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
521         }
522     }
523     return NETMANAGER_SUCCESS;
524 }
525 
OnSetAppStats(MessageParcel & data,MessageParcel & reply)526 int32_t NetStatsServiceStub::OnSetAppStats(MessageParcel &data, MessageParcel &reply)
527 {
528     int32_t ret = CheckNetManagerAvailable(reply);
529     if (ret != NETMANAGER_SUCCESS) {
530         return ret;
531     }
532     PushStatsInfo info;
533     if (!PushStatsInfo::Unmarshalling(data, info)) {
534         return NETMANAGER_ERR_READ_DATA_FAIL;
535     }
536     int32_t result = SetAppStats(info);
537     if (!reply.WriteInt32(result)) {
538         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
539     }
540     return NETMANAGER_SUCCESS;
541 }
542 
CheckNetManagerAvailable(MessageParcel & reply)543 int32_t NetStatsServiceStub::CheckNetManagerAvailable(MessageParcel &reply)
544 {
545     if (!NetManagerPermission::IsSystemCaller()) {
546         NETMGR_LOG_E("Permission check failed.");
547         if (!reply.WriteInt32(NETMANAGER_ERR_NOT_SYSTEM_CALL)) {
548             return IPC_STUB_WRITE_PARCEL_ERR;
549         }
550         return NETMANAGER_ERR_NOT_SYSTEM_CALL;
551     }
552     if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_STATS)) {
553         if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
554             return IPC_STUB_WRITE_PARCEL_ERR;
555         }
556         return NETMANAGER_ERR_PERMISSION_DENIED;
557     }
558 
559     return NETMANAGER_SUCCESS;
560 }
561 
OnGetCookieRxBytes(MessageParcel & data,MessageParcel & reply)562 int32_t NetStatsServiceStub::OnGetCookieRxBytes(MessageParcel &data, MessageParcel &reply)
563 {
564     uint64_t cookie = 0;
565     uint64_t stats = 0;
566     if (!data.ReadUint64(cookie)) {
567         NETMGR_LOG_E("ReadUint64 failed");
568         return NETMANAGER_ERR_READ_DATA_FAIL;
569     }
570 
571     int32_t result = GetCookieRxBytes(stats, cookie);
572     if (!reply.WriteInt32(result)) {
573         NETMGR_LOG_E("WriteInt32 failed");
574         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
575     }
576     if (result == NETMANAGER_SUCCESS) {
577         if (!reply.WriteUint64(stats)) {
578             NETMGR_LOG_E("WriteUint64 failed");
579             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
580         }
581     }
582     return NETMANAGER_SUCCESS;
583 }
584 
OnGetCookieTxBytes(MessageParcel & data,MessageParcel & reply)585 int32_t NetStatsServiceStub::OnGetCookieTxBytes(MessageParcel &data, MessageParcel &reply)
586 {
587     uint64_t cookie = 0;
588     uint64_t stats = 0;
589     if (!data.ReadUint64(cookie)) {
590         NETMGR_LOG_E("ReadUint64 failed");
591         return NETMANAGER_ERR_READ_DATA_FAIL;
592     }
593 
594     int32_t result = GetCookieTxBytes(stats, cookie);
595     if (!reply.WriteInt32(result)) {
596         NETMGR_LOG_E("WriteInt32 failed");
597         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
598     }
599     if (result == NETMANAGER_SUCCESS) {
600         if (!reply.WriteUint64(stats)) {
601             NETMGR_LOG_E("WriteUint64 failed");
602             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
603         }
604     }
605     return NETMANAGER_SUCCESS;
606 }
607 } // namespace NetManagerStandard
608 } // namespace OHOS
609