summaryrefslogtreecommitdiffstats
path: root/src/phHciNfc_Generic.c
blob: 875e2957a5a3ebc2ef4b533d933b607c2030773d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
/*
 * Copyright (C) 2010 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/*!
* =========================================================================== *
*                                                                             *
*                                                                             *
* \file  phHciNfc_Generic.c                                                   *
* \brief Generic HCI Source for the HCI Management.                           *
*                                                                             *
*                                                                             *
* Project: NFC-FRI-1.1                                                        *
*                                                                             *
* $Date: Tue Jun  8 09:31:49 2010 $                                           *
* $Author: ing04880 $                                                         *
* $Revision: 1.108 $                                                           *
* $Aliases: NFC_FRI1.1_WK1023_R35_1 $  
*                                                                             *
* =========================================================================== *
*/

/*
################################################################################
***************************** Header File Inclusion ****************************
################################################################################
*/

#include <phNfcCompId.h>
#include <phHciNfc_Sequence.h>
#include <phHciNfc_Pipe.h>
#include <phHciNfc_AdminMgmt.h>
#include <phHciNfc_IDMgmt.h>
#include <phHciNfc_LinkMgmt.h>
#include <phHciNfc_PollingLoop.h>
#include <phHciNfc_RFReader.h>
#include <phHciNfc_RFReaderA.h>
#include <phOsalNfc.h>

/*
################################################################################
****************************** Macro Definitions *******************************
################################################################################
*/

/* HCI timeout value */
uint32_t nxp_nfc_hci_response_timeout = NXP_NFC_HCI_TIMEOUT;

/*
################################################################################
************************ Static Variable Definitions ***************************
################################################################################
*/


#if  (NXP_NFC_HCI_TIMER == 1) 

#define NXP_HCI_RESPONSE_TIMEOUT  (NXP_NFC_HCI_TIMEOUT)

#include <phOsalNfc_Timer.h>
/** \internal HCI Response Timer to detect the 
 * Stalled HCI Response */
static uint32_t                    hci_resp_timer_id = NXP_INVALID_TIMER_ID;
static phHciNfc_sContext_t        *gpsHciContext= NULL;

#endif /* (NXP_NFC_HCI_TIMER == 1) */


/*
################################################################################
************************* Function Prototype Declaration ***********************
################################################################################
*/

#if  (NXP_NFC_HCI_TIMER == 1)

static
void
phHciNfc_Response_Timeout (
                uint32_t resp_timer_id, void *pContext
                );

#endif /* (NXP_NFC_HCI_TIMER == 1) */

/**
 * \ingroup grp_hci_nfc
 *
 *  The phHciNfc_Send function sends the HCI Commands to the
 *  corresponding peripheral device, described by the HCI Context Structure.
 *
 *  \param[in]  psContext               psContext is the context of
 *                                      the HCI Layer.
 *  \param[in]  pHwRef                  pHwRef is the Information of
 *                                      the Device Interface Link .
 *  \param[in]  pdata                   Pointer to the buffer containing
 *                                      the command to be sent.
 *  \param[in] length                   Variable that receives
 *                                      the number of bytes actually sent.
 *
 *  \retval NFCSTATUS_PENDING           Command successfully sent.
 *  \retval NFCSTATUS_INVALID_PARAMETER One or more of the supplied parameters
 *                                      could not be interpreted properly.
 *  \retval Other errors                Errors related to the lower layers
 *
 */

static
 NFCSTATUS
 phHciNfc_Send(
                    void                    *psContext,
                    void                    *pHwRef,
                    uint8_t                 *pdata,
#ifdef ONE_BYTE_LEN
                    uint8_t                 length
#else
                    uint16_t                length
#endif
              );

 static
 NFCSTATUS
 phHciNfc_Process_HCP (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                void                    *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t                 length
#else
                                uint16_t                length
#endif
                      );


static
NFCSTATUS
phHciNfc_Process_Response (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                void                    *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t                 length
#else
                                uint16_t                length
#endif
                         );

static
NFCSTATUS
phHciNfc_Error_Response (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                void                    *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t                 length
#else
                                uint16_t                length
#endif
                         );

static
NFCSTATUS
phHciNfc_Process_Event (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                void                    *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t                 length
#else
                                uint16_t                length
#endif
                         );


static
NFCSTATUS
phHciNfc_Process_Command (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                void                    *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t                 length
#else
                                uint16_t                length
#endif
                         );


static
void
phHciNfc_Reset_Pipe_MsgInfo(    
                            phHciNfc_Pipe_Info_t    *p_pipe_info
                        );

static
void
phHciNfc_Build_HCPMessage(
                                phHciNfc_HCP_Packet_t *hcp_packet,
                                uint8_t             msg_type,
                                uint8_t             instruction
                          );

static
void
phHciNfc_Build_HCPHeader(
                                phHciNfc_HCP_Packet_t *hcp_packet,
                                uint8_t             chainbit,
                                uint8_t             pipe_id
                          );
/**
 * \ingroup grp_hci_nfc
 *
 *  The phHciNfc_Receive_HCP function receive the HCI Host Control Packet 
 *  Frames from the device.
 *
 *  \param[in]  psHciContext            psHciContext is the context of
 *                                      the HCI Layer.
 *  \param[in]  pHwRef                  pHwRef is the Information of
 *                                      the Device Interface Link .
 *  \param[in] pdata                    Pointer to the response buffer that
 *                                      receives the response read.
 *  \param[in] length                   Variable that receives
 *                                      the number of bytes read.
 *
 *  \retval NFCSTATUS_PENDING           HCP Frame receive pending.
 *  \retval NFCSTATUS_INVALID_PARAMETER One or more of the supplied parameters
 *                                      could not be interpreted properly.
 *  \retval Other errors                Other related errors
 *
 *
 */


static
NFCSTATUS
phHciNfc_Receive_HCP (
                            phHciNfc_sContext_t     *psHciContext,
                            void                    *pHwRef,
                            uint8_t                 *pdata,
#ifdef ONE_BYTE_LEN
                            uint8_t                 length
#else
                            uint16_t                length
#endif
                     );


/*
################################################################################
***************************** Function Definitions *****************************
################################################################################
*/


#if  (NXP_NFC_HCI_TIMER == 1)

static
void
phHciNfc_Response_Timeout (
                    uint32_t resp_timer_id, void *pContext
                )
{
    phNfc_sCompletionInfo_t  comp_info = {0,0,0};

    if ( ( NULL != gpsHciContext)
            && (resp_timer_id == hci_resp_timer_id ))
    {
        pphNfcIF_Notification_CB_t  p_upper_notify =
            gpsHciContext->p_upper_notify;
        void                        *p_upper_context =
                                gpsHciContext->p_upper_context;
        phHal_sHwReference_t        *pHwRef = gpsHciContext->p_hw_ref;
		uint32_t				i = 0;


        HCI_DEBUG(" HCI TIMEOUT: HCI Response Timeout Occurred in %X Timer\n"
                                                                 ,resp_timer_id);
        /* Stop the Response Timer */
        phOsalNfc_Timer_Stop( hci_resp_timer_id );

		comp_info.status = PHNFCSTVAL(CID_NFC_HCI,
                        NFCSTATUS_BOARD_COMMUNICATION_ERROR); 
        /* Roll Back to the Select State */
        phHciNfc_FSM_Rollback(gpsHciContext);

		for(i=0;i < PHHCINFC_MAX_PIPE; i++)
		{
			phHciNfc_Reset_Pipe_MsgInfo(gpsHciContext->p_pipe_list[i]);
		}

        /* Notify the Error/Success Scenario to the upper layer */
        phHciNfc_Notify( p_upper_notify, p_upper_context,
                    pHwRef, (uint8_t) NFC_NOTIFY_DEVICE_ERROR, &comp_info );
    }

    return ;

}

#endif /* (NXP_NFC_HCI_TIMER == 1) */



