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_specifier.h" 17 18 #include "net_mgr_log_wrapper.h" 19 20 namespace OHOS { 21 namespace NetManagerStandard { SpecifierIsValid() const22bool NetSpecifier::SpecifierIsValid() const 23 { 24 if (ident_.empty() && netCapabilities_.CapsIsNull()) { 25 return false; 26 } 27 return netCapabilities_.CapsIsValid(); 28 } 29 SetCapabilities(const std::set<NetCap> & netCaps)30void NetSpecifier::SetCapabilities(const std::set<NetCap> &netCaps) 31 { 32 netCapabilities_.netCaps_ = netCaps; 33 } 34 SetCapability(NetCap netCap)35void NetSpecifier::SetCapability(NetCap netCap) 36 { 37 netCapabilities_.netCaps_.clear(); 38 netCapabilities_.netCaps_.insert(netCap); 39 } 40 SetTypes(const std::set<NetBearType> & bearerTypes)41void NetSpecifier::SetTypes(const std::set<NetBearType> &bearerTypes) 42 { 43 netCapabilities_.bearerTypes_ = bearerTypes; 44 } 45 SetType(NetBearType bearerType)46void NetSpecifier::SetType(NetBearType bearerType) 47 { 48 netCapabilities_.bearerTypes_.clear(); 49 netCapabilities_.bearerTypes_.insert(bearerType); 50 } 51 Marshalling(Parcel & parcel) const52bool NetSpecifier::Marshalling(Parcel &parcel) const 53 { 54 if (!parcel.WriteString(ident_)) { 55 return false; 56 } 57 return netCapabilities_.Marshalling(parcel); 58 } 59 Unmarshalling(Parcel & parcel)60sptr<NetSpecifier> NetSpecifier::Unmarshalling(Parcel &parcel) 61 { 62 sptr<NetSpecifier> ptr = (std::make_unique<NetSpecifier>()).release(); 63 if (ptr == nullptr) { 64 NETMGR_LOG_E("make_unique<NetSpecifier>() failed"); 65 return nullptr; 66 } 67 if (!parcel.ReadString(ptr->ident_)) { 68 return nullptr; 69 } 70 if (!ptr->netCapabilities_.Unmarshalling(parcel)) { 71 return nullptr; 72 } 73 return ptr; 74 } 75 Marshalling(Parcel & parcel,const sptr<NetSpecifier> & object)76bool NetSpecifier::Marshalling(Parcel &parcel, const sptr<NetSpecifier> &object) 77 { 78 if (object == nullptr) { 79 NETMGR_LOG_E("NetSpecifier object ptr is nullptr"); 80 return false; 81 } 82 if (!parcel.WriteString(object->ident_)) { 83 return false; 84 } 85 return object->netCapabilities_.Marshalling(parcel); 86 } 87 ToString(const std::string & tab) const88std::string NetSpecifier::ToString(const std::string &tab) const 89 { 90 std::string str; 91 str.append("\n"); 92 str.append(tab); 93 str.append("[NetSpecifier]"); 94 95 str.append("\n"); 96 str.append(tab); 97 str.append("ident_ = "); 98 str.append(ident_); 99 100 str.append(netCapabilities_.ToString(tab)); 101 102 return str; 103 } 104 } // namespace NetManagerStandard 105 } // namespace OHOS