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 "network_search_result.h"
17 
18 #include <securec.h>
19 
20 #include "telephony_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace Telephony {
24 
25 const int32_t AVAILABLE_NETWORK_LIST_LENGTH = 50;
26 
NetworkSearchResult()27 NetworkSearchResult::NetworkSearchResult() {}
28 
SetNetworkSearchResultValue(int32_t listSize,const std::vector<NetworkInformation> & operatorInfo)29 void NetworkSearchResult::SetNetworkSearchResultValue(
30     int32_t listSize, const std::vector<NetworkInformation> &operatorInfo)
31 {
32     listSize_ = listSize;
33     operatorInfoList_ = operatorInfo;
34     TELEPHONY_LOGI("NetworkSearchResult::SetNetworkSearchResultValue size:%{public}d, %{public}zu)", listSize_,
35         operatorInfoList_.size());
36 }
37 
GetNetworkSearchInformation() const38 std::vector<NetworkInformation> NetworkSearchResult::GetNetworkSearchInformation() const
39 {
40     return operatorInfoList_;
41 }
42 
GetNetworkSearchInformationSize() const43 int32_t NetworkSearchResult::GetNetworkSearchInformationSize() const
44 {
45     return listSize_;
46 }
47 
ReadFromParcel(Parcel & parcel)48 bool NetworkSearchResult::ReadFromParcel(Parcel &parcel)
49 {
50     int32_t rat;
51     if (!parcel.ReadInt32(rat)) {
52         return false;
53     }
54     listSize_ = rat;
55     TELEPHONY_LOGI("ReadParcelable<NetworkState> %{public}d", listSize_);
56     if (listSize_ > AVAILABLE_NETWORK_LIST_LENGTH) {
57         return false;
58     }
59     for (int32_t i = 0; i < listSize_; i++) {
60         std::unique_ptr<NetworkInformation> networkInfo(parcel.ReadParcelable<NetworkInformation>());
61         if (networkInfo == nullptr) {
62             TELEPHONY_LOGE("ReadParcelable<NetworkState> failed");
63             return false;
64         }
65         operatorInfoList_.emplace_back(*networkInfo);
66     }
67     return true;
68 }
69 
Marshalling(Parcel & parcel) const70 bool NetworkSearchResult::Marshalling(Parcel &parcel) const
71 {
72     if (!parcel.WriteInt32(listSize_)) {
73         TELEPHONY_LOGE("NetworkSearchResult::Marshalling WriteInt32 failed");
74         return false;
75     }
76     TELEPHONY_LOGI("ReadParcelable<NetworkState> size:%{public}d", listSize_);
77     for (auto &networkState : operatorInfoList_) {
78         parcel.WriteParcelable(&networkState);
79     }
80     return true;
81 }
82 
Unmarshalling(Parcel & parcel)83 NetworkSearchResult *NetworkSearchResult::Unmarshalling(Parcel &parcel)
84 {
85     std::unique_ptr<NetworkSearchResult> param = std::make_unique<NetworkSearchResult>();
86     if (param == nullptr) {
87         return nullptr;
88     }
89     if (!param->ReadFromParcel(parcel)) {
90         return nullptr;
91     }
92     return param.release();
93 }
94 } // namespace Telephony
95 } // namespace OHOS
96