1 /*
2  * Copyright 2012-2022 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   if (hal_write_timer_fired == 1) {
863     NXPLOG_NCIHAL_D("hal_read_cb - response timeout occurred");
864 
865     hal_write_timer_fired = 0;
866     p_cb_data->status = NFCSTATUS_RESPONSE_TIMEOUT;
867     phTmlNfc_ReadAbort();
868   } else {
869     NFCSTATUS status = phOsalNfc_Timer_Stop(timeoutTimerId);
870 
871     if (NFCSTATUS_SUCCESS == status) {
872       NXPLOG_NCIHAL_D("Response timer stopped");
873     } else {
874       NXPLOG_NCIHAL_E("Response timer stop ERROR!!!");
875       p_cb_data->status = NFCSTATUS_FAILED;
876     }
877     if (pInfo == NULL) {
878       NXPLOG_NCIHAL_E("Empty TransactInfo");
879       p_cb_data->status = NFCSTATUS_FAILED;
880     } else {
881       if (pInfo->wStatus == NFCSTATUS_SUCCESS) {
882         NXPLOG_NCIHAL_D("hal_read_cb successful status = 0x%x", pInfo->wStatus);
883         p_cb_data->status = NFCSTATUS_SUCCESS;
884       } else {
885         NXPLOG_NCIHAL_E("hal_read_cb error status = 0x%x", pInfo->wStatus);
886         p_cb_data->status = NFCSTATUS_FAILED;
887       }
888 
889       p_cb_data->status = pInfo->wStatus;
890       nci_test_data_t* test_data = (nci_test_data_t*)p_cb_data->pContext;
891 
892       if (test_data->exp_rsp.len == 0) {
893         /* Compare the actual notification with expected notification.*/
894         if (test_data->ntf_validator(&(test_data->exp_ntf), pInfo) == 1) {
895           p_cb_data->status = NFCSTATUS_SUCCESS;
896         } else {
897           p_cb_data->status = NFCSTATUS_FAILED;
898         }
899       }
900 
901       /* Compare the actual response with expected response.*/
902       else if (test_data->rsp_validator(&(test_data->exp_rsp), pInfo) == 1) {
903         p_cb_data->status = NFCSTATUS_SUCCESS;
904       } else {
905         p_cb_data->status = NFCSTATUS_FAILED;
906       }
907 
908       test_data->exp_rsp.len = 0;
909     }
910   }
911 
912   SEM_POST(p_cb_data);
913 
914   return;
915 }
916 
917 /*******************************************************************************
918 **
919 ** Function         phNxpNciHal_test_rx_thread
920 **
921 ** Description      Thread to fetch and process messages from message queue.
922 **
923 ** Returns          NULL
924 **
925 *******************************************************************************/
phNxpNciHal_test_rx_thread(void * arg)926 static void* phNxpNciHal_test_rx_thread(void* arg) {
927   phLibNfc_Message_t msg;
928   UNUSED_PROP(arg);
929   NXPLOG_NCIHAL_D("Self test thread started");
930 
931   thread_running = 1;
932 
933   while (thread_running == 1) {
934     /* Fetch next message from the NFC stack message queue */
935     if (phDal4Nfc_msgrcv(gDrvCfg.nClientId, &msg, 0, 0) == -1) {
936       NXPLOG_NCIHAL_E("Received bad message");
937       continue;
938     }
939 
940     if (thread_running == 0) {
941       break;
942     }
943 
944     switch (msg.eMsgType) {
945       case PH_LIBNFC_DEFERREDCALL_MSG: {
946         phLibNfc_DeferredCall_t* deferCall =
947             (phLibNfc_DeferredCall_t*)(msg.pMsgData);
948 
949         REENTRANCE_LOCK();
950         deferCall->pCallback(deferCall->pParameter);
951         REENTRANCE_UNLOCK();
952 
953         break;
954       }
955     }
956   }
957 
958   NXPLOG_NCIHAL_D("Self test thread stopped");
959 
960   return NULL;
961 }
962 
963 /*******************************************************************************
964 **
965 ** Function         phNxpNciHal_readLocked
966 **
967 ** Description      Reads response and notification from NFCC and waits for
968 **                  read completion, for a definitive timeout value.
969 **
970 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED,
971 **                  NFCSTATUS_RESPONSE_TIMEOUT in case of timeout.
972 **
973 *******************************************************************************/
phNxpNciHal_readLocked(nci_test_data_t * pData)974 static NFCSTATUS phNxpNciHal_readLocked(nci_test_data_t* pData) {
975   NFCSTATUS status = NFCSTATUS_SUCCESS;
976   phNxpNciHal_Sem_t cb_data;
977   uint16_t read_len = 16;
978   /* RX Buffer */
979   uint32_t rx_data[NCI_MAX_DATA_LEN];
980 
981   /* Create the local semaphore */
982   if (phNxpNciHal_init_cb_data(&cb_data, pData) != NFCSTATUS_SUCCESS) {
983     NXPLOG_NCIHAL_D("phTmlNfc_Read Create cb data failed");
984     status = NFCSTATUS_FAILED;
985     goto clean_and_return;
986   }
987 
988   /* call read pending */
989   status =
990       phTmlNfc_Read((uint8_t*)rx_data, (uint16_t)read_len,
991                     (pphTmlNfc_TransactCompletionCb_t)&hal_read_cb, &cb_data);
992 
993   if (status != NFCSTATUS_PENDING) {
994     NXPLOG_NCIHAL_E("TML Read status error status = %x", status);
995     status = NFCSTATUS_FAILED;
996     goto clean_and_return;
997   }
998 
999   status = phOsalNfc_Timer_Start(timeoutTimerId, HAL_WRITE_RSP_TIMEOUT,
1000                                  &hal_write_rsp_timeout_cb, &cb_data);
1001 
1002   if (NFCSTATUS_SUCCESS == status) {
1003     NXPLOG_NCIHAL_D("Response timer started");
1004   } else {
1005     NXPLOG_NCIHAL_E("Response timer not started");
1006     status = NFCSTATUS_FAILED;
1007     goto clean_and_return;
1008   }
1009 
1010   /* Wait for callback response */
1011   if (SEM_WAIT(cb_data)) {
1012     NXPLOG_NCIHAL_E("phTmlNfc_Read semaphore error");
1013     status = NFCSTATUS_FAILED;
1014     goto clean_and_return;
1015   }
1016 
1017   if (cb_data.status == NFCSTATUS_RESPONSE_TIMEOUT) {
1018     NXPLOG_NCIHAL_E("Response timeout!!!");
1019     status = NFCSTATUS_RESPONSE_TIMEOUT;
1020     goto clean_and_return;
1021   }
1022 
1023   if (cb_data.status != NFCSTATUS_SUCCESS) {
1024     NXPLOG_NCIHAL_E("phTmlNfc_Read failed  ");
1025     status = NFCSTATUS_FAILED;
1026     goto clean_and_return;
1027   }
1028 
1029 clean_and_return:
1030   phNxpNciHal_cleanup_cb_data(&cb_data);
1031 
1032   return status;
1033 }
1034 
1035 /*******************************************************************************
1036 **
1037 ** Function         phNxpNciHal_writeLocked
1038 **
1039 ** Description      Send command to NFCC and waits for cmd write completion, for
1040 **                  a definitive timeout value.
1041 **
1042 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED,
1043 **                  NFCSTATUS_RESPONSE_TIMEOUT in case of timeout.
1044 **
1045 *******************************************************************************/
phNxpNciHal_writeLocked(nci_test_data_t * pData)1046 static NFCSTATUS phNxpNciHal_writeLocked(nci_test_data_t* pData) {
1047   NFCSTATUS status = NFCSTATUS_SUCCESS;
1048 
1049   phNxpNciHal_Sem_t cb_data;
1050   int retryCnt = 0;
1051 
1052   /* Create the local semaphore */
1053   if (phNxpNciHal_init_cb_data(&cb_data, NULL) != NFCSTATUS_SUCCESS) {
1054     NXPLOG_NCIHAL_D("phTmlNfc_Write Create cb data failed");
1055     goto clean_and_return;
1056   }
1057 
1058 retry:
1059   status =
1060       phTmlNfc_Write(pData->cmd.p_data, pData->cmd.len,
1061                      (pphTmlNfc_TransactCompletionCb_t)&hal_write_cb, &cb_data);
1062 
1063   if (status != NFCSTATUS_PENDING) {
1064     NXPLOG_NCIHAL_E("phTmlNfc_Write status error");
1065     goto clean_and_return;
1066   }
1067 
1068   /* Wait for callback response */
1069   if (SEM_WAIT(cb_data)) {
1070     NXPLOG_NCIHAL_E("write_unlocked semaphore error");
1071     status = NFCSTATUS_FAILED;
1072     goto clean_and_return;
1073   }
1074 
1075   if (cb_data.status != NFCSTATUS_SUCCESS && retryCnt < HAL_WRITE_MAX_RETRY) {
1076     retryCnt++;
1077     NXPLOG_NCIHAL_D(
1078         "write_unlocked failed - PN54X Maybe in Standby Mode - Retry %d",
1079         retryCnt);
1080     goto retry;
1081   }
1082 
1083   status = cb_data.status;
1084 
1085 clean_and_return:
1086   phNxpNciHal_cleanup_cb_data(&cb_data);
1087 
1088   return status;
1089 }
1090 
1091 /*******************************************************************************
1092 **
1093 ** Function         phNxpNciHal_performTest
1094 **
1095 ** Description      Performs a single cycle of command,response and
1096 **                  notification.
1097 **
1098 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED,
1099 **
1100 *******************************************************************************/
phNxpNciHal_performTest(nci_test_data_t * pData)1101 NFCSTATUS phNxpNciHal_performTest(nci_test_data_t* pData) {
1102   NFCSTATUS status = NFCSTATUS_SUCCESS;
1103 
1104   if (NULL == pData) {
1105     return NFCSTATUS_FAILED;
1106   }
1107 
1108   CONCURRENCY_LOCK();
1109 
1110   status = phNxpNciHal_writeLocked(pData);
1111 
1112   if (status == NFCSTATUS_RESPONSE_TIMEOUT) {
1113     goto clean_and_return;
1114   }
1115   if (status != NFCSTATUS_SUCCESS) {
1116     goto clean_and_return;
1117   }
1118 
1119   status = phNxpNciHal_readLocked(pData);
1120 
1121   if (status != NFCSTATUS_SUCCESS) {
1122     goto clean_and_return;
1123   }
1124 
1125   if (0 != pData->exp_ntf.len) {
1126     status = phNxpNciHal_readLocked(pData);
1127 
1128     if (status != NFCSTATUS_SUCCESS) {
1129       goto clean_and_return;
1130     }
1131   }
1132 
1133 clean_and_return:
1134   CONCURRENCY_UNLOCK();
1135   return status;
1136 }
1137 
1138 /*******************************************************************************
1139  **
1140  ** Function         phNxpNciHal_TestMode_open
1141  **
1142  ** Description      It opens the physical connection with NFCC (PN54X) and
1143  **                  creates required client thread for operation.
1144  **
1145  ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1146  **
1147  ******************************************************************************/
phNxpNciHal_TestMode_open(void)1148 NFCSTATUS phNxpNciHal_TestMode_open(void) {
1149   /* Thread */
1150   pthread_t test_rx_thread;
1151 
1152   phOsalNfc_Config_t tOsalConfig;
1153   phTmlNfc_Config_t tTmlConfig;
1154   char* nfc_dev_node = NULL;
1155   const uint16_t max_len = 260;
1156   NFCSTATUS status = NFCSTATUS_SUCCESS;
1157   int8_t ret_val = 0x00;
1158   /* initialize trace level */
1159   phNxpLog_InitializeLogLevel();
1160 
1161   if (phNxpNciHal_init_monitor() == NULL) {
1162     NXPLOG_NCIHAL_E("Init monitor failed");
1163     return NFCSTATUS_FAILED;
1164   }
1165 
1166   CONCURRENCY_LOCK();
1167 
1168   memset(&tOsalConfig, 0x00, sizeof(tOsalConfig));
1169   memset(&tTmlConfig, 0x00, sizeof(tTmlConfig));
1170 
1171   /* Read the nfc device node name */
1172   nfc_dev_node = (char*)malloc(max_len * sizeof(char));
1173   if (nfc_dev_node == NULL) {
1174     NXPLOG_NCIHAL_D("malloc of nfc_dev_node failed ");
1175     goto clean_and_return;
1176   } else if (!GetNxpStrValue(NAME_NXP_NFC_DEV_NODE, nfc_dev_node, max_len)) {
1177     NXPLOG_NCIHAL_D(
1178         "Invalid nfc device node name keeping the default device node "
1179         "/dev/pn54x");
1180     strlcpy(nfc_dev_node, "/dev/pn54x", (max_len * sizeof(char)));
1181   }
1182 
1183   gDrvCfg.nClientId = phDal4Nfc_msgget(0, 0600);
1184   gDrvCfg.nLinkType = ENUM_LINK_TYPE_I2C; /* For PN54X */
1185   tTmlConfig.pDevName = (int8_t*)nfc_dev_node;
1186   tOsalConfig.dwCallbackThreadId = (uintptr_t)gDrvCfg.nClientId;
1187   tOsalConfig.pLogFile = NULL;
1188   tTmlConfig.dwGetMsgThreadId = (uintptr_t)gDrvCfg.nClientId;
1189   nxpncihal_ctrl.gDrvCfg.nClientId = (uintptr_t)gDrvCfg.nClientId;
1190 
1191   /* Initialize TML layer */
1192   status = phTmlNfc_Init(&tTmlConfig);
1193   if (status != NFCSTATUS_SUCCESS) {
1194     NXPLOG_NCIHAL_E("phTmlNfc_Init Failed");
1195     goto clean_and_return;
1196   } else {
1197     if (nfc_dev_node != NULL) {
1198       free(nfc_dev_node);
1199       nfc_dev_node = NULL;
1200     }
1201   }
1202 
1203   pthread_attr_t attr;
1204   pthread_attr_init(&attr);
1205   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1206   ret_val =
1207       pthread_create(&test_rx_thread, &attr, phNxpNciHal_test_rx_thread, NULL);
1208   pthread_attr_destroy(&attr);
1209   if (ret_val != 0) {
1210     NXPLOG_NCIHAL_E("pthread_create failed");
1211     phTmlNfc_Shutdown_CleanUp();
1212     goto clean_and_return;
1213   }
1214 
1215   timeoutTimerId = phOsalNfc_Timer_Create();
1216 
1217   if (timeoutTimerId == 0xFFFF) {
1218     NXPLOG_NCIHAL_E("phOsalNfc_Timer_Create failed");
1219   } else {
1220     NXPLOG_NCIHAL_D("phOsalNfc_Timer_Create SUCCESS");
1221   }
1222   CONCURRENCY_UNLOCK();
1223 
1224   return NFCSTATUS_SUCCESS;
1225 
1226 clean_and_return:
1227   CONCURRENCY_UNLOCK();
1228   if (nfc_dev_node != NULL) {
1229     free(nfc_dev_node);
1230     nfc_dev_node = NULL;
1231   }
1232   phNxpNciHal_cleanup_monitor();
1233   return NFCSTATUS_FAILED;
1234 }
1235 
1236 /*******************************************************************************
1237  **
1238  ** Function         phNxpNciHal_TestMode_close
1239  **
1240  ** Description      This function close the NFCC interface and free all
1241  **                  resources.
1242  **
1243  ** Returns          None.
1244  **
1245  ******************************************************************************/
1246 
phNxpNciHal_TestMode_close()1247 void phNxpNciHal_TestMode_close() {
1248   NFCSTATUS status = NFCSTATUS_SUCCESS;
1249 
1250   CONCURRENCY_LOCK();
1251 
1252   if (NULL != gpphTmlNfc_Context->pDevHandle) {
1253     /* Abort any pending read and write */
1254     status = phTmlNfc_ReadAbort();
1255     status = phTmlNfc_WriteAbort();
1256 
1257     phOsalNfc_Timer_Cleanup();
1258 
1259     status = phTmlNfc_Shutdown_CleanUp();
1260 
1261     NXPLOG_NCIHAL_D("phNxpNciHal_close return status = %d", status);
1262 
1263     thread_running = 0;
1264 
1265     phDal4Nfc_msgrelease(gDrvCfg.nClientId);
1266 
1267     status = phOsalNfc_Timer_Delete(timeoutTimerId);
1268   }
1269 
1270   CONCURRENCY_UNLOCK();
1271 
1272   phNxpNciHal_cleanup_monitor();
1273 
1274   /* Return success always */
1275   return;
1276 }
1277 
1278 /*******************************************************************************
1279  **
1280  ** Function         phNxpNciHal_SwpTest
1281  **
1282  ** Description      Test function to validate the SWP line. SWP line number is
1283  **                  is sent as parameter to the API.
1284  **
1285  ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1286  **
1287  ******************************************************************************/
1288 
phNxpNciHal_SwpTest(uint8_t swp_line)1289 NFCSTATUS phNxpNciHal_SwpTest(uint8_t swp_line) {
1290   NFCSTATUS status = NFCSTATUS_SUCCESS;
1291   int len = 0;
1292   int cnt = 0;
1293 
1294   NXPLOG_NCIHAL_D("phNxpNciHal_SwpTest - start\n");
1295 
1296   if (swp_line == 0x01) {
1297     len = (sizeof(swp1_test_data) / sizeof(swp1_test_data[0]));
1298 
1299     for (cnt = 0; cnt < len; cnt++) {
1300       status = phNxpNciHal_performTest(&(swp1_test_data[cnt]));
1301       if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1302         break;
1303       }
1304     }
1305   } else if (swp_line == 0x02) {
1306     len = (sizeof(swp2_test_data) / sizeof(swp2_test_data[0]));
1307 
1308     for (cnt = 0; cnt < len; cnt++) {
1309       status = phNxpNciHal_performTest(&(swp2_test_data[cnt]));
1310       if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1311         break;
1312       }
1313     }
1314   } else {
1315     status = NFCSTATUS_FAILED;
1316   }
1317 
1318   if (status == NFCSTATUS_SUCCESS) {
1319     NXPLOG_NCIHAL_D("phNxpNciHal_SwpTest - SUCCESS\n");
1320   } else {
1321     NXPLOG_NCIHAL_D("phNxpNciHal_SwpTest - FAILED\n");
1322   }
1323 
1324   NXPLOG_NCIHAL_D("phNxpNciHal_SwpTest - end\n");
1325 
1326   return status;
1327 }
1328 
1329 /*******************************************************************************
1330  **
1331  ** Function         phNxpNciHal_PrbsTestStart
1332  **
1333  ** Description      Test function start RF generation for RF technology and bit
1334  **                  rate. RF technology and bit rate are sent as parameter to
1335  **                  the API.
1336  **
1337  ** Returns          NFCSTATUS_SUCCESS if RF generation successful,
1338  **                  otherwise NFCSTATUS_FAILED.
1339  **
1340  ******************************************************************************/
1341 
phNxpNciHal_PrbsTestStart(phNxpNfc_PrbsType_t prbs_type,phNxpNfc_PrbsHwType_t hw_prbs_type,phNxpNfc_Tech_t tech,phNxpNfc_Bitrate_t bitrate)1342 NFCSTATUS phNxpNciHal_PrbsTestStart(phNxpNfc_PrbsType_t prbs_type,
1343                                     phNxpNfc_PrbsHwType_t hw_prbs_type,
1344                                     phNxpNfc_Tech_t tech,
1345                                     phNxpNfc_Bitrate_t bitrate) {
1346   NFCSTATUS status = NFCSTATUS_FAILED;
1347 
1348   nci_test_data_t prbs_cmd_data;
1349 
1350   uint8_t rsp_cmd_info[] = {0x4F, 0x30, 0x01, 0x00};
1351   prbs_cmd_data.cmd.len = 0x09;
1352 
1353   memcpy(prbs_cmd_data.exp_rsp.p_data, &rsp_cmd_info[0], sizeof(rsp_cmd_info));
1354   prbs_cmd_data.exp_rsp.len = sizeof(rsp_cmd_info);
1355 
1356   // prbs_cmd_data.exp_rsp.len = 0x00;
1357   prbs_cmd_data.exp_ntf.len = 0x00;
1358   prbs_cmd_data.rsp_validator = st_validator_testEquals;
1359   prbs_cmd_data.ntf_validator = st_validator_null;
1360 
1361   uint8_t len = 0;
1362   uint8_t cnt = 0;
1363 
1364   //    [NCI] -> [0x2F 0x30 0x04 0x00 0x00 0x01 0xFF]
1365   status =
1366       phNxpNciHal_getPrbsCmd(prbs_type, hw_prbs_type, tech, bitrate,
1367                              prbs_cmd_data.cmd.p_data, prbs_cmd_data.cmd.len);
1368 
1369   if (status == NFCSTATUS_FAILED) {
1370     // Invalid Param.
1371     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStart - INVALID_PARAM\n");
1372 
1373     goto clean_and_return;
1374   }
1375 
1376   len = (sizeof(prbs_test_data) / sizeof(prbs_test_data[0]));
1377 
1378   for (cnt = 0; cnt < len; cnt++) {
1379     status = phNxpNciHal_performTest(&(prbs_test_data[cnt]));
1380     if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1381       break;
1382     }
1383   }
1384 
1385   /* Ignoring status, as there will be no response - Applicable till FW version
1386    * 8.1.1*/
1387   status = phNxpNciHal_performTest(&prbs_cmd_data);
1388 clean_and_return:
1389 
1390   if (status == NFCSTATUS_SUCCESS) {
1391     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStart - SUCCESS\n");
1392   } else {
1393     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStart - FAILED\n");
1394   }
1395 
1396   NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStart - end\n");
1397 
1398   return status;
1399 }
1400 
1401 /*******************************************************************************
1402  **
1403  ** Function         phNxpNciHal_PrbsTestStop
1404  **
1405  ** Description      Test function stop RF generation for RF technology started
1406  **                  by phNxpNciHal_PrbsTestStart.
1407  **
1408  ** Returns          NFCSTATUS_SUCCESS if operation successful,
1409  **                  otherwise NFCSTATUS_FAILED.
1410  **
1411  ******************************************************************************/
1412 
phNxpNciHal_PrbsTestStop()1413 NFCSTATUS phNxpNciHal_PrbsTestStop() {
1414   NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStop - Start\n");
1415 
1416   NFCSTATUS status = NFCSTATUS_SUCCESS;
1417 
1418   status = phTmlNfc_IoCtl(phTmlNfc_e_ResetDevice);
1419 
1420   if (NFCSTATUS_SUCCESS == status) {
1421     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStop - SUCCESS\n");
1422   } else {
1423     NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStop - FAILED\n");
1424   }
1425   NXPLOG_NCIHAL_D("phNxpNciHal_PrbsTestStop - end\n");
1426 
1427   return status;
1428 }
1429 
1430 /*******************************************************************************
1431 **
1432 ** Function         phNxpNciHal_getPrbsCmd
1433 **
1434 ** Description      Test function frames the PRBS command.
1435 **
1436 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1437 **
1438 *******************************************************************************/
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)1439 NFCSTATUS phNxpNciHal_getPrbsCmd(phNxpNfc_PrbsType_t prbs_type,
1440                                  phNxpNfc_PrbsHwType_t hw_prbs_type,
1441                                  uint8_t tech, uint8_t bitrate,
1442                                  uint8_t* prbs_cmd, uint8_t prbs_cmd_len) {
1443   NFCSTATUS status = NFCSTATUS_SUCCESS;
1444   int position_tech_param = 0;
1445   int position_bit_param = 0;
1446 
1447   NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - tech 0x%x bitrate = 0x%x", tech,
1448                   bitrate);
1449   if (NULL == prbs_cmd || prbs_cmd_len != 0x09) {
1450     return status;
1451   }
1452 
1453   prbs_cmd[0] = 0x2F;
1454   prbs_cmd[1] = 0x30;
1455   prbs_cmd[2] = 0x06;
1456   prbs_cmd[3] = (uint8_t)prbs_type;
1457   // 0xFF Error value used for validation.
1458   prbs_cmd[4] = (uint8_t)hw_prbs_type;
1459   prbs_cmd[5] = 0xFF;  // TECH
1460   prbs_cmd[6] = 0xFF;  // BITRATE
1461   prbs_cmd[7] = 0x01;
1462   prbs_cmd[8] = 0xFF;
1463   position_tech_param = 5;
1464   position_bit_param = 6;
1465 
1466   switch (tech) {
1467     case NFC_RF_TECHNOLOGY_A:
1468       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_RF_TECHNOLOGY_A");
1469       prbs_cmd[position_tech_param] = 0x00;
1470       break;
1471     case NFC_RF_TECHNOLOGY_B:
1472       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_RF_TECHNOLOGY_B");
1473       prbs_cmd[position_tech_param] = 0x01;
1474       break;
1475     case NFC_RF_TECHNOLOGY_F:
1476       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_RF_TECHNOLOGY_F");
1477       prbs_cmd[position_tech_param] = 0x02;
1478       break;
1479     default:
1480       break;
1481   }
1482 
1483   switch (bitrate) {
1484     case NFC_BIT_RATE_106:
1485       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_BIT_RATE_106");
1486       if (prbs_cmd[position_tech_param] != 0x02) {
1487         prbs_cmd[position_bit_param] = 0x00;
1488       }
1489       break;
1490     case NFC_BIT_RATE_212:
1491       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_BIT_RATE_212");
1492       prbs_cmd[position_bit_param] = 0x01;
1493       break;
1494     case NFC_BIT_RATE_424:
1495       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_BIT_RATE_424");
1496       prbs_cmd[position_bit_param] = 0x02;
1497       break;
1498     case NFC_BIT_RATE_848:
1499       NXPLOG_NCIHAL_D("phNxpNciHal_getPrbsCmd - NFC_BIT_RATE_848");
1500       if (prbs_cmd[position_tech_param] != 0x02) {
1501         prbs_cmd[position_bit_param] = 0x03;
1502       }
1503       break;
1504     default:
1505       break;
1506   }
1507 
1508   if (prbs_cmd[position_tech_param] == 0xFF ||
1509       prbs_cmd[position_bit_param] == 0xFF) {
1510     // Invalid Param.
1511     status = NFCSTATUS_FAILED;
1512   }
1513 
1514   return status;
1515 }
1516 
1517 /*******************************************************************************
1518 **
1519 ** Function         phNxpNciHal_RfFieldTest
1520 **
1521 ** Description      Test function performs RF filed test.
1522 **
1523 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1524 **
1525 *******************************************************************************/
phNxpNciHal_RfFieldTest(uint8_t on)1526 NFCSTATUS phNxpNciHal_RfFieldTest(uint8_t on) {
1527   NFCSTATUS status = NFCSTATUS_SUCCESS;
1528   int len = 0;
1529   int cnt = 0;
1530 
1531   NXPLOG_NCIHAL_D("phNxpNciHal_RfFieldTest - start %x\n", on);
1532 
1533   if (on == 0x01) {
1534     len = (sizeof(rf_field_on_test_data) / sizeof(rf_field_on_test_data[0]));
1535 
1536     for (cnt = 0; cnt < len; cnt++) {
1537       status = phNxpNciHal_performTest(&(rf_field_on_test_data[cnt]));
1538       if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1539         break;
1540       }
1541     }
1542   } else if (on == 0x00) {
1543     len = (sizeof(rf_field_off_test_data) / sizeof(rf_field_off_test_data[0]));
1544 
1545     for (cnt = 0; cnt < len; cnt++) {
1546       status = phNxpNciHal_performTest(&(rf_field_off_test_data[cnt]));
1547       if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1548         break;
1549       }
1550     }
1551   } else {
1552     status = NFCSTATUS_FAILED;
1553   }
1554 
1555   if (status == NFCSTATUS_SUCCESS) {
1556     NXPLOG_NCIHAL_D("phNxpNciHal_RfFieldTest - SUCCESS\n");
1557   } else {
1558     NXPLOG_NCIHAL_D("phNxpNciHal_RfFieldTest - FAILED\n");
1559   }
1560 
1561   NXPLOG_NCIHAL_D("phNxpNciHal_RfFieldTest - end\n");
1562 
1563   return status;
1564 }
1565 
1566 /*******************************************************************************
1567  **
1568  ** Function         phNxpNciHal_AntennaTest
1569  **
1570  ** Description
1571  **
1572  ** Returns
1573  **
1574  ******************************************************************************/
phNxpNciHal_AntennaTest()1575 NFCSTATUS phNxpNciHal_AntennaTest() {
1576   NFCSTATUS status = NFCSTATUS_FAILED;
1577 
1578   return status;
1579 }
1580 
1581 /*******************************************************************************
1582 **
1583 ** Function         phNxpNciHal_DownloadPinTest
1584 **
1585 ** Description      Test function to validate the FW download pin connection.
1586 **
1587 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1588 **
1589 *******************************************************************************/
phNxpNciHal_DownloadPinTest(void)1590 NFCSTATUS phNxpNciHal_DownloadPinTest(void) {
1591   NFCSTATUS status = NFCSTATUS_FAILED;
1592   int len = 0;
1593   int cnt = 0;
1594 
1595   NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - start\n");
1596 
1597   len = (sizeof(download_pin_test_data1) / sizeof(download_pin_test_data1[0]));
1598 
1599   for (cnt = 0; cnt < len; cnt++) {
1600     status = phNxpNciHal_performTest(&(download_pin_test_data1[cnt]));
1601     if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1602       break;
1603     }
1604   }
1605 
1606   if (status != NFCSTATUS_SUCCESS) {
1607     NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - FAILED\n");
1608     return status;
1609   }
1610 
1611   status = NFCSTATUS_FAILED;
1612   status = phTmlNfc_IoCtl(phTmlNfc_e_EnableDownloadMode);
1613   if (NFCSTATUS_SUCCESS != status) {
1614     NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - FAILED\n");
1615     return status;
1616   }
1617 
1618   status = NFCSTATUS_FAILED;
1619   len = (sizeof(download_pin_test_data2) / sizeof(download_pin_test_data2[0]));
1620 
1621   for (cnt = 0; cnt < len; cnt++) {
1622     status = phNxpNciHal_performTest(&(download_pin_test_data2[cnt]));
1623     if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1624       break;
1625     }
1626   }
1627 
1628   if (status == NFCSTATUS_SUCCESS) {
1629     NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - SUCCESS\n");
1630   } else {
1631     NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - FAILED\n");
1632   }
1633 
1634   NXPLOG_NCIHAL_D("phNxpNciHal_DownloadPinTest - end\n");
1635 
1636   return status;
1637 }
1638 /*******************************************************************************
1639 **
1640 ** Function         phNxpNciHal_AntennaSelfTest
1641 **
1642 ** Description      Test function to validate the Antenna's discrete
1643 **                  components connection.
1644 **
1645 ** Returns          NFCSTATUS_SUCCESS if successful,otherwise NFCSTATUS_FAILED.
1646 **
1647 *******************************************************************************/
phNxpNciHal_AntennaSelfTest(phAntenna_St_Resp_t * phAntenna_St_Resp)1648 NFCSTATUS phNxpNciHal_AntennaSelfTest(phAntenna_St_Resp_t* phAntenna_St_Resp) {
1649   NFCSTATUS status = NFCSTATUS_FAILED;
1650   NFCSTATUS antenna_st_status = NFCSTATUS_FAILED;
1651   int len = 0;
1652   int cnt = 0;
1653 
1654   NXPLOG_NCIHAL_D("phNxpNciHal_AntennaSelfTest - start\n");
1655   memcpy(&phAntenna_resp, phAntenna_St_Resp, sizeof(phAntenna_St_Resp_t));
1656   len = (sizeof(antenna_self_test_data) / sizeof(antenna_self_test_data[0]));
1657 
1658   for (cnt = 0; cnt < len; cnt++) {
1659     status = phNxpNciHal_performTest(&(antenna_self_test_data[cnt]));
1660     if (status == NFCSTATUS_RESPONSE_TIMEOUT || status == NFCSTATUS_FAILED) {
1661       NXPLOG_NCIHAL_E(
1662           "phNxpNciHal_AntennaSelfTest: command execution - FAILED\n");
1663       break;
1664     }
1665   }
1666 
1667   if (status == NFCSTATUS_SUCCESS) {
1668     if ((gtxldo_status == NFCSTATUS_SUCCESS) &&
1669         (gagc_value_status == NFCSTATUS_SUCCESS) &&
1670         (gagc_nfcld_status == NFCSTATUS_SUCCESS) &&
1671         (gagc_differential_status == NFCSTATUS_SUCCESS)) {
1672       antenna_st_status = NFCSTATUS_SUCCESS;
1673       NXPLOG_NCIHAL_D("phNxpNciHal_AntennaSelfTest - SUCCESS\n");
1674     } else {
1675       NXPLOG_NCIHAL_E("phNxpNciHal_AntennaSelfTest - FAILED\n");
1676     }
1677   } else {
1678     NXPLOG_NCIHAL_D("phNxpNciHal_AntennaSelfTest - FAILED\n");
1679   }
1680 
1681   NXPLOG_NCIHAL_D("phNxpNciHal_AntennaSelfTest - end\n");
1682 
1683   return antenna_st_status;
1684 }
1685 
1686 #endif /*#ifdef NXP_HW_SELF_TEST*/
1687