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[0] = '\0';
37 csOperatorInfo_.fullName[0] = '\0';
38 psOperatorInfo_.shortName[0] = '\0';
39 csOperatorInfo_.shortName[0] = '\0';
40 psOperatorInfo_.operatorNumeric[0] = '\0';
41 csOperatorInfo_.operatorNumeric[0] = '\0';
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 const char *readString = parcel.ReadCString();
55 if ((readString == nullptr) ||
56 (memcpy_s(psOperatorInfo_.fullName, OperatorInformation::NETWORK_MAX_FULL_NAME_LEN, readString,
57 OperatorInformation::NETWORK_MAX_FULL_NAME_LEN) != 0)) {
58 TELEPHONY_LOGE("fail to copy memory");
59 return false;
60 }
61 if (((readString = parcel.ReadCString()) == nullptr) ||
62 (memcpy_s(psOperatorInfo_.shortName, OperatorInformation::NETWORK_MAX_NAME_LEN, readString,
63 OperatorInformation::NETWORK_MAX_NAME_LEN) != 0)) {
64 TELEPHONY_LOGE("fail to copy memory");
65 return false;
66 }
67 if (((readString = parcel.ReadCString()) == nullptr) ||
68 (memcpy_s(psOperatorInfo_.operatorNumeric, OperatorInformation::NETWORK_MAX_PLMN_LEN, readString,
69 OperatorInformation::NETWORK_MAX_PLMN_LEN) != 0)) {
70 TELEPHONY_LOGE("fail to copy memory");
71 return false;
72 }
73 if (((readString = parcel.ReadCString()) == nullptr) ||
74 (memcpy_s(csOperatorInfo_.fullName, OperatorInformation::NETWORK_MAX_FULL_NAME_LEN, readString,
75 OperatorInformation::NETWORK_MAX_FULL_NAME_LEN) != 0)) {
76 TELEPHONY_LOGE("fail to copy memory");
77 return false;
78 }
79 if (((readString = parcel.ReadCString()) == nullptr) ||
80 (memcpy_s(csOperatorInfo_.shortName, OperatorInformation::NETWORK_MAX_NAME_LEN, readString,
81 OperatorInformation::NETWORK_MAX_NAME_LEN) != 0)) {
82 TELEPHONY_LOGE("fail to copy memory");
83 return false;
84 }
85 if (((readString = parcel.ReadCString()) == nullptr) ||
86 (memcpy_s(csOperatorInfo_.operatorNumeric, OperatorInformation::NETWORK_MAX_PLMN_LEN, readString,
87 OperatorInformation::NETWORK_MAX_PLMN_LEN) != 0)) {
88 TELEPHONY_LOGE("fail to copy memory");
89 return false;
90 }
91
92 csRoaming_ = static_cast<RoamingType>(parcel.ReadInt32());
93 psRoaming_ = static_cast<RoamingType>(parcel.ReadInt32());
94 psRegStatus_ = static_cast<RegServiceState>(parcel.ReadInt32());
95 csRegStatus_ = static_cast<RegServiceState>(parcel.ReadInt32());
96 psRadioTech_ = static_cast<RadioTech>(parcel.ReadInt32());
97 csRadioTech_ = static_cast<RadioTech>(parcel.ReadInt32());
98 cfgTech_ = static_cast<RadioTech>(parcel.ReadInt32());
99 nrState_ = static_cast<NrState>(parcel.ReadInt32());
100 return true;
101 }
102
operator ==(const NetworkState & other) const103 bool NetworkState::operator==(const NetworkState &other) const
104 {
105 return isEmergency_ == other.isEmergency_ && csRoaming_ == other.csRoaming_ && psRoaming_ == other.psRoaming_ &&
106 psRegStatus_ == other.psRegStatus_ && csRegStatus_ == other.csRegStatus_ &&
107 psRadioTech_ == other.psRadioTech_ && csRadioTech_ == other.csRadioTech_ &&
108 cfgTech_ == other.cfgTech_ && nrState_ == other.nrState_ &&
109 !memcmp(psOperatorInfo_.operatorNumeric, other.psOperatorInfo_.operatorNumeric,
110 strlen(psOperatorInfo_.operatorNumeric)) &&
111 !memcmp(psOperatorInfo_.fullName, other.psOperatorInfo_.fullName, strlen(psOperatorInfo_.fullName)) &&
112 !memcmp(psOperatorInfo_.shortName, other.psOperatorInfo_.shortName, strlen(psOperatorInfo_.shortName)) &&
113 !memcmp(csOperatorInfo_.operatorNumeric, other.csOperatorInfo_.operatorNumeric,
114 strlen(csOperatorInfo_.operatorNumeric)) &&
115 !memcmp(csOperatorInfo_.fullName, other.csOperatorInfo_.fullName, strlen(csOperatorInfo_.fullName)) &&
116 !memcmp(csOperatorInfo_.shortName, other.csOperatorInfo_.shortName, strlen(csOperatorInfo_.shortName));
117 }
118
Marshalling(Parcel & parcel) const119 bool NetworkState::Marshalling(Parcel &parcel) const
120 {
121 if (!parcel.WriteBool(isEmergency_)) {
122 return false;
123 }
124 if (!parcel.WriteCString(psOperatorInfo_.fullName)) {
125 return false;
126 }
127 if (!parcel.WriteCString(psOperatorInfo_.shortName)) {
128 return false;
129 }
130 if (!parcel.WriteCString(psOperatorInfo_.operatorNumeric)) {
131 return false;
132 }
133 if (!parcel.WriteCString(csOperatorInfo_.fullName)) {
134 return false;
135 }
136 if (!parcel.WriteCString(csOperatorInfo_.shortName)) {
137 return false;
138 }
139 if (!parcel.WriteCString(csOperatorInfo_.operatorNumeric)) {
140 return false;
141 }
142 if (!parcel.WriteInt32(static_cast<int32_t>(csRoaming_))) {
143 return false;
144 }
145 if (!parcel.WriteInt32(static_cast<int32_t>(psRoaming_))) {
146 return false;
147 }
148 if (!parcel.WriteInt32(static_cast<int32_t>(psRegStatus_))) {
149 return false;
150 }
151 if (!parcel.WriteInt32(static_cast<int32_t>(csRegStatus_))) {
152 return false;
153 }
154 if (!parcel.WriteInt32(static_cast<int32_t>(psRadioTech_))) {
155 return false;
156 }
157 if (!parcel.WriteInt32(static_cast<int32_t>(csRadioTech_))) {
158 return false;
159 }
160 if (!parcel.WriteInt32(static_cast<int32_t>(cfgTech_))) {
161 return false;
162 }
163 if (!parcel.WriteInt32(static_cast<int32_t>(nrState_))) {
164 return false;
165 }
166 return true;
167 }
168
Unmarshalling(Parcel & parcel)169 NetworkState *NetworkState::Unmarshalling(Parcel &parcel)
170 {
171 std::unique_ptr<NetworkState> param = std::make_unique<NetworkState>();
172 if (param == nullptr) {
173 return nullptr;
174 }
175 if (!param->ReadFromParcel(parcel)) {
176 return nullptr;
177 }
178 return param.release();
179 }
180
GetLongOperatorName() const181 std::string NetworkState::GetLongOperatorName() const
182 {
183 if (strlen(psOperatorInfo_.fullName) > 0) {
184 return std::string(psOperatorInfo_.fullName);
185 } else {
186 return std::string(csOperatorInfo_.fullName);
187 }
188 }
189
GetShortOperatorName() const190 std::string NetworkState::GetShortOperatorName() const
191 {
192 if (strlen(psOperatorInfo_.shortName) > 0) {
193 return std::string(psOperatorInfo_.shortName);
194 } else {
195 return std::string(csOperatorInfo_.shortName);
196 }
197 }
198
GetPlmnNumeric() const199 std::string NetworkState::GetPlmnNumeric() const
200 {
201 if (strlen(psOperatorInfo_.operatorNumeric) > 0) {
202 return std::string(psOperatorInfo_.operatorNumeric);
203 } else {
204 return std::string(csOperatorInfo_.operatorNumeric);
205 }
206 }
207
GetRegStatus() const208 RegServiceState NetworkState::GetRegStatus() const
209 {
210 if (psRegStatus_ == RegServiceState::REG_STATE_IN_SERVICE) {
211 return psRegStatus_;
212 } else {
213 return csRegStatus_;
214 }
215 }
216
IsEmergency() const217 bool NetworkState::IsEmergency() const
218 {
219 return isEmergency_;
220 }
221
IsRoaming() const222 bool NetworkState::IsRoaming() const
223 {
224 if (psRoaming_ > RoamingType::ROAMING_STATE_UNKNOWN) {
225 return true;
226 } else if (csRoaming_ > RoamingType::ROAMING_STATE_UNKNOWN) {
227 return true;
228 } else {
229 return false;
230 }
231 }
232
GetPsRadioTech() const233 RadioTech NetworkState::GetPsRadioTech() const
234 {
235 return psRadioTech_;
236 }
237
GetCsRadioTech() const238 RadioTech NetworkState::GetCsRadioTech() const
239 {
240 return csRadioTech_;
241 }
242
GetPsRegStatus() const243 RegServiceState NetworkState::GetPsRegStatus() const
244 {
245 return psRegStatus_;
246 }
247
GetCsRegStatus() const248 RegServiceState NetworkState::GetCsRegStatus() const
249 {
250 return csRegStatus_;
251 }
252
SetOperatorInfo(const std::string & longName,const std::string & shortName,const std::string & numeric,DomainType domainType)253 void NetworkState::SetOperatorInfo(
254 const std::string &longName, const std::string &shortName, const std::string &numeric, DomainType domainType)
255 {
256 if (domainType == DomainType::DOMAIN_TYPE_PS) {
257 if (memcpy_s(static_cast<void *>(psOperatorInfo_.fullName), OperatorInformation::NETWORK_MAX_FULL_NAME_LEN,
258 static_cast<const void *>(longName.c_str()), OperatorInformation::NETWORK_MAX_FULL_NAME_LEN) != 0) {
259 return;
260 }
261 if (memcpy_s(static_cast<void *>(psOperatorInfo_.shortName), OperatorInformation::NETWORK_MAX_NAME_LEN,
262 static_cast<const void *>(shortName.c_str()), OperatorInformation::NETWORK_MAX_NAME_LEN) != 0) {
263 return;
264 }
265 if (memcpy_s(static_cast<void *>(psOperatorInfo_.operatorNumeric), OperatorInformation::NETWORK_MAX_PLMN_LEN,
266 static_cast<const void *>(numeric.c_str()), OperatorInformation::NETWORK_MAX_PLMN_LEN) != 0) {
267 return;
268 }
269 } else {
270 if (memcpy_s(static_cast<void *>(csOperatorInfo_.fullName), OperatorInformation::NETWORK_MAX_FULL_NAME_LEN,
271 static_cast<const void *>(longName.c_str()), OperatorInformation::NETWORK_MAX_FULL_NAME_LEN) != 0) {
272 return;
273 }
274 if (memcpy_s(static_cast<void *>(csOperatorInfo_.shortName), OperatorInformation::NETWORK_MAX_NAME_LEN,
275 static_cast<const void *>(shortName.c_str()), OperatorInformation::NETWORK_MAX_NAME_LEN) != 0) {
276 return;
277 }
278 if (memcpy_s(static_cast<void *>(csOperatorInfo_.operatorNumeric), OperatorInformation::NETWORK_MAX_PLMN_LEN,
279 static_cast<const void *>(numeric.c_str()), OperatorInformation::NETWORK_MAX_PLMN_LEN) != 0) {
280 return;
281 }
282 }
283 }
284
SetEmergency(bool isEmergency)285 void NetworkState::SetEmergency(bool isEmergency)
286 {
287 isEmergency_ = isEmergency;
288 }
289
SetNetworkType(RadioTech tech,DomainType domainType)290 void NetworkState::SetNetworkType(RadioTech tech, DomainType domainType)
291 {
292 if (domainType == DomainType::DOMAIN_TYPE_CS) {
293 csRadioTech_ = tech;
294 } else {
295 psRadioTech_ = tech;
296 }
297 }
298
SetNetworkState(RegServiceState state,DomainType domainType)299 void NetworkState::SetNetworkState(RegServiceState state, DomainType domainType)
300 {
301 if (domainType == DomainType::DOMAIN_TYPE_CS) {
302 csRegStatus_ = state;
303 } else {
304 psRegStatus_ = state;
305 }
306 }
307
SetRoaming(RoamingType roamingType,DomainType domainType)308 void NetworkState::SetRoaming(RoamingType roamingType, DomainType domainType)
309 {
310 if (domainType == DomainType::DOMAIN_TYPE_CS) {
311 csRoaming_ = roamingType;
312 } else {
313 psRoaming_ = roamingType;
314 }
315 }
316
GetPsRoamingStatus() const317 RoamingType NetworkState::GetPsRoamingStatus() const
318 {
319 return psRoaming_;
320 }
321
GetCsRoamingStatus() const322 RoamingType NetworkState::GetCsRoamingStatus() const
323 {
324 return csRoaming_;
325 }
326
ToString() const327 std::string NetworkState::ToString() const
328 {
329 int32_t csRoaming = static_cast<int32_t>(csRoaming_);
330 int32_t psRoaming = static_cast<int32_t>(psRoaming_);
331 int32_t psRegStatus = static_cast<int32_t>(psRegStatus_);
332 int32_t csRegStatus = static_cast<int32_t>(csRegStatus_);
333 int32_t psRadioTech = static_cast<int32_t>(psRadioTech_);
334 int32_t csRadioTech = static_cast<int32_t>(csRadioTech_);
335 int32_t cfgTech = static_cast<int32_t>(cfgTech_);
336 int32_t nrState = static_cast<int32_t>(nrState_);
337 std::string psFullName(psOperatorInfo_.fullName);
338 std::string psOperatorNumeric(psOperatorInfo_.operatorNumeric);
339 std::string psShortName(psOperatorInfo_.shortName);
340 std::string psOperatorInfoStr = psFullName + "|" + psOperatorNumeric + "|" + psShortName;
341 std::string csFullName(csOperatorInfo_.fullName);
342 std::string csOperatorNumeric(csOperatorInfo_.operatorNumeric);
343 std::string csShortName(csOperatorInfo_.shortName);
344 std::string csOperatorInfoStr = csFullName + "|" + csOperatorNumeric + "|" + csShortName;
345 std::string content("isEmergency_:" + std::to_string(isEmergency_ ? 0 : 1) +
346 ",psOperatorInfo:" + psOperatorInfoStr + ",csOperatorInfo:" + csOperatorInfoStr +
347 ",csRoaming:" + std::to_string(csRoaming) + ",psRoaming:" + std::to_string(psRoaming) +
348 ",psRegStatus:" + std::to_string(psRegStatus) + ",csRegStatus:" + std::to_string(csRegStatus) +
349 ",cfgTech:" + std::to_string(cfgTech) + ",nrState:" + std::to_string(nrState) +
350 ",psRadioTech:" + std::to_string(psRadioTech) + ",csRadioTech:" + std::to_string(csRadioTech));
351 return content;
352 }
353
SetCfgTech(RadioTech tech)354 void NetworkState::SetCfgTech(RadioTech tech)
355 {
356 cfgTech_ = tech;
357 }
358
GetCfgTech() const359 RadioTech NetworkState::GetCfgTech() const
360 {
361 return cfgTech_;
362 }
363
SetNrState(NrState state)364 void NetworkState::SetNrState(NrState state)
365 {
366 nrState_ = state;
367 }
368
GetNrState() const369 NrState NetworkState::GetNrState() const
370 {
371 return nrState_;
372 }
373 } // namespace Telephony
374 } // namespace OHOS
375