• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_state.h"
17 
18 #include <securec.h>
19 #include "telephony_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace Telephony {
NetworkState()23 NetworkState::NetworkState()
24 {
25     Init();
26 }
27 
Init()28 void NetworkState::Init()
29 {
30     TELEPHONY_LOGI("NetworkState::Init");
31     isEmergency_ = false;
32     csRoaming_ = RoamingType::ROAMING_STATE_UNKNOWN;
33     psRoaming_ = RoamingType::ROAMING_STATE_UNKNOWN;
34     psRegStatus_ = RegServiceState::REG_STATE_UNKNOWN;
35     csRegStatus_ = RegServiceState::REG_STATE_UNKNOWN;
36     psOperatorInfo_.fullName = "";
37     csOperatorInfo_.fullName = "";
38     psOperatorInfo_.shortName = "";
39     csOperatorInfo_.shortName = "";
40     psOperatorInfo_.operatorNumeric = "";
41     csOperatorInfo_.operatorNumeric = "";
42     psRadioTech_ = RadioTech::RADIO_TECHNOLOGY_UNKNOWN;
43     csRadioTech_ = RadioTech::RADIO_TECHNOLOGY_UNKNOWN;
44     cfgTech_ = RadioTech::RADIO_TECHNOLOGY_UNKNOWN;
45     nrState_ = NrState::NR_STATE_NOT_SUPPORT;
46 }
47 
ReadFromParcel(Parcel & parcel)48 bool NetworkState::ReadFromParcel(Parcel &parcel)
49 {
50     if (!parcel.ReadBool(isEmergency_)) {
51         return false;
52     }
53 
54     std::string readString = parcel.ReadString();
55     psOperatorInfo_.fullName = readString;
56 
57     readString = parcel.ReadString();
58     psOperatorInfo_.shortName = readString;
59 
60     readString = parcel.ReadString();
61     psOperatorInfo_.operatorNumeric = readString;
62 
63     readString = parcel.ReadString();
64     csOperatorInfo_.fullName = readString;
65 
66     readString = parcel.ReadString();
67     csOperatorInfo_.shortName = readString;
68 
69     readString = parcel.ReadString();
70     csOperatorInfo_.operatorNumeric = readString;
71     csRoaming_ = static_cast<RoamingType>(parcel.ReadInt32());
72     psRoaming_ = static_cast<RoamingType>(parcel.ReadInt32());
73     psRegStatus_ = static_cast<RegServiceState>(parcel.ReadInt32());
74     csRegStatus_ = static_cast<RegServiceState>(parcel.ReadInt32());
75     psRadioTech_ = static_cast<RadioTech>(parcel.ReadInt32());
76     csRadioTech_ = static_cast<RadioTech>(parcel.ReadInt32());
77     cfgTech_ = static_cast<RadioTech>(parcel.ReadInt32());
78     nrState_ = static_cast<NrState>(parcel.ReadInt32());
79     return true;
80 }
81 
operator ==(const NetworkState & other) const82 bool NetworkState::operator==(const NetworkState &other) const
83 {
84     return isEmergency_ == other.isEmergency_ && csRoaming_ == other.csRoaming_ && psRoaming_ == other.psRoaming_ &&
85         psRegStatus_ == other.psRegStatus_ && csRegStatus_ == other.csRegStatus_ &&
86         psRadioTech_ == other.psRadioTech_ && csRadioTech_ == other.csRadioTech_ &&
87         cfgTech_ == other.cfgTech_ && nrState_ == other.nrState_ &&
88         psOperatorInfo_.operatorNumeric == other.psOperatorInfo_.operatorNumeric &&
89         psOperatorInfo_.fullName == other.psOperatorInfo_.fullName &&
90         psOperatorInfo_.shortName == other.psOperatorInfo_.shortName &&
91         csOperatorInfo_.operatorNumeric == other.csOperatorInfo_.operatorNumeric &&
92         csOperatorInfo_.fullName == other.csOperatorInfo_.fullName &&
93         csOperatorInfo_.shortName == other.csOperatorInfo_.shortName;
94 }
95 
Marshalling(Parcel & parcel) const96 bool NetworkState::Marshalling(Parcel &parcel) const
97 {
98     if (!parcel.WriteBool(isEmergency_)) {
99         return false;
100     }
101 
102     if (!parcel.WriteString(psOperatorInfo_.fullName)) {
103         return false;
104     }
105     if (!parcel.WriteString(psOperatorInfo_.shortName)) {
106         return false;
107     }
108     if (!parcel.WriteString(psOperatorInfo_.operatorNumeric)) {
109         return false;
110     }
111     if (!parcel.WriteString(csOperatorInfo_.fullName)) {
112         return false;
113     }
114     if (!parcel.WriteString(csOperatorInfo_.shortName)) {
115         return false;
116     }
117     if (!parcel.WriteString(csOperatorInfo_.operatorNumeric)) {
118         return false;
119     }
120     if (!parcel.WriteInt32(static_cast<int32_t>(csRoaming_))) {
121         return false;
122     }
123     if (!parcel.WriteInt32(static_cast<int32_t>(psRoaming_))) {
124         return false;
125     }
126     if (!parcel.WriteInt32(static_cast<int32_t>(psRegStatus_))) {
127         return false;
128     }
129     if (!parcel.WriteInt32(static_cast<int32_t>(csRegStatus_))) {
130         return false;
131     }
132     if (!parcel.WriteInt32(static_cast<int32_t>(psRadioTech_))) {
133         return false;
134     }
135     if (!parcel.WriteInt32(static_cast<int32_t>(csRadioTech_))) {
136         return false;
137     }
138     if (!parcel.WriteInt32(static_cast<int32_t>(cfgTech_))) {
139         return false;
140     }
141     if (!parcel.WriteInt32(static_cast<int32_t>(nrState_))) {
142         return false;
143     }
144     return true;
145 }
146 
Unmarshalling(Parcel & parcel)147 NetworkState *NetworkState::Unmarshalling(Parcel &parcel)
148 {
149     std::unique_ptr<NetworkState> param = std::make_unique<NetworkState>();
150     if (param == nullptr) {
151         return nullptr;
152     }
153     if (!param->ReadFromParcel(parcel)) {
154         return nullptr;
155     }
156     return param.release();
157 }
158 
GetLongOperatorName() const159 std::string NetworkState::GetLongOperatorName() const
160 {
161     if (!psOperatorInfo_.fullName.empty()) {
162         return psOperatorInfo_.fullName;
163     } else {
164         return csOperatorInfo_.fullName;
165     }
166 }
167 
GetShortOperatorName() const168 std::string NetworkState::GetShortOperatorName() const
169 {
170     if (!psOperatorInfo_.shortName.empty()) {
171         return psOperatorInfo_.shortName;
172     } else {
173         return csOperatorInfo_.shortName;
174     }
175 }
176 
GetPlmnNumeric() const177 std::string NetworkState::GetPlmnNumeric() const
178 {
179     if (!psOperatorInfo_.operatorNumeric.empty()) {
180         return psOperatorInfo_.operatorNumeric;
181     } else {
182         return csOperatorInfo_.operatorNumeric;
183     }
184 }
185 
GetRegStatus() const186 RegServiceState NetworkState::GetRegStatus() const
187 {
188     if (psRegStatus_ == RegServiceState::REG_STATE_IN_SERVICE) {
189         return psRegStatus_;
190     } else {
191         return csRegStatus_;
192     }
193 }
194 
IsEmergency() const195 bool NetworkState::IsEmergency() const
196 {
197     return isEmergency_;
198 }
199 
IsRoaming() const200 bool NetworkState::IsRoaming() const
201 {
202     if (psRoaming_ > RoamingType::ROAMING_STATE_UNKNOWN) {
203         return true;
204     } else if (csRoaming_ > RoamingType::ROAMING_STATE_UNKNOWN) {
205         return true;
206     } else {
207         return false;
208     }
209 }
210 
GetPsRadioTech() const211 RadioTech NetworkState::GetPsRadioTech() const
212 {
213     return psRadioTech_;
214 }
215 
GetCsRadioTech() const216 RadioTech NetworkState::GetCsRadioTech() const
217 {
218     return csRadioTech_;
219 }
220 
GetPsRegStatus() const221 RegServiceState NetworkState::GetPsRegStatus() const
222 {
223     return psRegStatus_;
224 }
225 
GetCsRegStatus() const226 RegServiceState NetworkState::GetCsRegStatus() const
227 {
228     return csRegStatus_;
229 }
230 
SetOperatorInfo(const std::string & longName,const std::string & shortName,const std::string & numeric,DomainType domainType)231 void NetworkState::SetOperatorInfo(
232     const std::string &longName, const std::string &shortName, const std::string &numeric, DomainType domainType)
233 {
234     if (domainType == DomainType::DOMAIN_TYPE_PS) {
235         psOperatorInfo_.fullName = longName;
236         psOperatorInfo_.shortName = shortName;
237         psOperatorInfo_.operatorNumeric = numeric;
238     } else {
239         csOperatorInfo_.fullName = longName;
240         csOperatorInfo_.shortName = shortName;
241         csOperatorInfo_.operatorNumeric = numeric;
242     }
243 }
244 
SetEmergency(bool isEmergency)245 void NetworkState::SetEmergency(bool isEmergency)
246 {
247     isEmergency_ = isEmergency;
248 }
249 
SetNetworkType(RadioTech tech,DomainType domainType)250 void NetworkState::SetNetworkType(RadioTech tech, DomainType domainType)
251 {
252     if (domainType == DomainType::DOMAIN_TYPE_CS) {
253         csRadioTech_ = tech;
254     } else {
255         psRadioTech_ = tech;
256     }
257 }
258 
SetNetworkState(RegServiceState state,DomainType domainType)259 void NetworkState::SetNetworkState(RegServiceState state, DomainType domainType)
260 {
261     if (domainType == DomainType::DOMAIN_TYPE_CS) {
262         csRegStatus_ = state;
263     } else {
264         psRegStatus_ = state;
265     }
266 }
267 
SetRoaming(RoamingType roamingType,DomainType domainType)268 void NetworkState::SetRoaming(RoamingType roamingType, DomainType domainType)
269 {
270     if (domainType == DomainType::DOMAIN_TYPE_CS) {
271         csRoaming_ = roamingType;
272     } else {
273         psRoaming_ = roamingType;
274     }
275 }
276 
GetPsRoamingStatus() const277 RoamingType NetworkState::GetPsRoamingStatus() const
278 {
279     return psRoaming_;
280 }
281 
GetCsRoamingStatus() const282 RoamingType NetworkState::GetCsRoamingStatus() const
283 {
284     return csRoaming_;
285 }
286 
ToString() const287 std::string NetworkState::ToString() const
288 {
289     int32_t csRoaming = static_cast<int32_t>(csRoaming_);
290     int32_t psRoaming = static_cast<int32_t>(psRoaming_);
291     int32_t psRegStatus = static_cast<int32_t>(psRegStatus_);
292     int32_t csRegStatus = static_cast<int32_t>(csRegStatus_);
293     int32_t psRadioTech = static_cast<int32_t>(psRadioTech_);
294     int32_t csRadioTech = static_cast<int32_t>(csRadioTech_);
295     int32_t cfgTech = static_cast<int32_t>(cfgTech_);
296     int32_t nrState = static_cast<int32_t>(nrState_);
297     std::string psOperatorInfoStr =
298         psOperatorInfo_.fullName + "|" + psOperatorInfo_.operatorNumeric + "|" + psOperatorInfo_.shortName;
299     std::string csOperatorInfoStr =
300         csOperatorInfo_.fullName + "|" + csOperatorInfo_.operatorNumeric + "|" + csOperatorInfo_.shortName;
301     std::string content("isEmergency_:" + std::to_string(isEmergency_ ? 0 : 1) +
302         ",psOperatorInfo:" + psOperatorInfoStr + ",csOperatorInfo:" + csOperatorInfoStr +
303         ",csRoaming:" + std::to_string(csRoaming) + ",psRoaming:" + std::to_string(psRoaming) +
304         ",psRegStatus:" + std::to_string(psRegStatus) + ",csRegStatus:" + std::to_string(csRegStatus) +
305         ",cfgTech:" + std::to_string(cfgTech) + ",nrState:" + std::to_string(nrState) +
306         ",psRadioTech:" + std::to_string(psRadioTech) + ",csRadioTech:" + std::to_string(csRadioTech));
307     return content;
308 }
309 
SetCfgTech(RadioTech tech)310 void NetworkState::SetCfgTech(RadioTech tech)
311 {
312     cfgTech_ = tech;
313 }
314 
GetCfgTech() const315 RadioTech NetworkState::GetCfgTech() const
316 {
317     return cfgTech_;
318 }
319 
SetNrState(NrState state)320 void NetworkState::SetNrState(NrState state)
321 {
322     nrState_ = state;
323 }
324 
GetNrState() const325 NrState NetworkState::GetNrState() const
326 {
327     return nrState_;
328 }
329 } // namespace Telephony
330 } // namespace OHOS
331