• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::V1_0::SETID_TYPE_IMSI;
59     setId.id = imsi;
60     auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
61     if (gnssAbility == nullptr) {
62         LBSLOGE(GNSS, "RequestSubscriberSetId: gnss ability is nullptr");
63         return ERR_OK;
64     }
65     gnssAbility.get()->SetSetId(setId);
66     return ERR_OK;
67 }
68 
GetWiFiRefInfo(AGnssRefInfo & refInfo)69 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()141 int32_t AGnssEventCallback::RequestAgnssRefInfo()
142 {
143     AGnssRefInfo refInfo;
144     refInfo.type = HDI::Location::Agnss::V1_0::ANSS_REF_INFO_TYPE_CELLID;
145     GetWiFiRefInfo(refInfo);
146     GetCellRefInfo(refInfo);
147     auto gnssAbility = DelayedSingleton<GnssAbility>::GetInstance();
148     if (gnssAbility == nullptr) {
149         LBSLOGE(GNSS, "RequestAgnssRefInfo: gnss ability is nullptr");
150         return ERR_OK;
151     }
152     gnssAbility.get()->SetRefInfo(refInfo);
153     return ERR_OK;
154 }
155 
JudgmentDataGsm(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)156 void AGnssEventCallback::JudgmentDataGsm(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
157 {
158     auto gsmCellInfo = static_cast<Telephony::GsmCellInformation *>(infoItem.GetRefPtr());
159     if (gsmCellInfo != nullptr) {
160         refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_GSM;
161         refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMcc()));
162         refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMnc()));
163         refInfo.cellId.lac = static_cast<unsigned short>(gsmCellInfo->GetLac());
164         refInfo.cellId.cid = static_cast<unsigned int>(gsmCellInfo->GetCellId());
165     }
166 }
167 
JudgmentDataLte(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)168 void AGnssEventCallback::JudgmentDataLte(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
169 {
170     auto lteCellInfo = static_cast<Telephony::LteCellInformation *>(infoItem.GetRefPtr());
171     if (lteCellInfo != nullptr) {
172         refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_LTE;
173         refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMcc()));
174         refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMnc()));
175         refInfo.cellId.tac = static_cast<unsigned short>(lteCellInfo->GetTac());
176         refInfo.cellId.cid = static_cast<unsigned int>(lteCellInfo->GetCellId());
177         refInfo.cellId.pcid = static_cast<unsigned short>(lteCellInfo->GetPci());
178     }
179 }
180 
JudgmentDataNr(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)181 void AGnssEventCallback::JudgmentDataNr(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
182 {
183     auto nrCellInfo = static_cast<Telephony::NrCellInformation *>(infoItem.GetRefPtr());
184     if (nrCellInfo != nullptr) {
185         refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_NR;
186         refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMcc()));
187         refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMnc()));
188         refInfo.cellId.tac = static_cast<unsigned short>(nrCellInfo->GetTac());
189         refInfo.cellId.cid = static_cast<unsigned int>(nrCellInfo->GetCellId());
190         refInfo.cellId.pcid = static_cast<unsigned short>(nrCellInfo->GetPci());
191         refInfo.cellId.nci = static_cast<unsigned int>(nrCellInfo->GetNci());
192     }
193 }
194 
JudgmentDataUmts(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)195 void AGnssEventCallback::JudgmentDataUmts(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
196 {
197     auto wcdmaCellInfo = static_cast<Telephony::WcdmaCellInformation *>(infoItem.GetRefPtr());
198     if (wcdmaCellInfo != nullptr) {
199         refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_UMTS;
200         refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMcc()));
201         refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMnc()));
202         refInfo.cellId.lac = static_cast<unsigned short>(wcdmaCellInfo->GetLac());
203         refInfo.cellId.cid = static_cast<unsigned int>(wcdmaCellInfo->GetCellId());
204     }
205 }
206 }  // namespace Location
207 }  // namespace OHOS
208 #endif
209 #endif
210