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     if (gnssAbility == nullptr) {
62         LBSLOGE(GNSS, "RequestSubscriberSetId: gnss ability is nullptr");
63         return ERR_OK;
64     }
65     gnssAbility->SetSetId(setId);
66     return ERR_OK;
67 }
68 
GetWiFiRefInfo(AGnssRefInfo & refInfo)69 __attribute__((no_sanitize("cfi"))) void AGnssEventCallback::GetWiFiRefInfo(AGnssRefInfo& refInfo)
70 {
71 #ifdef WIFI_ENABLE
72     std::vector<Wifi::WifiScanInfo> wifiScanInfo;
73     std::shared_ptr<Wifi::WifiScan> ptrWifiScan = Wifi::WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
74     if (ptrWifiScan == nullptr) {
75         LBSLOGE(GNSS, "%{public}s WifiScan get instance failed", __func__);
76         return;
77     }
78     int ret = ptrWifiScan->GetScanInfoList(wifiScanInfo);
79     if (ret != Wifi::WIFI_OPT_SUCCESS) {
80         LBSLOGE(GNSS, "GetScanInfoList failed");
81         return;
82     }
83     if (wifiScanInfo.size() == 0) {
84         LBSLOGE(GNSS, "empty mac.");
85         return;
86     }
87     uint8_t macArray[MAC_LEN];
88     if (CommonUtils::GetMacArray(wifiScanInfo[0].bssid, macArray) != EOK) {
89         LBSLOGE(GNSS, "GetMacArray failed.");
90         return;
91     }
92     for (size_t i = 0; i < MAC_LEN; i++) {
93         refInfo.mac.mac.push_back(macArray[i]);
94     }
95 #endif
96 }
97 
GetCellRefInfo(AGnssRefInfo & refInfo)98 void AGnssEventCallback::GetCellRefInfo(AGnssRefInfo& refInfo)
99 {
100 #if defined(TEL_CORE_SERVICE_ENABLE) && defined(TEL_CELLULAR_DATA_ENABLE)
101     int slotId = Telephony::CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
102     std::vector<sptr<CellInformation>> cellInformations;
103     DelayedRefSingleton<Telephony::CoreServiceClient>::GetInstance().GetCellInfoList(slotId, cellInformations);
104     if (cellInformations.size() == 0) {
105         LBSLOGE(GNSS, "empty cell info list.");
106         return;
107     }
108     for (sptr<CellInformation> infoItem : cellInformations) {
109         if (!infoItem->GetIsCamped()) {
110             LBSLOGE(GNSS, "GetIsCamped return false");
111             continue;
112         }
113         CellInformation::CellType cellType = infoItem->GetNetworkType();
114         switch (cellType) {
115             case CellInformation::CellType::CELL_TYPE_GSM: {
116                 JudgmentDataGsm(refInfo, infoItem);
117                 break;
118             }
119             case CellInformation::CellType::CELL_TYPE_LTE: {
120                 JudgmentDataLte(refInfo, infoItem);
121                 break;
122             }
123             case CellInformation::CellType::CELL_TYPE_CDMA:
124             case CellInformation::CellType::CELL_TYPE_WCDMA:
125             case CellInformation::CellType::CELL_TYPE_TDSCDMA: {
126                 JudgmentDataUmts(refInfo, infoItem);
127                 break;
128             }
129             case CellInformation::CellType::CELL_TYPE_NR: {
130                 JudgmentDataNr(refInfo, infoItem);
131                 break;
132             }
133             default:
134                 break;
135         }
136         break;
137     }
138 #endif
139 }
140 
RequestAgnssRefInfo(AGnssRefInfoType type)141 int32_t AGnssEventCallback::RequestAgnssRefInfo(AGnssRefInfoType type)
142 {
143     AGnssRefInfo refInfo;
144     refInfo.type = type;
145     GetWiFiRefInfo(refInfo);
146     GetCellRefInfo(refInfo);
147     auto gnssAbility = GnssAbility::GetInstance();
148     if (gnssAbility == nullptr) {
149         LBSLOGE(GNSS, "RequestAgnssRefInfo: gnss ability is nullptr");
150         return ERR_OK;
151     }
152     gnssAbility->SetRefInfo(refInfo);
153     return ERR_OK;
154 }
155 
156 #ifdef TEL_CORE_SERVICE_ENABLE
JudgmentDataGsm(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)157 void AGnssEventCallback::JudgmentDataGsm(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
158 {
159     auto gsmCellInfo = static_cast<Telephony::GsmCellInformation *>(infoItem.GetRefPtr());
160     if (gsmCellInfo != nullptr) {
161         refInfo.cellId.type = HDI::Location::Agnss::V2_0::CELLID_TYPE_GSM;
162         refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMcc()));
163         refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMnc()));
164         refInfo.cellId.lac = static_cast<unsigned short>(gsmCellInfo->GetLac());
165         refInfo.cellId.cid = static_cast<unsigned int>(gsmCellInfo->GetCellId());
166     }
167 }
168 
JudgmentDataLte(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)169 void AGnssEventCallback::JudgmentDataLte(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
170 {
171     auto lteCellInfo = static_cast<Telephony::LteCellInformation *>(infoItem.GetRefPtr());
172     if (lteCellInfo != nullptr) {
173         refInfo.cellId.type = HDI::Location::Agnss::V2_0::CELLID_TYPE_LTE;
174         refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMcc()));
175         refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMnc()));
176         refInfo.cellId.tac = static_cast<unsigned short>(lteCellInfo->GetTac());
177         refInfo.cellId.cid = static_cast<unsigned int>(lteCellInfo->GetCellId());
178         refInfo.cellId.pcid = static_cast<unsigned short>(lteCellInfo->GetPci());
179     }
180 }
181 
JudgmentDataNr(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)182 void AGnssEventCallback::JudgmentDataNr(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
183 {
184     auto nrCellInfo = static_cast<Telephony::NrCellInformation *>(infoItem.GetRefPtr());
185     if (nrCellInfo != nullptr) {
186         refInfo.cellId.type = HDI::Location::Agnss::V2_0::CELLID_TYPE_NR;
187         refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMcc()));
188         refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMnc()));
189         refInfo.cellId.tac = static_cast<unsigned short>(nrCellInfo->GetTac());
190         refInfo.cellId.cid = static_cast<unsigned int>(nrCellInfo->GetCellId());
191         refInfo.cellId.pcid = static_cast<unsigned short>(nrCellInfo->GetPci());
192         refInfo.cellId.nci = static_cast<unsigned int>(nrCellInfo->GetNci());
193     }
194 }
195 
JudgmentDataUmts(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)196 void AGnssEventCallback::JudgmentDataUmts(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
197 {
198     auto wcdmaCellInfo = static_cast<Telephony::WcdmaCellInformation *>(infoItem.GetRefPtr());
199     if (wcdmaCellInfo != nullptr) {
200         refInfo.cellId.type = HDI::Location::Agnss::V2_0::CELLID_TYPE_UMTS;
201         refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMcc()));
202         refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMnc()));
203         refInfo.cellId.lac = static_cast<unsigned short>(wcdmaCellInfo->GetLac());
204         refInfo.cellId.cid = static_cast<unsigned int>(wcdmaCellInfo->GetCellId());
205     }
206 }
207 #endif
208 }  // namespace Location
209 }  // namespace OHOS
210 #endif
211 #endif
212