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 "NfcAdaptation.h"
27 #include "nfc_chip_type_parser.h"
28
29 using namespace OHOS::HDI::Nfc::V1_0;
30 using namespace testing::ext;
31 using namespace std;
32 using namespace OHOS::HDI::Nfc;
33 using INfcV1_0 = OHOS::HDI::Nfc::V1_0::INfcInterface;
34 using OHOS::HDI::Nfc::V1_0::NfcStatus;
35 using OHOS::HDI::Nfc::V1_0::NfcEvent;
36 using OHOS::HDI::Nfc::V1_0::INfcCallback;
37 using OHOS::NFC::NfcChipTypeParser;
38
NfcInterfaceImplGetInstance(void)39 extern "C" INfcInterface *NfcInterfaceImplGetInstance(void)
40 {
41 using OHOS::HDI::Nfc::V1_0::NfcImpl;
42 NfcImpl *service = new (std::nothrow) NfcImpl();
43 if (service == nullptr) {
44 return nullptr;
45 }
46 return service;
47 }
48 tHAL_NFC_CBACK* NfcAdaptation::mHalCallback = nullptr;
49 tHAL_NFC_DATA_CBACK* NfcAdaptation::mHalDataCallback = nullptr;
50 namespace {
51 OHOS::sptr<INfcV1_0> mHal = nullptr;
52 OHOS::sptr<V1_0::INfcCallback> g_callbackV1_0 = nullptr;
53 OHOS::sptr<INfcCallback> mCallback = nullptr;
54 }
55
56 class NfcClientCallback : public INfcCallback {
57 public:
NfcClientCallback()58 NfcClientCallback()
59 {}
~NfcClientCallback()60 virtual ~NfcClientCallback()
61 {}
NfcClientCallback(tHAL_NFC_CBACK * eventCallback,tHAL_NFC_DATA_CBACK dataCallback)62 NfcClientCallback(tHAL_NFC_CBACK* eventCallback, tHAL_NFC_DATA_CBACK dataCallback)
63 {
64 mEventCallback = eventCallback;
65 mDataCallback = dataCallback;
66 };
67
OnData(const std::vector<uint8_t> & data)68 int32_t OnData(const std::vector<uint8_t>& data) override
69 {
70 if (mDataCallback != nullptr && !data.empty()) {
71 mDataCallback(data.size(), (uint8_t *)&data[0]);
72 }
73 return HDF_SUCCESS;
74 }
75
OnEvent(NfcEvent event,NfcStatus status)76 int32_t OnEvent(NfcEvent event, NfcStatus status) override
77 {
78 if (mEventCallback != nullptr) {
79 mEventCallback((uint8_t)event, (tHAL_NFC_STATUS)status);
80 }
81 return HDF_SUCCESS;
82 }
83 private:
84 tHAL_NFC_CBACK* mEventCallback;
85 tHAL_NFC_DATA_CBACK* mDataCallback;
86 };
87
88 class HdfNfcHdiTest : public testing::Test {
89 public:
90 static void SetUpTestCase();
91 static void TearDownTestCase();
92 void SetUp();
93 void TearDown();
94 };
SetUpTestCase()95 void HdfNfcHdiTest::SetUpTestCase()
96 {
97 mHal = INfcV1_0::Get();
98 }
TearDownTestCase()99 void HdfNfcHdiTest::TearDownTestCase()
100 {
101 }
SetUp()102 void HdfNfcHdiTest::SetUp()
103 {
104 }
TearDown()105 void HdfNfcHdiTest::TearDown()
106 {
107 }
108
EventCallback(uint8_t event,uint8_t status)109 static void EventCallback(uint8_t event, uint8_t status)
110 {
111 if (g_callbackV1_0 != nullptr) {
112 printf("EventCallback:%d,%d", event, status);
113 }
114 }
115
DataCallback(uint16_t len,uint8_t * data)116 static void DataCallback(uint16_t len, uint8_t *data)
117 {
118 if (g_callbackV1_0 != nullptr) {
119 printf("DataCallback:%d,%d", len, data[0]);
120 }
121 }
122
123 /**
124 * @tc.name: SUB_DriverSystem_Hdinfcopen_0100
125 * @tc.desc: Enables the nfc controller and initialize the nfc core.
126 * @tc.type: FUNC
127 */
128 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_Hdinfcopen_0100, TestSize.Level2)
129 {
130 if (!NfcChipTypeParser::IsSn110()) {
131 EXPECT_EQ(HDF_SUCCESS, 0);
132 }
133 else{
134 if (mHal == nullptr) {
135 ASSERT_NE(nullptr, mHal);
136 return;
137 }
138 mCallback = new NfcClientCallback(EventCallback, DataCallback);
139 if (mCallback == nullptr) {
140 ASSERT_NE(nullptr, mCallback);
141 return;
142 }
143 NfcStatus nfcbtType = NfcStatus::OK;
144 int32_t ret = mHal->Open(mCallback, nfcbtType);
145 EXPECT_EQ(HDF_SUCCESS, ret);
146 }
147 }
148
149 /**
150 * @tc.name: SUB_DriverSystem_HdinfcCoreInitialized_0200
151 * @tc.desc: Configures the nfc chip after initializing the nfc core.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcCoreInitialized_0200, TestSize.Level2)
155 {
156 if (!NfcChipTypeParser::IsSn110()) {
157 EXPECT_EQ(HDF_SUCCESS, 0);
158 }
159 else{
160 if (mHal == nullptr) {
161 ASSERT_NE(nullptr, mHal);
162 return;
163 }
164 std::vector<uint8_t> data(0);
165 NfcStatus nfcbtType = NfcStatus::OK;
166 int32_t ret = mHal->CoreInitialized(data, nfcbtType);
167 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
168 }
169 }
170
171 /**
172 * @tc.name: SUB_DriverSystem_HdinfcPrediscover_0300
173 * @tc.desc: Specifically configures the nfc chip before starting RF discovering.
174 * @tc.type: FUNC
175 */
176 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcPrediscover_0300, TestSize.Level2)
177 {
178 if (!NfcChipTypeParser::IsSn110()) {
179 EXPECT_EQ(HDF_SUCCESS, 0);
180 }
181 else{
182 if (mHal == nullptr) {
183 ASSERT_NE(nullptr, mHal);
184 return;
185 }
186 NfcStatus nfcbtType = NfcStatus::OK;
187 int32_t ret = mHal->Prediscover(nfcbtType);
188 EXPECT_EQ(HDF_SUCCESS, ret);
189 }
190 }
191
192 /**
193 * @tc.name: SUB_DriverSystem_HdinfcWrite_0400
194 * @tc.desc: Writes NCI data to the nfc core.
195 * @tc.type: FUNC
196 */
197 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcWrite_0400, TestSize.Level2)
198 {
199 if (!NfcChipTypeParser::IsSn110()) {
200 EXPECT_EQ(HDF_SUCCESS, 0);
201 }
202 else{
203 if (mHal == nullptr) {
204 ASSERT_NE(nullptr, mHal);
205 return;
206 }
207 std::vector<uint8_t> data;
208 NfcStatus nfcbtType = NfcStatus::OK;
209 int32_t ret = mHal->Write(data, nfcbtType);
210 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
211 }
212 }
213
214 /**
215 * @tc.name: SUB_DriverSystem_HdinfcControlGranted_0500
216 * @tc.desc: Sets the HDF to allow to send NCI data.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcControlGranted_0500, TestSize.Level2)
220 {
221 if (!NfcChipTypeParser::IsSn110()) {
222 EXPECT_EQ(HDF_SUCCESS, 0);
223 }
224 else{
225 if (mHal == nullptr) {
226 ASSERT_NE(nullptr, mHal);
227 return;
228 }
229 NfcStatus nfcbtType = NfcStatus::OK;
230 int32_t ret = mHal->ControlGranted(nfcbtType);
231 EXPECT_EQ(HDF_SUCCESS, ret);
232 }
233 }
234
235 /**
236 * @tc.name: SUB_DriverSystem_HdinfcPowerCycle_0600
237 * @tc.desc: Restarts the nfc controller according to each power cycle.
238 * @tc.type: FUNC
239 */
240 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcPowerCycle_0600, TestSize.Level2)
241 {
242 if (!NfcChipTypeParser::IsSn110()) {
243 EXPECT_EQ(HDF_SUCCESS, 0);
244 }
245 else{
246 if (mHal == nullptr) {
247 ASSERT_NE(nullptr, mHal);
248 return;
249 }
250 NfcStatus nfcbtType = NfcStatus::OK;
251 int32_t ret = mHal->PowerCycle(nfcbtType);
252 EXPECT_EQ(HDF_SUCCESS, ret);
253 }
254 }
255
256 /**
257 * @tc.name: SUB_DriverSystem_HdinfcIoctl_0700
258 * @tc.desc: Sends I/O control commands and data from the nfc stack to HDI.
259 * @tc.type: FUNC
260 */
261 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcIoctl_0700, TestSize.Level2)
262 {
263 if (!NfcChipTypeParser::IsSn110()) {
264 EXPECT_EQ(HDF_SUCCESS, 0);
265 }
266 else{
267 if (mHal == nullptr) {
268 ASSERT_NE(nullptr, mHal);
269 return;
270 }
271 uint8_t p_core_init_rsp_params = 0;
272 uint16_t data_len = sizeof(uint8_t);
273 std::vector<uint8_t> v_data(p_core_init_rsp_params,
274 p_core_init_rsp_params + data_len / sizeof(uint8_t));
275 NfcStatus nfcbtType = NfcStatus::OK;
276 NfcCommand nfcCommand = NfcCommand::CMD_INVALID ;
277 int32_t ret = mHal->Ioctl(nfcCommand, v_data, nfcbtType);
278 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
279 }
280 }
281
282 /**
283 * @tc.name: SUB_DriverSystem_HdinfcClose_0800
284 * @tc.desc: Disables the nfc controller and releases the resource.
285 * @tc.type: FUNC
286 */
287 HWTEST_F(HdfNfcHdiTest, SUB_DriverSystem_HdinfcClose_0800, TestSize.Level2)
288 {
289 if (!NfcChipTypeParser::IsSn110()) {
290 EXPECT_EQ(HDF_SUCCESS, 0);
291 }
292 else{
293 if (mHal == nullptr) {
294 ASSERT_NE(nullptr, mHal);
295 return;
296 }
297 NfcStatus nfcbtType = NfcStatus::OK;
298 int32_t ret = mHal->Close(nfcbtType);
299 EXPECT_EQ(HDF_SUCCESS, ret);
300 }
301 }
302
303