1 /*
2 * Copyright (c) 2022 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 "agnss_event_callback.h"
17
18 #include <singleton.h>
19
20 #include "cellular_data_client.h"
21
22 #include "common_utils.h"
23 #include "gnss_ability.h"
24 #include "location_log.h"
25
26 namespace OHOS {
27 namespace Location {
RequestSetUpAgnssDataLink(const AGnssDataLinkRequest & request)28 int32_t AGnssEventCallback::RequestSetUpAgnssDataLink(const AGnssDataLinkRequest& request)
29 {
30 LBSLOGI(GNSS, "AGnssEventCallback::RequestSetUpAgnssDataLink. agnsstype:%{public}d, setUpType:%{public}d",
31 static_cast<int>(request.agnssType), static_cast<int>(request.setUpType));
32 return ERR_OK;
33 }
34
RequestSubscriberSetId(SubscriberSetIdType type)35 int32_t AGnssEventCallback::RequestSubscriberSetId(SubscriberSetIdType type)
36 {
37 LBSLOGI(GNSS, "AGnssEventCallback::RequestSubscriberSetId. type:%{public}d", static_cast<int>(type));
38 int slotId = Telephony::CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
39 std::u16string tempImsi;
40 DelayedRefSingleton<Telephony::CoreServiceClient>::GetInstance().GetIMSI(slotId, tempImsi);
41 std::string imsi = CommonUtils::Str16ToStr8(tempImsi);
42 SubscriberSetId setId;
43 setId.type = HDI::Location::Agnss::V1_0::SETID_TYPE_IMSI;
44 setId.id = imsi;
45 auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
46 if (gnssAbility == nullptr) {
47 LBSLOGE(GNSS, "RequestSubscriberSetId: gnss ability is nullptr");
48 return ERR_OK;
49 }
50 gnssAbility.get()->SetSetId(setId);
51 return ERR_OK;
52 }
53
RequestAgnssRefInfo()54 int32_t AGnssEventCallback::RequestAgnssRefInfo()
55 {
56 int slotId = Telephony::CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
57 std::vector<sptr<CellInformation>> cellInformations;
58 DelayedRefSingleton<Telephony::CoreServiceClient>::GetInstance().GetCellInfoList(slotId, cellInformations);
59 LBSLOGI(GNSS, "RequestAgnssRefInfo,cellInformations.");
60 for (sptr<CellInformation> infoItem : cellInformations) {
61 if (!infoItem->GetIsCamped()) {
62 LBSLOGE(GNSS, "GetIsCamped return false");
63 continue;
64 }
65 AGnssRefInfo refInfo;
66 CellInformation::CellType cellType = infoItem->GetNetworkType();
67 refInfo.type = HDI::Location::Agnss::V1_0::ANSS_REF_INFO_TYPE_CELLID;
68 switch (cellType) {
69 case CellInformation::CellType::CELL_TYPE_GSM: {
70 JudgmentDataGsm(refInfo, infoItem);
71 break;
72 }
73 case CellInformation::CellType::CELL_TYPE_LTE: {
74 JudgmentDataLte(refInfo, infoItem);
75 break;
76 }
77 case CellInformation::CellType::CELL_TYPE_CDMA:
78 case CellInformation::CellType::CELL_TYPE_WCDMA:
79 case CellInformation::CellType::CELL_TYPE_TDSCDMA: {
80 JudgmentDataUmts(refInfo, infoItem);
81 break;
82 }
83 case CellInformation::CellType::CELL_TYPE_NR: {
84 JudgmentDataNr(refInfo, infoItem);
85 break;
86 }
87 default:
88 break;
89 }
90 auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
91 if (gnssAbility == nullptr) {
92 LBSLOGE(GNSS, "RequestAgnssRefInfo: gnss ability is nullptr");
93 break;
94 }
95 gnssAbility.get()->SetRefInfo(refInfo);
96 break;
97 }
98 return ERR_OK;
99 }
100
JudgmentDataGsm(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)101 void AGnssEventCallback::JudgmentDataGsm(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
102 {
103 auto gsmCellInfo = static_cast<Telephony::GsmCellInformation *>(infoItem.GetRefPtr());
104 if (gsmCellInfo != nullptr) {
105 refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_GSM;
106 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMcc()));
107 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMnc()));
108 refInfo.cellId.lac = static_cast<unsigned short>(gsmCellInfo->GetLac());
109 refInfo.cellId.cid = static_cast<unsigned int>(gsmCellInfo->GetCellId());
110 }
111 }
112
JudgmentDataLte(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)113 void AGnssEventCallback::JudgmentDataLte(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
114 {
115 auto lteCellInfo = static_cast<Telephony::LteCellInformation *>(infoItem.GetRefPtr());
116 if (lteCellInfo != nullptr) {
117 refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_LTE;
118 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMcc()));
119 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMnc()));
120 refInfo.cellId.tac = static_cast<unsigned short>(lteCellInfo->GetTac());
121 refInfo.cellId.cid = static_cast<unsigned int>(lteCellInfo->GetCellId());
122 refInfo.cellId.pcid = static_cast<unsigned short>(lteCellInfo->GetPci());
123 }
124 }
125
JudgmentDataNr(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)126 void AGnssEventCallback::JudgmentDataNr(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
127 {
128 auto nrCellInfo = static_cast<Telephony::NrCellInformation *>(infoItem.GetRefPtr());
129 if (nrCellInfo != nullptr) {
130 refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_NR;
131 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMcc()));
132 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMnc()));
133 refInfo.cellId.tac = static_cast<unsigned short>(nrCellInfo->GetTac());
134 refInfo.cellId.cid = static_cast<unsigned int>(nrCellInfo->GetCellId());
135 refInfo.cellId.pcid = static_cast<unsigned short>(nrCellInfo->GetPci());
136 refInfo.cellId.nci = static_cast<unsigned int>(nrCellInfo->GetNci());
137 }
138 }
139
JudgmentDataUmts(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)140 void AGnssEventCallback::JudgmentDataUmts(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
141 {
142 auto wcdmaCellInfo = static_cast<Telephony::WcdmaCellInformation *>(infoItem.GetRefPtr());
143 if (wcdmaCellInfo != nullptr) {
144 refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_UMTS;
145 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMcc()));
146 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMnc()));
147 refInfo.cellId.lac = static_cast<unsigned short>(wcdmaCellInfo->GetLac());
148 refInfo.cellId.cid = static_cast<unsigned int>(wcdmaCellInfo->GetCellId());
149 }
150 }
151 } // namespace Location
152 } // namespace OHOS
153