1 /*
2 * Copyright (C) 2016 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/INfc.h>
21 #include <android/hardware/nfc/1.0/INfcClientCallback.h>
22 #include <android/hardware/nfc/1.0/types.h>
23 #include <gtest/gtest.h>
24 #include <hardware/nfc.h>
25 #include <hidl/GtestPrinter.h>
26 #include <hidl/ServiceManagement.h>
27
28 #include <VtsHalHidlTargetCallbackBase.h>
29
30 using ::android::hardware::nfc::V1_0::INfc;
31 using ::android::hardware::nfc::V1_0::INfcClientCallback;
32 using ::android::hardware::nfc::V1_0::NfcEvent;
33 using ::android::hardware::nfc::V1_0::NfcStatus;
34 using ::android::hardware::nfc::V1_0::NfcData;
35 using ::android::hardware::Return;
36 using ::android::hardware::Void;
37 using ::android::hardware::hidl_vec;
38 using ::android::sp;
39
40 /* NCI Commands */
41 #define CORE_RESET_CMD \
42 { 0x20, 0x00, 0x01, 0x00 }
43 #define CORE_RESET_CMD_CONFIG_RESET \
44 { 0x20, 0x00, 0x01, 0x01 }
45 #define CORE_CONN_CREATE_CMD \
46 { 0x20, 0x04, 0x02, 0x01, 0x00 }
47 #define CORE_INIT_CMD \
48 { 0x20, 0x01, 0x00 }
49 #define CORE_INIT_CMD_NCI20 \
50 { 0x20, 0x01, 0x02, 0x00, 0x00 }
51 #define INVALID_COMMAND \
52 { 0x20, 0x00, 0x00 }
53
54 #define LOOP_BACK_HEADER_SIZE 3
55 #define SYNTAX_ERROR 5
56 #define NUMBER_LOOPS 3922
57 #define NCI_VERSION_1_1 0x11
58 #define NCI_VERSION_2 0x20
59 #define TIMEOUT_PERIOD 5
60
61 constexpr char kCallbackNameSendEvent[] = "sendEvent";
62 constexpr char kCallbackNameSendData[] = "sendData";
63
64 class NfcClientCallbackArgs {
65 public:
66 NfcEvent last_event_;
67 NfcStatus last_status_;
68 NfcData last_data_;
69 };
70
71 /* Callback class for data & Event. */
72 class NfcClientCallback
73 : public ::testing::VtsHalHidlTargetCallbackBase<NfcClientCallbackArgs>,
74 public INfcClientCallback {
75 public:
76 virtual ~NfcClientCallback() = default;
77
78 /* sendEvent callback function - Records the Event & Status
79 * and notifies the TEST
80 **/
sendEvent(NfcEvent event,NfcStatus event_status)81 Return<void> sendEvent(NfcEvent event, NfcStatus event_status) override {
82 NfcClientCallbackArgs args;
83 args.last_event_ = event;
84 args.last_status_ = event_status;
85 NotifyFromCallback(kCallbackNameSendEvent, args);
86 return Void();
87 };
88
89 /* sendData callback function. Records the data and notifies the TEST*/
sendData(const NfcData & data)90 Return<void> sendData(const NfcData& data) override {
91 NfcClientCallbackArgs args;
92 args.last_data_ = data;
93 NotifyFromCallback(kCallbackNameSendData, args);
94 return Void();
95 };
96 };
97
98 // The main test class for NFC HIDL HAL.
99 class NfcHidlTest : public ::testing::TestWithParam<std::string> {
100 public:
SetUp()101 virtual void SetUp() override {
102 nfc_ = INfc::getService(GetParam());
103 ASSERT_NE(nfc_, nullptr);
104
105 nfc_cb_ = new NfcClientCallback();
106 ASSERT_NE(nfc_cb_, nullptr);
107
108 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
109 // Wait for OPEN_CPLT event
110 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
111 EXPECT_TRUE(res.no_timeout);
112 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
113 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
114
115 /* Get the NCI version that the device supports */
116 std::vector<uint8_t> cmd = CORE_RESET_CMD;
117 NfcData data = cmd;
118 EXPECT_EQ(data.size(), nfc_->write(data));
119 // Wait for CORE_RESET_RSP
120 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
121 EXPECT_TRUE(res.no_timeout);
122 EXPECT_GE(6ul, res.args->last_data_.size());
123 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
124 if (res.args->last_data_.size() == 6) {
125 nci_version = res.args->last_data_[4];
126 } else {
127 EXPECT_EQ(4ul, res.args->last_data_.size());
128 nci_version = NCI_VERSION_2;
129 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
130 EXPECT_TRUE(res.no_timeout);
131 }
132
133 /*
134 * Close the hal and then re-open to make sure we are in a predictable
135 * state for all the tests.
136 */
137 EXPECT_EQ(NfcStatus::OK, nfc_->close());
138 // Wait for CLOSE_CPLT event
139 res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
140 EXPECT_TRUE(res.no_timeout);
141 EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
142 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
143
144 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
145 // Wait for OPEN_CPLT event
146 res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
147 EXPECT_TRUE(res.no_timeout);
148 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
149 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
150 }
151
TearDown()152 virtual void TearDown() override {
153 EXPECT_EQ(NfcStatus::OK, nfc_->close());
154 // Wait for CLOSE_CPLT event
155 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
156 EXPECT_TRUE(res.no_timeout);
157 EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
158 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
159 }
160
161 /* NCI version the device supports
162 * 0x11 for NCI 1.1, 0x20 for NCI 2.0 and so forth */
163 uint8_t nci_version;
164 sp<INfc> nfc_;
165 sp<NfcClientCallback> nfc_cb_;
166 };
167
168 /*
169 * OpenAndClose:
170 * Makes an open call, waits for NfcEvent.OPEN_CPLT
171 * Immediately calls close() and waits for NfcEvent.CLOSE_CPLT
172 * Since open and close calls are a part of SetUp() and TearDown(),
173 * the function definition is intentionally kept empty
174 */
TEST_P(NfcHidlTest,OpenAndClose)175 TEST_P(NfcHidlTest, OpenAndClose) {}
176
177 /*
178 * WriteCoreReset:
179 * Sends CORE_RESET_CMD
180 * Waits for CORE_RESET_RSP
181 * Checks the status, version number and configuration status
182 */
TEST_P(NfcHidlTest,WriteCoreReset)183 TEST_P(NfcHidlTest, WriteCoreReset) {
184 std::vector<uint8_t> cmd = CORE_RESET_CMD;
185 NfcData data = cmd;
186 EXPECT_EQ(data.size(), nfc_->write(data));
187 // Wait for CORE_RESET_RSP
188 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
189 EXPECT_TRUE(res.no_timeout);
190
191 /* The response/notification format for CORE_RESET_CMD differs
192 * with NCI 1.0 and 2.0. */
193 if (nci_version <= NCI_VERSION_1_1) {
194 EXPECT_EQ(6ul, res.args->last_data_.size());
195 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
196 EXPECT_GE(NCI_VERSION_1_1, res.args->last_data_[4]);
197 EXPECT_GE(1ul, res.args->last_data_[5]);
198 } else {
199 EXPECT_EQ(4ul, res.args->last_data_.size());
200 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
201 // Wait for CORE_RESET_NTF
202 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
203 EXPECT_TRUE(res.no_timeout);
204 // Check if reset trigger was due to CORE_RESET_CMD
205 EXPECT_LE(8ul, res.args->last_data_.size());
206 EXPECT_EQ(2ul, res.args->last_data_[3]);
207 EXPECT_GE(1ul, res.args->last_data_[4]);
208 EXPECT_EQ(NCI_VERSION_2, res.args->last_data_[5]);
209 }
210 }
211
212 /*
213 * WriteCoreResetConfigReset:
214 * Sends CORE_RESET_CMD_CONFIG_RESET
215 * Waits for CORE_RESET_RSP
216 * Checks the status, version number and configuration status
217 */
TEST_P(NfcHidlTest,WriteCoreResetConfigReset)218 TEST_P(NfcHidlTest, WriteCoreResetConfigReset) {
219 std::vector<uint8_t> cmd = CORE_RESET_CMD_CONFIG_RESET;
220 NfcData data = cmd;
221 EXPECT_EQ(data.size(), nfc_->write(data));
222 // Wait for CORE_RESET_RSP
223 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
224 EXPECT_TRUE(res.no_timeout);
225
226 /* The response/notification format for CORE_RESET_CMD differs
227 * with NCI 1.0 and 2.0. */
228 if (nci_version <= NCI_VERSION_1_1) {
229 EXPECT_EQ(6ul, res.args->last_data_.size());
230 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
231 EXPECT_GE(NCI_VERSION_1_1, res.args->last_data_[4]);
232 EXPECT_EQ(1ul, res.args->last_data_[5]);
233 } else {
234 EXPECT_EQ(4ul, res.args->last_data_.size());
235 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
236 // Wait for CORE_RESET_NTF
237 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
238 EXPECT_TRUE(res.no_timeout);
239 // Check if reset trigger was due to CORE_RESET_CMD
240 EXPECT_LE(8ul, res.args->last_data_.size());
241 EXPECT_EQ(2ul, res.args->last_data_[3]);
242 EXPECT_EQ(1ul, res.args->last_data_[4]);
243 EXPECT_EQ(NCI_VERSION_2, res.args->last_data_[5]);
244 }
245 }
246
247 /*
248 * WriteInvalidCommand:
249 * Sends an invalid command
250 * Waits for response
251 * Checks SYNTAX_ERROR status
252 */
TEST_P(NfcHidlTest,WriteInvalidCommand)253 TEST_P(NfcHidlTest, WriteInvalidCommand) {
254 // Send an Error Command
255 std::vector<uint8_t> cmd = INVALID_COMMAND;
256 NfcData data = cmd;
257 EXPECT_EQ(data.size(), nfc_->write(data));
258 // Wait for RSP
259 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
260 EXPECT_TRUE(res.no_timeout);
261 EXPECT_EQ(4ul, res.args->last_data_.size());
262 EXPECT_EQ(SYNTAX_ERROR, res.args->last_data_[3]);
263 }
264
265 /*
266 * WriteInvalidAndThenValidCommand:
267 * Sends an Invalid command
268 * Waits for response
269 * Checks SYNTAX_ERROR status
270 * Repeat for 100 times appending 0xFF each time to the packet
271 * Send CORE_CONN_CREATE_CMD for loop-back mode
272 * Check the response
273 */
TEST_P(NfcHidlTest,WriteInvalidAndThenValidCommand)274 TEST_P(NfcHidlTest, WriteInvalidAndThenValidCommand) {
275 std::vector<uint8_t> cmd = CORE_RESET_CMD;
276 NfcData data = cmd;
277 EXPECT_EQ(data.size(), nfc_->write(data));
278 // Wait for CORE_RESET_RSP
279 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
280 EXPECT_TRUE(res.no_timeout);
281 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
282
283 /* NCI 2.0 sends CORE_RESET_NTF everytime. */
284 if (nci_version == NCI_VERSION_2) {
285 // Wait for CORE_RESET_NTF
286 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
287 EXPECT_TRUE(res.no_timeout);
288 cmd = CORE_INIT_CMD_NCI20;
289 } else {
290 cmd = CORE_INIT_CMD;
291 }
292 data = cmd;
293
294 EXPECT_EQ(data.size(), nfc_->write(data));
295 // Wait for CORE_INIT_RSP
296 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
297 EXPECT_TRUE(res.no_timeout);
298 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
299 // Send an Error Data Packet
300 cmd = INVALID_COMMAND;
301 data = cmd;
302 size_t size = data.size();
303
304 for (int i = 0; i < 100; i++) {
305 data.resize(++size);
306 data[size - 1] = 0xFF;
307 EXPECT_EQ(data.size(), nfc_->write(data));
308 // Wait for response with SYNTAX_ERROR
309 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
310 EXPECT_TRUE(res.no_timeout);
311 EXPECT_EQ(4ul, res.args->last_data_.size());
312 EXPECT_EQ(SYNTAX_ERROR, res.args->last_data_[3]);
313 }
314
315 cmd = CORE_CONN_CREATE_CMD;
316 data = cmd;
317 EXPECT_EQ(data.size(), nfc_->write(data));
318 // Wait for CORE_CONN_CREATE_RSP
319 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
320 EXPECT_TRUE(res.no_timeout);
321 EXPECT_EQ(7ul, res.args->last_data_.size());
322 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
323 }
324 /*
325 * Bandwidth:
326 * Sets the loop-back mode using CORE_CONN_CREATE_CMD
327 * Sends max payload size data
328 * Waits for the response
329 * Checks the data received
330 * Repeat to send total of 1Mb data
331 */
TEST_P(NfcHidlTest,Bandwidth)332 TEST_P(NfcHidlTest, Bandwidth) {
333 std::vector<uint8_t> cmd = CORE_RESET_CMD;
334 NfcData data = cmd;
335 EXPECT_EQ(data.size(), nfc_->write(data));
336 // Wait for CORE_RESET_RSP
337 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
338 EXPECT_TRUE(res.no_timeout);
339 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
340
341 /* NCI 2.0 sends CORE_RESET_NTF everytime. */
342 if (nci_version == NCI_VERSION_2) {
343 // Wait for CORE_RESET_NTF
344 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
345 EXPECT_TRUE(res.no_timeout);
346 cmd = CORE_INIT_CMD_NCI20;
347 } else {
348 cmd = CORE_INIT_CMD;
349 }
350 data = cmd;
351
352 EXPECT_EQ(data.size(), nfc_->write(data));
353 // Wait for CORE_INIT_RSP
354 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
355 EXPECT_TRUE(res.no_timeout);
356 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
357 cmd = CORE_CONN_CREATE_CMD;
358 data = cmd;
359 EXPECT_EQ(data.size(), nfc_->write(data));
360 // Wait for CORE_CONN_CREATE_RSP
361 res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
362 EXPECT_TRUE(res.no_timeout);
363 EXPECT_TRUE(res.no_timeout);
364 EXPECT_EQ(7ul, res.args->last_data_.size());
365 EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
366 uint8_t conn_id = res.args->last_data_[6];
367 uint32_t max_payload_size = res.args->last_data_[4];
368
369 for (int loops = 0; loops < NUMBER_LOOPS; loops++) {
370 res.args->last_data_.resize(0);
371 data.resize(max_payload_size + LOOP_BACK_HEADER_SIZE);
372 data[0] = conn_id;
373 data[1] = 0x00;
374 data[2] = max_payload_size;
375 for (uint32_t i = 0; i < max_payload_size; i++) {
376 data[i + LOOP_BACK_HEADER_SIZE] = i;
377 }
378 EXPECT_EQ(max_payload_size + LOOP_BACK_HEADER_SIZE, nfc_->write(data));
379 // Wait for data and CORE_CONN_CREDITS_NTF
380 auto res1 = nfc_cb_->WaitForCallback(kCallbackNameSendData);
381 EXPECT_TRUE(res1.no_timeout);
382 auto res2 = nfc_cb_->WaitForCallback(kCallbackNameSendData);
383 EXPECT_TRUE(res2.no_timeout);
384 // Check if the same data was received back
385 EXPECT_TRUE(res1.args);
386 EXPECT_TRUE(res2.args);
387
388 NfcData credits_ntf = res1.args->last_data_;
389 NfcData received_data = res2.args->last_data_;
390 /* It is possible that CORE_CONN_CREDITS_NTF is received before data,
391 * Find the order and do further checks depending on that */
392 if (received_data.size() != data.size()) {
393 credits_ntf = res2.args->last_data_;
394 received_data = res1.args->last_data_;
395 }
396 EXPECT_EQ(data.size(), received_data.size());
397 for (size_t i = 0; i < data.size(); i++) {
398 EXPECT_EQ(data[i], received_data[i]);
399 }
400
401 EXPECT_EQ(6ul, credits_ntf.size());
402 // Check if the credit is refilled to 1
403 EXPECT_EQ(1, credits_ntf[5]);
404 }
405 }
406
407 /*
408 * PowerCycle:
409 * Calls powerCycle()
410 * Waits for NfcEvent.OPEN_CPLT
411 * Checks status
412 */
TEST_P(NfcHidlTest,PowerCycle)413 TEST_P(NfcHidlTest, PowerCycle) {
414 EXPECT_EQ(NfcStatus::OK, nfc_->powerCycle());
415 // Wait for NfcEvent.OPEN_CPLT
416 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
417 EXPECT_TRUE(res.no_timeout);
418 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
419 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
420 }
421
422 /*
423 * PowerCycleAfterClose:
424 * Calls powerCycle() after close()
425 * Checks status
426 */
TEST_P(NfcHidlTest,PowerCycleAfterClose)427 TEST_P(NfcHidlTest, PowerCycleAfterClose) {
428 EXPECT_EQ(NfcStatus::OK, nfc_->close());
429 // Wait for CLOSE_CPLT event
430 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
431 EXPECT_TRUE(res.no_timeout);
432 EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
433 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
434
435 EXPECT_EQ(NfcStatus::FAILED, nfc_->powerCycle());
436
437 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
438 // Wait for OPEN_CPLT event
439 res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
440 EXPECT_TRUE(res.no_timeout);
441 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
442 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
443 }
444
445 /*
446 * CoreInitialized:
447 * Calls coreInitialized() with different data
448 * Waits for NfcEvent.POST_INIT_CPLT
449 */
TEST_P(NfcHidlTest,CoreInitialized)450 TEST_P(NfcHidlTest, CoreInitialized) {
451 NfcData data;
452 data.resize(1);
453 // These parameters might lead to device specific proprietary behavior
454 // Using > 10 values should result in predictable and common results for
455 // most devices.
456 for (int i = 10; i <= 16; i++) {
457 data[0] = i;
458 NfcStatus status = nfc_->coreInitialized(data);
459
460 /* In case coreInitialized returned FAILED, do not wait for
461 * POST_INIT_CLPT event. */
462 if (status == NfcStatus::FAILED) continue;
463
464 EXPECT_EQ(NfcStatus::OK, status);
465 // Wait for NfcEvent.POST_INIT_CPLT
466 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
467 EXPECT_TRUE(res.no_timeout);
468 EXPECT_EQ(NfcEvent::POST_INIT_CPLT, res.args->last_event_);
469 }
470 }
471
472 /*
473 * ControlGranted:
474 * Calls controlGranted()
475 * Checks the return value
476 */
TEST_P(NfcHidlTest,ControlGranted)477 TEST_P(NfcHidlTest, ControlGranted) {
478 EXPECT_EQ(NfcStatus::OK, nfc_->controlGranted());
479 }
480
481 /*
482 * ControlGrantedAfterClose:
483 * Call controlGranted() after close
484 * Checks the return value
485 */
TEST_P(NfcHidlTest,ControlGrantedAfterClose)486 TEST_P(NfcHidlTest, ControlGrantedAfterClose) {
487 EXPECT_EQ(NfcStatus::OK, nfc_->close());
488 // Wait for CLOSE_CPLT event
489 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
490 EXPECT_TRUE(res.no_timeout);
491 EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
492 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
493
494 EXPECT_EQ(NfcStatus::OK, nfc_->controlGranted());
495
496 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
497 // Wait for OPEN_CPLT event
498 res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
499 EXPECT_TRUE(res.no_timeout);
500 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
501 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
502 }
503
504 /* PreDiscover:
505 * Calls prediscover()
506 * Checks the return value
507 */
TEST_P(NfcHidlTest,PreDiscover)508 TEST_P(NfcHidlTest, PreDiscover) {
509 EXPECT_EQ(NfcStatus::OK, nfc_->prediscover());
510 }
511
512 /*
513 * PreDiscoverAfterClose:
514 * Call prediscover() after close
515 * Checks the return value
516 */
TEST_P(NfcHidlTest,PreDiscoverAfterClose)517 TEST_P(NfcHidlTest, PreDiscoverAfterClose) {
518 EXPECT_EQ(NfcStatus::OK, nfc_->close());
519 // Wait for CLOSE_CPLT event
520 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
521 EXPECT_TRUE(res.no_timeout);
522 EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
523 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
524
525 EXPECT_EQ(NfcStatus::OK, nfc_->prediscover());
526
527 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
528 // Wait for OPEN_CPLT event
529 res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
530 EXPECT_TRUE(res.no_timeout);
531 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
532 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
533 }
534
535 /*
536 * CloseAfterClose:
537 * Calls close() multiple times
538 * Checks status
539 */
TEST_P(NfcHidlTest,CloseAfterClose)540 TEST_P(NfcHidlTest, CloseAfterClose) {
541 EXPECT_EQ(NfcStatus::OK, nfc_->close());
542 // Wait for CLOSE_CPLT event
543 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
544 EXPECT_TRUE(res.no_timeout);
545 EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
546 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
547
548 EXPECT_EQ(NfcStatus::FAILED, nfc_->close());
549
550 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
551 // Wait for OPEN_CPLT event
552 res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
553 EXPECT_TRUE(res.no_timeout);
554 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
555 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
556 }
557
558 /*
559 * OpenAfterOpen:
560 * Calls open() multiple times
561 * Checks status
562 */
TEST_P(NfcHidlTest,OpenAfterOpen)563 TEST_P(NfcHidlTest, OpenAfterOpen) {
564 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
565 // Wait for OPEN_CPLT event
566 auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
567 EXPECT_TRUE(res.no_timeout);
568 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
569 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
570
571 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
572 // Wait for OPEN_CPLT event
573 res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
574 EXPECT_TRUE(res.no_timeout);
575 EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
576 EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
577 }
578
579 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NfcHidlTest);
580 INSTANTIATE_TEST_SUITE_P(
581 PerInstance, NfcHidlTest,
582 testing::ValuesIn(android::hardware::getAllHalInstanceNames(INfc::descriptor)),
583 android::hardware::PrintInstanceNameToString);
584
main(int argc,char ** argv)585 int main(int argc, char** argv) {
586 ::testing::InitGoogleTest(&argc, argv);
587
588 std::system("svc nfc disable"); /* Turn off NFC */
589 sleep(5);
590
591 int status = RUN_ALL_TESTS();
592 LOG(INFO) << "Test result = " << status;
593
594 std::system("svc nfc enable"); /* Turn on NFC */
595 sleep(5);
596
597 return status;
598 }
599