• 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 "signal_information.h"
17 
18 #include <string_ex.h>
19 #include "telephony_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace Telephony {
23 constexpr int32_t SIGNAL_RSSI_MAXIMUM = -1;
24 constexpr int32_t SIGNAL_INTENSITY_INVALID = 0;
25 constexpr int32_t SIGNAL_LEVEL_INVALID = 0;
26 constexpr int32_t SIGNAL_FIVE_BARS = 5;
27 constexpr int32_t SIGNAL_FOUR_BARS = 4;
28 const int32_t *GSM_SIGNAL_THRESHOLD = SignalInformation::GSM_SIGNAL_THRESHOLD_5BAR;
29 const int32_t *CDMA_SIGNAL_THRESHOLD = SignalInformation::CDMA_SIGNAL_THRESHOLD_5BAR;
30 const int32_t *LTE_SIGNAL_THRESHOLD = SignalInformation::LTE_SIGNAL_THRESHOLD_5BAR;
31 const int32_t *WCDMA_SIGNAL_THRESHOLD = SignalInformation::WCDMA_SIGNAL_THRESHOLD_5BAR;
32 const int32_t *TD_SCDMA_SIGNAL_THRESHOLD = SignalInformation::TD_SCDMA_SIGNAL_THRESHOLD_5BAR;
33 const int32_t *NR_SIGNAL_THRESHOLD = SignalInformation::NR_SIGNAL_THRESHOLD_5BAR;
34 int32_t SignalInformation::signalBar_ = SIGNAL_FIVE_BARS;
35 
SignalInformation()36 SignalInformation::SignalInformation()
37 {
38     InitSignalBar();
39 }
40 
InitSignalBar(const int32_t bar)41 void SignalInformation::InitSignalBar(const int32_t bar)
42 {
43     if (bar == SIGNAL_FOUR_BARS) {
44         GSM_SIGNAL_THRESHOLD = SignalInformation::GSM_SIGNAL_THRESHOLD_4BAR;
45         CDMA_SIGNAL_THRESHOLD = SignalInformation::CDMA_SIGNAL_THRESHOLD_4BAR;
46         LTE_SIGNAL_THRESHOLD = SignalInformation::LTE_SIGNAL_THRESHOLD_4BAR;
47         WCDMA_SIGNAL_THRESHOLD = SignalInformation::WCDMA_SIGNAL_THRESHOLD_4BAR;
48         TD_SCDMA_SIGNAL_THRESHOLD = SignalInformation::TD_SCDMA_SIGNAL_THRESHOLD_4BAR;
49         NR_SIGNAL_THRESHOLD = SignalInformation::NR_SIGNAL_THRESHOLD_4BAR;
50         signalBar_ = SIGNAL_FOUR_BARS;
51     } else {
52         GSM_SIGNAL_THRESHOLD = SignalInformation::GSM_SIGNAL_THRESHOLD_5BAR;
53         CDMA_SIGNAL_THRESHOLD = SignalInformation::CDMA_SIGNAL_THRESHOLD_5BAR;
54         LTE_SIGNAL_THRESHOLD = SignalInformation::LTE_SIGNAL_THRESHOLD_5BAR;
55         WCDMA_SIGNAL_THRESHOLD = SignalInformation::WCDMA_SIGNAL_THRESHOLD_5BAR;
56         TD_SCDMA_SIGNAL_THRESHOLD = SignalInformation::TD_SCDMA_SIGNAL_THRESHOLD_5BAR;
57         NR_SIGNAL_THRESHOLD = SignalInformation::NR_SIGNAL_THRESHOLD_5BAR;
58         signalBar_ = SIGNAL_FIVE_BARS;
59     }
60 }
61 
Unmarshalling(Parcel & parcel)62 std::unique_ptr<SignalInformation> SignalInformation::Unmarshalling(Parcel &parcel)
63 {
64     return nullptr;
65 }
66 
operator =(const GsmSignalInformation & gsm)67 GsmSignalInformation &GsmSignalInformation::operator=(const GsmSignalInformation &gsm)
68 {
69     signalBar_ = gsm.signalBar_;
70     gsmRxlev_ = gsm.gsmRxlev_;
71     gsmBer_ = gsm.gsmBer_;
72     return *this;
73 }
74 
operator ==(const GsmSignalInformation & gsm) const75 bool GsmSignalInformation::operator==(const GsmSignalInformation &gsm) const
76 {
77     return gsmRxlev_ == gsm.gsmRxlev_ && gsmBer_ == gsm.gsmBer_;
78 }
79 
SetValue(const int32_t gsmRssi,const int32_t gsmBer)80 void GsmSignalInformation::SetValue(const int32_t gsmRssi, const int32_t gsmBer)
81 {
82     gsmRxlev_ = gsmRssi;
83     gsmBer_ = gsmBer;
84 }
85 
GetRssi() const86 int32_t GsmSignalInformation::GetRssi() const
87 {
88     return gsmRxlev_;
89 }
90 
GetGsmBer() const91 int32_t GsmSignalInformation::GetGsmBer() const
92 {
93     return gsmBer_;
94 }
95 
GetSignalIntensity() const96 int32_t GsmSignalInformation::GetSignalIntensity() const
97 {
98     int32_t signalIntensity = SIGNAL_INTENSITY_INVALID;
99 
100     if (ValidateGsmValue()) {
101         signalIntensity = GetRssi();
102     } else {
103         TELEPHONY_LOGE("GsmSignalInformation::GetSignalIntensity Value is Invalid.");
104     }
105 
106     return signalIntensity;
107 }
108 
GetSignalLevel() const109 int32_t GsmSignalInformation::GetSignalLevel() const
110 {
111     int32_t level = SIGNAL_LEVEL_INVALID;
112     int32_t gsmRxlev = GetRssi();
113     if (ValidateGsmValue()) {
114         // Reference: TS 27.007 section 8.69
115         for (int32_t i = signalBar_; i >= 0; --i) {
116             if (gsmRxlev >= GSM_SIGNAL_THRESHOLD[i]) {
117                 level = i;
118                 break;
119             }
120         }
121     } else {
122         TELEPHONY_LOGE("GsmSignalInformation::GetSignalLevel Value is Invalid.");
123     }
124     return level;
125 }
126 
GetNetworkType() const127 SignalInformation::NetworkType GsmSignalInformation::GetNetworkType() const
128 {
129     return SignalInformation::NetworkType::GSM;
130 }
131 
ToString() const132 std::string GsmSignalInformation::ToString() const
133 {
134     int32_t netWorkType = static_cast<int32_t>(GsmSignalInformation::GetNetworkType());
135     std::string content("networkType:" + std::to_string(netWorkType) + ",gsmRxlev:" + std::to_string(gsmRxlev_) +
136         ",signalLevel:" + std::to_string(GsmSignalInformation::GetSignalLevel()) +
137         ",gsmBer:" + std::to_string(gsmBer_));
138     return content;
139 }
140 
NewInstance() const141 sptr<SignalInformation> GsmSignalInformation::NewInstance() const
142 {
143     std::unique_ptr<GsmSignalInformation> gsm = std::make_unique<GsmSignalInformation>();
144     if (gsm == nullptr) {
145         return nullptr;
146     }
147     gsm->SetValue(this->gsmRxlev_, this->gsmBer_);
148     return gsm.release();
149 }
150 
Marshalling(Parcel & parcel) const151 bool GsmSignalInformation::Marshalling(Parcel &parcel) const
152 {
153     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::GSM))) {
154         return false;
155     }
156     if (!parcel.WriteInt32(gsmRxlev_)) {
157         return false;
158     }
159     if (!parcel.WriteInt32(gsmBer_)) {
160         return false;
161     }
162     return true;
163 }
164 
Unmarshalling(Parcel & parcel)165 std::unique_ptr<GsmSignalInformation> GsmSignalInformation::Unmarshalling(Parcel &parcel)
166 {
167     std::unique_ptr<GsmSignalInformation> signal = std::make_unique<GsmSignalInformation>();
168     if (signal && !signal->ReadFromParcel(parcel)) {
169         signal = nullptr;
170     }
171     return signal;
172 }
173 
ReadFromParcel(Parcel & parcel)174 bool GsmSignalInformation::ReadFromParcel(Parcel &parcel)
175 {
176     if (!parcel.ReadInt32(gsmRxlev_)) {
177         return false;
178     }
179     if (!parcel.ReadInt32(gsmBer_)) {
180         return false;
181     }
182     return true;
183 }
184 
ValidateGsmValue() const185 bool GsmSignalInformation::ValidateGsmValue() const
186 {
187     return gsmRxlev_ < SIGNAL_RSSI_MAXIMUM;
188 }
189 
SetValue(const int32_t cdmaRssi,const int32_t cdmaEcno)190 void CdmaSignalInformation::SetValue(const int32_t cdmaRssi, const int32_t cdmaEcno)
191 {
192     cdmaRssi_ = cdmaRssi;
193     cdmaEcno_ = cdmaEcno;
194 }
195 
operator =(const CdmaSignalInformation & cdma)196 CdmaSignalInformation &CdmaSignalInformation::operator=(const CdmaSignalInformation &cdma)
197 {
198     signalBar_ = cdma.signalBar_;
199     cdmaRssi_ = cdma.cdmaRssi_;
200     cdmaEcno_ = cdma.cdmaEcno_;
201     return *this;
202 }
203 
operator ==(const CdmaSignalInformation & cdma) const204 bool CdmaSignalInformation::operator==(const CdmaSignalInformation &cdma) const
205 {
206     return (cdmaRssi_ == cdma.cdmaRssi_ && cdmaEcno_ == cdma.cdmaEcno_);
207 }
208 
GetCdmaRssi() const209 int32_t CdmaSignalInformation::GetCdmaRssi() const
210 {
211     return cdmaRssi_;
212 }
213 
GetSignalIntensity() const214 int32_t CdmaSignalInformation::GetSignalIntensity() const
215 {
216     int32_t signalIntensity = SIGNAL_INTENSITY_INVALID;
217 
218     if (ValidateCdmaValue()) {
219         signalIntensity = GetCdmaRssi();
220     } else {
221         TELEPHONY_LOGE("CdmaSignalInformation::GetSignalIntensity Value is Invalid.");
222     }
223 
224     return signalIntensity;
225 }
226 
GetSignalLevel() const227 int32_t CdmaSignalInformation::GetSignalLevel() const
228 {
229     int32_t cdmaRssi = GetCdmaRssi();
230     int32_t level = SIGNAL_LEVEL_INVALID;
231     if (ValidateCdmaValue()) {
232         // Reference: TS 27.007 section 8.69
233         for (int32_t i = signalBar_; i >= 0; --i) {
234             if (cdmaRssi >= CDMA_SIGNAL_THRESHOLD[i]) {
235                 level = i;
236                 break;
237             }
238         }
239     }
240     return level;
241 }
242 
GetNetworkType() const243 SignalInformation::NetworkType CdmaSignalInformation::GetNetworkType() const
244 {
245     return SignalInformation::NetworkType::CDMA;
246 }
247 
ToString() const248 std::string CdmaSignalInformation::ToString() const
249 {
250     int32_t netWorkType = static_cast<int32_t>(CdmaSignalInformation::GetNetworkType());
251     std::string content(",networkType:" + std::to_string(netWorkType) + ",cdmaRssi:" + std::to_string(cdmaRssi_) +
252         ",cdmaEcno:" + std::to_string(cdmaEcno_) +
253         ",signalLevel:" + std::to_string(CdmaSignalInformation::GetSignalLevel()));
254     return content;
255 }
256 
NewInstance() const257 sptr<SignalInformation> CdmaSignalInformation::NewInstance() const
258 {
259     std::unique_ptr<CdmaSignalInformation> cdma = std::make_unique<CdmaSignalInformation>();
260     if (cdma == nullptr) {
261         return nullptr;
262     }
263     cdma->SetValue(this->cdmaRssi_, this->cdmaEcno_);
264     return cdma.release();
265 }
266 
Marshalling(Parcel & parcel) const267 bool CdmaSignalInformation::Marshalling(Parcel &parcel) const
268 {
269     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::CDMA))) {
270         return false;
271     }
272     if (!parcel.WriteInt32(cdmaRssi_)) {
273         return false;
274     }
275     if (!parcel.WriteInt32(cdmaEcno_)) {
276         return false;
277     }
278     return true;
279 }
280 
Unmarshalling(Parcel & parcel)281 std::unique_ptr<CdmaSignalInformation> CdmaSignalInformation::Unmarshalling(Parcel &parcel)
282 {
283     std::unique_ptr<CdmaSignalInformation> signal = std::make_unique<CdmaSignalInformation>();
284     if (signal && !signal->ReadFromParcel(parcel)) {
285         signal = nullptr;
286     }
287     return signal;
288 }
289 
ReadFromParcel(Parcel & parcel)290 bool CdmaSignalInformation::ReadFromParcel(Parcel &parcel)
291 {
292     if (!parcel.ReadInt32(cdmaRssi_)) {
293         return false;
294     }
295     if (!parcel.ReadInt32(cdmaEcno_)) {
296         return false;
297     }
298     return true;
299 }
300 
ValidateCdmaValue() const301 bool CdmaSignalInformation::ValidateCdmaValue() const
302 {
303     return cdmaRssi_ < SIGNAL_RSSI_MAXIMUM;
304 }
305 
operator =(const LteSignalInformation & lte)306 LteSignalInformation &LteSignalInformation::operator=(const LteSignalInformation &lte)
307 {
308     signalBar_ = lte.signalBar_;
309     rxlev_ = lte.rxlev_;
310     lteRsrp_ = lte.lteRsrp_;
311     lteRsrq_ = lte.lteRsrq_;
312     lteSnr_ = lte.lteSnr_;
313     return *this;
314 }
315 
operator ==(const LteSignalInformation & lte) const316 bool LteSignalInformation::operator==(const LteSignalInformation &lte) const
317 {
318     return rxlev_ == lte.rxlev_ && lteRsrp_ == lte.lteRsrp_ && lteRsrq_ == lte.lteRsrq_ && lteSnr_ == lte.lteSnr_;
319 }
320 
SetValue(const int32_t rxlev,const int32_t lteRsrp,const int32_t lteRsrq,const int32_t lteSnr)321 void LteSignalInformation::SetValue(
322     const int32_t rxlev, const int32_t lteRsrp, const int32_t lteRsrq, const int32_t lteSnr)
323 {
324     rxlev_ = rxlev;
325     lteRsrp_ = lteRsrp;
326     lteRsrq_ = lteRsrq;
327     lteSnr_ = lteSnr;
328 }
329 
GetRxlev() const330 int32_t LteSignalInformation::GetRxlev() const
331 {
332     return rxlev_;
333 }
334 
GetRsrp() const335 int32_t LteSignalInformation::GetRsrp() const
336 {
337     return lteRsrp_;
338 }
339 
GetRsrq() const340 int32_t LteSignalInformation::GetRsrq() const
341 {
342     return lteRsrq_;
343 }
344 
GetSnr() const345 int32_t LteSignalInformation::GetSnr() const
346 {
347     return lteSnr_;
348 }
349 
GetSignalIntensity() const350 int32_t LteSignalInformation::GetSignalIntensity() const
351 {
352     int32_t signalIntensity = SIGNAL_INTENSITY_INVALID;
353 
354     if (ValidateLteValue()) {
355         signalIntensity = GetRsrp();
356     } else {
357         TELEPHONY_LOGE("LteSignalInformation::GetSignalIntensity Value is Invalid.");
358     }
359 
360     return signalIntensity;
361 }
362 
GetSignalLevel() const363 int32_t LteSignalInformation::GetSignalLevel() const
364 {
365     int32_t level = SIGNAL_LEVEL_INVALID;
366     int32_t lteRsrp = GetRsrp();
367     if (ValidateLteValue()) {
368         // Reference: TS 27.007 section 8.69
369         for (int32_t i = signalBar_; i >= 0; --i) {
370             if (lteRsrp >= LTE_SIGNAL_THRESHOLD[i]) {
371                 level = i;
372                 break;
373             }
374         }
375     } else {
376         TELEPHONY_LOGE("LteSignalInformation::GetSignalLevel Value is Invalid.");
377     }
378     return level;
379 }
380 
GetNetworkType() const381 SignalInformation::NetworkType LteSignalInformation::GetNetworkType() const
382 {
383     return SignalInformation::NetworkType::LTE;
384 }
385 
ToString() const386 std::string LteSignalInformation::ToString() const
387 {
388     int32_t netWorkType = static_cast<int32_t>(LteSignalInformation::GetNetworkType());
389     std::string content("networkType:" + std::to_string(netWorkType) + ",lteRsrp:" + std::to_string(lteRsrp_) +
390         ",lteRsrq:" + std::to_string(lteRsrq_) + ",lteSnr:" + std::to_string(lteSnr_) + ",lteRxlev:" +
391         std::to_string(rxlev_) + ",signalLevel:" + std::to_string(LteSignalInformation::GetSignalLevel()));
392     return content;
393 }
394 
NewInstance() const395 sptr<SignalInformation> LteSignalInformation::NewInstance() const
396 {
397     std::unique_ptr<LteSignalInformation> lte = std::make_unique<LteSignalInformation>();
398     if (lte == nullptr) {
399         return nullptr;
400     }
401     lte->SetValue(this->rxlev_, this->lteRsrp_, this->lteRsrq_, this->lteSnr_);
402     return lte.release();
403 }
404 
Marshalling(Parcel & parcel) const405 bool LteSignalInformation::Marshalling(Parcel &parcel) const
406 {
407     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::LTE))) {
408         return false;
409     }
410     if (!parcel.WriteInt32(rxlev_)) {
411         return false;
412     }
413     if (!parcel.WriteInt32(lteRsrp_)) {
414         return false;
415     }
416     if (!parcel.WriteInt32(lteRsrq_)) {
417         return false;
418     }
419     if (!parcel.WriteInt32(lteSnr_)) {
420         return false;
421     }
422     return true;
423 }
424 
Unmarshalling(Parcel & parcel)425 std::unique_ptr<LteSignalInformation> LteSignalInformation::Unmarshalling(Parcel &parcel)
426 {
427     std::unique_ptr<LteSignalInformation> signal = std::make_unique<LteSignalInformation>();
428     if (signal && !signal->ReadFromParcel(parcel)) {
429         signal = nullptr;
430     }
431     return signal;
432 }
433 
ReadFromParcel(Parcel & parcel)434 bool LteSignalInformation::ReadFromParcel(Parcel &parcel)
435 {
436     if (!parcel.ReadInt32(rxlev_)) {
437         return false;
438     }
439     if (!parcel.ReadInt32(lteRsrp_)) {
440         return false;
441     }
442     if (!parcel.ReadInt32(lteRsrq_)) {
443         return false;
444     }
445     if (!parcel.ReadInt32(lteSnr_)) {
446         return false;
447     }
448     return true;
449 }
450 
ValidateLteValue() const451 bool LteSignalInformation::ValidateLteValue() const
452 {
453     return lteRsrp_ < SIGNAL_RSSI_MAXIMUM;
454 }
455 
operator =(const WcdmaSignalInformation & wcdma)456 WcdmaSignalInformation &WcdmaSignalInformation::operator=(const WcdmaSignalInformation &wcdma)
457 {
458     signalBar_ = wcdma.signalBar_;
459     wcdmaRxlev_ = wcdma.wcdmaRxlev_;
460     wcdmaRscp_ = wcdma.wcdmaRscp_;
461     wcdmaEcio_ = wcdma.wcdmaEcio_;
462     wcdmaBer_ = wcdma.wcdmaBer_;
463     return *this;
464 }
465 
operator ==(const WcdmaSignalInformation & wcdma) const466 bool WcdmaSignalInformation::operator==(const WcdmaSignalInformation &wcdma) const
467 {
468     return wcdmaRxlev_ == wcdma.wcdmaRxlev_ && wcdmaRscp_ == wcdma.wcdmaRscp_ && wcdmaEcio_ == wcdma.wcdmaEcio_ &&
469         wcdmaBer_ == wcdma.wcdmaBer_;
470 }
471 
SetValue(const int32_t wcdmaRxlev,const int32_t wcdmaRscp,const int32_t wcdmaEcio,const int32_t wcdmaBer)472 void WcdmaSignalInformation::SetValue(
473     const int32_t wcdmaRxlev, const int32_t wcdmaRscp, const int32_t wcdmaEcio, const int32_t wcdmaBer)
474 {
475     wcdmaRxlev_ = wcdmaRxlev;
476     wcdmaRscp_ = wcdmaRscp;
477     wcdmaEcio_ = wcdmaEcio;
478     wcdmaBer_ = wcdmaBer;
479 }
480 
GetRxlev() const481 int32_t WcdmaSignalInformation::GetRxlev() const
482 {
483     return wcdmaRxlev_;
484 }
485 
GetRscp() const486 int32_t WcdmaSignalInformation::GetRscp() const
487 {
488     return wcdmaRscp_;
489 }
490 
GetEcno() const491 int32_t WcdmaSignalInformation::GetEcno() const
492 {
493     return wcdmaEcio_;
494 }
495 
GetBer() const496 int32_t WcdmaSignalInformation::GetBer() const
497 {
498     return wcdmaBer_;
499 }
500 
GetSignalIntensity() const501 int32_t WcdmaSignalInformation::GetSignalIntensity() const
502 {
503     int32_t signalIntensity = SIGNAL_INTENSITY_INVALID;
504 
505     if (ValidateWcdmaValue()) {
506         signalIntensity = GetRscp();
507     } else {
508         TELEPHONY_LOGE("WcdmaSignalInformation::GetSignalIntensity Value is Invalid.");
509     }
510 
511     return signalIntensity;
512 }
513 
GetSignalLevel() const514 int32_t WcdmaSignalInformation::GetSignalLevel() const
515 {
516     int32_t level = SIGNAL_LEVEL_INVALID;
517     int32_t wcdmaRscp = GetRscp();
518     if (ValidateWcdmaValue()) {
519         // Reference: TS 27.007 section 8.69
520         for (int32_t i = signalBar_; i >= 0; --i) {
521             if (wcdmaRscp >= WCDMA_SIGNAL_THRESHOLD[i]) {
522                 level = i;
523                 break;
524             }
525         }
526     } else {
527         TELEPHONY_LOGE("WcdmaSignalInformation::GetSignalLevel Value is Invalid.");
528     }
529     return level;
530 }
531 
GetNetworkType() const532 SignalInformation::NetworkType WcdmaSignalInformation::GetNetworkType() const
533 {
534     return SignalInformation::NetworkType::WCDMA;
535 }
536 
ToString() const537 std::string WcdmaSignalInformation::ToString() const
538 {
539     int32_t netWorkType = static_cast<int32_t>(WcdmaSignalInformation::GetNetworkType());
540     std::string content("networkType:" + std::to_string(netWorkType) + ",wcdmaRscp:" + std::to_string(wcdmaRscp_) +
541         ",wcdmaEcio:" + std::to_string(wcdmaEcio_) + ",wcdmaBer:" + std::to_string(wcdmaBer_) + ",wcdmaRxlev:" +
542         std::to_string(wcdmaRxlev_) + ",signalLevel:" + std::to_string(WcdmaSignalInformation::GetSignalLevel()));
543     return content;
544 }
545 
NewInstance() const546 sptr<SignalInformation> WcdmaSignalInformation::NewInstance() const
547 {
548     std::unique_ptr<WcdmaSignalInformation> wcdma = std::make_unique<WcdmaSignalInformation>();
549     if (wcdma == nullptr) {
550         return nullptr;
551     }
552     wcdma->SetValue(this->wcdmaRxlev_, this->wcdmaRscp_, this->wcdmaEcio_, this->wcdmaBer_);
553     return wcdma.release();
554 }
555 
Marshalling(Parcel & parcel) const556 bool WcdmaSignalInformation::Marshalling(Parcel &parcel) const
557 {
558     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::WCDMA))) {
559         return false;
560     }
561     if (!parcel.WriteInt32(wcdmaRxlev_)) {
562         return false;
563     }
564     if (!parcel.WriteInt32(wcdmaRscp_)) {
565         return false;
566     }
567     if (!parcel.WriteInt32(wcdmaEcio_)) {
568         return false;
569     }
570     if (!parcel.WriteInt32(wcdmaBer_)) {
571         return false;
572     }
573     return true;
574 }
575 
Unmarshalling(Parcel & parcel)576 std::unique_ptr<WcdmaSignalInformation> WcdmaSignalInformation::Unmarshalling(Parcel &parcel)
577 {
578     std::unique_ptr<WcdmaSignalInformation> signal = std::make_unique<WcdmaSignalInformation>();
579     if (signal && !signal->ReadFromParcel(parcel)) {
580         signal = nullptr;
581     }
582     return signal;
583 }
584 
ReadFromParcel(Parcel & parcel)585 bool WcdmaSignalInformation::ReadFromParcel(Parcel &parcel)
586 {
587     if (!parcel.ReadInt32(wcdmaRxlev_)) {
588         return false;
589     }
590     if (!parcel.ReadInt32(wcdmaRscp_)) {
591         return false;
592     }
593     if (!parcel.ReadInt32(wcdmaEcio_)) {
594         return false;
595     }
596     if (!parcel.ReadInt32(wcdmaBer_)) {
597         return false;
598     }
599     return true;
600 }
601 
ValidateWcdmaValue() const602 bool WcdmaSignalInformation::ValidateWcdmaValue() const
603 {
604     return wcdmaRscp_ < SIGNAL_RSSI_MAXIMUM;
605 }
606 
operator =(const TdScdmaSignalInformation & tdScdma)607 TdScdmaSignalInformation &TdScdmaSignalInformation::operator=(const TdScdmaSignalInformation &tdScdma)
608 {
609     signalBar_ = tdScdma.signalBar_;
610     tdScdmaRscp_ = tdScdma.tdScdmaRscp_;
611     return *this;
612 }
613 
operator ==(const TdScdmaSignalInformation & tdScdma) const614 bool TdScdmaSignalInformation::operator==(const TdScdmaSignalInformation &tdScdma) const
615 {
616     return tdScdmaRscp_ == tdScdma.tdScdmaRscp_;
617 }
618 
SetValue(const int32_t tdScdmaRscp)619 void TdScdmaSignalInformation::SetValue(const int32_t tdScdmaRscp)
620 {
621     tdScdmaRscp_ = tdScdmaRscp;
622 }
623 
GetRscp() const624 int32_t TdScdmaSignalInformation::GetRscp() const
625 {
626     return tdScdmaRscp_;
627 }
628 
GetSignalIntensity() const629 int32_t TdScdmaSignalInformation::GetSignalIntensity() const
630 {
631     int32_t signalIntensity = SIGNAL_INTENSITY_INVALID;
632 
633     if (ValidateTdScdmaValue()) {
634         signalIntensity = GetRscp();
635     } else {
636         TELEPHONY_LOGE("TdScdmaSignalInformation::GetSignalIntensity Value is Invalid.");
637     }
638 
639     return signalIntensity;
640 }
641 
GetSignalLevel() const642 int32_t TdScdmaSignalInformation::GetSignalLevel() const
643 {
644     int32_t tdScdmaRscp = GetRscp();
645     int32_t level = SIGNAL_LEVEL_INVALID;
646     if (ValidateTdScdmaValue()) {
647         // Reference: TS 27.007 section 8.69
648         for (int32_t i = signalBar_; i >= 0; --i) {
649             if (tdScdmaRscp >= TD_SCDMA_SIGNAL_THRESHOLD[i]) {
650                 level = i;
651                 break;
652             }
653         }
654     }
655     return level;
656 }
657 
GetNetworkType() const658 SignalInformation::NetworkType TdScdmaSignalInformation::GetNetworkType() const
659 {
660     return SignalInformation::NetworkType::TDSCDMA;
661 }
662 
ToString() const663 std::string TdScdmaSignalInformation::ToString() const
664 {
665     int32_t netWorkType = static_cast<int32_t>(TdScdmaSignalInformation::GetNetworkType());
666     std::string content("networkType:" + std::to_string(netWorkType) + ",tdScdmaRscp:" + std::to_string(tdScdmaRscp_) +
667         ",signalLevel:" + std::to_string(TdScdmaSignalInformation::GetSignalLevel()));
668     return content;
669 }
670 
NewInstance() const671 sptr<SignalInformation> TdScdmaSignalInformation::NewInstance() const
672 {
673     std::unique_ptr<TdScdmaSignalInformation> tdScdma = std::make_unique<TdScdmaSignalInformation>();
674     if (tdScdma == nullptr) {
675         return nullptr;
676     }
677     tdScdma->SetValue(this->tdScdmaRscp_);
678     return tdScdma.release();
679 }
680 
Marshalling(Parcel & parcel) const681 bool TdScdmaSignalInformation::Marshalling(Parcel &parcel) const
682 {
683     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::TDSCDMA))) {
684         return false;
685     }
686     if (!parcel.WriteInt32(tdScdmaRscp_)) {
687         return false;
688     }
689     return true;
690 }
691 
Unmarshalling(Parcel & parcel)692 std::unique_ptr<TdScdmaSignalInformation> TdScdmaSignalInformation::Unmarshalling(Parcel &parcel)
693 {
694     std::unique_ptr<TdScdmaSignalInformation> signal = std::make_unique<TdScdmaSignalInformation>();
695     if (signal && !signal->ReadFromParcel(parcel)) {
696         signal = nullptr;
697     }
698     return signal;
699 }
700 
ReadFromParcel(Parcel & parcel)701 bool TdScdmaSignalInformation::ReadFromParcel(Parcel &parcel)
702 {
703     if (!parcel.ReadInt32(tdScdmaRscp_)) {
704         return false;
705     }
706     return true;
707 }
708 
ValidateTdScdmaValue() const709 bool TdScdmaSignalInformation::ValidateTdScdmaValue() const
710 {
711     return tdScdmaRscp_ < SIGNAL_RSSI_MAXIMUM;
712 }
713 
operator =(const NrSignalInformation & nr)714 NrSignalInformation &NrSignalInformation::operator=(const NrSignalInformation &nr)
715 {
716     signalBar_ = nr.signalBar_;
717     nrRsrp_ = nr.nrRsrp_;
718     nrRsrq_ = nr.nrRsrq_;
719     nrSinr_ = nr.nrSinr_;
720     return *this;
721 }
722 
operator ==(const NrSignalInformation & nr) const723 bool NrSignalInformation::operator==(const NrSignalInformation &nr) const
724 {
725     return nrRsrp_ == nr.nrRsrp_ && nrRsrq_ == nr.nrRsrq_ && nrSinr_ == nr.nrSinr_;
726 }
727 
SetValue(const int32_t rsrp,const int32_t rsrq,const int32_t sinr)728 void NrSignalInformation::SetValue(const int32_t rsrp, const int32_t rsrq, const int32_t sinr)
729 {
730     nrRsrp_ = rsrp;
731     nrRsrq_ = rsrq;
732     nrSinr_ = sinr;
733 }
734 
GetRsrp() const735 int32_t NrSignalInformation::GetRsrp() const
736 {
737     return nrRsrp_;
738 }
739 
GetRsrq() const740 int32_t NrSignalInformation::GetRsrq() const
741 {
742     return nrRsrq_;
743 }
744 
GetSinr() const745 int32_t NrSignalInformation::GetSinr() const
746 {
747     return nrSinr_;
748 }
749 
GetSignalIntensity() const750 int32_t NrSignalInformation::GetSignalIntensity() const
751 {
752     int32_t signalIntensity = SIGNAL_INTENSITY_INVALID;
753 
754     if (ValidateNrValue()) {
755         signalIntensity = GetRsrp();
756     } else {
757         TELEPHONY_LOGE("NrSignalInformation::GetSignalIntensity Value is Invalid.");
758     }
759 
760     return signalIntensity;
761 }
762 
GetSignalLevel() const763 int32_t NrSignalInformation::GetSignalLevel() const
764 {
765     int32_t nrRsrp = GetRsrp();
766     int32_t level = SIGNAL_LEVEL_INVALID;
767     if (ValidateNrValue()) {
768         // Reference: TS 27.007 section 8.69
769         for (int32_t i = signalBar_; i >= 0; --i) {
770             if (nrRsrp >= NR_SIGNAL_THRESHOLD[i]) {
771                 level = i;
772                 break;
773             }
774         }
775     }
776     return level;
777 }
778 
GetNetworkType() const779 SignalInformation::NetworkType NrSignalInformation::GetNetworkType() const
780 {
781     return SignalInformation::NetworkType::NR;
782 }
783 
ToString() const784 std::string NrSignalInformation::ToString() const
785 {
786     int32_t netWorkType = static_cast<int32_t>(NrSignalInformation::GetNetworkType());
787     std::string content("networkType:" + std::to_string(netWorkType) + ",nrRsrp:" + std::to_string(nrRsrp_) +
788                         ",nrRsrq:" + std::to_string(nrRsrq_) + ",nrSinr:" + std::to_string(nrSinr_) +
789                         ",signalLevel:" + std::to_string(NrSignalInformation::GetSignalLevel()));
790     return content;
791 }
792 
NewInstance() const793 sptr<SignalInformation> NrSignalInformation::NewInstance() const
794 {
795     std::unique_ptr<NrSignalInformation> nr = std::make_unique<NrSignalInformation>();
796     if (nr == nullptr) {
797         return nullptr;
798     }
799     nr->SetValue(this->nrRsrp_, this->nrRsrq_, this->nrSinr_);
800     return nr.release();
801 }
802 
Marshalling(Parcel & parcel) const803 bool NrSignalInformation::Marshalling(Parcel &parcel) const
804 {
805     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::NR))) {
806         return false;
807     }
808     if (!parcel.WriteInt32(nrRsrp_)) {
809         return false;
810     }
811     if (!parcel.WriteInt32(nrRsrq_)) {
812         return false;
813     }
814     if (!parcel.WriteInt32(nrSinr_)) {
815         return false;
816     }
817     return true;
818 }
819 
Unmarshalling(Parcel & parcel)820 std::unique_ptr<NrSignalInformation> NrSignalInformation::Unmarshalling(Parcel &parcel)
821 {
822     std::unique_ptr<NrSignalInformation> signal = std::make_unique<NrSignalInformation>();
823     if (signal && !signal->ReadFromParcel(parcel)) {
824         signal = nullptr;
825     }
826     return signal;
827 }
828 
ReadFromParcel(Parcel & parcel)829 bool NrSignalInformation::ReadFromParcel(Parcel &parcel)
830 {
831     if (!parcel.ReadInt32(nrRsrp_)) {
832         return false;
833     }
834     if (!parcel.ReadInt32(nrRsrq_)) {
835         return false;
836     }
837     if (!parcel.ReadInt32(nrSinr_)) {
838         return false;
839     }
840     return true;
841 }
842 
ValidateNrValue() const843 bool NrSignalInformation::ValidateNrValue() const
844 {
845     return nrRsrp_ < SIGNAL_RSSI_MAXIMUM;
846 }
847 } // namespace Telephony
848 } // namespace OHOS
849