/*!
 * \brief Allocation of the HCI Interface resources.
 *
 * This function releases and frees all the resources used by HCI Command and
 * Response Mechanism
 */

 NFCSTATUS
 phHciNfc_Allocate_Resource (
                                void                **ppBuffer,
                                uint16_t            size
                            )
{
    NFCSTATUS           status = NFCSTATUS_SUCCESS;

    *ppBuffer = (void *) phOsalNfc_GetMemory(size);
    if( *ppBuffer != NULL )
    {
        (void )memset(((void *)*ppBuffer), 0,
                                    size);
    }
    else
    {
        *ppBuffer = NULL;
        status = PHNFCSTVAL(CID_NFC_HCI,
                        NFCSTATUS_INSUFFICIENT_RESOURCES);
    }
    return status;
}



/*!
 * \brief Release of the HCI Interface resources.
 *
 * This function releases and frees all the resources used by HCI Command and
 * Response Mechanism
 */
 void
 phHciNfc_Release_Resources (
                                phHciNfc_sContext_t **ppsHciContext
                            )
{
    uint8_t i = 0;


#if  (NXP_NFC_HCI_TIMER == 1)

    if ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
    {
        /* Stop and Un-Intialise the Response Timer */
        phOsalNfc_Timer_Stop( hci_resp_timer_id );
        phOsalNfc_Timer_Delete( hci_resp_timer_id );
        HCI_DEBUG(" HCI : Timer %X Stopped and Released\n",
                                            hci_resp_timer_id);
        hci_resp_timer_id = NXP_INVALID_TIMER_ID;
    }
    gpsHciContext = NULL;

#endif /* (NXP_NFC_HCI_TIMER == 1) */


    if(NULL != (*ppsHciContext)->p_admin_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_admin_info);
        (*ppsHciContext)->p_admin_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_link_mgmt_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_link_mgmt_info);
        (*ppsHciContext)->p_link_mgmt_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_identity_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_identity_info);
        (*ppsHciContext)->p_identity_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_device_mgmt_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_device_mgmt_info);
        (*ppsHciContext)->p_device_mgmt_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_reader_mgmt_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_reader_mgmt_info);
        (*ppsHciContext)->p_reader_mgmt_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_poll_loop_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_poll_loop_info);
        (*ppsHciContext)->p_poll_loop_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_reader_a_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_reader_a_info);
        (*ppsHciContext)->p_reader_a_info = NULL;
    }
#ifdef TYPE_B
    if(NULL !=(*ppsHciContext)->p_reader_b_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_reader_b_info);
        (*ppsHciContext)->p_reader_b_info = NULL;
    }
#endif
#ifdef TYPE_FELICA
    if(NULL !=(*ppsHciContext)->p_felica_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_felica_info);
        (*ppsHciContext)->p_felica_info = NULL;
    }
#endif
#ifdef TYPE_JEWEL
    if(NULL !=(*ppsHciContext)->p_jewel_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_jewel_info);
        (*ppsHciContext)->p_jewel_info = NULL;
    }
#endif
#ifdef  TYPE_ISO15693
    if(NULL !=(*ppsHciContext)->p_iso_15693_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_iso_15693_info);
        (*ppsHciContext)->p_iso_15693_info = NULL;
    }
#endif /* #ifdef    TYPE_ISO15693 */
#ifdef ENABLE_P2P
    if(NULL !=(*ppsHciContext)->p_nfcip_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_nfcip_info);
        (*ppsHciContext)->p_nfcip_info = NULL;
    }
#endif
    if(NULL !=(*ppsHciContext)->p_emulation_mgmt_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_emulation_mgmt_info);
        (*ppsHciContext)->p_emulation_mgmt_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_wi_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_wi_info);
        (*ppsHciContext)->p_wi_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_swp_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_swp_info);
        (*ppsHciContext)->p_swp_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_uicc_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_uicc_info);
        (*ppsHciContext)->p_uicc_info = NULL;
    }
#ifdef HOST_EMULATION
    if(NULL !=(*ppsHciContext)->p_ce_a_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_ce_a_info);
        (*ppsHciContext)->p_ce_a_info = NULL;
    }
    if(NULL !=(*ppsHciContext)->p_ce_b_info)
    {
        phOsalNfc_FreeMemory((*ppsHciContext)->p_ce_b_info);
        (*ppsHciContext)->p_ce_b_info = NULL;
    }
#endif

    for(i=0;i < PHHCINFC_MAX_PIPE; i++)
    {
        if(NULL != (*ppsHciContext)->p_pipe_list[i])
        {
            phOsalNfc_FreeMemory((*ppsHciContext)->p_pipe_list[i]);
        }
    }


    phOsalNfc_FreeMemory((*ppsHciContext));
    (*ppsHciContext) = NULL;

    return ;
}

static
void
phHciNfc_Reset_Pipe_MsgInfo(    
                            phHciNfc_Pipe_Info_t    *p_pipe_info
                        )
{
    if (p_pipe_info != NULL)
    {
        p_pipe_info->sent_msg_type = HCP_MSG_TYPE_RESERVED;
        p_pipe_info->prev_msg = MSG_INSTRUCTION_UNKNWON;
        p_pipe_info->prev_status = NFCSTATUS_INVALID_HCI_INSTRUCTION;
        p_pipe_info->param_info = NULL;
        p_pipe_info->param_length = FALSE ;
    }
    return;
}


void
phHciNfc_Release_Lower(
                    phHciNfc_sContext_t         *psHciContext,
                    void                        *pHwRef
               )
{
    phNfc_sLowerIF_t            *plower_if = 
                                    &(psHciContext->lower_interface);
    NFCSTATUS            status = NFCSTATUS_SUCCESS;

    PHNFC_UNUSED_VARIABLE(status);
    if(NULL != plower_if->release)
    {
        status = plower_if->release((void *)plower_if->pcontext,
                                        (void *)pHwRef);
        (void) memset((void *)plower_if, 0, sizeof(phNfc_sLowerIF_t));
        HCI_DEBUG(" HCI Releasing the Lower Layer Resources: Status = %02X\n"
                                                                    ,status);
    }

    return;
}



/*!
 * \brief Sends the HCI Commands to the corresponding peripheral device.
 *
 * This function sends the HCI Commands to the connected NFC Pheripheral device
 */
 static
 NFCSTATUS
 phHciNfc_Send (
                      void                  *psContext,
                      void                  *pHwRef,
                      uint8_t               *pdata,
#ifdef ONE_BYTE_LEN
                      uint8_t               length
#else
                      uint16_t              length
#endif
                     )
{
    phHciNfc_sContext_t     *psHciContext= (phHciNfc_sContext_t  *)psContext;
    NFCSTATUS               status = NFCSTATUS_SUCCESS;

    phNfc_sLowerIF_t        *plower_if = &(psHciContext->lower_interface);

    if( (NULL != plower_if) 
        && (NULL != plower_if->send)
      )
    {
        HCI_DEBUG("HCI: In Function: %s \n", __FUNCTION__);
        HCI_DEBUG("HCI: Response Pending status --> %s \n",
            (psHciContext->response_pending)?"TRUE":"FALSE");
        HCI_PRINT_BUFFER("Send Buffer",pdata,length);
        /* psHciContext->hci_transact_state = NFC_TRANSACT_SEND_IN_PROGRESS; */

#if  (NXP_NFC_HCI_TIMER == 1)

    if ( 
        (TRUE != psHciContext->tx_hcp_chaining)
        &&  (TRUE == psHciContext->response_pending)
        && ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
       )
    {
        /* Start the HCI Response Timer */
        phOsalNfc_Timer_Start( hci_resp_timer_id,
                nxp_nfc_hci_response_timeout, phHciNfc_Response_Timeout, NULL );
        HCI_DEBUG(" HCI : Timer %X Started \n", hci_resp_timer_id);
    }

#endif /* (NXP_NFC_HCI_TIMER == 1) */

        status = plower_if->send((void *)plower_if->pcontext,
                                (void *)pHwRef, pdata, length);
    }

    return status;
}


