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_all_capabilities.h"
17 
18 #include "__tree"
19 #include <functional>
20 
21 #include "parcel.h"
22 
23 namespace OHOS {
24 namespace NetManagerStandard {
25 static constexpr uint32_t MAX_NET_CAP_NUM = 32;
26 
NetAllCapabilities(const NetAllCapabilities & cap)27 NetAllCapabilities::NetAllCapabilities(const NetAllCapabilities &cap)
28 {
29     linkUpBandwidthKbps_ = cap.linkUpBandwidthKbps_;
30     linkDownBandwidthKbps_ = cap.linkDownBandwidthKbps_;
31     netCaps_ = cap.netCaps_;
32     bearerTypes_ = cap.bearerTypes_;
33 }
34 
operator =(const NetAllCapabilities & cap)35 NetAllCapabilities &NetAllCapabilities::operator=(const NetAllCapabilities &cap)
36 {
37     linkUpBandwidthKbps_ = cap.linkUpBandwidthKbps_;
38     linkDownBandwidthKbps_ = cap.linkDownBandwidthKbps_;
39     netCaps_ = cap.netCaps_;
40     bearerTypes_ = cap.bearerTypes_;
41     return *this;
42 }
43 
CapsIsValid() const44 bool NetAllCapabilities::CapsIsValid() const
45 {
46     for (auto it = netCaps_.begin(); it != netCaps_.end(); it++) {
47         if (*it < NET_CAPABILITY_MMS || *it >= NET_CAPABILITY_END) {
48             return false;
49         }
50     }
51     for (auto it = bearerTypes_.begin(); it != bearerTypes_.end(); it++) {
52         if ((*it < BEARER_CELLULAR) || (*it >= BEARER_DEFAULT)) {
53             return false;
54         }
55     }
56     return true;
57 }
58 
CapsIsNull() const59 bool NetAllCapabilities::CapsIsNull() const
60 {
61     if ((linkUpBandwidthKbps_ == 0) && (linkDownBandwidthKbps_ == 0) && (netCaps_.size() == 0) &&
62         (bearerTypes_.size() == 0)) {
63         return true;
64     }
65     return false;
66 }
67 
Marshalling(Parcel & parcel) const68 bool NetAllCapabilities::Marshalling(Parcel &parcel) const
69 {
70     if (!parcel.WriteUint32(linkUpBandwidthKbps_) || !parcel.WriteUint32(linkDownBandwidthKbps_)) {
71         return false;
72     }
73     uint32_t capSize = netCaps_.size();
74     capSize = capSize > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : capSize;
75     if (!parcel.WriteUint32(capSize)) {
76         return false;
77     }
78     uint32_t index = 0;
79     for (auto it = netCaps_.begin(); it != netCaps_.end(); it++) {
80         if (++index > MAX_NET_CAP_NUM) {
81             break;
82         }
83         if (!parcel.WriteUint32(static_cast<uint32_t>(*it))) {
84             return false;
85         }
86     }
87     uint32_t typeSize = bearerTypes_.size();
88     typeSize = typeSize > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : typeSize;
89     if (!parcel.WriteUint32(typeSize)) {
90         return false;
91     }
92     index = 0;
93     for (auto it = bearerTypes_.begin(); it != bearerTypes_.end(); it++) {
94         if (++index > MAX_NET_CAP_NUM) {
95             break;
96         }
97         if (!parcel.WriteUint32(static_cast<uint32_t>(*it))) {
98             return false;
99         }
100     }
101     return true;
102 }
103 
Unmarshalling(Parcel & parcel)104 bool NetAllCapabilities::Unmarshalling(Parcel &parcel)
105 {
106     if (!parcel.ReadUint32(linkUpBandwidthKbps_)) {
107         return false;
108     }
109     if (!parcel.ReadUint32(linkDownBandwidthKbps_)) {
110         return false;
111     }
112     uint32_t size = 0;
113     if (!parcel.ReadUint32(size)) {
114         return false;
115     }
116     size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
117     uint32_t cap = 0;
118     for (uint32_t i = 0; i < size; i++) {
119         if (!parcel.ReadUint32(cap)) {
120             return false;
121         }
122         if (cap >= NET_CAPABILITY_END) {
123             continue;
124         }
125         netCaps_.insert(static_cast<NetCap>(cap));
126     }
127     if (!parcel.ReadUint32(size)) {
128         return false;
129     }
130     size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
131     uint32_t type = 0;
132     for (uint32_t i = 0; i < size; i++) {
133         if (!parcel.ReadUint32(type)) {
134             return false;
135         }
136         if (type >= BEARER_DEFAULT) {
137             continue;
138         }
139         bearerTypes_.insert(static_cast<NetBearType>(type));
140     }
141     return true;
142 }
143 
ToString(const std::string & tab) const144 std::string NetAllCapabilities::ToString(const std::string &tab) const
145 {
146     std::string str;
147     str.append(tab);
148     str.append("[NetAllCapabilities]");
149 
150     str.append(tab);
151     str.append("linkUpBandwidthKbps_ = ");
152     str.append(std::to_string(linkUpBandwidthKbps_));
153 
154     str.append(tab);
155     str.append("linkDownBandwidthKbps_ = ");
156     str.append(std::to_string(linkDownBandwidthKbps_));
157 
158     str.append(tab);
159     ToStrNetCaps(netCaps_, str);
160 
161     str.append(tab);
162     ToStrNetBearTypes(bearerTypes_, str);
163 
164     return str;
165 }
166 
ToStrNetCaps(const std::set<NetCap> & netCaps,std::string & str) const167 void NetAllCapabilities::ToStrNetCaps(const std::set<NetCap> &netCaps, std::string &str) const
168 {
169     str.append("netCaps_ =");
170     for (auto netCap : netCaps) {
171         str.append(" ");
172         switch (netCap) {
173             case NET_CAPABILITY_MMS:
174                 str.append("NET_CAPABILITY_MMS");
175                 break;
176             case NET_CAPABILITY_SUPL:
177                 str.append("NET_CAPABILITY_SUPL");
178                 break;
179             case NET_CAPABILITY_DUN:
180                 str.append("NET_CAPABILITY_DUN");
181                 break;
182             case NET_CAPABILITY_IA:
183                 str.append("NET_CAPABILITY_IA");
184                 break;
185             case NET_CAPABILITY_XCAP:
186                 str.append("NET_CAPABILITY_XCAP");
187                 break;
188             case NET_CAPABILITY_NOT_METERED:
189                 str.append("NET_CAPABILITY_NOT_METERED");
190                 break;
191             case NET_CAPABILITY_INTERNET:
192                 str.append("NET_CAPABILITY_INTERNET");
193                 break;
194             case NET_CAPABILITY_NOT_VPN:
195                 str.append("NET_CAPABILITY_NOT_VPN");
196                 break;
197             case NET_CAPABILITY_VALIDATED:
198                 str.append("NET_CAPABILITY_VALIDATED");
199                 break;
200             case NET_CAPABILITY_PORTAL:
201                 str.append("NET_CAPABILITY_PORTAL");
202                 break;
203             case NET_CAPABILITY_INTERNAL_DEFAULT:
204                 str.append("NET_CAPABILITY_INTERNAL_DEFAULT");
205                 break;
206             case NET_CAPABILITY_CHECKING_CONNECTIVITY:
207                 str.append("NET_CAPABILITY_CHECKING_CONNECTIVITY");
208                 break;
209             default:
210                 str.append("unknown NetCap");
211                 break;
212         }
213     }
214 }
215 
ToStrNetBearTypes(const std::set<NetBearType> & bearerTypes,std::string & str) const216 void NetAllCapabilities::ToStrNetBearTypes(const std::set<NetBearType> &bearerTypes, std::string &str) const
217 {
218     str.append("NetBearType =");
219     for (auto bearerType : bearerTypes) {
220         str.append(" ");
221         switch (bearerType) {
222             case BEARER_CELLULAR:
223                 str.append("BEARER_CELLULAR");
224                 break;
225             case BEARER_WIFI:
226                 str.append("BEARER_WIFI");
227                 break;
228             case BEARER_ETHERNET:
229                 str.append("BEARER_ETHERNET");
230                 break;
231             case BEARER_VPN:
232                 str.append("BEARER_VPN");
233                 break;
234             case BEARER_WIFI_AWARE:
235                 str.append("BEARER_WIFI_AWARE");
236                 break;
237             default:
238                 str.append("unknown NetBearType");
239                 break;
240         }
241     }
242 }
243 } // namespace NetManagerStandard
244 } // namespace OHOS
245