• 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 GSM_RXLEV_MINIMUM = 0;
24 constexpr int32_t GSM_RXLEV_MAXIMUM = 63;
25 constexpr int32_t GSM_RSSI_INVALID = -111;
26 constexpr int32_t CDMA_RSSI_MINIMUM = 0;
27 constexpr int32_t CDMA_RSSI_INVALID = -113;
28 constexpr int32_t LTE_RSRP_MINIMUM = 0;
29 constexpr int32_t LTE_RSRP_MAXIMUM = 99;
30 constexpr int32_t LTE_RSSI_INVALID = -141;
31 constexpr int32_t WCDMA_RSCP_MINIMUM = 0;
32 constexpr int32_t WCDMA_RSCP_MAXIMUM = 99;
33 constexpr int32_t WCDMA_RSSI_INVALID = -121;
34 constexpr int32_t TD_SCDMA_RSCP_MINIMUM = 25;
35 constexpr int32_t TD_SCDMA_RSCP_MAXIMUM = 112;
36 constexpr int32_t TD_SCDMA_RSSI_INVALID = -113;
37 constexpr int32_t NR_RSRP_MINIMUM = 0;
38 constexpr int32_t NR_RSRP_MAXIMUM = 99;
39 constexpr int32_t NR_RSSI_INVALID = -141;
40 constexpr int32_t SIGNAL_LEVEL_INVALID = 0;
41 constexpr int32_t SIGNAL_FIVE_BARS = 5;
42 constexpr int32_t SIGNAL_FOUR_BARS = 4;
43 const int32_t *GSM_SIGNAL_THRESHOLD = SignalInformation::GSM_SIGNAL_THRESHOLD_5BAR;
44 const int32_t *CDMA_SIGNAL_THRESHOLD = SignalInformation::CDMA_SIGNAL_THRESHOLD_5BAR;
45 const int32_t *LTE_SIGNAL_THRESHOLD = SignalInformation::LTE_SIGNAL_THRESHOLD_5BAR;
46 const int32_t *WCDMA_SIGNAL_THRESHOLD = SignalInformation::WCDMA_SIGNAL_THRESHOLD_5BAR;
47 const int32_t *TD_SCDMA_SIGNAL_THRESHOLD = SignalInformation::TD_SCDMA_SIGNAL_THRESHOLD_5BAR;
48 const int32_t *NR_SIGNAL_THRESHOLD = SignalInformation::NR_SIGNAL_THRESHOLD_5BAR;
49 int32_t SignalInformation::signalBar_ = SIGNAL_FIVE_BARS;
50 
SignalInformation()51 SignalInformation::SignalInformation()
52 {
53     InitSignalBar();
54 }
55 
InitSignalBar(const int32_t bar)56 void SignalInformation::InitSignalBar(const int32_t bar)
57 {
58     if (bar == SIGNAL_FOUR_BARS) {
59         GSM_SIGNAL_THRESHOLD = SignalInformation::GSM_SIGNAL_THRESHOLD_4BAR;
60         CDMA_SIGNAL_THRESHOLD = SignalInformation::CDMA_SIGNAL_THRESHOLD_4BAR;
61         LTE_SIGNAL_THRESHOLD = SignalInformation::LTE_SIGNAL_THRESHOLD_4BAR;
62         WCDMA_SIGNAL_THRESHOLD = SignalInformation::WCDMA_SIGNAL_THRESHOLD_4BAR;
63         TD_SCDMA_SIGNAL_THRESHOLD = SignalInformation::TD_SCDMA_SIGNAL_THRESHOLD_4BAR;
64         NR_SIGNAL_THRESHOLD = SignalInformation::NR_SIGNAL_THRESHOLD_4BAR;
65         signalBar_ = SIGNAL_FOUR_BARS;
66     } else {
67         GSM_SIGNAL_THRESHOLD = SignalInformation::GSM_SIGNAL_THRESHOLD_5BAR;
68         CDMA_SIGNAL_THRESHOLD = SignalInformation::CDMA_SIGNAL_THRESHOLD_5BAR;
69         LTE_SIGNAL_THRESHOLD = SignalInformation::LTE_SIGNAL_THRESHOLD_5BAR;
70         WCDMA_SIGNAL_THRESHOLD = SignalInformation::WCDMA_SIGNAL_THRESHOLD_5BAR;
71         TD_SCDMA_SIGNAL_THRESHOLD = SignalInformation::TD_SCDMA_SIGNAL_THRESHOLD_5BAR;
72         NR_SIGNAL_THRESHOLD = SignalInformation::NR_SIGNAL_THRESHOLD_5BAR;
73         signalBar_ = SIGNAL_FIVE_BARS;
74     }
75 }
76 
Unmarshalling(Parcel & parcel)77 std::unique_ptr<SignalInformation> SignalInformation::Unmarshalling(Parcel &parcel)
78 {
79     return nullptr;
80 }
81 
operator ==(const GsmSignalInformation & gsm) const82 bool GsmSignalInformation::operator==(const GsmSignalInformation &gsm) const
83 {
84     return gsmRxlev_ == gsm.gsmRxlev_ && gsmBer_ == gsm.gsmBer_;
85 }
86 
SetValue(const int32_t gsmRssi,const int32_t gsmBer)87 void GsmSignalInformation::SetValue(const int32_t gsmRssi, const int32_t gsmBer)
88 {
89     gsmRxlev_ = gsmRssi;
90     gsmBer_ = gsmBer;
91 }
92 
GetRssi() const93 int32_t GsmSignalInformation::GetRssi() const
94 {
95     return gsmRxlev_;
96 }
97 
GetGsmBer() const98 int32_t GsmSignalInformation::GetGsmBer() const
99 {
100     return gsmBer_;
101 }
102 
GetSignalLevel() const103 int32_t GsmSignalInformation::GetSignalLevel() const
104 {
105     int32_t level = SIGNAL_LEVEL_INVALID;
106     int32_t gsmRxlev = GetRssi();
107     int32_t gsmRssi = GSM_RSSI_INVALID;
108     if (ValidateGsmValue()) {
109         // Reference: TS 27.007 section 8.69
110         gsmRssi += gsmRxlev;
111         for (int32_t i = signalBar_; i >= 0; --i) {
112             if (gsmRssi >= GSM_SIGNAL_THRESHOLD[i]) {
113                 level = i;
114                 break;
115             }
116         }
117     } else {
118         TELEPHONY_LOGE("GsmSignalInformation::GetSignalLevel Value is Invalid\n");
119     }
120     return level;
121 }
122 
GetNetworkType() const123 SignalInformation::NetworkType GsmSignalInformation::GetNetworkType() const
124 {
125     return SignalInformation::NetworkType::GSM;
126 }
127 
ToString() const128 std::string GsmSignalInformation::ToString() const
129 {
130     int32_t netWorkType = static_cast<int32_t>(GsmSignalInformation::GetNetworkType());
131     std::string content("networkType:" + std::to_string(netWorkType) + ",gsmRxlev:" + std::to_string(gsmRxlev_) +
132         ",signalLevel:" + std::to_string(GsmSignalInformation::GetSignalLevel()) +
133         ",gsmBer:" + std::to_string(gsmBer_));
134     return content;
135 }
136 
NewInstance() const137 sptr<SignalInformation> GsmSignalInformation::NewInstance() const
138 {
139     std::unique_ptr<GsmSignalInformation> gsm = std::make_unique<GsmSignalInformation>();
140     if (gsm == nullptr) {
141         return nullptr;
142     }
143     gsm->SetValue(this->gsmRxlev_, this->gsmBer_);
144     return gsm.release();
145 }
146 
Marshalling(Parcel & parcel) const147 bool GsmSignalInformation::Marshalling(Parcel &parcel) const
148 {
149     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::GSM))) {
150         return false;
151     }
152     if (!parcel.WriteInt32(gsmRxlev_)) {
153         return false;
154     }
155     if (!parcel.WriteInt32(gsmBer_)) {
156         return false;
157     }
158     return true;
159 }
160 
Unmarshalling(Parcel & parcel)161 std::unique_ptr<GsmSignalInformation> GsmSignalInformation::Unmarshalling(Parcel &parcel)
162 {
163     std::unique_ptr<GsmSignalInformation> signal = std::make_unique<GsmSignalInformation>();
164     if (signal && !signal->ReadFromParcel(parcel)) {
165         signal = nullptr;
166     }
167     return signal;
168 }
169 
ReadFromParcel(Parcel & parcel)170 bool GsmSignalInformation::ReadFromParcel(Parcel &parcel)
171 {
172     if (!parcel.ReadInt32(gsmRxlev_)) {
173         return false;
174     }
175     if (!parcel.ReadInt32(gsmBer_)) {
176         return false;
177     }
178     return true;
179 }
180 
ValidateGsmValue() const181 bool GsmSignalInformation::ValidateGsmValue() const
182 {
183     return (gsmRxlev_ > GSM_RXLEV_MINIMUM && gsmRxlev_ <= GSM_RXLEV_MAXIMUM);
184 }
185 
SetValue(const int32_t cdmaRssi,const int32_t cdmaEcno)186 void CdmaSignalInformation::SetValue(const int32_t cdmaRssi, const int32_t cdmaEcno)
187 {
188     cdmaRssi_ = cdmaRssi;
189     cdmaEcno_ = cdmaEcno;
190 }
191 
operator ==(const CdmaSignalInformation & cdma) const192 bool CdmaSignalInformation::operator==(const CdmaSignalInformation &cdma) const
193 {
194     return (cdmaRssi_ == cdma.cdmaRssi_ && cdmaEcno_ == cdma.cdmaEcno_);
195 }
196 
GetCdmaRssi() const197 int32_t CdmaSignalInformation::GetCdmaRssi() const
198 {
199     return cdmaRssi_;
200 }
201 
GetSignalLevel() const202 int32_t CdmaSignalInformation::GetSignalLevel() const
203 {
204     int32_t cdmaRssi = GetCdmaRssi();
205     int32_t level = SIGNAL_LEVEL_INVALID;
206     int32_t rssi = CDMA_RSSI_INVALID;
207     if (ValidateCdmaValue()) {
208         // Reference: TS 27.007 section 8.69
209         rssi += cdmaRssi;
210         for (int32_t i = signalBar_; i >= 0; --i) {
211             if (rssi >= CDMA_SIGNAL_THRESHOLD[i]) {
212                 level = i;
213                 break;
214             }
215         }
216     }
217     return level;
218 }
219 
GetNetworkType() const220 SignalInformation::NetworkType CdmaSignalInformation::GetNetworkType() const
221 {
222     return SignalInformation::NetworkType::CDMA;
223 }
224 
ToString() const225 std::string CdmaSignalInformation::ToString() const
226 {
227     int32_t netWorkType = static_cast<int32_t>(CdmaSignalInformation::GetNetworkType());
228     std::string content(",networkType:" + std::to_string(netWorkType) + ",cdmaRssi:" + std::to_string(cdmaRssi_) +
229         ",cdmaEcno:" + std::to_string(cdmaEcno_) +
230         ",signalLevel:" + std::to_string(CdmaSignalInformation::GetSignalLevel()));
231     return content;
232 }
233 
NewInstance() const234 sptr<SignalInformation> CdmaSignalInformation::NewInstance() const
235 {
236     std::unique_ptr<CdmaSignalInformation> cdma = std::make_unique<CdmaSignalInformation>();
237     if (cdma == nullptr) {
238         return nullptr;
239     }
240     cdma->SetValue(this->cdmaRssi_, this->cdmaEcno_);
241     return cdma.release();
242 }
243 
Marshalling(Parcel & parcel) const244 bool CdmaSignalInformation::Marshalling(Parcel &parcel) const
245 {
246     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::CDMA))) {
247         return false;
248     }
249     if (!parcel.WriteInt32(cdmaRssi_)) {
250         return false;
251     }
252     if (!parcel.WriteInt32(cdmaEcno_)) {
253         return false;
254     }
255     return true;
256 }
257 
Unmarshalling(Parcel & parcel)258 std::unique_ptr<CdmaSignalInformation> CdmaSignalInformation::Unmarshalling(Parcel &parcel)
259 {
260     std::unique_ptr<CdmaSignalInformation> signal = std::make_unique<CdmaSignalInformation>();
261     if (signal && !signal->ReadFromParcel(parcel)) {
262         signal = nullptr;
263     }
264     return signal;
265 }
266 
ReadFromParcel(Parcel & parcel)267 bool CdmaSignalInformation::ReadFromParcel(Parcel &parcel)
268 {
269     if (!parcel.ReadInt32(cdmaRssi_)) {
270         return false;
271     }
272     if (!parcel.ReadInt32(cdmaEcno_)) {
273         return false;
274     }
275     return true;
276 }
277 
ValidateCdmaValue() const278 bool CdmaSignalInformation::ValidateCdmaValue() const
279 {
280     return (cdmaRssi_ > CDMA_RSSI_MINIMUM && cdmaRssi_ < -CDMA_RSSI_INVALID);
281 }
282 
operator ==(const LteSignalInformation & lte) const283 bool LteSignalInformation::operator==(const LteSignalInformation &lte) const
284 {
285     return rxlev_ == lte.rxlev_ && lteRsrp_ == lte.lteRsrp_ && lteRsrq_ == lte.lteRsrq_ && lteSnr_ == lte.lteSnr_;
286 }
287 
SetValue(const int32_t rxlev,const int32_t lteRsrp,const int32_t lteRsrq,const int32_t lteSnr)288 void LteSignalInformation::SetValue(
289     const int32_t rxlev, const int32_t lteRsrp, const int32_t lteRsrq, const int32_t lteSnr)
290 {
291     rxlev_ = rxlev;
292     lteRsrp_ = lteRsrp;
293     lteRsrq_ = lteRsrq;
294     lteSnr_ = lteSnr;
295 }
296 
GetRxlev() const297 int32_t LteSignalInformation::GetRxlev() const
298 {
299     return rxlev_;
300 }
301 
GetRsrp() const302 int32_t LteSignalInformation::GetRsrp() const
303 {
304     return lteRsrp_;
305 }
306 
GetRsrq() const307 int32_t LteSignalInformation::GetRsrq() const
308 {
309     return lteRsrq_;
310 }
311 
GetSnr() const312 int32_t LteSignalInformation::GetSnr() const
313 {
314     return lteSnr_;
315 }
316 
GetSignalLevel() const317 int32_t LteSignalInformation::GetSignalLevel() const
318 {
319     int32_t level = SIGNAL_LEVEL_INVALID;
320     int32_t lteRsrp = GetRsrp();
321     int32_t lteRssi = LTE_RSSI_INVALID;
322     if (ValidateLteValue()) {
323         // Reference: TS 27.007 section 8.69
324         lteRssi += lteRsrp;
325         for (int32_t i = signalBar_; i >= 0; --i) {
326             if (lteRssi >= LTE_SIGNAL_THRESHOLD[i]) {
327                 level = i;
328                 break;
329             }
330         }
331     } else {
332         TELEPHONY_LOGE("LteSignalInformation::GetSignalLevel Value is Invalid\n");
333     }
334     return level;
335 }
336 
GetNetworkType() const337 SignalInformation::NetworkType LteSignalInformation::GetNetworkType() const
338 {
339     return SignalInformation::NetworkType::LTE;
340 }
341 
ToString() const342 std::string LteSignalInformation::ToString() const
343 {
344     int32_t netWorkType = static_cast<int32_t>(LteSignalInformation::GetNetworkType());
345     std::string content("networkType:" + std::to_string(netWorkType) + ",lteRsrp:" + std::to_string(lteRsrp_) +
346         ",lteRsrq:" + std::to_string(lteRsrq_) + ",lteSnr:" + std::to_string(lteSnr_) + ",lteRxlev:" +
347         std::to_string(rxlev_) + ",signalLevel:" + std::to_string(LteSignalInformation::GetSignalLevel()));
348     return content;
349 }
350 
NewInstance() const351 sptr<SignalInformation> LteSignalInformation::NewInstance() const
352 {
353     std::unique_ptr<LteSignalInformation> lte = std::make_unique<LteSignalInformation>();
354     if (lte == nullptr) {
355         return nullptr;
356     }
357     lte->SetValue(this->rxlev_, this->lteRsrp_, this->lteRsrq_, this->lteSnr_);
358     return lte.release();
359 }
360 
Marshalling(Parcel & parcel) const361 bool LteSignalInformation::Marshalling(Parcel &parcel) const
362 {
363     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::LTE))) {
364         return false;
365     }
366     if (!parcel.WriteInt32(rxlev_)) {
367         return false;
368     }
369     if (!parcel.WriteInt32(lteRsrp_)) {
370         return false;
371     }
372     if (!parcel.WriteInt32(lteRsrq_)) {
373         return false;
374     }
375     if (!parcel.WriteInt32(lteSnr_)) {
376         return false;
377     }
378     return true;
379 }
380 
Unmarshalling(Parcel & parcel)381 std::unique_ptr<LteSignalInformation> LteSignalInformation::Unmarshalling(Parcel &parcel)
382 {
383     std::unique_ptr<LteSignalInformation> signal = std::make_unique<LteSignalInformation>();
384     if (signal && !signal->ReadFromParcel(parcel)) {
385         signal = nullptr;
386     }
387     return signal;
388 }
389 
ReadFromParcel(Parcel & parcel)390 bool LteSignalInformation::ReadFromParcel(Parcel &parcel)
391 {
392     if (!parcel.ReadInt32(rxlev_)) {
393         return false;
394     }
395     if (!parcel.ReadInt32(lteRsrp_)) {
396         return false;
397     }
398     if (!parcel.ReadInt32(lteRsrq_)) {
399         return false;
400     }
401     if (!parcel.ReadInt32(lteSnr_)) {
402         return false;
403     }
404     return true;
405 }
406 
ValidateLteValue() const407 bool LteSignalInformation::ValidateLteValue() const
408 {
409     return (lteRsrp_ > LTE_RSRP_MINIMUM && lteRsrp_ <= LTE_RSRP_MAXIMUM);
410 }
411 
operator ==(const WcdmaSignalInformation & wcdma) const412 bool WcdmaSignalInformation::operator==(const WcdmaSignalInformation &wcdma) const
413 {
414     return wcdmaRxlev_ == wcdma.wcdmaRxlev_ && wcdmaRscp_ == wcdma.wcdmaRscp_ && wcdmaEcio_ == wcdma.wcdmaEcio_ &&
415         wcdmaBer_ == wcdma.wcdmaBer_;
416 }
417 
SetValue(const int32_t wcdmaRxlev,const int32_t wcdmaRscp,const int32_t wcdmaEcio,const int32_t wcdmaBer)418 void WcdmaSignalInformation::SetValue(
419     const int32_t wcdmaRxlev, const int32_t wcdmaRscp, const int32_t wcdmaEcio, const int32_t wcdmaBer)
420 {
421     wcdmaRxlev_ = wcdmaRxlev;
422     wcdmaRscp_ = wcdmaRscp;
423     wcdmaEcio_ = wcdmaEcio;
424     wcdmaBer_ = wcdmaBer;
425 }
426 
GetRxlev() const427 int32_t WcdmaSignalInformation::GetRxlev() const
428 {
429     return wcdmaRxlev_;
430 }
431 
GetRscp() const432 int32_t WcdmaSignalInformation::GetRscp() const
433 {
434     return wcdmaRscp_;
435 }
436 
GetEcno() const437 int32_t WcdmaSignalInformation::GetEcno() const
438 {
439     return wcdmaEcio_;
440 }
441 
GetBer() const442 int32_t WcdmaSignalInformation::GetBer() const
443 {
444     return wcdmaBer_;
445 }
446 
GetSignalLevel() const447 int32_t WcdmaSignalInformation::GetSignalLevel() const
448 {
449     int32_t level = SIGNAL_LEVEL_INVALID;
450     int32_t wcdmaRscp = GetRscp();
451     int32_t wcdmaRssi = WCDMA_RSSI_INVALID;
452     if (ValidateWcdmaValue()) {
453         // Reference: TS 27.007 section 8.69
454         wcdmaRssi += wcdmaRscp;
455         for (int32_t i = signalBar_; i >= 0; --i) {
456             if (wcdmaRssi >= WCDMA_SIGNAL_THRESHOLD[i]) {
457                 level = i;
458                 break;
459             }
460         }
461     } else {
462         TELEPHONY_LOGE("WcdmaSignalInformation::GetSignalLevel Value is Invalid\n");
463     }
464     return level;
465 }
466 
GetNetworkType() const467 SignalInformation::NetworkType WcdmaSignalInformation::GetNetworkType() const
468 {
469     return SignalInformation::NetworkType::WCDMA;
470 }
471 
ToString() const472 std::string WcdmaSignalInformation::ToString() const
473 {
474     int32_t netWorkType = static_cast<int32_t>(WcdmaSignalInformation::GetNetworkType());
475     std::string content("networkType:" + std::to_string(netWorkType) + ",wcdmaRscp:" + std::to_string(wcdmaRscp_) +
476         ",wcdmaEcio:" + std::to_string(wcdmaEcio_) + ",wcdmaBer:" + std::to_string(wcdmaBer_) + ",wcdmaRxlev:" +
477         std::to_string(wcdmaRxlev_) + ",signalLevel:" + std::to_string(WcdmaSignalInformation::GetSignalLevel()));
478     return content;
479 }
480 
NewInstance() const481 sptr<SignalInformation> WcdmaSignalInformation::NewInstance() const
482 {
483     std::unique_ptr<WcdmaSignalInformation> wcdma = std::make_unique<WcdmaSignalInformation>();
484     if (wcdma == nullptr) {
485         return nullptr;
486     }
487     wcdma->SetValue(this->wcdmaRxlev_, this->wcdmaRscp_, this->wcdmaEcio_, this->wcdmaBer_);
488     return wcdma.release();
489 }
490 
Marshalling(Parcel & parcel) const491 bool WcdmaSignalInformation::Marshalling(Parcel &parcel) const
492 {
493     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::WCDMA))) {
494         return false;
495     }
496     if (!parcel.WriteInt32(wcdmaRxlev_)) {
497         return false;
498     }
499     if (!parcel.WriteInt32(wcdmaRscp_)) {
500         return false;
501     }
502     if (!parcel.WriteInt32(wcdmaEcio_)) {
503         return false;
504     }
505     if (!parcel.WriteInt32(wcdmaBer_)) {
506         return false;
507     }
508     return true;
509 }
510 
Unmarshalling(Parcel & parcel)511 std::unique_ptr<WcdmaSignalInformation> WcdmaSignalInformation::Unmarshalling(Parcel &parcel)
512 {
513     std::unique_ptr<WcdmaSignalInformation> signal = std::make_unique<WcdmaSignalInformation>();
514     if (signal && !signal->ReadFromParcel(parcel)) {
515         signal = nullptr;
516     }
517     return signal;
518 }
519 
ReadFromParcel(Parcel & parcel)520 bool WcdmaSignalInformation::ReadFromParcel(Parcel &parcel)
521 {
522     if (!parcel.ReadInt32(wcdmaRxlev_)) {
523         return false;
524     }
525     if (!parcel.ReadInt32(wcdmaRscp_)) {
526         return false;
527     }
528     if (!parcel.ReadInt32(wcdmaEcio_)) {
529         return false;
530     }
531     if (!parcel.ReadInt32(wcdmaBer_)) {
532         return false;
533     }
534     return true;
535 }
536 
ValidateWcdmaValue() const537 bool WcdmaSignalInformation::ValidateWcdmaValue() const
538 {
539     return (wcdmaRscp_ > WCDMA_RSCP_MINIMUM && wcdmaRscp_ <= WCDMA_RSCP_MAXIMUM);
540 }
541 
operator ==(const TdScdmaSignalInformation & tdScdma) const542 bool TdScdmaSignalInformation::operator==(const TdScdmaSignalInformation &tdScdma) const
543 {
544     return tdScdmaRscp_ == tdScdma.tdScdmaRscp_;
545 }
546 
SetValue(const int32_t tdScdmaRscp)547 void TdScdmaSignalInformation::SetValue(const int32_t tdScdmaRscp)
548 {
549     tdScdmaRscp_ = tdScdmaRscp;
550 }
551 
GetRscp() const552 int32_t TdScdmaSignalInformation::GetRscp() const
553 {
554     return (tdScdmaRscp_ >= TD_SCDMA_RSCP_MINIMUM &&
555         tdScdmaRscp_ <= TD_SCDMA_RSCP_MAXIMUM) ? tdScdmaRscp_ : TD_SCDMA_RSSI_INVALID;
556 }
557 
GetSignalLevel() const558 int32_t TdScdmaSignalInformation::GetSignalLevel() const
559 {
560     int32_t tdScdmaRscp = GetRscp();
561     int32_t level = SIGNAL_LEVEL_INVALID;
562     int32_t tdScdmaRssi = TD_SCDMA_RSSI_INVALID;
563     if (ValidateTdScdmaValue()) {
564         // Reference: TS 27.007 section 8.69
565         tdScdmaRssi += tdScdmaRscp;
566         for (int32_t i = signalBar_; i >= 0; --i) {
567             if (tdScdmaRssi >= TD_SCDMA_SIGNAL_THRESHOLD[i]) {
568                 level = i;
569                 break;
570             }
571         }
572     }
573     return level;
574 }
575 
GetNetworkType() const576 SignalInformation::NetworkType TdScdmaSignalInformation::GetNetworkType() const
577 {
578     return SignalInformation::NetworkType::TDSCDMA;
579 }
580 
ToString() const581 std::string TdScdmaSignalInformation::ToString() const
582 {
583     int32_t netWorkType = static_cast<int32_t>(TdScdmaSignalInformation::GetNetworkType());
584     std::string content("networkType:" + std::to_string(netWorkType) + ",tdScdmaRscp:" + std::to_string(tdScdmaRscp_) +
585         ",signalLevel:" + std::to_string(TdScdmaSignalInformation::GetSignalLevel()));
586     return content;
587 }
588 
NewInstance() const589 sptr<SignalInformation> TdScdmaSignalInformation::NewInstance() const
590 {
591     std::unique_ptr<TdScdmaSignalInformation> tdScdma = std::make_unique<TdScdmaSignalInformation>();
592     if (tdScdma == nullptr) {
593         return nullptr;
594     }
595     tdScdma->SetValue(this->tdScdmaRscp_);
596     return tdScdma.release();
597 }
598 
Marshalling(Parcel & parcel) const599 bool TdScdmaSignalInformation::Marshalling(Parcel &parcel) const
600 {
601     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::TDSCDMA))) {
602         return false;
603     }
604     if (!parcel.WriteInt32(tdScdmaRscp_)) {
605         return false;
606     }
607     return true;
608 }
609 
Unmarshalling(Parcel & parcel)610 std::unique_ptr<TdScdmaSignalInformation> TdScdmaSignalInformation::Unmarshalling(Parcel &parcel)
611 {
612     std::unique_ptr<TdScdmaSignalInformation> signal = std::make_unique<TdScdmaSignalInformation>();
613     if (signal && !signal->ReadFromParcel(parcel)) {
614         signal = nullptr;
615     }
616     return signal;
617 }
618 
ReadFromParcel(Parcel & parcel)619 bool TdScdmaSignalInformation::ReadFromParcel(Parcel &parcel)
620 {
621     if (!parcel.ReadInt32(tdScdmaRscp_)) {
622         return false;
623     }
624     return true;
625 }
626 
ValidateTdScdmaValue() const627 bool TdScdmaSignalInformation::ValidateTdScdmaValue() const
628 {
629     return (tdScdmaRscp_ >= TD_SCDMA_RSCP_MINIMUM && tdScdmaRscp_ <= TD_SCDMA_RSCP_MAXIMUM);
630 }
631 
operator ==(const NrSignalInformation & nr) const632 bool NrSignalInformation::operator==(const NrSignalInformation &nr) const
633 {
634     return nrRsrp_ == nr.nrRsrp_ && nrRsrq_ == nr.nrRsrq_ && nrSinr_ == nr.nrSinr_;
635 }
636 
SetValue(const int32_t rsrp,const int32_t rsrq,const int32_t sinr)637 void NrSignalInformation::SetValue(const int32_t rsrp, const int32_t rsrq, const int32_t sinr)
638 {
639     nrRsrp_ = rsrp;
640     nrRsrq_ = rsrq;
641     nrSinr_ = sinr;
642 }
643 
GetRsrp() const644 int32_t NrSignalInformation::GetRsrp() const
645 {
646     return nrRsrp_;
647 }
648 
GetRsrq() const649 int32_t NrSignalInformation::GetRsrq() const
650 {
651     return nrRsrq_;
652 }
653 
GetSinr() const654 int32_t NrSignalInformation::GetSinr() const
655 {
656     return nrSinr_;
657 }
658 
GetSignalLevel() const659 int32_t NrSignalInformation::GetSignalLevel() const
660 {
661     int32_t nrRsrp = GetRsrp();
662     int32_t level = SIGNAL_LEVEL_INVALID;
663     int32_t rssi = NR_RSSI_INVALID;
664     if (ValidateNrValue()) {
665         // Reference: TS 27.007 section 8.69
666         rssi += nrRsrp;
667         for (int32_t i = signalBar_; i >= 0; --i) {
668             if (rssi >= NR_SIGNAL_THRESHOLD[i]) {
669                 level = i;
670                 break;
671             }
672         }
673     }
674     return level;
675 }
676 
GetNetworkType() const677 SignalInformation::NetworkType NrSignalInformation::GetNetworkType() const
678 {
679     return SignalInformation::NetworkType::NR;
680 }
681 
ToString() const682 std::string NrSignalInformation::ToString() const
683 {
684     int32_t netWorkType = static_cast<int32_t>(NrSignalInformation::GetNetworkType());
685     std::string content("networkType:" + std::to_string(netWorkType) + ",nrRsrp:" + std::to_string(nrRsrp_) +
686                         ",nrRsrq:" + std::to_string(nrRsrq_) + ",nrSinr:" + std::to_string(nrSinr_) +
687                         ",signalLevel:" + std::to_string(NrSignalInformation::GetSignalLevel()));
688     return content;
689 }
690 
NewInstance() const691 sptr<SignalInformation> NrSignalInformation::NewInstance() const
692 {
693     std::unique_ptr<NrSignalInformation> nr = std::make_unique<NrSignalInformation>();
694     if (nr == nullptr) {
695         return nullptr;
696     }
697     nr->SetValue(this->nrRsrp_, this->nrRsrq_, this->nrSinr_);
698     return nr.release();
699 }
700 
Marshalling(Parcel & parcel) const701 bool NrSignalInformation::Marshalling(Parcel &parcel) const
702 {
703     if (!parcel.WriteInt32(static_cast<int32_t>(SignalInformation::NetworkType::NR))) {
704         return false;
705     }
706     if (!parcel.WriteInt32(nrRsrp_)) {
707         return false;
708     }
709     if (!parcel.WriteInt32(nrRsrq_)) {
710         return false;
711     }
712     if (!parcel.WriteInt32(nrSinr_)) {
713         return false;
714     }
715     return true;
716 }
717 
Unmarshalling(Parcel & parcel)718 std::unique_ptr<NrSignalInformation> NrSignalInformation::Unmarshalling(Parcel &parcel)
719 {
720     std::unique_ptr<NrSignalInformation> signal = std::make_unique<NrSignalInformation>();
721     if (signal && !signal->ReadFromParcel(parcel)) {
722         signal = nullptr;
723     }
724     return signal;
725 }
726 
ReadFromParcel(Parcel & parcel)727 bool NrSignalInformation::ReadFromParcel(Parcel &parcel)
728 {
729     if (!parcel.ReadInt32(nrRsrp_)) {
730         return false;
731     }
732     if (!parcel.ReadInt32(nrRsrq_)) {
733         return false;
734     }
735     if (!parcel.ReadInt32(nrSinr_)) {
736         return false;
737     }
738     return true;
739 }
740 
ValidateNrValue() const741 bool NrSignalInformation::ValidateNrValue() const
742 {
743     return (nrRsrp_ > NR_RSRP_MINIMUM && nrRsrp_ <= NR_RSRP_MAXIMUM);
744 }
745 } // namespace Telephony
746 } // namespace OHOS
747