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 * StartAndCheckFirstLocation: 111 * Helper function to start location, and check the first one. 112 * 113 * <p> Note this leaves the Location request active, to enable Stop call vs. other call 114 * reordering tests. 115 * 116 * returns true if a location was successfully generated 117 */ 118 bool StartAndCheckFirstLocation(); 119 120 /* 121 * CheckLocation: 122 * Helper function to vet Location fields 123 */ 124 void CheckLocation(GnssLocation& location, bool check_speed); 125 126 /* 127 * StartAndCheckLocations: 128 * Helper function to collect, and check a number of 129 * normal ~1Hz locations. 130 * 131 * Note this leaves the Location request active, to enable Stop call vs. other call 132 * reordering tests. 133 */ 134 void StartAndCheckLocations(int count); 135 136 /* 137 * StopAndClearLocations: 138 * Helper function to stop locations, and clear any remaining notifications 139 */ 140 void StopAndClearLocations(); 141 142 /* 143 * SetPositionMode: 144 * Helper function to set positioning mode and verify output 145 */ 146 void SetPositionMode(const int min_interval_msec, const bool low_power_mode); 147 148 sp<IGnss> gnss_hal_; // GNSS HAL to call into 149 sp<IGnssCallback> gnss_cb_; // Primary callback interface 150 151 /* Count of calls to set the following items, and the latest item (used by 152 * test.) 153 */ 154 int info_called_count_; 155 IGnssCallback::GnssSystemInfo last_info_; 156 uint32_t last_capabilities_; 157 int capabilities_called_count_; 158 int location_called_count_; 159 GnssLocation last_location_; 160 list<IGnssCallback::GnssSvStatus> list_gnss_sv_status_; 161 162 int name_called_count_; 163 android::hardware::hidl_string last_name_; 164 165 private: 166 std::mutex mtx_; 167 std::condition_variable cv_; 168 int notify_count_; 169 }; 170 171 #endif // GNSS_HAL_TEST_H_ 172