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 #define LOG_TAG "nfc_hidl_hal_test" 18 #include <android-base/logging.h> 19 20 #include <android/hardware/nfc/1.1/INfcClientCallback.h> 21 #include <android/hardware/nfc/1.2/INfc.h> 22 #include <android/hardware/nfc/1.2/types.h> 23 #include <hardware/nfc.h> 24 25 #include <VtsHalHidlTargetCallbackBase.h> 26 #include <VtsHalHidlTargetTestBase.h> 27 #include <VtsHalHidlTargetTestEnvBase.h> 28 29 using ::android::sp; 30 using ::android::hardware::hidl_vec; 31 using ::android::hardware::Return; 32 using ::android::hardware::Void; 33 using ::android::hardware::nfc::V1_0::NfcData; 34 using ::android::hardware::nfc::V1_0::NfcStatus; 35 using ::android::hardware::nfc::V1_1::INfcClientCallback; 36 using ::android::hardware::nfc::V1_1::NfcEvent; 37 using ::android::hardware::nfc::V1_2::INfc; 38 using ::android::hardware::nfc::V1_2::NfcConfig; 39 40 // Range of valid off host route ids 41 constexpr unsigned int MIN_OFFHOST_ROUTE_ID = 0x01; 42 constexpr unsigned int MAX_OFFHOST_ROUTE_ID = 0xFE; 43 44 constexpr char kCallbackNameSendEvent[] = "sendEvent"; 45 constexpr char kCallbackNameSendData[] = "sendData"; 46 47 class NfcClientCallbackArgs { 48 public: 49 NfcEvent last_event_; 50 NfcStatus last_status_; 51 NfcData last_data_; 52 }; 53 54 /* Callback class for data & Event. */ 55 class NfcClientCallback : public ::testing::VtsHalHidlTargetCallbackBase<NfcClientCallbackArgs>, 56 public INfcClientCallback { 57 public: 58 virtual ~NfcClientCallback() = default; 59 60 /* sendEvent callback function - Records the Event & Status 61 * and notifies the TEST 62 **/ 63 Return<void> sendEvent_1_1(NfcEvent event, NfcStatus event_status) override { 64 NfcClientCallbackArgs args; 65 args.last_event_ = event; 66 args.last_status_ = event_status; 67 NotifyFromCallback(kCallbackNameSendEvent, args); 68 return Void(); 69 }; 70 71 /** NFC 1.1 HAL shouldn't send 1.0 callbacks */ 72 Return<void> sendEvent(__attribute__((unused))::android::hardware::nfc::V1_0::NfcEvent event, 73 __attribute__((unused)) NfcStatus event_status) override { 74 return Void(); 75 } 76 77 /* sendData callback function. Records the data and notifies the TEST*/ 78 Return<void> sendData(const NfcData& data) override { 79 NfcClientCallbackArgs args; 80 args.last_data_ = data; 81 NotifyFromCallback(kCallbackNameSendData, args); 82 return Void(); 83 }; 84 }; 85 86 // Test environment for Nfc HIDL HAL. 87 class NfcHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { 88 public: 89 // get the test environment singleton 90 static NfcHidlEnvironment* Instance() { 91 static NfcHidlEnvironment* instance = new NfcHidlEnvironment; 92 return instance; 93 } 94 95 virtual void registerTestServices() override { registerTestService<INfc>(); } 96 97 private: 98 NfcHidlEnvironment() {} 99 }; 100 101 // The main test class for NFC HIDL HAL. 102 class NfcHidlTest : public ::testing::VtsHalHidlTargetTestBase { 103 public: 104 virtual void SetUp() override { 105 nfc_ = ::testing::VtsHalHidlTargetTestBase::getService<INfc>(); 106 ASSERT_NE(nfc_, nullptr); 107 108 nfc_cb_ = new NfcClientCallback(); 109 ASSERT_NE(nfc_cb_, nullptr); 110 111 EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_)); 112 // Wait for OPEN_CPLT event 113 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); 114 EXPECT_TRUE(res.no_timeout); 115 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_); 116 EXPECT_EQ(NfcStatus::OK, res.args->last_status_); 117 118 /* 119 * Close the hal and then re-open to make sure we are in a predictable 120 * state for all the tests. 121 */ 122 EXPECT_EQ(NfcStatus::OK, nfc_->close()); 123 // Wait for CLOSE_CPLT event 124 res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); 125 EXPECT_TRUE(res.no_timeout); 126 EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_); 127 EXPECT_EQ(NfcStatus::OK, res.args->last_status_); 128 129 EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_)); 130 // Wait for OPEN_CPLT event 131 res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); 132 EXPECT_TRUE(res.no_timeout); 133 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_); 134 EXPECT_EQ(NfcStatus::OK, res.args->last_status_); 135 } 136 137 virtual void TearDown() override { 138 EXPECT_EQ(NfcStatus::OK, nfc_->close()); 139 // Wait for CLOSE_CPLT event 140 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); 141 EXPECT_TRUE(res.no_timeout); 142 EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_); 143 EXPECT_EQ(NfcStatus::OK, res.args->last_status_); 144 } 145 146 sp<INfc> nfc_; 147 sp<NfcClientCallback> nfc_cb_; 148 }; 149 150 /* 151 * getConfig: 152 * Calls getConfig() 153 * checks if fields in NfcConfig are populated correctly 154 */ 155 TEST_F(NfcHidlTest, GetExtendedConfig) { 156 nfc_->getConfig_1_2([](NfcConfig config) { 157 for (uint8_t uicc : config.offHostRouteUicc) { 158 EXPECT_GE(uicc, MIN_OFFHOST_ROUTE_ID); 159 EXPECT_LE(uicc, MAX_OFFHOST_ROUTE_ID); 160 } 161 for (uint8_t ese : config.offHostRouteEse) { 162 EXPECT_GE(ese, MIN_OFFHOST_ROUTE_ID); 163 EXPECT_LE(ese, MAX_OFFHOST_ROUTE_ID); 164 } 165 if (config.defaultIsoDepRoute != 0) { 166 EXPECT_GE(config.defaultIsoDepRoute, MIN_OFFHOST_ROUTE_ID); 167 EXPECT_LE(config.defaultIsoDepRoute, MAX_OFFHOST_ROUTE_ID); 168 } 169 }); 170 } 171 172 int main(int argc, char** argv) { 173 ::testing::AddGlobalTestEnvironment(NfcHidlEnvironment::Instance()); 174 ::testing::InitGoogleTest(&argc, argv); 175 NfcHidlEnvironment::Instance()->init(&argc, argv); 176 177 std::system("svc nfc disable"); /* Turn off NFC */ 178 sleep(5); 179 180 int status = RUN_ALL_TESTS(); 181 LOG(INFO) << "Test result = " << status; 182 183 std::system("svc nfc enable"); /* Turn on NFC */ 184 sleep(5); 185 186 return status; 187 } 188