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