• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 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 "cell_info.h"
17 #include "telephony_log_wrapper.h"
18 #include "network_search_manager.h"
19 #include "network_search_notify.h"
20 
21 namespace OHOS {
22 namespace Telephony {
23 constexpr int32_t ZERO_VALUE = 0;
24 constexpr int32_t SIGNAL_LEVEL_INVALID = 0;
25 constexpr int32_t SIGNAL_RSSI_MAXIMUM = -1;
26 constexpr int32_t SIGNAL_FOUR_BARS = 4;
27 constexpr int32_t SIGNAL_FIVE_BARS = 5;
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 CellInfo::signalBar_ = SIGNAL_FIVE_BARS;
35 
36 const std::map<RatType, CellInfo::CallInfoFunc> CellInfo::memberFuncMap_ = {
37     {RatType::NETWORK_TYPE_GSM, &CellInfo::ProcessNeighboringCellGsm},
38     {RatType::NETWORK_TYPE_CDMA, &CellInfo::ProcessNeighboringCellCdma},
39     {RatType::NETWORK_TYPE_WCDMA, &CellInfo::ProcessNeighboringCellWcdma},
40     {RatType::NETWORK_TYPE_TDSCDMA, &CellInfo::ProcessNeighboringCellTdscdma},
41     {RatType::NETWORK_TYPE_LTE, &CellInfo::ProcessNeighboringCellLte},
42     {RatType::NETWORK_TYPE_NR, &CellInfo::ProcessNeighboringCellNr}};
43 
CellInfo(std::weak_ptr<NetworkSearchManager> networkSearchManager,int32_t slotId)44 CellInfo::CellInfo(std::weak_ptr<NetworkSearchManager> networkSearchManager, int32_t slotId)
45     : networkSearchManager_(networkSearchManager), slotId_(slotId)
46 {
47     InitCellSignalBar();
48 }
49 
InitCellSignalBar(const int32_t bar)50 void CellInfo::InitCellSignalBar(const int32_t bar)
51 {
52     if (bar == SIGNAL_FOUR_BARS) {
53         GSM_SIGNAL_THRESHOLD = SignalInformation::GSM_SIGNAL_THRESHOLD_4BAR;
54         CDMA_SIGNAL_THRESHOLD = SignalInformation::CDMA_SIGNAL_THRESHOLD_4BAR;
55         LTE_SIGNAL_THRESHOLD = SignalInformation::LTE_SIGNAL_THRESHOLD_4BAR;
56         WCDMA_SIGNAL_THRESHOLD = SignalInformation::WCDMA_SIGNAL_THRESHOLD_4BAR;
57         TD_SCDMA_SIGNAL_THRESHOLD = SignalInformation::TD_SCDMA_SIGNAL_THRESHOLD_4BAR;
58         NR_SIGNAL_THRESHOLD = SignalInformation::NR_SIGNAL_THRESHOLD_4BAR;
59         signalBar_ = SIGNAL_FOUR_BARS;
60     } else {
61         GSM_SIGNAL_THRESHOLD = SignalInformation::GSM_SIGNAL_THRESHOLD_5BAR;
62         CDMA_SIGNAL_THRESHOLD = SignalInformation::CDMA_SIGNAL_THRESHOLD_5BAR;
63         LTE_SIGNAL_THRESHOLD = SignalInformation::LTE_SIGNAL_THRESHOLD_5BAR;
64         WCDMA_SIGNAL_THRESHOLD = SignalInformation::WCDMA_SIGNAL_THRESHOLD_5BAR;
65         TD_SCDMA_SIGNAL_THRESHOLD = SignalInformation::TD_SCDMA_SIGNAL_THRESHOLD_5BAR;
66         NR_SIGNAL_THRESHOLD = SignalInformation::NR_SIGNAL_THRESHOLD_5BAR;
67         signalBar_ = SIGNAL_FIVE_BARS;
68     }
69 }
70 
ProcessNeighboringCellInfo(const AppExecFwk::InnerEvent::Pointer & event)71 void CellInfo::ProcessNeighboringCellInfo(const AppExecFwk::InnerEvent::Pointer &event)
72 {
73     TELEPHONY_LOGD("CellInfo::ProcessNeighboringCellInfo cell info start...... slotId:%{public}d", slotId_);
74     std::lock_guard<std::mutex> lock(mutex_);
75     if (event == nullptr) {
76         TELEPHONY_LOGE("CellInfo::ProcessNeighboringCellInfo event is nullptr slotId:%{public}d", slotId_);
77         return;
78     }
79 
80     CellListNearbyInfo *cellInfo = event->GetSharedObject<CellListNearbyInfo>().get();
81     if (cellInfo == nullptr) {
82         TELEPHONY_LOGE("CellInfo::ProcessNeighboringCellInfo rssi is nullptr slotId:%{public}d", slotId_);
83         return;
84     }
85 
86     int32_t cellSize = cellInfo->itemNum >= CellInformation::MAX_CELL_NUM ? CellInformation::MAX_CELL_NUM :
87                                                                             cellInfo->itemNum;
88     if (cellSize > 0) {
89         currentCellInfo_ = nullptr;
90         cellInfos_.clear();
91     } else {
92         return;
93     }
94 
95     TELEPHONY_LOGI(
96         "CellInfo::ProcessNeighboringCellInfo cell size:%{public}d, cur size:%{public}zu slotId:%{public}d",
97         cellInfo->itemNum, cellInfos_.size(), slotId_);
98     std::vector<CellNearbyInfo> cell = cellInfo->cellNearbyInfo;
99     for (int32_t i = 0; i < cellSize; ++i) {
100         int32_t type = cell[i].ratType;
101         auto itFunc = memberFuncMap_.find(static_cast<RatType>(type));
102         if (itFunc != memberFuncMap_.end()) {
103             auto memberFunc = itFunc->second;
104             if (memberFunc != nullptr) {
105                 (this->*memberFunc)(&cell[i]);
106             }
107         }
108     }
109 }
110 
ProcessCurrentCellInfo(const AppExecFwk::InnerEvent::Pointer & event)111 void CellInfo::ProcessCurrentCellInfo(const AppExecFwk::InnerEvent::Pointer &event)
112 {
113     TELEPHONY_LOGD("CellInfo::ProcessCurrentCellInfo cell info start... slotId:%{public}d", slotId_);
114     std::lock_guard<std::mutex> lock(mutex_);
115     if (event == nullptr) {
116         TELEPHONY_LOGE("CellInfo::ProcessCurrentCellInfo event is nullptr slotId:%{public}d", slotId_);
117         return;
118     }
119     CellListCurrentInformation *cellInfoList = event->GetSharedObject<CellListCurrentInformation>().get();
120     if (cellInfoList == nullptr) {
121         TELEPHONY_LOGE("CellInfo::ProcessCurrentCellInfo rssi is nullptr");
122         return;
123     }
124 
125     int32_t cellSize = cellInfoList->itemNum >= CellInformation::MAX_CELL_NUM ? CellInformation::MAX_CELL_NUM :
126                                                                                 cellInfoList->itemNum;
127     if (cellSize <= 0) {
128         TELEPHONY_LOGE("CellInfo::ProcessCurrentCellInfo rssi is nullptr");
129         return;
130     }
131 
132     TELEPHONY_LOGI("CellInfo::ProcessCurrentCellInfo cell size:%{public}d, cur size:%{public}zu",
133         cellInfoList->itemNum, cellInfos_.size());
134     currentCellInfo_ = nullptr;
135     cellInfos_.clear();
136     std::vector<CurrentCellInformation> cell = cellInfoList->cellCurrentInfo;
137     for (int32_t i = 0; i < cellSize; ++i) {
138         CurrentCellInformation currentCell = cell[i];
139         ProcessCurrentCell(&currentCell);
140     }
141     NotifyCellInfoUpdated();
142 }
143 
NotifyCellInfoUpdated() const144 void CellInfo::NotifyCellInfoUpdated() const
145 {
146     if (cellInfos_.size() > 0) {
147         DelayedSingleton<NetworkSearchNotify>::GetInstance()->NotifyCellInfoUpdated(slotId_, cellInfos_);
148     } else {
149         TELEPHONY_LOGI("CellInfo::NotifyCellInfoUpdated no notify slotId:%{public}d", slotId_);
150     }
151 }
152 
UpdateCellLocation(int32_t techType,int32_t cellId,int32_t lac)153 void CellInfo::UpdateCellLocation(int32_t techType, int32_t cellId, int32_t lac)
154 {
155     CellInformation::CellType type = ConvertTechToCellType(static_cast<RadioTech>(techType));
156     if (type == CellInformation::CellType::CELL_TYPE_NONE) {
157         TELEPHONY_LOGE("CellInfo::UpdateCellLocation type error");
158         return;
159     }
160 
161     for (auto &cell : cellInfos_) {
162         if (cell->GetNetworkType() == type) {
163             if (ProcessCellLocation(cell, type, cellId, lac)) {
164                 UpdateSignalLevel(cell, type);
165                 NotifyCellInfoUpdated();
166                 cell->SetIsCamped(true);
167                 currentCellInfo_ = cell;
168             }
169             break;
170         }
171     }
172 }
173 
ProcessCellLocation(sptr<CellInformation> & cell,CellInformation::CellType type,int32_t cellId,int32_t lac)174 bool CellInfo::ProcessCellLocation(
175     sptr<CellInformation> &cell, CellInformation::CellType type, int32_t cellId, int32_t lac)
176 {
177     switch (type) {
178         case CellInformation::CellType::CELL_TYPE_GSM: {
179             GsmCellInformation *gsm = reinterpret_cast<GsmCellInformation *>(cell.GetRefPtr());
180             if (gsm->GetCellId() != cellId || gsm->GetLac() != lac) {
181                 gsm->UpdateLocation(cellId, lac);
182                 return true;
183             }
184             break;
185         }
186         case CellInformation::CellType::CELL_TYPE_LTE: {
187             LteCellInformation *lte = reinterpret_cast<LteCellInformation *>(cell.GetRefPtr());
188             if (lte->GetCellId() != cellId || lte->GetTac() != lac) {
189                 lte->UpdateLocation(cellId, lac);
190                 return true;
191             }
192             break;
193         }
194         case CellInformation::CellType::CELL_TYPE_WCDMA: {
195             WcdmaCellInformation *wcdma = reinterpret_cast<WcdmaCellInformation *>(cell.GetRefPtr());
196             if (wcdma->GetCellId() != cellId || wcdma->GetLac() != lac) {
197                 wcdma->UpdateLocation(cellId, lac);
198                 return true;
199             }
200             break;
201         }
202         case CellInformation::CellType::CELL_TYPE_TDSCDMA: {
203             TdscdmaCellInformation *tdscdma = reinterpret_cast<TdscdmaCellInformation *>(cell.GetRefPtr());
204             if (tdscdma->GetCellId() != cellId || tdscdma->GetLac() != lac) {
205                 tdscdma->UpdateLocation(cellId, lac);
206                 return true;
207             }
208             break;
209         }
210         case CellInformation::CellType::CELL_TYPE_NR: {
211             NrCellInformation *nr = reinterpret_cast<NrCellInformation *>(cell.GetRefPtr());
212             if (nr->GetCellId() != cellId || nr->GetTac() != lac) {
213                 nr->UpdateLocation(cellId, lac);
214                 return true;
215             }
216             break;
217         }
218         default:
219             TELEPHONY_LOGE("CellInfo::ProcessCellLocation type error");
220             break;
221     }
222     return false;
223 }
224 
UpdateSignalLevel(sptr<CellInformation> & cell,CellInformation::CellType cellType)225 void CellInfo::UpdateSignalLevel(sptr<CellInformation> &cell, CellInformation::CellType cellType)
226 {
227     if (cellType == CellInformation::CellType::CELL_TYPE_NONE || cell->GetNetworkType() != cellType) {
228         TELEPHONY_LOGE("CellInfo::UpdateSignalLevel type error");
229         return;
230     }
231 
232     std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
233     if (nsm == nullptr) {
234         TELEPHONY_LOGE("CellInfo::UpdateSignalLevel nsm is nullptr slotId:%{public}d", slotId_);
235         return;
236     }
237 
238     std::vector<sptr<SignalInformation>> signals;
239     nsm->GetSignalInfoList(slotId_, signals);
240     int32_t signalLevel = 0;
241     int32_t signalIntensity = 0;
242     for (const auto &v : signals) {
243         if (ConvertToCellType(v->GetNetworkType()) == cellType) {
244             TELEPHONY_LOGI("CellInfo::UpdateSignalLevel signal level %{public}d slotId:%{public}d",
245                 v->GetSignalLevel(), slotId_);
246             signalLevel = v->GetSignalLevel();
247             signalIntensity = v->GetSignalIntensity();
248             break;
249         }
250     }
251     cell->SetSignalLevel(signalLevel);
252     cell->SetSignalIntensity(signalIntensity);
253 }
254 
ConvertToCellType(SignalInformation::NetworkType signalType) const255 CellInformation::CellType CellInfo::ConvertToCellType(SignalInformation::NetworkType signalType) const
256 {
257     switch (signalType) {
258         case SignalInformation::NetworkType::GSM:
259             return CellInformation::CellType::CELL_TYPE_GSM;
260         case SignalInformation::NetworkType::WCDMA:
261             return CellInformation::CellType::CELL_TYPE_WCDMA;
262         case SignalInformation::NetworkType::LTE:
263             return CellInformation::CellType::CELL_TYPE_LTE;
264         case SignalInformation::NetworkType::CDMA:
265             return CellInformation::CellType::CELL_TYPE_CDMA;
266         case SignalInformation::NetworkType::TDSCDMA:
267             return CellInformation::CellType::CELL_TYPE_TDSCDMA;
268         case SignalInformation::NetworkType::NR:
269             return CellInformation::CellType::CELL_TYPE_NR;
270         default:
271             return CellInformation::CellType::CELL_TYPE_NONE;
272     }
273 }
274 
ConvertRatToCellType(int ratType) const275 CellInformation::CellType CellInfo::ConvertRatToCellType(int ratType) const
276 {
277     switch (ratType) {
278         case RatType::NETWORK_TYPE_GSM:
279             return CellInformation::CellType::CELL_TYPE_GSM;
280         case RatType::NETWORK_TYPE_WCDMA:
281             return CellInformation::CellType::CELL_TYPE_WCDMA;
282         case RatType::NETWORK_TYPE_LTE:
283             return CellInformation::CellType::CELL_TYPE_LTE;
284         case RatType::NETWORK_TYPE_CDMA:
285             return CellInformation::CellType::CELL_TYPE_CDMA;
286         case RatType::NETWORK_TYPE_TDSCDMA:
287             return CellInformation::CellType::CELL_TYPE_TDSCDMA;
288         case RatType::NETWORK_TYPE_NR:
289             return CellInformation::CellType::CELL_TYPE_NR;
290         default:
291             return CellInformation::CellType::CELL_TYPE_NONE;
292     }
293 }
294 
ConvertTechToCellType(RadioTech techType) const295 CellInformation::CellType CellInfo::ConvertTechToCellType(RadioTech techType) const
296 {
297     switch (techType) {
298         case RadioTech::RADIO_TECHNOLOGY_GSM:
299             return CellInformation::CellType::CELL_TYPE_GSM;
300         case RadioTech::RADIO_TECHNOLOGY_WCDMA:
301         case RadioTech::RADIO_TECHNOLOGY_HSPAP:
302         case RadioTech::RADIO_TECHNOLOGY_HSPA:
303             return CellInformation::CellType::CELL_TYPE_WCDMA;
304         case RadioTech::RADIO_TECHNOLOGY_LTE:
305         case RadioTech::RADIO_TECHNOLOGY_LTE_CA:
306             return CellInformation::CellType::CELL_TYPE_LTE;
307         case RadioTech::RADIO_TECHNOLOGY_TD_SCDMA:
308             return CellInformation::CellType::CELL_TYPE_TDSCDMA;
309         case RadioTech::RADIO_TECHNOLOGY_1XRTT:
310         case RadioTech::RADIO_TECHNOLOGY_EVDO:
311         case RadioTech::RADIO_TECHNOLOGY_EHRPD:
312             return CellInformation::CellType::CELL_TYPE_CDMA;
313         case RadioTech::RADIO_TECHNOLOGY_NR:
314             return CellInformation::CellType::CELL_TYPE_NR;
315         default:
316             return CellInformation::CellType::CELL_TYPE_NONE;
317     }
318 }
319 
ProcessCurrentCell(CurrentCellInformation * cellInfo)320 bool CellInfo::ProcessCurrentCell(CurrentCellInformation *cellInfo)
321 {
322     bool ret = false;
323     switch (cellInfo->ratType) {
324         case RatType::NETWORK_TYPE_GSM: {
325             ret = ProcessCurrentCellGsm(cellInfo);
326             break;
327         }
328         case RatType::NETWORK_TYPE_LTE: {
329             ret = ProcessCurrentCellLte(cellInfo);
330             break;
331         }
332         case RatType::NETWORK_TYPE_WCDMA: {
333             ret = ProcessCurrentCellWcdma(cellInfo);
334             break;
335         }
336         case RatType::NETWORK_TYPE_TDSCDMA: {
337             ret = ProcessCurrentCellTdscdma(cellInfo);
338             break;
339         }
340         case RatType::NETWORK_TYPE_CDMA: {
341             ret = ProcessCurrentCellCdma(cellInfo);
342             break;
343         }
344         case RatType::NETWORK_TYPE_NR: {
345             ret = ProcessCurrentCellNr(cellInfo);
346             break;
347         }
348         default: {
349             TELEPHONY_LOGI("CellInfo::ProcessCurrentCell error rat type:%{public}d slotId:%{public}d",
350                 cellInfo->ratType, slotId_);
351             return false;
352         }
353     }
354     if (!ret) {
355         TELEPHONY_LOGI(
356             "CellInfo::ProcessCurrentCell currentCellInfo is null or cell info no change slotId:%{public}d",
357             slotId_);
358         return false;
359     }
360     return true;
361 }
362 
ProcessNeighboringCellGsm(CellNearbyInfo * cellInfo)363 bool CellInfo::ProcessNeighboringCellGsm(CellNearbyInfo *cellInfo)
364 {
365     bool ret = false;
366     if (cellInfo == nullptr) {
367         TELEPHONY_LOGE("cellInfo is nullptr");
368         return ret;
369     }
370     sptr<GsmCellInformation> cell = new GsmCellInformation;
371     if (cell != nullptr) {
372         int32_t &arfcn = cellInfo->ServiceCellParas.gsm.arfcn;
373         int32_t &cellId = cellInfo->ServiceCellParas.gsm.cellId;
374         int32_t &bsic = cellInfo->ServiceCellParas.gsm.bsic;
375         int32_t &lac = cellInfo->ServiceCellParas.gsm.lac;
376         cell->Init(0, 0, cellId);
377         cell->SetGsmParam(bsic, lac, arfcn);
378         cellInfos_.emplace_back(cell);
379         TELEPHONY_LOGI("CellInfo::ProcessNeighboringCellGsm arfcn:%{private}d cellId:%{private}d"
380             "bsic:%{private}d lac:%{private}d slotId:%{public}d",
381             arfcn, cellId, bsic, lac, slotId_);
382         ret = true;
383     }
384     return ret;
385 }
386 
ProcessNeighboringCellLte(CellNearbyInfo * cellInfo)387 bool CellInfo::ProcessNeighboringCellLte(CellNearbyInfo *cellInfo)
388 {
389     bool ret = false;
390     if (cellInfo == nullptr) {
391         TELEPHONY_LOGE("cellInfo is nullptr");
392         return ret;
393     }
394     sptr<LteCellInformation> cell = new LteCellInformation;
395     if (cell != nullptr) {
396         int32_t &arfcn = cellInfo->ServiceCellParas.lte.arfcn;
397         int32_t pci = cellInfo->ServiceCellParas.lte.pci;
398         cell->Init(0, 0, 0);
399         cell->SetLteParam(pci, 0, arfcn);
400         cellInfos_.emplace_back(cell);
401         TELEPHONY_LOGI("CellInfo::ProcessLte arfcn:%{private}d pci:%{private}d slotId:%{public}d", arfcn, pci, slotId_);
402         ret = true;
403     }
404     return ret;
405 }
406 
ProcessNeighboringCellWcdma(CellNearbyInfo * cellInfo)407 bool CellInfo::ProcessNeighboringCellWcdma(CellNearbyInfo *cellInfo)
408 {
409     bool ret = false;
410     if (cellInfo == nullptr) {
411         TELEPHONY_LOGE("cellInfo is nullptr");
412         return ret;
413     }
414     sptr<WcdmaCellInformation> cell = new WcdmaCellInformation;
415     if (cell != nullptr) {
416         int32_t &arfcn = cellInfo->ServiceCellParas.wcdma.arfcn;
417         int32_t psc = cellInfo->ServiceCellParas.wcdma.psc;
418         cell->Init(0, 0, 0);
419         cell->SetWcdmaParam(psc, 0, arfcn);
420         cellInfos_.emplace_back(cell);
421         TELEPHONY_LOGI(
422             "CellInfo::ProcessWcdma arfcn:%{private}d psc:%{private}d slotId:%{public}d", arfcn, psc, slotId_);
423         ret = true;
424     }
425     return ret;
426 }
427 
ProcessNeighboringCellCdma(CellNearbyInfo * cellInfo)428 bool CellInfo::ProcessNeighboringCellCdma(CellNearbyInfo *cellInfo)
429 {
430     bool ret = false;
431     if (cellInfo == nullptr) {
432         TELEPHONY_LOGE("cellInfo is nullptr");
433         return ret;
434     }
435     sptr<CdmaCellInformation> cell = new CdmaCellInformation;
436     if (cell != nullptr) {
437         int32_t &baseId = cellInfo->ServiceCellParas.cdma.baseId;
438         int32_t &longitude = cellInfo->ServiceCellParas.cdma.longitude;
439         int32_t &latitude = cellInfo->ServiceCellParas.cdma.latitude;
440         int32_t &networkId = cellInfo->ServiceCellParas.cdma.networkId;
441         int32_t &systemId = cellInfo->ServiceCellParas.cdma.systemId;
442         cell->Init(0, 0, 0);
443         cell->SetCdmaParam(baseId, latitude, longitude, networkId, systemId);
444         cellInfos_.emplace_back(cell);
445         TELEPHONY_LOGI(
446             "CellInfo::ProcessCdma baseId:%{private}d psc:%{private}d slotId:%{public}d", baseId, systemId, slotId_);
447         ret = true;
448     }
449     return ret;
450 }
451 
ProcessNeighboringCellTdscdma(CellNearbyInfo * cellInfo)452 bool CellInfo::ProcessNeighboringCellTdscdma(CellNearbyInfo *cellInfo)
453 {
454     bool ret = false;
455     if (cellInfo == nullptr) {
456         TELEPHONY_LOGE("cellInfo is nullptr");
457         return ret;
458     }
459     sptr<TdscdmaCellInformation> cell = new TdscdmaCellInformation;
460     if (cell != nullptr) {
461         int32_t &arfcn = cellInfo->ServiceCellParas.tdscdma.arfcn;
462         int32_t &cpid = cellInfo->ServiceCellParas.tdscdma.cpid;
463         int32_t &lac = cellInfo->ServiceCellParas.tdscdma.lac;
464         cell->Init(0, 0, 0);
465         cell->SetTdscdmaParam(cpid, lac, arfcn);
466         cellInfos_.emplace_back(cell);
467         TELEPHONY_LOGI(
468             "CellInfo::ProcessTdscdma arfcn:%{private}d psc:%{private}d slotId:%{public}d", arfcn, cpid, slotId_);
469         ret = true;
470     }
471     return ret;
472 }
473 
ProcessNeighboringCellNr(CellNearbyInfo * cellInfo)474 bool CellInfo::ProcessNeighboringCellNr(CellNearbyInfo *cellInfo)
475 {
476     bool ret = false;
477     if (cellInfo == nullptr) {
478         TELEPHONY_LOGE("cellInfo is nullptr");
479         return ret;
480     }
481     sptr<NrCellInformation> cell = new NrCellInformation;
482     if (cell != nullptr && cellInfo != nullptr) {
483         int32_t &nrArfcn = cellInfo->ServiceCellParas.nr.nrArfcn;
484         int32_t &pci = cellInfo->ServiceCellParas.nr.pci;
485         int32_t &tac = cellInfo->ServiceCellParas.nr.tac;
486         int64_t &nci = cellInfo->ServiceCellParas.nr.nci;
487         cell->Init(0, 0, 0);
488         cell->SetNrParam(nrArfcn, pci, tac, nci);
489         cellInfos_.emplace_back(cell);
490         TELEPHONY_LOGI("CellInfo::ProcessNeighboringCellNr arfcn:%{private}d pci:%{private}d slotId:%{public}d",
491             nrArfcn, pci, slotId_);
492         ret = true;
493     }
494     return ret;
495 }
496 
ProcessCurrentCellGsm(CurrentCellInformation * cellInfo)497 bool CellInfo::ProcessCurrentCellGsm(CurrentCellInformation *cellInfo)
498 {
499     bool ret = false;
500     if (cellInfo == nullptr) {
501         TELEPHONY_LOGE("cellInfo is nullptr");
502         return ret;
503     }
504     sptr<GsmCellInformation> cell = new GsmCellInformation;
505     if (cell != nullptr) {
506         int32_t &arfcn = cellInfo->ServiceCellParas.gsm.arfcn;
507         int32_t &cellId = cellInfo->ServiceCellParas.gsm.cellId;
508         int32_t &bsic = cellInfo->ServiceCellParas.gsm.bsic;
509         int32_t &lac = cellInfo->ServiceCellParas.gsm.lac;
510         int32_t &rxlev = cellInfo->ServiceCellParas.gsm.rxlev;
511         rxlev = ZERO_VALUE - rxlev;
512         cell->Init(cellInfo->mcc, cellInfo->mnc, cellId);
513         cell->SetGsmParam(bsic, lac, arfcn);
514         cell->SetSignalIntensity(rxlev);
515         cell->SetIsCamped(true);
516         int32_t level = GetCurrentSignalLevelGsm(rxlev);
517         cell->SetSignalLevel(level);
518         currentCellInfo_ = cell;
519         cellInfos_.emplace_back(cell);
520         TELEPHONY_LOGI(
521             "CellInfo::ProcessCurrentCellGsm arfcn:%{private}d cellId:%{private}d"
522             "bsic:%{private}d lac:%{private}d rxlev:%{public}d slotId:%{public}d",
523             arfcn, cellId, bsic, lac, rxlev, slotId_);
524         ret = true;
525     }
526     return ret;
527 }
528 
ProcessCurrentCellLte(CurrentCellInformation * cellInfo)529 bool CellInfo::ProcessCurrentCellLte(CurrentCellInformation *cellInfo)
530 {
531     bool ret = false;
532     if (cellInfo == nullptr) {
533         TELEPHONY_LOGE("cellInfo is nullptr");
534         return ret;
535     }
536     sptr<LteCellInformation> cell = new LteCellInformation;
537     if (cell != nullptr) {
538         int32_t &arfcn = cellInfo->ServiceCellParas.lte.arfcn;
539         int32_t &pci = cellInfo->ServiceCellParas.lte.pci;
540         int32_t &cellId = cellInfo->ServiceCellParas.lte.cellId;
541         int32_t &tac = cellInfo->ServiceCellParas.lte.tac;
542         int32_t &rsrp = cellInfo->ServiceCellParas.lte.rsrp;
543         rsrp = ZERO_VALUE - rsrp;
544         cell->Init(cellInfo->mcc, cellInfo->mnc, cellId);
545         cell->SetLteParam(pci, tac, arfcn);
546         cell->SetSignalIntensity(rsrp);
547         cell->SetIsCamped(true);
548         int32_t level = GetCurrentSignalLevelLte(rsrp);
549         cell->SetSignalLevel(level);
550         currentCellInfo_ = cell;
551         cellInfos_.emplace_back(cell);
552         TELEPHONY_LOGI(
553             "CellInfo::ProcessCurrentCellLte arfcn:%{private}d pci:%{private}d rsrp:%{public}d slotId:%{public}d",
554             arfcn, pci, rsrp, slotId_);
555         ret = true;
556     }
557     return ret;
558 }
559 
ProcessCurrentCellWcdma(CurrentCellInformation * cellInfo)560 bool CellInfo::ProcessCurrentCellWcdma(CurrentCellInformation *cellInfo)
561 {
562     bool ret = false;
563     if (cellInfo == nullptr) {
564         TELEPHONY_LOGE("cellInfo is nullptr");
565         return ret;
566     }
567     sptr<WcdmaCellInformation> cell = new WcdmaCellInformation;
568     if (cell != nullptr) {
569         int32_t &arfcn = cellInfo->ServiceCellParas.wcdma.arfcn;
570         int32_t &psc = cellInfo->ServiceCellParas.wcdma.psc;
571         int32_t &cellId = cellInfo->ServiceCellParas.wcdma.cellId;
572         int32_t &lac = cellInfo->ServiceCellParas.wcdma.lac;
573         int32_t &rscp = cellInfo->ServiceCellParas.wcdma.rscp;
574         rscp = ZERO_VALUE - rscp;
575         cell->Init(cellInfo->mcc, cellInfo->mnc, cellId);
576         cell->SetWcdmaParam(psc, lac, arfcn);
577         cell->SetSignalIntensity(rscp);
578         cell->SetIsCamped(true);
579         int32_t level = GetCurrentSignalLevelWcdma(rscp);
580         cell->SetSignalLevel(level);
581         currentCellInfo_ = cell;
582         cellInfos_.emplace_back(cell);
583         TELEPHONY_LOGI(
584             "CellInfo::ProcessCurrentCellWcdma arfcn:%{private}d psc:%{private}d rscp:%{public}d", arfcn, psc, rscp);
585         ret = true;
586     }
587     return ret;
588 }
589 
ProcessCurrentCellCdma(CurrentCellInformation * cellInfo)590 bool CellInfo::ProcessCurrentCellCdma(CurrentCellInformation *cellInfo)
591 {
592     bool ret = false;
593     if (cellInfo == nullptr) {
594         TELEPHONY_LOGE("cellInfo is nullptr");
595         return ret;
596     }
597     sptr<CdmaCellInformation> cell = new CdmaCellInformation;
598     if (cell != nullptr) {
599         int32_t &baseId = cellInfo->ServiceCellParas.cdma.baseId;
600         int32_t &longitude = cellInfo->ServiceCellParas.cdma.longitude;
601         int32_t &latitude = cellInfo->ServiceCellParas.cdma.latitude;
602         int32_t &networkId = cellInfo->ServiceCellParas.cdma.networkId;
603         int32_t &systemId = cellInfo->ServiceCellParas.cdma.systemId;
604         int32_t &pilotStrength = cellInfo->ServiceCellParas.cdma.pilotStrength;
605         pilotStrength = ZERO_VALUE - pilotStrength;
606         cell->Init(cellInfo->mcc, cellInfo->mnc, baseId);
607         cell->SetCdmaParam(baseId, latitude, longitude, networkId, systemId);
608         cell->SetSignalIntensity(pilotStrength);
609         cell->SetIsCamped(true);
610         int32_t level = GetCurrentSignalLevelCdma(pilotStrength);
611         cell->SetSignalLevel(level);
612         currentCellInfo_ = cell;
613         cellInfos_.emplace_back(cell);
614         TELEPHONY_LOGI(
615             "CellInfo::ProcessCurrentCellCdma baseId:%{private}d networkId:%{private}d pilotStrength:%{public}d",
616             baseId, networkId, pilotStrength);
617         ret = true;
618     }
619     return ret;
620 }
621 
ProcessCurrentCellTdscdma(CurrentCellInformation * cellInfo)622 bool CellInfo::ProcessCurrentCellTdscdma(CurrentCellInformation *cellInfo)
623 {
624     bool ret = false;
625     if (cellInfo == nullptr) {
626         TELEPHONY_LOGE("cellInfo is nullptr");
627         return ret;
628     }
629     sptr<TdscdmaCellInformation> cell = new TdscdmaCellInformation;
630     if (cell != nullptr) {
631         int32_t &arfcn = cellInfo->ServiceCellParas.tdscdma.arfcn;
632         int32_t &cpid = cellInfo->ServiceCellParas.tdscdma.cpid;
633         int32_t &cellId = cellInfo->ServiceCellParas.tdscdma.cellId;
634         int32_t &lac = cellInfo->ServiceCellParas.tdscdma.lac;
635         int32_t &rscp = cellInfo->ServiceCellParas.tdscdma.rscp;
636         rscp = ZERO_VALUE - rscp;
637         cell->Init(cellInfo->mcc, cellInfo->mnc, cellId);
638         cell->SetTdscdmaParam(cpid, lac, arfcn);
639         cell->SetSignalIntensity(rscp);
640         cell->SetIsCamped(true);
641         int32_t level = GetCurrentSignalLevelTdscdma(rscp);
642         cell->SetSignalLevel(level);
643         currentCellInfo_ = cell;
644         cellInfos_.emplace_back(cell);
645         TELEPHONY_LOGI("CellInfo::ProcessCurrentCellTdscdma arfcn:%{private}d pci:%{private}d slotId:%{public}d", arfcn,
646             cpid, slotId_);
647         ret = true;
648     }
649     return ret;
650 }
651 
ProcessCurrentCellNr(CurrentCellInformation * cellInfo)652 bool CellInfo::ProcessCurrentCellNr(CurrentCellInformation *cellInfo)
653 {
654     bool ret = false;
655     if (cellInfo == nullptr) {
656         TELEPHONY_LOGE("cellInfo is nullptr");
657         return ret;
658     }
659     sptr<NrCellInformation> cell = new NrCellInformation;
660     if (cell != nullptr && cellInfo != nullptr) {
661         int32_t &nrArfcn = cellInfo->ServiceCellParas.nr.nrArfcn;
662         int32_t &pci = cellInfo->ServiceCellParas.nr.pci;
663         int32_t &tac = cellInfo->ServiceCellParas.nr.tac;
664         int64_t &nci = cellInfo->ServiceCellParas.nr.nci;
665         int32_t &rsrp = cellInfo->ServiceCellParas.nr.rsrp;
666         int32_t &rsrq = cellInfo->ServiceCellParas.nr.rsrq;
667         rsrp = ZERO_VALUE - rsrp;
668         rsrq = ZERO_VALUE - rsrq;
669         cell->Init(cellInfo->mcc, cellInfo->mnc, 0);
670         cell->SetNrParam(nrArfcn, pci, tac, nci);
671         cell->SetNrSignalParam(rsrp, rsrq);
672         cell->SetSignalIntensity(rsrp);
673         cell->SetIsCamped(true);
674         int32_t level = GetCurrentSignalLevelNr(rsrp);
675         cell->SetSignalLevel(level);
676         currentCellInfo_ = cell;
677         cellInfos_.emplace_back(cell);
678 
679         TELEPHONY_LOGI(
680             "CellInfo::ProcessCurrentCellNr arfcn:%{private}d pci:%{private}d slotId:%{public}d rsrp:%{public}d "
681             "rsrq:%{public}d", nrArfcn, pci, slotId_, rsrp, rsrq);
682         ret = true;
683     }
684     return ret;
685 }
686 
GetCurrentSignalLevelGsm(int32_t rxlev)687 int32_t CellInfo::GetCurrentSignalLevelGsm(int32_t rxlev)
688 {
689     int32_t level = SIGNAL_LEVEL_INVALID;
690     if (rxlev >= SIGNAL_RSSI_MAXIMUM) {
691         TELEPHONY_LOGE("CellInfo::GetCurrentSignalLevelGsm Value is Invalid.");
692         return level;
693     }
694     for (int32_t i = signalBar_; i >= 0; --i) {
695         if (rxlev >= GSM_SIGNAL_THRESHOLD[i]) {
696             level = i;
697             break;
698         }
699     }
700     return level;
701 }
702 
GetCurrentSignalLevelLte(int32_t rsrp)703 int32_t CellInfo::GetCurrentSignalLevelLte(int32_t rsrp)
704 {
705     int32_t level = SIGNAL_LEVEL_INVALID;
706     if (rsrp >= SIGNAL_RSSI_MAXIMUM) {
707         TELEPHONY_LOGE("CellInfo::GetCurrentSignalLevelLte Value is Invalid.");
708         return level;
709     }
710     for (int32_t i = signalBar_; i >= 0; --i) {
711         if (rsrp >= LTE_SIGNAL_THRESHOLD[i]) {
712             level = i;
713             break;
714         }
715     }
716     return level;
717 }
718 
GetCurrentSignalLevelWcdma(int32_t rscp)719 int32_t CellInfo::GetCurrentSignalLevelWcdma(int32_t rscp)
720 {
721     int32_t level = SIGNAL_LEVEL_INVALID;
722     if (rscp >= SIGNAL_RSSI_MAXIMUM) {
723         TELEPHONY_LOGE("CellInfo::GetCurrentSignalLevelWcdma Value is Invalid.");
724         return level;
725     }
726     for (int32_t i = signalBar_; i >= 0; --i) {
727         if (rscp >= WCDMA_SIGNAL_THRESHOLD[i]) {
728             level = i;
729             break;
730         }
731     }
732     return level;
733 }
734 
GetCurrentSignalLevelCdma(int32_t pilotStrength)735 int32_t CellInfo::GetCurrentSignalLevelCdma(int32_t pilotStrength)
736 {
737     int32_t level = SIGNAL_LEVEL_INVALID;
738     if (pilotStrength >= SIGNAL_RSSI_MAXIMUM) {
739         TELEPHONY_LOGE("CellInfo::GetCurrentSignalLevelCdma Value is Invalid.");
740         return level;
741     }
742     for (int32_t i = signalBar_; i >= 0; --i) {
743         if (pilotStrength >= CDMA_SIGNAL_THRESHOLD[i]) {
744             level = i;
745             break;
746         }
747     }
748     return level;
749 }
750 
GetCurrentSignalLevelTdscdma(int32_t rscp)751 int32_t CellInfo::GetCurrentSignalLevelTdscdma(int32_t rscp)
752 {
753     int32_t level = SIGNAL_LEVEL_INVALID;
754     if (rscp >= SIGNAL_RSSI_MAXIMUM) {
755         TELEPHONY_LOGE("CellInfo::GetCurrentSignalLevelTdscdma Value is Invalid.");
756         return level;
757     }
758     for (int32_t i = signalBar_; i >= 0; --i) {
759         if (rscp >= TD_SCDMA_SIGNAL_THRESHOLD[i]) {
760             level = i;
761             break;
762         }
763     }
764     return level;
765 }
766 
GetCurrentSignalLevelNr(int32_t rsrp)767 int32_t CellInfo::GetCurrentSignalLevelNr(int32_t rsrp)
768 {
769     int32_t level = SIGNAL_LEVEL_INVALID;
770     if (rsrp >= SIGNAL_RSSI_MAXIMUM) {
771         TELEPHONY_LOGE("CellInfo::GetCurrentSignalLevelNr Value is Invalid.");
772         return level;
773     }
774     for (int32_t i = signalBar_; i >= 0; --i) {
775         if (rsrp >= NR_SIGNAL_THRESHOLD[i]) {
776             level = i;
777             break;
778         }
779     }
780     return level;
781 }
782 
GetCellInfoList(std::vector<sptr<CellInformation>> & cellInfo)783 void CellInfo::GetCellInfoList(std::vector<sptr<CellInformation>> &cellInfo)
784 {
785     cellInfo.clear();
786     {
787         std::lock_guard<std::mutex> lock(mutex_);
788         for (auto &cell : cellInfos_) {
789             AddCellInformation(cell, cellInfo);
790         }
791     }
792     TELEPHONY_LOGI("CellInfo::GetCellInfoList size:%{public}zu slotId:%{public}d", cellInfo.size(), slotId_);
793 }
794 
ClearCellInfoList()795 void CellInfo::ClearCellInfoList()
796 {
797     std::lock_guard<std::mutex> lock(mutex_);
798     currentCellInfo_ = nullptr;
799     cellInfos_.clear();
800 }
801 
AddCellInformation(sptr<CellInformation> & cellInfo,std::vector<sptr<CellInformation>> & cellInfos)802 void CellInfo::AddCellInformation(sptr<CellInformation> &cellInfo, std::vector<sptr<CellInformation>> &cellInfos)
803 {
804     CellInformation::CellType type = cellInfo->GetNetworkType();
805     switch (type) {
806         case CellInformation::CellType::CELL_TYPE_GSM: {
807             sptr<GsmCellInformation> cell = new GsmCellInformation;
808             GsmCellInformation &gsmCell = *cell;
809             gsmCell = *(static_cast<GsmCellInformation *>(cellInfo.GetRefPtr()));
810             cellInfos.emplace_back(cell);
811             break;
812         }
813         case CellInformation::CellType::CELL_TYPE_LTE: {
814             sptr<LteCellInformation> cell = new LteCellInformation;
815             LteCellInformation &lteCell = *cell;
816             lteCell = *(static_cast<LteCellInformation *>(cellInfo.GetRefPtr()));
817             cellInfos.emplace_back(cell);
818             break;
819         }
820         case CellInformation::CellType::CELL_TYPE_WCDMA: {
821             sptr<WcdmaCellInformation> cell = new WcdmaCellInformation;
822             WcdmaCellInformation &wcdmaCell = *cell;
823             wcdmaCell = *(static_cast<WcdmaCellInformation *>(cellInfo.GetRefPtr()));
824             cellInfos.emplace_back(cell);
825             break;
826         }
827         case CellInformation::CellType::CELL_TYPE_CDMA: {
828             sptr<CdmaCellInformation> cell = new CdmaCellInformation;
829             CdmaCellInformation &cdmaCell = *cell;
830             cdmaCell = *(static_cast<CdmaCellInformation *>(cellInfo.GetRefPtr()));
831             cellInfos.emplace_back(cell);
832             break;
833         }
834         case CellInformation::CellType::CELL_TYPE_TDSCDMA: {
835             sptr<TdscdmaCellInformation> cell = new TdscdmaCellInformation;
836             TdscdmaCellInformation &tdscdmaCell = *cell;
837             tdscdmaCell = *(static_cast<TdscdmaCellInformation *>(cellInfo.GetRefPtr()));
838             cellInfos.emplace_back(cell);
839             break;
840         }
841         case CellInformation::CellType::CELL_TYPE_NR: {
842             sptr<NrCellInformation> cell = new NrCellInformation;
843             NrCellInformation &nrCell = *cell;
844             nrCell = *(static_cast<NrCellInformation *>(cellInfo.GetRefPtr()));
845             cellInfos.emplace_back(cell);
846             break;
847         }
848         default:
849             break;
850     }
851 }
852 
GetCellLocation()853 sptr<CellLocation> CellInfo::GetCellLocation()
854 {
855     if (currentCellInfo_ == nullptr) {
856         TELEPHONY_LOGE("CellInfo::GetCellLocation is null slotId:%{public}d", slotId_);
857         return nullptr;
858     }
859     CellInformation::CellType type = currentCellInfo_->GetNetworkType();
860     switch (type) {
861         case CellInformation::CellType::CELL_TYPE_GSM: {
862             sptr<GsmCellInformation> cellinfo =
863                 static_cast<GsmCellInformation *>(currentCellInfo_.GetRefPtr());
864             sptr<GsmCellLocation> cellLocation = new GsmCellLocation;
865             cellLocation->SetGsmParam(cellinfo->GetCellId(), cellinfo->GetLac());
866             return cellLocation;
867         }
868         case CellInformation::CellType::CELL_TYPE_TDSCDMA: {
869             sptr<GsmCellInformation> cellinfo = static_cast<GsmCellInformation *>(currentCellInfo_.GetRefPtr());
870             sptr<GsmCellLocation> cellLocation = new GsmCellLocation;
871             cellLocation->SetGsmParam(cellinfo->GetCellId(), cellinfo->GetLac());
872             return cellLocation;
873         }
874         case CellInformation::CellType::CELL_TYPE_WCDMA: {
875             sptr<WcdmaCellInformation> cellinfo = static_cast<WcdmaCellInformation *>(currentCellInfo_.GetRefPtr());
876             sptr<GsmCellLocation> cellLocation = new GsmCellLocation;
877             cellLocation->SetGsmParam(cellinfo->GetCellId(), cellinfo->GetLac(), cellinfo->GetPsc());
878             return cellLocation;
879         }
880         case CellInformation::CellType::CELL_TYPE_CDMA: {
881             sptr<CdmaCellInformation> cellinfo = static_cast<CdmaCellInformation *>(currentCellInfo_.GetRefPtr());
882             sptr<CdmaCellLocation> cellLocation = new CdmaCellLocation;
883             cellLocation->SetCdmaParam(cellinfo->GetBaseId(), cellinfo->GetLatitude(), cellinfo->GetLongitude(),
884                 cellinfo->GetNid(), cellinfo->GetSid());
885             return cellLocation;
886         }
887         case CellInformation::CellType::CELL_TYPE_LTE:
888         case CellInformation::CellType::CELL_TYPE_NR:
889             return GetCellLocationExt(type);
890         default:
891             TELEPHONY_LOGE("CellInfo::GetCellLocation cell type error slotId:%{public}d", slotId_);
892             break;
893     }
894     return nullptr;
895 }
896 
GetCellLocationExt(CellInformation::CellType type)897 sptr<CellLocation> CellInfo::GetCellLocationExt(CellInformation::CellType type)
898 {
899     if (currentCellInfo_ == nullptr) {
900         TELEPHONY_LOGE("CellInfo::GetCellLocationExt is null slotId:%{public}d", slotId_);
901         return nullptr;
902     }
903     if (type == CellInformation::CellType::CELL_TYPE_LTE) {
904         sptr<LteCellInformation> cellinfo = static_cast<LteCellInformation *>(currentCellInfo_.GetRefPtr());
905         sptr<GsmCellLocation> cellLocation = new GsmCellLocation;
906         cellLocation->SetGsmParam(cellinfo->GetCellId(), cellinfo->GetTac());
907         return cellLocation;
908     } else if (type == CellInformation::CellType::CELL_TYPE_NR) {
909         sptr<NrCellInformation> cellinfo = static_cast<NrCellInformation *>(currentCellInfo_.GetRefPtr());
910         sptr<GsmCellLocation> cellLocation = new GsmCellLocation;
911         cellLocation->SetGsmParam(cellinfo->GetCellId(), cellinfo->GetTac());
912         return cellLocation;
913     }
914     return nullptr;
915 }
916 } // namespace Telephony
917 } // namespace OHOS