• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
19 #include <gtest/gtest.h>
20 #include <hidl/GtestPrinter.h>
21 #include <hidl/ServiceManagement.h>
22 #include <log/log.h>
23 
24 #include <chrono>
25 #include <cmath>
26 #include <condition_variable>
27 #include <mutex>
28 
29 #include <cutils/properties.h>
30 
31 using android::hardware::Return;
32 using android::hardware::Void;
33 
34 using android::hardware::gnss::V1_0::GnssLocation;
35 using android::hardware::gnss::V1_0::GnssLocationFlags;
36 using android::hardware::gnss::V1_0::IGnss;
37 using android::hardware::gnss::V1_0::IGnssCallback;
38 using android::hardware::gnss::V1_0::IGnssDebug;
39 using android::hardware::gnss::V1_0::IGnssMeasurement;
40 using android::sp;
41 
IsAutomotiveDevice()42 static bool IsAutomotiveDevice() {
43     char buffer[PROPERTY_VALUE_MAX] = {0};
44     property_get("ro.hardware.type", buffer, "");
45     return strncmp(buffer, "automotive", PROPERTY_VALUE_MAX) == 0;
46 }
47 
48 #define TIMEOUT_SEC 2  // for basic commands/responses
49 
50 // for command line argument on how strictly to run the test
51 bool sAgpsIsPresent = false;  // if SUPL or XTRA assistance available
52 bool sSignalIsWeak = false;   // if GNSS signals are weak (e.g. light indoor)
53 
54 // The main test class for GNSS HAL.
55 class GnssHalTest : public testing::TestWithParam<std::string> {
56  public:
SetUp()57   virtual void SetUp() override {
58     // Clean between tests
59     capabilities_called_count_ = 0;
60     location_called_count_ = 0;
61     info_called_count_ = 0;
62     notify_count_ = 0;
63 
64     gnss_hal_ = IGnss::getService(GetParam());
65     ASSERT_NE(gnss_hal_, nullptr);
66 
67     gnss_cb_ = new GnssCallback(*this);
68     ASSERT_NE(gnss_cb_, nullptr);
69 
70     auto result = gnss_hal_->setCallback(gnss_cb_);
71     if (!result.isOk()) {
72       ALOGE("result of failed setCallback %s", result.description().c_str());
73     }
74 
75     ASSERT_TRUE(result.isOk());
76     ASSERT_TRUE(result);
77 
78     /*
79      * At least one callback should trigger - it may be capabilites, or
80      * system info first, so wait again if capabilities not received.
81      */
82     EXPECT_EQ(std::cv_status::no_timeout, wait(TIMEOUT_SEC));
83     if (capabilities_called_count_ == 0) {
84       EXPECT_EQ(std::cv_status::no_timeout, wait(TIMEOUT_SEC));
85     }
86 
87     /*
88      * Generally should be 1 capabilites callback -
89      * or possibly 2 in some recovery cases (default cached & refreshed)
90      */
91     EXPECT_GE(capabilities_called_count_, 1);
92     EXPECT_LE(capabilities_called_count_, 2);
93 
94     /*
95      * Clear notify/waiting counter, allowing up till the timeout after
96      * the last reply for final startup messages to arrive (esp. system
97      * info.)
98      */
99     while (wait(TIMEOUT_SEC) == std::cv_status::no_timeout) {
100     }
101   }
102 
TearDown()103   virtual void TearDown() override {
104     if (gnss_hal_ != nullptr) {
105       gnss_hal_->cleanup();
106     }
107     if (notify_count_ > 0) {
108         ALOGW("%d unprocessed callbacks discarded", notify_count_);
109     }
110   }
111 
112   /* Used as a mechanism to inform the test that a callback has occurred */
notify()113   inline void notify() {
114     std::unique_lock<std::mutex> lock(mtx_);
115     notify_count_++;
116     cv_.notify_one();
117   }
118 
119   /* Test code calls this function to wait for a callback */
wait(int timeoutSeconds)120   inline std::cv_status wait(int timeoutSeconds) {
121     std::unique_lock<std::mutex> lock(mtx_);
122 
123     std::cv_status status = std::cv_status::no_timeout;
124     auto now = std::chrono::system_clock::now();
125     while (notify_count_ == 0) {
126         status = cv_.wait_until(lock, now + std::chrono::seconds(timeoutSeconds));
127         if (status == std::cv_status::timeout) return status;
128     }
129     notify_count_--;
130     return status;
131   }
132 
133   /*
134    * StartAndGetSingleLocation:
135    * Helper function to get one Location and check fields
136    *
137    * returns  true if a location was successfully generated
138    */
StartAndGetSingleLocation(bool checkAccuracies)139   bool StartAndGetSingleLocation(bool checkAccuracies) {
140       auto result = gnss_hal_->start();
141 
142       EXPECT_TRUE(result.isOk());
143       EXPECT_TRUE(result);
144 
145       /*
146        * GPS signals initially optional for this test, so don't expect fast fix,
147        * or no timeout, unless signal is present
148        */
149       int firstGnssLocationTimeoutSeconds = sAgpsIsPresent ? 15 : 45;
150       if (sSignalIsWeak) {
151           // allow more time for weak signals
152           firstGnssLocationTimeoutSeconds += 30;
153       }
154 
155       wait(firstGnssLocationTimeoutSeconds);
156       if (sAgpsIsPresent) {
157           EXPECT_EQ(location_called_count_, 1);
158       }
159       if (location_called_count_ > 0) {
160           // don't require speed on first fix
161           CheckLocation(last_location_, checkAccuracies, false);
162           return true;
163       }
164       return false;
165   }
166 
167   /*
168    * StopAndClearLocations:
169    * Helper function to stop locations
170    *
171    * returns  true if a location was successfully generated
172    */
StopAndClearLocations()173   void StopAndClearLocations() {
174       auto result = gnss_hal_->stop();
175 
176       EXPECT_TRUE(result.isOk());
177       EXPECT_TRUE(result);
178 
179       /*
180        * Clear notify/waiting counter, allowing up till the timeout after
181        * the last reply for final startup messages to arrive (esp. system
182        * info.)
183        */
184       while (wait(TIMEOUT_SEC) == std::cv_status::no_timeout) {
185       }
186   }
187 
188   /*
189    * CheckLocation:
190    *   Helper function to vet Location fields
191    */
CheckLocation(GnssLocation & location,bool checkAccuracies,bool checkSpeed)192   void CheckLocation(GnssLocation& location, bool checkAccuracies, bool checkSpeed) {
193       EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_LAT_LONG);
194       EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_ALTITUDE);
195       if (checkSpeed) {
196           EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED);
197       }
198       EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_HORIZONTAL_ACCURACY);
199       // New uncertainties available in O must be provided,
200       // at least when paired with modern hardware (2017+)
201       if (checkAccuracies) {
202           EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_VERTICAL_ACCURACY);
203           if (checkSpeed) {
204               EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED_ACCURACY);
205               if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING) {
206                   EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING_ACCURACY);
207               }
208           }
209       }
210       EXPECT_GE(location.latitudeDegrees, -90.0);
211       EXPECT_LE(location.latitudeDegrees, 90.0);
212       EXPECT_GE(location.longitudeDegrees, -180.0);
213       EXPECT_LE(location.longitudeDegrees, 180.0);
214       EXPECT_GE(location.altitudeMeters, -1000.0);
215       EXPECT_LE(location.altitudeMeters, 30000.0);
216       if (checkSpeed) {
217           EXPECT_GE(location.speedMetersPerSec, 0.0);
218           EXPECT_LE(location.speedMetersPerSec, 5.0);  // VTS tests are stationary.
219 
220           // Non-zero speeds must be reported with an associated bearing
221           if (location.speedMetersPerSec > 0.0) {
222               EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING);
223           }
224       }
225 
226       /*
227        * Tolerating some especially high values for accuracy estimate, in case of
228        * first fix with especially poor geometry (happens occasionally)
229        */
230       EXPECT_GT(location.horizontalAccuracyMeters, 0.0);
231       EXPECT_LE(location.horizontalAccuracyMeters, 250.0);
232 
233       /*
234        * Some devices may define bearing as -180 to +180, others as 0 to 360.
235        * Both are okay & understandable.
236        */
237       if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING) {
238           EXPECT_GE(location.bearingDegrees, -180.0);
239           EXPECT_LE(location.bearingDegrees, 360.0);
240       }
241       if (location.gnssLocationFlags & GnssLocationFlags::HAS_VERTICAL_ACCURACY) {
242           EXPECT_GT(location.verticalAccuracyMeters, 0.0);
243           EXPECT_LE(location.verticalAccuracyMeters, 500.0);
244       }
245       if (location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED_ACCURACY) {
246           EXPECT_GT(location.speedAccuracyMetersPerSecond, 0.0);
247           EXPECT_LE(location.speedAccuracyMetersPerSecond, 50.0);
248       }
249       if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING_ACCURACY) {
250           EXPECT_GT(location.bearingAccuracyDegrees, 0.0);
251           EXPECT_LE(location.bearingAccuracyDegrees, 360.0);
252       }
253 
254       // Check timestamp > 1.48e12 (47 years in msec - 1970->2017+)
255       EXPECT_GT(location.timestamp, 1.48e12);
256   }
257 
258   /* Callback class for data & Event. */
259   class GnssCallback : public IGnssCallback {
260    public:
261     GnssHalTest& parent_;
262 
GnssCallback(GnssHalTest & parent)263     GnssCallback(GnssHalTest& parent) : parent_(parent){};
264 
265     virtual ~GnssCallback() = default;
266 
267     // Dummy callback handlers
gnssStatusCb(const IGnssCallback::GnssStatusValue)268     Return<void> gnssStatusCb(
269         const IGnssCallback::GnssStatusValue /* status */) override {
270       return Void();
271     }
gnssSvStatusCb(const IGnssCallback::GnssSvStatus &)272     Return<void> gnssSvStatusCb(
273         const IGnssCallback::GnssSvStatus& /* svStatus */) override {
274       return Void();
275     }
gnssNmeaCb(int64_t,const android::hardware::hidl_string &)276     Return<void> gnssNmeaCb(
277         int64_t /* timestamp */,
278         const android::hardware::hidl_string& /* nmea */) override {
279       return Void();
280     }
gnssAcquireWakelockCb()281     Return<void> gnssAcquireWakelockCb() override { return Void(); }
gnssReleaseWakelockCb()282     Return<void> gnssReleaseWakelockCb() override { return Void(); }
gnssRequestTimeCb()283     Return<void> gnssRequestTimeCb() override { return Void(); }
284 
285     // Actual (test) callback handlers
gnssLocationCb(const GnssLocation & location)286     Return<void> gnssLocationCb(const GnssLocation& location) override {
287       ALOGI("Location received");
288       parent_.location_called_count_++;
289       parent_.last_location_ = location;
290       parent_.notify();
291       return Void();
292     }
293 
gnssSetCapabilitesCb(uint32_t capabilities)294     Return<void> gnssSetCapabilitesCb(uint32_t capabilities) override {
295       ALOGI("Capabilities received %d", capabilities);
296       parent_.capabilities_called_count_++;
297       parent_.last_capabilities_ = capabilities;
298       parent_.notify();
299       return Void();
300     }
301 
gnssSetSystemInfoCb(const IGnssCallback::GnssSystemInfo & info)302     Return<void> gnssSetSystemInfoCb(
303         const IGnssCallback::GnssSystemInfo& info) override {
304       ALOGI("Info received, year %d", info.yearOfHw);
305       parent_.info_called_count_++;
306       parent_.last_info_ = info;
307       parent_.notify();
308       return Void();
309     }
310   };
311 
312   sp<IGnss> gnss_hal_;         // GNSS HAL to call into
313   sp<IGnssCallback> gnss_cb_;  // Primary callback interface
314 
315   /* Count of calls to set the following items, and the latest item (used by
316    * test.)
317    */
318   int capabilities_called_count_;
319   uint32_t last_capabilities_;
320 
321   int location_called_count_;
322   GnssLocation last_location_;
323 
324   int info_called_count_;
325   IGnssCallback::GnssSystemInfo last_info_;
326 
327  private:
328   std::mutex mtx_;
329   std::condition_variable cv_;
330   int notify_count_;
331 };
332 
333 /*
334  * SetCallbackCapabilitiesCleanup:
335  * Sets up the callback, awaits the capabilities, and calls cleanup
336  *
337  * Since this is just the basic operation of SetUp() and TearDown(),
338  * the function definition is intentionally empty
339  */
TEST_P(GnssHalTest,SetCallbackCapabilitiesCleanup)340 TEST_P(GnssHalTest, SetCallbackCapabilitiesCleanup) {}
341 
342 /*
343  * GetLocation:
344  * Turns on location, waits 45 second for at least 5 locations,
345  * and checks them for reasonable validity.
346  */
TEST_P(GnssHalTest,GetLocation)347 TEST_P(GnssHalTest, GetLocation) {
348 #define MIN_INTERVAL_MSEC 500
349 #define PREFERRED_ACCURACY 0   // Ideally perfect (matches GnssLocationProvider)
350 #define PREFERRED_TIME_MSEC 0  // Ideally immediate
351 
352 #define LOCATION_TIMEOUT_SUBSEQUENT_SEC 3
353 #define LOCATIONS_TO_CHECK 5
354 
355   bool checkMoreAccuracies =
356       (info_called_count_ > 0 && last_info_.yearOfHw >= 2017);
357 
358   auto result = gnss_hal_->setPositionMode(
359       IGnss::GnssPositionMode::MS_BASED,
360       IGnss::GnssPositionRecurrence::RECURRENCE_PERIODIC, MIN_INTERVAL_MSEC,
361       PREFERRED_ACCURACY, PREFERRED_TIME_MSEC);
362 
363   ASSERT_TRUE(result.isOk());
364   EXPECT_TRUE(result);
365 
366   /*
367    * GPS signals initially optional for this test, so don't expect no timeout
368    * yet
369    */
370   bool gotLocation = StartAndGetSingleLocation(checkMoreAccuracies);
371 
372   if (gotLocation) {
373     for (int i = 1; i < LOCATIONS_TO_CHECK; i++) {
374         EXPECT_EQ(std::cv_status::no_timeout, wait(LOCATION_TIMEOUT_SUBSEQUENT_SEC));
375         EXPECT_EQ(location_called_count_, i + 1);
376         CheckLocation(last_location_, checkMoreAccuracies, true);
377     }
378   }
379 
380   StopAndClearLocations();
381 }
382 
383 /*
384  * InjectDelete:
385  * Ensures that calls to inject and/or delete information state are handled.
386  */
TEST_P(GnssHalTest,InjectDelete)387 TEST_P(GnssHalTest, InjectDelete) {
388   // confidently, well north of Alaska
389   auto result = gnss_hal_->injectLocation(80.0, -170.0, 1000.0);
390 
391   ASSERT_TRUE(result.isOk());
392   EXPECT_TRUE(result);
393 
394   // fake time, but generally reasonable values (time in Aug. 2018)
395   result = gnss_hal_->injectTime(1534567890123L, 123456L, 10000L);
396 
397   ASSERT_TRUE(result.isOk());
398   EXPECT_TRUE(result);
399 
400   auto resultVoid = gnss_hal_->deleteAidingData(IGnss::GnssAidingData::DELETE_POSITION);
401 
402   ASSERT_TRUE(resultVoid.isOk());
403 
404   resultVoid = gnss_hal_->deleteAidingData(IGnss::GnssAidingData::DELETE_TIME);
405 
406   ASSERT_TRUE(resultVoid.isOk());
407 
408   // Ensure we can get a good location after a bad injection has been deleted
409   StartAndGetSingleLocation(false);
410 
411   StopAndClearLocations();
412 }
413 
414 /*
415  * InjectSeedLocation:
416  * Injects a seed location and ensures the injected seed location is not fused in the resulting
417  * GNSS location.
418  */
TEST_P(GnssHalTest,InjectSeedLocation)419 TEST_P(GnssHalTest, InjectSeedLocation) {
420     // An arbitrary position in North Pacific Ocean (where no VTS labs will ever likely be located).
421     const double seedLatDegrees = 32.312894;
422     const double seedLngDegrees = -172.954117;
423     const float seedAccuracyMeters = 150.0;
424 
425     auto result = gnss_hal_->injectLocation(seedLatDegrees, seedLngDegrees, seedAccuracyMeters);
426     ASSERT_TRUE(result.isOk());
427     EXPECT_TRUE(result);
428 
429     StartAndGetSingleLocation(false);
430 
431     // Ensure we don't get a location anywhere within 111km (1 degree of lat or lng) of the seed
432     // location.
433     EXPECT_TRUE(std::abs(last_location_.latitudeDegrees - seedLatDegrees) > 1.0 ||
434                 std::abs(last_location_.longitudeDegrees - seedLngDegrees) > 1.0);
435 
436     StopAndClearLocations();
437 
438     auto resultVoid = gnss_hal_->deleteAidingData(IGnss::GnssAidingData::DELETE_POSITION);
439     ASSERT_TRUE(resultVoid.isOk());
440 }
441 
442 /*
443  * GetAllExtentions:
444  * Tries getting all optional extensions, and ensures a valid return
445  *   null or actual extension, no crash.
446  * Confirms year-based required extensions (Measurement & Debug) are present
447  */
TEST_P(GnssHalTest,GetAllExtensions)448 TEST_P(GnssHalTest, GetAllExtensions) {
449   // Basic call-is-handled checks
450   auto gnssXtra = gnss_hal_->getExtensionXtra();
451   ASSERT_TRUE(gnssXtra.isOk());
452 
453   auto gnssRil = gnss_hal_->getExtensionAGnssRil();
454   ASSERT_TRUE(gnssRil.isOk());
455 
456   auto gnssAgnss = gnss_hal_->getExtensionAGnss();
457   ASSERT_TRUE(gnssAgnss.isOk());
458 
459   auto gnssNi = gnss_hal_->getExtensionGnssNi();
460   ASSERT_TRUE(gnssNi.isOk());
461 
462   auto gnssNavigationMessage = gnss_hal_->getExtensionGnssNavigationMessage();
463   ASSERT_TRUE(gnssNavigationMessage.isOk());
464 
465   auto gnssConfiguration = gnss_hal_->getExtensionGnssConfiguration();
466   ASSERT_TRUE(gnssConfiguration.isOk());
467 
468   auto gnssGeofencing = gnss_hal_->getExtensionGnssGeofencing();
469   ASSERT_TRUE(gnssGeofencing.isOk());
470 
471   auto gnssBatching = gnss_hal_->getExtensionGnssBatching();
472   ASSERT_TRUE(gnssBatching.isOk());
473 
474   // Verifying, in some cases, that these return actual extensions
475   auto gnssMeasurement = gnss_hal_->getExtensionGnssMeasurement();
476   ASSERT_TRUE(gnssMeasurement.isOk());
477   if (last_capabilities_ & IGnssCallback::Capabilities::MEASUREMENTS) {
478     sp<IGnssMeasurement> iGnssMeas = gnssMeasurement;
479     EXPECT_NE(iGnssMeas, nullptr);
480   }
481 
482   auto gnssDebug = gnss_hal_->getExtensionGnssDebug();
483   ASSERT_TRUE(gnssDebug.isOk());
484   if (!IsAutomotiveDevice() && info_called_count_ > 0 && last_info_.yearOfHw >= 2017) {
485       sp<IGnssDebug> iGnssDebug = gnssDebug;
486       EXPECT_NE(iGnssDebug, nullptr);
487   }
488 }
489 
490 /*
491  * MeasurementCapabilities:
492  * Verifies that modern hardware supports measurement capabilities.
493  */
TEST_P(GnssHalTest,MeasurementCapabilites)494 TEST_P(GnssHalTest, MeasurementCapabilites) {
495   if (info_called_count_ > 0 && last_info_.yearOfHw >= 2016) {
496     EXPECT_TRUE(last_capabilities_ & IGnssCallback::Capabilities::MEASUREMENTS);
497   }
498 }
499 
500 /*
501  * SchedulingCapabilities:
502  * Verifies that 2018+ hardware supports Scheduling capabilities.
503  */
TEST_P(GnssHalTest,SchedulingCapabilities)504 TEST_P(GnssHalTest, SchedulingCapabilities) {
505     if (info_called_count_ > 0 && last_info_.yearOfHw >= 2018) {
506         EXPECT_TRUE(last_capabilities_ & IGnssCallback::Capabilities::SCHEDULING);
507     }
508 }
509 
510 INSTANTIATE_TEST_SUITE_P(
511         PerInstance, GnssHalTest,
512         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IGnss::descriptor)),
513         android::hardware::PrintInstanceNameToString);
514 
main(int argc,char ** argv)515 int main(int argc, char** argv) {
516   ::testing::InitGoogleTest(&argc, argv);
517   /*
518    * These arguments not used by automated VTS testing.
519    * Only for use in manual testing, when wanting to run
520    * stronger tests that require the presence of GPS signal.
521    */
522   for (int i = 1; i < argc; i++) {
523     if (strcmp(argv[i], "-agps") == 0) {
524         sAgpsIsPresent = true;
525     } else if (strcmp(argv[i], "-weak") == 0) {
526         sSignalIsWeak = true;
527     }
528   }
529 
530   return RUN_ALL_TESTS();
531 }