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