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(const bool strict,const int min_interval_msec,const bool low_power_mode)100 bool GnssHalTest::StartAndCheckFirstLocation(const bool strict, const int min_interval_msec,
101 const bool low_power_mode) {
102 SetPositionMode(min_interval_msec, low_power_mode);
103 const auto result = gnss_hal_->start();
104
105 EXPECT_TRUE(result.isOk());
106 EXPECT_TRUE(result);
107 /*
108 * GnssLocationProvider support of AGPS SUPL & XtraDownloader is not available in VTS,
109 * so allow time to demodulate ephemeris over the air.
110 */
111 const int kFirstGnssLocationTimeoutSeconds = 75;
112 int locationCalledCount = 0;
113
114 if (strict) {
115 EXPECT_TRUE(gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_,
116 kFirstGnssLocationTimeoutSeconds));
117 locationCalledCount = gnss_cb_->location_cbq_.calledCount();
118 EXPECT_EQ(locationCalledCount, 1);
119 }
120 if (locationCalledCount > 0) {
121 // don't require speed on first fix
122 CheckLocation(gnss_cb_->last_location_, false);
123 return true;
124 }
125 return false;
126 }
127
CheckLocation(const GnssLocation_2_0 & location,bool check_speed)128 void GnssHalTest::CheckLocation(const GnssLocation_2_0& location, bool check_speed) {
129 const bool check_more_accuracies =
130 (gnss_cb_->info_cbq_.calledCount() > 0 && gnss_cb_->last_info_.yearOfHw >= 2017);
131
132 Utils::checkLocation(location.v1_0, check_speed, check_more_accuracies);
133 }
134
StartAndCheckLocations(int count)135 void GnssHalTest::StartAndCheckLocations(int count) {
136 const int kMinIntervalMsec = 500;
137 const int kLocationTimeoutSubsequentSec = 2;
138 const bool kLowPowerMode = false;
139
140 SetPositionMode(kMinIntervalMsec, kLowPowerMode);
141
142 EXPECT_TRUE(StartAndCheckFirstLocation(/* strict= */ true,
143 /* min_interval_msec= */ 1000,
144 /* low_power_mode= */ false));
145
146 for (int i = 1; i < count; i++) {
147 EXPECT_TRUE(gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_,
148 kLocationTimeoutSubsequentSec));
149 int locationCalledCount = gnss_cb_->location_cbq_.calledCount();
150 EXPECT_EQ(locationCalledCount, i + 1);
151 // Don't cause confusion by checking details if no location yet
152 if (locationCalledCount > 0) {
153 // Should be more than 1 location by now, but if not, still don't check first fix speed
154 CheckLocation(gnss_cb_->last_location_, locationCalledCount > 1);
155 }
156 }
157 }
158
IsGnssHalVersion_2_0() const159 bool GnssHalTest::IsGnssHalVersion_2_0() const {
160 using ::android::hidl::manager::V1_2::IServiceManager;
161 sp<IServiceManager> manager = ::android::hardware::defaultServiceManager1_2();
162
163 bool hasGnssHalVersion_2_0 = false;
164 manager->listManifestByInterface(
165 "android.hardware.gnss@2.0::IGnss",
166 [&hasGnssHalVersion_2_0](const hidl_vec<hidl_string>& registered) {
167 hasGnssHalVersion_2_0 = registered.size() != 0;
168 });
169
170 bool hasGnssHalVersion_2_1 = false;
171 manager->listManifestByInterface(
172 "android.hardware.gnss@2.1::IGnss",
173 [&hasGnssHalVersion_2_1](const hidl_vec<hidl_string>& registered) {
174 hasGnssHalVersion_2_1 = registered.size() != 0;
175 });
176
177 return hasGnssHalVersion_2_0 && !hasGnssHalVersion_2_1;
178 }
179
GnssCallback()180 GnssHalTest::GnssCallback::GnssCallback()
181 : info_cbq_("system_info"),
182 name_cbq_("name"),
183 capabilities_cbq_("capabilities"),
184 location_cbq_("location"),
185 sv_info_list_cbq_("sv_info") {}
186
gnssSetSystemInfoCb(const IGnssCallback_1_0::GnssSystemInfo & info)187 Return<void> GnssHalTest::GnssCallback::gnssSetSystemInfoCb(
188 const IGnssCallback_1_0::GnssSystemInfo& info) {
189 ALOGI("Info received, year %d", info.yearOfHw);
190 info_cbq_.store(info);
191 return Void();
192 }
193
gnssSetCapabilitesCb(uint32_t capabilities)194 Return<void> GnssHalTest::GnssCallback::gnssSetCapabilitesCb(uint32_t capabilities) {
195 ALOGI("Capabilities received %d", capabilities);
196 capabilities_cbq_.store(capabilities);
197 return Void();
198 }
199
gnssSetCapabilitiesCb_2_0(uint32_t capabilities)200 Return<void> GnssHalTest::GnssCallback::gnssSetCapabilitiesCb_2_0(uint32_t capabilities) {
201 ALOGI("Capabilities (v2.0) received %d", capabilities);
202 capabilities_cbq_.store(capabilities);
203 return Void();
204 }
205
gnssNameCb(const android::hardware::hidl_string & name)206 Return<void> GnssHalTest::GnssCallback::gnssNameCb(const android::hardware::hidl_string& name) {
207 ALOGI("Name received: %s", name.c_str());
208 name_cbq_.store(name);
209 return Void();
210 }
211
gnssLocationCb(const GnssLocation_1_0 & location)212 Return<void> GnssHalTest::GnssCallback::gnssLocationCb(const GnssLocation_1_0& location) {
213 ALOGI("Location received");
214 GnssLocation_2_0 location_v2_0;
215 location_v2_0.v1_0 = location;
216 return gnssLocationCbImpl(location_v2_0);
217 }
218
gnssLocationCb_2_0(const GnssLocation_2_0 & location)219 Return<void> GnssHalTest::GnssCallback::gnssLocationCb_2_0(const GnssLocation_2_0& location) {
220 ALOGI("Location (v2.0) received");
221 return gnssLocationCbImpl(location);
222 }
223
gnssLocationCbImpl(const GnssLocation_2_0 & location)224 Return<void> GnssHalTest::GnssCallback::gnssLocationCbImpl(const GnssLocation_2_0& location) {
225 location_cbq_.store(location);
226 return Void();
227 }
228
gnssSvStatusCb(const IGnssCallback_1_0::GnssSvStatus &)229 Return<void> GnssHalTest::GnssCallback::gnssSvStatusCb(const IGnssCallback_1_0::GnssSvStatus&) {
230 ALOGI("gnssSvStatusCb");
231 return Void();
232 }
233
gnssSvStatusCb_2_0(const hidl_vec<IGnssCallback_2_0::GnssSvInfo> & svInfoList)234 Return<void> GnssHalTest::GnssCallback::gnssSvStatusCb_2_0(
235 const hidl_vec<IGnssCallback_2_0::GnssSvInfo>& svInfoList) {
236 ALOGI("gnssSvStatusCb_2_0. Size = %d", (int)svInfoList.size());
237 sv_info_list_cbq_.store(svInfoList);
238 return Void();
239 }
240
gnssMeasurementCb_2_0(const IGnssMeasurementCallback_2_0::GnssData & data)241 Return<void> GnssHalTest::GnssMeasurementCallback::gnssMeasurementCb_2_0(
242 const IGnssMeasurementCallback_2_0::GnssData& data) {
243 ALOGD("GnssMeasurement received. Size = %d", (int)data.measurements.size());
244 measurement_cbq_.store(data);
245 return Void();
246 }
247
setCapabilitiesCb(uint32_t capabilities)248 Return<void> GnssHalTest::GnssMeasurementCorrectionsCallback::setCapabilitiesCb(
249 uint32_t capabilities) {
250 ALOGI("GnssMeasurementCorrectionsCallback capabilities received %d", capabilities);
251 capabilities_cbq_.store(capabilities);
252 return Void();
253 }
254
startLocationAndGetNonGpsConstellation()255 GnssConstellationType_1_0 GnssHalTest::startLocationAndGetNonGpsConstellation() {
256 const int kLocationsToAwait = 3;
257
258 gnss_cb_->location_cbq_.reset();
259 StartAndCheckLocations(kLocationsToAwait);
260 const int location_called_count = gnss_cb_->location_cbq_.calledCount();
261
262 // Tolerate 1 less sv status to handle edge cases in reporting.
263 int sv_info_list_cbq_size = gnss_cb_->sv_info_list_cbq_.size();
264 EXPECT_GE(sv_info_list_cbq_size + 1, kLocationsToAwait);
265 ALOGD("Observed %d GnssSvStatus, while awaiting %d Locations (%d received)",
266 sv_info_list_cbq_size, kLocationsToAwait, location_called_count);
267
268 // Find first non-GPS constellation to blacklist. Exclude IRNSS in GnssConstellationType_2_0
269 // as blacklisting of this constellation is not supported in gnss@2.0.
270 const int kGnssSvStatusTimeout = 2;
271 GnssConstellationType_1_0 constellation_to_blacklist = GnssConstellationType_1_0::UNKNOWN;
272 for (int i = 0; i < sv_info_list_cbq_size; ++i) {
273 hidl_vec<IGnssCallback_2_0::GnssSvInfo> sv_info_list;
274 gnss_cb_->sv_info_list_cbq_.retrieve(sv_info_list, kGnssSvStatusTimeout);
275 for (IGnssCallback_2_0::GnssSvInfo sv_info : sv_info_list) {
276 if ((sv_info.v1_0.svFlag & IGnssCallback_2_0::GnssSvFlags::USED_IN_FIX) &&
277 (sv_info.constellation != GnssConstellationType_2_0::UNKNOWN) &&
278 (sv_info.constellation != GnssConstellationType_2_0::IRNSS) &&
279 (sv_info.constellation != GnssConstellationType_2_0::GPS)) {
280 // found a non-GPS V1_0 constellation
281 constellation_to_blacklist = Utils::mapConstellationType(sv_info.constellation);
282 break;
283 }
284 }
285 if (constellation_to_blacklist != GnssConstellationType_1_0::UNKNOWN) {
286 break;
287 }
288 }
289
290 if (constellation_to_blacklist == GnssConstellationType_1_0::UNKNOWN) {
291 ALOGI("No non-GPS constellations found, constellation blacklist test less effective.");
292 // Proceed functionally to blacklist something.
293 constellation_to_blacklist = GnssConstellationType_1_0::GLONASS;
294 }
295 return constellation_to_blacklist;
296 }
297