/*!
 * \brief Receives the HCI Response from the corresponding peripheral device.
 *
 * This function receives the HCI Command Response to the connected NFC
 * Pheripheral device.
 */

NFCSTATUS
phHciNfc_Receive(
                        void                *psContext,
                        void                *pHwRef,
                        uint8_t             *pdata,
#ifdef ONE_BYTE_LEN
                        uint8_t             length
#else
                        uint16_t            length
#endif
                    )
{
    phHciNfc_sContext_t     *psHciContext= (phHciNfc_sContext_t  *)psContext;
    phNfc_sLowerIF_t *plower_if = NULL ;
    NFCSTATUS         status = NFCSTATUS_SUCCESS;

    if(NULL == psHciContext )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        plower_if = &(psHciContext->lower_interface);

        if( (NULL != plower_if) 
            && (NULL != plower_if->receive)
          )
        {
            /* psHciContext->hci_transact_state = NFC_TRANSACT_RECV_IN_PROGRESS; */
            status = plower_if->receive((void *)plower_if->pcontext,
                                    (void *)pHwRef, pdata, length);
        }
    }
    return status;
}


/*!
 * \brief Sends the HCP Packet to the lower link layer .
 *
 * This function Sends the HCI Data in the HCP packet format to the below
 * Link layer.
 */

 NFCSTATUS
 phHciNfc_Send_HCP (
                                phHciNfc_sContext_t *psHciContext,
                                void                *pHwRef
                   )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    phHciNfc_HCP_Packet_t   *tx_data = (phHciNfc_HCP_Packet_t *)
                                    psHciContext->send_buffer;
    /* Skip the HCP Header Byte initially */
    uint16_t                tx_length = psHciContext->tx_total - 1 ;
    uint16_t                hcp_index = HCP_ZERO_LEN;
    uint8_t                 pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
    static  uint8_t         chain_bit = HCP_CHAINBIT_DEFAULT;

    pipe_id =  (uint8_t) GET_BITS8( tx_data->hcp_header,
        HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);

    /* Fragmentation of the HCP Frames */
    if ( tx_length > PHHCINFC_MAX_PACKET_DATA )
    {
        tx_data = &psHciContext->tx_packet;
        (void)memset((void *)tx_data, FALSE,
                        sizeof(phHciNfc_HCP_Packet_t));
        if (HCP_CHAINBIT_DEFAULT == chain_bit)
        {
            /* HCI Chaining Needs to be Done */
            psHciContext->tx_remain = tx_length;
            psHciContext->tx_hcp_frgmnt_index = HCP_ZERO_LEN ;
            chain_bit = HCP_CHAINBIT_BEGIN;
            /* Increment the Fragment index to skip the HCP Header */
            psHciContext->tx_hcp_frgmnt_index++; 
            psHciContext->tx_hcp_chaining = TRUE ;
            tx_length = PHHCINFC_MAX_PACKET_DATA ;
        }
        else if ( psHciContext->tx_remain > PHHCINFC_MAX_PACKET_DATA )
        {
			/* Intermediate Chained HCI Frames */
            tx_length = PHHCINFC_MAX_PACKET_DATA ;
        }
        else
        {
            /* End of Chaining Reached */
            chain_bit = HCP_CHAINBIT_END;
            tx_length = psHciContext->tx_remain ;
            psHciContext->tx_hcp_chaining = FALSE ;
        }
        
        /* Build the HCP Header to have Chaining Enabled */
        phHciNfc_Build_HCPHeader(tx_data, chain_bit , pipe_id );

        phHciNfc_Append_HCPFrame((uint8_t *)tx_data->msg.payload, hcp_index, 
            (&psHciContext->send_buffer[psHciContext->tx_hcp_frgmnt_index])
            , tx_length );
    }
    else
    {
        /* No Chaining Required */
        chain_bit = HCP_CHAINBIT_DEFAULT;

        psHciContext->tx_hcp_chaining = FALSE ;

        psHciContext->tx_remain = tx_length ;
    }
    
    /* Include the Skipped HCP Header Byte */
    tx_length++;

    status = phHciNfc_Send ( (void *) psHciContext, pHwRef,
                        (uint8_t *)tx_data, tx_length );
    
    return status;
}


/*!
 * \brief Receives the HCP Packet from the lower link layer .
 *
 * This function receives the HCI Data in the HCP packet format from the below
 * Link layer.
 */
 static
 NFCSTATUS
 phHciNfc_Receive_HCP (
                                phHciNfc_sContext_t *psHciContext,
                                void                *pHwRef,
                                uint8_t             *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t             length
#else
                                uint16_t            length
#endif
                   )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    phHciNfc_HCP_Packet_t   *packet = NULL;
    uint8_t                 chainbit = HCP_CHAINBIT_DEFAULT;
    uint16_t                hcp_index = 0;

    packet = (phHciNfc_HCP_Packet_t *)pdata;
    chainbit = (uint8_t) GET_BITS8( packet->hcp_header,
        HCP_CHAINBIT_OFFSET, HCP_CHAINBIT_LEN);
    hcp_index = psHciContext->rx_hcp_frgmnt_index;
    HCI_PRINT_BUFFER("Receive Buffer",((uint8_t *)pdata),length);
    if (HCP_CHAINBIT_BEGIN == chainbit)
    {
        /* pdata = (uint8_t *)&psHciContext->rx_packet; */
        /* De Fragmentation of the Received HCP Frames */
        /* Subsequent Chaining Frames */
        if( hcp_index  > 0 )
        {
            /* Copy the obtained fragment and receive the next fragment */
            phHciNfc_Append_HCPFrame(
                psHciContext->recv_buffer, hcp_index, 
                    (uint8_t *)&pdata[HCP_MESSAGE_LEN],
                            (length - HCP_MESSAGE_LEN) );
            psHciContext->rx_hcp_frgmnt_index =(uint16_t)
                        (hcp_index + length - HCP_MESSAGE_LEN);
        }
        /* First Chaining Frame*/
        else
        {
            psHciContext->rx_hcp_chaining = TRUE ;
            /* Copy the obtained fragment and receive the next fragment */
            phHciNfc_Append_HCPFrame(psHciContext->recv_buffer, 
                hcp_index, pdata, length);
            psHciContext->rx_hcp_frgmnt_index = ( hcp_index + length ) ;

        }
        status = phHciNfc_Receive ( (void *) psHciContext, pHwRef,
                                                pdata, length);
    } 
    else
    {
        if(TRUE == psHciContext->rx_hcp_chaining)
        {
            /* If the chaining was done earlier */
            psHciContext->rx_hcp_chaining = FALSE ;
            /* Copy the Remaining buffer to the RX_BUFFER */
            phHciNfc_Append_HCPFrame(
                psHciContext->recv_buffer, hcp_index, 
                    (uint8_t *)&pdata[HCP_MESSAGE_LEN],
                            (length - HCP_MESSAGE_LEN) );
            /* If there is chaining done the return the same data */
            psHciContext->rx_total = 
                        (hcp_index + length - HCP_MESSAGE_LEN);
            psHciContext->rx_hcp_frgmnt_index = FALSE ;
        }
        else
        {
            (void) memcpy( psHciContext->recv_buffer, pdata, length);
            /* If there is no chaining done then return the same data */
            psHciContext->rx_total = (hcp_index + length);

        }
    }

    return status;
}


