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 #ifndef GNSS_HAL_TEST_H_
18 #define GNSS_HAL_TEST_H_
19 
20 #include <android/hardware/gnss/2.0/IGnss.h>
21 #include "GnssCallbackEventQueue.h"
22 
23 #include <gtest/gtest.h>
24 
25 using android::hardware::hidl_vec;
26 using android::hardware::Return;
27 using android::hardware::Void;
28 
29 using android::hardware::gnss::common::GnssCallbackEventQueue;
30 using android::hardware::gnss::measurement_corrections::V1_0::IMeasurementCorrectionsCallback;
31 using android::hardware::gnss::V1_0::GnssLocationFlags;
32 using android::hardware::gnss::V2_0::IGnss;
33 
34 using GnssConstellationType_1_0 = android::hardware::gnss::V1_0::GnssConstellationType;
35 using GnssConstellationType_2_0 = android::hardware::gnss::V2_0::GnssConstellationType;
36 
37 using GnssLocation_1_0 = android::hardware::gnss::V1_0::GnssLocation;
38 using GnssLocation_2_0 = android::hardware::gnss::V2_0::GnssLocation;
39 
40 using IGnssCallback_1_0 = android::hardware::gnss::V1_0::IGnssCallback;
41 using IGnssCallback_2_0 = android::hardware::gnss::V2_0::IGnssCallback;
42 
43 using IGnssMeasurementCallback_1_0 = android::hardware::gnss::V1_0::IGnssMeasurementCallback;
44 using IGnssMeasurementCallback_1_1 = android::hardware::gnss::V1_1::IGnssMeasurementCallback;
45 using IGnssMeasurementCallback_2_0 = android::hardware::gnss::V2_0::IGnssMeasurementCallback;
46 
47 using android::sp;
48 
49 #define TIMEOUT_SEC 2  // for basic commands/responses
50 
51 // The main test class for GNSS HAL.
52 class GnssHalTest : public testing::TestWithParam<std::string> {
53   public:
54     virtual void SetUp() override;
55 
56     virtual void TearDown() override;
57 
58     /* Callback class for data & Event. */
59     class GnssCallback : public IGnssCallback_2_0 {
60       public:
61         IGnssCallback_1_0::GnssSystemInfo last_info_;
62         android::hardware::hidl_string last_name_;
63         uint32_t last_capabilities_;
64         GnssLocation_2_0 last_location_;
65 
66         GnssCallbackEventQueue<IGnssCallback_1_0::GnssSystemInfo> info_cbq_;
67         GnssCallbackEventQueue<android::hardware::hidl_string> name_cbq_;
68         GnssCallbackEventQueue<uint32_t> capabilities_cbq_;
69         GnssCallbackEventQueue<GnssLocation_2_0> location_cbq_;
70         GnssCallbackEventQueue<hidl_vec<IGnssCallback_2_0::GnssSvInfo>> sv_info_list_cbq_;
71 
72         GnssCallback();
73         virtual ~GnssCallback() = default;
74 
75         // Dummy callback handlers
gnssStatusCb(const IGnssCallback_1_0::GnssStatusValue)76         Return<void> gnssStatusCb(const IGnssCallback_1_0::GnssStatusValue /* status */) override {
77             return Void();
78         }
gnssNmeaCb(int64_t,const android::hardware::hidl_string &)79         Return<void> gnssNmeaCb(int64_t /* timestamp */,
80                                 const android::hardware::hidl_string& /* nmea */) override {
81             return Void();
82         }
gnssAcquireWakelockCb()83         Return<void> gnssAcquireWakelockCb() override { return Void(); }
gnssReleaseWakelockCb()84         Return<void> gnssReleaseWakelockCb() override { return Void(); }
gnssRequestLocationCb(bool)85         Return<void> gnssRequestLocationCb(bool /* independentFromGnss */) override {
86             return Void();
87         }
gnssRequestTimeCb()88         Return<void> gnssRequestTimeCb() override { return Void(); }
89         // Actual (test) callback handlers
90         Return<void> gnssNameCb(const android::hardware::hidl_string& name) override;
91         Return<void> gnssLocationCb(const GnssLocation_1_0& location) override;
92         Return<void> gnssSetCapabilitesCb(uint32_t capabilities) override;
93         Return<void> gnssSetSystemInfoCb(const IGnssCallback_1_0::GnssSystemInfo& info) override;
94         Return<void> gnssSvStatusCb(const IGnssCallback_1_0::GnssSvStatus& svStatus) override;
95 
96         // New in v2.0
97         Return<void> gnssLocationCb_2_0(const GnssLocation_2_0& location) override;
gnssRequestLocationCb_2_0(bool,bool)98         Return<void> gnssRequestLocationCb_2_0(bool /* independentFromGnss */,
99                                                bool /* isUserEmergency */) override {
100             return Void();
101         }
102         Return<void> gnssSetCapabilitiesCb_2_0(uint32_t capabilities) override;
103         Return<void> gnssSvStatusCb_2_0(
104                 const hidl_vec<IGnssCallback_2_0::GnssSvInfo>& svInfoList) override;
105 
106       private:
107         Return<void> gnssLocationCbImpl(const GnssLocation_2_0& location);
108     };
109 
110     /* Callback class for GnssMeasurement. */
111     class GnssMeasurementCallback : public IGnssMeasurementCallback_2_0 {
112       public:
113         GnssCallbackEventQueue<IGnssMeasurementCallback_2_0::GnssData> measurement_cbq_;
114 
GnssMeasurementCallback()115         GnssMeasurementCallback() : measurement_cbq_("measurement"){};
116         virtual ~GnssMeasurementCallback() = default;
117 
118         // Methods from V1_0::IGnssMeasurementCallback follow.
GnssMeasurementCb(const IGnssMeasurementCallback_1_0::GnssData &)119         Return<void> GnssMeasurementCb(const IGnssMeasurementCallback_1_0::GnssData&) override {
120             return Void();
121         }
122 
123         // Methods from V1_1::IGnssMeasurementCallback follow.
gnssMeasurementCb(const IGnssMeasurementCallback_1_1::GnssData &)124         Return<void> gnssMeasurementCb(const IGnssMeasurementCallback_1_1::GnssData&) override {
125             return Void();
126         }
127 
128         // Methods from V2_0::IGnssMeasurementCallback follow.
129         Return<void> gnssMeasurementCb_2_0(const IGnssMeasurementCallback_2_0::GnssData&) override;
130     };
131 
132     /* Callback class for GnssMeasurementCorrections. */
133     class GnssMeasurementCorrectionsCallback : public IMeasurementCorrectionsCallback {
134       public:
135         uint32_t last_capabilities_;
136         GnssCallbackEventQueue<uint32_t> capabilities_cbq_;
137 
GnssMeasurementCorrectionsCallback()138         GnssMeasurementCorrectionsCallback() : capabilities_cbq_("capabilities"){};
139         virtual ~GnssMeasurementCorrectionsCallback() = default;
140 
141         // Methods from V1_0::IMeasurementCorrectionsCallback follow.
142         Return<void> setCapabilitiesCb(uint32_t capabilities) override;
143     };
144 
145     /*
146      * SetUpGnssCallback:
147      *   Set GnssCallback and verify the result.
148      */
149     void SetUpGnssCallback();
150 
151     /*
152      * StartAndCheckFirstLocation:
153      *   Helper function to start location, and check the first one.
154      *
155      *   <p> Note this leaves the Location request active, to enable Stop call vs. other call
156      *   reordering tests.
157      *
158      *   <p> if 'strict' is true, the test will fail if no location is generated.
159      *
160      * returns  true if a location was successfully generated
161      */
162     bool StartAndCheckFirstLocation(const bool strict, const int min_interval_msec,
163                                     const bool low_power_mode);
164 
165     /*
166      * CheckLocation:
167      *   Helper function to vet Location fields
168      *
169      *   check_speed: true if speed related fields are also verified.
170      */
171     void CheckLocation(const GnssLocation_2_0& location, const bool check_speed);
172 
173     /*
174      * StartAndCheckLocations:
175      *   Helper function to collect, and check a number of
176      *   normal ~1Hz locations.
177      *
178      *   Note this leaves the Location request active, to enable Stop call vs. other call
179      *   reordering tests.
180      */
181     void StartAndCheckLocations(int count);
182 
183     /*
184      * StopAndClearLocations:
185      * Helper function to stop locations, and clear any remaining notifications
186      */
187     void StopAndClearLocations();
188 
189     /*
190      * IsGnssHalVersion_2_0:
191      * returns  true if the GNSS HAL version is exactly 2.0.
192      */
193     bool IsGnssHalVersion_2_0() const;
194 
195     /*
196      * SetPositionMode:
197      * Helper function to set positioning mode and verify output
198      */
199     void SetPositionMode(const int min_interval_msec, const bool low_power_mode);
200 
201     /*
202      * startLocationAndGetNonGpsConstellation:
203      * 1. Start location
204      * 2. Find and return first non-GPS constellation
205      *
206      * Note that location is not stopped in this method. The client should call
207      * StopAndClearLocations() after the call.
208      */
209     GnssConstellationType_1_0 startLocationAndGetNonGpsConstellation();
210 
211     sp<IGnss> gnss_hal_;         // GNSS HAL to call into
212     sp<GnssCallback> gnss_cb_;   // Primary callback interface
213 };
214 
215 #endif  // GNSS_HAL_TEST_H_
216