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