/*!
 * \brief Receives the HCP Packet from the lower link layer .
 *
 * This function receives the HCI Data in the HCP packet format from the below
 * Link layer.
 */

 static
 NFCSTATUS
 phHciNfc_Process_HCP (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                void                    *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t             length
#else
                                uint16_t            length
#endif
                      )
{
    phHciNfc_HCP_Packet_t   *packet = NULL;
    phHciNfc_HCP_Message_t  *message = NULL;
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    uint8_t                 msg_type = 0;

    if( (NULL == pdata)
        || ( length < HCP_HEADER_LEN )
      )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    }
    else
    {
        status = phHciNfc_Receive_HCP( psHciContext, pHwRef, pdata, length ); 
    }/* End of the Valid Data Handling */

    if( NFCSTATUS_SUCCESS  == status )
    {
        packet = (phHciNfc_HCP_Packet_t *)psHciContext->recv_buffer;
        length = 
#ifdef ONE_BYTE_LEN
            (uint8_t)
#endif
            psHciContext->rx_total ;
        message = &packet->msg.message;
        /* HCI_PRINT_BUFFER("Total Receive Buffer",((uint8_t *)pdata),length); */
        msg_type = (uint8_t) GET_BITS8( message->msg_header,
            HCP_MSG_TYPE_OFFSET, HCP_MSG_TYPE_LEN);
        switch ( msg_type )
        {
            case HCP_MSG_TYPE_RESPONSE:
            {
                status = phHciNfc_Process_Response( psHciContext,
                                                pHwRef, (void *)packet, length );
                break;
            }
            case HCP_MSG_TYPE_EVENT:
            {
                status = phHciNfc_Process_Event( psHciContext,
                                                pHwRef,(void *)packet, length );
                break;
            }
            case HCP_MSG_TYPE_COMMAND:
            {

                status = phHciNfc_Process_Command( psHciContext,
                                                pHwRef, (void *)packet, length );
                break;
            }
            /* case HCP_MSG_TYPE_RESERVED: */
            default:
            {
                status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
                break;
            }
        }
    }/* End of Receive HCP Status */
    return status;
}


 static
 NFCSTATUS
 phHciNfc_Process_Response (
                                 phHciNfc_sContext_t    *psHciContext,
                                 void                   *pHwRef,
                                 void                   *pdata,
#ifdef ONE_BYTE_LEN
                                 uint8_t             length
#else
                                 uint16_t            length
#endif
                             )
{
    phHciNfc_HCP_Packet_t   *packet = NULL;
    phHciNfc_HCP_Message_t  *message = NULL;
    uint8_t                 instruction=0;
    uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
    phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;

    NFCSTATUS               status = NFCSTATUS_SUCCESS;

    packet = (phHciNfc_HCP_Packet_t *)pdata;
    message = &packet->msg.message;
    /* Get the instruction bits from the Message Header */
    instruction = (uint8_t) GET_BITS8( message->msg_header,
                                HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
    /* Get the Pipe ID from the HCP Header */
    pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
                                HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);

#if  (NXP_NFC_HCI_TIMER == 1)

    if ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
    {
        /* Stop the HCI Response Timer */
        HCI_DEBUG(" HCI : Timer %X Stopped \n", hci_resp_timer_id);
        phOsalNfc_Timer_Stop( hci_resp_timer_id );
    }

#endif /* (NXP_NFC_HCI_TIMER == 1) */
    
    if (pipe_id >=  PHHCINFC_MAX_PIPE )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
    }
    else if( ((uint8_t) ANY_OK != instruction)
        && ( (pipe_id !=    PIPETYPE_STATIC_ADMIN )
        && ( ADM_CLEAR_ALL_PIPE != (psHciContext->p_pipe_list[pipe_id])->prev_msg ))
        )
    {
        status = phHciNfc_Error_Response( psHciContext, pHwRef, pdata, length );
    }
    else
    {
        p_pipe_info = psHciContext->p_pipe_list[pipe_id];
        if( ( NULL != p_pipe_info )
            &&   ( HCP_MSG_TYPE_COMMAND == p_pipe_info->sent_msg_type  )
            &&   ( NULL != p_pipe_info->recv_resp )
        )
        {
            status = psHciContext->p_pipe_list[pipe_id]->recv_resp( psHciContext,
                                                            pHwRef, pdata, length );
        }
        else
        {
            status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
        }
        /* There is no Pending Response */
        psHciContext->response_pending = FALSE ;
        HCI_DEBUG("HCI: Response Pending status --> FALSE, %s \n",
            __FUNCTION__);
        if( NFCSTATUS_SUCCESS == status )
        {
            phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]);
            status = phHciNfc_Resume_Sequence(psHciContext, pHwRef);

        }/* End of Success Status validation */
        else
        {
            HCI_DEBUG("HCI: Status --> %X \n", status );
        }

    } /* End of the Valid Response handling */
    return status;
}


static
 NFCSTATUS
 phHciNfc_Error_Response (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                void                    *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t                 length
#else
                                uint16_t                length
#endif
                         )
{

    phHciNfc_HCP_Packet_t   *packet = (phHciNfc_HCP_Packet_t *)pdata;
    phHciNfc_HCP_Message_t  *message = &packet->msg.message;
    NFCSTATUS               status = NFCSTATUS_SUCCESS;
    uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
#if defined(HCI_TRACE) || defined (ERROR_INSTRUCTION)
    uint8_t                 instruction = 0;
    instruction = (uint8_t) GET_BITS8(message->msg_header,
                            HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
#endif

    /* Get the Pipe ID from the HCP Header */
    pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
                                HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
    /* Process the Error Response based on the obtained instruction */
#ifdef ERROR_INSTRUCTION
    switch(instruction)
    {
        case ANY_E_NOT_CONNECTED:
        case ANY_E_CMD_PAR_UNKNOWN:
        case ANY_E_NOK:
        case ANY_E_PIPES_FULL:
        case ANY_E_REG_PAR_UNKNOWN:
        case ANY_E_PIPE_NOT_OPENED:
        case ANY_E_CMD_NOT_SUPPORTED:
        case ANY_E_TIMEOUT:
        case ANY_E_REG_ACCESS_DENIED:
        case ANY_E_PIPE_ACCESS_DENIED:
        {
            /* Receive Error Notification to the Upper Layer */
            status = PHNFCSTVAL( CID_NFC_HCI, \
                            message->msg_header);
            phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, length );
            /* Return Success as the Error Sequence is already handled */
            psHciContext->response_pending = FALSE ;
            HCI_DEBUG("HCI: Response Pending status --> FALSE, %s \n",
            __FUNCTION__);
            status = NFCSTATUS_SUCCESS;
            break;
        }
            /* The Statement should not reach this case */
        /* case ANY_OK: */
        default:
        {
            /* status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE); */
            break;
        }
    }
#else
    status = PHNFCSTVAL( CID_NFC_HCI, message->msg_header);
    HCI_DEBUG("HCI Error Response(%u) from the Device \n", instruction);
    psHciContext->response_pending = FALSE ;
    HCI_DEBUG("HCI: Response Pending status --> FALSE, %s \n",
        __FUNCTION__);
    phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]);
    phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, (uint8_t) length );
    /* Return Success as the Error Sequence is already handled */
    status = NFCSTATUS_SUCCESS;
#endif

    return status;
}


