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