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_LOGD("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 lastPsRadioTech_ = RadioTech::RADIO_TECHNOLOGY_UNKNOWN;
45 lastCfgTech_ = RadioTech::RADIO_TECHNOLOGY_UNKNOWN;
46 cfgTech_ = RadioTech::RADIO_TECHNOLOGY_UNKNOWN;
47 nrState_ = NrState::NR_STATE_NOT_SUPPORT;
48 }
49
ReadFromParcel(Parcel & parcel)50 bool NetworkState::ReadFromParcel(Parcel &parcel)
51 {
52 if (!parcel.ReadBool(isEmergency_)) {
53 return false;
54 }
55
56 if (!ReadParcelString(parcel)) {
57 return false;
58 }
59
60 if (!ReadParcelInt(parcel)) {
61 return false;
62 }
63 return true;
64 }
65
ReadParcelString(Parcel & parcel)66 bool NetworkState::ReadParcelString(Parcel &parcel)
67 {
68 std::string readString;
69 if (!parcel.ReadString(readString)) {
70 return false;
71 }
72 psOperatorInfo_.fullName = readString;
73 if (!parcel.ReadString(readString)) {
74 return false;
75 }
76 psOperatorInfo_.shortName = readString;
77 if (!parcel.ReadString(readString)) {
78 return false;
79 }
80 psOperatorInfo_.operatorNumeric = readString;
81 if (!parcel.ReadString(readString)) {
82 return false;
83 }
84 csOperatorInfo_.fullName = readString;
85 if (!parcel.ReadString(readString)) {
86 return false;
87 }
88 csOperatorInfo_.shortName = readString;
89 if (!parcel.ReadString(readString)) {
90 return false;
91 }
92 csOperatorInfo_.operatorNumeric = readString;
93 return true;
94 }
95
ReadParcelInt(Parcel & parcel)96 bool NetworkState::ReadParcelInt(Parcel &parcel)
97 {
98 int32_t rat;
99 if (!parcel.ReadInt32(rat)) {
100 return false;
101 }
102 csRoaming_ = static_cast<RoamingType>(rat);
103 if (!parcel.ReadInt32(rat)) {
104 return false;
105 }
106 psRoaming_ = static_cast<RoamingType>(rat);
107 if (!parcel.ReadInt32(rat)) {
108 return false;
109 }
110 psRegStatus_ = static_cast<RegServiceState>(rat);
111 if (!parcel.ReadInt32(rat)) {
112 return false;
113 }
114 csRegStatus_ = static_cast<RegServiceState>(rat);
115 if (!parcel.ReadInt32(rat)) {
116 return false;
117 }
118 psRadioTech_ = static_cast<RadioTech>(rat);
119 if (!parcel.ReadInt32(rat)) {
120 return false;
121 }
122 csRadioTech_ = static_cast<RadioTech>(rat);
123 if (!parcel.ReadInt32(rat)) {
124 return false;
125 }
126 cfgTech_ = static_cast<RadioTech>(rat);
127 if (!parcel.ReadInt32(rat)) {
128 return false;
129 }
130 nrState_ = static_cast<NrState>(rat);
131 if (!parcel.ReadInt32(rat)) {
132 return false;
133 }
134 lastPsRadioTech_ = static_cast<RadioTech>(rat);
135 if (!parcel.ReadInt32(rat)) {
136 return false;
137 }
138 lastCfgTech_ = static_cast<RadioTech>(rat);
139 return true;
140 }
141
operator ==(const NetworkState & other) const142 bool NetworkState::operator==(const NetworkState &other) const
143 {
144 return isEmergency_ == other.isEmergency_ && csRoaming_ == other.csRoaming_ && psRoaming_ == other.psRoaming_ &&
145 psRegStatus_ == other.psRegStatus_ && csRegStatus_ == other.csRegStatus_ &&
146 psRadioTech_ == other.psRadioTech_ && csRadioTech_ == other.csRadioTech_ &&
147 cfgTech_ == other.cfgTech_ && nrState_ == other.nrState_ &&
148 psOperatorInfo_.operatorNumeric == other.psOperatorInfo_.operatorNumeric &&
149 psOperatorInfo_.fullName == other.psOperatorInfo_.fullName &&
150 psOperatorInfo_.shortName == other.psOperatorInfo_.shortName &&
151 csOperatorInfo_.operatorNumeric == other.csOperatorInfo_.operatorNumeric &&
152 csOperatorInfo_.fullName == other.csOperatorInfo_.fullName &&
153 csOperatorInfo_.shortName == other.csOperatorInfo_.shortName;
154 }
155
Marshalling(Parcel & parcel) const156 bool NetworkState::Marshalling(Parcel &parcel) const
157 {
158 if (!parcel.WriteBool(isEmergency_)) {
159 return false;
160 }
161 if (!MarshallingString(parcel)) {
162 return false;
163 }
164 if (!MarshallingInt(parcel)) {
165 return false;
166 }
167 return true;
168 }
169
MarshallingString(Parcel & parcel) const170 bool NetworkState::MarshallingString(Parcel &parcel) const
171 {
172 if (!parcel.WriteString(psOperatorInfo_.fullName)) {
173 return false;
174 }
175 if (!parcel.WriteString(psOperatorInfo_.shortName)) {
176 return false;
177 }
178 if (!parcel.WriteString(psOperatorInfo_.operatorNumeric)) {
179 return false;
180 }
181 if (!parcel.WriteString(csOperatorInfo_.fullName)) {
182 return false;
183 }
184 if (!parcel.WriteString(csOperatorInfo_.shortName)) {
185 return false;
186 }
187 if (!parcel.WriteString(csOperatorInfo_.operatorNumeric)) {
188 return false;
189 }
190 return true;
191 }
192
MarshallingInt(Parcel & parcel) const193 bool NetworkState::MarshallingInt(Parcel &parcel) const
194 {
195 if (!parcel.WriteInt32(static_cast<int32_t>(csRoaming_))) {
196 return false;
197 }
198 if (!parcel.WriteInt32(static_cast<int32_t>(psRoaming_))) {
199 return false;
200 }
201 if (!parcel.WriteInt32(static_cast<int32_t>(psRegStatus_))) {
202 return false;
203 }
204 if (!parcel.WriteInt32(static_cast<int32_t>(csRegStatus_))) {
205 return false;
206 }
207 if (!parcel.WriteInt32(static_cast<int32_t>(psRadioTech_))) {
208 return false;
209 }
210 if (!parcel.WriteInt32(static_cast<int32_t>(csRadioTech_))) {
211 return false;
212 }
213 if (!parcel.WriteInt32(static_cast<int32_t>(cfgTech_))) {
214 return false;
215 }
216 if (!parcel.WriteInt32(static_cast<int32_t>(nrState_))) {
217 return false;
218 }
219 if (!parcel.WriteInt32(static_cast<int32_t>(lastPsRadioTech_))) {
220 return false;
221 }
222 if (!parcel.WriteInt32(static_cast<int32_t>(lastCfgTech_))) {
223 return false;
224 }
225 return true;
226 }
227
Unmarshalling(Parcel & parcel)228 NetworkState *NetworkState::Unmarshalling(Parcel &parcel)
229 {
230 std::unique_ptr<NetworkState> param = std::make_unique<NetworkState>();
231 if (param == nullptr) {
232 return nullptr;
233 }
234 if (!param->ReadFromParcel(parcel)) {
235 return nullptr;
236 }
237 return param.release();
238 }
239
GetLongOperatorName() const240 std::string NetworkState::GetLongOperatorName() const
241 {
242 if (!psOperatorInfo_.fullName.empty()) {
243 return psOperatorInfo_.fullName;
244 } else {
245 return csOperatorInfo_.fullName;
246 }
247 }
248
GetShortOperatorName() const249 std::string NetworkState::GetShortOperatorName() const
250 {
251 if (!psOperatorInfo_.shortName.empty()) {
252 return psOperatorInfo_.shortName;
253 } else {
254 return csOperatorInfo_.shortName;
255 }
256 }
257
GetPlmnNumeric() const258 std::string NetworkState::GetPlmnNumeric() const
259 {
260 if (!psOperatorInfo_.operatorNumeric.empty()) {
261 return psOperatorInfo_.operatorNumeric;
262 } else {
263 return csOperatorInfo_.operatorNumeric;
264 }
265 }
266
GetRegStatus() const267 RegServiceState NetworkState::GetRegStatus() const
268 {
269 if (psRegStatus_ == RegServiceState::REG_STATE_IN_SERVICE) {
270 return psRegStatus_;
271 } else {
272 return csRegStatus_;
273 }
274 }
275
IsEmergency() const276 bool NetworkState::IsEmergency() const
277 {
278 return isEmergency_;
279 }
280
IsRoaming() const281 bool NetworkState::IsRoaming() const
282 {
283 if (psRoaming_ > RoamingType::ROAMING_STATE_UNKNOWN) {
284 return true;
285 } else if (csRoaming_ > RoamingType::ROAMING_STATE_UNKNOWN) {
286 return true;
287 } else {
288 return false;
289 }
290 }
291
GetPsRadioTech() const292 RadioTech NetworkState::GetPsRadioTech() const
293 {
294 return psRadioTech_;
295 }
296
GetLastPsRadioTech() const297 RadioTech NetworkState::GetLastPsRadioTech() const
298 {
299 return lastPsRadioTech_;
300 }
301
GetCsRadioTech() const302 RadioTech NetworkState::GetCsRadioTech() const
303 {
304 return csRadioTech_;
305 }
306
GetPsRegStatus() const307 RegServiceState NetworkState::GetPsRegStatus() const
308 {
309 return psRegStatus_;
310 }
311
GetCsRegStatus() const312 RegServiceState NetworkState::GetCsRegStatus() const
313 {
314 return csRegStatus_;
315 }
316
SetOperatorInfo(const std::string & longName,const std::string & shortName,const std::string & numeric,DomainType domainType)317 void NetworkState::SetOperatorInfo(
318 const std::string &longName, const std::string &shortName, const std::string &numeric, DomainType domainType)
319 {
320 if (domainType == DomainType::DOMAIN_TYPE_PS) {
321 psOperatorInfo_.fullName = longName;
322 psOperatorInfo_.shortName = shortName;
323 psOperatorInfo_.operatorNumeric = numeric;
324 } else {
325 csOperatorInfo_.fullName = longName;
326 csOperatorInfo_.shortName = shortName;
327 csOperatorInfo_.operatorNumeric = numeric;
328 }
329 }
330
SetEmergency(bool isEmergency)331 void NetworkState::SetEmergency(bool isEmergency)
332 {
333 isEmergency_ = isEmergency;
334 }
335
SetNetworkType(RadioTech tech,DomainType domainType)336 void NetworkState::SetNetworkType(RadioTech tech, DomainType domainType)
337 {
338 if (domainType == DomainType::DOMAIN_TYPE_CS) {
339 csRadioTech_ = tech;
340 } else {
341 lastPsRadioTech_ = psRadioTech_;
342 psRadioTech_ = tech;
343 }
344 }
345
SetNetworkState(RegServiceState state,DomainType domainType)346 void NetworkState::SetNetworkState(RegServiceState state, DomainType domainType)
347 {
348 if (domainType == DomainType::DOMAIN_TYPE_CS) {
349 csRegStatus_ = state;
350 } else {
351 psRegStatus_ = state;
352 }
353 }
354
SetRoaming(RoamingType roamingType,DomainType domainType)355 void NetworkState::SetRoaming(RoamingType roamingType, DomainType domainType)
356 {
357 if (domainType == DomainType::DOMAIN_TYPE_CS) {
358 csRoaming_ = roamingType;
359 } else {
360 psRoaming_ = roamingType;
361 }
362 }
363
GetPsRoamingStatus() const364 RoamingType NetworkState::GetPsRoamingStatus() const
365 {
366 return psRoaming_;
367 }
368
GetCsRoamingStatus() const369 RoamingType NetworkState::GetCsRoamingStatus() const
370 {
371 return csRoaming_;
372 }
373
ToString() const374 std::string NetworkState::ToString() const
375 {
376 int32_t csRoaming = static_cast<int32_t>(csRoaming_);
377 int32_t psRoaming = static_cast<int32_t>(psRoaming_);
378 int32_t psRegStatus = static_cast<int32_t>(psRegStatus_);
379 int32_t csRegStatus = static_cast<int32_t>(csRegStatus_);
380 int32_t psRadioTech = static_cast<int32_t>(psRadioTech_);
381 int32_t csRadioTech = static_cast<int32_t>(csRadioTech_);
382 int32_t cfgTech = static_cast<int32_t>(cfgTech_);
383 int32_t nrState = static_cast<int32_t>(nrState_);
384 std::string psOperatorInfoStr =
385 psOperatorInfo_.fullName + "|" + psOperatorInfo_.operatorNumeric + "|" + psOperatorInfo_.shortName;
386 std::string csOperatorInfoStr =
387 csOperatorInfo_.fullName + "|" + csOperatorInfo_.operatorNumeric + "|" + csOperatorInfo_.shortName;
388 std::string content("isEmergency_:" + std::to_string(isEmergency_ ? 0 : 1) +
389 ",psOperatorInfo:" + psOperatorInfoStr + ",csOperatorInfo:" + csOperatorInfoStr +
390 ",csRoaming:" + std::to_string(csRoaming) + ",psRoaming:" + std::to_string(psRoaming) +
391 ",psRegStatus:" + std::to_string(psRegStatus) + ",csRegStatus:" + std::to_string(csRegStatus) +
392 ",cfgTech:" + std::to_string(cfgTech) + ",nrState:" + std::to_string(nrState) +
393 ",psRadioTech:" + std::to_string(psRadioTech) + ",csRadioTech:" + std::to_string(csRadioTech));
394 return content;
395 }
396
SetCfgTech(RadioTech tech)397 void NetworkState::SetCfgTech(RadioTech tech)
398 {
399 lastCfgTech_ = cfgTech_;
400 cfgTech_ = tech;
401 }
402
GetCfgTech() const403 RadioTech NetworkState::GetCfgTech() const
404 {
405 return cfgTech_;
406 }
407
GetLastCfgTech() const408 RadioTech NetworkState::GetLastCfgTech() const
409 {
410 return lastCfgTech_;
411 }
412
SetNrState(NrState state)413 void NetworkState::SetNrState(NrState state)
414 {
415 nrState_ = state;
416 }
417
GetNrState() const418 NrState NetworkState::GetNrState() const
419 {
420 return nrState_;
421 }
422
SetLongOperatorName(const std::string & longName,DomainType domainType)423 void NetworkState::SetLongOperatorName(const std::string &longName, DomainType domainType)
424 {
425 if (domainType == DomainType::DOMAIN_TYPE_PS) {
426 psOperatorInfo_.fullName = longName;
427 } else {
428 csOperatorInfo_.fullName = longName;
429 }
430 }
431
IsGsm() const432 bool NetworkState::IsGsm() const
433 {
434 switch (psRadioTech_) {
435 case RadioTech::RADIO_TECHNOLOGY_GSM:
436 case RadioTech::RADIO_TECHNOLOGY_WCDMA:
437 case RadioTech::RADIO_TECHNOLOGY_HSPA:
438 case RadioTech::RADIO_TECHNOLOGY_HSPAP:
439 case RadioTech::RADIO_TECHNOLOGY_TD_SCDMA:
440 case RadioTech::RADIO_TECHNOLOGY_LTE:
441 case RadioTech::RADIO_TECHNOLOGY_LTE_CA:
442 case RadioTech::RADIO_TECHNOLOGY_IWLAN:
443 return true;
444 default:
445 return false;
446 }
447 }
448
IsCdma() const449 bool NetworkState::IsCdma() const
450 {
451 switch (psRadioTech_) {
452 case RadioTech::RADIO_TECHNOLOGY_1XRTT:
453 case RadioTech::RADIO_TECHNOLOGY_EVDO:
454 case RadioTech::RADIO_TECHNOLOGY_EHRPD:
455 return true;
456 default:
457 return false;
458 }
459 }
460 } // namespace Telephony
461 } // namespace OHOS
462