static
 NFCSTATUS
 phHciNfc_Process_Event (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                void                    *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t                 length
#else
                                uint16_t                length
#endif
                        )
{
    phHciNfc_HCP_Packet_t   *packet = NULL;
    phHciNfc_HCP_Message_t  *message = NULL;
    phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
    uint8_t                 instruction=0;
    uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;

    NFCSTATUS               status = NFCSTATUS_SUCCESS;

    packet = (phHciNfc_HCP_Packet_t *)pdata;
    message = &packet->msg.message;
    /* Get the instruction bits from the Message Header */
    PHNFC_UNUSED_VARIABLE(instruction);
    instruction = (uint8_t) GET_BITS8( message->msg_header,
                                HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
    /* Get the Pipe ID from the HCP Header */
    pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
                                HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);

    if (pipe_id >=  PHHCINFC_MAX_PIPE )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
    }
    else
    {
        p_pipe_info = psHciContext->p_pipe_list[pipe_id];
    }

    if( (p_pipe_info != NULL ) )
    {
        if( NULL != p_pipe_info->recv_event)
        {
            status = p_pipe_info->recv_event( psHciContext, pHwRef,
                                                        pdata, length );
        }
        else
        {
            HCI_DEBUG(" Event Handling Not Supported by the #%u Pipe \n",
                                                        pipe_id);
            status = PHNFCSTVAL(CID_NFC_HCI,
                                        NFCSTATUS_FEATURE_NOT_SUPPORTED);
        }
    }
    else
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
    }

    HCI_DEBUG("HCI: In Function: %s \n",
        __FUNCTION__);
    HCI_DEBUG("HCI: Response Pending status --> %s \n",
        (psHciContext->response_pending)?"TRUE":"FALSE");
    HCI_DEBUG("HCI: Event Pending status --> %s \n",
        (psHciContext->event_pending)?"TRUE":"FALSE");

        if ((TRUE == psHciContext->response_pending)
        || (TRUE == psHciContext->event_pending))
    {
        (void)memset(psHciContext->recv_buffer,
            FALSE, PHHCINFC_MAX_BUFFERSIZE);
        (void)memset((void *)&psHciContext->rx_packet,
            FALSE, sizeof(phHciNfc_HCP_Packet_t));

        /* Reset the Received Data Index */
        psHciContext->rx_index = ZERO;
        /* Reset the size of the total response data received */
        psHciContext->rx_total = ZERO;

        /* psHciContext->hci_transact_state = NFC_TRANSACT_SEND_COMPLETE;*/
        /* Receive the Response Packet */

        status = phHciNfc_Receive( psHciContext, pHwRef, 
                    (uint8_t *)(&psHciContext->rx_packet), 
                    sizeof(phHciNfc_HCP_Packet_t) );

        /* HCI_DEBUG("HCI Lower Layer Send Completion After Receive,\
        Status = %02X\n",status); */
    }
    else
    {
        if( 
/* #define EVENT_NOTIFY */
#ifndef EVENT_NOTIFY
            ( NFCSTATUS_SUCCESS == status  )
            || ( NFCSTATUS_RF_TIMEOUT == status  )
            || (( NFCSTATUS_MORE_INFORMATION == status  )
#else
            ((FALSE == psHciContext->event_pending )
#endif
            && ( pipe_id <= PHHCINFC_MAX_PIPE ))
            )
        {
            /* phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]); */
            status = phHciNfc_Resume_Sequence(psHciContext, pHwRef);

        }/* End of Success Status validation */
        else
        {
            HCI_DEBUG(" HCI: Pipe-ID --> %02X  \n", pipe_id);
            HCI_DEBUG(" HCI: PROCESS EVENT - Pending/Invalid Status : %X\n", status);
        }
    }

    return status;
}

static
 NFCSTATUS
 phHciNfc_Process_Command (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                void                    *pdata,
#ifdef ONE_BYTE_LEN
                                uint8_t                 length
#else
                                uint16_t                length
#endif
                             )
{
    phHciNfc_HCP_Packet_t   *packet = NULL;
    phHciNfc_HCP_Message_t  *message = NULL;
    phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
    uint8_t                 instruction=0;
    uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;

    NFCSTATUS               status = NFCSTATUS_SUCCESS;

    packet = (phHciNfc_HCP_Packet_t *)pdata;
    message = &packet->msg.message;
    /* Get the instruction bits from the Message Header */
    PHNFC_UNUSED_VARIABLE(instruction);

    instruction = (uint8_t) GET_BITS8( message->msg_header,
                                HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
    /* Get the Pipe ID from the HCP Header */
    pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
                                HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
    if (pipe_id >=  PHHCINFC_MAX_PIPE )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
    }
    else
    {
        p_pipe_info = psHciContext->p_pipe_list[pipe_id];
    }

    if( (p_pipe_info != NULL )
        )
    {
        if( NULL != p_pipe_info->recv_cmd)
        {
            status = p_pipe_info->recv_cmd( psHciContext,   pHwRef,
                                                        pdata, length );
        }
        else
        {
            HCI_DEBUG(" Command Handling Not Supported by the #%u Pipe \n",
                                                        pipe_id);
            status = PHNFCSTVAL(CID_NFC_HCI,
                                        NFCSTATUS_FEATURE_NOT_SUPPORTED);
        }
    }
    else
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
    }

    HCI_DEBUG("HCI: In Function: %s \n", __FUNCTION__);
    HCI_DEBUG("HCI: Response Pending status --> %s \n",
        (psHciContext->response_pending)?"TRUE":"FALSE");

    if(( NFCSTATUS_SUCCESS == status )
        && (TRUE != psHciContext->response_pending)
        )
    {
        /* Reset the Pipe Information Stored in the particular Pipe */
        /* phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]); */
        /* Resume the Execution Sequence */
        status = phHciNfc_Resume_Sequence(psHciContext, pHwRef);

    }/* End of Success Status validation */

    return status;
}


static
void
phHciNfc_Build_HCPMessage(
                                phHciNfc_HCP_Packet_t *hcp_packet,
                                uint8_t             msg_type,
                                uint8_t             instruction
                          )
{
    phHciNfc_HCP_Message_t  *hcp_message = NULL;

    hcp_message = &(hcp_packet->msg.message);
    /* Set the type to the provided message type in the HCP Message Header */ 
    hcp_message->msg_header = (uint8_t) SET_BITS8(hcp_message->msg_header,HCP_MSG_TYPE_OFFSET,
                HCP_MSG_TYPE_LEN, msg_type);
    /* Set the instruction to the kind of instruction in the HCP Message Header */ 
    hcp_message->msg_header  = (uint8_t) SET_BITS8(hcp_message->msg_header,HCP_MSG_INSTRUCTION_OFFSET,
                HCP_MSG_INSTRUCTION_LEN, instruction);
    /* hcp_message->msg_header = hcp_message->msg_header | temp ; */

}


static
void
phHciNfc_Build_HCPHeader(
                                phHciNfc_HCP_Packet_t *hcp_packet,
                                uint8_t             chainbit,
                                uint8_t             pipe_id
                          )
{
    /* Set the Chaining bit to the default type */ 
    hcp_packet->hcp_header = (uint8_t) SET_BITS8(hcp_packet->hcp_header,
                HCP_CHAINBIT_OFFSET, HCP_CHAINBIT_LEN, chainbit);
    /* Populate the Pipe ID to the HCP Header */ 
    hcp_packet->hcp_header  = (uint8_t) SET_BITS8(hcp_packet->hcp_header,HCP_PIPEID_OFFSET,
                HCP_PIPEID_LEN, pipe_id);

}

/*!
 * \brief Builds the HCP Frame Packet.
 *
 * This function builds the HCP Frame in the HCP packet format to send to the 
 * connected reader device.
 */

void
 phHciNfc_Build_HCPFrame (
                                phHciNfc_HCP_Packet_t *hcp_packet,
                                uint8_t             chainbit,
                                uint8_t             pipe_id,
                                uint8_t             msg_type,
                                uint8_t             instruction
                          )
{
    /* Fills the HCP Header in the packet */
    phHciNfc_Build_HCPHeader( hcp_packet,chainbit,pipe_id );
    /* Fills the HCP Message in the packet */
    phHciNfc_Build_HCPMessage( hcp_packet,msg_type,instruction );
}

/*!
 * \brief Appends the HCP Frame Packet.
 *
 * This function Appends the HCP Frame of the HCP packet to complete the
 * entire HCP Packet.
 */

