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