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(bool strict);
110 
111     /*
112      * CheckLocation:
113      *   Helper function to vet Location fields
114      */
115     void CheckLocation(GnssLocation& location, bool check_speed);
116 
117     /*
118      * StartAndCheckLocations:
119      *   Helper function to collect, and check a number of
120      *   normal ~1Hz locations.
121      *
122      *   Note this leaves the Location request active, to enable Stop call vs. other call
123      *   reordering tests.
124      */
125     void StartAndCheckLocations(int count);
126 
127     /*
128      * StopAndClearLocations:
129      * Helper function to stop locations, and clear any remaining notifications
130      */
131     void StopAndClearLocations();
132 
133     /*
134      * SetPositionMode:
135      * Helper function to set positioning mode and verify output
136      */
137     void SetPositionMode(const int min_interval_msec, const bool low_power_mode);
138 
139     /*
140      * IsGnssHalVersion_1_1:
141      * returns  true if the GNSS HAL version is exactly 1.1.
142      */
143     bool IsGnssHalVersion_1_1() const;
144 
145     /*
146      * startLocationAndGetNonGpsConstellation:
147      * 1. Start location
148      * 2. Find and return first non-GPS constellation
149      *
150      * Note that location is not stopped in this method. The client should call
151      * StopAndClearLocations() after the call.
152      */
153     GnssConstellationType startLocationAndGetNonGpsConstellation(
154             const int locations_to_await, const int gnss_sv_info_list_timeout);
155 
156     sp<IGnss> gnss_hal_;         // GNSS HAL to call into
157     sp<GnssCallback> gnss_cb_;   // Primary callback interface
158 };
159 
160 #endif  // GNSS_HAL_TEST_H_
161