blob: 91e75fe1d7bda3a34fd9a8ffd285f8750ed7a93c [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 Riley6ec696b2017-06-19 14:42:34 -070052 notify_count_ = 0;
Wyatt Riley6c26ed72017-02-21 17:21:53 -080053
Yuexi Maed2bb4e2017-03-10 00:44:45 -080054 gnss_hal_ = ::testing::VtsHalHidlTargetTestBase::getService<IGnss>();
Wyatt Riley6c26ed72017-02-21 17:21:53 -080055 ASSERT_NE(gnss_hal_, nullptr);
56
57 gnss_cb_ = new GnssCallback(*this);
58 ASSERT_NE(gnss_cb_, nullptr);
59
60 auto result = gnss_hal_->setCallback(gnss_cb_);
61 if (!result.isOk()) {
Wyatt Riley917640b2017-03-16 16:25:55 -070062 ALOGE("result of failed setCallback %s", result.description().c_str());
Wyatt Riley6c26ed72017-02-21 17:21:53 -080063 }
64
65 ASSERT_TRUE(result.isOk());
66 ASSERT_TRUE(result);
67
Wyatt Riley917640b2017-03-16 16:25:55 -070068 /*
69 * At least one callback should trigger - it may be capabilites, or
70 * system info first, so wait again if capabilities not received.
Wyatt Riley6c26ed72017-02-21 17:21:53 -080071 */
Wyatt Riley917640b2017-03-16 16:25:55 -070072 EXPECT_EQ(std::cv_status::no_timeout, wait(TIMEOUT_SEC));
73 if (capabilities_called_count_ == 0) {
74 EXPECT_EQ(std::cv_status::no_timeout, wait(TIMEOUT_SEC));
75 }
76
77 /*
78 * Generally should be 1 capabilites callback -
79 * or possibly 2 in some recovery cases (default cached & refreshed)
80 */
81 EXPECT_GE(capabilities_called_count_, 1);
82 EXPECT_LE(capabilities_called_count_, 2);
83
84 /*
85 * Clear notify/waiting counter, allowing up till the timeout after
86 * the last reply for final startup messages to arrive (esp. system
87 * info.)
88 */
89 while (wait(TIMEOUT_SEC) == std::cv_status::no_timeout) {
90 }
Wyatt Riley6c26ed72017-02-21 17:21:53 -080091 }
92
93 virtual void TearDown() override {
94 if (gnss_hal_ != nullptr) {
95 gnss_hal_->cleanup();
96 }
Wyatt Riley6ec696b2017-06-19 14:42:34 -070097 if (notify_count_ > 0) {
98 ALOGW("%d unprocessed callbacks discarded", notify_count_);
99 }
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800100 }
101
102 /* Used as a mechanism to inform the test that a callback has occurred */
103 inline void notify() {
104 std::unique_lock<std::mutex> lock(mtx_);
Wyatt Riley6ec696b2017-06-19 14:42:34 -0700105 notify_count_++;
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800106 cv_.notify_one();
107 }
108
109 /* Test code calls this function to wait for a callback */
110 inline std::cv_status wait(int timeoutSeconds) {
111 std::unique_lock<std::mutex> lock(mtx_);
112
113 std::cv_status status = std::cv_status::no_timeout;
114 auto now = std::chrono::system_clock::now();
Wyatt Riley6ec696b2017-06-19 14:42:34 -0700115 while (notify_count_ == 0) {
116 status = cv_.wait_until(lock, now + std::chrono::seconds(timeoutSeconds));
117 if (status == std::cv_status::timeout) return status;
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800118 }
Wyatt Riley6ec696b2017-06-19 14:42:34 -0700119 notify_count_--;
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800120 return status;
121 }
122
Wyatt Riley94c7a042017-06-15 17:16:24 -0700123 /*
124 * StartAndGetSingleLocation:
125 * Helper function to get one Location and check fields
126 *
127 * returns true if a location was successfully generated
128 */
129 bool StartAndGetSingleLocation(bool checkAccuracies) {
130 auto result = gnss_hal_->start();
131
132 EXPECT_TRUE(result.isOk());
133 EXPECT_TRUE(result);
134
135 /*
136 * GPS signals initially optional for this test, so don't expect fast fix,
137 * or no timeout, unless signal is present
138 */
139 int firstGnssLocationTimeoutSeconds = sAgpsIsPresent ? 15 : 45;
140 if (sSignalIsWeak) {
141 // allow more time for weak signals
142 firstGnssLocationTimeoutSeconds += 30;
143 }
144
145 wait(firstGnssLocationTimeoutSeconds);
146 if (sAgpsIsPresent) {
147 EXPECT_EQ(location_called_count_, 1);
148 }
149 if (location_called_count_ > 0) {
150 CheckLocation(last_location_, checkAccuracies);
151 return true;
152 }
153 return false;
154 }
155
156 /*
157 * StopAndClearLocations:
158 * Helper function to stop locations
159 *
160 * returns true if a location was successfully generated
161 */
162 void StopAndClearLocations() {
163 auto result = gnss_hal_->stop();
164
165 EXPECT_TRUE(result.isOk());
166 EXPECT_TRUE(result);
167
168 /*
169 * Clear notify/waiting counter, allowing up till the timeout after
170 * the last reply for final startup messages to arrive (esp. system
171 * info.)
172 */
173 while (wait(TIMEOUT_SEC) == std::cv_status::no_timeout) {
174 }
175 }
176
177 /*
178 * CheckLocation:
179 * Helper function to vet Location fields
180 */
181 void CheckLocation(GnssLocation& location, bool checkAccuracies) {
182 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_LAT_LONG);
183 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_ALTITUDE);
184 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED);
185 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_HORIZONTAL_ACCURACY);
186 // New uncertainties available in O must be provided,
187 // at least when paired with modern hardware (2017+)
188 if (checkAccuracies) {
189 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_VERTICAL_ACCURACY);
190 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED_ACCURACY);
191 if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING) {
192 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING_ACCURACY);
193 }
194 }
195 EXPECT_GE(location.latitudeDegrees, -90.0);
196 EXPECT_LE(location.latitudeDegrees, 90.0);
197 EXPECT_GE(location.longitudeDegrees, -180.0);
198 EXPECT_LE(location.longitudeDegrees, 180.0);
199 EXPECT_GE(location.altitudeMeters, -1000.0);
200 EXPECT_LE(location.altitudeMeters, 30000.0);
201 EXPECT_GE(location.speedMetersPerSec, 0.0);
202 EXPECT_LE(location.speedMetersPerSec, 5.0); // VTS tests are stationary.
203
204 // Non-zero speeds must be reported with an associated bearing
205 if (location.speedMetersPerSec > 0.0) {
206 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING);
207 }
208
209 /*
210 * Tolerating some especially high values for accuracy estimate, in case of
211 * first fix with especially poor geometry (happens occasionally)
212 */
213 EXPECT_GT(location.horizontalAccuracyMeters, 0.0);
214 EXPECT_LE(location.horizontalAccuracyMeters, 250.0);
215
216 /*
217 * Some devices may define bearing as -180 to +180, others as 0 to 360.
218 * Both are okay & understandable.
219 */
220 if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING) {
221 EXPECT_GE(location.bearingDegrees, -180.0);
222 EXPECT_LE(location.bearingDegrees, 360.0);
223 }
224 if (location.gnssLocationFlags & GnssLocationFlags::HAS_VERTICAL_ACCURACY) {
225 EXPECT_GT(location.verticalAccuracyMeters, 0.0);
226 EXPECT_LE(location.verticalAccuracyMeters, 500.0);
227 }
228 if (location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED_ACCURACY) {
229 EXPECT_GT(location.speedAccuracyMetersPerSecond, 0.0);
230 EXPECT_LE(location.speedAccuracyMetersPerSecond, 50.0);
231 }
232 if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING_ACCURACY) {
233 EXPECT_GT(location.bearingAccuracyDegrees, 0.0);
234 EXPECT_LE(location.bearingAccuracyDegrees, 360.0);
235 }
236
237 // Check timestamp > 1.48e12 (47 years in msec - 1970->2017+)
238 EXPECT_GT(location.timestamp, 1.48e12);
239 }
240
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800241 /* Callback class for data & Event. */
242 class GnssCallback : public IGnssCallback {
Wyatt Riley917640b2017-03-16 16:25:55 -0700243 public:
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800244 GnssHalTest& parent_;
245
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800246 GnssCallback(GnssHalTest& parent) : parent_(parent){};
247
248 virtual ~GnssCallback() = default;
249
250 // Dummy callback handlers
251 Return<void> gnssStatusCb(
Steven Moreland4b523952017-03-08 19:20:30 -0800252 const IGnssCallback::GnssStatusValue /* status */) override {
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800253 return Void();
254 }
255 Return<void> gnssSvStatusCb(
Steven Moreland4b523952017-03-08 19:20:30 -0800256 const IGnssCallback::GnssSvStatus& /* svStatus */) override {
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800257 return Void();
258 }
259 Return<void> gnssNmeaCb(
Steven Moreland4b523952017-03-08 19:20:30 -0800260 int64_t /* timestamp */,
261 const android::hardware::hidl_string& /* nmea */) override {
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800262 return Void();
263 }
264 Return<void> gnssAcquireWakelockCb() override { return Void(); }
265 Return<void> gnssReleaseWakelockCb() override { return Void(); }
266 Return<void> gnssRequestTimeCb() override { return Void(); }
267
268 // Actual (test) callback handlers
269 Return<void> gnssLocationCb(const GnssLocation& location) override {
270 ALOGI("Location received");
271 parent_.location_called_count_++;
272 parent_.last_location_ = location;
273 parent_.notify();
274 return Void();
275 }
276
277 Return<void> gnssSetCapabilitesCb(uint32_t capabilities) override {
278 ALOGI("Capabilities received %d", capabilities);
279 parent_.capabilities_called_count_++;
280 parent_.last_capabilities_ = capabilities;
281 parent_.notify();
282 return Void();
283 }
284
285 Return<void> gnssSetSystemInfoCb(
286 const IGnssCallback::GnssSystemInfo& info) override {
287 ALOGI("Info received, year %d", info.yearOfHw);
288 parent_.info_called_count_++;
289 parent_.last_info_ = info;
290 parent_.notify();
291 return Void();
292 }
293 };
294
295 sp<IGnss> gnss_hal_; // GNSS HAL to call into
296 sp<IGnssCallback> gnss_cb_; // Primary callback interface
297
298 /* Count of calls to set the following items, and the latest item (used by
299 * test.)
300 */
301 int capabilities_called_count_;
302 uint32_t last_capabilities_;
303
304 int location_called_count_;
305 GnssLocation last_location_;
306
307 int info_called_count_;
308 IGnssCallback::GnssSystemInfo last_info_;
309
310 private:
311 std::mutex mtx_;
312 std::condition_variable cv_;
Wyatt Riley6ec696b2017-06-19 14:42:34 -0700313 int notify_count_;
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800314};
315
316/*
317 * SetCallbackCapabilitiesCleanup:
318 * Sets up the callback, awaits the capabilities, and calls cleanup
319 *
320 * Since this is just the basic operation of SetUp() and TearDown(),
Wyatt Riley917640b2017-03-16 16:25:55 -0700321 * the function definition is intentionally empty
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800322 */
323TEST_F(GnssHalTest, SetCallbackCapabilitiesCleanup) {}
324
Wyatt Riley917640b2017-03-16 16:25:55 -0700325/*
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800326 * GetLocation:
327 * Turns on location, waits 45 second for at least 5 locations,
328 * and checks them for reasonable validity.
329 */
330TEST_F(GnssHalTest, GetLocation) {
331#define MIN_INTERVAL_MSEC 500
332#define PREFERRED_ACCURACY 0 // Ideally perfect (matches GnssLocationProvider)
333#define PREFERRED_TIME_MSEC 0 // Ideally immediate
334
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800335#define LOCATION_TIMEOUT_SUBSEQUENT_SEC 3
336#define LOCATIONS_TO_CHECK 5
337
Wyatt Riley917640b2017-03-16 16:25:55 -0700338 bool checkMoreAccuracies =
339 (info_called_count_ > 0 && last_info_.yearOfHw >= 2017);
340
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800341 auto result = gnss_hal_->setPositionMode(
342 IGnss::GnssPositionMode::MS_BASED,
343 IGnss::GnssPositionRecurrence::RECURRENCE_PERIODIC, MIN_INTERVAL_MSEC,
344 PREFERRED_ACCURACY, PREFERRED_TIME_MSEC);
345
346 ASSERT_TRUE(result.isOk());
Wyatt Rileyc1399592017-03-22 14:08:26 -0700347 EXPECT_TRUE(result);
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800348
Wyatt Rileyc1399592017-03-22 14:08:26 -0700349 /*
350 * GPS signals initially optional for this test, so don't expect no timeout
351 * yet
352 */
Wyatt Riley94c7a042017-06-15 17:16:24 -0700353 bool gotLocation = StartAndGetSingleLocation(checkMoreAccuracies);
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800354
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -0700355 if (gotLocation) {
356 for (int i = 1; i < LOCATIONS_TO_CHECK; i++) {
Wyatt Riley94c7a042017-06-15 17:16:24 -0700357 EXPECT_EQ(std::cv_status::no_timeout, wait(LOCATION_TIMEOUT_SUBSEQUENT_SEC));
358 EXPECT_EQ(location_called_count_, i + 1);
359 CheckLocation(last_location_, checkMoreAccuracies);
Wyatt Riley917640b2017-03-16 16:25:55 -0700360 }
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800361 }
362
Wyatt Riley94c7a042017-06-15 17:16:24 -0700363 StopAndClearLocations();
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800364}
365
Wyatt Rileyc1399592017-03-22 14:08:26 -0700366/*
367 * InjectDelete:
368 * Ensures that calls to inject and/or delete information state are handled.
Wyatt Rileyc1399592017-03-22 14:08:26 -0700369 */
370TEST_F(GnssHalTest, InjectDelete) {
371 // confidently, well north of Alaska
372 auto result = gnss_hal_->injectLocation(80.0, -170.0, 1000.0);
373
Wyatt Rileyc1399592017-03-22 14:08:26 -0700374 ASSERT_TRUE(result.isOk());
375 EXPECT_TRUE(result);
376
377 // fake time, but generally reasonable values (time in Aug. 2018)
378 result = gnss_hal_->injectTime(1534567890123L, 123456L, 10000L);
379
380 ASSERT_TRUE(result.isOk());
381 EXPECT_TRUE(result);
382
383 auto resultVoid = gnss_hal_->deleteAidingData(IGnss::GnssAidingData::DELETE_ALL);
384
385 ASSERT_TRUE(resultVoid.isOk());
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -0700386
387 // Ensure we can get a good location after a bad injection has been deleted
Wyatt Riley94c7a042017-06-15 17:16:24 -0700388 StartAndGetSingleLocation(false);
389
390 StopAndClearLocations();
Wyatt Rileyc1399592017-03-22 14:08:26 -0700391}
392
393/*
394 * GetAllExtentions:
395 * Tries getting all optional extensions, and ensures a valid return
396 * null or actual extension, no crash.
397 * Confirms year-based required extensions (Measurement & Debug) are present
398 */
399TEST_F(GnssHalTest, GetAllExtensions) {
400 // Basic call-is-handled checks
401 auto gnssXtra = gnss_hal_->getExtensionXtra();
402 ASSERT_TRUE(gnssXtra.isOk());
403
404 auto gnssRil = gnss_hal_->getExtensionAGnssRil();
405 ASSERT_TRUE(gnssRil.isOk());
406
407 auto gnssAgnss = gnss_hal_->getExtensionAGnss();
408 ASSERT_TRUE(gnssAgnss.isOk());
409
410 auto gnssNi = gnss_hal_->getExtensionGnssNi();
411 ASSERT_TRUE(gnssNi.isOk());
412
413 auto gnssNavigationMessage = gnss_hal_->getExtensionGnssNavigationMessage();
414 ASSERT_TRUE(gnssNavigationMessage.isOk());
415
416 auto gnssConfiguration = gnss_hal_->getExtensionGnssConfiguration();
417 ASSERT_TRUE(gnssConfiguration.isOk());
418
419 auto gnssGeofencing = gnss_hal_->getExtensionGnssGeofencing();
420 ASSERT_TRUE(gnssGeofencing.isOk());
421
422 auto gnssBatching = gnss_hal_->getExtensionGnssBatching();
423 ASSERT_TRUE(gnssBatching.isOk());
424
425 // Verifying, in some cases, that these return actual extensions
426 auto gnssMeasurement = gnss_hal_->getExtensionGnssMeasurement();
427 ASSERT_TRUE(gnssMeasurement.isOk());
428 if (last_capabilities_ & IGnssCallback::Capabilities::MEASUREMENTS) {
429 sp<IGnssMeasurement> iGnssMeas = gnssMeasurement;
430 EXPECT_NE(iGnssMeas, nullptr);
431 }
432
433 auto gnssDebug = gnss_hal_->getExtensionGnssDebug();
434 ASSERT_TRUE(gnssDebug.isOk());
435 if (info_called_count_ > 0 && last_info_.yearOfHw >= 2017) {
436 sp<IGnssDebug> iGnssDebug = gnssDebug;
437 EXPECT_NE(iGnssDebug, nullptr);
438 }
439}
440
441/*
442 * MeasurementCapabilities:
443 * Verifies that modern hardware supports measurement capabilities.
444 */
445TEST_F(GnssHalTest, MeasurementCapabilites) {
446 if (info_called_count_ > 0 && last_info_.yearOfHw >= 2016) {
447 EXPECT_TRUE(last_capabilities_ & IGnssCallback::Capabilities::MEASUREMENTS);
448 }
449}
450
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800451int main(int argc, char** argv) {
452 ::testing::InitGoogleTest(&argc, argv);
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -0700453 /*
454 * These arguments not used by automated VTS testing.
455 * Only for use in manual testing, when wanting to run
456 * stronger tests that require the presence of GPS signal.
457 */
458 for (int i = 1; i < argc; i++) {
Wyatt Riley94c7a042017-06-15 17:16:24 -0700459 if (strcmp(argv[i], "-agps") == 0) {
460 sAgpsIsPresent = true;
461 } else if (strcmp(argv[i], "-weak") == 0) {
462 sSignalIsWeak = true;
Wyatt Riley0ee0cfb2017-04-04 12:34:53 -0700463 }
464 }
Wyatt Riley6c26ed72017-02-21 17:21:53 -0800465 int status = RUN_ALL_TESTS();
466 ALOGI("Test result = %d", status);
467 return status;
Wyatt Riley94c7a042017-06-15 17:16:24 -0700468}