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 HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
24 #include <osal_mem.h>
25 #include "osal_time.h"
26 #include "hdf_sbuf.h"
27 #include "hdf_base.h"
28 #include "hdf_log.h"
29 #include "v2_0/igeofence_interface.h"
30 #include "geofence_callback_impl.h"
31
32 using namespace OHOS::HDI::Location::Geofence::V2_0;
33 #endif
34 using namespace std;
35 using namespace testing::ext;
36
37 namespace {
38 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
39 sptr<IGeofenceInterface> g_igeofenceHci = nullptr;
40 constexpr const char *AGNSS_SERVICE_NAME = "agnss_interface_service";
41 constexpr const char *GNSS_SERVICE_NAME = "gnss_interface_service";
42 constexpr const char *GEOFENCE_SERVICE_NAME = "geofence_interface_service";
43 constexpr const char *LOCATION_HOST_NAME = "location_host";
44 #endif
45 }
46
47 class LocationGeofenceTest: public testing::Test {
48 public:
49 static void SetUpTestCase();
50 static void TearDownTestCase();
51 void SetUp();
52 void TearDown();
53 };
54
55 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
IsDeviceLoaded(const std::string & servName)56 bool IsDeviceLoaded(const std::string &servName)
57 {
58 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
59 if (devmgr == nullptr) {
60 printf("fail to get devmgr.\n");
61 return false;
62 }
63 std::vector<OHOS::HDI::DeviceManager::V1_0::HdiDevHostInfo> deviceInfos;
64 int ret = devmgr->ListAllDevice(deviceInfos);
65 if (ret != HDF_SUCCESS) {
66 printf("get listAllDevice failed\n");
67 return false;
68 }
69 auto itDevicesInfo = deviceInfos.begin();
70 for (;itDevicesInfo != deviceInfos.end(); itDevicesInfo++) {
71 if (itDevicesInfo->hostName == LOCATION_HOST_NAME) {
72 break;
73 }
74 }
75 if (itDevicesInfo == deviceInfos.end()) {
76 printf("The host is not found:LOCATION_HOST_NAME\n");
77 return false;
78 }
79 auto itDevInfo = itDevicesInfo->devInfo.begin();
80 for (;itDevInfo != itDevicesInfo->devInfo.end(); itDevInfo++) {
81 if (itDevInfo->servName == servName) {
82 break;
83 }
84 }
85 if (itDevInfo == itDevicesInfo->devInfo.end()) {
86 printf("The devices is not found\n");
87 return false;
88 }
89 printf("already loaded...\n");
90 return true;
91 }
92
ReportGeofenceAvailability(bool isAvailable)93 int32_t GeofenceCallbackImpl::ReportGeofenceAvailability(bool isAvailable)
94 {
95 (void)isAvailable;
96 return HDF_SUCCESS;
97 }
98
ReportGeofenceEvent(int32_t fenceIndex,const LocationInfo & location,GeofenceEvent event,int64_t timestamp)99 int32_t GeofenceCallbackImpl::ReportGeofenceEvent(int32_t fenceIndex, const LocationInfo& location, GeofenceEvent event, int64_t timestamp)
100 {
101 (void)fenceIndex;
102 (void)location;
103 (void)event;
104 (void)timestamp;
105 return HDF_SUCCESS;
106 }
107
ReportGeofenceOperateResult(int32_t fenceIndex,GeofenceOperateType type,GeofenceOperateResult result)108 int32_t GeofenceCallbackImpl::ReportGeofenceOperateResult(int32_t fenceIndex, GeofenceOperateType type, GeofenceOperateResult result)
109 {
110 (void)fenceIndex;
111 (void)type;
112 (void)result;
113 return HDF_SUCCESS;
114 }
115 #endif
116
SetUpTestCase()117 void LocationGeofenceTest::SetUpTestCase()
118 {
119 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
120 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
121 if (devmgr == nullptr) {
122 printf("fail to get devmgr.\n");
123 return;
124 }
125 if (!IsDeviceLoaded(GNSS_SERVICE_NAME)) {
126 if (devmgr->LoadDevice(GNSS_SERVICE_NAME) != 0) {
127 printf("Load gnss service failed!\n");
128 return;
129 }
130 }
131 if (!IsDeviceLoaded(AGNSS_SERVICE_NAME)) {
132 if (devmgr->LoadDevice(AGNSS_SERVICE_NAME) != 0) {
133 printf("Load agnss service failed!\n");
134 return;
135 }
136 }
137 if (!IsDeviceLoaded(GEOFENCE_SERVICE_NAME)) {
138 if (devmgr->LoadDevice(GEOFENCE_SERVICE_NAME) != 0) {
139 printf("Load geofence service failed!\n");
140 return;
141 }
142 }
143 g_igeofenceHci = IGeofenceInterface::Get();
144 #endif
145 }
146
TearDownTestCase()147 void LocationGeofenceTest::TearDownTestCase()
148 {
149 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
150 auto devmgr = HDI::DeviceManager::V1_0::IDeviceManager::Get();
151 if (devmgr == nullptr) {
152 printf("fail to get devmgr.\n");
153 return;
154 }
155 if (devmgr->UnloadDevice(GNSS_SERVICE_NAME) != 0) {
156 printf("Load gnss service failed!\n");
157 return;
158 }
159 if (devmgr->UnloadDevice(AGNSS_SERVICE_NAME) != 0) {
160 printf("Load agnss service failed!\n");
161 return;
162 }
163 if (devmgr->UnloadDevice(GEOFENCE_SERVICE_NAME) != 0) {
164 printf("Load geofence service failed!\n");
165 return;
166 }
167 #endif
168 }
169
SetUp()170 void LocationGeofenceTest::SetUp()
171 {
172 }
173
TearDown()174 void LocationGeofenceTest::TearDown()
175 {
176 }
177
178
179 /**
180 * @tc.name: SetGeofenceCallback0100
181 * @tc.desc: Set callback function for geofence.
182 * @tc.type: FUNC
183 */
184 HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_EnableGnss_0100, TestSize.Level1)
185 {
186 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
187 if (g_igeofenceHci == nullptr) {
188 ASSERT_NE(nullptr, g_igeofenceHci);
189 return;
190 }
191 sptr<IGeofenceCallback> geo_callback = new (std::nothrow) GeofenceCallbackImpl();
192 if (geo_callback == nullptr) {
193 ASSERT_NE(nullptr, geo_callback);
194 return;
195 }
196 int32_t ret = g_igeofenceHci->SetGeofenceCallback(geo_callback);
197 EXPECT_EQ(HDF_SUCCESS, ret);
198 #endif
199 }
200
201
202 /**
203 * @tc.name: AddGnssGeofence0100
204 * @tc.desc: Add a geofence.
205 * @tc.type: FUNC
206 */
207 HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_AddGnssGeofence_0100, TestSize.Level1)
208 {
209 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
210 if (g_igeofenceHci == nullptr) {
211 ASSERT_NE(nullptr, g_igeofenceHci);
212 return;
213 }
214 GeofenceInfo fence;
215 fence.fenceIndex = 3;
216 fence.latitude = 118.90;
217 fence.longitude = 15.25;
218 fence.radius = 12.26;
219 GeofenceEvent geoevent = GeofenceEvent::GEOFENCE_EVENT_ENTERED ;
220 int32_t ret = g_igeofenceHci->AddGnssGeofence(fence,geoevent);
221 EXPECT_EQ(HDF_SUCCESS, ret);
222 #endif
223 }
224
225
226 /**
227 * @tc.name: DeleteGnssGeofence0100
228 * @tc.desc: Delete a geofence.
229 * @tc.type: FUNC
230 */
231 HWTEST_F(LocationGeofenceTest, SUB_DriverSystem_DeleteGnssGeofence_0100, TestSize.Level1)
232 {
233 #ifdef HDF_DRIVERS_INTERFACE_GEOFENCE_ENABLE
234 if (g_igeofenceHci == nullptr) {
235 ASSERT_NE(nullptr, g_igeofenceHci);
236 return;
237 }
238 int fenceIndex = 5;
239 int32_t ret = g_igeofenceHci->DeleteGnssGeofence(fenceIndex);
240 EXPECT_EQ(HDF_SUCCESS, ret);
241 #endif
242 }
243