blob: aa3bc9c484e009848213167ed6fd9e77d361dd2b [file] [log] [blame]
Keun Soo Yim105bcaa2016-10-22 15:09:27 -07001/*
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
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070020#include <android/hardware/nfc/1.0/INfc.h>
21#include <android/hardware/nfc/1.0/INfcClientCallback.h>
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080022#include <android/hardware/nfc/1.0/types.h>
23#include <hardware/nfc.h>
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070024
Yuexi Ma6b872012017-03-10 00:42:13 -080025#include <VtsHalHidlTargetTestBase.h>
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080026#include <chrono>
27#include <condition_variable>
28#include <mutex>
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070029
30using ::android::hardware::nfc::V1_0::INfc;
31using ::android::hardware::nfc::V1_0::INfcClientCallback;
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080032using ::android::hardware::nfc::V1_0::NfcEvent;
33using ::android::hardware::nfc::V1_0::NfcStatus;
34using ::android::hardware::nfc::V1_0::NfcData;
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070035using ::android::hardware::Return;
36using ::android::hardware::Void;
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080037using ::android::hardware::hidl_vec;
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070038using ::android::sp;
39
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080040/* NCI Commands */
41#define CORE_RESET_CMD \
42 { 0x20, 0x00, 0x01, 0x00 }
Ruchi Kandoi185b5a72017-01-18 11:12:58 -080043#define CORE_RESET_CMD_CONFIG_RESET \
44 { 0x20, 0x00, 0x01, 0x01 }
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080045#define CORE_CONN_CREATE_CMD \
46 { 0x20, 0x04, 0x02, 0x01, 0x00 }
47#define INVALID_COMMAND \
48 { 0x20, 0x00, 0x00 }
49#define FAULTY_DATA_PACKET \
50 { 0x00, 0x00, 0xFF }
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070051
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080052#define LOOP_BACK_HEADER_SIZE 3
53#define SYNTAX_ERROR 5
54#define NUMBER_LOOPS 3922
55#define VERSION 0x11
56#define TIMEOUT_PERIOD 5
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070057
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070058// The main test class for NFC HIDL HAL.
Yuexi Ma6b872012017-03-10 00:42:13 -080059class NfcHidlTest : public ::testing::VtsHalHidlTargetTestBase {
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070060 public:
61 virtual void SetUp() override {
Yuexi Ma6b872012017-03-10 00:42:13 -080062 nfc_ = ::testing::VtsHalHidlTargetTestBase::getService<INfc>();
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080063 ASSERT_NE(nfc_, nullptr);
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070064
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080065 nfc_cb_ = new NfcClientCallback(*this);
66 ASSERT_NE(nfc_cb_, nullptr);
67
68 count = 0;
69 last_event_ = NfcEvent::ERROR;
70 last_status_ = NfcStatus::FAILED;
71
72 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
73 // Wait for OPEN_CPLT event
74 EXPECT_EQ(std::cv_status::no_timeout, wait());
75 EXPECT_EQ(NfcEvent::OPEN_CPLT, last_event_);
76 EXPECT_EQ(NfcStatus::OK, last_status_);
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070077 }
78
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080079 virtual void TearDown() override {
80 EXPECT_EQ(NfcStatus::OK, nfc_->close());
81 // Wait for CLOSE_CPLT event
82 EXPECT_EQ(std::cv_status::no_timeout, wait());
83 EXPECT_EQ(NfcEvent::CLOSE_CPLT, last_event_);
84 EXPECT_EQ(NfcStatus::OK, last_status_);
85 }
Keun Soo Yim105bcaa2016-10-22 15:09:27 -070086
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -080087 /* Used as a mechanism to inform the test about data/event callback */
88 inline void notify() {
89 std::unique_lock<std::mutex> lock(mtx);
90 count++;
91 cv.notify_one();
92 }
93
94 /* Test code calls this function to wait for data/event callback */
95 inline std::cv_status wait() {
96 std::unique_lock<std::mutex> lock(mtx);
97
98 std::cv_status status = std::cv_status::no_timeout;
99 auto now = std::chrono::system_clock::now();
100 while (count == 0) {
101 status = cv.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
102 if (status == std::cv_status::timeout) return status;
103 }
104 count--;
105 return status;
106 }
107
108 /* Callback class for data & Event. */
109 class NfcClientCallback : public INfcClientCallback {
110 NfcHidlTest& parent_;
111
112 public:
113 NfcClientCallback(NfcHidlTest& parent) : parent_(parent){};
114
115 virtual ~NfcClientCallback() = default;
116
117 /* sendEvent callback function - Records the Event & Status
118 * and notifies the TEST
119 **/
120 Return<void> sendEvent(NfcEvent event, NfcStatus event_status) override {
121 parent_.last_event_ = event;
122 parent_.last_status_ = event_status;
123 parent_.notify();
124 return Void();
125 };
126
127 /* sendData callback function. Records the data and notifies the TEST*/
128 Return<void> sendData(const NfcData& data) override {
129 size_t size = parent_.last_data_.size();
130 parent_.last_data_.resize(size + 1);
131 parent_.last_data_[size] = data;
132 parent_.notify();
133 return Void();
134 };
135 };
136
137 sp<INfc> nfc_;
138 sp<INfcClientCallback> nfc_cb_;
139 NfcEvent last_event_;
140 NfcStatus last_status_;
141 hidl_vec<NfcData> last_data_;
142
143 private:
144 std::mutex mtx;
145 std::condition_variable cv;
146 int count;
Keun Soo Yim105bcaa2016-10-22 15:09:27 -0700147};
148
Keun Soo Yim105bcaa2016-10-22 15:09:27 -0700149// A class for test environment setup (kept since this file is a template).
150class NfcHidlEnvironment : public ::testing::Environment {
151 public:
152 virtual void SetUp() {}
153 virtual void TearDown() {}
154
155 private:
156};
157
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800158/*
159 * OpenAndClose:
160 * Makes an open call, waits for NfcEvent.OPEN_CPLT
161 * Immediately calls close() and waits for NfcEvent.CLOSE_CPLT
162 * Since open and close calls are a part of SetUp() and TearDown(),
163 * the function definition is intentionally kept empty
164 */
165TEST_F(NfcHidlTest, OpenAndClose) {}
166
167/*
168 * WriteCoreReset:
169 * Sends CORE_RESET_CMD
170 * Waits for CORE_RESET_RSP
Ruchi Kandoi185b5a72017-01-18 11:12:58 -0800171 * Checks the status, version number and configuration status
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800172 */
173TEST_F(NfcHidlTest, WriteCoreReset) {
174 std::vector<uint8_t> cmd = CORE_RESET_CMD;
175 NfcData data = cmd;
176 EXPECT_EQ(data.size(), nfc_->write(data));
177 // Wait for CORE_RESET_RSP
178 EXPECT_EQ(std::cv_status::no_timeout, wait());
179 EXPECT_EQ(1ul, last_data_.size());
180 EXPECT_EQ(6ul, last_data_[0].size());
181 EXPECT_EQ((int)NfcStatus::OK, last_data_[0][3]);
182 EXPECT_GE(VERSION, last_data_[0][4]);
Ruchi Kandoi185b5a72017-01-18 11:12:58 -0800183 EXPECT_EQ(0ul, last_data_[0][5]);
184}
185
186/*
187 * WriteCoreResetConfigReset:
188 * Sends CORE_RESET_CMD_CONFIG_RESET
189 * Waits for CORE_RESET_RSP
190 * Checks the status, version number and configuration status
191 */
192TEST_F(NfcHidlTest, WriteCoreResetConfigReset) {
193 std::vector<uint8_t> cmd = CORE_RESET_CMD_CONFIG_RESET;
194 NfcData data = cmd;
195 EXPECT_EQ(data.size(), nfc_->write(data));
196 // Wait for CORE_RESET_RSP
197 EXPECT_EQ(std::cv_status::no_timeout, wait());
198 EXPECT_EQ(1ul, last_data_.size());
199 EXPECT_EQ(6ul, last_data_[0].size());
200 EXPECT_EQ((int)NfcStatus::OK, last_data_[0][3]);
201 EXPECT_GE(VERSION, last_data_[0][4]);
202 EXPECT_EQ(1ul, last_data_[0][5]);
Keun Soo Yim105bcaa2016-10-22 15:09:27 -0700203}
204
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800205/*
206 * WriteInvalidCommand:
207 * Sends an invalid command
208 * Waits for response
209 * Checks SYNTAX_ERROR status
210 */
211TEST_F(NfcHidlTest, WriteInvalidCommand) {
212 // Send an Error Command
213 std::vector<uint8_t> cmd = INVALID_COMMAND;
214 NfcData data = cmd;
215 EXPECT_EQ(data.size(), nfc_->write(data));
216 // Wait for RSP
217 EXPECT_EQ(std::cv_status::no_timeout, wait());
218 EXPECT_EQ(1ul, last_data_.size());
219 EXPECT_EQ(4ul, last_data_[0].size());
220 EXPECT_EQ(SYNTAX_ERROR, last_data_[0][3]);
221}
222
223/*
224 * WriteInvalidAndThenValidCommand:
225 * Sends an Faulty Data Packet
226 * Waits for CORE_INTERFACE_ERROR_NTF
227 * Checks SYNTAX_ERROR status
228 * Repeat for 100 times appending 0xFF each time to the packet
229 * Send CORE_CONN_CREATE_CMD for loop-back mode
230 * Check the response
231 */
232TEST_F(NfcHidlTest, WriteInvalidAndThenValidCommand) {
233 // Send an Error Data Packet
234 std::vector<uint8_t> cmd = FAULTY_DATA_PACKET;
235 NfcData data = cmd;
236 size_t size = data.size();
237
238 for (int i = 0; i < 100; i++) {
239 last_data_.resize(0);
240 data.resize(++size);
241 data[size - 1] = 0xFF;
242 EXPECT_EQ(data.size(), nfc_->write(data));
243 // Wait for CORE_INTERFACE_ERROR_NTF
244 EXPECT_EQ(std::cv_status::no_timeout, wait());
245 EXPECT_EQ(1ul, last_data_.size());
246 EXPECT_EQ(5ul, last_data_[0].size());
247 EXPECT_EQ(0x60, last_data_[0][0]);
248 EXPECT_EQ(0x08, last_data_[0][1]);
249 EXPECT_EQ(0x02, last_data_[0][2]);
250 EXPECT_EQ(SYNTAX_ERROR, last_data_[0][3]);
251 }
252
253 cmd = CORE_CONN_CREATE_CMD;
254 data = cmd;
255 last_data_.resize(0);
256 EXPECT_EQ(data.size(), nfc_->write(data));
257 // Wait for CORE_CONN_CREATE_RSP
258 EXPECT_EQ(std::cv_status::no_timeout, wait());
259 EXPECT_EQ(1ul, last_data_.size());
260 EXPECT_EQ(7ul, last_data_[0].size());
261 EXPECT_EQ((int)NfcStatus::OK, last_data_[0][3]);
262}
263/*
264 * Bandwidth:
265 * Sets the loop-back mode using CORE_CONN_CREATE_CMD
266 * Sends max payload size data
267 * Waits for the response
268 * Checks the data received
269 * Repeat to send total of 1Mb data
270 */
271TEST_F(NfcHidlTest, Bandwidth) {
272 std::vector<uint8_t> cmd = CORE_CONN_CREATE_CMD;
273 NfcData data = cmd;
274 EXPECT_EQ(data.size(), nfc_->write(data));
275 // Wait for CORE_CONN_CREATE_RSP
276 EXPECT_EQ(std::cv_status::no_timeout, wait());
277 EXPECT_EQ(1ul, last_data_.size());
278 EXPECT_EQ(7ul, last_data_[0].size());
279 EXPECT_EQ((int)NfcStatus::OK, last_data_[0][3]);
280 uint8_t conn_id = last_data_[0][6];
281 uint32_t max_payload_size = last_data_[0][4];
282
283 for (int loops = 0; loops < NUMBER_LOOPS; loops++) {
284 last_data_.resize(0);
285 data.resize(max_payload_size + LOOP_BACK_HEADER_SIZE);
286 data[0] = conn_id;
287 data[1] = 0x00;
288 data[2] = max_payload_size;
289 for (uint32_t i = 0; i < max_payload_size; i++) {
290 data[i + LOOP_BACK_HEADER_SIZE] = i;
291 }
292 EXPECT_EQ(max_payload_size + LOOP_BACK_HEADER_SIZE, nfc_->write(data));
293 // Wait for data and CORE_CONN_CREDITS_NTF
294 EXPECT_EQ(std::cv_status::no_timeout, wait());
295 EXPECT_EQ(std::cv_status::no_timeout, wait());
296 // Check if the same data was recieved back
297 EXPECT_EQ(2ul, last_data_.size());
Ruchi Kandoi185b5a72017-01-18 11:12:58 -0800298
299 /* It is possible that CORE_CONN_CREDITS_NTF is received before data,
300 * Find the order and do further checks depending on that */
301 uint8_t data_index = last_data_[0].size() == data.size() ? 0 : 1;
302 EXPECT_EQ(data.size(), last_data_[data_index].size());
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800303 for (size_t i = 0; i < data.size(); i++) {
Ruchi Kandoi185b5a72017-01-18 11:12:58 -0800304 EXPECT_EQ(data[i], last_data_[data_index][i]);
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800305 }
306
Ruchi Kandoi185b5a72017-01-18 11:12:58 -0800307 EXPECT_EQ(6ul, last_data_[!data_index].size());
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800308 // Check if the credit is refilled to 1
Ruchi Kandoi185b5a72017-01-18 11:12:58 -0800309 EXPECT_EQ(1, last_data_[!data_index][5]);
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800310 }
311}
312
313/*
314 * PowerCycle:
315 * Calls powerCycle()
316 * Waits for NfcEvent.OPEN_CPLT
317 * Checks status
318 */
319TEST_F(NfcHidlTest, PowerCycle) {
320 EXPECT_EQ(NfcStatus::OK, nfc_->powerCycle());
321 // Wait for NfcEvent.OPEN_CPLT
322 EXPECT_EQ(std::cv_status::no_timeout, wait());
323 EXPECT_EQ(NfcEvent::OPEN_CPLT, last_event_);
324 EXPECT_EQ(NfcStatus::OK, last_status_);
325}
326
327/*
Ruchi Kandoi3d7c5f92017-01-25 17:08:35 -0800328 * PowerCycleAfterClose:
329 * Calls powerCycle() after close()
330 * Checks status
331 */
332TEST_F(NfcHidlTest, PowerCycleAfterClose) {
333 EXPECT_EQ(NfcStatus::OK, nfc_->close());
334 // Wait for CLOSE_CPLT event
335 EXPECT_EQ(std::cv_status::no_timeout, wait());
336 EXPECT_EQ(NfcEvent::CLOSE_CPLT, last_event_);
337 EXPECT_EQ(NfcStatus::OK, last_status_);
338
339 EXPECT_EQ(NfcStatus::FAILED, nfc_->powerCycle());
340
341 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
342 // Wait for OPEN_CPLT event
343 EXPECT_EQ(std::cv_status::no_timeout, wait());
344 EXPECT_EQ(NfcEvent::OPEN_CPLT, last_event_);
345 EXPECT_EQ(NfcStatus::OK, last_status_);
346}
347
348/*
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800349 * CoreInitialized:
Ruchi Kandoi3d7c5f92017-01-25 17:08:35 -0800350 * Calls coreInitialized() with different data
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800351 * Waits for NfcEvent.POST_INIT_CPLT
352 */
353TEST_F(NfcHidlTest, CoreInitialized) {
354 NfcData data;
355 data.resize(1);
Ruchi Kandoi3d7c5f92017-01-25 17:08:35 -0800356 for (int i = 0; i <= 6; i++)
357 {
358 data[0] = i;
359 EXPECT_EQ(NfcStatus::OK, nfc_->coreInitialized(data));
360 // Wait for NfcEvent.POST_INIT_CPLT
361 EXPECT_EQ(std::cv_status::no_timeout, wait());
362 EXPECT_EQ(NfcEvent::POST_INIT_CPLT, last_event_);
363 }
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800364}
365
366/*
367 * ControlGranted:
368 * Calls controlGranted()
369 * Checks the return value
370 */
371TEST_F(NfcHidlTest, ControlGranted) {
372 EXPECT_EQ(NfcStatus::OK, nfc_->controlGranted());
373}
374
Ruchi Kandoi3d7c5f92017-01-25 17:08:35 -0800375/*
376 * ControlGrantedAfterClose:
377 * Call controlGranted() after close
378 * Checks the return value
379 */
380TEST_F(NfcHidlTest, ControlGrantedAfterClose) {
381 EXPECT_EQ(NfcStatus::OK, nfc_->close());
382 // Wait for CLOSE_CPLT event
383 EXPECT_EQ(std::cv_status::no_timeout, wait());
384 EXPECT_EQ(NfcEvent::CLOSE_CPLT, last_event_);
385 EXPECT_EQ(NfcStatus::OK, last_status_);
386
387 EXPECT_EQ(NfcStatus::OK, nfc_->controlGranted());
388
389 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
390 // Wait for OPEN_CPLT event
391 EXPECT_EQ(std::cv_status::no_timeout, wait());
392 EXPECT_EQ(NfcEvent::OPEN_CPLT, last_event_);
393 EXPECT_EQ(NfcStatus::OK, last_status_);
394}
395
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800396/* PreDiscover:
397 * Calls prediscover()
398 * Checks the return value
399 */
400TEST_F(NfcHidlTest, PreDiscover) {
401 EXPECT_EQ(NfcStatus::OK, nfc_->prediscover());
402}
403
Ruchi Kandoi3d7c5f92017-01-25 17:08:35 -0800404/*
405 * PreDiscoverAfterClose:
406 * Call prediscover() after close
407 * Checks the return value
408 */
409TEST_F(NfcHidlTest, PreDiscoverAfterClose) {
410 EXPECT_EQ(NfcStatus::OK, nfc_->close());
411 // Wait for CLOSE_CPLT event
412 EXPECT_EQ(std::cv_status::no_timeout, wait());
413 EXPECT_EQ(NfcEvent::CLOSE_CPLT, last_event_);
414 EXPECT_EQ(NfcStatus::OK, last_status_);
415
416 EXPECT_EQ(NfcStatus::OK, nfc_->prediscover());
417
418 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
419 // Wait for OPEN_CPLT event
420 EXPECT_EQ(std::cv_status::no_timeout, wait());
421 EXPECT_EQ(NfcEvent::OPEN_CPLT, last_event_);
422 EXPECT_EQ(NfcStatus::OK, last_status_);
423}
424
425/*
426 * CloseAfterClose:
427 * Calls close() multiple times
428 * Checks status
429 */
430TEST_F(NfcHidlTest, CloseAfterClose) {
431 EXPECT_EQ(NfcStatus::OK, nfc_->close());
432 // Wait for CLOSE_CPLT event
433 EXPECT_EQ(std::cv_status::no_timeout, wait());
434 EXPECT_EQ(NfcEvent::CLOSE_CPLT, last_event_);
435 EXPECT_EQ(NfcStatus::OK, last_status_);
436
437 EXPECT_EQ(NfcStatus::FAILED, nfc_->close());
438
439 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
440 // Wait for OPEN_CPLT event
441 EXPECT_EQ(std::cv_status::no_timeout, wait());
442 EXPECT_EQ(NfcEvent::OPEN_CPLT, last_event_);
443 EXPECT_EQ(NfcStatus::OK, last_status_);
444}
445
446
447/*
448 * OpenAfterOpen:
449 * Calls open() multiple times
450 * Checks status
451 */
452TEST_F(NfcHidlTest, OpenAfterOpen) {
453 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
454 EXPECT_EQ(NfcStatus::OK, nfc_->open(nfc_cb_));
455}
456
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800457int main(int argc, char** argv) {
Keun Soo Yim105bcaa2016-10-22 15:09:27 -0700458 ::testing::AddGlobalTestEnvironment(new NfcHidlEnvironment);
459 ::testing::InitGoogleTest(&argc, argv);
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800460
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800461 std::system("svc nfc disable"); /* Turn off NFC */
462 sleep(5);
463
Keun Soo Yim105bcaa2016-10-22 15:09:27 -0700464 int status = RUN_ALL_TESTS();
Ruchi Kandoi6b600362017-01-11 12:38:40 -0800465 LOG(INFO) << "Test result = " << status;
Ruchi Kandoiea37e1a2016-11-11 16:37:34 -0800466
467 std::system("svc nfc enable"); /* Turn on NFC */
468 sleep(5);
469
Keun Soo Yim105bcaa2016-10-22 15:09:27 -0700470 return status;
471}