summaryrefslogtreecommitdiffstats
blob: 8f4691e1a60577790effc55052f3e923d2d1f7c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "GnssHalTestCases"

#include <gnss_hal_test.h>

#include <VtsHalHidlTargetTestBase.h>

#include <android/hardware/gnss/1.1/IGnssConfiguration.h>

using android::hardware::hidl_vec;

using android::hardware::gnss::V1_0::GnssConstellationType;
using android::hardware::gnss::V1_0::GnssLocation;
using android::hardware::gnss::V1_0::IGnssDebug;
using android::hardware::gnss::V1_1::IGnssConfiguration;
using android::hardware::gnss::V1_1::IGnssMeasurement;

/*
 * SetupTeardownCreateCleanup:
 * Requests the gnss HAL then calls cleanup
 *
 * Empty test fixture to verify basic Setup & Teardown
 */
TEST_F(GnssHalTest, SetupTeardownCreateCleanup) {}

/*
 * TestGnssMeasurementCallback:
 * Gets the GnssMeasurementExtension and verify that it returns an actual extension.
 */
TEST_F(GnssHalTest, TestGnssMeasurementCallback) {
    auto gnssMeasurement = gnss_hal_->getExtensionGnssMeasurement_1_1();
    ASSERT_TRUE(gnssMeasurement.isOk());
    if (last_capabilities_ & IGnssCallback::Capabilities::MEASUREMENTS) {
        sp<IGnssMeasurement> iGnssMeas = gnssMeasurement;
        EXPECT_NE(iGnssMeas, nullptr);
    }
}

/*
 * GetLocationLowPower:
 * Turns on location, waits for at least 5 locations allowing max of LOCATION_TIMEOUT_SUBSEQUENT_SEC
 * between one location and the next. Also ensure that MIN_INTERVAL_MSEC is respected by waiting
 * NO_LOCATION_PERIOD_SEC and verfiy that no location is received. Also perform validity checks on
 * each received location.
 */
TEST_F(GnssHalTest, GetLocationLowPower) {
    const int kMinIntervalMsec = 5000;
    const int kLocationTimeoutSubsequentSec = (kMinIntervalMsec / 1000) + 1;
    const int kNoLocationPeriodSec = 2;
    const int kLocationsToCheck = 5;
    const bool kLowPowerMode = true;

    SetPositionMode(kMinIntervalMsec, kLowPowerMode);

    EXPECT_TRUE(StartAndGetSingleLocation());

    for (int i = 1; i < kLocationsToCheck; i++) {
        // Verify that kMinIntervalMsec is respected by waiting kNoLocationPeriodSec and
        // ensure that no location is received yet
        wait(kNoLocationPeriodSec);
        EXPECT_EQ(location_called_count_, i);
        EXPECT_EQ(std::cv_status::no_timeout,
                  wait(kLocationTimeoutSubsequentSec - kNoLocationPeriodSec));
        EXPECT_EQ(location_called_count_, i + 1);
        CheckLocation(last_location_, true);
    }

    StopAndClearLocations();
}

/*
 * FindStrongFrequentSource:
 *
 * Search through a GnssSvStatus list for the strongest satellite observed enough times
 *
 * returns the strongest source,
 *         or a source with constellation == UNKNOWN if none are found sufficient times
 */

IGnssConfiguration::BlacklistedSource FindStrongFrequentSource(
    const list<IGnssCallback::GnssSvStatus> list_gnss_sv_status, const int min_observations) {
    struct ComparableBlacklistedSource {
        IGnssConfiguration::BlacklistedSource id;

        bool operator<(const ComparableBlacklistedSource& compare) const {
            return ((id.svid < compare.id.svid) || ((id.svid == compare.id.svid) &&
                                                    (id.constellation < compare.id.constellation)));
        }
    };

    struct SignalCounts {
        int observations;
        float max_cn0_dbhz;
    };

    std::map<ComparableBlacklistedSource, SignalCounts> mapSignals;

    for (const auto& gnss_sv_status : list_gnss_sv_status) {
        for (uint32_t iSv = 0; iSv < gnss_sv_status.numSvs; iSv++) {
            const auto& gnss_sv = gnss_sv_status.gnssSvList[iSv];
            if (gnss_sv.svFlag & IGnssCallback::GnssSvFlags::USED_IN_FIX) {
                ComparableBlacklistedSource source;
                source.id.svid = gnss_sv.svid;
                source.id.constellation = gnss_sv.constellation;

                const auto& itSignal = mapSignals.find(source);
                if (itSignal == mapSignals.end()) {
                    SignalCounts counts;
                    counts.observations = 1;
                    counts.max_cn0_dbhz = gnss_sv.cN0Dbhz;
                    mapSignals.insert(
                        std::pair<ComparableBlacklistedSource, SignalCounts>(source, counts));
                } else {
                    itSignal->second.observations++;
                    if (itSignal->second.max_cn0_dbhz < gnss_sv.cN0Dbhz) {
                        itSignal->second.max_cn0_dbhz = gnss_sv.cN0Dbhz;
                    }
                }
            }
        }
    }

    float max_cn0_dbhz_with_sufficient_count = 0.;
    int total_observation_count = 0;
    int blacklisted_source_count_observation = 0;

    ComparableBlacklistedSource source_to_blacklist;  // initializes to zero = UNKNOWN constellation
    for (auto const& pairSignal : mapSignals) {
        total_observation_count += pairSignal.second.observations;
        if ((pairSignal.second.observations >= min_observations) &&
            (pairSignal.second.max_cn0_dbhz > max_cn0_dbhz_with_sufficient_count)) {
            source_to_blacklist = pairSignal.first;
            blacklisted_source_count_observation = pairSignal.second.observations;
            max_cn0_dbhz_with_sufficient_count = pairSignal.second.max_cn0_dbhz;
        }
    }
    ALOGD(
        "Among %d observations, chose svid %d, constellation %d, "
        "with %d observations at %.1f max CNo",
        total_observation_count, source_to_blacklist.id.svid,
        (int)source_to_blacklist.id.constellation, blacklisted_source_count_observation,
        max_cn0_dbhz_with_sufficient_count);

    return source_to_blacklist.id;
}

