blob: 4828f19f7d0c6fbc9ebb327265dd805f5f5bc6f4 [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);
103 auto result = aidl_gnss_hal_->start();
104
105 EXPECT_TRUE(result.isOk());
106
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
113 EXPECT_TRUE(aidl_gnss_cb_->location_cbq_.retrieve(aidl_gnss_cb_->last_location_,
114 kFirstGnssLocationTimeoutSeconds));
115 int locationCalledCount = aidl_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(aidl_gnss_cb_->last_location_, false);
121 return true;
122 }
123 return false;
124}
125
126void GnssHalTest::StopAndClearLocations() {
127 ALOGD("StopAndClearLocations");
128 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
129 // Invoke the super method.
130 return GnssHalTestTemplate<IGnss_V2_1>::StopAndClearLocations();
131 }
132
133 auto status = aidl_gnss_hal_->stop();
134 EXPECT_TRUE(status.isOk());
135
136 /*
137 * Clear notify/waiting counter, allowing up till the timeout after
138 * the last reply for final startup messages to arrive (esp. system
139 * info.)
140 */
141 while (aidl_gnss_cb_->location_cbq_.retrieve(aidl_gnss_cb_->last_location_, TIMEOUT_SEC)) {
142 }
143 aidl_gnss_cb_->location_cbq_.reset();
144}
145
146void GnssHalTest::StartAndCheckLocations(int count) {
147 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
148 // Invoke the super method.
149 return GnssHalTestTemplate<IGnss_V2_1>::StartAndCheckLocations(count);
150 }
151 const int kMinIntervalMsec = 500;
152 const int kLocationTimeoutSubsequentSec = 2;
153 const bool kLowPowerMode = false;
154
155 EXPECT_TRUE(StartAndCheckFirstLocation(kMinIntervalMsec, kLowPowerMode));
156
157 for (int i = 1; i < count; i++) {
158 EXPECT_TRUE(aidl_gnss_cb_->location_cbq_.retrieve(aidl_gnss_cb_->last_location_,
159 kLocationTimeoutSubsequentSec));
160 int locationCalledCount = aidl_gnss_cb_->location_cbq_.calledCount();
161 EXPECT_EQ(locationCalledCount, i + 1);
162 // Don't cause confusion by checking details if no location yet
163 if (locationCalledCount > 0) {
164 // Should be more than 1 location by now, but if not, still don't check first fix speed
165 CheckLocation(aidl_gnss_cb_->last_location_, locationCalledCount > 1);
166 }
167 }
168}
169
170std::list<std::vector<IGnssCallback::GnssSvInfo>> GnssHalTest::convertToAidl(
171 const std::list<hidl_vec<IGnssCallback_2_1::GnssSvInfo>>& sv_info_list) {
172 std::list<std::vector<IGnssCallback::GnssSvInfo>> aidl_sv_info_list;
173 for (const auto& sv_info_vec : sv_info_list) {
174 std::vector<IGnssCallback::GnssSvInfo> aidl_sv_info_vec;
175 for (const auto& sv_info : sv_info_vec) {
176 IGnssCallback::GnssSvInfo aidl_sv_info;
177 aidl_sv_info.svid = sv_info.v2_0.v1_0.svid;
178 aidl_sv_info.constellation =
179 static_cast<GnssConstellationType>(sv_info.v2_0.constellation);
180 aidl_sv_info.cN0Dbhz = sv_info.v2_0.v1_0.cN0Dbhz;
181 aidl_sv_info.basebandCN0DbHz = sv_info.basebandCN0DbHz;
182 aidl_sv_info.elevationDegrees = sv_info.v2_0.v1_0.elevationDegrees;
183 aidl_sv_info.azimuthDegrees = sv_info.v2_0.v1_0.azimuthDegrees;
184 aidl_sv_info.carrierFrequencyHz = (int64_t)sv_info.v2_0.v1_0.carrierFrequencyHz;
185 aidl_sv_info.svFlag = (int)sv_info.v2_0.v1_0.svFlag;
186 aidl_sv_info_vec.push_back(aidl_sv_info);
187 }
188 aidl_sv_info_list.push_back(aidl_sv_info_vec);
189 }
190 return aidl_sv_info_list;
191}
192
193/*
194 * FindStrongFrequentNonGpsSource:
195 *
196 * Search through a GnssSvStatus list for the strongest non-GPS satellite observed enough times
197 *
198 * returns the strongest source,
199 * or a source with constellation == UNKNOWN if none are found sufficient times
200 */
201BlocklistedSource GnssHalTest::FindStrongFrequentNonGpsSource(
202 const std::list<hidl_vec<IGnssCallback_2_1::GnssSvInfo>> sv_info_list,
203 const int min_observations) {
204 return FindStrongFrequentNonGpsSource(convertToAidl(sv_info_list), min_observations);
205}
206
207BlocklistedSource GnssHalTest::FindStrongFrequentNonGpsSource(
208 const std::list<std::vector<IGnssCallback::GnssSvInfo>> sv_info_list,
209 const int min_observations) {
210 std::map<ComparableBlocklistedSource, SignalCounts> mapSignals;
211
212 for (const auto& sv_info_vec : sv_info_list) {
213 for (uint32_t iSv = 0; iSv < sv_info_vec.size(); iSv++) {
214 const auto& gnss_sv = sv_info_vec[iSv];
215 if ((gnss_sv.svFlag & (int)IGnssCallback::GnssSvFlags::USED_IN_FIX) &&
216 (gnss_sv.constellation != GnssConstellationType::GPS)) {
217 ComparableBlocklistedSource source;
218 source.id.svid = gnss_sv.svid;
219 source.id.constellation = gnss_sv.constellation;
220
221 const auto& itSignal = mapSignals.find(source);
222 if (itSignal == mapSignals.end()) {
223 SignalCounts counts;
224 counts.observations = 1;
225 counts.max_cn0_dbhz = gnss_sv.cN0Dbhz;
226 mapSignals.insert(
227 std::pair<ComparableBlocklistedSource, SignalCounts>(source, counts));
228 } else {
229 itSignal->second.observations++;
230 if (itSignal->second.max_cn0_dbhz < gnss_sv.cN0Dbhz) {
231 itSignal->second.max_cn0_dbhz = gnss_sv.cN0Dbhz;
232 }
233 }
234 }
235 }
236 }
237
238 float max_cn0_dbhz_with_sufficient_count = 0.;
239 int total_observation_count = 0;
240 int blocklisted_source_count_observation = 0;
241
242 ComparableBlocklistedSource source_to_blocklist; // initializes to zero = UNKNOWN constellation
243 for (auto const& pairSignal : mapSignals) {
244 total_observation_count += pairSignal.second.observations;
245 if ((pairSignal.second.observations >= min_observations) &&
246 (pairSignal.second.max_cn0_dbhz > max_cn0_dbhz_with_sufficient_count)) {
247 source_to_blocklist = pairSignal.first;
248 blocklisted_source_count_observation = pairSignal.second.observations;
249 max_cn0_dbhz_with_sufficient_count = pairSignal.second.max_cn0_dbhz;
250 }
251 }
252 ALOGD("Among %d observations, chose svid %d, constellation %d, "
253 "with %d observations at %.1f max CNo",
254 total_observation_count, source_to_blocklist.id.svid,
255 (int)source_to_blocklist.id.constellation, blocklisted_source_count_observation,
256 max_cn0_dbhz_with_sufficient_count);
257
258 return source_to_blocklist.id;
259}
260
261GnssConstellationType GnssHalTest::startLocationAndGetNonGpsConstellation(
262 const int locations_to_await, const int gnss_sv_info_list_timeout) {
263 if (aidl_gnss_hal_->getInterfaceVersion() == 1) {
264 return static_cast<GnssConstellationType>(
265 GnssHalTestTemplate<IGnss_V2_1>::startLocationAndGetNonGpsConstellation(
266 locations_to_await, gnss_sv_info_list_timeout));
267 }
268 aidl_gnss_cb_->location_cbq_.reset();
269 StartAndCheckLocations(locations_to_await);
270 const int location_called_count = aidl_gnss_cb_->location_cbq_.calledCount();
271
272 // Tolerate 1 less sv status to handle edge cases in reporting.
273 int sv_info_list_cbq_size = aidl_gnss_cb_->sv_info_list_cbq_.size();
274 EXPECT_GE(sv_info_list_cbq_size + 1, locations_to_await);
275 ALOGD("Observed %d GnssSvInfo, while awaiting %d Locations (%d received)",
276 sv_info_list_cbq_size, locations_to_await, location_called_count);
277
278 // Find first non-GPS constellation to blocklist
279 GnssConstellationType constellation_to_blocklist = GnssConstellationType::UNKNOWN;
280 for (int i = 0; i < sv_info_list_cbq_size; ++i) {
281 std::vector<IGnssCallback::GnssSvInfo> sv_info_vec;
282 aidl_gnss_cb_->sv_info_list_cbq_.retrieve(sv_info_vec, gnss_sv_info_list_timeout);
283 for (uint32_t iSv = 0; iSv < sv_info_vec.size(); iSv++) {
284 auto& gnss_sv = sv_info_vec[iSv];
285 if ((gnss_sv.svFlag & (uint32_t)IGnssCallback::GnssSvFlags::USED_IN_FIX) &&
286 (gnss_sv.constellation != GnssConstellationType::UNKNOWN) &&
287 (gnss_sv.constellation != GnssConstellationType::GPS)) {
288 // found a non-GPS constellation
289 constellation_to_blocklist = gnss_sv.constellation;
290 break;
291 }
292 }
293 if (constellation_to_blocklist != GnssConstellationType::UNKNOWN) {
294 break;
295 }
296 }
297
298 if (constellation_to_blocklist == GnssConstellationType::UNKNOWN) {
299 ALOGI("No non-GPS constellations found, constellation blocklist test less effective.");
300 // Proceed functionally to blocklist something.
301 constellation_to_blocklist = GnssConstellationType::GLONASS;
302 }
303
304 return constellation_to_blocklist;
Yu-Han Yang1e1a6762020-09-30 17:01:53 -0700305}