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 #ifdef FEATURE_GNSS_SUPPORT
17 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE
18 #include "agnss_event_callback.h"
19
20 #include <singleton.h>
21
22 #ifdef TEL_CELLULAR_DATA_ENABLE
23 #include "cellular_data_client.h"
24 #endif
25 #ifdef TEL_CORE_SERVICE_ENABLE
26 #include "core_service_client.h"
27 #endif
28
29 #include "common_utils.h"
30 #include "gnss_ability.h"
31 #include "location_log.h"
32 #include "securec.h"
33
34 #ifdef WIFI_ENABLE
35 #include "wifi_scan.h"
36 #endif
37
38 namespace OHOS {
39 namespace Location {
RequestSetUpAgnssDataLink(const AGnssDataLinkRequest & request)40 int32_t AGnssEventCallback::RequestSetUpAgnssDataLink(const AGnssDataLinkRequest& request)
41 {
42 LBSLOGI(GNSS, "AGnssEventCallback::RequestSetUpAgnssDataLink. agnsstype:%{public}d, setUpType:%{public}d",
43 static_cast<int>(request.agnssType), static_cast<int>(request.setUpType));
44 return ERR_OK;
45 }
46
RequestSubscriberSetId(SubscriberSetIdType type)47 int32_t AGnssEventCallback::RequestSubscriberSetId(SubscriberSetIdType type)
48 {
49 LBSLOGI(GNSS, "AGnssEventCallback::RequestSubscriberSetId. type:%{public}d", static_cast<int>(type));
50 std::string imsi;
51 #if defined(TEL_CORE_SERVICE_ENABLE) && defined(TEL_CELLULAR_DATA_ENABLE)
52 int slotId = Telephony::CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
53 std::u16string tempImsi;
54 DelayedRefSingleton<Telephony::CoreServiceClient>::GetInstance().GetIMSI(slotId, tempImsi);
55 imsi = CommonUtils::Str16ToStr8(tempImsi);
56 #endif
57 SubscriberSetId setId;
58 setId.type = HDI::Location::Agnss::V2_0::AGNSS_SETID_TYPE_IMSI;
59 setId.id = imsi;
60 auto gnssAbility = GnssAbility::GetInstance();
61 gnssAbility->SetSetId(setId);
62 return ERR_OK;
63 }
64
GetWiFiRefInfo(AGnssRefInfo & refInfo)65 __attribute__((no_sanitize("cfi"))) void AGnssEventCallback::GetWiFiRefInfo(AGnssRefInfo& refInfo)
66 {
67 #ifdef WIFI_ENABLE
68 std::vector<Wifi::WifiScanInfo> wifiScanInfo;
69 std::shared_ptr<Wifi::WifiScan> ptrWifiScan = Wifi::WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
70 if (ptrWifiScan == nullptr) {
71 LBSLOGE(GNSS, "%{public}s WifiScan get instance failed", __func__);
72 return;
73 }
74 int ret = ptrWifiScan->GetScanInfoList(wifiScanInfo);
75 if (ret != Wifi::WIFI_OPT_SUCCESS) {
76 LBSLOGE(GNSS, "GetScanInfoList failed");
77 return;
78 }
79 if (wifiScanInfo.size() == 0) {
80 LBSLOGE(GNSS, "empty mac.");
81 return;
82 }
83 uint8_t macArray[MAC_LEN];
84 if (CommonUtils::GetMacArray(wifiScanInfo[0].bssid, macArray) != EOK) {
85 LBSLOGE(GNSS, "GetMacArray failed.");
86 return;
87 }
88 for (size_t i = 0; i < MAC_LEN; i++) {
89 refInfo.mac.mac.push_back(macArray[i]);
90 }
91 #endif
92 }
93
GetCellRefInfo(AGnssRefInfo & refInfo)94 void AGnssEventCallback::GetCellRefInfo(AGnssRefInfo& refInfo)
95 {
96 #if defined(TEL_CORE_SERVICE_ENABLE) && defined(TEL_CELLULAR_DATA_ENABLE)
97 int slotId = Telephony::CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
98 std::vector<sptr<CellInformation>> cellInformations;
99 DelayedRefSingleton<Telephony::CoreServiceClient>::GetInstance().GetCellInfoList(slotId, cellInformations);
100 if (cellInformations.size() == 0) {
101 LBSLOGE(GNSS, "empty cell info list.");
102 return;
103 }
104 for (sptr<CellInformation> infoItem : cellInformations) {
105 if (!infoItem->GetIsCamped()) {
106 LBSLOGE(GNSS, "GetIsCamped return false");
107 continue;
108 }
109 CellInformation::CellType cellType = infoItem->GetNetworkType();
110 switch (cellType) {
111 case CellInformation::CellType::CELL_TYPE_GSM: {
112 JudgmentDataGsm(refInfo, infoItem);
113 break;
114 }
115 case CellInformation::CellType::CELL_TYPE_LTE: {
116 JudgmentDataLte(refInfo, infoItem);
117 break;
118 }
119 case CellInformation::CellType::CELL_TYPE_CDMA:
120 case CellInformation::CellType::CELL_TYPE_WCDMA:
121 case CellInformation::CellType::CELL_TYPE_TDSCDMA: {
122 JudgmentDataUmts(refInfo, infoItem);
123 break;
124 }
125 case CellInformation::CellType::CELL_TYPE_NR: {
126 JudgmentDataNr(refInfo, infoItem);
127 break;
128 }
129 default:
130 break;
131 }
132 break;
133 }
134 #endif
135 }
136
RequestAgnssRefInfo(AGnssRefInfoType type)137 int32_t AGnssEventCallback::RequestAgnssRefInfo(AGnssRefInfoType type)
138 {
139 AGnssRefInfo refInfo;
140 refInfo.type = type;
141 GetWiFiRefInfo(refInfo);
142 GetCellRefInfo(refInfo);
143 auto gnssAbility = GnssAbility::GetInstance();
144 if (gnssAbility == nullptr) {
145 LBSLOGE(GNSS, "RequestAgnssRefInfo: gnss ability is nullptr");
146 return ERR_OK;
147 }
148 gnssAbility->SetRefInfo(refInfo);
149 return ERR_OK;
150 }
151
152 #ifdef TEL_CORE_SERVICE_ENABLE
JudgmentDataGsm(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)153 void AGnssEventCallback::JudgmentDataGsm(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
154 {
155 auto gsmCellInfo = static_cast<Telephony::GsmCellInformation *>(infoItem.GetRefPtr());
156 if (gsmCellInfo != nullptr) {
157 refInfo.cellId.type = HDI::Location::Agnss::V2_0::CELLID_TYPE_GSM;
158 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMcc()));
159 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMnc()));
160 refInfo.cellId.lac = static_cast<unsigned short>(gsmCellInfo->GetLac());
161 refInfo.cellId.cid = static_cast<unsigned int>(gsmCellInfo->GetCellId());
162 }
163 }
164
JudgmentDataLte(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)165 void AGnssEventCallback::JudgmentDataLte(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
166 {
167 auto lteCellInfo = static_cast<Telephony::LteCellInformation *>(infoItem.GetRefPtr());
168 if (lteCellInfo != nullptr) {
169 refInfo.cellId.type = HDI::Location::Agnss::V2_0::CELLID_TYPE_LTE;
170 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMcc()));
171 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMnc()));
172 refInfo.cellId.tac = static_cast<unsigned short>(lteCellInfo->GetTac());
173 refInfo.cellId.cid = static_cast<unsigned int>(lteCellInfo->GetCellId());
174 refInfo.cellId.pcid = static_cast<unsigned short>(lteCellInfo->GetPci());
175 }
176 }
177
JudgmentDataNr(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)178 void AGnssEventCallback::JudgmentDataNr(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
179 {
180 auto nrCellInfo = static_cast<Telephony::NrCellInformation *>(infoItem.GetRefPtr());
181 if (nrCellInfo != nullptr) {
182 refInfo.cellId.type = HDI::Location::Agnss::V2_0::CELLID_TYPE_NR;
183 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMcc()));
184 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMnc()));
185 refInfo.cellId.tac = static_cast<unsigned short>(nrCellInfo->GetTac());
186 refInfo.cellId.cid = static_cast<unsigned int>(nrCellInfo->GetCellId());
187 refInfo.cellId.pcid = static_cast<unsigned short>(nrCellInfo->GetPci());
188 refInfo.cellId.nci = static_cast<unsigned int>(nrCellInfo->GetNci());
189 }
190 }
191
JudgmentDataUmts(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)192 void AGnssEventCallback::JudgmentDataUmts(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
193 {
194 auto wcdmaCellInfo = static_cast<Telephony::WcdmaCellInformation *>(infoItem.GetRefPtr());
195 if (wcdmaCellInfo != nullptr) {
196 refInfo.cellId.type = HDI::Location::Agnss::V2_0::CELLID_TYPE_UMTS;
197 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMcc()));
198 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMnc()));
199 refInfo.cellId.lac = static_cast<unsigned short>(wcdmaCellInfo->GetLac());
200 refInfo.cellId.cid = static_cast<unsigned int>(wcdmaCellInfo->GetCellId());
201 }
202 }
203 #endif
204 } // namespace Location
205 } // namespace OHOS
206 #endif
207 #endif
208