blob: 61d23b0237ccc1e213d1499bbdf1de9fee805cc2 [file] [log] [blame]
Wyatt Riley6c26ed72017-02-21 17:21:53 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "VtsHalGnssV1_0TargetTest"
18#include <android/hardware/gnss/1.0/IGnss.h>
Steven Moreland4e7a3072017-04-06 12:15:23 -070019#include <log/log.h>
Wyatt Riley6c26ed72017-02-21 17:21:53 -080020
Yuexi Maed2bb4e2017-03-10 00:44:45 -080021#include <VtsHalHidlTargetTestBase.h>
Wyatt Riley6c26ed72017-02-21 17:21:53 -080022
23#include <chrono>
24#include <condition_variable>
25#include <mutex>
26
27using android::hardware::Return;
28using android::hardware::Void;
29
30using android::hardware::gnss::V1_0::GnssLocation;
31using android::hardware::gnss::V1_0::GnssLocationFlags;
32using android::hardware::gnss::V1_0::IGnss;
33using android::hardware::gnss::V1_0::IGnssCallback;
Wyatt Rileyc1399592017-03-22 14:08:26 -070034using android::hardware::gnss::V1_0::IGnssDebug;
35using android::hardware::gnss::V1_0::IGnssMeasurement;
Wyatt Riley6c26ed72017-02-21 17:21:53 -080036using android::sp;
37
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -070038#define TIMEOUT_SEC 2 // for basic commands/responses
Wyatt Riley6c26ed72017-02-21 17:21:53 -080039
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -070040// for command line argument on how strictly to run the test
41bool sAgpsIsPresent = false; // if SUPL or XTRA assistance available
Wyatt Riley94c7a042017-06-15 17:16:24 -070042bool sSignalIsWeak = false; // if GNSS signals are weak (e.g. light indoor)
Wyatt Rileyc1399592017-03-22 14:08:26 -070043
Wyatt Riley6c26ed72017-02-21 17:21:53 -080044// The main test class for GNSS HAL.
Yuexi Maed2bb4e2017-03-10 00:44:45 -080045class GnssHalTest : public ::testing::VtsHalHidlTargetTestBase {
Wyatt Riley6c26ed72017-02-21 17:21:53 -080046 public:
47 virtual void SetUp() override {
Wyatt Riley917640b2017-03-16 16:25:55 -070048 // Clean between tests
49 capabilities_called_count_ = 0;
50 location_called_count_ = 0;
51 info_called_count_ = 0;
Wyatt Riley6c26ed72017-02-21 17:21:53 -080052
Yuexi Maed2bb4e2017-03-10 00:44:45 -080053 gnss_hal_ = ::testing::VtsHalHidlTargetTestBase::getService<IGnss>();
Wyatt Riley6c26ed72017-02-21 17:21:53 -080054 ASSERT_NE(gnss_hal_, nullptr);
55
56 gnss_cb_ = new GnssCallback(*this);
57 ASSERT_NE(gnss_cb_, nullptr);
58
59 auto result = gnss_hal_->setCallback(gnss_cb_);
60 if (!result.isOk()) {
Wyatt Riley917640b2017-03-16 16:25:55 -070061 ALOGE("result of failed setCallback %s", result.description().c_str());
Wyatt Riley6c26ed72017-02-21 17:21:53 -080062 }
63
64 ASSERT_TRUE(result.isOk());
65 ASSERT_TRUE(result);
66
Wyatt Riley917640b2017-03-16 16:25:55 -070067 /*
68 * At least one callback should trigger - it may be capabilites, or
69 * system info first, so wait again if capabilities not received.
Wyatt Riley6c26ed72017-02-21 17:21:53 -080070 */
Wyatt Riley917640b2017-03-16 16:25:55 -070071 EXPECT_EQ(std::cv_status::no_timeout, wait(TIMEOUT_SEC));
72 if (capabilities_called_count_ == 0) {
73 EXPECT_EQ(std::cv_status::no_timeout, wait(TIMEOUT_SEC));
74 }
75
76 /*
77 * Generally should be 1 capabilites callback -
78 * or possibly 2 in some recovery cases (default cached & refreshed)
79 */
80 EXPECT_GE(capabilities_called_count_, 1);
81 EXPECT_LE(capabilities_called_count_, 2);
82
83 /*
84 * Clear notify/waiting counter, allowing up till the timeout after
85 * the last reply for final startup messages to arrive (esp. system
86 * info.)
87 */
88 while (wait(TIMEOUT_SEC) == std::cv_status::no_timeout) {
89 }
Wyatt Riley6c26ed72017-02-21 17:21:53 -080090 }
91
92 virtual void TearDown() override {
93 if (gnss_hal_ != nullptr) {
94 gnss_hal_->cleanup();
95 }
96 }
97
98 /* Used as a mechanism to inform the test that a callback has occurred */
99 inline void notify() {
100 std::unique_lock<std::mutex> lock(mtx_);
101 count++;
102 cv_.notify_one();
103 }
104
105 /* Test code calls this function to wait for a callback */
106 inline std::cv_status wait(int timeoutSeconds) {
107 std::unique_lock<std::mutex> lock(mtx_);
108
109 std::cv_status status = std::cv_status::no_timeout;
110 auto now = std::chrono::system_clock::now();
111 while (count == 0) {
112 status = cv_.wait_until(lock, now + std::chrono::seconds(timeoutSeconds));
113 if (status == std::cv_status::timeout) return status;
114 }
115 count--;
116 return status;
117 }
118
Wyatt Riley94c7a042017-06-15 17:16:24 -0700119 /*
120 * StartAndGetSingleLocation:
121 * Helper function to get one Location and check fields
122 *
123 * returns true if a location was successfully generated
124 */
125 bool StartAndGetSingleLocation(bool checkAccuracies) {
126 auto result = gnss_hal_->start();
127
128 EXPECT_TRUE(result.isOk());
129 EXPECT_TRUE(result);
130
131 /*
132 * GPS signals initially optional for this test, so don't expect fast fix,
133 * or no timeout, unless signal is present
134 */
135 int firstGnssLocationTimeoutSeconds = sAgpsIsPresent ? 15 : 45;
136 if (sSignalIsWeak) {
137 // allow more time for weak signals
138 firstGnssLocationTimeoutSeconds += 30;
139 }
140
141 wait(firstGnssLocationTimeoutSeconds);
142 if (sAgpsIsPresent) {
143 EXPECT_EQ(location_called_count_, 1);
144 }
145 if (location_called_count_ > 0) {
146 CheckLocation(last_location_, checkAccuracies);
147 return true;
148 }
149 return false;
150 }
151
152 /*
153 * StopAndClearLocations:
154 * Helper function to stop locations
155 *
156 * returns true if a location was successfully generated
157 */
158 void StopAndClearLocations() {
159 auto result = gnss_hal_->stop();
160
161 EXPECT_TRUE(result.isOk());
162 EXPECT_TRUE(result);
163
164 /*
165 * Clear notify/waiting counter, allowing up till the timeout after
166 * the last reply for final startup messages to arrive (esp. system
167 * info.)
168 */
169 while (wait(TIMEOUT_SEC) == std::cv_status::no_timeout) {
170 }
171 }
172
173 /*
174 * CheckLocation:
175 * Helper function to vet Location fields
176 */
177 void CheckLocation(GnssLocation& location, bool checkAccuracies) {
178 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_LAT_LONG);
179 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_ALTITUDE);
180 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED);
181 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_HORIZONTAL_ACCURACY);
182 // New uncertainties available in O must be provided,
183 // at least when paired with modern hardware (2017+)
184 if (checkAccuracies) {
185 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_VERTICAL_ACCURACY);
186 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED_ACCURACY);
187 if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING) {
188 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING_ACCURACY);
189 }
190 }
191 EXPECT_GE(location.latitudeDegrees, -90.0);
192 EXPECT_LE(location.latitudeDegrees, 90.0);
193 EXPECT_GE(location.longitudeDegrees, -180.0);
194 EXPECT_LE(location.longitudeDegrees, 180.0);
195 EXPECT_GE(location.altitudeMeters, -1000.0);
196 EXPECT_LE(location.altitudeMeters, 30000.0);
197 EXPECT_GE(location.speedMetersPerSec, 0.0);
198 EXPECT_LE(location.speedMetersPerSec, 5.0); // VTS tests are stationary.
199
200 // Non-zero speeds must be reported with an associated bearing
201 if (location.speedMetersPerSec > 0.0) {
202 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING);
203 }
204
205 /*
206 * Tolerating some especially high values for accuracy estimate, in case of
207 * first fix with especially poor geometry (happens occasionally)
208 */
209 EXPECT_GT(location.horizontalAccuracyMeters, 0.0);
210 EXPECT_LE(location.horizontalAccuracyMeters, 250.0);
211
212 /*
213 * Some devices may define bearing as -180 to +180, others as 0 to 360.
214 * Both are okay & understandable.
215 */
216 if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING) {
217 EXPECT_GE(location.bearingDegrees, -180.0);
218 EXPECT_LE(location.bearingDegrees, 360.0);
219 }
220 if (location.gnssLocationFlags & GnssLocationFlags::HAS_VERTICAL_ACCURACY) {
221 EXPECT_GT(location.verticalAccuracyMeters, 0.0);
222 EXPECT_LE(location.verticalAccuracyMeters, 500.0);
223 }
224 if (location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED_ACCURACY) {
225 EXPECT_GT(location.speedAccuracyMetersPerSecond, 0.0);
226 EXPECT_LE(location.speedAccuracyMetersPerSecond, 50.0);
227 }
228 if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING_ACCURACY) {
229 EXPECT_GT(location.bearingAccuracyDegrees, 0.0);
230 EXPECT_LE(location.bearingAccuracyDegrees, 360.0);
231 }
232
233 // Check timestamp > 1.48e12 (47 years in msec - 1970->2017+)
234 EXPECT_GT(location.timestamp, 1.48e12);
235 }
236
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800237 /* Callback class for data & Event. */
238 class GnssCallback : public IGnssCallback {
Wyatt Riley917640b2017-03-16 16:25:55 -0700239 public:
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800240 GnssHalTest& parent_;
241
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800242 GnssCallback(GnssHalTest& parent) : parent_(parent){};
243
244 virtual ~GnssCallback() = default;
245
246 // Dummy callback handlers
247 Return<void> gnssStatusCb(
Steven Moreland4b523952017-03-08 19:20:30 -0800248 const IGnssCallback::GnssStatusValue /* status */) override {
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800249 return Void();
250 }
251 Return<void> gnssSvStatusCb(
Steven Moreland4b523952017-03-08 19:20:30 -0800252 const IGnssCallback::GnssSvStatus& /* svStatus */) override {
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800253 return Void();
254 }
255 Return<void> gnssNmeaCb(
Steven Moreland4b523952017-03-08 19:20:30 -0800256 int64_t /* timestamp */,
257 const android::hardware::hidl_string& /* nmea */) override {
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800258 return Void();
259 }
260 Return<void> gnssAcquireWakelockCb() override { return Void(); }
261 Return<void> gnssReleaseWakelockCb() override { return Void(); }
262 Return<void> gnssRequestTimeCb() override { return Void(); }
263
264 // Actual (test) callback handlers
265 Return<void> gnssLocationCb(const GnssLocation& location) override {
266 ALOGI("Location received");
267 parent_.location_called_count_++;
268 parent_.last_location_ = location;
269 parent_.notify();
270 return Void();
271 }
272
273 Return<void> gnssSetCapabilitesCb(uint32_t capabilities) override {
274 ALOGI("Capabilities received %d", capabilities);
275 parent_.capabilities_called_count_++;
276 parent_.last_capabilities_ = capabilities;
277 parent_.notify();
278 return Void();
279 }
280
281 Return<void> gnssSetSystemInfoCb(
282 const IGnssCallback::GnssSystemInfo& info) override {
283 ALOGI("Info received, year %d", info.yearOfHw);
284 parent_.info_called_count_++;
285 parent_.last_info_ = info;
286 parent_.notify();
287 return Void();
288 }
289 };
290
291 sp<IGnss> gnss_hal_; // GNSS HAL to call into
292 sp<IGnssCallback> gnss_cb_; // Primary callback interface
293
294 /* Count of calls to set the following items, and the latest item (used by
295 * test.)
296 */
297 int capabilities_called_count_;
298 uint32_t last_capabilities_;
299
300 int location_called_count_;
301 GnssLocation last_location_;
302
303 int info_called_count_;
304 IGnssCallback::GnssSystemInfo last_info_;
305
306 private:
307 std::mutex mtx_;
308 std::condition_variable cv_;
309 int count;
310};
311
312/*
313 * SetCallbackCapabilitiesCleanup:
314 * Sets up the callback, awaits the capabilities, and calls cleanup
315 *
316 * Since this is just the basic operation of SetUp() and TearDown(),
Wyatt Riley917640b2017-03-16 16:25:55 -0700317 * the function definition is intentionally empty
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800318 */
319TEST_F(GnssHalTest, SetCallbackCapabilitiesCleanup) {}
320
Wyatt Riley917640b2017-03-16 16:25:55 -0700321/*
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800322 * GetLocation:
323 * Turns on location, waits 45 second for at least 5 locations,
324 * and checks them for reasonable validity.
325 */
326TEST_F(GnssHalTest, GetLocation) {
327#define MIN_INTERVAL_MSEC 500
328#define PREFERRED_ACCURACY 0 // Ideally perfect (matches GnssLocationProvider)
329#define PREFERRED_TIME_MSEC 0 // Ideally immediate
330
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800331#define LOCATION_TIMEOUT_SUBSEQUENT_SEC 3
332#define LOCATIONS_TO_CHECK 5
333
Wyatt Riley917640b2017-03-16 16:25:55 -0700334 bool checkMoreAccuracies =
335 (info_called_count_ > 0 && last_info_.yearOfHw >= 2017);
336
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800337 auto result = gnss_hal_->setPositionMode(
338 IGnss::GnssPositionMode::MS_BASED,
339 IGnss::GnssPositionRecurrence::RECURRENCE_PERIODIC, MIN_INTERVAL_MSEC,
340 PREFERRED_ACCURACY, PREFERRED_TIME_MSEC);
341
342 ASSERT_TRUE(result.isOk());
Wyatt Rileyc1399592017-03-22 14:08:26 -0700343 EXPECT_TRUE(result);
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800344
Wyatt Rileyc1399592017-03-22 14:08:26 -0700345 /*
346 * GPS signals initially optional for this test, so don't expect no timeout
347 * yet
348 */
Wyatt Riley94c7a042017-06-15 17:16:24 -0700349 bool gotLocation = StartAndGetSingleLocation(checkMoreAccuracies);
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800350
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -0700351 if (gotLocation) {
352 for (int i = 1; i < LOCATIONS_TO_CHECK; i++) {
Wyatt Riley94c7a042017-06-15 17:16:24 -0700353 EXPECT_EQ(std::cv_status::no_timeout, wait(LOCATION_TIMEOUT_SUBSEQUENT_SEC));
354 EXPECT_EQ(location_called_count_, i + 1);
355 CheckLocation(last_location_, checkMoreAccuracies);
Wyatt Riley917640b2017-03-16 16:25:55 -0700356 }
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800357 }
358
Wyatt Riley94c7a042017-06-15 17:16:24 -0700359 StopAndClearLocations();
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800360}
361
Wyatt Rileyc1399592017-03-22 14:08:26 -0700362/*
363 * InjectDelete:
364 * Ensures that calls to inject and/or delete information state are handled.
Wyatt Rileyc1399592017-03-22 14:08:26 -0700365 */
366TEST_F(GnssHalTest, InjectDelete) {
367 // confidently, well north of Alaska
368 auto result = gnss_hal_->injectLocation(80.0, -170.0, 1000.0);
369
Wyatt Rileyc1399592017-03-22 14:08:26 -0700370 ASSERT_TRUE(result.isOk());
371 EXPECT_TRUE(result);
372
373 // fake time, but generally reasonable values (time in Aug. 2018)
374 result = gnss_hal_->injectTime(1534567890123L, 123456L, 10000L);
375
376 ASSERT_TRUE(result.isOk());
377 EXPECT_TRUE(result);
378
379 auto resultVoid = gnss_hal_->deleteAidingData(IGnss::GnssAidingData::DELETE_ALL);
380
381 ASSERT_TRUE(resultVoid.isOk());
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -0700382
383 // Ensure we can get a good location after a bad injection has been deleted
Wyatt Riley94c7a042017-06-15 17:16:24 -0700384 StartAndGetSingleLocation(false);
385
386 StopAndClearLocations();
Wyatt Rileyc1399592017-03-22 14:08:26 -0700387}
388
389/*
390 * GetAllExtentions:
391 * Tries getting all optional extensions, and ensures a valid return
392 * null or actual extension, no crash.
393 * Confirms year-based required extensions (Measurement & Debug) are present
394 */
395TEST_F(GnssHalTest, GetAllExtensions) {
396 // Basic call-is-handled checks
397 auto gnssXtra = gnss_hal_->getExtensionXtra();
398 ASSERT_TRUE(gnssXtra.isOk());
399
400 auto gnssRil = gnss_hal_->getExtensionAGnssRil();
401 ASSERT_TRUE(gnssRil.isOk());
402
403 auto gnssAgnss = gnss_hal_->getExtensionAGnss();
404 ASSERT_TRUE(gnssAgnss.isOk());
405
406 auto gnssNi = gnss_hal_->getExtensionGnssNi();
407 ASSERT_TRUE(gnssNi.isOk());
408
409 auto gnssNavigationMessage = gnss_hal_->getExtensionGnssNavigationMessage();
410 ASSERT_TRUE(gnssNavigationMessage.isOk());
411
412 auto gnssConfiguration = gnss_hal_->getExtensionGnssConfiguration();
413 ASSERT_TRUE(gnssConfiguration.isOk());
414
415 auto gnssGeofencing = gnss_hal_->getExtensionGnssGeofencing();
416 ASSERT_TRUE(gnssGeofencing.isOk());
417
418 auto gnssBatching = gnss_hal_->getExtensionGnssBatching();
419 ASSERT_TRUE(gnssBatching.isOk());
420
421 // Verifying, in some cases, that these return actual extensions
422 auto gnssMeasurement = gnss_hal_->getExtensionGnssMeasurement();
423 ASSERT_TRUE(gnssMeasurement.isOk());
424 if (last_capabilities_ & IGnssCallback::Capabilities::MEASUREMENTS) {
425 sp<IGnssMeasurement> iGnssMeas = gnssMeasurement;
426 EXPECT_NE(iGnssMeas, nullptr);
427 }
428
429 auto gnssDebug = gnss_hal_->getExtensionGnssDebug();
430 ASSERT_TRUE(gnssDebug.isOk());
431 if (info_called_count_ > 0 && last_info_.yearOfHw >= 2017) {
432 sp<IGnssDebug> iGnssDebug = gnssDebug;
433 EXPECT_NE(iGnssDebug, nullptr);
434 }
435}
436
437/*
438 * MeasurementCapabilities:
439 * Verifies that modern hardware supports measurement capabilities.
440 */
441TEST_F(GnssHalTest, MeasurementCapabilites) {
442 if (info_called_count_ > 0 && last_info_.yearOfHw >= 2016) {
443 EXPECT_TRUE(last_capabilities_ & IGnssCallback::Capabilities::MEASUREMENTS);
444 }
445}
446
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800447int main(int argc, char** argv) {
448 ::testing::InitGoogleTest(&argc, argv);
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -0700449 /*
450 * These arguments not used by automated VTS testing.
451 * Only for use in manual testing, when wanting to run
452 * stronger tests that require the presence of GPS signal.
453 */
454 for (int i = 1; i < argc; i++) {
Wyatt Riley94c7a042017-06-15 17:16:24 -0700455 if (strcmp(argv[i], "-agps") == 0) {
456 sAgpsIsPresent = true;
457 } else if (strcmp(argv[i], "-weak") == 0) {
458 sSignalIsWeak = true;
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -0700459 }
460 }
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800461 int status = RUN_ALL_TESTS();
462 ALOGI("Test result = %d", status);
463 return status;
Wyatt Riley94c7a042017-06-15 17:16:24 -0700464}