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_info.h"
17 #include "net_mgr_log_wrapper.h"
18 #include "parcel.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
22 static constexpr uint32_t STATS_INFO_MAX_SIZE = 5000;
23
Marshalling(Parcel & parcel) const24 bool NetStatsInfo::Marshalling(Parcel &parcel) const
25 {
26 if (!parcel.WriteUint32(uid_)) {
27 return false;
28 }
29 if (!parcel.WriteString(iface_)) {
30 return false;
31 }
32 if (!parcel.WriteUint64(date_)) {
33 return false;
34 }
35 if (!parcel.WriteUint64(rxBytes_)) {
36 return false;
37 }
38 if (!parcel.WriteUint64(txBytes_)) {
39 return false;
40 }
41 if (!parcel.WriteUint64(rxPackets_)) {
42 return false;
43 }
44 if (!parcel.WriteUint64(txPackets_)) {
45 return false;
46 }
47 return true;
48 }
49
Marshalling(Parcel & parcel,const NetStatsInfo & stats)50 bool NetStatsInfo::Marshalling(Parcel &parcel, const NetStatsInfo &stats)
51 {
52 if (!parcel.WriteUint32(stats.uid_)) {
53 return false;
54 }
55 if (!parcel.WriteString(stats.iface_)) {
56 return false;
57 }
58 if (!parcel.WriteUint64(stats.date_)) {
59 return false;
60 }
61 if (!parcel.WriteUint64(stats.rxBytes_)) {
62 return false;
63 }
64 if (!parcel.WriteUint64(stats.txBytes_)) {
65 return false;
66 }
67 if (!parcel.WriteUint64(stats.rxPackets_)) {
68 return false;
69 }
70 if (!parcel.WriteUint64(stats.txPackets_)) {
71 return false;
72 }
73 return true;
74 }
75
Marshalling(Parcel & parcel,const std::vector<NetStatsInfo> & statsInfos)76 bool NetStatsInfo::Marshalling(Parcel &parcel, const std::vector<NetStatsInfo> &statsInfos)
77 {
78 uint32_t vSize = statsInfos.size();
79 if (!parcel.WriteUint32(vSize)) {
80 return false;
81 }
82 if (vSize > STATS_INFO_MAX_SIZE) {
83 NETMGR_LOG_E("Size of the statsInfos exceeds maximum.");
84 return false;
85 }
86
87 std::for_each(statsInfos.begin(), statsInfos.end(), [&parcel](const auto &info) { info.Marshalling(parcel); });
88 return true;
89 }
90
Unmarshalling(Parcel & parcel,std::vector<NetStatsInfo> & statsInfos)91 bool NetStatsInfo::Unmarshalling(Parcel &parcel, std::vector<NetStatsInfo> &statsInfos)
92 {
93 uint32_t vSize = 0;
94 if (!parcel.ReadUint32(vSize)) {
95 return false;
96 }
97
98 if (vSize > STATS_INFO_MAX_SIZE) {
99 NETMGR_LOG_E("Size of the statsInfos exceeds maximum.");
100 return false;
101 }
102 statsInfos.reserve(vSize);
103 for (uint32_t i = 0; i < vSize; i++) {
104 NetStatsInfo tmpData;
105 NetStatsInfo::Unmarshalling(parcel, tmpData);
106 statsInfos.push_back(tmpData);
107 }
108
109 return true;
110 }
111
Unmarshalling(Parcel & parcel,NetStatsInfo & stats)112 bool NetStatsInfo::Unmarshalling(Parcel &parcel, NetStatsInfo &stats)
113 {
114 if (!parcel.ReadUint32(stats.uid_)) {
115 return false;
116 }
117 if (!parcel.ReadString(stats.iface_)) {
118 return false;
119 }
120 if (!parcel.ReadUint64(stats.date_)) {
121 return false;
122 }
123 if (!parcel.ReadUint64(stats.rxBytes_)) {
124 return false;
125 }
126 if (!parcel.ReadUint64(stats.txBytes_)) {
127 return false;
128 }
129 if (!parcel.ReadUint64(stats.rxPackets_)) {
130 return false;
131 }
132 if (!parcel.ReadUint64(stats.txPackets_)) {
133 return false;
134 }
135 return true;
136 }
137 } // namespace NetManagerStandard
138 } // namespace OHOS
139