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