• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "net_mgr_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace NetManagerStandard {
22 static constexpr uint32_t MAX_NET_CAP_NUM = 32;
23 
CapsIsValid() const24 bool NetAllCapabilities::CapsIsValid() const
25 {
26     for (auto it = netCaps_.begin(); it != netCaps_.end(); it++) {
27         if ((*it < NET_CAPABILITY_MMS) || (*it >= NET_CAPABILITY_INTERNAL_DEFAULT)) {
28             return false;
29         }
30     }
31     for (auto it = bearerTypes_.begin(); it != bearerTypes_.end(); it++) {
32         if ((*it < BEARER_CELLULAR) || (*it >= BEARER_DEFAULT)) {
33             return false;
34         }
35     }
36     return true;
37 }
38 
CapsIsNull() const39 bool NetAllCapabilities::CapsIsNull() const
40 {
41     if ((linkUpBandwidthKbps_ == 0) && (linkDownBandwidthKbps_ == 0) && (netCaps_.size() == 0) &&
42         (bearerTypes_.size() == 0)) {
43         return true;
44     }
45     return false;
46 }
47 
Marshalling(Parcel & parcel) const48 bool NetAllCapabilities::Marshalling(Parcel &parcel) const
49 {
50     if (!parcel.WriteUint32(linkUpBandwidthKbps_) || !parcel.WriteUint32(linkDownBandwidthKbps_)) {
51         return false;
52     }
53     uint32_t capSize = netCaps_.size();
54     capSize = capSize > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : capSize;
55     if (!parcel.WriteUint32(capSize)) {
56         return false;
57     }
58     int32_t index = 0;
59     for (auto it = netCaps_.begin(); it != netCaps_.end(); it++) {
60         if (++index > MAX_NET_CAP_NUM) {
61             break;
62         }
63         if (!parcel.WriteUint32(static_cast<uint32_t>(*it))) {
64             return false;
65         }
66     }
67     uint32_t typeSize = bearerTypes_.size();
68     typeSize = typeSize > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : typeSize;
69     if (!parcel.WriteUint32(typeSize)) {
70         return false;
71     }
72     index = 0;
73     for (auto it = bearerTypes_.begin(); it != bearerTypes_.end(); it++) {
74         if (++index > MAX_NET_CAP_NUM) {
75             break;
76         }
77         if (!parcel.WriteUint32(static_cast<uint32_t>(*it))) {
78             return false;
79         }
80     }
81     return true;
82 }
83 
Unmarshalling(Parcel & parcel)84 bool NetAllCapabilities::Unmarshalling(Parcel &parcel)
85 {
86     if (!parcel.ReadUint32(linkUpBandwidthKbps_)) {
87         return false;
88     }
89     if (!parcel.ReadUint32(linkDownBandwidthKbps_)) {
90         return false;
91     }
92     uint32_t size = 0;
93     if (!parcel.ReadUint32(size)) {
94         return false;
95     }
96     size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
97     uint32_t cap = 0;
98     for (uint32_t i = 0; i < size; i++) {
99         if (!parcel.ReadUint32(cap)) {
100             return false;
101         }
102         if (cap >= NET_CAPABILITY_INTERNAL_DEFAULT) {
103             continue;
104         }
105         netCaps_.insert(static_cast<NetCap>(cap));
106     }
107     if (!parcel.ReadUint32(size)) {
108         return false;
109     }
110     size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
111     uint32_t type = 0;
112     for (uint32_t i = 0; i < size; i++) {
113         if (!parcel.ReadUint32(type)) {
114             return false;
115         }
116         if (type >= BEARER_DEFAULT) {
117             continue;
118         }
119         bearerTypes_.insert(static_cast<NetBearType>(type));
120     }
121     return true;
122 }
123 
ToString(const std::string & tab) const124 std::string NetAllCapabilities::ToString(const std::string &tab) const
125 {
126     std::string str;
127     str.append("\n");
128     str.append(tab);
129     str.append("[NetAllCapabilities]");
130 
131     str.append("\n");
132     str.append(tab);
133     str.append("linkUpBandwidthKbps_ = ");
134     str.append(std::to_string(linkUpBandwidthKbps_));
135 
136     str.append("\n");
137     str.append(tab);
138     str.append("linkDownBandwidthKbps_ = ");
139     str.append(std::to_string(linkDownBandwidthKbps_));
140 
141     str.append("\n");
142     str.append(tab);
143     ToStrNetCaps(netCaps_, str);
144 
145     str.append("\n");
146     str.append(tab);
147     ToStrNetBearTypes(bearerTypes_, str);
148 
149     return str;
150 }
151 
ToStrNetCaps(const std::set<NetCap> & netCaps,std::string & str) const152 void NetAllCapabilities::ToStrNetCaps(const std::set<NetCap> &netCaps, std::string &str) const
153 {
154     str.append("netCaps_ =");
155     for (auto netCap : netCaps) {
156         str.append(" ");
157         switch (netCap) {
158             case NET_CAPABILITY_MMS:
159                 str.append("NET_CAPABILITY_MMS");
160                 break;
161             case NET_CAPABILITY_NOT_METERED:
162                 str.append("NET_CAPABILITY_NOT_METERED");
163                 break;
164             case NET_CAPABILITY_INTERNET:
165                 str.append("NET_CAPABILITY_INTERNET");
166                 break;
167             case NET_CAPABILITY_NOT_VPN:
168                 str.append("NET_CAPABILITY_NOT_VPN");
169                 break;
170             case NET_CAPABILITY_VALIDATED:
171                 str.append("NET_CAPABILITY_VALIDATED");
172                 break;
173             case NET_CAPABILITY_CAPTIVE_PORTAL:
174                 str.append("NET_CAPABILITY_CAPTIVE_PORTAL");
175                 break;
176             default:
177                 str.append("unknown NetCap");
178                 break;
179         }
180     }
181 }
182 
ToStrNetBearTypes(const std::set<NetBearType> & bearerTypes,std::string & str) const183 void NetAllCapabilities::ToStrNetBearTypes(const std::set<NetBearType> &bearerTypes, std::string &str) const
184 {
185     str.append("NetBearType =");
186     for (auto bearerType : bearerTypes) {
187         str.append(" ");
188         switch (bearerType) {
189             case BEARER_CELLULAR:
190                 str.append("BEARER_CELLULAR");
191                 break;
192             case BEARER_WIFI:
193                 str.append("BEARER_WIFI");
194                 break;
195             case BEARER_ETHERNET:
196                 str.append("BEARER_ETHERNET");
197                 break;
198             case BEARER_VPN:
199                 str.append("BEARER_VPN");
200                 break;
201             case BEARER_WIFI_AWARE:
202                 str.append("BEARER_WIFI_AWARE");
203                 break;
204             default:
205                 str.append("unknown NetBearType");
206                 break;
207         }
208     }
209 }
210 } // namespace NetManagerStandard
211 } // namespace OHOS