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 <memory>
18 #include <iostream>
19 #include <cmath>
20 #include <cstdio>
21 #include <unistd.h>
22 #include <gtest/gtest.h>
23 #include <securec.h>
24 #ifdef FEATURE_GNSS_SUPPORT
25 #include <osal_mem.h>
26 #include "hdf_base.h"
27 #include "hdf_log.h"
28 #include "osal_time.h"
29 #include "hdf_sbuf.h"
30 #include "v2_0/ignss_interface.h"
31 #include "gnss_callback_impl.h"
32
33 using namespace OHOS::HDI::Location::Gnss::V2_0;
34 #endif
35 using namespace std;
36 using namespace testing::ext;
37
38 namespace {
39 #ifdef FEATURE_GNSS_SUPPORT
40 sptr<IGnssInterface> g_ignssHci = nullptr;
41 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE
42 constexpr const char *AGNSS_SERVICE_NAME = "agnss_interface_service";
43 #endif
44 constexpr const char *GNSS_SERVICE_NAME = "gnss_interface_service";
45 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
46 constexpr const char *GEOFENCE_SERVICE_NAME = "geofence_interface_service";
47 #endif
48 constexpr const char *LOCATION_HOST_NAME = "location_host";
49 #endif
50 }
51
52 class LocationGnssTest : public testing::Test {
53 public:
54 static void SetUpTestCase();
55 static void TearDownTestCase();
56 void SetUp();
57 void TearDown();
58 };
59
60 #ifdef FEATURE_GNSS_SUPPORT
IsDeviceLoaded(const std::string & servName)61 bool IsDeviceLoaded(const std::string &servName)
62 {
63 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
64 if (devmgr == nullptr) {
65 printf("fail to get devmgr.\n");
66 return false;
67 }
68 std::vector<OHOS::HDI::DeviceManager::V1_0::HdiDevHostInfo> deviceInfos;
69 int ret = devmgr->ListAllDevice(deviceInfos);
70 if (ret != HDF_SUCCESS) {
71 printf("get listAllDevice failed\n");
72 return false;
73 }
74 auto itDevicesInfo = deviceInfos.begin();
75 for (;itDevicesInfo != deviceInfos.end(); itDevicesInfo++) {
76 if (itDevicesInfo->hostName == LOCATION_HOST_NAME) {
77 break;
78 }
79 }
80 if (itDevicesInfo == deviceInfos.end()) {
81 printf("The host is not found:LOCATION_HOST_NAME\n");
82 return false;
83 }
84 auto itDevInfo = itDevicesInfo->devInfo.begin();
85 for (;itDevInfo != itDevicesInfo->devInfo.end(); itDevInfo++) {
86 if (itDevInfo->servName == servName) {
87 break;
88 }
89 }
90 if (itDevInfo == itDevicesInfo->devInfo.end()) {
91 printf("The devices is not found\n");
92 return false;
93 }
94 printf("already loaded...\n");
95 return true;
96 }
97
ReportLocation(const LocationInfo & location)98 int32_t GnssCallbackImpl::ReportLocation(const LocationInfo& location)
99 {
100 if (location.timeSinceBoot != 0) {
101 printf("Location success!!\n");
102 return HDF_SUCCESS;
103 }
104 else{
105 printf("Location fail!!\n");
106 return HDF_FAILURE;
107 }
108 }
109
ReportGnssWorkingStatus(GnssWorkingStatus status)110 int32_t GnssCallbackImpl::ReportGnssWorkingStatus(GnssWorkingStatus status)
111 {
112 if (status == GnssWorkingStatus::GNSS_WORKING_STATUS_NONE) {
113 printf("GNSS_WORKING_STATUS_NONE\n");
114 return HDF_SUCCESS;
115 } else if (status == GnssWorkingStatus::GNSS_WORKING_STATUS_SESSION_BEGIN) {
116 printf("GNSS_WORKING_STATUS_SESSION_BEGIN\n");
117 return HDF_SUCCESS;
118 } else if (status == GnssWorkingStatus::GNSS_WORKING_STATUS_SESSION_END) {
119 printf("GNSS_WORKING_STATUS_SESSION_END\n");
120 return HDF_SUCCESS;
121 } else if (status == GnssWorkingStatus::GNSS_WORKING_STATUS_ENGINE_ON) {
122 printf("GNSS_WORKING_STATUS_ENGINE_ON\n");
123 return HDF_SUCCESS;
124 } else if (status == GnssWorkingStatus::GNSS_WORKING_STATUS_ENGINE_OFF) {
125 printf("GNSS_WORKING_STATUS_ENGINE_OFF\n");
126 return HDF_SUCCESS;
127 } else {
128 printf("Gnss status fail\n");
129 return HDF_FAILURE;
130 }
131 }
132
ReportNmea(int64_t timestamp,const std::string & nmea,int32_t length)133 int32_t GnssCallbackImpl::ReportNmea(int64_t timestamp, const std::string& nmea, int32_t length)
134 {
135 (void)timestamp;
136 (void)nmea;
137 if (length >= 0) {
138 printf("Report nmea success\n");
139 return HDF_SUCCESS;
140 }
141 else{
142 printf("Report nmea fail\n");
143 return HDF_FAILURE;
144 }
145 }
146
ReportGnssCapabilities(unsigned int capabilities)147 int32_t GnssCallbackImpl::ReportGnssCapabilities(unsigned int capabilities)
148 {
149 (void)capabilities;
150 return HDF_SUCCESS;
151 }
152
ReportSatelliteStatusInfo(const SatelliteStatusInfo & info)153 int32_t GnssCallbackImpl::ReportSatelliteStatusInfo(const SatelliteStatusInfo& info)
154 {
155 if (info.satellitesNumber <= 0) {
156 printf("SvStatusCallback, satellites_num <= 0!\n");
157 return HDF_ERR_INVALID_PARAM;
158 }
159 if (((info.carrierFrequencies).size()) > 0) {
160 printf("Get satellite info success!!\n");
161 return HDF_SUCCESS;
162 }
163 else{
164 printf("Get satellite info fail!!\n");
165 return HDF_FAILURE;
166 }
167 }
168
RequestGnssReferenceInfo(GnssRefInfoType type)169 int32_t GnssCallbackImpl::RequestGnssReferenceInfo(GnssRefInfoType type)
170 {
171 (void)type;
172 return HDF_SUCCESS;
173 }
174
RequestPredictGnssData()175 int32_t GnssCallbackImpl::RequestPredictGnssData()
176 {
177 return HDF_SUCCESS;
178 }
179
ReportCachedLocation(const std::vector<LocationInfo> & gnssLocations)180 int32_t GnssCallbackImpl::ReportCachedLocation(const std::vector<LocationInfo>& gnssLocations)
181 {
182 (void)gnssLocations;
183 return HDF_SUCCESS;
184 }
185
ReportGnssNiNotification(const GnssNiNotificationRequest & notification)186 int32_t GnssCallbackImpl::ReportGnssNiNotification(const GnssNiNotificationRequest& notification)
187 {
188 (void)notification;
189 return HDF_SUCCESS;
190 }
191 #endif
192
SetUpTestCase()193 void LocationGnssTest::SetUpTestCase()
194 {
195 #ifdef FEATURE_GNSS_SUPPORT
196 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
197 if (devmgr == nullptr) {
198 printf("fail to get devmgr.\n");
199 return;
200 }
201 if (!IsDeviceLoaded(GNSS_SERVICE_NAME)) {
202 if (devmgr->LoadDevice(GNSS_SERVICE_NAME) != 0) {
203 printf("Load gnss service failed!\n");
204 return;
205 }
206 }
207 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE
208 if (!IsDeviceLoaded(AGNSS_SERVICE_NAME)) {
209 if (devmgr->LoadDevice(AGNSS_SERVICE_NAME) != 0) {
210 printf("Load agnss service failed!\n");
211 return;
212 }
213 }
214 #endif
215 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
216 if (!IsDeviceLoaded(GEOFENCE_SERVICE_NAME)) {
217 if (devmgr->LoadDevice(GEOFENCE_SERVICE_NAME) != 0) {
218 printf("Load geofence service failed!\n");
219 return;
220 }
221 }
222 #endif
223 g_ignssHci = IGnssInterface::Get();
224 #endif
225 }
226
TearDownTestCase()227 void LocationGnssTest::TearDownTestCase()
228 {
229 #ifdef FEATURE_GNSS_SUPPORT
230 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
231 if (devmgr == nullptr) {
232 printf("fail to get devmgr.\n");
233 return;
234 }
235 if (devmgr->UnloadDevice(GNSS_SERVICE_NAME) != 0) {
236 printf("Load gnss service failed!\n");
237 return;
238 }
239 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE
240 if (devmgr->UnloadDevice(AGNSS_SERVICE_NAME) != 0) {
241 printf("Load agnss service failed!\n");
242 return;
243 }
244 #endif
245 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
246 if (devmgr->UnloadDevice(GEOFENCE_SERVICE_NAME) != 0) {
247 printf("Load geofence service failed!\n");
248 return;
249 }
250 #endif
251 #endif
252 }
253
SetUp()254 void LocationGnssTest::SetUp()
255 {
256 }
257
TearDown()258 void LocationGnssTest::TearDown()
259 {
260 }
261
262
263 /**
264 * @tc.name: EnableGnss0100
265 * @tc.desc: Enable the GNSS module and transmit the callback of the upper layer to the GNSS module..
266 * @tc.type: FUNC
267 */
268 HWTEST_F(LocationGnssTest, SUB_DriverSystem_EnableGnss_0100, TestSize.Level1)
269 {
270 #ifdef FEATURE_GNSS_SUPPORT
271 if (g_ignssHci == nullptr) {
272 ASSERT_NE(nullptr, g_ignssHci);
273 return;
274 }
275 sptr<IGnssCallback> gnss_callback = new (std::nothrow) GnssCallbackImpl();
276 if (gnss_callback == nullptr) {
277 ASSERT_NE(nullptr, gnss_callback);
278 return;
279 }
280 int32_t ret = g_ignssHci->EnableGnss(gnss_callback);
281 EXPECT_EQ(HDF_SUCCESS, ret);
282 GnssStartType starttype = GnssStartType::GNSS_START_TYPE_NORMAL;
283 int32_t ret1 = g_ignssHci->StartGnss(starttype);
284 EXPECT_EQ(HDF_SUCCESS, ret1);
285 #endif
286 }
287
288
289 /**
290 * @tc.name: SetGnssConfigPara0100
291 * @tc.desc: Setting gnss configuration parameters.
292 * @tc.type: FUNC
293 */
294 HWTEST_F(LocationGnssTest, SUB_DriverSystem_SetGnssConfigPara_0100, TestSize.Level1)
295 {
296 #ifdef FEATURE_GNSS_SUPPORT
297 if (g_ignssHci == nullptr) {
298 ASSERT_NE(nullptr, g_ignssHci);
299 return;
300 }
301 GnssConfigPara para;
302 para.gnssBasic.minInterval = 10;
303 para.gnssBasic.gnssMode = GnssWorkingMode::GNSS_WORKING_MODE_STANDALONE;
304 para.gnssCaching.interval = 20;
305 para.gnssCaching.fifoFullNotify = true;
306 int32_t ret = g_ignssHci->SetGnssConfigPara(para);
307 EXPECT_EQ(HDF_SUCCESS, ret);
308 #endif
309 }
310
311
312 /**
313 * @tc.name: SetGnssReferenceInfo0100
314 * @tc.desc: Inject reference information to the GNSS module.
315 * @tc.type: FUNC
316 */
317 HWTEST_F(LocationGnssTest, SUB_DriverSystem_SetGnssReferenceInfo_0100, TestSize.Level1)
318 {
319 #ifdef FEATURE_GNSS_SUPPORT
320 if (g_ignssHci == nullptr) {
321 ASSERT_NE(nullptr, g_ignssHci);
322 return;
323 }
324 GnssRefInfo refInfo;
325 refInfo.type = GnssRefInfoType::GNSS_REF_INFO_TIME;
326 refInfo.time.time = 50;
327 refInfo.time.elapsedRealtime = 100;
328 refInfo.time.uncertaintyOfTime = 200;
329 refInfo.gnssLocation.latitude = 39.56;
330 refInfo.gnssLocation.longitude = 116.20;
331 refInfo.gnssLocation.horizontalAccuracy = 90;
332 refInfo.bestLocation.latitude = 39.58;
333 refInfo.bestLocation.longitude = 116.45;
334 refInfo.bestLocation.altitude = 110;
335 refInfo.bestLocation.horizontalAccuracy = 60;
336 refInfo.bestLocation.speed = 60;
337 refInfo.bestLocation.bearing = 60;
338 refInfo.bestLocation.timeForFix = 60;
339 refInfo.bestLocation.timeSinceBoot = 60;
340 int32_t ret = g_ignssHci->SetGnssReferenceInfo(refInfo);
341 EXPECT_EQ(HDF_SUCCESS, ret);
342 #endif
343 }
344
345 /**
346 * @tc.name: StopGnss0100
347 * @tc.desc: Stop the navigation function in normal mode.
348 * @tc.type: FUNC
349 */
350 HWTEST_F(LocationGnssTest, SUB_DriverSystem_StopGnss_0100, TestSize.Level1)
351 {
352 #ifdef FEATURE_GNSS_SUPPORT
353 if (g_ignssHci == nullptr) {
354 ASSERT_NE(nullptr, g_ignssHci);
355 return;
356 }
357 GnssStartType stoptype = GnssStartType::GNSS_START_TYPE_NORMAL;
358 int32_t ret = g_ignssHci->StopGnss(stoptype);
359 EXPECT_EQ(HDF_SUCCESS, ret);
360 #endif
361 }
362
363 /**
364 * @tc.name: DeleteAuxiliaryData0100
365 * @tc.desc: Delete the specified auxiliary data.
366 * @tc.type: FUNC
367 */
368 HWTEST_F(LocationGnssTest, SUB_DriverSystem_DeleteAuxiliaryData_0100, TestSize.Level1)
369 {
370 #ifdef FEATURE_GNSS_SUPPORT
371 if (g_ignssHci == nullptr) {
372 ASSERT_NE(nullptr, g_ignssHci);
373 return;
374 }
375 GnssAuxiliaryDataType auxdata = GnssAuxiliaryDataType::GNSS_AUXILIARY_DATA_EPHEMERIS;
376 int32_t ret = g_ignssHci->DeleteAuxiliaryData(auxdata);
377 EXPECT_EQ(HDF_SUCCESS, ret);
378 #endif
379 }
380
381
382 /**
383 * @tc.name: SetPredictGnssData0100
384 * @tc.desc: Ingesting Predict Gnss Data.
385 * @tc.type: FUNC
386 */
387 HWTEST_F(LocationGnssTest, SUB_DriverSystem_SetPredictGnssData_0100, TestSize.Level1)
388 {
389 #ifdef FEATURE_GNSS_SUPPORT
390 if (g_ignssHci == nullptr) {
391 ASSERT_NE(nullptr, g_ignssHci);
392 return;
393 }
394 const std::string str1 = "testing";
395 int32_t ret = g_ignssHci->SetPredictGnssData(str1);
396 EXPECT_EQ(HDF_SUCCESS, ret);
397 #endif
398 }
399
400 /**
401 * @tc.name: GetCachedGnssLocationsSize0100
402 * @tc.desc: Obtain the number of locations that can be cached by the GNSS module.
403 * @tc.type: FUNC
404 */
405 HWTEST_F(LocationGnssTest, SUB_DriverSystem_GetCachedGnssLocationsSize_0100, TestSize.Level1)
406 {
407 #ifdef FEATURE_GNSS_SUPPORT
408 if (g_ignssHci == nullptr) {
409 ASSERT_NE(nullptr, g_ignssHci);
410 return;
411 }
412 int cach_size = 60;
413 int32_t ret = g_ignssHci->GetCachedGnssLocationsSize(cach_size);
414 EXPECT_EQ(HDF_SUCCESS, ret);
415 #endif
416 }
417
418 /**
419 * @tc.name: GetCachedGnssLocations0100
420 * @tc.desc: Request to obtain all the location information in the GNSS cache at a time and clear the cache buffer.
421 * @tc.type: FUNC
422 */
423 HWTEST_F(LocationGnssTest, SUB_DriverSystem_GetCachedGnssLocations_0100, TestSize.Level1)
424 {
425 #ifdef FEATURE_GNSS_SUPPORT
426 if (g_ignssHci == nullptr) {
427 ASSERT_NE(nullptr, g_ignssHci);
428 return;
429 }
430 int32_t ret = g_ignssHci->GetCachedGnssLocations();
431 EXPECT_EQ(HDF_SUCCESS, ret);
432 #endif
433 }
434
435 /**
436 * @tc.name: DisableGnss0100
437 * @tc.desc: Disable the GNSS module.
438 * @tc.type: FUNC
439 */
440 HWTEST_F(LocationGnssTest, SUB_DriverSystem_DisableGnss_0100, TestSize.Level1)
441 {
442 #ifdef FEATURE_GNSS_SUPPORT
443 if (g_ignssHci == nullptr) {
444 ASSERT_NE(nullptr, g_ignssHci);
445 return;
446 }
447 int32_t ret = g_ignssHci->DisableGnss();
448 EXPECT_EQ(HDF_SUCCESS, ret);
449 #endif
450 }
451