1 /*
2  * Copyright 2012-2021 NXP
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 #ifdef NXP_HW_SELF_TEST
18 
19 #include <phNxpConfig.h>
20 #include <phNxpLog.h>
21 #include <phNxpNciHal_SelfTest.h>
22 #include <phOsalNfc_Timer.h>
23 #include <pthread.h>
24 
25 /* Timeout value to wait for response from PN54X */
26 #define HAL_WRITE_RSP_TIMEOUT (2000)
27 #define HAL_WRITE_MAX_RETRY (10)
28 
29 /******************* Structures and definitions *******************************/
30 
31 typedef uint8_t (*st_validator_t)(nci_data_t* exp,
32                                   phTmlNfc_TransactInfo_t* act);
33 
34 phAntenna_St_Resp_t phAntenna_resp;
35 
36 typedef struct nci_test_data {
37   nci_data_t cmd;
38   nci_data_t exp_rsp;
39   nci_data_t exp_ntf;
40   st_validator_t rsp_validator;
41   st_validator_t ntf_validator;
42 
43 } nci_test_data_t;
44 
45 /******************* Global variables *****************************************/
46 
47 static int thread_running = 0;
48 static uint32_t timeoutTimerId = 0;
49 static int hal_write_timer_fired = 0;
50 
51 /* TML Context */
52 extern phTmlNfc_Context_t* gpphTmlNfc_Context;
53 
54 /* Global HAL Ref */
55 extern phNxpNciHal_Control_t nxpncihal_ctrl;
56 
57 /* Driver parameters */
58 phLibNfc_sConfig_t gDrvCfg;
59 
60 NFCSTATUS gtxldo_status = NFCSTATUS_FAILED;
61 NFCSTATUS gagc_value_status = NFCSTATUS_FAILED;
62 NFCSTATUS gagc_nfcld_status = NFCSTATUS_FAILED;
63 NFCSTATUS gagc_differential_status = NFCSTATUS_FAILED;
64 
65 static uint8_t st_validator_testEquals(nci_data_t* exp,
66                                        phTmlNfc_TransactInfo_t* act);
67 static uint8_t st_validator_null(nci_data_t* exp, phTmlNfc_TransactInfo_t* act);
68 static uint8_t st_validator_testSWP1_vltg(nci_data_t* exp,
69                                           phTmlNfc_TransactInfo_t* act);
70 static uint8_t st_validator_testAntenna_Txldo(nci_data_t* exp,
71                                               phTmlNfc_TransactInfo_t* act);
72 static uint8_t st_validator_testAntenna_AgcVal(nci_data_t* exp,
73                                                phTmlNfc_TransactInfo_t* act);
74 static uint8_t st_validator_testAntenna_AgcVal_FixedNfcLd(
75     nci_data_t* exp, phTmlNfc_TransactInfo_t* act);
76 static uint8_t st_validator_testAntenna_AgcVal_Differential(
77     nci_data_t* exp, phTmlNfc_TransactInfo_t* act);
78 
79 NFCSTATUS phNxpNciHal_getPrbsCmd(phNxpNfc_PrbsType_t prbs_type,
80                                  phNxpNfc_PrbsHwType_t hw_prbs_type,
81                                  uint8_t tech, uint8_t bitrate,
82                                  uint8_t* prbs_cmd, uint8_t prbs_cmd_len);
83 /* Test data to validate SWP line 2*/
84 static nci_test_data_t swp2_test_data[] = {
85     {{
86          0x04, {0x20, 0x00, 0x01, 0x00} /* cmd */
87      },
88      {
89          0x04, {0x40, 0x00, 0x01, 0x00} /* exp_rsp */
90      },
91      {
92          0x03, {0x60, 0x00, 0x0A} /* ext_ntf */
93      },
94      st_validator_testEquals, /* validator */
95      st_validator_null},
96     {{
97          0x05, {0x20, 0x01, 0x02, 0x00, 0x00} /* cmd */
98      },
99      {
100          0x04, {0x40, 0x01, 0x1E, 0x00} /* exp_rsp */
101      },
102      {
103          0x00, {0x00} /* ext_ntf */
104      },
105      st_validator_testEquals, /* validator */
106      st_validator_null},
107     {{
108          0x03, {0x2F, 0x02, 0x00} /* cmd */
109      },
110      {
111          0x04, {0x4F, 0x02, 0x05, 0x00} /* exp_rsp */
112      },
113      {
114          0x00, {0x00} /* ext_ntf */
115      },
116      st_validator_testEquals, /* validator */
117      st_validator_null},
118     {{
119          0x04, {0x2F, 0x3E, 0x01, 0x01} /* cmd */
120      },
121      {
122          0x04, {0x4F, 0x3E, 0x01, 0x00} /* exp_rsp */
123      },
124      {
125          0x04, {0x6F, 0x3E, 0x02, 0x00} /* ext_ntf */
126      },
127      st_validator_testEquals, /* validator */
128      st_validator_testEquals},
129 
130 };
131 
132 /* Test data to validate SWP line 1*/
133 static nci_test_data_t swp1_test_data[] = {
134 
135     {{
136          0x04, {0x20, 0x00, 0x01, 0x00} /* cmd */
137      },
138      {
139          0x04, {0x40, 0x00, 0x01, 0x00} /* exp_rsp */
140      },
141      {
142          0x03, {0x60, 0x00, 0x0A} /* ext_ntf */
143      },
144      st_validator_testEquals, /* validator */
145      st_validator_null},
146     {{
147          0x05, {0x20, 0x01, 0x02, 0x00, 0x00} /* cmd */
148      },
149      {
150          0x04, {0x40, 0x01, 0x1E, 0x00} /* exp_rsp */
151      },
152      {
153          0x00, {0x00} /* ext_ntf */
154      },
155      st_validator_testEquals, /* validator */
156      st_validator_null},
157     {{
158          0x03, {0x2F, 0x02, 0x00} /* cmd */
159      },
160      {
161          0x04, {0x4F, 0x02, 0x05, 0x00} /* exp_rsp */
162      },
163      {
164          0x00, {0x00} /* ext_ntf */
165      },
166      st_validator_testEquals, /* validator */
167      st_validator_null},
168     {{
169          0x04, {0x2F, 0x3E, 0x01, 0x00} /* cmd */
170      },
171      {
172          0x04, {0x4F, 0x3E, 0x01, 0x00} /* exp_rsp */
173      },
174      {
175          0x04, {0x6F, 0x3E, 0x02, 0x00} /* ext_ntf */
176      },
177 
178      st_validator_testEquals, /* validator */
179      st_validator_testSWP1_vltg},
180 };
181 
182 static nci_test_data_t prbs_test_data[] = {
183     {{
184          0x04, {0x20, 0x00, 0x01, 0x00} /* cmd */
185      },
186      {
187          0x04, {0x40, 0x00, 0x01, 0x00} /* exp_rsp */
188      },
189      {
190          0x03, {0x60, 0x00, 0x0A} /* ext_ntf */
191      },
192      st_validator_testEquals, /* validator */
193      st_validator_null},
194     {{
195          0x05, {0x20, 0x01, 0x02, 0x00, 0x00} /* cmd */
196      },
197      {
198          0x04, {0x40, 0x01, 0x1E, 0x00} /* exp_rsp */
199      },
200      {
201          0x00, {0x00} /* ext_ntf */
202      },
203      st_validator_testEquals, /* validator */
204      st_validator_null},
205     {{
206          0x04, {0x2F, 0x00, 0x01, 0x00} /* cmd */
207      },
208      {
209          0x04, {0x4F, 0x00, 0x01, 0x00} /* exp_rsp */
210      },
211      {
212          0x00, {0x00} /* ext_ntf */
213      },
214      st_validator_testEquals, /* validator */
215      st_validator_null}};
216 
217 /* for rf field test, first requires to disable the standby mode */
218 static nci_test_data_t rf_field_on_test_data[] = {
219     {{
220          0x04, {0x20, 0x00, 0x01, 0x00} /* cmd */
221      },
222      {
223          0x04, {0x40, 0x00, 0x01, 0x00} /* exp_rsp */
224      },
225      {
226          0x03, {0x60, 0x00, 0x0A} /* ext_ntf */
227      },
228      st_validator_testEquals, /* validator */
229      st_validator_null},
230     {{
231          0x05, {0x20, 0x01, 0x02, 0x00, 0x00} /* cmd */
232      },
233      {
234          0x04, {0x40, 0x01, 0x1E, 0x00} /* exp_rsp */
235      },
236      {
237          0x00, {0x00} /* ext_ntf */
238      },
239      st_validator_testEquals, /* validator */
240      st_validator_null},
241     {{
242          0x03, {0x2F, 0x02, 0x00} /* cmd */
243      },
244      {
245          0x04, {0x4F, 0x02, 0x05, 0x00} /* exp_rsp */
246      },
247      {
248          0x00, {0x00} /* ext_ntf */
249      },
250      st_validator_testEquals, /* validator */
251      st_validator_null},
252     {{
253          0x04, {0x2F, 0x00, 0x01, 0x00} /* cmd */
254      },
255      {
256          0x04, {0x4F, 0x00, 0x01, 0x00} /* exp_rsp */
257      },
258      {
259          0x00, {0x00} /* ext_ntf */
260      },
261      st_validator_testEquals, /* validator */
262      st_validator_null},
263     {{
264          0x05, {0x2F, 0x3D, 0x02, 0x20, 0x01} /* cmd */
265      },
266      {
267          0x04, {0x4F, 0x3D, 0x05, 0x00} /* exp_rsp */
268      },
269      {
270          0x00, {0x00} /* ext_ntf */
271      },
272      st_validator_testEquals, /* validator */
273      st_validator_null},
274     {{
275          0x04, {0x2F, 0x00, 0x01, 0x01} /* cmd */
276      },
277      {
278          0x04, {0x4F, 0x00, 0x01, 0x00} /* exp_rsp */
279      },
280      {
281          0x00, {0x00} /* ext_ntf */
282      },
283      st_validator_testEquals, /* validator */
284      st_validator_null}};
285 
286 static nci_test_data_t rf_field_off_test_data[] = {
287     {{
288          0x04, {0x20, 0x00, 0x01, 0x00} /* cmd */
289      },
290      {
291          0x04, {0x40, 0x00, 0x01, 0x00} /* exp_rsp */
292      },
293      {
294          0x03, {0x60, 0x00, 0x0A} /* ext_ntf */
295      },
296      st_validator_testEquals, /* validator */
297      st_validator_null},
298     {{
299          0x05, {0x20, 0x01, 0x02, 0x00, 0x00} /* cmd */
300      },
301      {
302          0x04, {0x40, 0x01, 0x1E, 0x00} /* exp_rsp */
303      },
304      {
305          0x00, {0x00} /* ext_ntf */
306      },
307      st_validator_testEquals, /* validator */
308      st_validator_null},
309     {{
310          0x03, {0x2F, 0x02, 0x00} /* cmd */
311      },
312      {
313          0x04, {0x4F, 0x02, 0x05, 0x00} /* exp_rsp */
314      },
315      {
316          0x00, {0x00} /* ext_ntf */
317      },
318      st_validator_testEquals, /* validator */
319      st_validator_null},
320     {{
321          0x04, {0x2F, 0x00, 0x01, 0x00} /* cmd */
322      },
323      {
324          0x04, {0x4F, 0x00, 0x01, 0x00} /* exp_rsp */
325      },
326      {
327          0x00, {0x00} /* ext_ntf */
328      },
329      st_validator_testEquals, /* validator */
330      st_validator_null},
331     {{
332          0x05, {0x2F, 0x3D, 0x02, 0x20, 0x00} /* cmd */
333      },
334      {
335          0x04, {0x4F, 0x3D, 0x05, 0x00} /* exp_rsp */
336      },
337      {
338          0x00, {0x00} /* ext_ntf */
339      },
340      st_validator_testEquals, /* validator */
341      st_validator_null},
342     {{
343          0x04, {0x2F, 0x00, 0x01, 0x01} /* cmd */
344      },
345      {
346          0x04, {0x4F, 0x00, 0x01, 0x00} /* exp_rsp */
347      },
348      {
349          0x00, {0x00} /* ext_ntf */
350      },
351      st_validator_testEquals, /* validator */
352      st_validator_null}};
353 
354 /* Download pin test data 1 */
355 static nci_test_data_t download_pin_test_data1[] = {
356     {{
357          0x04, {0x20, 0x00, 0x01, 0x00} /* cmd */
358      },
359      {
360          0x04, {0x40, 0x00, 0x01, 0x00} /* exp_rsp */
361      },
362      {
363          0x03, {0x60, 0x00, 0x0A} /* ext_ntf */
364      },
365      st_validator_testEquals, /* validator */
366      st_validator_null},
367 };
368 
369 /* Download pin test data 2 */
370 static nci_test_data_t download_pin_test_data2[] = {
371     {{
372          0x08, {0x00, 0x04, 0xD0, 0x11, 0x00, 0x00, 0x5B, 0x46} /* cmd */
373      },
374      {
375          0x08, {0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x87, 0x16} /* exp_rsp */
376      },
377      {
378          0x00, {0x00} /* ext_ntf */
379      },
380      st_validator_testEquals, /* validator */
381      st_validator_null},
382 };
383 /* Antenna self test data*/
384 static nci_test_data_t antenna_self_test_data[] = {
385     {{
386          0x04, {0x20, 0x00, 0x01, 0x00} /* cmd */
387      },
388      {
389          0x04, {0x40, 0x00, 0x01, 0x00} /* exp_rsp */
390      },
391      {
392          0x03, {0x60, 0x00, 0x0A} /* ext_ntf */
393      },
394      st_validator_testEquals, /* validator */
395      st_validator_null},
396     {{
397          0x05, {0x20, 0x01, 0x02, 0x00, 0x00} /* cmd */
398      },
399      {
400          0x04, {0x40, 0x01, 0x1E, 0x00} /* exp_rsp */
401      },
402      {
403          0x00, {0x00} /* ext_ntf */
404      },
405      st_validator_testEquals, /* validator */
406      st_validator_null},
407     {{
408          0x03, {0x2F, 0x02, 0x00} /* cmd */
409      },
410      {
411          0x04, {0x4F, 0x02, 0x05, 0x00} /* exp_rsp */
412      },
413      {
414          0x00, {0x00} /* ext_ntf */
415      },
416      st_validator_testEquals, /* validator */
417      st_validator_null},
418     {{
419          0x04, {0x2F, 0x00, 0x01, 0x00} /* cmd */
420      },
421      {
422          0x04, {0x4F, 0x00, 0x01, 0x00} /* exp_rsp */
423      },
424      {
425          0x00, {0x00} /* ext_ntf */
426      },
427      st_validator_testEquals, /* validator */
428      st_validator_null},
429     {{
430          0x05,
431          {0x2F, 0x3D, 0x02, 0x01, 0x80} /* TxLDO cureent measurement cmd */
432      },
433      {
434          0x03, {0x4F, 0x3D, 05} /* exp_rsp */
435      },
436      {
437          0x00, {0x00} /* ext_ntf */
438      },
439      st_validator_testAntenna_Txldo,
440      st_validator_null},
441     {{
442          0x07,
443          {0x2F, 0x3D, 0x04, 0x02, 0xC8, 0x60, 0x03} /* AGC measurement cmd */
444      },
445      {
446          0x03, {0x4F, 0x3D, 05} /* exp_rsp */
447      },
448      {
449          0x00, {0x00} /* ext_ntf */
450      },
451      st_validator_testAntenna_AgcVal,
452      st_validator_null},
453     {{
454          0x07,
455          {0x2F, 0x3D, 0x04, 0x04, 0x20, 0x08,
456           0x20} /* AGC with NFCLD measurement cmd */
457      },
458      {
459          0x03, {0x4F, 0x3D, 05} /* exp_rsp */
460      },
461      {
462          0x00, {0x00} /* ext_ntf */
463      },
464      st_validator_testAntenna_AgcVal_FixedNfcLd,
465      st_validator_null},
466     {{
467          0x07,
468          {0x2F, 0x3D, 0x04, 0x08, 0x8C, 0x60,
469           0x03} /* AGC with NFCLD measurement cmd */
470      },
471      {
472          0x03, {0x4F, 0x3D, 05} /* exp_rsp */
473      },
474      {
475          0x00, {0x00} /* ext_ntf */
476      },
477      st_validator_testAntenna_AgcVal_Differential,
478      st_validator_null},
479     {{
480          0x04, {0x2F, 0x00, 0x01, 0x01} /* cmd */
481      },
482      {
483          0x04, {0x4F, 0x00, 0x01, 0x00} /* exp_rsp */
484      },
485      {
486          0x00, {0x00} /* ext_ntf */
487      },
488      st_validator_testEquals, /* validator */
489      st_validator_null}};
490 
491 /************** Self test functions ***************************************/
492 
493 static uint8_t st_validator_testEquals(nci_data_t* exp,
494                                        phTmlNfc_TransactInfo_t* act);
495 static void hal_write_cb(void* pContext, phTmlNfc_TransactInfo_t* pInfo);
496 static void hal_write_rsp_timeout_cb(uint32_t TimerId, void* pContext);
497 static void hal_read_cb(void* pContext, phTmlNfc_TransactInfo_t* pInfo);
498 
499 /*******************************************************************************
500 **
501 ** Function         st_validator_null
502 **
503 ** Description      Null Validator
504 **
505 ** Returns          One
506 **
507 *******************************************************************************/
st_validator_null(nci_data_t * exp,phTmlNfc_TransactInfo_t * act)508 static uint8_t st_validator_null(nci_data_t* exp,
509                                  phTmlNfc_TransactInfo_t* act) {
510   UNUSED_PROP(exp);
511   UNUSED_PROP(act);
512   return 1;
513 }
514 
515 /*******************************************************************************
516 **
517 ** Function         st_validator_testSWP1_vltg
518 **
519 ** Description      Validator function to validate swp1 connection.
520 **
521 ** Returns          One if successful otherwise Zero.
522 **
523 *******************************************************************************/
st_validator_testSWP1_vltg(nci_data_t * exp,phTmlNfc_TransactInfo_t * act)524 static uint8_t st_validator_testSWP1_vltg(nci_data_t* exp,
525                                           phTmlNfc_TransactInfo_t* act) {
526   uint8_t result = 0;
527 
528   if (NULL == exp || NULL == act) {
529     return result;
530   }
531 
532   if ((act->wLength == 0x05) &&
533       (memcmp(exp->p_data, act->pBuff, exp->len) == 0)) {
534     if (act->pBuff[4] == 0x01 || act->pBuff[4] == 0x02) {
535       result = 1;
536     }
537   }
538 
539   return result;
540 }
541 
542 /*******************************************************************************
543 **
544 ** Function         st_validator_testAntenna_Txldo
545 **
546 ** Description      Validator function to validate Antenna TxLDO current
547 **                  measurement.
548 **
549 ** Returns          One if successful otherwise Zero.
550 **
551 *******************************************************************************/
st_validator_testAntenna_Txldo(nci_data_t * exp,phTmlNfc_TransactInfo_t * act)552 static uint8_t st_validator_testAntenna_Txldo(nci_data_t* exp,
553                                               phTmlNfc_TransactInfo_t* act) {
554   uint8_t result = 0;
555   long measured_val = 0;
556   int tolerance = 0;
557 
558   if (NULL == exp || NULL == act) {
559     return result;
560   }
561 
562   NXPLOG_NCIHAL_D("st_validator_testAntenna_Txldo = 0x%x", act->pBuff[3]);
563   if (0x05 == act->pBuff[2]) {
564     if (NFCSTATUS_SUCCESS == act->pBuff[3]) {
565       result = 1;
566       NXPLOG_NCIHAL_D("Antenna: TxLDO current measured raw value in mA : 0x%x",
567                       act->pBuff[4]);
568       if (0x00 == act->pBuff[5]) {
569         NXPLOG_NCIHAL_D("Measured range : 0x00 = 50 - 100 mA");
570         measured_val = ((0.40 * act->pBuff[4]) + 50);
571         NXPLOG_NCIHAL_D("TxLDO current absolute value in mA = %ld",
572                         measured_val);
573       } else {
574         NXPLOG_NCIHAL_D("Measured range : 0x01 = 20 - 70 mA");
575         measured_val = ((0.40 * act->pBuff[4]) + 20);
576         NXPLOG_NCIHAL_D("TxLDO current absolute value in mA = %ld",
577                         measured_val);
578       }
579 
580       tolerance = (phAntenna_resp.wTxdoMeasuredRangeMax *
581                    phAntenna_resp.wTxdoMeasuredTolerance) /
582                   100;
583       if ((measured_val <= phAntenna_resp.wTxdoMeasuredRangeMax + tolerance)) {
584         tolerance = (phAntenna_resp.wTxdoMeasuredRangeMin *
585                      phAntenna_resp.wTxdoMeasuredTolerance) /
586                     100;
587         if ((measured_val >=
588              phAntenna_resp.wTxdoMeasuredRangeMin - tolerance)) {
589           gtxldo_status = NFCSTATUS_SUCCESS;
590           NXPLOG_NCIHAL_D("Test Antenna Response for TxLDO measurement PASS");
591         } else {
592           gtxldo_status = NFCSTATUS_FAILED;
593           NXPLOG_NCIHAL_E("Test Antenna Response for TxLDO measurement FAIL");
594         }
595       } else {
596         gtxldo_status = NFCSTATUS_FAILED;
597         NXPLOG_NCIHAL_E("Test Antenna Response for TxLDO measurement FAIL");
598       }
599     } else {
600       gtxldo_status = NFCSTATUS_FAILED;
601       NXPLOG_NCIHAL_E(
602           "Test Antenna Response for TxLDO measurement failed: Invalid status");
603     }
604 
605   } else {
606     gtxldo_status = NFCSTATUS_FAILED;
607     NXPLOG_NCIHAL_E(
608         "Test Antenna Response for TxLDO measurement failed: Invalid payload "
609         "length");
610   }
611 
612   return result;
613 }
614 
615 /*******************************************************************************
616 **
617 ** Function         st_validator_testAntenna_AgcVal
618 **
619 ** Description      Validator function reads AGC value of antenna and print the
620 **                  info
621 **
622 ** Returns          One if successful otherwise Zero.
623 **
624 *******************************************************************************/
st_validator_testAntenna_AgcVal(nci_data_t * exp,phTmlNfc_TransactInfo_t * act)625 static uint8_t st_validator_testAntenna_AgcVal(nci_data_t* exp,
626                                                phTmlNfc_TransactInfo_t* act) {
627   uint8_t result = 0;
628   int agc_tolerance = 0;
629   long agc_val = 0;
630 
631   if (NULL == exp || NULL == act) {
632     return result;
633   }
634 
635   if (0x05 == act->pBuff[2]) {
636     if (NFCSTATUS_SUCCESS == act->pBuff[3]) {
637       result = 1;
638       agc_tolerance =
639           (phAntenna_resp.wAgcValue * phAntenna_resp.wAgcValueTolerance) / 100;
640       agc_val = ((act->pBuff[5] << 8) | (act->pBuff[4]));
641       NXPLOG_NCIHAL_D("AGC value : %ld", agc_val);
642       if (((phAntenna_resp.wAgcValue - agc_tolerance) <= agc_val) &&
643           (agc_val <= (phAntenna_resp.wAgcValue + agc_tolerance))) {
644         gagc_value_status = NFCSTATUS_SUCCESS;
645         NXPLOG_NCIHAL_D("Test Antenna Response for AGC Values  PASS");
646       } else {
647         gagc_value_status = NFCSTATUS_FAILED;
648         NXPLOG_NCIHAL_E("Test Antenna Response for AGC Values  FAIL");
649       }
650     } else {
651       gagc_value_status = NFCSTATUS_FAILED;
652       NXPLOG_NCIHAL_E("Test Antenna Response for AGC Values  FAIL");
653     }
654   } else {
655     gagc_value_status = NFCSTATUS_FAILED;
656     NXPLOG_NCIHAL_E(
657         "Test Antenna Response for AGC value failed: Invalid payload length");
658   }
659 
660   return result;
661 }
662 /*******************************************************************************
663 **
664 ** Function         st_validator_testAntenna_AgcVal_FixedNfcLd
665 **
666 ** Description      Validator function reads and print AGC value of
667 **                  antenna with fixed NFCLD
668 **
669 ** Returns          One if successful otherwise Zero.
670 **
671 *******************************************************************************/
st_validator_testAntenna_AgcVal_FixedNfcLd(nci_data_t * exp,phTmlNfc_TransactInfo_t * act)672 static uint8_t st_validator_testAntenna_AgcVal_FixedNfcLd(
673     nci_data_t* exp, phTmlNfc_TransactInfo_t* act) {
674   uint8_t result = 0;
675   int agc_nfcld_tolerance = 0;
676   long agc_nfcld = 0;
677 
678   if (NULL == exp || NULL == act) {
679     return result;
680   }
681 
682   if (0x05 == act->pBuff[2]) {
683     if (NFCSTATUS_SUCCESS == act->pBuff[3]) {
684       result = 1;
685       agc_nfcld_tolerance = (phAntenna_resp.wAgcValuewithfixedNFCLD *
686                              phAntenna_resp.wAgcValuewithfixedNFCLDTolerance) /
687                             100;
688       agc_nfcld = ((act->pBuff[5] << 8) | (act->pBuff[4]));
689       NXPLOG_NCIHAL_D("AGC value with Fixed Nfcld  : %ld", agc_nfcld);
690 
691       if (((phAntenna_resp.wAgcValuewithfixedNFCLD - agc_nfcld_tolerance) <=
692            agc_nfcld) &&
693           (agc_nfcld <=
694            (phAntenna_resp.wAgcValuewithfixedNFCLD + agc_nfcld_tolerance))) {
695         gagc_nfcld_status = NFCSTATUS_SUCCESS;
696         NXPLOG_NCIHAL_D(
697             "Test Antenna Response for AGC value with fixed NFCLD PASS");
698       } else {
699         gagc_nfcld_status = NFCSTATUS_FAILED;
700         NXPLOG_NCIHAL_E(
701             "Test Antenna Response for AGC value with fixed NFCLD FAIL");
702       }
703     } else {
704       gagc_nfcld_status = NFCSTATUS_FAILED;
705       NXPLOG_NCIHAL_E(
706           "Test Antenna Response for AGC value with fixed NFCLD failed: "
707           "Invalid status");
708     }
709   } else {
710     gagc_nfcld_status = NFCSTATUS_FAILED;
711     NXPLOG_NCIHAL_E(
712         "Test Antenna Response for AGC value with fixed NFCLD failed: Invalid "
713         "payload length");
714   }
715 
716   return result;
717 }
718 
719 /*******************************************************************************
720 **
721 ** Function         st_validator_testAntenna_AgcVal_Differential
722 **
723 ** Description      Reads the AGC value with open/short RM from buffer and print
724 **
725 ** Returns          One if successful otherwise Zero.
726 **
727 *******************************************************************************/
st_validator_testAntenna_AgcVal_Differential(nci_data_t * exp,phTmlNfc_TransactInfo_t * act)728 static uint8_t st_validator_testAntenna_AgcVal_Differential(
729     nci_data_t* exp, phTmlNfc_TransactInfo_t* act) {
730   uint8_t result = 0;
731   int agc_toleranceopne1 = 0;
732   int agc_toleranceopne2 = 0;
733   long agc_differentialOpne1 = 0;
734   long agc_differentialOpne2 = 0;
735 
736   if (NULL == exp || NULL == act) {
737     return result;
738   }
739 
740   if (0x05 == act->pBuff[2]) {
741     if (NFCSTATUS_SUCCESS == act->pBuff[3]) {
742       result = 1;
743       agc_toleranceopne1 = (phAntenna_resp.wAgcDifferentialWithOpen1 *
744                             phAntenna_resp.wAgcDifferentialWithOpenTolerance1) /
745                            100;
746       agc_toleranceopne2 = (phAntenna_resp.wAgcDifferentialWithOpen2 *
747                             phAntenna_resp.wAgcDifferentialWithOpenTolerance2) /
748                            100;
749       agc_differentialOpne1 = ((act->pBuff[5] << 8) | (act->pBuff[4]));
750       agc_differentialOpne2 = ((act->pBuff[7] << 8) | (act->pBuff[6]));
751       NXPLOG_NCIHAL_D("AGC value differential Opne 1  : %ld",
752                       agc_differentialOpne1);
753       NXPLOG_NCIHAL_D("AGC value differentialOpne  2 : %ld",
754                       agc_differentialOpne2);
755 
756       if (((agc_differentialOpne1 >=
757             phAntenna_resp.wAgcDifferentialWithOpen1 - agc_toleranceopne1) &&
758            (agc_differentialOpne1 <=
759             phAntenna_resp.wAgcDifferentialWithOpen1 + agc_toleranceopne1)) &&
760           ((agc_differentialOpne2 >=
761             phAntenna_resp.wAgcDifferentialWithOpen2 - agc_toleranceopne2) &&
762            (agc_differentialOpne2 <=
763             phAntenna_resp.wAgcDifferentialWithOpen2 + agc_toleranceopne2))) {
764         gagc_differential_status = NFCSTATUS_SUCCESS;
765         NXPLOG_NCIHAL_D("Test Antenna Response for AGC Differential Open PASS");
766       } else {
767         gagc_differential_status = NFCSTATUS_FAILED;
768         NXPLOG_NCIHAL_E(
769             "Test Antenna Response for AGC Differential Open  FAIL");
770       }
771     } else {
772       NXPLOG_NCIHAL_E(
773           "Test Antenna Response for AGC Differential failed: Invalid status");
774       gagc_differential_status = NFCSTATUS_FAILED;
775     }
776 
777   } else {
778     NXPLOG_NCIHAL_E(
779         "Test Antenna Response for AGC Differential failed: Invalid payload "
780         "length");
781     gagc_differential_status = NFCSTATUS_FAILED;
782   }
783 
784   return result;
785 }
786 /*******************************************************************************
787 **
788 ** Function         st_validator_testEquals
789 **
790 ** Description      Validator function to validate for equality between actual
791 **                  and expected values.
792 **
793 ** Returns          One if successful otherwise Zero.
794 **
795 *******************************************************************************/
st_validator_testEquals(nci_data_t * exp,phTmlNfc_TransactInfo_t * act)796 static uint8_t st_validator_testEquals(nci_data_t* exp,
797                                        phTmlNfc_TransactInfo_t* act) {
798   uint8_t result = 0;
799 
800   if (NULL == exp || NULL == act) {
801     return result;
802   }
803   if (exp->len <= act->wLength &&
804       (memcmp(exp->p_data, act->pBuff, exp->len) == 0)) {
805     result = 1;
806   }
807 
808   return result;
809 }
810 
811 /*******************************************************************************
812 **
813 ** Function         hal_write_rsp_timeout_cb
814 **
815 ** Description      Callback function for hal write response timer.
816 **
817 ** Returns          None
818 **
819 *******************************************************************************/
hal_write_rsp_timeout_cb(uint32_t timerId,void * pContext)820 static void hal_write_rsp_timeout_cb(uint32_t timerId, void* pContext) {
821   UNUSED_PROP(timerId);
822   NXPLOG_NCIHAL_E("hal_write_rsp_timeout_cb - write timeout!!!");
823   hal_write_timer_fired = 1;
824   hal_read_cb(pContext, NULL);
825 }
826 
827 /*******************************************************************************
828 **
829 ** Function         hal_write_cb
830 **
831 ** Description      Callback function for hal write.
832 **
833 ** Returns          None
834 **
835 *******************************************************************************/
hal_write_cb(void * pContext,phTmlNfc_TransactInfo_t * pInfo)836 static void hal_write_cb(void* pContext, phTmlNfc_TransactInfo_t* pInfo) {
837   phNxpNciHal_Sem_t* p_cb_data = (phNxpNciHal_Sem_t*)pContext;
838 
839   if (pInfo->wStatus == NFCSTATUS_SUCCESS) {
840     NXPLOG_NCIHAL_D("write successful status = 0x%x", pInfo->wStatus);
841   } else {
842     NXPLOG_NCIHAL_E("write error status = 0x%x", pInfo->wStatus);
843   }
844 
845   p_cb_data->status = pInfo->wStatus;
846   SEM_POST(p_cb_data);
847 
848   return;
849 }
850 
851 /*******************************************************************************
852 **
853 ** Function         hal_read_cb
854 **
855 ** Description      Callback function for hal read.
856 **
857 ** Returns          None
858 **
859 *******************************************************************************/
hal_read_cb(void * pContext,phTmlNfc_TransactInfo_t * pInfo)860 static void hal_read_cb(void* pContext, phTmlNfc_TransactInfo_t* pInfo) {
861   phNxpNciHal_Sem_t* p_cb_data = (phNxpNciHal_Sem_t*)pContext;
862   NFCSTATUS status;
863   if (hal_write_timer_fired == 1) {
864     NXPLOG_NCIHAL_D("hal_read_cb - response timeout occurred");
865 
866     hal_write_timer_fired = 0;
867     p_cb_data->status = NFCSTATUS_RESPONSE_TIMEOUT;
868     status = phTmlNfc_ReadAbort();
869   } else {
870     NFCSTATUS status = phOsalNfc_Timer_Stop(timeoutTimerId);
871 
872     if (NFCSTATUS_SUCCESS == status) {
873       NXPLOG_NCIHAL_D("Response timer stopped");
874     } else {
875       NXPLOG_NCIHAL_E("Response timer stop ERROR!!!");
876       p_cb_data->status = NFCSTATUS_FAILED;
877     }
878     if (pInfo == NULL) {
879       NXPLOG_NCIHAL_E("Empty TransactInfo");
880       p_cb_data->status = NFCSTATUS_FAILED;
881     } else {
882       if (pInfo->wStatus == NFCSTATUS_SUCCESS) {
883         NXPLOG_NCIHAL_D("hal_read_cb successful status = 0x%x", pInfo->wStatus);
884         p_cb_data->status = NFCSTATUS_SUCCESS;
885       } else {
886         NXPLOG_NCIHAL_E("hal_read_cb error status = 0x%x", pInfo->wStatus);
887         p_cb_data->status = NFCSTATUS_FAILED;
888       }
889 
890       p_cb_data->status = pInfo->wStatus;
891       nci_test_data_t* test_data = (nci_test_data_t*)p_cb_data->pContext;
892 
893       if (test_data->exp_rsp.len == 0) {
894         /* Compare the actual notification with expected notification.*/
895         if (test_data->ntf_validator(&(test_data->exp_ntf), pInfo) == 1) {
896           p_cb_data->status = NFCSTATUS_SUCCESS;
897         } else {
898           p_cb_data->status = NFCSTATUS_FAILED;
899         }
900       }
901 
902       /* Compare the actual response with expected response.*/
903       else if (test_data->rsp_validator(&(test_data->exp_rsp), pInfo) == 1) {
904         p_cb_data->status = NFCSTATUS_SUCCESS;
905       } else {
906         p_cb_data->status = NFCSTATUS_FAILED;
907       }
908 
909       test_data->exp_rsp.len = 0;
910     }
911   }
912 
913   SEM_POST(p_cb_data);
914 
915   return;
916 }
917 
918 /*******************************************************************************
919 **
920 ** Function         phNxpNciHal_test_rx_thread
921 **
922 ** Description      Thread to fetch and process messages from message queue.
923 **
924 ** Returns          NULL
925 **
926 *******************************************************************************/
phNxpNciHal_test_rx_thread(void * arg)927 static void* phNxpNciHal_test_rx_thread(void* arg) {
928   phLibNfc_Message_t msg;
929   UNUSED_PROP(arg);
930   NXPLOG_NCIHAL_D("Self test thread started");
931 
932   thread_running = 1;
933 
934   while (thread_running == 1) {
935     /* Fetch next message from the NFC stack message queue */
936     if (phDal4Nfc_msgrcv(gDrvCfg.nClientId, &msg, 0, 0) == -1) {
937       NXPLOG_NCIHAL_E("Received bad message");
938       continue;
939     }
940 
941     if (thread_running == 0) {
942       break;
943     }
944 
945     switch (msg.eMsgType) {
946       case PH_LIBNFC_DEFERREDCALL_MSG: {
947         phLibNfc_DeferredCall_t* deferCall =
948             (phLibNfc_DeferredCall_t*)(msg.pMsgData);
949 
950         REENTRANCE_LOCK();
951         deferCall->pCallback(deferCall->pParameter);
952         REENTRANCE_UNLOCK();
953 
954         break;
955       }
956     }
957   }
958 
959   NXPLOG_NCIHAL_D("Self test thread stopped");
960 
961   return NULL;
962 }
963 
964 /*******************************************************************************
965 **
966 ** Function         phNxpNciHal_readLocked
967 **
968 ** Description      Reads response and notification from NFCC and waits for
969 **                  read completion, for a definitive timeout value.
970 **
971 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED,
972 **                  NFCSTATUS_RESPONSE_TIMEOUT in case of timeout.
973 **
974 *******************************************************************************/
phNxpNciHal_readLocked(nci_test_data_t * pData)975 static NFCSTATUS phNxpNciHal_readLocked(nci_test_data_t* pData) {
976   NFCSTATUS status = NFCSTATUS_SUCCESS;
977   phNxpNciHal_Sem_t cb_data;
978   uint16_t read_len = 16;
979   /* RX Buffer */
980   uint32_t rx_data[NCI_MAX_DATA_LEN];
981 
982   /* Create the local semaphore */
983   if (phNxpNciHal_init_cb_data(&cb_data, pData) != NFCSTATUS_SUCCESS) {
984     NXPLOG_NCIHAL_D("phTmlNfc_Read Create cb data failed");
985     status = NFCSTATUS_FAILED;
986     goto clean_and_return;
987   }
988 
989   /* call read pending */
990   status =
991       phTmlNfc_Read((uint8_t*)rx_data, (uint16_t)read_len,
992                     (pphTmlNfc_TransactCompletionCb_t)&hal_read_cb, &cb_data);
993 
994   if (status != NFCSTATUS_PENDING) {
995     NXPLOG_NCIHAL_E("TML Read status error status = %x", status);
996     status = NFCSTATUS_FAILED;
997     goto clean_and_return;
998   }
999 
1000   status = phOsalNfc_Timer_Start(timeoutTimerId, HAL_WRITE_RSP_TIMEOUT,
1001                                  &hal_write_rsp_timeout_cb, &cb_data);
1002 
1003   if (NFCSTATUS_SUCCESS == status) {
1004     NXPLOG_NCIHAL_D("Response timer started");
1005   } else {
1006     NXPLOG_NCIHAL_E("Response timer not started");
1007     status = NFCSTATUS_FAILED;
1008     goto clean_and_return;
1009   }
1010 
1011   /* Wait for callback response */
1012   if (SEM_WAIT(cb_data)) {
1013     NXPLOG_NCIHAL_E("phTmlNfc_Read semaphore error");
1014     status = NFCSTATUS_FAILED;
1015     goto clean_and_return;
1016   }
1017 
1018   if (cb_data.status == NFCSTATUS_RESPONSE_TIMEOUT) {
1019     NXPLOG_NCIHAL_E("Response timeout!!!");
1020     status = NFCSTATUS_RESPONSE_TIMEOUT;
1021     goto clean_and_return;
1022   }
1023 
1024   if (cb_data.status != NFCSTATUS_SUCCESS) {
1025     NXPLOG_NCIHAL_E("phTmlNfc_Read failed  ");
1026     status = NFCSTATUS_FAILED;
1027     goto clean_and_return;
1028   }
1029 
1030 clean_and_return:
1031   phNxpNciHal_cleanup_cb_data(&cb_data);
1032 
1033   return status;
1034 }
1035 
1036 /*******************************************************************************
1037 **
1038 ** Function         phNxpNciHal_writeLocked
1039 **
1040 ** Description      Send command to NFCC and waits for cmd write completion, for
1041 **                  a definitive timeout value.
1042 **
1043 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED,
1044 **                  NFCSTATUS_RESPONSE_TIMEOUT in case of timeout.
1045 **
1046 *******************************************************************************/
phNxpNciHal_writeLocked(nci_test_data_t * pData)1047 static NFCSTATUS phNxpNciHal_writeLocked(nci_test_data_t* pData) {
1048   NFCSTATUS status = NFCSTATUS_SUCCESS;
1049 
1050   phNxpNciHal_Sem_t cb_data;
1051   int retryCnt = 0;
1052 
1053   /* Create the local semaphore */
1054   if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS) {
1055     NXPLOG_NCIHAL_D("phTmlNfc_Write Create cb data failed");
1056     goto clean_and_return;
1057   }
1058 
1059 retry:
1060   status =
1061       phTmlNfc_Write(pData->cmd.p_data, pData->cmd.len,
1062                      (pphTmlNfc_TransactCompletionCb_t)&hal_write_cb, &cb_data);
1063 
1064   if (status != NFCSTATUS_PENDING) {
1065     NXPLOG_NCIHAL_E("phTmlNfc_Write status error");
1066     goto clean_and_return;
1067   }
1068 
1069   /* Wait for callback response */
1070   if (SEM_WAIT(cb_data)) {
1071     NXPLOG_NCIHAL_E("write_unlocked semaphore error");
1072     status = NFCSTATUS_FAILED;
1073     goto clean_and_return;
1074   }
1075 
1076   if (cb_data.status != NFCSTATUS_SUCCESS && retryCnt < HAL_WRITE_MAX_RETRY) {
1077     retryCnt++;
1078     NXPLOG_NCIHAL_D(
1079         "write_unlocked failed - PN54X Maybe in Standby Mode - Retry %d",
1080         retryCnt);
1081     goto retry;
1082   }
1083 
1084   status = cb_data.status;
1085 
1086 clean_and_return:
1087   phNxpNciHal_cleanup_cb_data(&cb_data);
1088 
1089   return status;
1090 }
1091 
1092 /*******************************************************************************
1093 **
1094 ** Function         phNxpNciHal_performTest
1095 **
1096 ** Description      Performs a single cycle of command,response and
1097 **                  notification.
1098 **
1099 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED,
1100 **
1101 *******************************************************************************/
phNxpNciHal_performTest(nci_test_data_t * pData)1102 NFCSTATUS phNxpNciHal_performTest(nci_test_data_t* pData) {
1103   NFCSTATUS status = NFCSTATUS_SUCCESS;
1104 
1105   if (NULL == pData) {
1106     return NFCSTATUS_FAILED;
1107   }
1108 
1109   CONCURRENCY_LOCK();
1110 
1111   status = phNxpNciHal_writeLocked(pData);
1112 
1113   if (status == NFCSTATUS_RESPONSE_TIMEOUT) {
1114     goto clean_and_return;
1115   }
1116   if (status != NFCSTATUS_SUCCESS) {
1117     goto clean_and_return;
1118   }
1119 
1120   status = phNxpNciHal_readLocked(pData);
1121 
1122   if (status != NFCSTATUS_SUCCESS) {
1123     goto clean_and_return;
1124   }
1125 
1126   if (0 != pData->exp_ntf.len) {
1127     status = phNxpNciHal_readLocked(pData);
1128 
1129     if (status != NFCSTATUS_SUCCESS) {
1130       goto clean_and_return;
1131     }
1132   }
1133 
1134 clean_and_return:
1135   CONCURRENCY_UNLOCK();
1136   return status;
1137 }
1138 
1139 /*******************************************************************************
1140  **
1141  ** Function         phNxpNciHal_TestMode_open
1142  **
1143  ** Description      It opens the physical connection with NFCC (PN54X) and
1144  **                  creates required client thread for operation.
1145  **
1146  ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1147  **
1148  ******************************************************************************/
phNxpNciHal_TestMode_open(void)1149 NFCSTATUS phNxpNciHal_TestMode_open(void) {
1150   /* Thread */
1151   pthread_t test_rx_thread;
1152 
1153   phOsalNfc_Config_t tOsalConfig;
1154   phTmlNfc_Config_t tTmlConfig;
1155   char* nfc_dev_node = NULL;
1156   const uint16_t max_len = 260;
1157   NFCSTATUS status = NFCSTATUS_SUCCESS;
1158   int8_t ret_val = 0x00;
1159   /* initialize trace level */
1160   phNxpLog_InitializeLogLevel();
1161 
1162   if (phNxpNciHal_init_monitor() == NULL) {
1163     NXPLOG_NCIHAL_E("Init monitor failed");
1164     return NFCSTATUS_FAILED;
1165   }
1166 
1167   CONCURRENCY_LOCK();
1168 
1169   memset(&tOsalConfig, 0x00, sizeof(tOsalConfig));
1170   memset(&tTmlConfig, 0x00, sizeof(tTmlConfig));
1171 
1172   /* Read the nfc device node name */
1173   nfc_dev_node = (char*)malloc(max_len * sizeof(char));
1174   if (nfc_dev_node == NULL) {
1175     NXPLOG_NCIHAL_D("malloc of nfc_dev_node failed ");
1176     goto clean_and_return;
1177   } else if (!GetNxpStrValue(NAME_NXP_NFC_DEV_NODE, nfc_dev_node, max_len)) {
1178     NXPLOG_NCIHAL_D(
1179         "Invalid nfc device node name keeping the default device node "
1180         "/dev/pn54x");
1181     strlcpy(nfc_dev_node, "/dev/pn54x", (max_len * sizeof(char)));
1182   }
1183 
1184   gDrvCfg.nClientId = phDal4Nfc_msgget(0, 0600);
1185   gDrvCfg.nLinkType = ENUM_LINK_TYPE_I2C; /* For PN54X */
1186   tTmlConfig.pDevName = (int8_t*)nfc_dev_node;
1187   tOsalConfig.dwCallbackThreadId = (uintptr_t)gDrvCfg.nClientId;
1188   tOsalConfig.pLogFile = NULL;
1189   tTmlConfig.dwGetMsgThreadId = (uintptr_t)gDrvCfg.nClientId;
1190   nxpncihal_ctrl.gDrvCfg.nClientId = (uintptr_t)gDrvCfg.nClientId;
1191 
1192   /* Initialize TML layer */
1193   status = phTmlNfc_Init(&tTmlConfig);
1194   if (status != NFCSTATUS_SUCCESS) {
1195     NXPLOG_NCIHAL_E("phTmlNfc_Init Failed");
1196     goto clean_and_return;
1197   } else {
1198     if (nfc_dev_node != NULL) {
1199       free(nfc_dev_node);
1200       nfc_dev_node = NULL;
1201     }
1202   }
1203 
1204   pthread_attr_t attr;
1205   pthread_attr_init(&attr);
1206   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1207   ret_val =
1208       pthread_create(&test_rx_thread, &attr, phNxpNciHal_test_rx_thread, NULL);
1209   pthread_attr_destroy(&attr);
1210   if (ret_val != 0) {
1211     NXPLOG_NCIHAL_E("pthread_create failed");
1212     phTmlNfc_Shutdown_CleanUp();
1213     goto clean_and_return;
1214   }
1215 
1216   timeoutTimerId = phOsalNfc_Timer_Create();
1217 
1218   if (timeoutTimerId == 0xFFFF) {
1219     NXPLOG_NCIHAL_E("phOsalNfc_Timer_Create failed");
1220   } else {
1221     NXPLOG_NCIHAL_D("phOsalNfc_Timer_Create SUCCESS");
1222   }
1223   CONCURRENCY_UNLOCK();
1224 
1225   return NFCSTATUS_SUCCESS;
1226 
1227 clean_and_return:
1228   CONCURRENCY_UNLOCK();
1229   if (nfc_dev_node != NULL) {
1230     free(nfc_dev_node);
1231     nfc_dev_node = NULL;
1232   }
1233   phNxpNciHal_cleanup_monitor();
1234   return NFCSTATUS_FAILED;
1235 }
1236 
1237 /*******************************************************************************
1238  **
1239  ** Function         phNxpNciHal_TestMode_close
1240  **
1241  ** Description      This function close the NFCC interface and free all
1242  **                  resources.
1243  **
1244  ** Returns          None.
1245  **
1246  ******************************************************************************/
1247 
phNxpNciHal_TestMode_close()1248 void phNxpNciHal_TestMode_close() {
1249   NFCSTATUS status = NFCSTATUS_SUCCESS;
1250 
1251   CONCURRENCY_LOCK();
1252 
1253   if (NULL != gpphTmlNfc_Context->pDevHandle) {
1254     /* Abort any pending read and write */
1255     status = phTmlNfc_ReadAbort();
1256     status = phTmlNfc_WriteAbort();
1257 
1258     phOsalNfc_Timer_Cleanup();
1259 
1260     status = phTmlNfc_Shutdown_CleanUp();
1261 
1262     NXPLOG_NCIHAL_D("phNxpNciHal_close return status = %d", status);
1263 
1264     thread_running = 0;
1265 
1266     phDal4Nfc_msgrelease(gDrvCfg.nClientId);
1267 
1268     status = phOsalNfc_Timer_Delete(timeoutTimerId);
1269   }
1270 
1271   CONCURRENCY_UNLOCK();
1272 
1273   phNxpNciHal_cleanup_monitor();
1274 
1275   /* Return success always */
1276   return;
1277 }
1278 
1279 /*******************************************************************************
1280  **
1281  ** Function         phNxpNciHal_SwpTest
1282  **
1283  ** Description      Test function to validate the SWP line. SWP line number is
1284  **                  is sent as parameter to the API.
1285  **
1286  ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1287  **
1288  ******************************************************************************/
1289 
phNxpNciHal_SwpTest(uint8_t swp_line)1290 NFCSTATUS phNxpNciHal_SwpTest(uint8_t swp_line) {
1291   NFCSTATUS status = NFCSTATUS_SUCCESS;
1292   int len = 0;
1293   int cnt = 0;
1294 
1295   NXPLOG_NCIHAL_D("phNxpNciHal_SwpTest - start\n");
1296 
1297   if (swp_line == 0x01) {
1298     len = (sizeof(swp1_test_data) / sizeof(swp1_test_data[0]));
1299 
1300     for (cnt = 0; cnt < len; cnt++) {
1301       status = phNxpNciHal_performTest(&(swp1_test_data[cnt]));
1302       if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1303         break;
1304       }
1305     }
1306   } else if (swp_line == 0x02) {
1307     len = (sizeof(swp2_test_data) / sizeof(swp2_test_data[0]));
1308 
1309     for (cnt = 0; cnt < len; cnt++) {
1310       status = phNxpNciHal_performTest(&(swp2_test_data[cnt]));
1311       if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1312         break;
1313       }
1314     }
1315   } else {
1316     status = NFCSTATUS_FAILED;
1317   }
1318 
1319   if (status == NFCSTATUS_SUCCESS) {
1320     NXPLOG_NCIHAL_D("phNxpNciHal_SwpTest - SUCCESS\n");
1321   } else {
1322     NXPLOG_NCIHAL_D("phNxpNciHal_SwpTest - FAILED\n");
1323   }
1324 
1325   NXPLOG_NCIHAL_D("phNxpNciHal_SwpTest - end\n");
1326 
1327   return status;
1328 }
1329 
1330 /*******************************************************************************
1331  **
1332  ** Function         phNxpNciHal_PrbsTestStart
1333  **
1334  ** Description      Test function start RF generation for RF technology and bit
1335  **                  rate. RF technology and bit rate are sent as parameter to
1336  **                  the API.
1337  **
1338  ** Returns          NFCSTATUS_SUCCESS if RF generation successful,
1339  **                  otherwise NFCSTATUS_FAILED.
1340  **
1341  ******************************************************************************/
1342 
phNxpNciHal_PrbsTestStart(phNxpNfc_PrbsType_t prbs_type,phNxpNfc_PrbsHwType_t hw_prbs_type,phNxpNfc_Tech_t tech,phNxpNfc_Bitrate_t bitrate)1343 NFCSTATUS phNxpNciHal_PrbsTestStart(phNxpNfc_PrbsType_t prbs_type,
1344                                     phNxpNfc_PrbsHwType_t hw_prbs_type,
1345                                     phNxpNfc_Tech_t tech,
1346                                     phNxpNfc_Bitrate_t bitrate) {
1347   NFCSTATUS status = NFCSTATUS_FAILED;
1348 
1349   nci_test_data_t prbs_cmd_data;
1350 
1351   uint8_t rsp_cmd_info[] = {0x4F, 0x30, 0x01, 0x00};
1352   prbs_cmd_data.cmd.len = 0x09;
1353 
1354   memcpy(prbs_cmd_data.exp_rsp.p_data, &rsp_cmd_info[0], sizeof(rsp_cmd_info));
1355   prbs_cmd_data.exp_rsp.len = sizeof(rsp_cmd_info);
1356 
1357   // prbs_cmd_data.exp_rsp.len = 0x00;
1358   prbs_cmd_data.exp_ntf.len = 0x00;
1359   prbs_cmd_data.rsp_validator = st_validator_testEquals;
1360   prbs_cmd_data.ntf_validator = st_validator_null;
1361 
1362   uint8_t len = 0;
1363   uint8_t cnt = 0;
1364 
1365   //    [NCI] -> [0x2F 0x30 0x04 0x00 0x00 0x01 0xFF]
1366   status =
1367       phNxpNciHal_getPrbsCmd(prbs_type, hw_prbs_type, tech, bitrate,
1368                              prbs_cmd_data.cmd.p_data, prbs_cmd_data.cmd.len);
1369 
1370   if (status == NFCSTATUS_FAILED) {
1371     // Invalid Param.
1372     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStart - INVALID_PARAM\n");
1373 
1374     goto clean_and_return;
1375   }
1376 
1377   len = (sizeof(prbs_test_data) / sizeof(prbs_test_data[0]));
1378 
1379   for (cnt = 0; cnt < len; cnt++) {
1380     status = phNxpNciHal_performTest(&(prbs_test_data[cnt]));
1381     if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1382       break;
1383     }
1384   }
1385 
1386   /* Ignoring status, as there will be no response - Applicable till FW version
1387    * 8.1.1*/
1388   status = phNxpNciHal_performTest(&prbs_cmd_data);
1389 clean_and_return:
1390 
1391   if (status == NFCSTATUS_SUCCESS) {
1392     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStart - SUCCESS\n");
1393   } else {
1394     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStart - FAILED\n");
1395   }
1396 
1397   NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStart - end\n");
1398 
1399   return status;
1400 }
1401 
1402 /*******************************************************************************
1403  **
1404  ** Function         phNxpNciHal_PrbsTestStop
1405  **
1406  ** Description      Test function stop RF generation for RF technology started
1407  **                  by phNxpNciHal_PrbsTestStart.
1408  **
1409  ** Returns          NFCSTATUS_SUCCESS if operation successful,
1410  **                  otherwise NFCSTATUS_FAILED.
1411  **
1412  ******************************************************************************/
1413 
phNxpNciHal_PrbsTestStop()1414 NFCSTATUS phNxpNciHal_PrbsTestStop() {
1415   NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStop - Start\n");
1416 
1417   NFCSTATUS status = NFCSTATUS_SUCCESS;
1418 
1419   status = phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
1420 
1421   if (NFCSTATUS_SUCCESS == status) {
1422     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStop - SUCCESS\n");
1423   } else {
1424     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStop - FAILED\n");
1425   }
1426   NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStop - end\n");
1427 
1428   return status;
1429 }
1430 
1431 /*******************************************************************************
1432 **
1433 ** Function         phNxpNciHal_getPrbsCmd
1434 **
1435 ** Description      Test function frames the PRBS command.
1436 **
1437 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1438 **
1439 *******************************************************************************/
phNxpNciHal_getPrbsCmd(phNxpNfc_PrbsType_t prbs_type,phNxpNfc_PrbsHwType_t hw_prbs_type,uint8_t tech,uint8_t bitrate,uint8_t * prbs_cmd,uint8_t prbs_cmd_len)1440 NFCSTATUS phNxpNciHal_getPrbsCmd(phNxpNfc_PrbsType_t prbs_type,
1441                                  phNxpNfc_PrbsHwType_t hw_prbs_type,
1442                                  uint8_t tech, uint8_t bitrate,
1443                                  uint8_t* prbs_cmd, uint8_t prbs_cmd_len) {
1444   NFCSTATUS status = NFCSTATUS_SUCCESS;
1445   int position_tech_param = 0;
1446   int position_bit_param = 0;
1447 
1448   NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - tech 0x%x bitrate = 0x%x", tech,
1449                   bitrate);
1450   if (NULL == prbs_cmd || prbs_cmd_len != 0x09) {
1451     return status;
1452   }
1453 
1454   prbs_cmd[0] = 0x2F;
1455   prbs_cmd[1] = 0x30;
1456   prbs_cmd[2] = 0x06;
1457   prbs_cmd[3] = (uint8_t)prbs_type;
1458   // 0xFF Error value used for validation.
1459   prbs_cmd[4] = (uint8_t)hw_prbs_type;
1460   prbs_cmd[5] = 0xFF;  // TECH
1461   prbs_cmd[6] = 0xFF;  // BITRATE
1462   prbs_cmd[7] = 0x01;
1463   prbs_cmd[8] = 0xFF;
1464   position_tech_param = 5;
1465   position_bit_param = 6;
1466 
1467   switch (tech) {
1468     case NFC_RF_TECHNOLOGY_A:
1469       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_RF_TECHNOLOGY_A");
1470       prbs_cmd[position_tech_param] = 0x00;
1471       break;
1472     case NFC_RF_TECHNOLOGY_B:
1473       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_RF_TECHNOLOGY_B");
1474       prbs_cmd[position_tech_param] = 0x01;
1475       break;
1476     case NFC_RF_TECHNOLOGY_F:
1477       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_RF_TECHNOLOGY_F");
1478       prbs_cmd[position_tech_param] = 0x02;
1479       break;
1480     default:
1481       break;
1482   }
1483 
1484   switch (bitrate) {
1485     case NFC_BIT_RATE_106:
1486       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_BIT_RATE_106");
1487       if (prbs_cmd[position_tech_param] != 0x02) {
1488         prbs_cmd[position_bit_param] = 0x00;
1489       }
1490       break;
1491     case NFC_BIT_RATE_212:
1492       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_BIT_RATE_212");
1493       prbs_cmd[position_bit_param] = 0x01;
1494       break;
1495     case NFC_BIT_RATE_424:
1496       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_BIT_RATE_424");
1497       prbs_cmd[position_bit_param] = 0x02;
1498       break;
1499     case NFC_BIT_RATE_848:
1500       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_BIT_RATE_848");
1501       if (prbs_cmd[position_tech_param] != 0x02) {
1502         prbs_cmd[position_bit_param] = 0x03;
1503       }
1504       break;
1505     default:
1506       break;
1507   }
1508 
1509   if (prbs_cmd[position_tech_param] == 0xFF ||
1510       prbs_cmd[position_bit_param] == 0xFF) {
1511     // Invalid Param.
1512     status = NFCSTATUS_FAILED;
1513   }
1514 
1515   return status;
1516 }
1517 
1518 /*******************************************************************************
1519 **
1520 ** Function         phNxpNciHal_RfFieldTest
1521 **
1522 ** Description      Test function performs RF filed test.
1523 **
1524 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1525 **
1526 *******************************************************************************/
phNxpNciHal_RfFieldTest(uint8_t on)1527 NFCSTATUS phNxpNciHal_RfFieldTest(uint8_t on) {
1528   NFCSTATUS status = NFCSTATUS_SUCCESS;
1529   int len = 0;
1530   int cnt = 0;
1531 
1532   NXPLOG_NCIHAL_D("phNxpNciHal_RfFieldTest - start %x\n", on);
1533 
1534   if (on == 0x01) {
1535     len = (sizeof(rf_field_on_test_data) / sizeof(rf_field_on_test_data[0]));
1536 
1537     for (cnt = 0; cnt < len; cnt++) {
1538       status = phNxpNciHal_performTest(&(rf_field_on_test_data[cnt]));
1539       if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1540         break;
1541       }
1542     }
1543   } else if (on == 0x00) {
1544     len = (sizeof(rf_field_off_test_data) / sizeof(rf_field_off_test_data[0]));
1545 
1546     for (cnt = 0; cnt < len; cnt++) {
1547       status = phNxpNciHal_performTest(&(rf_field_off_test_data[cnt]));
1548       if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1549         break;
1550       }
1551     }
1552   } else {
1553     status = NFCSTATUS_FAILED;
1554   }
1555 
1556   if (status == NFCSTATUS_SUCCESS) {
1557     NXPLOG_NCIHAL_D("phNxpNciHal_RfFieldTest - SUCCESS\n");
1558   } else {
1559     NXPLOG_NCIHAL_D("phNxpNciHal_RfFieldTest - FAILED\n");
1560   }
1561 
1562   NXPLOG_NCIHAL_D("phNxpNciHal_RfFieldTest - end\n");
1563 
1564   return status;
1565 }
1566 
1567 /*******************************************************************************
1568  **
1569  ** Function         phNxpNciHal_AntennaTest
1570  **
1571  ** Description
1572  **
1573  ** Returns
1574  **
1575  ******************************************************************************/
phNxpNciHal_AntennaTest()1576 NFCSTATUS phNxpNciHal_AntennaTest() {
1577   NFCSTATUS status = NFCSTATUS_FAILED;
1578 
1579   return status;
1580 }
1581 
1582 /*******************************************************************************
1583 **
1584 ** Function         phNxpNciHal_DownloadPinTest
1585 **
1586 ** Description      Test function to validate the FW download pin connection.
1587 **
1588 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1589 **
1590 *******************************************************************************/
phNxpNciHal_DownloadPinTest(void)1591 NFCSTATUS phNxpNciHal_DownloadPinTest(void) {
1592   NFCSTATUS status = NFCSTATUS_FAILED;
1593   int len = 0;
1594   int cnt = 0;
1595 
1596   NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - start\n");
1597 
1598   len = (sizeof(download_pin_test_data1) / sizeof(download_pin_test_data1[0]));
1599 
1600   for (cnt = 0; cnt < len; cnt++) {
1601     status = phNxpNciHal_performTest(&(download_pin_test_data1[cnt]));
1602     if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1603       break;
1604     }
1605   }
1606 
1607   if (status != NFCSTATUS_SUCCESS) {
1608     NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - FAILED\n");
1609     return status;
1610   }
1611 
1612   status = NFCSTATUS_FAILED;
1613   status = phTmlNfc_IoCtl(phTmlNfc_e_EnableDownloadMode);
1614   if (NFCSTATUS_SUCCESS != status) {
1615     NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - FAILED\n");
1616     return status;
1617   }
1618 
1619   status = NFCSTATUS_FAILED;
1620   len = (sizeof(download_pin_test_data2) / sizeof(download_pin_test_data2[0]));
1621 
1622   for (cnt = 0; cnt < len; cnt++) {
1623     status = phNxpNciHal_performTest(&(download_pin_test_data2[cnt]));
1624     if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1625       break;
1626     }
1627   }
1628 
1629   if (status == NFCSTATUS_SUCCESS) {
1630     NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - SUCCESS\n");
1631   } else {
1632     NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - FAILED\n");
1633   }
1634 
1635   NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - end\n");
1636 
1637   return status;
1638 }
1639 /*******************************************************************************
1640 **
1641 ** Function         phNxpNciHal_AntennaSelfTest
1642 **
1643 ** Description      Test function to validate the Antenna's discrete
1644 **                  components connection.
1645 **
1646 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1647 **
1648 *******************************************************************************/
phNxpNciHal_AntennaSelfTest(phAntenna_St_Resp_t * phAntenna_St_Resp)1649 NFCSTATUS phNxpNciHal_AntennaSelfTest(phAntenna_St_Resp_t* phAntenna_St_Resp) {
1650   NFCSTATUS status = NFCSTATUS_FAILED;
1651   NFCSTATUS antenna_st_status = NFCSTATUS_FAILED;
1652   int len = 0;
1653   int cnt = 0;
1654 
1655   NXPLOG_NCIHAL_D("phNxpNciHal_AntennaSelfTest - start\n");
1656   memcpy(&phAntenna_resp, phAntenna_St_Resp, sizeof(phAntenna_St_Resp_t));
1657   len = (sizeof(antenna_self_test_data) / sizeof(antenna_self_test_data[0]));
1658 
1659   for (cnt = 0; cnt < len; cnt++) {
1660     status = phNxpNciHal_performTest(&(antenna_self_test_data[cnt]));
1661     if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1662       NXPLOG_NCIHAL_E(
1663           "phNxpNciHal_AntennaSelfTest: command execution - FAILED\n");
1664       break;
1665     }
1666   }
1667 
1668   if (status == NFCSTATUS_SUCCESS) {
1669     if ((gtxldo_status == NFCSTATUS_SUCCESS) &&
1670         (gagc_value_status == NFCSTATUS_SUCCESS) &&
1671         (gagc_nfcld_status == NFCSTATUS_SUCCESS) &&
1672         (gagc_differential_status == NFCSTATUS_SUCCESS)) {
1673       antenna_st_status = NFCSTATUS_SUCCESS;
1674       NXPLOG_NCIHAL_D("phNxpNciHal_AntennaSelfTest - SUCCESS\n");
1675     } else {
1676       NXPLOG_NCIHAL_E("phNxpNciHal_AntennaSelfTest - FAILED\n");
1677     }
1678   } else {
1679     NXPLOG_NCIHAL_D("phNxpNciHal_AntennaSelfTest - FAILED\n");
1680   }
1681 
1682   NXPLOG_NCIHAL_D("phNxpNciHal_AntennaSelfTest - end\n");
1683 
1684   return antenna_st_status;
1685 }
1686 
1687 #endif /*#ifdef NXP_HW_SELF_TEST*/
1688