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 <gnss_hal_test.h>
20 #include <chrono>
21 #include "Utils.h"
22
23 using ::android::hardware::gnss::common::Utils;
24
25 // Implementations for the main test class for GNSS HAL
SetUp()26 void GnssHalTest::SetUp() {
27 gnss_hal_ = ::testing::VtsHalHidlTargetTestBase::getService<IGnss>(
28 GnssHidlEnvironment::Instance()->getServiceName<IGnss>());
29 ASSERT_NE(gnss_hal_, nullptr);
30
31 SetUpGnssCallback();
32 }
33
TearDown()34 void GnssHalTest::TearDown() {
35 if (gnss_hal_ != nullptr) {
36 gnss_hal_->cleanup();
37 gnss_hal_ = nullptr;
38 }
39
40 // Set to nullptr to destruct the callback event queues and warn of any unprocessed events.
41 gnss_cb_ = nullptr;
42 }
43
SetUpGnssCallback()44 void GnssHalTest::SetUpGnssCallback() {
45 gnss_cb_ = new GnssCallback();
46 ASSERT_NE(gnss_cb_, nullptr);
47
48 auto result = gnss_hal_->setCallback_2_0(gnss_cb_);
49 if (!result.isOk()) {
50 ALOGE("result of failed setCallback %s", result.description().c_str());
51 }
52
53 ASSERT_TRUE(result.isOk());
54 ASSERT_TRUE(result);
55
56 /*
57 * All capabilities, name and systemInfo callbacks should trigger
58 */
59 EXPECT_TRUE(gnss_cb_->capabilities_cbq_.retrieve(gnss_cb_->last_capabilities_, TIMEOUT_SEC));
60 EXPECT_TRUE(gnss_cb_->info_cbq_.retrieve(gnss_cb_->last_info_, TIMEOUT_SEC));
61 EXPECT_TRUE(gnss_cb_->name_cbq_.retrieve(gnss_cb_->last_name_, TIMEOUT_SEC));
62
63 EXPECT_EQ(gnss_cb_->capabilities_cbq_.calledCount(), 1);
64 EXPECT_EQ(gnss_cb_->info_cbq_.calledCount(), 1);
65 EXPECT_EQ(gnss_cb_->name_cbq_.calledCount(), 1);
66 }
67
StopAndClearLocations()68 void GnssHalTest::StopAndClearLocations() {
69 const auto result = gnss_hal_->stop();
70
71 EXPECT_TRUE(result.isOk());
72 EXPECT_TRUE(result);
73
74 /*
75 * Clear notify/waiting counter, allowing up till the timeout after
76 * the last reply for final startup messages to arrive (esp. system
77 * info.)
78 */
79 while (gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_, TIMEOUT_SEC)) {
80 }
81 gnss_cb_->location_cbq_.reset();
82 }
83
SetPositionMode(const int min_interval_msec,const bool low_power_mode)84 void GnssHalTest::SetPositionMode(const int min_interval_msec, const bool low_power_mode) {
85 const int kPreferredAccuracy = 0; // Ideally perfect (matches GnssLocationProvider)
86 const int kPreferredTimeMsec = 0; // Ideally immediate
87
88 const auto result = gnss_hal_->setPositionMode_1_1(
89 IGnss::GnssPositionMode::MS_BASED, IGnss::GnssPositionRecurrence::RECURRENCE_PERIODIC,
90 min_interval_msec, kPreferredAccuracy, kPreferredTimeMsec, low_power_mode);
91
92 ASSERT_TRUE(result.isOk());
93 EXPECT_TRUE(result);
94 }
95
StartAndCheckFirstLocation()96 bool GnssHalTest::StartAndCheckFirstLocation() {
97 const auto result = gnss_hal_->start();
98
99 EXPECT_TRUE(result.isOk());
100 EXPECT_TRUE(result);
101
102 /*
103 * GnssLocationProvider support of AGPS SUPL & XtraDownloader is not available in VTS,
104 * so allow time to demodulate ephemeris over the air.
105 */
106 const int kFirstGnssLocationTimeoutSeconds = 75;
107
108 EXPECT_TRUE(gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_,
109 kFirstGnssLocationTimeoutSeconds));
110 int locationCalledCount = gnss_cb_->location_cbq_.calledCount();
111 EXPECT_EQ(locationCalledCount, 1);
112
113 if (locationCalledCount > 0) {
114 // don't require speed on first fix
115 CheckLocation(gnss_cb_->last_location_, false);
116 return true;
117 }
118 return false;
119 }
120
CheckLocation(const GnssLocation_2_0 & location,bool check_speed)121 void GnssHalTest::CheckLocation(const GnssLocation_2_0& location, bool check_speed) {
122 const bool check_more_accuracies =
123 (gnss_cb_->info_cbq_.calledCount() > 0 && gnss_cb_->last_info_.yearOfHw >= 2017);
124
125 Utils::checkLocation(location.v1_0, check_speed, check_more_accuracies);
126 }
127
StartAndCheckLocations(int count)128 void GnssHalTest::StartAndCheckLocations(int count) {
129 const int kMinIntervalMsec = 500;
130 const int kLocationTimeoutSubsequentSec = 2;
131 const bool kLowPowerMode = false;
132
133 SetPositionMode(kMinIntervalMsec, kLowPowerMode);
134
135 EXPECT_TRUE(StartAndCheckFirstLocation());
136
137 for (int i = 1; i < count; i++) {
138 EXPECT_TRUE(gnss_cb_->location_cbq_.retrieve(gnss_cb_->last_location_,
139 kLocationTimeoutSubsequentSec));
140 int locationCalledCount = gnss_cb_->location_cbq_.calledCount();
141 EXPECT_EQ(locationCalledCount, i + 1);
142 // Don't cause confusion by checking details if no location yet
143 if (locationCalledCount > 0) {
144 // Should be more than 1 location by now, but if not, still don't check first fix speed
145 CheckLocation(gnss_cb_->last_location_, locationCalledCount > 1);
146 }
147 }
148 }
149
GnssCallback()150 GnssHalTest::GnssCallback::GnssCallback()
151 : info_cbq_("system_info"),
152 name_cbq_("name"),
153 capabilities_cbq_("capabilities"),
154 location_cbq_("location"),
155 sv_info_cbq_("sv_info") {}
156
gnssSetSystemInfoCb(const IGnssCallback_1_0::GnssSystemInfo & info)157 Return<void> GnssHalTest::GnssCallback::gnssSetSystemInfoCb(
158 const IGnssCallback_1_0::GnssSystemInfo& info) {
159 ALOGI("Info received, year %d", info.yearOfHw);
160 info_cbq_.store(info);
161 return Void();
162 }
163
gnssSetCapabilitesCb(uint32_t capabilities)164 Return<void> GnssHalTest::GnssCallback::gnssSetCapabilitesCb(uint32_t capabilities) {
165 ALOGI("Capabilities received %d", capabilities);
166 capabilities_cbq_.store(capabilities);
167 return Void();
168 }
169
gnssSetCapabilitiesCb_2_0(uint32_t capabilities)170 Return<void> GnssHalTest::GnssCallback::gnssSetCapabilitiesCb_2_0(uint32_t capabilities) {
171 ALOGI("Capabilities (v2.0) received %d", capabilities);
172 capabilities_cbq_.store(capabilities);
173 return Void();
174 }
175
gnssNameCb(const android::hardware::hidl_string & name)176 Return<void> GnssHalTest::GnssCallback::gnssNameCb(const android::hardware::hidl_string& name) {
177 ALOGI("Name received: %s", name.c_str());
178 name_cbq_.store(name);
179 return Void();
180 }
181
gnssLocationCb(const GnssLocation_1_0 & location)182 Return<void> GnssHalTest::GnssCallback::gnssLocationCb(const GnssLocation_1_0& location) {
183 ALOGI("Location received");
184 GnssLocation_2_0 location_v2_0;
185 location_v2_0.v1_0 = location;
186 return gnssLocationCbImpl(location_v2_0);
187 }
188
gnssLocationCb_2_0(const GnssLocation_2_0 & location)189 Return<void> GnssHalTest::GnssCallback::gnssLocationCb_2_0(const GnssLocation_2_0& location) {
190 ALOGI("Location (v2.0) received");
191 return gnssLocationCbImpl(location);
192 }
193
gnssLocationCbImpl(const GnssLocation_2_0 & location)194 Return<void> GnssHalTest::GnssCallback::gnssLocationCbImpl(const GnssLocation_2_0& location) {
195 location_cbq_.store(location);
196 return Void();
197 }
198
gnssSvStatusCb(const IGnssCallback_1_0::GnssSvStatus &)199 Return<void> GnssHalTest::GnssCallback::gnssSvStatusCb(const IGnssCallback_1_0::GnssSvStatus&) {
200 ALOGI("gnssSvStatusCb");
201 return Void();
202 }
203
gnssSvStatusCb_2_0(const hidl_vec<IGnssCallback_2_0::GnssSvInfo> & svInfoList)204 Return<void> GnssHalTest::GnssCallback::gnssSvStatusCb_2_0(
205 const hidl_vec<IGnssCallback_2_0::GnssSvInfo>& svInfoList) {
206 ALOGI("gnssSvStatusCb_2_0. Size = %d", (int)svInfoList.size());
207 sv_info_cbq_.store(svInfoList);
208 return Void();
209 }
210
gnssMeasurementCb_2_0(const IGnssMeasurementCallback_2_0::GnssData & data)211 Return<void> GnssHalTest::GnssMeasurementCallback::gnssMeasurementCb_2_0(
212 const IGnssMeasurementCallback_2_0::GnssData& data) {
213 ALOGD("GnssMeasurement received. Size = %d", (int)data.measurements.size());
214 measurement_cbq_.store(data);
215 return Void();
216 }
217
setCapabilitiesCb(uint32_t capabilities)218 Return<void> GnssHalTest::GnssMeasurementCorrectionsCallback::setCapabilitiesCb(
219 uint32_t capabilities) {
220 ALOGI("GnssMeasurementCorrectionsCallback capabilities received %d", capabilities);
221 capabilities_cbq_.store(capabilities);
222 return Void();
223 }
224