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