blob: c1128ba28df117a36138c9f81cd52bda99032886 [file] [log] [blame]
Yu-Han Yang1e1a6762020-09-30 17:01:53 -07001/*
2 * Copyright (C) 2020 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
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -080017#define LOG_TAG "GnssHalTest"
18
Yu-Han Yang1e1a6762020-09-30 17:01:53 -070019#include "gnss_hal_test.h"
20#include <hidl/ServiceManagement.h>
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -080021#include "Utils.h"
Yu-Han Yang1e1a6762020-09-30 17:01:53 -070022
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -080023using android::hardware::gnss::GnssConstellationType;
24using android::hardware::gnss::GnssLocation;
25using android::hardware::gnss::IGnss;
26using android::hardware::gnss::IGnssCallback;
27using android::hardware::gnss::common::Utils;
28using GnssConstellationTypeV2_0 = android::hardware::gnss::V2_0::GnssConstellationType;
Yu-Han Yang1e1a6762020-09-30 17:01:53 -070029
30void GnssHalTest::SetUp() {
31 // Get AIDL handle
32 aidl_gnss_hal_ = android::waitForDeclaredService<IGnssAidl>(String16(GetParam().c_str()));
33 ASSERT_NE(aidl_gnss_hal_, nullptr);
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -080034 ALOGD("AIDL Interface Version = %d", aidl_gnss_hal_->getInterfaceVersion());
Yu-Han Yang1e1a6762020-09-30 17:01:53 -070035
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -080036 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
37 const auto& hidlInstanceNames = android::hardware::getAllHalInstanceNames(
38 android::hardware::gnss::V2_1::IGnss::descriptor);
39 gnss_hal_ = IGnss_V2_1::getService(hidlInstanceNames[0]);
40 ASSERT_NE(gnss_hal_, nullptr);
41 }
Yu-Han Yang1e1a6762020-09-30 17:01:53 -070042
43 SetUpGnssCallback();
44}
45
46void GnssHalTest::SetUpGnssCallback() {
47 aidl_gnss_cb_ = new GnssCallbackAidl();
48 ASSERT_NE(aidl_gnss_cb_, nullptr);
49
50 auto status = aidl_gnss_hal_->setCallback(aidl_gnss_cb_);
51 if (!status.isOk()) {
52 ALOGE("Failed to setCallback");
53 }
Yu-Han Yang1e1a6762020-09-30 17:01:53 -070054 ASSERT_TRUE(status.isOk());
55
56 /*
57 * Capabilities callback should trigger.
58 */
59 EXPECT_TRUE(aidl_gnss_cb_->capabilities_cbq_.retrieve(aidl_gnss_cb_->last_capabilities_,
60 TIMEOUT_SEC));
Yu-Han Yang1e1a6762020-09-30 17:01:53 -070061 EXPECT_EQ(aidl_gnss_cb_->capabilities_cbq_.calledCount(), 1);
62
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -080063 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
64 // Invoke the super method.
65 GnssHalTestTemplate<IGnss_V2_1>::SetUpGnssCallback();
66 }
67}
68
69void GnssHalTest::CheckLocation(const GnssLocation& location, bool check_speed) {
70 Utils::checkLocation(location, check_speed, /* check_more_accuracies= */ true);
71}
72
73void GnssHalTest::SetPositionMode(const int min_interval_msec, const bool low_power_mode) {
74 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
75 // Invoke the super method.
76 return GnssHalTestTemplate<IGnss_V2_1>::SetPositionMode(min_interval_msec, low_power_mode);
77 }
78
79 const int kPreferredAccuracy = 0; // Ideally perfect (matches GnssLocationProvider)
80 const int kPreferredTimeMsec = 0; // Ideally immediate
81
Yu-Han Yang75934f72022-01-24 15:35:25 -080082 IGnss::PositionModeOptions options;
83 options.mode = IGnss::GnssPositionMode::MS_BASED;
84 options.recurrence = IGnss::GnssPositionRecurrence::RECURRENCE_PERIODIC;
85 options.minIntervalMs = min_interval_msec;
86 options.preferredAccuracyMeters = kPreferredAccuracy;
87 options.preferredTimeMs = kPreferredTimeMsec;
88 options.lowPowerMode = low_power_mode;
89 auto status = aidl_gnss_hal_->setPositionMode(options);
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -080090
91 ASSERT_TRUE(status.isOk());
92}
93
94bool GnssHalTest::StartAndCheckFirstLocation(const int min_interval_msec,
95 const bool low_power_mode) {
96 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
97 // Invoke the super method.
98 return GnssHalTestTemplate<IGnss_V2_1>::StartAndCheckFirstLocation(min_interval_msec,
99 low_power_mode);
100 }
101
102 SetPositionMode(min_interval_msec, low_power_mode);
Yu-Han Yang69f0f8b2022-01-21 13:03:32 -0800103 auto status = aidl_gnss_hal_->start();
104 EXPECT_TRUE(status.isOk());
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -0800105
Yu-Han Yang69f0f8b2022-01-21 13:03:32 -0800106 status = aidl_gnss_hal_->startSvStatus();
107 EXPECT_TRUE(status.isOk());
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -0800108
109 /*
110 * GnssLocationProvider support of AGPS SUPL & XtraDownloader is not available in VTS,
111 * so allow time to demodulate ephemeris over the air.
112 */
113 const int kFirstGnssLocationTimeoutSeconds = 75;
114
115 EXPECT_TRUE(aidl_gnss_cb_->location_cbq_.retrieve(aidl_gnss_cb_->last_location_,
116 kFirstGnssLocationTimeoutSeconds));
117 int locationCalledCount = aidl_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(aidl_gnss_cb_->last_location_, false);
123 return true;
124 }
125 return false;
126}
127
128void GnssHalTest::StopAndClearLocations() {
129 ALOGD("StopAndClearLocations");
130 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
131 // Invoke the super method.
132 return GnssHalTestTemplate<IGnss_V2_1>::StopAndClearLocations();
133 }
Yu-Han Yang69f0f8b2022-01-21 13:03:32 -0800134 auto status = aidl_gnss_hal_->stopSvStatus();
135 EXPECT_TRUE(status.isOk());
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -0800136
Yu-Han Yang69f0f8b2022-01-21 13:03:32 -0800137 status = aidl_gnss_hal_->stop();
Yu-Han Yang1afbd5f2021-11-24 16:39:13 -0800138 EXPECT_TRUE(status.isOk());
139
140 /*
141 * Clear notify/waiting counter, allowing up till the timeout after
142 * the last reply for final startup messages to arrive (esp. system
143 * info.)
144 */
145 while (aidl_gnss_cb_->location_cbq_.retrieve(aidl_gnss_cb_->last_location_, TIMEOUT_SEC)) {
146 }
147 aidl_gnss_cb_->location_cbq_.reset();
148}
149
150void GnssHalTest::StartAndCheckLocations(int count) {
151 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
152 // Invoke the super method.
153 return GnssHalTestTemplate<IGnss_V2_1>::StartAndCheckLocations(count);
154 }
155 const int kMinIntervalMsec = 500;
156 const int kLocationTimeoutSubsequentSec = 2;
157 const bool kLowPowerMode = false;
158
159 EXPECT_TRUE(StartAndCheckFirstLocation(kMinIntervalMsec, kLowPowerMode));
160
161 for (int i = 1; i < count; i++) {
162 EXPECT_TRUE(aidl_gnss_cb_->location_cbq_.retrieve(aidl_gnss_cb_->last_location_,
163 kLocationTimeoutSubsequentSec));
164 int locationCalledCount = aidl_gnss_cb_->location_cbq_.calledCount();
165 EXPECT_EQ(locationCalledCount, i + 1);
166 // Don't cause confusion by checking details if no location yet
167 if (locationCalledCount > 0) {
168 // Should be more than 1 location by now, but if not, still don't check first fix speed
169 CheckLocation(aidl_gnss_cb_->last_location_, locationCalledCount > 1);
170 }
171 }
172}
173
174std::list<std::vector<IGnssCallback::GnssSvInfo>> GnssHalTest::convertToAidl(
175 const std::list<hidl_vec<IGnssCallback_2_1::GnssSvInfo>>& sv_info_list) {
176 std::list<std::vector<IGnssCallback::GnssSvInfo>> aidl_sv_info_list;
177 for (const auto& sv_info_vec : sv_info_list) {
178 std::vector<IGnssCallback::GnssSvInfo> aidl_sv_info_vec;
179 for (const auto& sv_info : sv_info_vec) {
180 IGnssCallback::GnssSvInfo aidl_sv_info;
181 aidl_sv_info.svid = sv_info.v2_0.v1_0.svid;
182 aidl_sv_info.constellation =
183 static_cast<GnssConstellationType>(sv_info.v2_0.constellation);
184 aidl_sv_info.cN0Dbhz = sv_info.v2_0.v1_0.cN0Dbhz;
185 aidl_sv_info.basebandCN0DbHz = sv_info.basebandCN0DbHz;
186 aidl_sv_info.elevationDegrees = sv_info.v2_0.v1_0.elevationDegrees;
187 aidl_sv_info.azimuthDegrees = sv_info.v2_0.v1_0.azimuthDegrees;
188 aidl_sv_info.carrierFrequencyHz = (int64_t)sv_info.v2_0.v1_0.carrierFrequencyHz;
189 aidl_sv_info.svFlag = (int)sv_info.v2_0.v1_0.svFlag;
190 aidl_sv_info_vec.push_back(aidl_sv_info);
191 }
192 aidl_sv_info_list.push_back(aidl_sv_info_vec);
193 }
194 return aidl_sv_info_list;
195}
196
197/*
198 * FindStrongFrequentNonGpsSource:
199 *
200 * Search through a GnssSvStatus list for the strongest non-GPS satellite observed enough times
201 *
202 * returns the strongest source,
203 * or a source with constellation == UNKNOWN if none are found sufficient times
204 */
205BlocklistedSource GnssHalTest::FindStrongFrequentNonGpsSource(
206 const std::list<hidl_vec<IGnssCallback_2_1::GnssSvInfo>> sv_info_list,
207 const int min_observations) {
208 return FindStrongFrequentNonGpsSource(convertToAidl(sv_info_list), min_observations);
209}
210
211BlocklistedSource GnssHalTest::FindStrongFrequentNonGpsSource(
212 const std::list<std::vector<IGnssCallback::GnssSvInfo>> sv_info_list,
213 const int min_observations) {
214 std::map<ComparableBlocklistedSource, SignalCounts> mapSignals;
215
216 for (const auto& sv_info_vec : sv_info_list) {
217 for (uint32_t iSv = 0; iSv < sv_info_vec.size(); iSv++) {
218 const auto& gnss_sv = sv_info_vec[iSv];
219 if ((gnss_sv.svFlag & (int)IGnssCallback::GnssSvFlags::USED_IN_FIX) &&
220 (gnss_sv.constellation != GnssConstellationType::GPS)) {
221 ComparableBlocklistedSource source;
222 source.id.svid = gnss_sv.svid;
223 source.id.constellation = gnss_sv.constellation;
224
225 const auto& itSignal = mapSignals.find(source);
226 if (itSignal == mapSignals.end()) {
227 SignalCounts counts;
228 counts.observations = 1;
229 counts.max_cn0_dbhz = gnss_sv.cN0Dbhz;
230 mapSignals.insert(
231 std::pair<ComparableBlocklistedSource, SignalCounts>(source, counts));
232 } else {
233 itSignal->second.observations++;
234 if (itSignal->second.max_cn0_dbhz < gnss_sv.cN0Dbhz) {
235 itSignal->second.max_cn0_dbhz = gnss_sv.cN0Dbhz;
236 }
237 }
238 }
239 }
240 }
241
242 float max_cn0_dbhz_with_sufficient_count = 0.;
243 int total_observation_count = 0;
244 int blocklisted_source_count_observation = 0;
245
246 ComparableBlocklistedSource source_to_blocklist; // initializes to zero = UNKNOWN constellation
247 for (auto const& pairSignal : mapSignals) {
248 total_observation_count += pairSignal.second.observations;
249 if ((pairSignal.second.observations >= min_observations) &&
250 (pairSignal.second.max_cn0_dbhz > max_cn0_dbhz_with_sufficient_count)) {
251 source_to_blocklist = pairSignal.first;
252 blocklisted_source_count_observation = pairSignal.second.observations;
253 max_cn0_dbhz_with_sufficient_count = pairSignal.second.max_cn0_dbhz;
254 }
255 }
256 ALOGD("Among %d observations, chose svid %d, constellation %d, "
257 "with %d observations at %.1f max CNo",
258 total_observation_count, source_to_blocklist.id.svid,
259 (int)source_to_blocklist.id.constellation, blocklisted_source_count_observation,
260 max_cn0_dbhz_with_sufficient_count);
261
262 return source_to_blocklist.id;
263}
264
265GnssConstellationType GnssHalTest::startLocationAndGetNonGpsConstellation(
266 const int locations_to_await, const int gnss_sv_info_list_timeout) {
267 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
268 return static_cast<GnssConstellationType>(
269 GnssHalTestTemplate<IGnss_V2_1>::startLocationAndGetNonGpsConstellation(
270 locations_to_await, gnss_sv_info_list_timeout));
271 }
272 aidl_gnss_cb_->location_cbq_.reset();
273 StartAndCheckLocations(locations_to_await);
274 const int location_called_count = aidl_gnss_cb_->location_cbq_.calledCount();
275
276 // Tolerate 1 less sv status to handle edge cases in reporting.
277 int sv_info_list_cbq_size = aidl_gnss_cb_->sv_info_list_cbq_.size();
278 EXPECT_GE(sv_info_list_cbq_size + 1, locations_to_await);
279 ALOGD("Observed %d GnssSvInfo, while awaiting %d Locations (%d received)",
280 sv_info_list_cbq_size, locations_to_await, location_called_count);
281
282 // Find first non-GPS constellation to blocklist
283 GnssConstellationType constellation_to_blocklist = GnssConstellationType::UNKNOWN;
284 for (int i = 0; i < sv_info_list_cbq_size; ++i) {
285 std::vector<IGnssCallback::GnssSvInfo> sv_info_vec;
286 aidl_gnss_cb_->sv_info_list_cbq_.retrieve(sv_info_vec, gnss_sv_info_list_timeout);
287 for (uint32_t iSv = 0; iSv < sv_info_vec.size(); iSv++) {
288 auto& gnss_sv = sv_info_vec[iSv];
289 if ((gnss_sv.svFlag & (uint32_t)IGnssCallback::GnssSvFlags::USED_IN_FIX) &&
290 (gnss_sv.constellation != GnssConstellationType::UNKNOWN) &&
291 (gnss_sv.constellation != GnssConstellationType::GPS)) {
292 // found a non-GPS constellation
293 constellation_to_blocklist = gnss_sv.constellation;
294 break;
295 }
296 }
297 if (constellation_to_blocklist != GnssConstellationType::UNKNOWN) {
298 break;
299 }
300 }
301
302 if (constellation_to_blocklist == GnssConstellationType::UNKNOWN) {
303 ALOGI("No non-GPS constellations found, constellation blocklist test less effective.");
304 // Proceed functionally to blocklist something.
305 constellation_to_blocklist = GnssConstellationType::GLONASS;
306 }
307
308 return constellation_to_blocklist;
Yu-Han Yang1e1a6762020-09-30 17:01:53 -0700309}