1 /*
2 * Copyright (c) 2021-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 "idevmgr_hdi.h"
17 #include <iostream>
18 #include <osal_mem.h>
19 #include "hdf_sbuf.h"
20 #include <cmath>
21 #include <cstdio>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <securec.h>
25 #include "hdf_base.h"
26 #include "hdf_log.h"
27 #include "osal_time.h"
28 #include "v1_0/ia_gnss_interface.h"
29 #include "agnss_interface_impl.h"
30 #include "agnss_callback_impl.h"
31 #include "cellular_data_client.h"
32 #include "gnss_callback_impl.h"
33 #include "gnss_interface_impl.h"
34
35 using namespace OHOS::HDI::Location::Agnss::V1_0;
36 using namespace OHOS::HDI::Location::Gnss::V1_0;
37 using namespace std;
38 using namespace testing::ext;
39
40 namespace {
41 using OHOS::Telephony::CellInformation;
42 sptr<IAGnssInterface> g_iagnssHci = nullptr;
43 sptr<IGnssInterface> g_ignssHci = nullptr;
44 constexpr const char *AGNSS_SERVICE_NAME = "agnss_interface_service";
45 constexpr const char *GNSS_SERVICE_NAME = "gnss_interface_service";
46 constexpr const char *GEOFENCE_SERVICE_NAME = "geofence_interface_service";
47 const char DEFAULT_STRING[] = "error";
48 const std::wstring DEFAULT_WSTRING = L"error";
49 const std::u16string DEFAULT_USTRING = u"error";
50 }
51
52 class LocationAgnssTest : public testing::Test {
53 public:
54 static void SetUpTestCase();
55 static void TearDownTestCase();
56 void SetUp();
57 void TearDown();
58 };
59
RequestSetUpAgnssDataLink(const AGnssDataLinkRequest & request)60 int32_t AgnssCallbackImpl::RequestSetUpAgnssDataLink(const AGnssDataLinkRequest& request)
61 {
62 printf("AGnssEventCallback::RequestSetUpAgnssDataLink. agnsstype:%d, setUpType:%d",
63 static_cast<int>(request.agnssType), static_cast<int>(request.setUpType));
64 return HDF_SUCCESS;
65 }
66
RequestSubscriberSetId(SubscriberSetIdType type)67 int32_t AgnssCallbackImpl::RequestSubscriberSetId(SubscriberSetIdType type)
68 {
69 printf("AGnssEventCallback::RequestSubscriberSetId. type:%d", static_cast<int>(type));
70 int slotId = Telephony::CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
71 std::u16string imsi;
72 DelayedRefSingleton<Telephony::CoreServiceClient>::GetInstance().GetIMSI(slotId, imsi);
73 SubscriberSetId setId;
74 setId.type = HDI::Location::Agnss::V1_0::SETID_TYPE_IMSI;
75 setId.id = Str16ToStr8(imsi);
76 if (g_iagnssHci == nullptr) {
77 printf("g_iagnssHci is null!");
78 return HDF_FAILURE;
79 }
80 g_iagnssHci->SetSubscriberSetId(setId);
81 return HDF_SUCCESS;
82 }
83
RequestAgnssRefInfo()84 int32_t AgnssCallbackImpl::RequestAgnssRefInfo()
85 {
86 if (g_iagnssHci == nullptr) {
87 printf("g_iagnssHci is null!");
88 return HDF_FAILURE;
89 }
90 int slotId = Telephony::CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
91 std::vector<sptr<CellInformation>> cellInformations;
92 DelayedRefSingleton<Telephony::CoreServiceClient>::GetInstance().GetCellInfoList(slotId, cellInformations);
93 printf("RequestAgnssRefInfo,cellInformations.");
94 for (sptr<CellInformation> infoItem : cellInformations) {
95 if (!infoItem->GetIsCamped()) {
96 printf("GetIsCamped return false");
97 continue;
98 }
99 AGnssRefInfo refInfo;
100 CellInformation::CellType cellType = infoItem->GetNetworkType();
101 refInfo.type = HDI::Location::Agnss::V1_0::ANSS_REF_INFO_TYPE_CELLID;
102 switch (cellType) {
103 case CellInformation::CellType::CELL_TYPE_GSM: {
104 JudgmentDataGsm(refInfo, infoItem);
105 break;
106 }
107 case CellInformation::CellType::CELL_TYPE_LTE: {
108 JudgmentDataLte(refInfo, infoItem);
109 break;
110 }
111 case CellInformation::CellType::CELL_TYPE_CDMA:
112 case CellInformation::CellType::CELL_TYPE_WCDMA:
113 case CellInformation::CellType::CELL_TYPE_TDSCDMA: {
114 JudgmentDataUmts(refInfo, infoItem);
115 break;
116 }
117 case CellInformation::CellType::CELL_TYPE_NR: {
118 JudgmentDataNr(refInfo, infoItem);
119 break;
120 }
121 default:
122 break;
123 }
124 g_iagnssHci->SetAgnssRefInfo(refInfo);
125 break;
126 }
127 return HDF_SUCCESS;
128 }
129
JudgmentDataGsm(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)130 void AgnssCallbackImpl::JudgmentDataGsm(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
131 {
132 auto gsmCellInfo = static_cast<Telephony::GsmCellInformation *>(infoItem.GetRefPtr());
133 if (gsmCellInfo != nullptr) {
134 refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_GSM;
135 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMcc()));
136 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(gsmCellInfo->GetMnc()));
137 refInfo.cellId.lac = static_cast<unsigned short>(gsmCellInfo->GetLac());
138 refInfo.cellId.cid = static_cast<unsigned int>(gsmCellInfo->GetCellId());
139 }
140 }
141
JudgmentDataLte(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)142 void AgnssCallbackImpl::JudgmentDataLte(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
143 {
144 auto lteCellInfo = static_cast<Telephony::LteCellInformation *>(infoItem.GetRefPtr());
145 if (lteCellInfo != nullptr) {
146 refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_LTE;
147 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMcc()));
148 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(lteCellInfo->GetMnc()));
149 refInfo.cellId.tac = static_cast<unsigned short>(lteCellInfo->GetTac());
150 refInfo.cellId.cid = static_cast<unsigned int>(lteCellInfo->GetCellId());
151 refInfo.cellId.pcid = static_cast<unsigned short>(lteCellInfo->GetPci());
152 }
153 }
154
JudgmentDataNr(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)155 void AgnssCallbackImpl::JudgmentDataNr(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
156 {
157 auto nrCellInfo = static_cast<Telephony::NrCellInformation *>(infoItem.GetRefPtr());
158 if (nrCellInfo != nullptr) {
159 refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_NR;
160 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMcc()));
161 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(nrCellInfo->GetMnc()));
162 refInfo.cellId.tac = static_cast<unsigned short>(nrCellInfo->GetTac());
163 refInfo.cellId.cid = static_cast<unsigned int>(nrCellInfo->GetCellId());
164 refInfo.cellId.pcid = static_cast<unsigned short>(nrCellInfo->GetPci());
165 refInfo.cellId.nci = static_cast<unsigned int>(nrCellInfo->GetNci());
166 }
167 }
168
JudgmentDataUmts(AGnssRefInfo & refInfo,sptr<CellInformation> infoItem)169 void AgnssCallbackImpl::JudgmentDataUmts(AGnssRefInfo& refInfo, sptr<CellInformation> infoItem)
170 {
171 auto wcdmaCellInfo = static_cast<Telephony::WcdmaCellInformation *>(infoItem.GetRefPtr());
172 if (wcdmaCellInfo != nullptr) {
173 refInfo.cellId.type = HDI::Location::Agnss::V1_0::CELLID_TYPE_UMTS;
174 refInfo.cellId.mcc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMcc()));
175 refInfo.cellId.mnc = static_cast<unsigned short>(std::stoi(wcdmaCellInfo->GetMnc()));
176 refInfo.cellId.lac = static_cast<unsigned short>(wcdmaCellInfo->GetLac());
177 refInfo.cellId.cid = static_cast<unsigned int>(wcdmaCellInfo->GetCellId());
178 }
179 }
180
Str16ToStr8(std::u16string str)181 std::string AgnssCallbackImpl::Str16ToStr8(std::u16string str)
182 {
183 if (str == DEFAULT_USTRING) {
184 return DEFAULT_STRING;
185 }
186 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert(DEFAULT_STRING);
187 std::string result = convert.to_bytes(str);
188 return result == DEFAULT_STRING ? "" : result;
189 }
190
ReportLocation(const LocationInfo & location)191 int32_t GnssCallbackImpl::ReportLocation(const LocationInfo& location)
192 {
193 if (location.timeSinceBoot != 0) {
194 printf("Location success!!");
195 return HDF_SUCCESS;
196 }
197 else{
198 printf("Location fail!!");
199 return HDF_FAILURE;
200 }
201 }
202
ReportGnssWorkingStatus(GnssWorkingStatus status)203 int32_t GnssCallbackImpl::ReportGnssWorkingStatus(GnssWorkingStatus status)
204 {
205 if (status == GnssWorkingStatus::GNSS_STATUS_NONE) {
206 printf("GNSS_STATUS_NONE");
207 return HDF_SUCCESS;
208 }
209 else if(status == GnssWorkingStatus::GNSS_STATUS_SESSION_BEGIN){
210 printf("GNSS_STATUS_SESSION_BEGIN");
211 return HDF_SUCCESS;
212 }
213 else if(status == GnssWorkingStatus::GNSS_STATUS_SESSION_END){
214 printf("GNSS_STATUS_SESSION_END");
215 return HDF_SUCCESS;
216 }
217 else if(status == GnssWorkingStatus::GNSS_STATUS_ENGINE_ON){
218 printf("GNSS_STATUS_ENGINE_ON");
219 return HDF_SUCCESS;
220 }
221 else if(status == GnssWorkingStatus::GNSS_STATUS_ENGINE_OFF){
222 printf("GNSS_STATUS_ENGINE_OFF");
223 return HDF_SUCCESS;
224 }
225 else{
226 printf("Gnss status fail");
227 return HDF_FAILURE;
228 }
229 }
230
ReportNmea(int64_t timestamp,const std::string & nmea,int32_t length)231 int32_t GnssCallbackImpl::ReportNmea(int64_t timestamp, const std::string& nmea, int32_t length)
232 {
233 if (length >= 0) {
234 printf("Report nmea success");
235 return HDF_SUCCESS;
236 }
237 else{
238 printf("Report nmea fail");
239 return HDF_FAILURE;
240 }
241 }
242
ReportGnssCapabilities(GnssCapabilities capabilities)243 int32_t GnssCallbackImpl::ReportGnssCapabilities(GnssCapabilities capabilities)
244 {
245 return HDF_SUCCESS;
246 }
247
ReportSatelliteStatusInfo(const SatelliteStatusInfo & info)248 int32_t GnssCallbackImpl::ReportSatelliteStatusInfo(const SatelliteStatusInfo& info)
249 {
250 if (info.satellitesNumber <= 0) {
251 printf("SvStatusCallback, satellites_num <= 0!");
252 return HDF_ERR_INVALID_PARAM;
253 }
254 if (((info.carrierFrequencies).size()) > 0) {
255 printf("Get satellite info success!!");
256 return HDF_SUCCESS;
257 }
258 else{
259 printf("Get satellite info fail!!");
260 return HDF_FAILURE;
261 }
262 }
263
RequestGnssReferenceInfo(GnssRefInfoType type)264 int32_t GnssCallbackImpl::RequestGnssReferenceInfo(GnssRefInfoType type)
265 {
266 return HDF_SUCCESS;
267 }
268
RequestPredictGnssData()269 int32_t GnssCallbackImpl::RequestPredictGnssData()
270 {
271 return HDF_SUCCESS;
272 }
273
ReportCachedLocation(const std::vector<LocationInfo> & gnssLocations)274 int32_t GnssCallbackImpl::ReportCachedLocation(const std::vector<LocationInfo>& gnssLocations)
275 {
276 return HDF_SUCCESS;
277 }
278
279
SetUpTestCase()280 void LocationAgnssTest::SetUpTestCase()
281 {
282 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
283 if (devmgr == nullptr) {
284 printf("fail to get devmgr.");
285 return;
286 }
287 if (devmgr->LoadDevice(GNSS_SERVICE_NAME) != 0) {
288 printf("Load gnss service failed!");
289 return;
290 }
291 if (devmgr->LoadDevice(AGNSS_SERVICE_NAME) != 0) {
292 printf("Load agnss service failed!");
293 return;
294 }
295 if (devmgr->LoadDevice(GEOFENCE_SERVICE_NAME) != 0) {
296 printf("Load geofence service failed!");
297 return;
298 }
299 g_iagnssHci = IAGnssInterface::Get();
300 g_ignssHci = IGnssInterface::Get();
301 sptr<IGnssCallback> gnss_callback = new (std::nothrow) GnssCallbackImpl();
302 if (gnss_callback == nullptr) {
303 ASSERT_NE(nullptr, gnss_callback);
304 return;
305 }
306 g_ignssHci->EnableGnss(gnss_callback);
307 }
308
TearDownTestCase()309 void LocationAgnssTest::TearDownTestCase()
310 {
311 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
312 if (devmgr == nullptr) {
313 printf("fail to get devmgr.");
314 return;
315 }
316 if (devmgr->UnloadDevice(GNSS_SERVICE_NAME) != 0) {
317 printf("Load gnss service failed!");
318 return;
319 }
320 if (devmgr->UnloadDevice(AGNSS_SERVICE_NAME) != 0) {
321 printf("Load agnss service failed!");
322 return;
323 }
324 if (devmgr->UnloadDevice(GEOFENCE_SERVICE_NAME) != 0) {
325 printf("Load geofence service failed!");
326 return;
327 }
328 g_ignssHci->DisableGnss();
329 }
330
SetUp()331 void LocationAgnssTest::SetUp()
332 {
333 }
334
TearDown()335 void LocationAgnssTest::TearDown()
336 {
337 }
338
339
340 /**
341 * @tc.name: SetAgnssServer0001
342 * @tc.desc: Set the Agnss Server Information.
343 * @tc.type: FUNC
344 */
345 HWTEST_F(LocationAgnssTest, SUB_DriverSystem_SetAgnssServer_0100, TestSize.Level1)
346 {
347 if (g_iagnssHci == nullptr) {
348 ASSERT_NE(nullptr, g_iagnssHci);
349 return;
350 }
351 AGnssServerInfo server;
352 server.type = AGnssUserPlaneProtocol::AGNSS_TYPE_SUPL;
353 server.server = "test";
354 server.port = 80001;
355 int32_t ret = g_iagnssHci->SetAgnssServer(server);
356 EXPECT_EQ(HDF_SUCCESS, ret);
357 }
358
359 /**
360 * @tc.name: SetSubscriberSetId0001
361 * @tc.desc: Set Subscriber Identity.
362 * @tc.type: FUNC
363 */
364 HWTEST_F(LocationAgnssTest, SUB_DriverSystem_SetSubscriberSetId_0100, TestSize.Level1)
365 {
366 if (g_iagnssHci == nullptr) {
367 ASSERT_NE(nullptr, g_iagnssHci);
368 return;
369 }
370 SubscriberSetId id;
371 id.type = SubscriberSetIdType::SETID_TYPE_NONE;
372 id.id = "111";
373 int32_t ret = g_iagnssHci->SetSubscriberSetId(id);
374 EXPECT_EQ(HDF_SUCCESS, ret);
375 }
376
377 /**
378 * @tc.name: SetAgnssRefInfo0001
379 * @tc.desc: Ingesting Reference Information.
380 * @tc.type: FUNC
381 */
382 HWTEST_F(LocationAgnssTest, SUB_DriverSystem_SetAgnssRefInfo_0100, TestSize.Level1)
383 {
384 if (g_iagnssHci == nullptr) {
385 ASSERT_NE(nullptr, g_iagnssHci);
386 return;
387 }
388 AGnssRefInfo refInfo;
389 refInfo.type = AGnssRefInfoType::ANSS_REF_INFO_TYPE_CELLID;
390 refInfo.cellId.type = CellIdType::CELLID_TYPE_GSM;
391 refInfo.cellId.mcc = 100;
392 refInfo.cellId.mnc = 8;
393 refInfo.cellId.lac = 20;
394 refInfo.cellId.cid = 50;
395 refInfo.cellId.tac = 60;
396 refInfo.cellId.pcid = 80;
397 refInfo.cellId.nci = 90;
398 int32_t ret = g_iagnssHci->SetAgnssRefInfo(refInfo);
399 EXPECT_EQ(HDF_SUCCESS, ret);
400 }
401
402 /**
403 * @tc.name: SetAgnssCallback0001
404 * @tc.desc: Set the agnss callback function.
405 * @tc.type: FUNC
406 */
407 HWTEST_F(LocationAgnssTest, SUB_DriverSystem_SetAgnssCallback_0100, TestSize.Level1)
408 {
409 if (g_iagnssHci == nullptr) {
410 ASSERT_NE(nullptr, g_iagnssHci);
411 return;
412 }
413 sptr<IAGnssCallback> agnss_callback = new (std::nothrow) AgnssCallbackImpl();
414 if (agnss_callback == nullptr) {
415 ASSERT_NE(nullptr, agnss_callback);
416 return;
417 }
418 int32_t ret = g_iagnssHci->SetAgnssCallback(agnss_callback);
419 EXPECT_EQ(HDF_SUCCESS, ret);
420 }
421