/*
 * BlacklistIndividualSatellites:
 *
 * 1) Turns on location, waits for 3 locations, ensuring they are valid, and checks corresponding
 * GnssStatus for common satellites (strongest and one other.)
 * 2a & b) Turns off location, and blacklists common satellites.
 * 3) Restart location, wait for 3 locations, ensuring they are valid, and checks corresponding
 * GnssStatus does not use those satellites.
 * 4a & b) Turns off location, and send in empty blacklist.
 * 5) Restart location, wait for 3 locations, ensuring they are valid, and checks corresponding
 * GnssStatus does re-use at least the previously strongest satellite
 */
TEST_F(GnssHalTest, BlacklistIndividualSatellites) {
    const int kLocationsToAwait = 3;

    StartAndCheckLocations(kLocationsToAwait);

    EXPECT_GE((int)list_gnss_sv_status_.size(), kLocationsToAwait);
    ALOGD("Observed %d GnssSvStatus, while awaiting %d Locations", (int)list_gnss_sv_status_.size(),
          kLocationsToAwait);

    /*
     * Identify strongest SV seen at least kLocationsToAwait -1 times
     * Why -1?  To avoid test flakiness in case of (plausible) slight flakiness in strongest signal
     * observability (one epoch RF null)
     */

    IGnssConfiguration::BlacklistedSource source_to_blacklist =
        FindStrongFrequentSource(list_gnss_sv_status_, kLocationsToAwait - 1);
    EXPECT_NE(source_to_blacklist.constellation, GnssConstellationType::UNKNOWN);

    // Stop locations, blacklist the common SV
    StopAndClearLocations();

    auto gnss_configuration_hal_return = gnss_hal_->getExtensionGnssConfiguration_1_1();
    ASSERT_TRUE(gnss_configuration_hal_return.isOk());
    sp<IGnssConfiguration> gnss_configuration_hal = gnss_configuration_hal_return;
    ASSERT_NE(gnss_configuration_hal, nullptr);

    hidl_vec<IGnssConfiguration::BlacklistedSource> sources;
    sources.resize(1);
    sources[0] = source_to_blacklist;

    auto result = gnss_configuration_hal->setBlacklist(sources);
    ASSERT_TRUE(result.isOk());
    EXPECT_TRUE(result);

    // retry and ensure satellite not used
    list_gnss_sv_status_.clear();

    location_called_count_ = 0;
    StartAndCheckLocations(kLocationsToAwait);

    EXPECT_GE((int)list_gnss_sv_status_.size(), kLocationsToAwait);
    ALOGD("Observed %d GnssSvStatus, while awaiting %d Locations", (int)list_gnss_sv_status_.size(),
          kLocationsToAwait);
    for (const auto& gnss_sv_status : list_gnss_sv_status_) {
        for (uint32_t iSv = 0; iSv < gnss_sv_status.numSvs; iSv++) {
            const auto& gnss_sv = gnss_sv_status.gnssSvList[iSv];
            EXPECT_FALSE((gnss_sv.svid == source_to_blacklist.svid) &&
                         (gnss_sv.constellation == source_to_blacklist.constellation) &&
                         (gnss_sv.svFlag & IGnssCallback::GnssSvFlags::USED_IN_FIX));
        }
    }

    // clear blacklist and restart - this time updating the blacklist while location is still on
    sources.resize(0);

    result = gnss_configuration_hal->setBlacklist(sources);
    ASSERT_TRUE(result.isOk());
    EXPECT_TRUE(result);

    location_called_count_ = 0;
    StopAndClearLocations();
    list_gnss_sv_status_.clear();

    StartAndCheckLocations(kLocationsToAwait);

    EXPECT_GE((int)list_gnss_sv_status_.size(), kLocationsToAwait);
    ALOGD("Observed %d GnssSvStatus, while awaiting %d Locations", (int)list_gnss_sv_status_.size(),
          kLocationsToAwait);

    bool strongest_sv_is_reobserved = false;
    for (const auto& gnss_sv_status : list_gnss_sv_status_) {
        for (uint32_t iSv = 0; iSv < gnss_sv_status.numSvs; iSv++) {
            const auto& gnss_sv = gnss_sv_status.gnssSvList[iSv];
            if ((gnss_sv.svid == source_to_blacklist.svid) &&
                (gnss_sv.constellation == source_to_blacklist.constellation) &&
                (gnss_sv.svFlag & IGnssCallback::GnssSvFlags::USED_IN_FIX)) {
                strongest_sv_is_reobserved = true;
                break;
            }
        }
        if (strongest_sv_is_reobserved) break;
    }
    EXPECT_TRUE(strongest_sv_is_reobserved);
    StopAndClearLocations();
}

