1 /*
2  * Copyright (c) 2022 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_adj_info.h"
17 
18 namespace OHOS {
19 namespace NetManagerStandard {
20 static constexpr uint32_t MAX_ADDR_SIZE = 16;
Marshalling(Parcel & parcel) const21 bool NetAdjInfo::Marshalling(Parcel &parcel) const
22 {
23     // Write type_
24     if (!parcel.WriteUint32(type_)) {
25         return false;
26     }
27 
28     // WRite identity_
29     if (!parcel.WriteString(identity_)) {
30         return false;
31     }
32 
33     // Write fromIface_
34     if (!parcel.WriteString(fromIface_)) {
35         return false;
36     }
37 
38     // Write adjQoeLevel_
39     if (!parcel.WriteUint32(adjQoeLevel_)) {
40         return false;
41     }
42 
43     // Write netAddrList_
44     if (!parcel.WriteUint32(netAddrList_.size())) {
45         return false;
46     }
47     for (const auto &addr : netAddrList_) {
48         if (!addr.Marshalling(parcel)) {
49             return false;
50         }
51     }
52 
53     return true;
54 }
55 
Unmarshalling(Parcel & parcel)56 sptr<NetAdjInfo> NetAdjInfo::Unmarshalling(Parcel &parcel)
57 {
58     sptr<NetAdjInfo> info = new (std::nothrow) NetAdjInfo();
59     if (info != nullptr) {
60         uint32_t size = 0;
61         if (!parcel.ReadUint32(info->type_) || !parcel.ReadString(info->identity_) ||
62             !parcel.ReadString(info->fromIface_) || !parcel.ReadUint32(info->adjQoeLevel_)) {
63             return nullptr;
64         }
65         // Read netAddrList_
66         if (!parcel.ReadUint32(size)) {
67             return nullptr;
68         }
69         size = size > MAX_ADDR_SIZE ? MAX_ADDR_SIZE : size;
70         sptr<INetAddr> netAddr;
71         for (uint32_t i = 0; i < size; i++) {
72             netAddr = INetAddr::Unmarshalling(parcel);
73             if (netAddr == nullptr) {
74                 return nullptr;
75             }
76             info->netAddrList_.push_back(*netAddr);
77         }
78     }
79     return info;
80 }
81 
operator ==(const NetAdjInfo & rhs) const82 bool NetAdjInfo::operator==(const NetAdjInfo &rhs) const
83 {
84     if (this == &rhs) {
85         return true;
86     }
87     return type_ == rhs.type_ && identity_ == rhs.identity_ && fromIface_ == rhs.fromIface_ &&
88            adjQoeLevel_ == rhs.adjQoeLevel_ && netAddrList_ == rhs.netAddrList_;
89 }
90 
operator !=(const NetAdjInfo & rhs) const91 bool NetAdjInfo::operator!=(const NetAdjInfo &rhs) const
92 {
93     return !(rhs == *this);
94 }
95 } // namespace NetManagerStandard
96 } // namespace OHOS