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