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