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     if (nci_version == NCI_VERSION_2 && res.args->last_data_.size() > 13 &&
300         res.args->last_data_[13] == 0x00) {
301         // Wait for CORE_CONN_CREDITS_NTF
302         res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
303         EXPECT_TRUE(res.no_timeout);
304     }
305     // Send an Error Data Packet
306     cmd = INVALID_COMMAND;
307     data = cmd;
308     size_t size = data.size();
309 
310     for (int i = 0; i < 100; i++) {
311         data.resize(++size);
312         data[size - 1] = 0xFF;
313         EXPECT_EQ(data.size(), nfc_->write(data));
314         // Wait for response with SYNTAX_ERROR
315         res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
316         EXPECT_TRUE(res.no_timeout);
317         EXPECT_EQ(4ul, res.args->last_data_.size());
318         EXPECT_EQ(SYNTAX_ERROR, res.args->last_data_[3]);
319   }
320 
321   cmd = CORE_CONN_CREATE_CMD;
322   data = cmd;
323   EXPECT_EQ(data.size(), nfc_->write(data));
324   // Wait for CORE_CONN_CREATE_RSP
325   res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
326   EXPECT_TRUE(res.no_timeout);
327   EXPECT_EQ(7ul, res.args->last_data_.size());
328   EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
329 }
330 /*
331  * Bandwidth:
332  * Sets the loop-back mode using CORE_CONN_CREATE_CMD
333  * Sends max payload size data
334  * Waits for the response
335  * Checks the data received
336  * Repeat to send total of 1Mb data
337  */
TEST_P(NfcHidlTest,Bandwidth)338 TEST_P(NfcHidlTest, Bandwidth) {
339     std::vector<uint8_t> cmd = CORE_RESET_CMD;
340     NfcData data = cmd;
341     EXPECT_EQ(data.size(), nfc_->write(data));
342     // Wait for CORE_RESET_RSP
343     auto res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
344     EXPECT_TRUE(res.no_timeout);
345     EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
346 
347     /* NCI 2.0 sends CORE_RESET_NTF everytime. */
348     if (nci_version == NCI_VERSION_2) {
349         // Wait for CORE_RESET_NTF
350         res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
351         EXPECT_TRUE(res.no_timeout);
352         cmd = CORE_INIT_CMD_NCI20;
353     } else {
354         cmd = CORE_INIT_CMD;
355     }
356     data = cmd;
357 
358     EXPECT_EQ(data.size(), nfc_->write(data));
359     // Wait for CORE_INIT_RSP
360     res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
361     EXPECT_TRUE(res.no_timeout);
362     EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
363     if (nci_version == NCI_VERSION_2 && res.args->last_data_.size() > 13 &&
364         res.args->last_data_[13] == 0x00) {
365         // Wait for CORE_CONN_CREDITS_NTF
366         res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
367         EXPECT_TRUE(res.no_timeout);
368     }
369 
370     cmd = CORE_CONN_CREATE_CMD;
371     data = cmd;
372     EXPECT_EQ(data.size(), nfc_->write(data));
373     // Wait for CORE_CONN_CREATE_RSP
374     res = nfc_cb_->WaitForCallback(kCallbackNameSendData);
375     EXPECT_TRUE(res.no_timeout);
376     EXPECT_TRUE(res.no_timeout);
377     EXPECT_EQ(7ul, res.args->last_data_.size());
378     EXPECT_EQ((int)NfcStatus::OK, res.args->last_data_[3]);
379     uint8_t conn_id = res.args->last_data_[6];
380     uint32_t max_payload_size = res.args->last_data_[4];
381 
382     for (int loops = 0; loops < NUMBER_LOOPS; loops++) {
383         res.args->last_data_.resize(0);
384         data.resize(max_payload_size + LOOP_BACK_HEADER_SIZE);
385         data[0] = conn_id;
386         data[1] = 0x00;
387         data[2] = max_payload_size;
388         for (uint32_t i = 0; i < max_payload_size; i++) {
389             data[i + LOOP_BACK_HEADER_SIZE] = i;
390         }
391         EXPECT_EQ(max_payload_size + LOOP_BACK_HEADER_SIZE, nfc_->write(data));
392         // Wait for data and CORE_CONN_CREDITS_NTF
393         auto res1 = nfc_cb_->WaitForCallback(kCallbackNameSendData);
394         EXPECT_TRUE(res1.no_timeout);
395         auto res2 = nfc_cb_->WaitForCallback(kCallbackNameSendData);
396         EXPECT_TRUE(res2.no_timeout);
397         // Check if the same data was received back
398         EXPECT_TRUE(res1.args);
399         EXPECT_TRUE(res2.args);
400 
401         NfcData credits_ntf = res1.args->last_data_;
402         NfcData received_data = res2.args->last_data_;
403         /* It is possible that CORE_CONN_CREDITS_NTF is received before data,
404          * Find the order and do further checks depending on that */
405         if (received_data.size() != data.size()) {
406             credits_ntf = res2.args->last_data_;
407             received_data = res1.args->last_data_;
408         }
409         EXPECT_EQ(data.size(), received_data.size());
410         for (size_t i = 0; i < data.size(); i++) {
411             EXPECT_EQ(data[i], received_data[i]);
412         }
413 
414         EXPECT_EQ(6ul, credits_ntf.size());
415         // Check if the credit is refilled to 1
416         EXPECT_EQ(1, credits_ntf[5]);
417   }
418 }
419 
420 /*
421  * PowerCycle:
422  * Calls powerCycle()
423  * Waits for NfcEvent.OPEN_CPLT
424  * Checks status
425  */
TEST_P(NfcHidlTest,PowerCycle)426 TEST_P(NfcHidlTest, PowerCycle) {
427   EXPECT_EQ(NfcStatus::OK, nfc_->powerCycle());
428   // Wait for NfcEvent.OPEN_CPLT
429   auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
430   EXPECT_TRUE(res.no_timeout);
431   EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
432   EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
433 }
434 
435 /*
436  * PowerCycleAfterClose:
437  * Calls powerCycle() after close()
438  * Checks status
439  */
TEST_P(NfcHidlTest,PowerCycleAfterClose)440 TEST_P(NfcHidlTest, PowerCycleAfterClose) {
441   EXPECT_EQ(NfcStatus::OK, nfc_->close());
442   // Wait for CLOSE_CPLT event
443   auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
444   EXPECT_TRUE(res.no_timeout);
445   EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
446   EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
447 
448   EXPECT_EQ(NfcStatus::FAILED, nfc_->powerCycle());
449 
450   EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
451   // Wait for OPEN_CPLT event
452   res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
453   EXPECT_TRUE(res.no_timeout);
454   EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
455   EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
456 }
457 
458 /*
459  * CoreInitialized:
460  * Calls coreInitialized() with different data
461  * Waits for NfcEvent.POST_INIT_CPLT
462  */
TEST_P(NfcHidlTest,CoreInitialized)463 TEST_P(NfcHidlTest, CoreInitialized) {
464   NfcData data;
465   data.resize(1);
466   // These parameters might lead to device specific proprietary behavior
467   // Using > 10 values should result in predictable and common results for
468   // most devices.
469   for (int i = 10; i <= 16; i++) {
470       data[0] = i;
471       NfcStatus status = nfc_->coreInitialized(data);
472 
473       /* In case coreInitialized returned FAILED, do not wait for
474        * POST_INIT_CLPT event. */
475       if (status == NfcStatus::FAILED) continue;
476 
477       EXPECT_EQ(NfcStatus::OK, status);
478       // Wait for NfcEvent.POST_INIT_CPLT
479       auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
480       EXPECT_TRUE(res.no_timeout);
481       EXPECT_EQ(NfcEvent::POST_INIT_CPLT, res.args->last_event_);
482   }
483 }
484 
485 /*
486  * ControlGranted:
487  * Calls controlGranted()
488  * Checks the return value
489  */
TEST_P(NfcHidlTest,ControlGranted)490 TEST_P(NfcHidlTest, ControlGranted) {
491   EXPECT_EQ(NfcStatus::OK, nfc_->controlGranted());
492 }
493 
494 /*
495  * ControlGrantedAfterClose:
496  * Call controlGranted() after close
497  * Checks the return value
498  */
TEST_P(NfcHidlTest,ControlGrantedAfterClose)499 TEST_P(NfcHidlTest, ControlGrantedAfterClose) {
500   EXPECT_EQ(NfcStatus::OK, nfc_->close());
501   // Wait for CLOSE_CPLT event
502   auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
503   EXPECT_TRUE(res.no_timeout);
504   EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
505   EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
506 
507   EXPECT_EQ(NfcStatus::OK, nfc_->controlGranted());
508 
509   EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
510   // Wait for OPEN_CPLT event
511   res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
512   EXPECT_TRUE(res.no_timeout);
513   EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
514   EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
515 }
516 
517 /* PreDiscover:
518  * Calls prediscover()
519  * Checks the return value
520  */
TEST_P(NfcHidlTest,PreDiscover)521 TEST_P(NfcHidlTest, PreDiscover) {
522   EXPECT_EQ(NfcStatus::OK, nfc_->prediscover());
523 }
524 
525 /*
526  * PreDiscoverAfterClose:
527  * Call prediscover() after close
528  * Checks the return value
529  */
TEST_P(NfcHidlTest,PreDiscoverAfterClose)530 TEST_P(NfcHidlTest, PreDiscoverAfterClose) {
531   EXPECT_EQ(NfcStatus::OK, nfc_->close());
532   // Wait for CLOSE_CPLT event
533   auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
534   EXPECT_TRUE(res.no_timeout);
535   EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
536   EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
537 
538   EXPECT_EQ(NfcStatus::OK, nfc_->prediscover());
539 
540   EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
541   // Wait for OPEN_CPLT event
542   res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
543   EXPECT_TRUE(res.no_timeout);
544   EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
545   EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
546 }
547 
548 /*
549  * CloseAfterClose:
550  * Calls close() multiple times
551  * Checks status
552  */
TEST_P(NfcHidlTest,CloseAfterClose)553 TEST_P(NfcHidlTest, CloseAfterClose) {
554   EXPECT_EQ(NfcStatus::OK, nfc_->close());
555   // Wait for CLOSE_CPLT event
556   auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
557   EXPECT_TRUE(res.no_timeout);
558   EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_);
559   EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
560 
561   EXPECT_EQ(NfcStatus::FAILED, nfc_->close());
562 
563   EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
564   // Wait for OPEN_CPLT event
565   res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent);
566   EXPECT_TRUE(res.no_timeout);
567   EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_);
568   EXPECT_EQ(NfcStatus::OK, res.args->last_status_);
569 }
570 
571 /*
572  * OpenAfterOpen:
573  * Calls open() multiple times
574  * Checks status
575  */
TEST_P(NfcHidlTest,OpenAfterOpen)576 TEST_P(NfcHidlTest, OpenAfterOpen) {
577   EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
578   EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
579 }
580 
581 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NfcHidlTest);
582 INSTANTIATE_TEST_SUITE_P(
583         PerInstance, NfcHidlTest,
584         testing::ValuesIn(android::hardware::getAllHalInstanceNames(INfc::descriptor)),
585         android::hardware::PrintInstanceNameToString);
586 
main(int argc,char ** argv)587 int main(int argc, char** argv) {
588   ::testing::InitGoogleTest(&argc, argv);
589 
590   std::system("svc nfc disable"); /* Turn off NFC */
591   sleep(5);
592 
593   int status = RUN_ALL_TESTS();
594   LOG(INFO) << "Test result = " << status;
595 
596   std::system("svc nfc enable"); /* Turn on NFC */
597   sleep(5);
598 
599   return status;
600 }
601