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 #ifndef GNSS_HAL_TEST_H_
18 #define GNSS_HAL_TEST_H_
19 
20 #include <android/hardware/gnss/1.1/IGnss.h>
21 
22 #include <gtest/gtest.h>
23 #include "GnssCallbackEventQueue.h"
24 
25 using android::hardware::Return;
26 using android::hardware::Void;
27 
28 using android::hardware::gnss::V1_0::GnssLocation;
29 
30 using android::hardware::gnss::common::GnssCallbackEventQueue;
31 using android::hardware::gnss::V1_0::GnssConstellationType;
32 using android::hardware::gnss::V1_0::GnssLocationFlags;
33 using android::hardware::gnss::V1_1::IGnss;
34 using android::hardware::gnss::V1_1::IGnssCallback;
35 
36 using android::sp;
37 
38 #define TIMEOUT_SEC 2  // for basic commands/responses
39 
40 // The main test class for GNSS HAL.
41 class GnssHalTest : public testing::TestWithParam<std::string> {
42   public:
43     virtual void SetUp() override;
44 
45     virtual void TearDown() override;
46 
47     /* Used as a mechanism to inform the test that a callback has occurred */
48     void notify();
49 
50     /* Test code calls this function to wait for a callback */
51     std::cv_status wait(int timeout_seconds);
52 
53     /* Callback class for data & Event. */
54     class GnssCallback : public IGnssCallback {
55        public:
56          IGnssCallback::GnssSystemInfo last_info_;
57          android::hardware::hidl_string last_name_;
58          uint32_t last_capabilities_;
59          GnssLocation last_location_;
60 
61          GnssCallbackEventQueue<IGnssCallback::GnssSystemInfo> info_cbq_;
62          GnssCallbackEventQueue<android::hardware::hidl_string> name_cbq_;
63          GnssCallbackEventQueue<uint32_t> capabilities_cbq_;
64          GnssCallbackEventQueue<GnssLocation> location_cbq_;
65          GnssCallbackEventQueue<IGnssCallback::GnssSvStatus> sv_status_cbq_;
66 
67          GnssCallback();
68          virtual ~GnssCallback() = default;
69 
70          // Dummy callback handlers
gnssStatusCb(const IGnssCallback::GnssStatusValue)71          Return<void> gnssStatusCb(const IGnssCallback::GnssStatusValue /* status */) override {
72              return Void();
73          }
gnssNmeaCb(int64_t,const android::hardware::hidl_string &)74          Return<void> gnssNmeaCb(int64_t /* timestamp */,
75                                  const android::hardware::hidl_string& /* nmea */) override {
76              return Void();
77          }
gnssAcquireWakelockCb()78          Return<void> gnssAcquireWakelockCb() override { return Void(); }
gnssReleaseWakelockCb()79          Return<void> gnssReleaseWakelockCb() override { return Void(); }
gnssRequestLocationCb(bool)80          Return<void> gnssRequestLocationCb(bool /* independentFromGnss */) override {
81              return Void();
82          }
gnssRequestTimeCb()83          Return<void> gnssRequestTimeCb() override { return Void(); }
84          // Actual (test) callback handlers
85          Return<void> gnssNameCb(const android::hardware::hidl_string& name) override;
86          Return<void> gnssLocationCb(const GnssLocation& location) override;
87          Return<void> gnssSetCapabilitesCb(uint32_t capabilities) override;
88          Return<void> gnssSetSystemInfoCb(const IGnssCallback::GnssSystemInfo& info) override;
89          Return<void> gnssSvStatusCb(const IGnssCallback::GnssSvStatus& svStatus) override;
90     };
91 
92     /*
93      * SetUpGnssCallback:
94      *   Set GnssCallback and verify the result.
95      */
96     void SetUpGnssCallback();
97 
98     /*
99      * StartAndCheckFirstLocation:
100      *   Helper function to start location, and check the first one.
101      *
102      *   <p> Note this leaves the Location request active, to enable Stop call vs. other call
103      *   reordering tests.
104      *
105      *   <p> if 'strict' is true, the test will fail if no location is generated.
106      *
107      * returns  true if a location was successfully generated
108      */
109     bool StartAndCheckFirstLocation(const bool strict, const int min_interval_msec,
110                                     const bool low_power_mode);
111 
112     /*
113      * CheckLocation:
114      *   Helper function to vet Location fields
115      */
116     void CheckLocation(GnssLocation& location, bool check_speed);
117 
118     /*
119      * StartAndCheckLocations:
120      *   Helper function to collect, and check a number of
121      *   normal ~1Hz locations.
122      *
123      *   Note this leaves the Location request active, to enable Stop call vs. other call
124      *   reordering tests.
125      */
126     void StartAndCheckLocations(int count);
127 
128     /*
129      * StopAndClearLocations:
130      * Helper function to stop locations, and clear any remaining notifications
131      */
132     void StopAndClearLocations();
133 
134     /*
135      * SetPositionMode:
136      * Helper function to set positioning mode and verify output
137      */
138     void SetPositionMode(const int min_interval_msec, const bool low_power_mode);
139 
140     /*
141      * IsGnssHalVersion_1_1:
142      * returns  true if the GNSS HAL version is exactly 1.1.
143      */
144     bool IsGnssHalVersion_1_1() const;
145 
146     /*
147      * startLocationAndGetNonGpsConstellation:
148      * 1. Start location
149      * 2. Find and return first non-GPS constellation
150      *
151      * Note that location is not stopped in this method. The client should call
152      * StopAndClearLocations() after the call.
153      */
154     GnssConstellationType startLocationAndGetNonGpsConstellation(
155             const int locations_to_await, const int gnss_sv_info_list_timeout);
156 
157     sp<IGnss> gnss_hal_;         // GNSS HAL to call into
158     sp<GnssCallback> gnss_cb_;   // Primary callback interface
159 };
160 
161 #endif  // GNSS_HAL_TEST_H_
162