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