/*
 * BlacklistConstellation:
 *
 * 1) Turns on location, waits for 3 locations, ensuring they are valid, and checks corresponding
 * GnssStatus for any non-GPS constellations.
 * 2a & b) Turns off location, and blacklist first non-GPS constellations.
 * 3) Restart location, wait for 3 locations, ensuring they are valid, and checks corresponding
 * GnssStatus does not use any constellation but GPS.
 * 4a & b) Clean up by turning off location, and send in empty blacklist.
 */
TEST_F(GnssHalTest, BlacklistConstellation) {
    const int kLocationsToAwait = 3;

    StartAndCheckLocations(kLocationsToAwait);

    EXPECT_GE((int)list_gnss_sv_status_.size(), kLocationsToAwait);
    ALOGD("Observed %d GnssSvStatus, while awaiting %d Locations", (int)list_gnss_sv_status_.size(),
          kLocationsToAwait);

    // Find first non-GPS constellation to blacklist
    GnssConstellationType constellation_to_blacklist = GnssConstellationType::UNKNOWN;
    for (const auto& gnss_sv_status : list_gnss_sv_status_) {
        for (uint32_t iSv = 0; iSv < gnss_sv_status.numSvs; iSv++) {
            const auto& gnss_sv = gnss_sv_status.gnssSvList[iSv];
            if ((gnss_sv.svFlag & IGnssCallback::GnssSvFlags::USED_IN_FIX) &&
                (gnss_sv.constellation != GnssConstellationType::UNKNOWN) &&
                (gnss_sv.constellation != GnssConstellationType::GPS)) {
                // found a non-GPS constellation
                constellation_to_blacklist = gnss_sv.constellation;
                break;
            }
        }
        if (constellation_to_blacklist != GnssConstellationType::UNKNOWN) {
            break;
        }
    }

    if (constellation_to_blacklist == GnssConstellationType::UNKNOWN) {
        ALOGI("No non-GPS constellations found, constellation blacklist test less effective.");
        // Proceed functionally to blacklist something.
        constellation_to_blacklist = GnssConstellationType::GLONASS;
    }
    IGnssConfiguration::BlacklistedSource source_to_blacklist;
    source_to_blacklist.constellation = constellation_to_blacklist;
    source_to_blacklist.svid = 0;  // documented wildcard for all satellites in this constellation

    auto gnss_configuration_hal_return = gnss_hal_->getExtensionGnssConfiguration_1_1();
    ASSERT_TRUE(gnss_configuration_hal_return.isOk());
    sp<IGnssConfiguration> gnss_configuration_hal = gnss_configuration_hal_return;
    ASSERT_NE(gnss_configuration_hal, nullptr);

    hidl_vec<IGnssConfiguration::BlacklistedSource> sources;
    sources.resize(1);
    sources[0] = source_to_blacklist;

    auto result = gnss_configuration_hal->setBlacklist(sources);
    ASSERT_TRUE(result.isOk());
    EXPECT_TRUE(result);

    // retry and ensure constellation not used
    list_gnss_sv_status_.clear();

    location_called_count_ = 0;
    StartAndCheckLocations(kLocationsToAwait);

    EXPECT_GE((int)list_gnss_sv_status_.size(), kLocationsToAwait);
    ALOGD("Observed %d GnssSvStatus, while awaiting %d Locations", (int)list_gnss_sv_status_.size(),
          kLocationsToAwait);
    for (const auto& gnss_sv_status : list_gnss_sv_status_) {
        for (uint32_t iSv = 0; iSv < gnss_sv_status.numSvs; iSv++) {
            const auto& gnss_sv = gnss_sv_status.gnssSvList[iSv];
            EXPECT_FALSE((gnss_sv.constellation == source_to_blacklist.constellation) &&
                         (gnss_sv.svFlag & IGnssCallback::GnssSvFlags::USED_IN_FIX));
        }
    }

    // clean up
    StopAndClearLocations();
    sources.resize(0);
    result = gnss_configuration_hal->setBlacklist(sources);
    ASSERT_TRUE(result.isOk());
    EXPECT_TRUE(result);
}

