• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 "GnssHalTest"
18 
19 #include <android/hidl/manager/1.2/IServiceManager.h>
20 #include <gnss_hal_test.h>
21 #include <gtest/gtest.h>
22 #include <hidl/ServiceManagement.h>
23 #include <chrono>
24 #include "Utils.h"
25 
26 using ::android::hardware::hidl_string;
27 
28 using ::android::hardware::gnss::common::Utils;
29 
30 // Implementations for the main test class for GNSS HAL
SetUp()31 void GnssHalTest::SetUp() {
32     gnss_hal_ = IGnss::getService(GetParam());
33     ASSERT_NE(gnss_hal_, nullptr);
34 
35     SetUpGnssCallback();
36 }
37 
TearDown()38 void GnssHalTest::TearDown() {
39     if (gnss_hal_ != nullptr) {
40         gnss_hal_->cleanup();
41         gnss_hal_ = nullptr;
42     }
43 
44     // Set to nullptr to destruct the callback event queues and warn of any unprocessed events.
45     gnss_cb_ = nullptr;
46 }
47 
SetUpGnssCallback()48 void GnssHalTest::SetUpGnssCallback() {
49     gnss_cb_ = new GnssCallback();
50     ASSERT_NE(gnss_cb_, nullptr);
51 
52     auto result = gnss_hal_->setCallback_2_0(gnss_cb_);
53     if (!result.isOk()) {
54         ALOGE("result of failed setCallback %s", result.description().c_str());
55     }
56 
57     ASSERT_TRUE(result.isOk());
58     ASSERT_TRUE(result);
59 
60     /*
61      * All capabilities, name and systemInfo callbacks should trigger
62      */
63     EXPECT_TRUE(gnss_cb_->capabilities_cbq_.retrieve(gnss_cb_->last_capabilities_, TIMEOUT_SEC));
64     EXPECT_TRUE(gnss_cb_->info_cbq_.retrieve(gnss_cb_->last_info_, TIMEOUT_SEC));
65     EXPECT_TRUE(gnss_cb_->name_cbq_.retrieve(gnss_cb_->last_name_, TIMEOUT_SEC));
66 
67     EXPECT_EQ(gnss_cb_->capabilities_cbq_.calledCount(), 1);
68     EXPECT_EQ(gnss_cb_->info_cbq_.calledCount(), 1);
69     EXPECT_EQ(gnss_cb_->name_cbq_.calledCount(), 1);
70 }
71 
StopAndClearLocations()72 void GnssHalTest::StopAndClearLocations() {
73     const auto result = gnss_hal_->stop();
74 
75     EXPECT_TRUE(result.isOk());
76     EXPECT_TRUE(result);
77 
78     /*
79      * Clear notify/waiting counter, allowing up till the timeout after
80      * the last reply for final startup messages to arrive (esp. system
81      * info.)
82      */
83     while (gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_, TIMEOUT_SEC)) {
84     }
85     gnss_cb_->location_cbq_.reset();
86 }
87 
SetPositionMode(const int min_interval_msec,const bool low_power_mode)88 void GnssHalTest::SetPositionMode(const int min_interval_msec, const bool low_power_mode) {
89     const int kPreferredAccuracy = 0;  // Ideally perfect (matches GnssLocationProvider)
90     const int kPreferredTimeMsec = 0;  // Ideally immediate
91 
92     const auto result = gnss_hal_->setPositionMode_1_1(
93             IGnss::GnssPositionMode::MS_BASED, IGnss::GnssPositionRecurrence::RECURRENCE_PERIODIC,
94             min_interval_msec, kPreferredAccuracy, kPreferredTimeMsec, low_power_mode);
95 
96     ASSERT_TRUE(result.isOk());
97     EXPECT_TRUE(result);
98 }
99 
StartAndCheckFirstLocation(bool strict)100 bool GnssHalTest::StartAndCheckFirstLocation(bool strict) {
101     const auto result = gnss_hal_->start();
102 
103     EXPECT_TRUE(result.isOk());
104     EXPECT_TRUE(result);
105     /*
106      * GnssLocationProvider support of AGPS SUPL & XtraDownloader is not available in VTS,
107      * so allow time to demodulate ephemeris over the air.
108      */
109     const int kFirstGnssLocationTimeoutSeconds = 75;
110     int locationCalledCount = 0;
111 
112     if (strict) {
113         EXPECT_TRUE(gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_,
114                                                      kFirstGnssLocationTimeoutSeconds));
115         locationCalledCount = gnss_cb_->location_cbq_.calledCount();
116         EXPECT_EQ(locationCalledCount, 1);
117     }
118     if (locationCalledCount > 0) {
119         // don't require speed on first fix
120         CheckLocation(gnss_cb_->last_location_, false);
121         return true;
122     }
123     return false;
124 }
125 
CheckLocation(const GnssLocation_2_0 & location,bool check_speed)126 void GnssHalTest::CheckLocation(const GnssLocation_2_0& location, bool check_speed) {
127     const bool check_more_accuracies =
128             (gnss_cb_->info_cbq_.calledCount() > 0 && gnss_cb_->last_info_.yearOfHw >= 2017);
129 
130     Utils::checkLocation(location.v1_0, check_speed, check_more_accuracies);
131 }
132 
StartAndCheckLocations(int count)133 void GnssHalTest::StartAndCheckLocations(int count) {
134     const int kMinIntervalMsec = 500;
135     const int kLocationTimeoutSubsequentSec = 2;
136     const bool kLowPowerMode = false;
137 
138     SetPositionMode(kMinIntervalMsec, kLowPowerMode);
139 
140     EXPECT_TRUE(StartAndCheckFirstLocation(/* strict= */ true));
141 
142     for (int i = 1; i < count; i++) {
143         EXPECT_TRUE(gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_,
144                                                      kLocationTimeoutSubsequentSec));
145         int locationCalledCount = gnss_cb_->location_cbq_.calledCount();
146         EXPECT_EQ(locationCalledCount, i + 1);
147         // Don't cause confusion by checking details if no location yet
148         if (locationCalledCount > 0) {
149             // Should be more than 1 location by now, but if not, still don't check first fix speed
150             CheckLocation(gnss_cb_->last_location_, locationCalledCount > 1);
151         }
152     }
153 }
154 
IsGnssHalVersion_2_0() const155 bool GnssHalTest::IsGnssHalVersion_2_0() const {
156     using ::android::hidl::manager::V1_2::IServiceManager;
157     sp<IServiceManager> manager = ::android::hardware::defaultServiceManager1_2();
158 
159     bool hasGnssHalVersion_2_0 = false;
160     manager->listManifestByInterface(
161             "android.hardware.gnss@2.0::IGnss",
162             [&hasGnssHalVersion_2_0](const hidl_vec<hidl_string>& registered) {
163                 hasGnssHalVersion_2_0 = registered.size() != 0;
164             });
165 
166     bool hasGnssHalVersion_2_1 = false;
167     manager->listManifestByInterface(
168             "android.hardware.gnss@2.1::IGnss",
169             [&hasGnssHalVersion_2_1](const hidl_vec<hidl_string>& registered) {
170                 hasGnssHalVersion_2_1 = registered.size() != 0;
171             });
172 
173     return hasGnssHalVersion_2_0 && !hasGnssHalVersion_2_1;
174 }
175 
GnssCallback()176 GnssHalTest::GnssCallback::GnssCallback()
177     : info_cbq_("system_info"),
178       name_cbq_("name"),
179       capabilities_cbq_("capabilities"),
180       location_cbq_("location"),
181       sv_info_list_cbq_("sv_info") {}
182 
gnssSetSystemInfoCb(const IGnssCallback_1_0::GnssSystemInfo & info)183 Return<void> GnssHalTest::GnssCallback::gnssSetSystemInfoCb(
184         const IGnssCallback_1_0::GnssSystemInfo& info) {
185     ALOGI("Info received, year %d", info.yearOfHw);
186     info_cbq_.store(info);
187     return Void();
188 }
189 
gnssSetCapabilitesCb(uint32_t capabilities)190 Return<void> GnssHalTest::GnssCallback::gnssSetCapabilitesCb(uint32_t capabilities) {
191     ALOGI("Capabilities received %d", capabilities);
192     capabilities_cbq_.store(capabilities);
193     return Void();
194 }
195 
gnssSetCapabilitiesCb_2_0(uint32_t capabilities)196 Return<void> GnssHalTest::GnssCallback::gnssSetCapabilitiesCb_2_0(uint32_t capabilities) {
197     ALOGI("Capabilities (v2.0) received %d", capabilities);
198     capabilities_cbq_.store(capabilities);
199     return Void();
200 }
201 
gnssNameCb(const android::hardware::hidl_string & name)202 Return<void> GnssHalTest::GnssCallback::gnssNameCb(const android::hardware::hidl_string& name) {
203     ALOGI("Name received: %s", name.c_str());
204     name_cbq_.store(name);
205     return Void();
206 }
207 
gnssLocationCb(const GnssLocation_1_0 & location)208 Return<void> GnssHalTest::GnssCallback::gnssLocationCb(const GnssLocation_1_0& location) {
209     ALOGI("Location received");
210     GnssLocation_2_0 location_v2_0;
211     location_v2_0.v1_0 = location;
212     return gnssLocationCbImpl(location_v2_0);
213 }
214 
gnssLocationCb_2_0(const GnssLocation_2_0 & location)215 Return<void> GnssHalTest::GnssCallback::gnssLocationCb_2_0(const GnssLocation_2_0& location) {
216     ALOGI("Location (v2.0) received");
217     return gnssLocationCbImpl(location);
218 }
219 
gnssLocationCbImpl(const GnssLocation_2_0 & location)220 Return<void> GnssHalTest::GnssCallback::gnssLocationCbImpl(const GnssLocation_2_0& location) {
221     location_cbq_.store(location);
222     return Void();
223 }
224 
gnssSvStatusCb(const IGnssCallback_1_0::GnssSvStatus &)225 Return<void> GnssHalTest::GnssCallback::gnssSvStatusCb(const IGnssCallback_1_0::GnssSvStatus&) {
226     ALOGI("gnssSvStatusCb");
227     return Void();
228 }
229 
gnssSvStatusCb_2_0(const hidl_vec<IGnssCallback_2_0::GnssSvInfo> & svInfoList)230 Return<void> GnssHalTest::GnssCallback::gnssSvStatusCb_2_0(
231         const hidl_vec<IGnssCallback_2_0::GnssSvInfo>& svInfoList) {
232     ALOGI("gnssSvStatusCb_2_0. Size = %d", (int)svInfoList.size());
233     sv_info_list_cbq_.store(svInfoList);
234     return Void();
235 }
236 
gnssMeasurementCb_2_0(const IGnssMeasurementCallback_2_0::GnssData & data)237 Return<void> GnssHalTest::GnssMeasurementCallback::gnssMeasurementCb_2_0(
238     const IGnssMeasurementCallback_2_0::GnssData& data) {
239     ALOGD("GnssMeasurement received. Size = %d", (int)data.measurements.size());
240     measurement_cbq_.store(data);
241     return Void();
242 }
243 
setCapabilitiesCb(uint32_t capabilities)244 Return<void> GnssHalTest::GnssMeasurementCorrectionsCallback::setCapabilitiesCb(
245         uint32_t capabilities) {
246     ALOGI("GnssMeasurementCorrectionsCallback capabilities received %d", capabilities);
247     capabilities_cbq_.store(capabilities);
248     return Void();
249 }
250 
startLocationAndGetNonGpsConstellation()251 GnssConstellationType_1_0 GnssHalTest::startLocationAndGetNonGpsConstellation() {
252     const int kLocationsToAwait = 3;
253 
254     gnss_cb_->location_cbq_.reset();
255     StartAndCheckLocations(kLocationsToAwait);
256     const int location_called_count = gnss_cb_->location_cbq_.calledCount();
257 
258     // Tolerate 1 less sv status to handle edge cases in reporting.
259     int sv_info_list_cbq_size = gnss_cb_->sv_info_list_cbq_.size();
260     EXPECT_GE(sv_info_list_cbq_size + 1, kLocationsToAwait);
261     ALOGD("Observed %d GnssSvStatus, while awaiting %d Locations (%d received)",
262           sv_info_list_cbq_size, kLocationsToAwait, location_called_count);
263 
264     // Find first non-GPS constellation to blacklist. Exclude IRNSS in GnssConstellationType_2_0
265     // as blacklisting of this constellation is not supported in gnss@2.0.
266     const int kGnssSvStatusTimeout = 2;
267     GnssConstellationType_1_0 constellation_to_blacklist = GnssConstellationType_1_0::UNKNOWN;
268     for (int i = 0; i < sv_info_list_cbq_size; ++i) {
269         hidl_vec<IGnssCallback_2_0::GnssSvInfo> sv_info_list;
270         gnss_cb_->sv_info_list_cbq_.retrieve(sv_info_list, kGnssSvStatusTimeout);
271         for (IGnssCallback_2_0::GnssSvInfo sv_info : sv_info_list) {
272             if ((sv_info.v1_0.svFlag & IGnssCallback_2_0::GnssSvFlags::USED_IN_FIX) &&
273                 (sv_info.constellation != GnssConstellationType_2_0::UNKNOWN) &&
274                 (sv_info.constellation != GnssConstellationType_2_0::IRNSS) &&
275                 (sv_info.constellation != GnssConstellationType_2_0::GPS)) {
276                 // found a non-GPS V1_0 constellation
277                 constellation_to_blacklist = Utils::mapConstellationType(sv_info.constellation);
278                 break;
279             }
280         }
281         if (constellation_to_blacklist != GnssConstellationType_1_0::UNKNOWN) {
282             break;
283         }
284     }
285 
286     if (constellation_to_blacklist == GnssConstellationType_1_0::UNKNOWN) {
287         ALOGI("No non-GPS constellations found, constellation blacklist test less effective.");
288         // Proceed functionally to blacklist something.
289         constellation_to_blacklist = GnssConstellationType_1_0::GLONASS;
290     }
291     return constellation_to_blacklist;
292 }
293