1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "nfc_impl.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include <vector>
20 #include "nfc_vendor_adaptions.h"
21 #include <gtest/gtest.h>
22 #include <hdf_device_desc.h>
23 #include <hdf_sbuf_ipc.h>
24 #include "v1_0/infc_interface.h"
25 #include "v1_0/nfc_types.h"
26 #include "nfc_chip_type_parser.h"
27
28 typedef uint8_t tHAL_NFC_STATUS;
29 typedef void(tHAL_NFC_STATUS_CBACK)(tHAL_NFC_STATUS status);
30 typedef void(tHAL_NFC_CBACK)(uint8_t event, tHAL_NFC_STATUS status);
31 typedef void(tHAL_NFC_DATA_CBACK)(uint16_t data_len, uint8_t* p_data);
32
33 using namespace OHOS::HDI::Nfc::V1_0;
34 using namespace testing::ext;
35 using namespace std;
36 using namespace OHOS::HDI::Nfc;
37 using INfcV1_0 = OHOS::HDI::Nfc::V1_0::INfcInterface;
38 using OHOS::HDI::Nfc::V1_0::NfcStatus;
39 using OHOS::HDI::Nfc::V1_0::NfcEvent;
40 using OHOS::HDI::Nfc::V1_0::INfcCallback;
41 using OHOS::NFC::NfcChipTypeParser;
42
43 namespace {
44 OHOS::sptr<INfcV1_0> mHal = nullptr;
45 OHOS::sptr<V1_0::INfcCallback> g_callbackV1_0 = nullptr;
46 OHOS::sptr<INfcCallback> mCallback = nullptr;
47 }
48
49 class NfcClientCallback : public INfcCallback {
50 public:
NfcClientCallback()51 NfcClientCallback()
52 {}
~NfcClientCallback()53 virtual ~NfcClientCallback()
54 {}
NfcClientCallback(tHAL_NFC_CBACK * eventCallback,tHAL_NFC_DATA_CBACK dataCallback)55 NfcClientCallback(tHAL_NFC_CBACK* eventCallback, tHAL_NFC_DATA_CBACK dataCallback)
56 {
57 mEventCallback = eventCallback;
58 mDataCallback = dataCallback;
59 };
60
OnData(const std::vector<uint8_t> & data)61 int32_t OnData(const std::vector<uint8_t>& data) override
62 {
63 if (mDataCallback != nullptr && !data.empty()) {
64 mDataCallback(data.size(), (uint8_t *)&data[0]);
65 }
66 return HDF_SUCCESS;
67 }
68
OnEvent(NfcEvent event,NfcStatus status)69 int32_t OnEvent(NfcEvent event, NfcStatus status) override
70 {
71 if (mEventCallback != nullptr) {
72 mEventCallback((uint8_t)event, (tHAL_NFC_STATUS)status);
73 }
74 return HDF_SUCCESS;
75 }
76 private:
77 tHAL_NFC_CBACK* mEventCallback;
78 tHAL_NFC_DATA_CBACK* mDataCallback;
79 };
80
81 class HdfNfcHdiTest : public testing::Test {
82 public:
83 static void SetUpTestCase();
84 static void TearDownTestCase();
85 void SetUp();
86 void TearDown();
87 };
SetUpTestCase()88 void HdfNfcHdiTest::SetUpTestCase()
89 {
90 mHal = INfcV1_0::Get();
91 }
TearDownTestCase()92 void HdfNfcHdiTest::TearDownTestCase()
93 {
94 }
SetUp()95 void HdfNfcHdiTest::SetUp()
96 {
97 }
TearDown()98 void HdfNfcHdiTest::TearDown()
99 {
100 }
101
EventCallback(uint8_t event,uint8_t status)102 static void EventCallback(uint8_t event, uint8_t status)
103 {
104 if (g_callbackV1_0 != nullptr) {
105 printf("EventCallback:%d,%d", event, status);
106 }
107 }
108
DataCallback(uint16_t len,uint8_t * data)109 static void DataCallback(uint16_t len, uint8_t *data)
110 {
111 if (g_callbackV1_0 != nullptr) {
112 printf("DataCallback:%d,%d", len, data[0]);
113 }
114 }
115
116 /**
117 * @tc.name: SUB_DriverSystem_Hdinfcopen_0100
118 * @tc.desc: Enables the nfc controller and initialize the nfc core.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_Hdinfcopen_0100, TestSize.Level2)
122 {
123 if (!NfcChipTypeParser::IsSupportedChipType()) {
124 EXPECT_EQ(HDF_SUCCESS, 0);
125 }
126 else{
127 if (mHal == nullptr) {
128 ASSERT_NE(nullptr, mHal);
129 return;
130 }
131 mCallback = new NfcClientCallback(EventCallback, DataCallback);
132 if (mCallback == nullptr) {
133 ASSERT_NE(nullptr, mCallback);
134 return;
135 }
136 NfcStatus nfcbtType = NfcStatus::OK;
137 int32_t ret = mHal->Open(mCallback, nfcbtType);
138 EXPECT_EQ(HDF_SUCCESS, ret);
139 }
140 }
141
142 /**
143 * @tc.name: SUB_DriverSystem_HdinfcCoreInitialized_0200
144 * @tc.desc: Configures the nfc chip after initializing the nfc core.
145 * @tc.type: FUNC
146 */
147 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcCoreInitialized_0200, TestSize.Level2)
148 {
149 if (!NfcChipTypeParser::IsSupportedChipType()) {
150 EXPECT_EQ(HDF_SUCCESS, 0);
151 }
152 else{
153 if (mHal == nullptr) {
154 ASSERT_NE(nullptr, mHal);
155 return;
156 }
157 std::vector<uint8_t> data(0);
158 NfcStatus nfcbtType = NfcStatus::OK;
159 int32_t ret = mHal->CoreInitialized(data, nfcbtType);
160 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
161 }
162 }
163
164 /**
165 * @tc.name: SUB_DriverSystem_HdinfcPrediscover_0300
166 * @tc.desc: Specifically configures the nfc chip before starting RF discovering.
167 * @tc.type: FUNC
168 */
169 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcPrediscover_0300, TestSize.Level2)
170 {
171 if (!NfcChipTypeParser::IsSupportedChipType()) {
172 EXPECT_EQ(HDF_SUCCESS, 0);
173 }
174 else{
175 if (mHal == nullptr) {
176 ASSERT_NE(nullptr, mHal);
177 return;
178 }
179 NfcStatus nfcbtType = NfcStatus::OK;
180 int32_t ret = mHal->Prediscover(nfcbtType);
181 EXPECT_EQ(HDF_SUCCESS, ret);
182 }
183 }
184
185 /**
186 * @tc.name: SUB_DriverSystem_HdinfcWrite_0400
187 * @tc.desc: Writes NCI data to the nfc core.
188 * @tc.type: FUNC
189 */
190 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcWrite_0400, TestSize.Level2)
191 {
192 if (!NfcChipTypeParser::IsSupportedChipType()) {
193 EXPECT_EQ(HDF_SUCCESS, 0);
194 }
195 else{
196 if (mHal == nullptr) {
197 ASSERT_NE(nullptr, mHal);
198 return;
199 }
200 std::vector<uint8_t> data;
201 NfcStatus nfcbtType = NfcStatus::OK;
202 int32_t ret = mHal->Write(data, nfcbtType);
203 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
204 }
205 }
206
207 /**
208 * @tc.name: SUB_DriverSystem_HdinfcControlGranted_0500
209 * @tc.desc: Sets the HDF to allow to send NCI data.
210 * @tc.type: FUNC
211 */
212 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcControlGranted_0500, TestSize.Level2)
213 {
214 if (!NfcChipTypeParser::IsSupportedChipType()) {
215 EXPECT_EQ(HDF_SUCCESS, 0);
216 }
217 else{
218 if (mHal == nullptr) {
219 ASSERT_NE(nullptr, mHal);
220 return;
221 }
222 NfcStatus nfcbtType = NfcStatus::OK;
223 int32_t ret = mHal->ControlGranted(nfcbtType);
224 EXPECT_EQ(HDF_SUCCESS, ret);
225 }
226 }
227
228 /**
229 * @tc.name: SUB_DriverSystem_HdinfcPowerCycle_0600
230 * @tc.desc: Restarts the nfc controller according to each power cycle.
231 * @tc.type: FUNC
232 */
233 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcPowerCycle_0600, TestSize.Level2)
234 {
235 if (!NfcChipTypeParser::IsSupportedChipType()) {
236 EXPECT_EQ(HDF_SUCCESS, 0);
237 }
238 else{
239 if (mHal == nullptr) {
240 ASSERT_NE(nullptr, mHal);
241 return;
242 }
243 NfcStatus nfcbtType = NfcStatus::OK;
244 int32_t ret = mHal->PowerCycle(nfcbtType);
245 EXPECT_EQ(HDF_SUCCESS, ret);
246 }
247 }
248
249 /**
250 * @tc.name: SUB_DriverSystem_HdinfcIoctl_0700
251 * @tc.desc: Sends I/O control commands and data from the nfc stack to HDI.
252 * @tc.type: FUNC
253 */
254 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcIoctl_0700, TestSize.Level2)
255 {
256 if (!NfcChipTypeParser::IsSupportedChipType()) {
257 EXPECT_EQ(HDF_SUCCESS, 0);
258 }
259 else{
260 if (mHal == nullptr) {
261 ASSERT_NE(nullptr, mHal);
262 return;
263 }
264 uint8_t p_core_init_rsp_params = 0;
265 uint16_t data_len = sizeof(uint8_t);
266 std::vector<uint8_t> v_data(p_core_init_rsp_params,
267 p_core_init_rsp_params + data_len / sizeof(uint8_t));
268 NfcStatus nfcbtType = NfcStatus::OK;
269 NfcCommand nfcCommand = NfcCommand::CMD_INVALID ;
270 int32_t ret = mHal->Ioctl(nfcCommand, v_data, nfcbtType);
271 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
272 }
273 }
274
275 /**
276 * @tc.name: SUB_DriverSystem_HdinfcClose_0800
277 * @tc.desc: Disables the nfc controller and releases the resource.
278 * @tc.type: FUNC
279 */
280 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcClose_0800, TestSize.Level2)
281 {
282 if (!NfcChipTypeParser::IsSupportedChipType()) {
283 EXPECT_EQ(HDF_SUCCESS, 0);
284 }
285 else{
286 if (mHal == nullptr) {
287 ASSERT_NE(nullptr, mHal);
288 return;
289 }
290 NfcStatus nfcbtType = NfcStatus::OK;
291 int32_t ret = mHal->Close(nfcbtType);
292 EXPECT_EQ(HDF_SUCCESS, ret);
293 }
294 }
295
296 /**
297 * @tc.name: SUB_DriverSystem_HdinfcClose_0900
298 * @tc.desc: the HdiNfc enum types test
299 * @tc.type: FUNC
300 */
301
302 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcClose_0900, TestSize.Level2)
303 {
304 NfcEvent open_cplt = NfcEvent::OPEN_CPLT;
305 std::cout << "NfcEvent OPEN_CPLT = " << open_cplt << std::endl;
306 EXPECT_EQ(open_cplt, 0);
307
308 NfcEvent close_cplt = NfcEvent::CLOSE_CPLT;
309 std::cout << "NfcEvent CLOSE_CPLT = " << close_cplt << std::endl;
310 EXPECT_EQ(close_cplt, 1);
311
312 NfcEvent post_init_cplt = NfcEvent::POST_INIT_CPLT;
313 std::cout << "NfcEvent POST_INIT_CPLT = " << post_init_cplt << std::endl;
314 EXPECT_EQ(post_init_cplt, 2);
315
316 NfcEvent per_discover_cplt = NfcEvent::PRE_DISCOVER_CPLT;
317 std::cout << "NfcEvent PRE_DISCOVER_CPLT = " << per_discover_cplt << std::endl;
318 EXPECT_EQ(per_discover_cplt, 3);
319
320 NfcEvent request_control = NfcEvent::REQUEST_CONTROL;
321 std::cout << "NfcEvent REQUEST_CONTROL = " << request_control << std::endl;
322 EXPECT_EQ(request_control, 4);
323
324 NfcEvent release_control = NfcEvent::RELEASE_CONTROL;
325 std::cout << "NfcEvent RELEASE_CONTROL = " << release_control << std::endl;
326 EXPECT_EQ(release_control, 5);
327
328 NfcEvent error = NfcEvent::ERROR;
329 std::cout << "NfcEvent ERROR = " << error << std::endl;
330 EXPECT_EQ(error, 6);
331
332 NfcEvent hci_network_reset = NfcEvent::HCI_NETWORK_RESET;
333 std::cout << "NfcEvent HCI_NETWORK_RESET = " << hci_network_reset << std::endl;
334 EXPECT_EQ(hci_network_reset, 7);
335
336 NfcStatus ok = NfcStatus::OK;
337 std::cout << "NfcStatus OK = " << ok << std::endl;
338 EXPECT_EQ(ok, 0);
339
340 NfcStatus failed = NfcStatus::FAILED;
341 std::cout << "NfcStatus FAILED = " << failed << std::endl;
342 EXPECT_EQ(failed, 1);
343
344 NfcStatus err_transport = NfcStatus::ERR_TRANSPORT;
345 std::cout << "NfcStatus ERR_TRANSPORT = " << err_transport << std::endl;
346 EXPECT_EQ(err_transport, 2);
347
348 NfcStatus err_cmd_timeout = NfcStatus::ERR_CMD_TIMEOUT;
349 std::cout << "NfcStatus ERR_CMD_TIMEOUT = " << err_cmd_timeout << std::endl;
350 EXPECT_EQ(err_cmd_timeout, 3);
351
352 NfcStatus refused = NfcStatus::REFUSED;
353 std::cout << "NfcStatus REFUSED = " << refused << std::endl;
354 EXPECT_EQ(refused, 4);
355
356 NfcCommand cmd_invalid = NfcCommand::CMD_INVALID;
357 std::cout << "NfcCommand CMD_INVALID = " << cmd_invalid << std::endl;
358 EXPECT_EQ(cmd_invalid, 0);
359 }