void
 phHciNfc_Append_HCPFrame (
/*                              phHciNfc_sContext_t     *psHciContext, */
                                uint8_t                 *hcp_data,
                                uint16_t                hcp_index,
                                uint8_t                 *src_data,
                                uint16_t                src_len
                          )
{
    uint16_t src_index = 0;
    if( (NULL != src_data) 
        /* && (hcp_index >= 0) */
        && (src_len > 0) 
        )
    {
        for(src_index=0; src_index < src_len ; src_index++)
        {
            hcp_data[hcp_index + src_index] = src_data[src_index];
        }
    }
    return;
}


/*!
 * \brief Sends the Generic HCI Commands to the connected reader device.
 *
 * This function Sends the Generic HCI Command frames in the HCP packet format to the 
 * connected reader device.
 */

 NFCSTATUS
 phHciNfc_Send_Generic_Cmd (
                                phHciNfc_sContext_t *psHciContext,
                                void                *pHwRef,
                                uint8_t             pipe_id,
                                uint8_t             cmd
                    )
 {
    phHciNfc_HCP_Packet_t   *hcp_packet = NULL;
    phHciNfc_HCP_Message_t  *hcp_message = NULL;
    phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
    uint16_t                 length = 0;
    uint16_t                 i=0;
    NFCSTATUS               status = NFCSTATUS_SUCCESS;

    if((NULL == psHciContext)
        || ( pipe_id > PHHCINFC_MAX_PIPE)
        ||(NULL == psHciContext->p_pipe_list[pipe_id])
      )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
        HCI_DEBUG("%s: Invalid Arguments passed \n",
                                                "phHciNfc_Send_Generic_Cmd");
    }
    else
    {
        p_pipe_info = (phHciNfc_Pipe_Info_t *) 
                                psHciContext->p_pipe_list[pipe_id];
        psHciContext->tx_total = 0 ;
        length +=  HCP_HEADER_LEN ;
        switch( cmd )
        {
            case ANY_SET_PARAMETER:
            {
                
                hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
                /* Construct the HCP Frame */
                phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
                                        (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
                hcp_message = &(hcp_packet->msg.message);
                hcp_message->payload[i++] = p_pipe_info->reg_index ;
                phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
                                            i, (uint8_t *)p_pipe_info->param_info,
                                            p_pipe_info->param_length);
                length =(uint16_t)(length + i + p_pipe_info->param_length);
                break;
            }
            case ANY_GET_PARAMETER:
            {

                hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
                /* Construct the HCP Frame */
                phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
                                        (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
                hcp_message = &(hcp_packet->msg.message);
                hcp_message->payload[i++] = p_pipe_info->reg_index ;
                length =(uint16_t)(length + i);
                break;
            }
            case ANY_OPEN_PIPE:
            case ANY_CLOSE_PIPE:
            {

                hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
                /* Construct the HCP Frame */
                phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
                                        (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
                break;
            }
            default:
            {
                status = PHNFCSTVAL( CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED );
                HCI_DEBUG("%s: Statement Should Not Occur \n","phHciNfc_Send_Generic_Cmd");
                break;
            }
        }
        if( NFCSTATUS_SUCCESS == status )
        {
            p_pipe_info->sent_msg_type = HCP_MSG_TYPE_COMMAND;
            p_pipe_info->prev_msg = cmd;
            psHciContext->tx_total = length;
            psHciContext->response_pending = TRUE ;
            /* Send the Constructed HCP packet to the lower layer */
            status = phHciNfc_Send_HCP( psHciContext, pHwRef );
            p_pipe_info->prev_status = NFCSTATUS_PENDING;
        }
    }

    return status;
}


/*!
 * \brief Sets the parameter of the registers in a particular Pipe.
 *
 * This function configures the registers in a particular Pipe.
 */

NFCSTATUS
phHciNfc_Set_Param (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                phHciNfc_Pipe_Info_t    *p_pipe_info,
                                uint8_t                 reg_index,
                                void                    *p_param,
                                uint16_t                 param_length
                    )
 {
    NFCSTATUS               status = NFCSTATUS_SUCCESS ;

    if( (NULL == p_pipe_info)
        || (NULL == p_param)
        || (0 == param_length)
        )
    {
        status = PHNFCSTVAL( CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION );
    }
    else
    {
        p_pipe_info->param_info = (uint8_t *)p_param;
        p_pipe_info->param_length =  param_length;
        p_pipe_info->reg_index = reg_index;
        status = phHciNfc_Send_Generic_Cmd( psHciContext, pHwRef, 
            (uint8_t)p_pipe_info->pipe.pipe_id,
                (uint8_t)ANY_SET_PARAMETER);
        p_pipe_info->prev_status = status;
    }

    return status;
 }


#if 0
 /*!
 * \brief Gets the parameter of the registers in a particular Pipe.
 *
 * This function configures the registers in a particular Pipe.
 */

 NFCSTATUS
 phHciNfc_Get_Param (
                                phHciNfc_sContext_t     *psHciContext,
                                void                    *pHwRef,
                                phHciNfc_Pipe_Info_t    *p_pipe_info,
                                uint8_t                 reg_index,
                    )
 {
    NFCSTATUS               status = NFCSTATUS_SUCCESS ;

    return status;
 }
#endif


void
phHciNfc_Send_Complete (
                                void                    *psContext,
                                void                    *pHwRef,
                                phNfc_sTransactionInfo_t *pInfo
                       )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS ;
    uint16_t                length = 0;

    HCI_PRINT("HCI Send Completion....\n");
    if ( (NULL != psContext)
        && (NULL != pInfo) 
        && (NULL != pHwRef) 
        )
    {
        phHciNfc_sContext_t *psHciContext = (phHciNfc_sContext_t *)psContext;
        status = pInfo->status ;
        length = pInfo->length ;
        /* HCI_DEBUG("HCI Lower Layer Send Completion Before Receive,\
                                                Status = %02X\n",status); */
        if(status != NFCSTATUS_SUCCESS)
        {
            /* Handle the Error Scenario */
            (void)memset(psHciContext->send_buffer,
                                            FALSE, PHHCINFC_MAX_BUFFERSIZE);
            /* psHciContext->hci_transact_state = NFC_TRANSACT_COMPLETE;*/
            phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
        }
        else
        {
	        HCI_DEBUG("HCI Send Completion... Length = %02X\n", length); 
            /* To complete the send complete with the send 
             * or receive with chaining.
             */
            if( (TRUE == psHciContext->tx_hcp_chaining)
                &&( psHciContext->tx_remain > HCP_ZERO_LEN ))
            {
                /* Skip the HCP Header Byte Sent */
                psHciContext->tx_remain -= length - 1;

                /* Skip the HCP Header Byte Sent */
                psHciContext->tx_hcp_frgmnt_index += length - 1;

                /* Send the Remaining HCP Data Frames */
                status = phHciNfc_Send_HCP( psHciContext, pHwRef );

                HCI_DEBUG("HCI (Chaining) Send Resume: Status = %02X\n", status);

                if( ( NFCSTATUS_SUCCESS != status )
                    && (NFCSTATUS_PENDING != status )
                    )
                {
                    phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
                }/* End of the Status check */
            }
            else
            {
                psHciContext->tx_total = HCP_ZERO_LEN ;
                psHciContext->tx_remain = HCP_ZERO_LEN ;
                psHciContext->tx_hcp_frgmnt_index = HCP_ZERO_LEN ;
                HCI_DEBUG("HCI: %s: response_pending=%s, event_pending=%s",
                        __FUNCTION__,
                        (psHciContext->response_pending)?"TRUE":"FALSE",
                        (psHciContext->event_pending)?"TRUE":"FALSE"
                         );
                if ((TRUE == psHciContext->response_pending)
                    || (TRUE == psHciContext->event_pending))
                {
                    (void) memset(psHciContext->recv_buffer,
                        FALSE, PHHCINFC_MAX_BUFFERSIZE);
                    (void) memset((void *)&psHciContext->rx_packet,
                        FALSE, sizeof(phHciNfc_HCP_Packet_t));

                    /* Reset the Received Data Index */
                    psHciContext->rx_index = ZERO;
                    /* Reset the size of the total response data received */
                    psHciContext->rx_total = ZERO;

                    /* psHciContext->hci_transact_state = NFC_TRANSACT_SEND_COMPLETE;*/
                    /* Receive the Response Packet */
                    status = phHciNfc_Receive( psHciContext, pHwRef, 
                                (uint8_t *)(&psHciContext->rx_packet), 
                                sizeof(phHciNfc_HCP_Packet_t) );

                    /* HCI_DEBUG("HCI Lower Layer Send Completion After Receive,\
                    Status = %02X\n",status); */

                    if( ( NFCSTATUS_SUCCESS != status )
                         && (NFCSTATUS_PENDING != status )
                        )
                    {
                        phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
                    }/* End of the Status check */
                }
                else
                {
                    status = phHciNfc_Resume_Sequence(psHciContext, pHwRef );
                } 
            } 

        } /* End of status != Success */

    } /* End of Context != NULL  */
}


