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_information.h"
17
18 #include <cstdint>
19
20 #include "iosfwd"
21 #include "memory"
22 #include "parcel.h"
23 #include "string"
24 #include "string_ex.h"
25
26 namespace OHOS {
27 namespace Telephony {
SetOperateInformation(const std::string & operatorLongName,const std::string & operatorShortName,const std::string & operatorNumeric,int32_t state,int32_t rat)28 void NetworkInformation::SetOperateInformation(const std::string &operatorLongName,
29 const std::string &operatorShortName, const std::string &operatorNumeric, int32_t state, int32_t rat)
30 {
31 operatorLongName_ = operatorLongName;
32 operatorShortName_ = operatorShortName;
33 operatorNumeric_ = operatorNumeric;
34 networkPlmnState_ = static_cast<NetworkPlmnState>(state);
35 rat_ = static_cast<NetworkRat>(rat);
36 }
37
GetNetworkState() const38 int32_t NetworkInformation::GetNetworkState() const
39 {
40 return static_cast<int32_t>(networkPlmnState_);
41 }
42
GetOperatorShortName() const43 std::string NetworkInformation::GetOperatorShortName() const
44 {
45 return operatorShortName_;
46 }
47
GetOperatorLongName() const48 std::string NetworkInformation::GetOperatorLongName() const
49 {
50 return operatorLongName_;
51 }
52
GetOperatorNumeric() const53 std::string NetworkInformation::GetOperatorNumeric() const
54 {
55 return operatorNumeric_;
56 }
57
GetRadioTech() const58 int32_t NetworkInformation::GetRadioTech() const
59 {
60 return static_cast<int32_t>(rat_);
61 }
62
ReadFromParcel(Parcel & parcel)63 bool NetworkInformation::ReadFromParcel(Parcel &parcel)
64 {
65 operatorLongName_ = Str16ToStr8(parcel.ReadString16());
66 operatorShortName_ = Str16ToStr8(parcel.ReadString16());
67 operatorNumeric_ = Str16ToStr8(parcel.ReadString16());
68
69 int32_t plmnState;
70 if (!parcel.ReadInt32(plmnState)) {
71 return false;
72 }
73 networkPlmnState_ = static_cast<NetworkPlmnState>(plmnState);
74
75 int32_t rat;
76 if (!parcel.ReadInt32(rat)) {
77 return false;
78 }
79 rat_ = static_cast<NetworkRat>(rat);
80 return true;
81 }
82
Marshalling(Parcel & parcel) const83 bool NetworkInformation::Marshalling(Parcel &parcel) const
84 {
85 if (!parcel.WriteString16(Str8ToStr16(operatorLongName_))) {
86 return false;
87 }
88 if (!parcel.WriteString16(Str8ToStr16(operatorShortName_))) {
89 return false;
90 }
91 if (!parcel.WriteString16(Str8ToStr16(operatorNumeric_))) {
92 return false;
93 }
94 if (!parcel.WriteInt32(static_cast<int32_t>(networkPlmnState_))) {
95 return false;
96 }
97 if (!parcel.WriteInt32(static_cast<int32_t>(rat_))) {
98 return false;
99 }
100 return true;
101 }
102
Unmarshalling(Parcel & parcel)103 NetworkInformation *NetworkInformation::Unmarshalling(Parcel &parcel)
104 {
105 std::unique_ptr<NetworkInformation> networkInfo = std::make_unique<NetworkInformation>();
106 if (networkInfo == nullptr) {
107 return nullptr;
108 }
109 if (!networkInfo->ReadFromParcel(parcel)) {
110 return nullptr;
111 }
112 return networkInfo.release();
113 }
114 } // namespace Telephony
115 } // namespace OHOS
116