1 /*
2 * Copyright (C) 2017 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 <gtest/gtest.h>
21 #include <hidl/GtestPrinter.h>
22 #include <hidl/ServiceManagement.h>
23
24 #include <gnss_hal_test.h>
25 #include <chrono>
26 #include "Utils.h"
27
28 using ::android::hardware::hidl_string;
29 using ::android::hardware::hidl_vec;
30
31 using ::android::hardware::gnss::common::Utils;
32
SetUp()33 void GnssHalTest::SetUp() {
34 gnss_hal_ = IGnss::getService(GetParam());
35 ASSERT_NE(gnss_hal_, nullptr);
36
37 SetUpGnssCallback();
38 }
39
TearDown()40 void GnssHalTest::TearDown() {
41 if (gnss_hal_ != nullptr) {
42 gnss_hal_->cleanup();
43 gnss_hal_ = nullptr;
44 }
45
46 // Set to nullptr to destruct the callback event queues and warn of any unprocessed events.
47 gnss_cb_ = nullptr;
48 }
49
SetUpGnssCallback()50 void GnssHalTest::SetUpGnssCallback() {
51 gnss_cb_ = new GnssCallback();
52 ASSERT_NE(gnss_cb_, nullptr);
53
54 auto result = gnss_hal_->setCallback_1_1(gnss_cb_);
55 if (!result.isOk()) {
56 ALOGE("result of failed setCallback %s", result.description().c_str());
57 }
58
59 ASSERT_TRUE(result.isOk());
60 ASSERT_TRUE(result);
61
62 /*
63 * All capabilities, name and systemInfo callbacks should trigger
64 */
65 EXPECT_TRUE(gnss_cb_->capabilities_cbq_.retrieve(gnss_cb_->last_capabilities_, TIMEOUT_SEC));
66 EXPECT_TRUE(gnss_cb_->info_cbq_.retrieve(gnss_cb_->last_info_, TIMEOUT_SEC));
67 EXPECT_TRUE(gnss_cb_->name_cbq_.retrieve(gnss_cb_->last_name_, TIMEOUT_SEC));
68
69 EXPECT_EQ(gnss_cb_->capabilities_cbq_.calledCount(), 1);
70 EXPECT_EQ(gnss_cb_->info_cbq_.calledCount(), 1);
71 EXPECT_EQ(gnss_cb_->name_cbq_.calledCount(), 1);
72 }
73
StopAndClearLocations()74 void GnssHalTest::StopAndClearLocations() {
75 auto result = gnss_hal_->stop();
76
77 EXPECT_TRUE(result.isOk());
78 EXPECT_TRUE(result);
79
80 /*
81 * Clear notify/waiting counter, allowing up till the timeout after
82 * the last reply for final startup messages to arrive (esp. system
83 * info.)
84 */
85 while (gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_, TIMEOUT_SEC)) {
86 }
87 gnss_cb_->location_cbq_.reset();
88 }
89
SetPositionMode(const int min_interval_msec,const bool low_power_mode)90 void GnssHalTest::SetPositionMode(const int min_interval_msec, const bool low_power_mode) {
91 const int kPreferredAccuracy = 0; // Ideally perfect (matches GnssLocationProvider)
92 const int kPreferredTimeMsec = 0; // Ideally immediate
93
94 auto result = gnss_hal_->setPositionMode_1_1(
95 IGnss::GnssPositionMode::MS_BASED, IGnss::GnssPositionRecurrence::RECURRENCE_PERIODIC,
96 min_interval_msec, kPreferredAccuracy, kPreferredTimeMsec, low_power_mode);
97
98 ASSERT_TRUE(result.isOk());
99 EXPECT_TRUE(result);
100 }
101
StartAndCheckFirstLocation(bool strict)102 bool GnssHalTest::StartAndCheckFirstLocation(bool strict) {
103 auto result = gnss_hal_->start();
104
105 EXPECT_TRUE(result.isOk());
106 EXPECT_TRUE(result);
107
108 /*
109 * GnssLocationProvider support of AGPS SUPL & XtraDownloader is not available in VTS,
110 * so allow time to demodulate ephemeris over the air.
111 */
112 const int kFirstGnssLocationTimeoutSeconds = 75;
113 int locationCalledCount = 0;
114
115 if (strict) {
116 EXPECT_TRUE(gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_,
117 kFirstGnssLocationTimeoutSeconds));
118 locationCalledCount = gnss_cb_->location_cbq_.calledCount();
119 EXPECT_EQ(locationCalledCount, 1);
120 }
121
122 if (locationCalledCount > 0) {
123 // don't require speed on first fix
124 CheckLocation(gnss_cb_->last_location_, false);
125 return true;
126 }
127 return false;
128 }
129
CheckLocation(GnssLocation & location,bool check_speed)130 void GnssHalTest::CheckLocation(GnssLocation& location, bool check_speed) {
131 const bool check_more_accuracies =
132 (gnss_cb_->info_cbq_.calledCount() > 0 && gnss_cb_->last_info_.yearOfHw >= 2017);
133
134 Utils::checkLocation(location, check_speed, check_more_accuracies);
135 }
136
StartAndCheckLocations(int count)137 void GnssHalTest::StartAndCheckLocations(int count) {
138 const int kMinIntervalMsec = 500;
139 const int kLocationTimeoutSubsequentSec = 2;
140 const bool kLowPowerMode = false;
141
142 SetPositionMode(kMinIntervalMsec, kLowPowerMode);
143
144 EXPECT_TRUE(StartAndCheckFirstLocation(/* strict= */ true));
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_1_1() const159 bool GnssHalTest::IsGnssHalVersion_1_1() const {
160 using ::android::hidl::manager::V1_2::IServiceManager;
161 sp<IServiceManager> manager = ::android::hardware::defaultServiceManager1_2();
162
163 bool hasGnssHalVersion_1_1 = false;
164 manager->listManifestByInterface(
165 "android.hardware.gnss@1.1::IGnss",
166 [&hasGnssHalVersion_1_1](const hidl_vec<hidl_string>& registered) {
167 ASSERT_EQ(1, registered.size());
168 hasGnssHalVersion_1_1 = true;
169 });
170
171 bool hasGnssHalVersion_2_0 = false;
172 manager->listManifestByInterface(
173 "android.hardware.gnss@2.0::IGnss",
174 [&hasGnssHalVersion_2_0](const hidl_vec<hidl_string>& registered) {
175 hasGnssHalVersion_2_0 = registered.size() != 0;
176 });
177
178 bool hasGnssHalVersion_2_1 = false;
179 manager->listManifestByInterface(
180 "android.hardware.gnss@2.1::IGnss",
181 [&hasGnssHalVersion_2_1](const hidl_vec<hidl_string>& registered) {
182 hasGnssHalVersion_2_1 = registered.size() != 0;
183 });
184
185 return hasGnssHalVersion_1_1 && !hasGnssHalVersion_2_0 && !hasGnssHalVersion_2_1;
186 }
187
startLocationAndGetNonGpsConstellation(const int locations_to_await,const int gnss_sv_info_list_timeout)188 GnssConstellationType GnssHalTest::startLocationAndGetNonGpsConstellation(
189 const int locations_to_await, const int gnss_sv_info_list_timeout) {
190 gnss_cb_->location_cbq_.reset();
191 StartAndCheckLocations(locations_to_await);
192 const int location_called_count = gnss_cb_->location_cbq_.calledCount();
193
194 // Tolerate 1 less sv status to handle edge cases in reporting.
195 int sv_status_cbq_size = gnss_cb_->sv_status_cbq_.size();
196 EXPECT_GE(sv_status_cbq_size + 1, locations_to_await);
197 ALOGD("Observed %d GnssSvStatus, while awaiting %d Locations (%d received)", sv_status_cbq_size,
198 locations_to_await, location_called_count);
199
200 // Find first non-GPS constellation to blacklist
201 GnssConstellationType constellation_to_blacklist = GnssConstellationType::UNKNOWN;
202 for (int i = 0; i < sv_status_cbq_size; ++i) {
203 IGnssCallback::GnssSvStatus gnss_sv_status;
204 gnss_cb_->sv_status_cbq_.retrieve(gnss_sv_status, gnss_sv_info_list_timeout);
205 for (uint32_t iSv = 0; iSv < gnss_sv_status.numSvs; iSv++) {
206 const auto& gnss_sv = gnss_sv_status.gnssSvList[iSv];
207 if ((gnss_sv.svFlag & IGnssCallback::GnssSvFlags::USED_IN_FIX) &&
208 (gnss_sv.constellation != GnssConstellationType::UNKNOWN) &&
209 (gnss_sv.constellation != GnssConstellationType::GPS)) {
210 // found a non-GPS constellation
211 constellation_to_blacklist = gnss_sv.constellation;
212 break;
213 }
214 }
215 if (constellation_to_blacklist != GnssConstellationType::UNKNOWN) {
216 break;
217 }
218 }
219
220 if (constellation_to_blacklist == GnssConstellationType::UNKNOWN) {
221 ALOGI("No non-GPS constellations found, constellation blacklist test less effective.");
222 // Proceed functionally to blacklist something.
223 constellation_to_blacklist = GnssConstellationType::GLONASS;
224 }
225 return constellation_to_blacklist;
226 }
227
GnssCallback()228 GnssHalTest::GnssCallback::GnssCallback()
229 : info_cbq_("system_info"),
230 name_cbq_("name"),
231 capabilities_cbq_("capabilities"),
232 location_cbq_("location"),
233 sv_status_cbq_("sv_status") {}
234
gnssSetSystemInfoCb(const IGnssCallback::GnssSystemInfo & info)235 Return<void> GnssHalTest::GnssCallback::gnssSetSystemInfoCb(
236 const IGnssCallback::GnssSystemInfo& info) {
237 ALOGI("Info received, year %d", info.yearOfHw);
238 info_cbq_.store(info);
239 return Void();
240 }
241
gnssSetCapabilitesCb(uint32_t capabilities)242 Return<void> GnssHalTest::GnssCallback::gnssSetCapabilitesCb(uint32_t capabilities) {
243 ALOGI("Capabilities received %d", capabilities);
244 capabilities_cbq_.store(capabilities);
245 return Void();
246 }
247
gnssNameCb(const android::hardware::hidl_string & name)248 Return<void> GnssHalTest::GnssCallback::gnssNameCb(const android::hardware::hidl_string& name) {
249 ALOGI("Name received: %s", name.c_str());
250 name_cbq_.store(name);
251 return Void();
252 }
253
gnssLocationCb(const GnssLocation & location)254 Return<void> GnssHalTest::GnssCallback::gnssLocationCb(const GnssLocation& location) {
255 ALOGI("Location received");
256 location_cbq_.store(location);
257 return Void();
258 }
259
gnssSvStatusCb(const IGnssCallback::GnssSvStatus & svStatus)260 Return<void> GnssHalTest::GnssCallback::gnssSvStatusCb(
261 const IGnssCallback::GnssSvStatus& svStatus) {
262 ALOGI("GnssSvStatus received");
263 sv_status_cbq_.store(svStatus);
264 return Void();
265 }
266