void
phHciNfc_Receive_Complete (
                                void                    *psContext,
                                void                    *pHwRef,
                                phNfc_sTransactionInfo_t *pInfo
                                )
{
    NFCSTATUS               status = NFCSTATUS_SUCCESS ;
    void                    *pdata = NULL ;
    uint16_t                length = 0 ;

    HCI_PRINT("HCI Receive Completion....\n");
    if ( (NULL != psContext)
        && (NULL != pInfo) 
        && (NULL != pHwRef) 
        )
    {
        phHciNfc_sContext_t *psHciContext = (phHciNfc_sContext_t *)psContext;

        status = pInfo->status ;
        pdata = pInfo->buffer ;
        length = pInfo->length ;
        HCI_DEBUG("HCI Lower Layer Receive Completion, Status = %02X\n",status);
        if( NFCSTATUS_SUCCESS != status )
        {
            /* Handle the Error Scenario */
            /* psHciContext->hci_transact_state = NFC_TRANSACT_COMPLETE; */
            phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, (uint8_t)length );
        }
        else
        {
             /* Receive the remaining Response Packet */
            /* psHciContext->hci_transact_state = NFC_TRANSACT_RECV_COMPLETE; */
            status = phHciNfc_Process_HCP( psHciContext, pHwRef, pdata,(uint8_t) length );
            if( ( NFCSTATUS_SUCCESS != status )
                && (NFCSTATUS_PENDING != status )
              )
            {
                phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, (uint8_t) length );
            }
        }
    }
}

void
phHciNfc_Notify(
                    pphNfcIF_Notification_CB_t  p_upper_notify,
                    void                        *p_upper_context,
                    void                        *pHwRef,
                    uint8_t                     type,
                    void                        *pInfo
               )
{
    if( ( NULL != p_upper_notify) )
    {
        /* Notify the to the Upper Layer */
        (p_upper_notify)(p_upper_context, pHwRef, type, pInfo);
    }

}


void
phHciNfc_Tag_Notify(
                            phHciNfc_sContext_t     *psHciContext,
                            void                    *pHwRef,
                            uint8_t                 type,
                            void                    *pInfo
               )
{
    phNfc_sCompletionInfo_t *psCompInfo = 
                                (phNfc_sCompletionInfo_t *)pInfo;
    pphNfcIF_Notification_CB_t  p_upper_notify = psHciContext->p_upper_notify;
    void                        *pcontext = psHciContext->p_upper_context;
    NFCSTATUS               status = NFCSTATUS_SUCCESS;

    switch( psHciContext->hci_state.next_state )
    {
        case hciState_Activate:
        {
            /* Roll Back to the Select State */
            phHciNfc_FSM_Rollback(psHciContext);
            break;
        }
        case hciState_Select:
        {
            status = phHciNfc_FSM_Complete(psHciContext);
            break;
        }
        default:
        {
            /* Roll Back to the Select State */
            phHciNfc_FSM_Rollback(psHciContext);
            break;
        }

    }

    if(NFCSTATUS_SUCCESS == status )
    {
            /* Notify the Tag Events to the Upper layer */
        phHciNfc_Notify( p_upper_notify, pcontext , pHwRef,
                                type, psCompInfo);
    }
    else
    {
        phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
    }
}


void
phHciNfc_Target_Select_Notify(
                            phHciNfc_sContext_t     *psHciContext,
                            void                    *pHwRef,
                            uint8_t                 type,
                            void                    *pInfo
               )
{
    phNfc_sCompletionInfo_t *psCompInfo = 
                                (phNfc_sCompletionInfo_t *)pInfo;
    pphNfcIF_Notification_CB_t  p_upper_notify = psHciContext->p_upper_notify;
    void                        *pcontext = psHciContext->p_upper_context;
    NFCSTATUS               status = NFCSTATUS_SUCCESS;

    switch( psHciContext->hci_state.next_state )
    {
        case hciState_Listen:
        {
            /* Roll Back to the Select State */
            status = phHciNfc_FSM_Complete(psHciContext);
            break;
        }
        case hciState_Select:
        {
            status = phHciNfc_FSM_Complete(psHciContext);
            break;
        }
        default:
        {
            /* Roll Back to the Select State */
            phHciNfc_FSM_Rollback(psHciContext);
            break;
        }

    }

    if(NFCSTATUS_SUCCESS == status )
    {
            /* Notify the Tag Events to the Upper layer */
        phHciNfc_Notify( p_upper_notify, pcontext , pHwRef,
                                type, psCompInfo);
    }
    else
    {
        phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
    }

}




void
phHciNfc_Release_Notify(
                            phHciNfc_sContext_t     *psHciContext,
                            void                    *pHwRef,
                            uint8_t                 type,
                            void                    *pInfo
               )
{
    phNfc_sCompletionInfo_t *psCompInfo = 
                                (phNfc_sCompletionInfo_t *)pInfo;
    pphNfcIF_Notification_CB_t  p_upper_notify = psHciContext->p_upper_notify;
    void                        *pcontext = psHciContext->p_upper_context;
    phHciNfc_Release_Resources( &psHciContext );
        /* Notify the Failure to the Upper Layer */
    phHciNfc_Notify( p_upper_notify, pcontext , pHwRef,
                            type, psCompInfo);
}