/*
 * InjectBestLocation
 *
 * Ensure successfully injecting a location.
 */
TEST_F(GnssHalTest, InjectBestLocation) {
    GnssLocation gnssLocation = {.gnssLocationFlags = 0,  // set below
                                 .latitudeDegrees = 43.0,
                                 .longitudeDegrees = -180,
                                 .altitudeMeters = 1000,
                                 .speedMetersPerSec = 0,
                                 .bearingDegrees = 0,
                                 .horizontalAccuracyMeters = 0.1,
                                 .verticalAccuracyMeters = 0.1,
                                 .speedAccuracyMetersPerSecond = 0.1,
                                 .bearingAccuracyDegrees = 0.1,
                                 .timestamp = 1534567890123L};
    gnssLocation.gnssLocationFlags |=
        GnssLocationFlags::HAS_LAT_LONG | GnssLocationFlags::HAS_ALTITUDE |
        GnssLocationFlags::HAS_SPEED | GnssLocationFlags::HAS_HORIZONTAL_ACCURACY |
        GnssLocationFlags::HAS_VERTICAL_ACCURACY | GnssLocationFlags::HAS_SPEED_ACCURACY |
        GnssLocationFlags::HAS_BEARING | GnssLocationFlags::HAS_BEARING_ACCURACY;

    CheckLocation(gnssLocation, true);

    auto result = gnss_hal_->injectBestLocation(gnssLocation);

    ASSERT_TRUE(result.isOk());
    EXPECT_TRUE(result);

    auto resultVoid = gnss_hal_->deleteAidingData(IGnss::GnssAidingData::DELETE_ALL);

    ASSERT_TRUE(resultVoid.isOk());
}

/*
 * GnssDebugValuesSanityTest:
 * Ensures that GnssDebug values make sense.
 */
TEST_F(GnssHalTest, GnssDebugValuesSanityTest) {
    auto gnssDebug = gnss_hal_->getExtensionGnssDebug();
    ASSERT_TRUE(gnssDebug.isOk());
    if (info_called_count_ > 0 && last_info_.yearOfHw >= 2017) {
        sp<IGnssDebug> iGnssDebug = gnssDebug;
        EXPECT_NE(iGnssDebug, nullptr);

        IGnssDebug::DebugData data;
        iGnssDebug->getDebugData(
            [&data](const IGnssDebug::DebugData& debugData) { data = debugData; });

        if (data.position.valid) {
            EXPECT_GE(data.position.latitudeDegrees, -90);
            EXPECT_LE(data.position.latitudeDegrees, 90);

            EXPECT_GE(data.position.longitudeDegrees, -180);
            EXPECT_LE(data.position.longitudeDegrees, 180);

            EXPECT_GE(data.position.altitudeMeters, -1000);  // Dead Sea: -414m
            EXPECT_LE(data.position.altitudeMeters, 20000);  // Mount Everest: 8850m

            EXPECT_GE(data.position.speedMetersPerSec, 0);
            EXPECT_LE(data.position.speedMetersPerSec, 600);

            EXPECT_GE(data.position.bearingDegrees, -360);
            EXPECT_LE(data.position.bearingDegrees, 360);

            EXPECT_GT(data.position.horizontalAccuracyMeters, 0);
            EXPECT_LE(data.position.horizontalAccuracyMeters, 20000000);

            EXPECT_GT(data.position.verticalAccuracyMeters, 0);
            EXPECT_LE(data.position.verticalAccuracyMeters, 20000);

            EXPECT_GT(data.position.speedAccuracyMetersPerSecond, 0);
            EXPECT_LE(data.position.speedAccuracyMetersPerSecond, 500);

            EXPECT_GT(data.position.bearingAccuracyDegrees, 0);
            EXPECT_LE(data.position.bearingAccuracyDegrees, 180);

            EXPECT_GE(data.position.ageSeconds, 0);
        }

        EXPECT_GE(data.time.timeEstimate, 1514764800000);  // Jan 01 2018 00:00:00

        EXPECT_GT(data.time.timeUncertaintyNs, 0);

        EXPECT_GT(data.time.frequencyUncertaintyNsPerSec, 0);
        EXPECT_LE(data.time.frequencyUncertaintyNsPerSec, 2.0e5);  // 200 ppm
    }
}