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 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 if (!ReadParcelString(parcel)) {
55 return false;
56 }
57
58 if (!ReadParcelInt(parcel)) {
59 return false;
60 }
61 return true;
62 }
63
ReadParcelString(Parcel & parcel)64 bool NetworkState::ReadParcelString(Parcel &parcel)
65 {
66 std::string readString;
67 if (!parcel.ReadString(readString)) {
68 return false;
69 }
70 psOperatorInfo_.fullName = readString;
71 if (!parcel.ReadString(readString)) {
72 return false;
73 }
74 psOperatorInfo_.shortName = readString;
75 if (!parcel.ReadString(readString)) {
76 return false;
77 }
78 psOperatorInfo_.operatorNumeric = readString;
79 if (!parcel.ReadString(readString)) {
80 return false;
81 }
82 csOperatorInfo_.fullName = readString;
83 if (!parcel.ReadString(readString)) {
84 return false;
85 }
86 csOperatorInfo_.shortName = readString;
87 if (!parcel.ReadString(readString)) {
88 return false;
89 }
90 csOperatorInfo_.operatorNumeric = readString;
91 return true;
92 }
93
ReadParcelInt(Parcel & parcel)94 bool NetworkState::ReadParcelInt(Parcel &parcel)
95 {
96 int32_t rat;
97 if (!parcel.ReadInt32(rat)) {
98 return false;
99 }
100 csRoaming_ = static_cast<RoamingType>(rat);
101 if (!parcel.ReadInt32(rat)) {
102 return false;
103 }
104 psRoaming_ = static_cast<RoamingType>(rat);
105 if (!parcel.ReadInt32(rat)) {
106 return false;
107 }
108 psRegStatus_ = static_cast<RegServiceState>(rat);
109 if (!parcel.ReadInt32(rat)) {
110 return false;
111 }
112 csRegStatus_ = static_cast<RegServiceState>(rat);
113 if (!parcel.ReadInt32(rat)) {
114 return false;
115 }
116 psRadioTech_ = static_cast<RadioTech>(rat);
117 if (!parcel.ReadInt32(rat)) {
118 return false;
119 }
120 csRadioTech_ = static_cast<RadioTech>(rat);
121 if (!parcel.ReadInt32(rat)) {
122 return false;
123 }
124 cfgTech_ = static_cast<RadioTech>(rat);
125 if (!parcel.ReadInt32(rat)) {
126 return false;
127 }
128 nrState_ = static_cast<NrState>(rat);
129 return true;
130 }
131
operator ==(const NetworkState & other) const132 bool NetworkState::operator==(const NetworkState &other) const
133 {
134 return isEmergency_ == other.isEmergency_ && csRoaming_ == other.csRoaming_ && psRoaming_ == other.psRoaming_ &&
135 psRegStatus_ == other.psRegStatus_ && csRegStatus_ == other.csRegStatus_ &&
136 psRadioTech_ == other.psRadioTech_ && csRadioTech_ == other.csRadioTech_ &&
137 cfgTech_ == other.cfgTech_ && nrState_ == other.nrState_ &&
138 psOperatorInfo_.operatorNumeric == other.psOperatorInfo_.operatorNumeric &&
139 psOperatorInfo_.fullName == other.psOperatorInfo_.fullName &&
140 psOperatorInfo_.shortName == other.psOperatorInfo_.shortName &&
141 csOperatorInfo_.operatorNumeric == other.csOperatorInfo_.operatorNumeric &&
142 csOperatorInfo_.fullName == other.csOperatorInfo_.fullName &&
143 csOperatorInfo_.shortName == other.csOperatorInfo_.shortName;
144 }
145
Marshalling(Parcel & parcel) const146 bool NetworkState::Marshalling(Parcel &parcel) const
147 {
148 if (!parcel.WriteBool(isEmergency_)) {
149 return false;
150 }
151
152 if (!parcel.WriteString(psOperatorInfo_.fullName)) {
153 return false;
154 }
155 if (!parcel.WriteString(psOperatorInfo_.shortName)) {
156 return false;
157 }
158 if (!parcel.WriteString(psOperatorInfo_.operatorNumeric)) {
159 return false;
160 }
161 if (!parcel.WriteString(csOperatorInfo_.fullName)) {
162 return false;
163 }
164 if (!parcel.WriteString(csOperatorInfo_.shortName)) {
165 return false;
166 }
167 if (!parcel.WriteString(csOperatorInfo_.operatorNumeric)) {
168 return false;
169 }
170 if (!parcel.WriteInt32(static_cast<int32_t>(csRoaming_))) {
171 return false;
172 }
173 if (!parcel.WriteInt32(static_cast<int32_t>(psRoaming_))) {
174 return false;
175 }
176 if (!parcel.WriteInt32(static_cast<int32_t>(psRegStatus_))) {
177 return false;
178 }
179 if (!parcel.WriteInt32(static_cast<int32_t>(csRegStatus_))) {
180 return false;
181 }
182 if (!parcel.WriteInt32(static_cast<int32_t>(psRadioTech_))) {
183 return false;
184 }
185 if (!parcel.WriteInt32(static_cast<int32_t>(csRadioTech_))) {
186 return false;
187 }
188 if (!parcel.WriteInt32(static_cast<int32_t>(cfgTech_))) {
189 return false;
190 }
191 if (!parcel.WriteInt32(static_cast<int32_t>(nrState_))) {
192 return false;
193 }
194 return true;
195 }
196
Unmarshalling(Parcel & parcel)197 NetworkState *NetworkState::Unmarshalling(Parcel &parcel)
198 {
199 std::unique_ptr<NetworkState> param = std::make_unique<NetworkState>();
200 if (param == nullptr) {
201 return nullptr;
202 }
203 if (!param->ReadFromParcel(parcel)) {
204 return nullptr;
205 }
206 return param.release();
207 }
208
GetLongOperatorName() const209 std::string NetworkState::GetLongOperatorName() const
210 {
211 if (!psOperatorInfo_.fullName.empty()) {
212 return psOperatorInfo_.fullName;
213 } else {
214 return csOperatorInfo_.fullName;
215 }
216 }
217
GetShortOperatorName() const218 std::string NetworkState::GetShortOperatorName() const
219 {
220 if (!psOperatorInfo_.shortName.empty()) {
221 return psOperatorInfo_.shortName;
222 } else {
223 return csOperatorInfo_.shortName;
224 }
225 }
226
GetPlmnNumeric() const227 std::string NetworkState::GetPlmnNumeric() const
228 {
229 if (!psOperatorInfo_.operatorNumeric.empty()) {
230 return psOperatorInfo_.operatorNumeric;
231 } else {
232 return csOperatorInfo_.operatorNumeric;
233 }
234 }
235
GetRegStatus() const236 RegServiceState NetworkState::GetRegStatus() const
237 {
238 if (psRegStatus_ == RegServiceState::REG_STATE_IN_SERVICE) {
239 return psRegStatus_;
240 } else {
241 return csRegStatus_;
242 }
243 }
244
IsEmergency() const245 bool NetworkState::IsEmergency() const
246 {
247 return isEmergency_;
248 }
249
IsRoaming() const250 bool NetworkState::IsRoaming() const
251 {
252 if (psRoaming_ > RoamingType::ROAMING_STATE_UNKNOWN) {
253 return true;
254 } else if (csRoaming_ > RoamingType::ROAMING_STATE_UNKNOWN) {
255 return true;
256 } else {
257 return false;
258 }
259 }
260
GetPsRadioTech() const261 RadioTech NetworkState::GetPsRadioTech() const
262 {
263 return psRadioTech_;
264 }
265
GetLastPsRadioTech() const266 RadioTech NetworkState::GetLastPsRadioTech() const
267 {
268 return lastPsRadioTech_;
269 }
270
GetCsRadioTech() const271 RadioTech NetworkState::GetCsRadioTech() const
272 {
273 return csRadioTech_;
274 }
275
GetPsRegStatus() const276 RegServiceState NetworkState::GetPsRegStatus() const
277 {
278 return psRegStatus_;
279 }
280
GetCsRegStatus() const281 RegServiceState NetworkState::GetCsRegStatus() const
282 {
283 return csRegStatus_;
284 }
285
SetOperatorInfo(const std::string & longName,const std::string & shortName,const std::string & numeric,DomainType domainType)286 void NetworkState::SetOperatorInfo(
287 const std::string &longName, const std::string &shortName, const std::string &numeric, DomainType domainType)
288 {
289 if (domainType == DomainType::DOMAIN_TYPE_PS) {
290 psOperatorInfo_.fullName = longName;
291 psOperatorInfo_.shortName = shortName;
292 psOperatorInfo_.operatorNumeric = numeric;
293 } else {
294 csOperatorInfo_.fullName = longName;
295 csOperatorInfo_.shortName = shortName;
296 csOperatorInfo_.operatorNumeric = numeric;
297 }
298 }
299
SetEmergency(bool isEmergency)300 void NetworkState::SetEmergency(bool isEmergency)
301 {
302 isEmergency_ = isEmergency;
303 }
304
SetNetworkType(RadioTech tech,DomainType domainType)305 void NetworkState::SetNetworkType(RadioTech tech, DomainType domainType)
306 {
307 if (domainType == DomainType::DOMAIN_TYPE_CS) {
308 csRadioTech_ = tech;
309 } else {
310 lastPsRadioTech_ = psRadioTech_;
311 psRadioTech_ = tech;
312 }
313 }
314
SetNetworkState(RegServiceState state,DomainType domainType)315 void NetworkState::SetNetworkState(RegServiceState state, DomainType domainType)
316 {
317 if (domainType == DomainType::DOMAIN_TYPE_CS) {
318 csRegStatus_ = state;
319 } else {
320 psRegStatus_ = state;
321 }
322 }
323
SetRoaming(RoamingType roamingType,DomainType domainType)324 void NetworkState::SetRoaming(RoamingType roamingType, DomainType domainType)
325 {
326 if (domainType == DomainType::DOMAIN_TYPE_CS) {
327 csRoaming_ = roamingType;
328 } else {
329 psRoaming_ = roamingType;
330 }
331 }
332
GetPsRoamingStatus() const333 RoamingType NetworkState::GetPsRoamingStatus() const
334 {
335 return psRoaming_;
336 }
337
GetCsRoamingStatus() const338 RoamingType NetworkState::GetCsRoamingStatus() const
339 {
340 return csRoaming_;
341 }
342
ToString() const343 std::string NetworkState::ToString() const
344 {
345 int32_t csRoaming = static_cast<int32_t>(csRoaming_);
346 int32_t psRoaming = static_cast<int32_t>(psRoaming_);
347 int32_t psRegStatus = static_cast<int32_t>(psRegStatus_);
348 int32_t csRegStatus = static_cast<int32_t>(csRegStatus_);
349 int32_t psRadioTech = static_cast<int32_t>(psRadioTech_);
350 int32_t csRadioTech = static_cast<int32_t>(csRadioTech_);
351 int32_t cfgTech = static_cast<int32_t>(cfgTech_);
352 int32_t nrState = static_cast<int32_t>(nrState_);
353 std::string psOperatorInfoStr =
354 psOperatorInfo_.fullName + "|" + psOperatorInfo_.operatorNumeric + "|" + psOperatorInfo_.shortName;
355 std::string csOperatorInfoStr =
356 csOperatorInfo_.fullName + "|" + csOperatorInfo_.operatorNumeric + "|" + csOperatorInfo_.shortName;
357 std::string content("isEmergency_:" + std::to_string(isEmergency_ ? 0 : 1) +
358 ",psOperatorInfo:" + psOperatorInfoStr + ",csOperatorInfo:" + csOperatorInfoStr +
359 ",csRoaming:" + std::to_string(csRoaming) + ",psRoaming:" + std::to_string(psRoaming) +
360 ",psRegStatus:" + std::to_string(psRegStatus) + ",csRegStatus:" + std::to_string(csRegStatus) +
361 ",cfgTech:" + std::to_string(cfgTech) + ",nrState:" + std::to_string(nrState) +
362 ",psRadioTech:" + std::to_string(psRadioTech) + ",csRadioTech:" + std::to_string(csRadioTech));
363 return content;
364 }
365
SetCfgTech(RadioTech tech)366 void NetworkState::SetCfgTech(RadioTech tech)
367 {
368 lastCfgTech_ = cfgTech_;
369 cfgTech_ = tech;
370 }
371
GetCfgTech() const372 RadioTech NetworkState::GetCfgTech() const
373 {
374 return cfgTech_;
375 }
376
GetLastCfgTech() const377 RadioTech NetworkState::GetLastCfgTech() const
378 {
379 return lastCfgTech_;
380 }
381
SetNrState(NrState state)382 void NetworkState::SetNrState(NrState state)
383 {
384 nrState_ = state;
385 }
386
GetNrState() const387 NrState NetworkState::GetNrState() const
388 {
389 return nrState_;
390 }
391 } // namespace Telephony
392 } // namespace OHOS
393