• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.0/types.h>
21 #include <android/hardware/nfc/1.1/INfc.h>
22 #include <android/hardware/nfc/1.1/INfcClientCallback.h>
23 #include <android/hardware/nfc/1.1/types.h>
24 #include <gtest/gtest.h>
25 #include <hardware/nfc.h>
26 #include <hidl/GtestPrinter.h>
27 #include <hidl/ServiceManagement.h>
28 
29 #include <VtsHalHidlTargetCallbackBase.h>
30 
31 using ::android::hardware::nfc::V1_1::INfc;
32 using ::android::hardware::nfc::V1_1::INfcClientCallback;
33 using ::android::hardware::nfc::V1_1::NfcEvent;
34 using ::android::hardware::nfc::V1_1::NfcConfig;
35 using ::android::hardware::nfc::V1_0::NfcStatus;
36 using ::android::hardware::nfc::V1_0::NfcData;
37 using ::android::hardware::Return;
38 using ::android::hardware::Void;
39 using ::android::hardware::hidl_vec;
40 using ::android::sp;
41 
42 // 261 bytes is the default and minimum transceive length
43 constexpr unsigned int MIN_ISO_DEP_TRANSCEIVE_LENGTH = 261;
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      **/
sendEvent_1_1(NfcEvent event,NfcStatus event_status)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 */
sendEvent(::android::hardware::nfc::V1_0::NfcEvent event,NfcStatus event_status)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*/
sendData(const NfcData & data)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 
87 // The main test class for NFC HIDL HAL.
88 class NfcHidlTest : public ::testing::TestWithParam<std::string> {
89    public:
SetUp()90     virtual void SetUp() override {
91         nfc_ = INfc::getService(GetParam());
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 
TearDown()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  * factoryReset
138  * calls factoryReset()
139  * checks status
140  */
TEST_P(NfcHidlTest,FactoryReset)141 TEST_P(NfcHidlTest, FactoryReset) {
142     nfc_->factoryReset();
143 
144     EXPECT_EQ(NfcStatus::OK, nfc_->close());
145     // Wait for CLOSE_CPLT event
146     auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
147     EXPECT_TRUE(res.no_timeout);
148     EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
149     EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
150 
151     EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_));
152     // Wait for OPEN_CPLT event
153     res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
154     EXPECT_TRUE(res.no_timeout);
155     EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
156     EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
157 }
158 
159 /*
160  * OpenAndClose:
161  * Makes an open call, waits for NfcEvent.OPEN_CPLT
162  * Immediately calls closeforPowerOffCase() and waits for NfcEvent.CLOSE_CPLT
163  */
TEST_P(NfcHidlTest,OpenAndCloseForPowerOff)164 TEST_P(NfcHidlTest, OpenAndCloseForPowerOff) {
165     EXPECT_EQ(NfcStatus::OK, nfc_->closeForPowerOffCase());
166     // Wait for CLOSE_CPLT event
167     auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
168     EXPECT_TRUE(res.no_timeout);
169     EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
170     EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
171 
172     EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_));
173     // Wait for OPEN_CPLT event
174     res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
175     EXPECT_TRUE(res.no_timeout);
176     EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
177     EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
178 }
179 
180 /*
181  * CloseForPowerOffCaseAfterClose:
182  * Calls closeForPowerOffCase()
183  * Calls close() - checks failed status
184  */
TEST_P(NfcHidlTest,CloseForPowerCaseOffAfterClose)185 TEST_P(NfcHidlTest, CloseForPowerCaseOffAfterClose) {
186     EXPECT_EQ(NfcStatus::OK, nfc_->closeForPowerOffCase());
187     // Wait for CLOSE_CPLT event
188     auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
189     EXPECT_TRUE(res.no_timeout);
190     EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
191     EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
192 
193     EXPECT_EQ(NfcStatus::FAILED, nfc_->close());
194 
195     EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_));
196     // Wait for OPEN_CPLT event
197     res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
198     EXPECT_TRUE(res.no_timeout);
199     EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
200     EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
201 }
202 
203 /*
204  * getConfig:
205  * Calls getConfig()
206  * checks if fields in NfcConfig are populated correctly
207  */
TEST_P(NfcHidlTest,GetConfig)208 TEST_P(NfcHidlTest, GetConfig) {
209     nfc_->getConfig([](NfcConfig config) {
210         EXPECT_GE(config.maxIsoDepTransceiveLength, MIN_ISO_DEP_TRANSCEIVE_LENGTH);
211     });
212 }
213 
214 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NfcHidlTest);
215 INSTANTIATE_TEST_SUITE_P(
216         PerInstance, NfcHidlTest,
217         testing::ValuesIn(android::hardware::getAllHalInstanceNames(INfc::descriptor)),
218         android::hardware::PrintInstanceNameToString);
219 
main(int argc,char ** argv)220 int main(int argc, char** argv) {
221     ::testing::InitGoogleTest(&argc, argv);
222 
223     std::system("svc nfc disable"); /* Turn off NFC */
224     sleep(5);
225 
226     int status = RUN_ALL_TESTS();
227     LOG(INFO) << "Test result = " << status;
228 
229     std::system("svc nfc enable"); /* Turn on NFC */
230     sleep(5);
231 
232     return status;
233 }
234