void
phHciNfc_Notify_Event(
                            void                    *psContext,
                            void                    *pHwRef,
                            uint8_t                 type,
                            void                    *pInfo
                    )
{
    NFCSTATUS            status = NFCSTATUS_SUCCESS;

    if ( (NULL != psContext)
        && (NULL != pInfo) 
        && (NULL != pHwRef) 
        )
    {
        phHciNfc_sContext_t *psHciContext = (phHciNfc_sContext_t *)psContext;

        /* Process based on the Notification type */
        switch(type)
        {
            case NFC_NOTIFY_INIT_COMPLETED:
            {
                phNfc_sCompletionInfo_t *psCompInfo = 
                                            (phNfc_sCompletionInfo_t *)pInfo;
                if(NFCSTATUS_SUCCESS == psCompInfo->status)
                {

#if  (NXP_NFC_HCI_TIMER == 1)
                    if ( NXP_INVALID_TIMER_ID == hci_resp_timer_id )
                    {
                        /* Create and Intialise the Response Timer */
                        hci_resp_timer_id = phOsalNfc_Timer_Create( );
                        HCI_DEBUG(" HCI : Timer %X Created \n",
                                                            hci_resp_timer_id);
                    }
                    else
                    {
                        HCI_DEBUG(" HCI : Timer Already Created, Timer ID : %X\n",
                                                                hci_resp_timer_id);
                    }
                    gpsHciContext = psHciContext;

#endif /* (NXP_NFC_HCI_TIMER == 1) */

                     /* Complete the Initialisation Sequence */
                    status = phHciNfc_Resume_Sequence(psContext ,pHwRef);
                }
                else
                {
                    /* Notify the Error Scenario to the Upper Layer */
                    phHciNfc_Notify(psHciContext->p_upper_notify,
                                    psHciContext->p_upper_context, pHwRef,
                                            NFC_NOTIFY_ERROR, psCompInfo);
                }
                break;
            }
            case NFC_NOTIFY_INIT_FAILED:
            {
                 /* Notify the Failure to the Upper Layer */
                phHciNfc_Release_Notify( psContext,pHwRef,
                                        type, pInfo );
                break;
            }
            case NFC_NOTIFY_RECV_COMPLETED:
            {
                /* Receive Completed from the Lower Layer */
                phHciNfc_Receive_Complete(psContext,pHwRef,pInfo);

                break;
            }
            case NFC_NOTIFY_SEND_COMPLETED:
            {
                /* Receive Completed from the Lower Layer */
                phHciNfc_Send_Complete(psContext,pHwRef,pInfo);

                break;
            }
            case NFC_NOTIFY_TRANSCEIVE_COMPLETED:
            {
                /* TODO: TO handle Both Send and Receive Complete */
                break;
            }
            case NFC_NOTIFY_TARGET_DISCOVERED:
            {
                HCI_PRINT(" PICC Discovery ! Obtain PICC Info .... \n");
                /* psHciContext->hci_seq = PL_DURATION_SEQ; */
                if ( hciState_Unknown == psHciContext->hci_state.next_state )
                {

                    status = phHciNfc_FSM_Update ( psHciContext, hciState_Select );


                    if (NFCSTATUS_SUCCESS != status)
                    {
                       status = phHciNfc_ReaderMgmt_Deselect( 
                            psHciContext, pHwRef, phHal_eISO14443_A_PICC, FALSE); 
                    }
                }
                else
                {
#ifdef SW_RELEASE_TARGET
                    /*status = phHciNfc_ReaderMgmt_Deselect( 
                        psHciContext, pHwRef, phHal_eISO14443_A_PICC, FALSE); */
                    psHciContext->target_release = TRUE;
#else
                    status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_STATE);
#endif
                }
                break;
            }
            /* To Notify the Target Released Notification 
             * to the Above Layer */
            case NFC_NOTIFY_TARGET_RELEASED:
            /* To Notify the NFC Secure Element Transaction 
             * Information to the Above Layer */
            /* case NFC_NOTIFY_TRANSACTION: */
            /* To Notify the Generic Events To the Upper 
             * Layer */
            case NFC_NOTIFY_EVENT:
            /* To Notify the Data Receive  Notification 
             * to the Above Layer */
            case NFC_NOTIFY_RECV_EVENT:
            {
                phNfc_sCompletionInfo_t *psCompInfo = 
		                (phNfc_sCompletionInfo_t *)pInfo;

                if (((TRUE == psHciContext->event_pending) || 
                    (NFCSTATUS_RF_TIMEOUT == psCompInfo->status))
                    && ( hciState_Transact == psHciContext->hci_state.next_state))
                {
                    /* Rollback due to Transmission Error */
                    phHciNfc_FSM_Rollback(psHciContext);
                }
                psHciContext->event_pending = FALSE;
                phHciNfc_Notify(psHciContext->p_upper_notify,
                            psHciContext->p_upper_context, pHwRef,
                                type, pInfo);
                break;
            }
            case NFC_NOTIFY_DEVICE_ACTIVATED:
            {
                HCI_PRINT("  Device Activated! Obtaining Remote Reader Info .... \n");
                if ( hciState_Unknown == psHciContext->hci_state.next_state )
                {
                    switch (psHciContext->host_rf_type)
                    {
                        case phHal_eISO14443_A_PCD:
                        case phHal_eISO14443_B_PCD:
                        case phHal_eISO14443_BPrime_PCD:
                        case phHal_eFelica_PCD:
                        {
                            break;
                        }
                        case phHal_eNfcIP1_Initiator:
                        case phHal_eNfcIP1_Target:
                        {
                            break;
                        }
                        case phHal_eUnknown_DevType:
                        default:
                        {
                            status = PHNFCSTVAL(CID_NFC_HCI, 
                                        NFCSTATUS_INVALID_PARAMETER);
                            break;
                        }

                    }
                    status = phHciNfc_FSM_Update ( psHciContext, hciState_Listen );
                }
                else
                {
                    status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_STATE);
                }
                break;
            }
            case NFC_NOTIFY_DEVICE_DEACTIVATED:
            {
                HCI_PRINT(" Device De-Activated! \n");
                if ( hciState_Unknown == psHciContext->hci_state.next_state )
                {
                    status = phHciNfc_FSM_Update ( psHciContext, hciState_Initialise );
                    if(NFCSTATUS_SUCCESS == status)
                    {
                        /* Complete to the Select State */
                        status = phHciNfc_FSM_Complete(psHciContext);
                    }
                    else
                    {
                        HCI_PRINT(" Device Deactivated.. But Invalid State \n");
                        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_STATE);
                    }
                }
                else
                {
                    status = phHciNfc_ReaderMgmt_Update_Sequence(
                                                psHciContext, INFO_SEQ );

                    if(( hciState_Listen == psHciContext->hci_state.next_state)
                        || (hciState_Transact == psHciContext->hci_state.next_state))
                    {
                        psHciContext->hci_state.next_state = hciState_Initialise;
                        /* Roll Back to the Default State */
                        status = phHciNfc_FSM_Complete(psHciContext);
                    }
                }
                psHciContext->event_pending = FALSE;
                phHciNfc_Notify(psHciContext->p_upper_notify,
                            psHciContext->p_upper_context, pHwRef,
                            NFC_NOTIFY_EVENT, pInfo);
                break;
            }
            case NFC_NOTIFY_DEVICE_ERROR:
            {
                phNfc_sCompletionInfo_t *psCompInfo = 
                                            (phNfc_sCompletionInfo_t *)pInfo;

                psCompInfo->status = ( NFCSTATUS_BOARD_COMMUNICATION_ERROR 
                                        != PHNFCSTATUS(psCompInfo->status))?
                                            NFCSTATUS_BOARD_COMMUNICATION_ERROR:
                                                psCompInfo->status ;

#if  (NXP_NFC_HCI_TIMER == 1)

                if ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
                {
                    HCI_DEBUG(" HCI : Response Timer Stop, Status:%02X",
                                                          psCompInfo->status);
                    /* Stop and Un-Intialise the Response Timer */
                    phOsalNfc_Timer_Stop( hci_resp_timer_id );
                }

#endif /* (NXP_NFC_HCI_TIMER == 1) */

                phHciNfc_Notify(psHciContext->p_upper_notify,
                            psHciContext->p_upper_context, pHwRef,
                            (uint8_t) NFC_NOTIFY_DEVICE_ERROR, pInfo);

                break;
            }

            case NFC_NOTIFY_ERROR:
            default:
            {
                phNfc_sCompletionInfo_t *psCompInfo = 
                                            (phNfc_sCompletionInfo_t *)pInfo;

#if  (NXP_NFC_HCI_TIMER == 1)

                if (( NFCSTATUS_BOARD_COMMUNICATION_ERROR == PHNFCSTATUS(psCompInfo->status))
                        && ( NXP_INVALID_TIMER_ID != hci_resp_timer_id ))                
                {
                    HCI_DEBUG(" HCI : Response Timer Stop, Status:%02X",
                                                          psCompInfo->status);
                    /* Stop the HCI Response Timer */
                    phOsalNfc_Timer_Stop( hci_resp_timer_id );
                }

#endif /* (NXP_NFC_HCI_TIMER == 1) */

                phHciNfc_Error_Sequence( psHciContext, pHwRef, 
                                                        psCompInfo->status, NULL, 0);
                break;
            }
        } /* End of Switch */
    } /* End of Context != NULL  */
}