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 <VtsHalHidlTargetTestBase.h>
23 #include <VtsHalHidlTargetTestEnvBase.h>
24 
25 #include <condition_variable>
26 #include <list>
27 #include <mutex>
28 
29 using android::hardware::Return;
30 using android::hardware::Void;
31 
32 using android::hardware::gnss::V1_0::GnssLocation;
33 
34 using android::hardware::gnss::V1_1::IGnss;
35 using android::hardware::gnss::V1_1::IGnssCallback;
36 using android::hardware::gnss::V1_0::GnssLocationFlags;
37 
38 using android::sp;
39 
40 #define TIMEOUT_SEC 2  // for basic commands/responses
41 
42 // Test environment for GNSS HIDL HAL.
43 class GnssHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
44    public:
45     // get the test environment singleton
Instance()46     static GnssHidlEnvironment* Instance() {
47         static GnssHidlEnvironment* instance = new GnssHidlEnvironment;
48         return instance;
49     }
50 
registerTestServices()51     virtual void registerTestServices() override { registerTestService<IGnss>(); }
52 
53    private:
GnssHidlEnvironment()54     GnssHidlEnvironment() {}
55 };
56 
57 // The main test class for GNSS HAL.
58 class GnssHalTest : public ::testing::VtsHalHidlTargetTestBase {
59    public:
60     GnssHalTest();
61 
62     virtual void SetUp() override;
63 
64     virtual void TearDown() override;
65 
66     /* Used as a mechanism to inform the test that a callback has occurred */
67     void notify();
68 
69     /* Test code calls this function to wait for a callback */
70     std::cv_status wait(int timeout_seconds);
71 
72     /* Callback class for data & Event. */
73     class GnssCallback : public IGnssCallback {
74        public:
75         GnssHalTest& parent_;
76 
GnssCallback(GnssHalTest & parent)77         GnssCallback(GnssHalTest& parent) : parent_(parent){};
78 
79         virtual ~GnssCallback() = default;
80 
81         // Dummy callback handlers
gnssStatusCb(const IGnssCallback::GnssStatusValue)82         Return<void> gnssStatusCb(const IGnssCallback::GnssStatusValue /* status */) override {
83             return Void();
84         }
gnssNmeaCb(int64_t,const android::hardware::hidl_string &)85         Return<void> gnssNmeaCb(int64_t /* timestamp */,
86                                 const android::hardware::hidl_string& /* nmea */) override {
87             return Void();
88         }
gnssAcquireWakelockCb()89         Return<void> gnssAcquireWakelockCb() override { return Void(); }
gnssReleaseWakelockCb()90         Return<void> gnssReleaseWakelockCb() override { return Void(); }
gnssRequestLocationCb(bool)91         Return<void> gnssRequestLocationCb(bool /* independentFromGnss */) override {
92             return Void();
93         }
gnssRequestTimeCb()94         Return<void> gnssRequestTimeCb() override { return Void(); }
95         // Actual (test) callback handlers
96         Return<void> gnssNameCb(const android::hardware::hidl_string& name) override;
97         Return<void> gnssLocationCb(const GnssLocation& location) override;
98         Return<void> gnssSetCapabilitesCb(uint32_t capabilities) override;
99         Return<void> gnssSetSystemInfoCb(const IGnssCallback::GnssSystemInfo& info) override;
100         Return<void> gnssSvStatusCb(const IGnssCallback::GnssSvStatus& svStatus) override;
101     };
102 
103     /*
104      * SetUpGnssCallback:
105      *   Set GnssCallback and verify the result.
106      */
107     void SetUpGnssCallback();
108 
109     /*
110      * StartAndGetSingleLocation:
111      * Helper function to get one Location and check fields
112      *
113      * returns  true if a location was successfully generated
114      */
115     bool StartAndGetSingleLocation();
116 
117     /*
118      * CheckLocation:
119      *   Helper function to vet Location fields
120      */
121     void CheckLocation(GnssLocation& location, bool check_speed);
122 
123     /*
124      * StartAndCheckLocations:
125      *   Helper function to collect, and check a number of
126      *   normal ~1Hz locations.
127      *
128      *   Note this leaves the Location request active, to enable Stop call vs. other call
129      *   reordering tests.
130      */
131     void StartAndCheckLocations(int count);
132 
133     /*
134      * StopAndClearLocations:
135      * Helper function to stop locations, and clear any remaining notifications
136      */
137     void StopAndClearLocations();
138 
139     /*
140      * SetPositionMode:
141      * Helper function to set positioning mode and verify output
142      */
143     void SetPositionMode(const int min_interval_msec, const bool low_power_mode);
144 
145     sp<IGnss> gnss_hal_;         // GNSS HAL to call into
146     sp<IGnssCallback> gnss_cb_;  // Primary callback interface
147 
148     /* Count of calls to set the following items, and the latest item (used by
149      * test.)
150      */
151     int info_called_count_;
152     IGnssCallback::GnssSystemInfo last_info_;
153     uint32_t last_capabilities_;
154     int capabilities_called_count_;
155     int location_called_count_;
156     GnssLocation last_location_;
157     list<IGnssCallback::GnssSvStatus> list_gnss_sv_status_;
158 
159     int name_called_count_;
160     android::hardware::hidl_string last_name_;
161 
162    private:
163     std::mutex mtx_;
164     std::condition_variable cv_;
165     int notify_count_;
166 };
167 
168 #endif  // GNSS_HAL_TEST_H_
169