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