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