Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2010 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 
     18 /*!
     19 * =========================================================================== *
     20 *                                                                             *
     21 *                                                                             *
     22 * \file  phHciNfc_Generic.c                                                   *
     23 * \brief Generic HCI Source for the HCI Management.                           *
     24 *                                                                             *
     25 *                                                                             *
     26 * Project: NFC-FRI-1.1                                                        *
     27 *                                                                             *
     28 * $Date: Tue Jun  8 09:31:49 2010 $                                           *
     29 * $Author: ing04880 $                                                         *
     30 * $Revision: 1.108 $                                                           *
     31 * $Aliases: NFC_FRI1.1_WK1023_R35_1 $
     32 *                                                                             *
     33 * =========================================================================== *
     34 */
     35 
     36 /*
     37 ################################################################################
     38 ***************************** Header File Inclusion ****************************
     39 ################################################################################
     40 */
     41 
     42 #include <phNfcCompId.h>
     43 #include <phHciNfc_Sequence.h>
     44 #include <phHciNfc_Pipe.h>
     45 #include <phHciNfc_AdminMgmt.h>
     46 #include <phHciNfc_IDMgmt.h>
     47 #include <phHciNfc_LinkMgmt.h>
     48 #include <phHciNfc_PollingLoop.h>
     49 #include <phHciNfc_RFReader.h>
     50 #include <phHciNfc_RFReaderA.h>
     51 #include <phOsalNfc.h>
     52 
     53 /*
     54 ################################################################################
     55 ****************************** Macro Definitions *******************************
     56 ################################################################################
     57 */
     58 
     59 
     60 /*
     61 ################################################################################
     62 ************************ Static Variable Definitions ***************************
     63 ################################################################################
     64 */
     65 
     66 
     67 #if  (NXP_NFC_HCI_TIMER == 1)
     68 
     69 #define NXP_HCI_RESPONSE_TIMEOUT  (NXP_NFC_HCI_TIMEOUT)
     70 
     71 #include <phOsalNfc_Timer.h>
     72 /** \internal HCI Response Timer to detect the
     73  * Stalled HCI Response */
     74 static uint32_t                    hci_resp_timer_id = NXP_INVALID_TIMER_ID;
     75 static phHciNfc_sContext_t        *gpsHciContext= NULL;
     76 
     77 #endif /* (NXP_NFC_HCI_TIMER == 1) */
     78 
     79 
     80 /*
     81 ################################################################################
     82 ************************* Function Prototype Declaration ***********************
     83 ################################################################################
     84 */
     85 
     86 #if  (NXP_NFC_HCI_TIMER == 1)
     87 
     88 static
     89 void
     90 phHciNfc_Response_Timeout (
     91                 uint32_t resp_timer_id
     92                 );
     93 
     94 #endif /* (NXP_NFC_HCI_TIMER == 1) */
     95 
     96 /**
     97  * \ingroup grp_hci_nfc
     98  *
     99  *  The phHciNfc_Send function sends the HCI Commands to the
    100  *  corresponding peripheral device, described by the HCI Context Structure.
    101  *
    102  *  \param[in]  psContext               psContext is the context of
    103  *                                      the HCI Layer.
    104  *  \param[in]  pHwRef                  pHwRef is the Information of
    105  *                                      the Device Interface Link .
    106  *  \param[in]  pdata                   Pointer to the buffer containing
    107  *                                      the command to be sent.
    108  *  \param[in] length                   Variable that receives
    109  *                                      the number of bytes actually sent.
    110  *
    111  *  \retval NFCSTATUS_PENDING           Command successfully sent.
    112  *  \retval NFCSTATUS_INVALID_PARAMETER One or more of the supplied parameters
    113  *                                      could not be interpreted properly.
    114  *  \retval Other errors                Errors related to the lower layers
    115  *
    116  */
    117 
    118 static
    119  NFCSTATUS
    120  phHciNfc_Send(
    121                     void                    *psContext,
    122                     void                    *pHwRef,
    123                     uint8_t                 *pdata,
    124 #ifdef ONE_BYTE_LEN
    125                     uint8_t                 length
    126 #else
    127                     uint16_t                length
    128 #endif
    129               );
    130 
    131  static
    132  NFCSTATUS
    133  phHciNfc_Process_HCP (
    134                                 phHciNfc_sContext_t     *psHciContext,
    135                                 void                    *pHwRef,
    136                                 void                    *pdata,
    137 #ifdef ONE_BYTE_LEN
    138                                 uint8_t                 length
    139 #else
    140                                 uint16_t                length
    141 #endif
    142                       );
    143 
    144 
    145 static
    146 NFCSTATUS
    147 phHciNfc_Process_Response (
    148                                 phHciNfc_sContext_t     *psHciContext,
    149                                 void                    *pHwRef,
    150                                 void                    *pdata,
    151 #ifdef ONE_BYTE_LEN
    152                                 uint8_t                 length
    153 #else
    154                                 uint16_t                length
    155 #endif
    156                          );
    157 
    158 static
    159 NFCSTATUS
    160 phHciNfc_Error_Response (
    161                                 phHciNfc_sContext_t     *psHciContext,
    162                                 void                    *pHwRef,
    163                                 void                    *pdata,
    164 #ifdef ONE_BYTE_LEN
    165                                 uint8_t                 length
    166 #else
    167                                 uint16_t                length
    168 #endif
    169                          );
    170 
    171 static
    172 NFCSTATUS
    173 phHciNfc_Process_Event (
    174                                 phHciNfc_sContext_t     *psHciContext,
    175                                 void                    *pHwRef,
    176                                 void                    *pdata,
    177 #ifdef ONE_BYTE_LEN
    178                                 uint8_t                 length
    179 #else
    180                                 uint16_t                length
    181 #endif
    182                          );
    183 
    184 
    185 static
    186 NFCSTATUS
    187 phHciNfc_Process_Command (
    188                                 phHciNfc_sContext_t     *psHciContext,
    189                                 void                    *pHwRef,
    190                                 void                    *pdata,
    191 #ifdef ONE_BYTE_LEN
    192                                 uint8_t                 length
    193 #else
    194                                 uint16_t                length
    195 #endif
    196                          );
    197 
    198 
    199 static
    200 void
    201 phHciNfc_Reset_Pipe_MsgInfo(
    202                             phHciNfc_Pipe_Info_t    *p_pipe_info
    203                         );
    204 
    205 static
    206 void
    207 phHciNfc_Build_HCPMessage(
    208                                 phHciNfc_HCP_Packet_t *hcp_packet,
    209                                 uint8_t             msg_type,
    210                                 uint8_t             instruction
    211                           );
    212 
    213 static
    214 void
    215 phHciNfc_Build_HCPHeader(
    216                                 phHciNfc_HCP_Packet_t *hcp_packet,
    217                                 uint8_t             chainbit,
    218                                 uint8_t             pipe_id
    219                           );
    220 /**
    221  * \ingroup grp_hci_nfc
    222  *
    223  *  The phHciNfc_Receive_HCP function receive the HCI Host Control Packet
    224  *  Frames from the device.
    225  *
    226  *  \param[in]  psHciContext            psHciContext is the context of
    227  *                                      the HCI Layer.
    228  *  \param[in]  pHwRef                  pHwRef is the Information of
    229  *                                      the Device Interface Link .
    230  *  \param[in] pdata                    Pointer to the response buffer that
    231  *                                      receives the response read.
    232  *  \param[in] length                   Variable that receives
    233  *                                      the number of bytes read.
    234  *
    235  *  \retval NFCSTATUS_PENDING           HCP Frame receive pending.
    236  *  \retval NFCSTATUS_INVALID_PARAMETER One or more of the supplied parameters
    237  *                                      could not be interpreted properly.
    238  *  \retval Other errors                Other related errors
    239  *
    240  *
    241  */
    242 
    243 
    244 static
    245 NFCSTATUS
    246 phHciNfc_Receive_HCP (
    247                             phHciNfc_sContext_t     *psHciContext,
    248                             void                    *pHwRef,
    249                             uint8_t                 *pdata,
    250 #ifdef ONE_BYTE_LEN
    251                             uint8_t                 length
    252 #else
    253                             uint16_t                length
    254 #endif
    255                      );
    256 
    257 
    258 /*
    259 ################################################################################
    260 ***************************** Function Definitions *****************************
    261 ################################################################################
    262 */
    263 
    264 
    265 #if  (NXP_NFC_HCI_TIMER == 1)
    266 
    267 static
    268 void
    269 phHciNfc_Response_Timeout (
    270                     uint32_t resp_timer_id
    271                 )
    272 {
    273     phNfc_sCompletionInfo_t  comp_info = {0};
    274 
    275     if ( ( NULL != gpsHciContext)
    276             && (resp_timer_id == hci_resp_timer_id ))
    277     {
    278         pphNfcIF_Notification_CB_t  p_upper_notify =
    279             gpsHciContext->p_upper_notify;
    280         void                        *p_upper_context =
    281                                 gpsHciContext->p_upper_context;
    282         phHal_sHwReference_t        *pHwRef = gpsHciContext->p_hw_ref;
    283 		uint32_t				i = 0;
    284 
    285 
    286         HCI_DEBUG(" HCI TIMEOUT: HCI Response Timeout Occurred in %X Timer\n"
    287                                                                  ,resp_timer_id);
    288         /* Stop the Response Timer */
    289         phOsalNfc_Timer_Stop( hci_resp_timer_id );
    290 
    291 		comp_info.status = PHNFCSTVAL(CID_NFC_HCI,
    292                         NFCSTATUS_BOARD_COMMUNICATION_ERROR);
    293         /* Roll Back to the Select State */
    294         phHciNfc_FSM_Rollback(gpsHciContext);
    295 
    296 		for(i=0;i < PHHCINFC_MAX_PIPE; i++)
    297 		{
    298 			phHciNfc_Reset_Pipe_MsgInfo(gpsHciContext->p_pipe_list[i]);
    299 		}
    300 
    301         /* Notify the Error/Success Scenario to the upper layer */
    302         phHciNfc_Notify( p_upper_notify, p_upper_context,
    303                     pHwRef, (uint8_t) NFC_NOTIFY_DEVICE_ERROR, &comp_info );
    304     }
    305 
    306     return ;
    307 
    308 }
    309 
    310 #endif /* (NXP_NFC_HCI_TIMER == 1) */
    311 
    312 
    313 
    314 /*!
    315  * \brief Allocation of the HCI Interface resources.
    316  *
    317  * This function releases and frees all the resources used by HCI Command and
    318  * Response Mechanism
    319  */
    320 
    321  NFCSTATUS
    322  phHciNfc_Allocate_Resource (
    323                                 void                **ppBuffer,
    324                                 uint16_t            size
    325                             )
    326 {
    327     NFCSTATUS           status = NFCSTATUS_SUCCESS;
    328 
    329     *ppBuffer = (void *) phOsalNfc_GetMemory(size);
    330     if( *ppBuffer != NULL )
    331     {
    332         (void )memset(((void *)*ppBuffer), 0,
    333                                     size);
    334     }
    335     else
    336     {
    337         *ppBuffer = NULL;
    338         status = PHNFCSTVAL(CID_NFC_HCI,
    339                         NFCSTATUS_INSUFFICIENT_RESOURCES);
    340     }
    341     return status;
    342 }
    343 
    344 
    345 
    346 /*!
    347  * \brief Release of the HCI Interface resources.
    348  *
    349  * This function releases and frees all the resources used by HCI Command and
    350  * Response Mechanism
    351  */
    352  void
    353  phHciNfc_Release_Resources (
    354                                 phHciNfc_sContext_t **ppsHciContext
    355                             )
    356 {
    357     uint8_t i = 0;
    358 
    359 
    360 #if  (NXP_NFC_HCI_TIMER == 1)
    361 
    362     if ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
    363     {
    364         /* Stop and Un-Intialise the Response Timer */
    365         phOsalNfc_Timer_Stop( hci_resp_timer_id );
    366         phOsalNfc_Timer_Delete( hci_resp_timer_id );
    367         HCI_DEBUG(" HCI : Timer %X Stopped and Released\n",
    368                                             hci_resp_timer_id);
    369         hci_resp_timer_id = NXP_INVALID_TIMER_ID;
    370     }
    371     gpsHciContext = NULL;
    372 
    373 #endif /* (NXP_NFC_HCI_TIMER == 1) */
    374 
    375 
    376     if(NULL != (*ppsHciContext)->p_admin_info)
    377     {
    378         phOsalNfc_FreeMemory((*ppsHciContext)->p_admin_info);
    379         (*ppsHciContext)->p_admin_info = NULL;
    380     }
    381     if(NULL !=(*ppsHciContext)->p_link_mgmt_info)
    382     {
    383         phOsalNfc_FreeMemory((*ppsHciContext)->p_link_mgmt_info);
    384         (*ppsHciContext)->p_link_mgmt_info = NULL;
    385     }
    386     if(NULL !=(*ppsHciContext)->p_identity_info)
    387     {
    388         phOsalNfc_FreeMemory((*ppsHciContext)->p_identity_info);
    389         (*ppsHciContext)->p_identity_info = NULL;
    390     }
    391     if(NULL !=(*ppsHciContext)->p_device_mgmt_info)
    392     {
    393         phOsalNfc_FreeMemory((*ppsHciContext)->p_device_mgmt_info);
    394         (*ppsHciContext)->p_device_mgmt_info = NULL;
    395     }
    396     if(NULL !=(*ppsHciContext)->p_reader_mgmt_info)
    397     {
    398         phOsalNfc_FreeMemory((*ppsHciContext)->p_reader_mgmt_info);
    399         (*ppsHciContext)->p_reader_mgmt_info = NULL;
    400     }
    401     if(NULL !=(*ppsHciContext)->p_poll_loop_info)
    402     {
    403         phOsalNfc_FreeMemory((*ppsHciContext)->p_poll_loop_info);
    404         (*ppsHciContext)->p_poll_loop_info = NULL;
    405     }
    406     if(NULL !=(*ppsHciContext)->p_reader_a_info)
    407     {
    408         phOsalNfc_FreeMemory((*ppsHciContext)->p_reader_a_info);
    409         (*ppsHciContext)->p_reader_a_info = NULL;
    410     }
    411 #ifdef TYPE_B
    412     if(NULL !=(*ppsHciContext)->p_reader_b_info)
    413     {
    414         phOsalNfc_FreeMemory((*ppsHciContext)->p_reader_b_info);
    415         (*ppsHciContext)->p_reader_b_info = NULL;
    416     }
    417 #endif
    418 #ifdef TYPE_FELICA
    419     if(NULL !=(*ppsHciContext)->p_felica_info)
    420     {
    421         phOsalNfc_FreeMemory((*ppsHciContext)->p_felica_info);
    422         (*ppsHciContext)->p_felica_info = NULL;
    423     }
    424 #endif
    425 #ifdef TYPE_JEWEL
    426     if(NULL !=(*ppsHciContext)->p_jewel_info)
    427     {
    428         phOsalNfc_FreeMemory((*ppsHciContext)->p_jewel_info);
    429         (*ppsHciContext)->p_jewel_info = NULL;
    430     }
    431 #endif
    432 #ifdef  TYPE_ISO15693
    433     if(NULL !=(*ppsHciContext)->p_iso_15693_info)
    434     {
    435         phOsalNfc_FreeMemory((*ppsHciContext)->p_iso_15693_info);
    436         (*ppsHciContext)->p_iso_15693_info = NULL;
    437     }
    438 #endif /* #ifdef    TYPE_ISO15693 */
    439 #ifdef ENABLE_P2P
    440     if(NULL !=(*ppsHciContext)->p_nfcip_info)
    441     {
    442         phOsalNfc_FreeMemory((*ppsHciContext)->p_nfcip_info);
    443         (*ppsHciContext)->p_nfcip_info = NULL;
    444     }
    445 #endif
    446     if(NULL !=(*ppsHciContext)->p_emulation_mgmt_info)
    447     {
    448         phOsalNfc_FreeMemory((*ppsHciContext)->p_emulation_mgmt_info);
    449         (*ppsHciContext)->p_emulation_mgmt_info = NULL;
    450     }
    451     if(NULL !=(*ppsHciContext)->p_wi_info)
    452     {
    453         phOsalNfc_FreeMemory((*ppsHciContext)->p_wi_info);
    454         (*ppsHciContext)->p_wi_info = NULL;
    455     }
    456     if(NULL !=(*ppsHciContext)->p_swp_info)
    457     {
    458         phOsalNfc_FreeMemory((*ppsHciContext)->p_swp_info);
    459         (*ppsHciContext)->p_swp_info = NULL;
    460     }
    461     if(NULL !=(*ppsHciContext)->p_uicc_info)
    462     {
    463         phOsalNfc_FreeMemory((*ppsHciContext)->p_uicc_info);
    464         (*ppsHciContext)->p_uicc_info = NULL;
    465     }
    466 #ifdef HOST_EMULATION
    467     if(NULL !=(*ppsHciContext)->p_ce_a_info)
    468     {
    469         phOsalNfc_FreeMemory((*ppsHciContext)->p_ce_a_info);
    470         (*ppsHciContext)->p_ce_a_info = NULL;
    471     }
    472     if(NULL !=(*ppsHciContext)->p_ce_b_info)
    473     {
    474         phOsalNfc_FreeMemory((*ppsHciContext)->p_ce_b_info);
    475         (*ppsHciContext)->p_ce_b_info = NULL;
    476     }
    477 #endif
    478 
    479     for(i=0;i < PHHCINFC_MAX_PIPE; i++)
    480     {
    481         if(NULL != (*ppsHciContext)->p_pipe_list[i])
    482         {
    483             phOsalNfc_FreeMemory((*ppsHciContext)->p_pipe_list[i]);
    484         }
    485     }
    486 
    487 
    488     phOsalNfc_FreeMemory((*ppsHciContext));
    489     (*ppsHciContext) = NULL;
    490 
    491     return ;
    492 }
    493 
    494 static
    495 void
    496 phHciNfc_Reset_Pipe_MsgInfo(
    497                             phHciNfc_Pipe_Info_t    *p_pipe_info
    498                         )
    499 {
    500     if (p_pipe_info != NULL)
    501     {
    502         p_pipe_info->sent_msg_type = HCP_MSG_TYPE_RESERVED;
    503         p_pipe_info->prev_msg = MSG_INSTRUCTION_UNKNWON;
    504         p_pipe_info->prev_status = NFCSTATUS_INVALID_HCI_INSTRUCTION;
    505         p_pipe_info->param_info = NULL;
    506         p_pipe_info->param_length = FALSE ;
    507     }
    508     return;
    509 }
    510 
    511 
    512 void
    513 phHciNfc_Release_Lower(
    514                     phHciNfc_sContext_t         *psHciContext,
    515                     void                        *pHwRef
    516                )
    517 {
    518     phNfc_sLowerIF_t            *plower_if =
    519                                     &(psHciContext->lower_interface);
    520     NFCSTATUS            status = NFCSTATUS_SUCCESS;
    521 
    522     PHNFC_UNUSED_VARIABLE(status);
    523     if(NULL != plower_if->release)
    524     {
    525         status = plower_if->release((void *)plower_if->pcontext,
    526                                         (void *)pHwRef);
    527         (void) memset((void *)plower_if, 0, sizeof(phNfc_sLowerIF_t));
    528         HCI_DEBUG(" HCI Releasing the Lower Layer Resources: Status = %02X\n"
    529                                                                     ,status);
    530     }
    531 
    532     return;
    533 }
    534 
    535 
    536 
    537 /*!
    538  * \brief Sends the HCI Commands to the corresponding peripheral device.
    539  *
    540  * This function sends the HCI Commands to the connected NFC Pheripheral device
    541  */
    542  static
    543  NFCSTATUS
    544  phHciNfc_Send (
    545                       void                  *psContext,
    546                       void                  *pHwRef,
    547                       uint8_t               *pdata,
    548 #ifdef ONE_BYTE_LEN
    549                       uint8_t               length
    550 #else
    551                       uint16_t              length
    552 #endif
    553                      )
    554 {
    555     phHciNfc_sContext_t     *psHciContext= (phHciNfc_sContext_t  *)psContext;
    556     NFCSTATUS               status = NFCSTATUS_SUCCESS;
    557 
    558     phNfc_sLowerIF_t        *plower_if = &(psHciContext->lower_interface);
    559 
    560     if( (NULL != plower_if)
    561         && (NULL != plower_if->send)
    562       )
    563     {
    564         HCI_DEBUG("HCI: In Function: %s \n", __FUNCTION__);
    565         HCI_DEBUG("HCI: Response Pending status --> %s \n",
    566             (psHciContext->response_pending)?"TRUE":"FALSE");
    567         HCI_PRINT_BUFFER("Send Buffer",pdata,length);
    568         /* psHciContext->hci_transact_state = NFC_TRANSACT_SEND_IN_PROGRESS; */
    569 
    570 #if  (NXP_NFC_HCI_TIMER == 1)
    571 
    572     if (
    573         (TRUE != psHciContext->tx_hcp_chaining)
    574         &&  (TRUE == psHciContext->response_pending)
    575         && ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
    576        )
    577     {
    578         /* Start the HCI Response Timer */
    579         phOsalNfc_Timer_Start( hci_resp_timer_id,
    580                 NXP_HCI_RESPONSE_TIMEOUT, phHciNfc_Response_Timeout, NULL);
    581         HCI_DEBUG(" HCI : Timer %X Started \n", hci_resp_timer_id);
    582     }
    583 
    584 #endif /* (NXP_NFC_HCI_TIMER == 1) */
    585 
    586         status = plower_if->send((void *)plower_if->pcontext,
    587                                 (void *)pHwRef, pdata, length);
    588     }
    589 
    590     return status;
    591 }
    592 
    593 
    594 /*!
    595  * \brief Receives the HCI Response from the corresponding peripheral device.
    596  *
    597  * This function receives the HCI Command Response to the connected NFC
    598  * Pheripheral device.
    599  */
    600 
    601 NFCSTATUS
    602 phHciNfc_Receive(
    603                         void                *psContext,
    604                         void                *pHwRef,
    605                         uint8_t             *pdata,
    606 #ifdef ONE_BYTE_LEN
    607                         uint8_t             length
    608 #else
    609                         uint16_t            length
    610 #endif
    611                     )
    612 {
    613     phHciNfc_sContext_t     *psHciContext= (phHciNfc_sContext_t  *)psContext;
    614     phNfc_sLowerIF_t *plower_if = NULL ;
    615     NFCSTATUS         status = NFCSTATUS_SUCCESS;
    616 
    617     if(NULL == psHciContext )
    618     {
    619         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    620     }
    621     else
    622     {
    623         plower_if = &(psHciContext->lower_interface);
    624 
    625         if( (NULL != plower_if)
    626             && (NULL != plower_if->receive)
    627           )
    628         {
    629             /* psHciContext->hci_transact_state = NFC_TRANSACT_RECV_IN_PROGRESS; */
    630             status = plower_if->receive((void *)plower_if->pcontext,
    631                                     (void *)pHwRef, pdata, length);
    632         }
    633     }
    634     return status;
    635 }
    636 
    637 
    638 /*!
    639  * \brief Sends the HCP Packet to the lower link layer .
    640  *
    641  * This function Sends the HCI Data in the HCP packet format to the below
    642  * Link layer.
    643  */
    644 
    645  NFCSTATUS
    646  phHciNfc_Send_HCP (
    647                                 phHciNfc_sContext_t *psHciContext,
    648                                 void                *pHwRef
    649                    )
    650 {
    651     NFCSTATUS               status = NFCSTATUS_SUCCESS;
    652     phHciNfc_HCP_Packet_t   *tx_data = (phHciNfc_HCP_Packet_t *)
    653                                     psHciContext->send_buffer;
    654     /* Skip the HCP Header Byte initially */
    655     uint16_t                tx_length = psHciContext->tx_total - 1 ;
    656     uint16_t                hcp_index = HCP_ZERO_LEN;
    657     uint8_t                 pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
    658     static  uint8_t         chain_bit = HCP_CHAINBIT_DEFAULT;
    659 
    660     pipe_id =  (uint8_t) GET_BITS8( tx_data->hcp_header,
    661         HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
    662 
    663     /* Fragmentation of the HCP Frames */
    664     if ( tx_length > PHHCINFC_MAX_PACKET_DATA )
    665     {
    666         tx_data = &psHciContext->tx_packet;
    667         (void)memset((void *)tx_data, FALSE,
    668                         sizeof(phHciNfc_HCP_Packet_t));
    669         if (HCP_CHAINBIT_DEFAULT == chain_bit)
    670         {
    671             /* HCI Chaining Needs to be Done */
    672             psHciContext->tx_remain = tx_length;
    673             psHciContext->tx_hcp_frgmnt_index = HCP_ZERO_LEN ;
    674             chain_bit = HCP_CHAINBIT_BEGIN;
    675             /* Increment the Fragment index to skip the HCP Header */
    676             psHciContext->tx_hcp_frgmnt_index++;
    677             psHciContext->tx_hcp_chaining = TRUE ;
    678             tx_length = PHHCINFC_MAX_PACKET_DATA ;
    679         }
    680         else if ( psHciContext->tx_remain > PHHCINFC_MAX_PACKET_DATA )
    681         {
    682 			/* Intermediate Chained HCI Frames */
    683             tx_length = PHHCINFC_MAX_PACKET_DATA ;
    684         }
    685         else
    686         {
    687             /* End of Chaining Reached */
    688             chain_bit = HCP_CHAINBIT_END;
    689             tx_length = psHciContext->tx_remain ;
    690             psHciContext->tx_hcp_chaining = FALSE ;
    691         }
    692 
    693         /* Build the HCP Header to have Chaining Enabled */
    694         phHciNfc_Build_HCPHeader(tx_data, chain_bit , pipe_id );
    695 
    696         phHciNfc_Append_HCPFrame((uint8_t *)tx_data->msg.payload, hcp_index,
    697             (&psHciContext->send_buffer[psHciContext->tx_hcp_frgmnt_index])
    698             , tx_length );
    699     }
    700     else
    701     {
    702         /* No Chaining Required */
    703         chain_bit = HCP_CHAINBIT_DEFAULT;
    704 
    705         psHciContext->tx_hcp_chaining = FALSE ;
    706 
    707         psHciContext->tx_remain = tx_length ;
    708     }
    709 
    710     /* Include the Skipped HCP Header Byte */
    711     tx_length++;
    712 
    713     status = phHciNfc_Send ( (void *) psHciContext, pHwRef,
    714                         (uint8_t *)tx_data, tx_length );
    715 
    716     return status;
    717 }
    718 
    719 
    720 /*!
    721  * \brief Receives the HCP Packet from the lower link layer .
    722  *
    723  * This function receives the HCI Data in the HCP packet format from the below
    724  * Link layer.
    725  */
    726  static
    727  NFCSTATUS
    728  phHciNfc_Receive_HCP (
    729                                 phHciNfc_sContext_t *psHciContext,
    730                                 void                *pHwRef,
    731                                 uint8_t             *pdata,
    732 #ifdef ONE_BYTE_LEN
    733                                 uint8_t             length
    734 #else
    735                                 uint16_t            length
    736 #endif
    737                    )
    738 {
    739     NFCSTATUS               status = NFCSTATUS_SUCCESS;
    740     phHciNfc_HCP_Packet_t   *packet = NULL;
    741     uint8_t                 chainbit = HCP_CHAINBIT_DEFAULT;
    742     uint16_t                hcp_index = 0;
    743 
    744     packet = (phHciNfc_HCP_Packet_t *)pdata;
    745     chainbit = (uint8_t) GET_BITS8( packet->hcp_header,
    746         HCP_CHAINBIT_OFFSET, HCP_CHAINBIT_LEN);
    747     hcp_index = psHciContext->rx_hcp_frgmnt_index;
    748     HCI_PRINT_BUFFER("Receive Buffer",((uint8_t *)pdata),length);
    749     if (HCP_CHAINBIT_BEGIN == chainbit)
    750     {
    751         /* pdata = (uint8_t *)&psHciContext->rx_packet; */
    752         /* De Fragmentation of the Received HCP Frames */
    753         /* Subsequent Chaining Frames */
    754         if( hcp_index  > 0 )
    755         {
    756             /* Copy the obtained fragment and receive the next fragment */
    757             phHciNfc_Append_HCPFrame(
    758                 psHciContext->recv_buffer, hcp_index,
    759                     (uint8_t *)&pdata[HCP_MESSAGE_LEN],
    760                             (length - HCP_MESSAGE_LEN) );
    761             psHciContext->rx_hcp_frgmnt_index =(uint16_t)
    762                         (hcp_index + length - HCP_MESSAGE_LEN);
    763         }
    764         /* First Chaining Frame*/
    765         else
    766         {
    767             psHciContext->rx_hcp_chaining = TRUE ;
    768             /* Copy the obtained fragment and receive the next fragment */
    769             phHciNfc_Append_HCPFrame(psHciContext->recv_buffer,
    770                 hcp_index, pdata, length);
    771             psHciContext->rx_hcp_frgmnt_index = ( hcp_index + length ) ;
    772 
    773         }
    774         status = phHciNfc_Receive ( (void *) psHciContext, pHwRef,
    775                                                 pdata, length);
    776     }
    777     else
    778     {
    779         if(TRUE == psHciContext->rx_hcp_chaining)
    780         {
    781             /* If the chaining was done earlier */
    782             psHciContext->rx_hcp_chaining = FALSE ;
    783             /* Copy the Remaining buffer to the RX_BUFFER */
    784             phHciNfc_Append_HCPFrame(
    785                 psHciContext->recv_buffer, hcp_index,
    786                     (uint8_t *)&pdata[HCP_MESSAGE_LEN],
    787                             (length - HCP_MESSAGE_LEN) );
    788             /* If there is chaining done the return the same data */
    789             psHciContext->rx_total =
    790                         (hcp_index + length - HCP_MESSAGE_LEN);
    791             psHciContext->rx_hcp_frgmnt_index = FALSE ;
    792         }
    793         else
    794         {
    795             (void) memcpy( psHciContext->recv_buffer, pdata, length);
    796             /* If there is no chaining done then return the same data */
    797             psHciContext->rx_total = (hcp_index + length);
    798 
    799         }
    800     }
    801 
    802     return status;
    803 }
    804 
    805 
    806 /*!
    807  * \brief Receives the HCP Packet from the lower link layer .
    808  *
    809  * This function receives the HCI Data in the HCP packet format from the below
    810  * Link layer.
    811  */
    812 
    813  static
    814  NFCSTATUS
    815  phHciNfc_Process_HCP (
    816                                 phHciNfc_sContext_t     *psHciContext,
    817                                 void                    *pHwRef,
    818                                 void                    *pdata,
    819 #ifdef ONE_BYTE_LEN
    820                                 uint8_t             length
    821 #else
    822                                 uint16_t            length
    823 #endif
    824                       )
    825 {
    826     phHciNfc_HCP_Packet_t   *packet = NULL;
    827     phHciNfc_HCP_Message_t  *message = NULL;
    828     NFCSTATUS               status = NFCSTATUS_SUCCESS;
    829     uint8_t                 msg_type = 0;
    830 
    831     if( (NULL == pdata)
    832         || ( length < HCP_HEADER_LEN )
    833       )
    834     {
    835         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    836     }
    837     else
    838     {
    839         status = phHciNfc_Receive_HCP( psHciContext, pHwRef, pdata, length );
    840     }/* End of the Valid Data Handling */
    841 
    842     if( NFCSTATUS_SUCCESS  == status )
    843     {
    844         packet = (phHciNfc_HCP_Packet_t *)psHciContext->recv_buffer;
    845         length =
    846 #ifdef ONE_BYTE_LEN
    847             (uint8_t)
    848 #endif
    849             psHciContext->rx_total ;
    850         message = &packet->msg.message;
    851         /* HCI_PRINT_BUFFER("Total Receive Buffer",((uint8_t *)pdata),length); */
    852         msg_type = (uint8_t) GET_BITS8( message->msg_header,
    853             HCP_MSG_TYPE_OFFSET, HCP_MSG_TYPE_LEN);
    854         switch ( msg_type )
    855         {
    856             case HCP_MSG_TYPE_RESPONSE:
    857             {
    858                 status = phHciNfc_Process_Response( psHciContext,
    859                                                 pHwRef, (void *)packet, length );
    860                 break;
    861             }
    862             case HCP_MSG_TYPE_EVENT:
    863             {
    864                 status = phHciNfc_Process_Event( psHciContext,
    865                                                 pHwRef,(void *)packet, length );
    866                 break;
    867             }
    868             case HCP_MSG_TYPE_COMMAND:
    869             {
    870 
    871                 status = phHciNfc_Process_Command( psHciContext,
    872                                                 pHwRef, (void *)packet, length );
    873                 break;
    874             }
    875             /* case HCP_MSG_TYPE_RESERVED: */
    876             default:
    877             {
    878                 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
    879                 break;
    880             }
    881         }
    882     }/* End of Receive HCP Status */
    883     return status;
    884 }
    885 
    886 
    887  static
    888  NFCSTATUS
    889  phHciNfc_Process_Response (
    890                                  phHciNfc_sContext_t    *psHciContext,
    891                                  void                   *pHwRef,
    892                                  void                   *pdata,
    893 #ifdef ONE_BYTE_LEN
    894                                  uint8_t             length
    895 #else
    896                                  uint16_t            length
    897 #endif
    898                              )
    899 {
    900     phHciNfc_HCP_Packet_t   *packet = NULL;
    901     phHciNfc_HCP_Message_t  *message = NULL;
    902     uint8_t                 instruction=0;
    903     uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
    904     phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
    905 
    906     NFCSTATUS               status = NFCSTATUS_SUCCESS;
    907 
    908     packet = (phHciNfc_HCP_Packet_t *)pdata;
    909     message = &packet->msg.message;
    910     /* Get the instruction bits from the Message Header */
    911     instruction = (uint8_t) GET_BITS8( message->msg_header,
    912                                 HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
    913     /* Get the Pipe ID from the HCP Header */
    914     pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
    915                                 HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
    916 
    917 #if  (NXP_NFC_HCI_TIMER == 1)
    918 
    919     if ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
    920     {
    921         /* Stop the HCI Response Timer */
    922         HCI_DEBUG(" HCI : Timer %X Stopped \n", hci_resp_timer_id);
    923         phOsalNfc_Timer_Stop( hci_resp_timer_id );
    924     }
    925 
    926 #endif /* (NXP_NFC_HCI_TIMER == 1) */
    927 
    928     if (pipe_id >=  PHHCINFC_MAX_PIPE )
    929     {
    930         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
    931     }
    932     else if( ((uint8_t) ANY_OK != instruction)
    933         && ( (pipe_id !=    PIPETYPE_STATIC_ADMIN )
    934         && ( ADM_CLEAR_ALL_PIPE != (psHciContext->p_pipe_list[pipe_id])->prev_msg ))
    935         )
    936     {
    937         status = phHciNfc_Error_Response( psHciContext, pHwRef, pdata, length );
    938     }
    939     else
    940     {
    941         p_pipe_info = psHciContext->p_pipe_list[pipe_id];
    942         if( ( NULL != p_pipe_info )
    943             &&   ( HCP_MSG_TYPE_COMMAND == p_pipe_info->sent_msg_type  )
    944             &&   ( NULL != p_pipe_info->recv_resp )
    945         )
    946         {
    947             status = psHciContext->p_pipe_list[pipe_id]->recv_resp( psHciContext,
    948                                                             pHwRef, pdata, length );
    949         }
    950         else
    951         {
    952             status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
    953         }
    954         /* There is no Pending Response */
    955         psHciContext->response_pending = FALSE ;
    956         HCI_DEBUG("HCI: Response Pending status --> FALSE, %s \n",
    957             __FUNCTION__);
    958         if( NFCSTATUS_SUCCESS == status )
    959         {
    960             phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]);
    961             status = phHciNfc_Resume_Sequence(psHciContext, pHwRef);
    962 
    963         }/* End of Success Status validation */
    964         else
    965         {
    966             HCI_DEBUG("HCI: Status --> %X \n", status );
    967         }
    968 
    969     } /* End of the Valid Response handling */
    970     return status;
    971 }
    972 
    973 
    974 static
    975  NFCSTATUS
    976  phHciNfc_Error_Response (
    977                                 phHciNfc_sContext_t     *psHciContext,
    978                                 void                    *pHwRef,
    979                                 void                    *pdata,
    980 #ifdef ONE_BYTE_LEN
    981                                 uint8_t                 length
    982 #else
    983                                 uint16_t                length
    984 #endif
    985                          )
    986 {
    987 
    988     phHciNfc_HCP_Packet_t   *packet = (phHciNfc_HCP_Packet_t *)pdata;
    989     phHciNfc_HCP_Message_t  *message = &packet->msg.message;
    990     NFCSTATUS               status = NFCSTATUS_SUCCESS;
    991     uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
    992 #if defined(HCI_TRACE) || defined (ERROR_INSTRUCTION)
    993     uint8_t                 instruction = 0;
    994     instruction = (uint8_t) GET_BITS8(message->msg_header,
    995                             HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
    996 #endif
    997 
    998     /* Get the Pipe ID from the HCP Header */
    999     pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
   1000                                 HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
   1001     /* Process the Error Response based on the obtained instruction */
   1002 #ifdef ERROR_INSTRUCTION
   1003     switch(instruction)
   1004     {
   1005         case ANY_E_NOT_CONNECTED:
   1006         case ANY_E_CMD_PAR_UNKNOWN:
   1007         case ANY_E_NOK:
   1008         case ANY_E_PIPES_FULL:
   1009         case ANY_E_REG_PAR_UNKNOWN:
   1010         case ANY_E_PIPE_NOT_OPENED:
   1011         case ANY_E_CMD_NOT_SUPPORTED:
   1012         case ANY_E_TIMEOUT:
   1013         case ANY_E_REG_ACCESS_DENIED:
   1014         case ANY_E_PIPE_ACCESS_DENIED:
   1015         {
   1016             /* Receive Error Notification to the Upper Layer */
   1017             status = PHNFCSTVAL( CID_NFC_HCI, \
   1018                             message->msg_header);
   1019             phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, length );
   1020             /* Return Success as the Error Sequence is already handled */
   1021             psHciContext->response_pending = FALSE ;
   1022             HCI_DEBUG("HCI: Response Pending status --> FALSE, %s \n",
   1023             __FUNCTION__);
   1024             status = NFCSTATUS_SUCCESS;
   1025             break;
   1026         }
   1027             /* The Statement should not reach this case */
   1028         /* case ANY_OK: */
   1029         default:
   1030         {
   1031             /* status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE); */
   1032             break;
   1033         }
   1034     }
   1035 #else
   1036     status = PHNFCSTVAL( CID_NFC_HCI, message->msg_header);
   1037     HCI_DEBUG("HCI Error Response(%u) from the Device \n", instruction);
   1038     psHciContext->response_pending = FALSE ;
   1039     HCI_DEBUG("HCI: Response Pending status --> FALSE, %s \n",
   1040         __FUNCTION__);
   1041     phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]);
   1042     phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, (uint8_t) length );
   1043     /* Return Success as the Error Sequence is already handled */
   1044     status = NFCSTATUS_SUCCESS;
   1045 #endif
   1046 
   1047     return status;
   1048 }
   1049 
   1050 
   1051 static
   1052  NFCSTATUS
   1053  phHciNfc_Process_Event (
   1054                                 phHciNfc_sContext_t     *psHciContext,
   1055                                 void                    *pHwRef,
   1056                                 void                    *pdata,
   1057 #ifdef ONE_BYTE_LEN
   1058                                 uint8_t                 length
   1059 #else
   1060                                 uint16_t                length
   1061 #endif
   1062                         )
   1063 {
   1064     phHciNfc_HCP_Packet_t   *packet = NULL;
   1065     phHciNfc_HCP_Message_t  *message = NULL;
   1066     phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
   1067     uint8_t                 instruction=0;
   1068     uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
   1069 
   1070     NFCSTATUS               status = NFCSTATUS_SUCCESS;
   1071 
   1072     packet = (phHciNfc_HCP_Packet_t *)pdata;
   1073     message = &packet->msg.message;
   1074     /* Get the instruction bits from the Message Header */
   1075     PHNFC_UNUSED_VARIABLE(instruction);
   1076     instruction = (uint8_t) GET_BITS8( message->msg_header,
   1077                                 HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
   1078     /* Get the Pipe ID from the HCP Header */
   1079     pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
   1080                                 HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
   1081 
   1082     if (pipe_id >=  PHHCINFC_MAX_PIPE )
   1083     {
   1084         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
   1085     }
   1086     else
   1087     {
   1088         p_pipe_info = psHciContext->p_pipe_list[pipe_id];
   1089     }
   1090 
   1091     if( (p_pipe_info != NULL ) )
   1092     {
   1093         if( NULL != p_pipe_info->recv_event)
   1094         {
   1095             status = p_pipe_info->recv_event( psHciContext, pHwRef,
   1096                                                         pdata, length );
   1097         }
   1098         else
   1099         {
   1100             HCI_DEBUG(" Event Handling Not Supported by the #%u Pipe \n",
   1101                                                         pipe_id);
   1102             status = PHNFCSTVAL(CID_NFC_HCI,
   1103                                         NFCSTATUS_FEATURE_NOT_SUPPORTED);
   1104         }
   1105     }
   1106     else
   1107     {
   1108         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
   1109     }
   1110 
   1111     HCI_DEBUG("HCI: In Function: %s \n",
   1112         __FUNCTION__);
   1113     HCI_DEBUG("HCI: Response Pending status --> %s \n",
   1114         (psHciContext->response_pending)?"TRUE":"FALSE");
   1115     HCI_DEBUG("HCI: Event Pending status --> %s \n",
   1116         (psHciContext->event_pending)?"TRUE":"FALSE");
   1117 
   1118         if ((TRUE == psHciContext->response_pending)
   1119         || (TRUE == psHciContext->event_pending))
   1120     {
   1121         (void)memset(psHciContext->recv_buffer,
   1122             FALSE, PHHCINFC_MAX_BUFFERSIZE);
   1123         (void)memset((void *)&psHciContext->rx_packet,
   1124             FALSE, sizeof(phHciNfc_HCP_Packet_t));
   1125 
   1126         /* Reset the Received Data Index */
   1127         psHciContext->rx_index = ZERO;
   1128         /* Reset the size of the total response data received */
   1129         psHciContext->rx_total = ZERO;
   1130 
   1131         /* psHciContext->hci_transact_state = NFC_TRANSACT_SEND_COMPLETE;*/
   1132         /* Receive the Response Packet */
   1133 
   1134         status = phHciNfc_Receive( psHciContext, pHwRef,
   1135                     (uint8_t *)(&psHciContext->rx_packet),
   1136                     sizeof(phHciNfc_HCP_Packet_t) );
   1137 
   1138         /* HCI_DEBUG("HCI Lower Layer Send Completion After Receive,\
   1139         Status = %02X\n",status); */
   1140     }
   1141     else
   1142     {
   1143         if(
   1144 /* #define EVENT_NOTIFY */
   1145 #ifndef EVENT_NOTIFY
   1146             ( NFCSTATUS_SUCCESS == status  )
   1147             || ( NFCSTATUS_RF_TIMEOUT == status  )
   1148             || ( NFCSTATUS_MORE_INFORMATION == status  )
   1149 #else
   1150             (FALSE == psHciContext->event_pending )
   1151 #endif
   1152             && ( pipe_id <= PHHCINFC_MAX_PIPE )
   1153             )
   1154         {
   1155             /* phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]); */
   1156             status = phHciNfc_Resume_Sequence(psHciContext, pHwRef);
   1157 
   1158         }/* End of Success Status validation */
   1159         else
   1160         {
   1161             HCI_DEBUG(" HCI: Pipe-ID --> %02X  \n", pipe_id);
   1162             HCI_DEBUG(" HCI: PROCESS EVENT - Pending/Invalid Status : %X\n", status);
   1163         }
   1164     }
   1165 
   1166     return status;
   1167 }
   1168 
   1169 static
   1170  NFCSTATUS
   1171  phHciNfc_Process_Command (
   1172                                 phHciNfc_sContext_t     *psHciContext,
   1173                                 void                    *pHwRef,
   1174                                 void                    *pdata,
   1175 #ifdef ONE_BYTE_LEN
   1176                                 uint8_t                 length
   1177 #else
   1178                                 uint16_t                length
   1179 #endif
   1180                              )
   1181 {
   1182     phHciNfc_HCP_Packet_t   *packet = NULL;
   1183     phHciNfc_HCP_Message_t  *message = NULL;
   1184     phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
   1185     uint8_t                 instruction=0;
   1186     uint8_t                 pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
   1187 
   1188     NFCSTATUS               status = NFCSTATUS_SUCCESS;
   1189 
   1190     packet = (phHciNfc_HCP_Packet_t *)pdata;
   1191     message = &packet->msg.message;
   1192     /* Get the instruction bits from the Message Header */
   1193     PHNFC_UNUSED_VARIABLE(instruction);
   1194 
   1195     instruction = (uint8_t) GET_BITS8( message->msg_header,
   1196                                 HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
   1197     /* Get the Pipe ID from the HCP Header */
   1198     pipe_id =  (uint8_t) GET_BITS8( packet->hcp_header,
   1199                                 HCP_PIPEID_OFFSET, HCP_PIPEID_LEN);
   1200     if (pipe_id >=  PHHCINFC_MAX_PIPE )
   1201     {
   1202         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
   1203     }
   1204     else
   1205     {
   1206         p_pipe_info = psHciContext->p_pipe_list[pipe_id];
   1207     }
   1208 
   1209     if( (p_pipe_info != NULL )
   1210         )
   1211     {
   1212         if( NULL != p_pipe_info->recv_cmd)
   1213         {
   1214             status = p_pipe_info->recv_cmd( psHciContext,   pHwRef,
   1215                                                         pdata, length );
   1216         }
   1217         else
   1218         {
   1219             HCI_DEBUG(" Command Handling Not Supported by the #%u Pipe \n",
   1220                                                         pipe_id);
   1221             status = PHNFCSTVAL(CID_NFC_HCI,
   1222                                         NFCSTATUS_FEATURE_NOT_SUPPORTED);
   1223         }
   1224     }
   1225     else
   1226     {
   1227         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
   1228     }
   1229 
   1230     HCI_DEBUG("HCI: In Function: %s \n", __FUNCTION__);
   1231     HCI_DEBUG("HCI: Response Pending status --> %s \n",
   1232         (psHciContext->response_pending)?"TRUE":"FALSE");
   1233 
   1234     if(( NFCSTATUS_SUCCESS == status )
   1235         && (TRUE != psHciContext->response_pending)
   1236         )
   1237     {
   1238         /* Reset the Pipe Information Stored in the particular Pipe */
   1239         /* phHciNfc_Reset_Pipe_MsgInfo(psHciContext->p_pipe_list[pipe_id]); */
   1240         /* Resume the Execution Sequence */
   1241         status = phHciNfc_Resume_Sequence(psHciContext, pHwRef);
   1242 
   1243     }/* End of Success Status validation */
   1244 
   1245     return status;
   1246 }
   1247 
   1248 
   1249 static
   1250 void
   1251 phHciNfc_Build_HCPMessage(
   1252                                 phHciNfc_HCP_Packet_t *hcp_packet,
   1253                                 uint8_t             msg_type,
   1254                                 uint8_t             instruction
   1255                           )
   1256 {
   1257     phHciNfc_HCP_Message_t  *hcp_message = NULL;
   1258 
   1259     hcp_message = &(hcp_packet->msg.message);
   1260     /* Set the type to the provided message type in the HCP Message Header */
   1261     hcp_message->msg_header = (uint8_t) SET_BITS8(hcp_message->msg_header,HCP_MSG_TYPE_OFFSET,
   1262                 HCP_MSG_TYPE_LEN, msg_type);
   1263     /* Set the instruction to the kind of instruction in the HCP Message Header */
   1264     hcp_message->msg_header  = (uint8_t) SET_BITS8(hcp_message->msg_header,HCP_MSG_INSTRUCTION_OFFSET,
   1265                 HCP_MSG_INSTRUCTION_LEN, instruction);
   1266     /* hcp_message->msg_header = hcp_message->msg_header | temp ; */
   1267 
   1268 }
   1269 
   1270 
   1271 static
   1272 void
   1273 phHciNfc_Build_HCPHeader(
   1274                                 phHciNfc_HCP_Packet_t *hcp_packet,
   1275                                 uint8_t             chainbit,
   1276                                 uint8_t             pipe_id
   1277                           )
   1278 {
   1279     /* Set the Chaining bit to the default type */
   1280     hcp_packet->hcp_header = (uint8_t) SET_BITS8(hcp_packet->hcp_header,
   1281                 HCP_CHAINBIT_OFFSET, HCP_CHAINBIT_LEN, chainbit);
   1282     /* Populate the Pipe ID to the HCP Header */
   1283     hcp_packet->hcp_header  = (uint8_t) SET_BITS8(hcp_packet->hcp_header,HCP_PIPEID_OFFSET,
   1284                 HCP_PIPEID_LEN, pipe_id);
   1285 
   1286 }
   1287 
   1288 /*!
   1289  * \brief Builds the HCP Frame Packet.
   1290  *
   1291  * This function builds the HCP Frame in the HCP packet format to send to the
   1292  * connected reader device.
   1293  */
   1294 
   1295 void
   1296  phHciNfc_Build_HCPFrame (
   1297                                 phHciNfc_HCP_Packet_t *hcp_packet,
   1298                                 uint8_t             chainbit,
   1299                                 uint8_t             pipe_id,
   1300                                 uint8_t             msg_type,
   1301                                 uint8_t             instruction
   1302                           )
   1303 {
   1304     /* Fills the HCP Header in the packet */
   1305     phHciNfc_Build_HCPHeader( hcp_packet,chainbit,pipe_id );
   1306     /* Fills the HCP Message in the packet */
   1307     phHciNfc_Build_HCPMessage( hcp_packet,msg_type,instruction );
   1308 }
   1309 
   1310 /*!
   1311  * \brief Appends the HCP Frame Packet.
   1312  *
   1313  * This function Appends the HCP Frame of the HCP packet to complete the
   1314  * entire HCP Packet.
   1315  */
   1316 
   1317 void
   1318  phHciNfc_Append_HCPFrame (
   1319 /*                              phHciNfc_sContext_t     *psHciContext, */
   1320                                 uint8_t                 *hcp_data,
   1321                                 uint16_t                hcp_index,
   1322                                 uint8_t                 *src_data,
   1323                                 uint16_t                src_len
   1324                           )
   1325 {
   1326     uint16_t src_index = 0;
   1327     if( (NULL != src_data)
   1328         /* && (hcp_index >= 0) */
   1329         && (src_len > 0)
   1330         )
   1331     {
   1332         for(src_index=0; src_index < src_len ; src_index++)
   1333         {
   1334             hcp_data[hcp_index + src_index] = src_data[src_index];
   1335         }
   1336     }
   1337     return;
   1338 }
   1339 
   1340 
   1341 /*!
   1342  * \brief Sends the Generic HCI Commands to the connected reader device.
   1343  *
   1344  * This function Sends the Generic HCI Command frames in the HCP packet format to the
   1345  * connected reader device.
   1346  */
   1347 
   1348  NFCSTATUS
   1349  phHciNfc_Send_Generic_Cmd (
   1350                                 phHciNfc_sContext_t *psHciContext,
   1351                                 void                *pHwRef,
   1352                                 uint8_t             pipe_id,
   1353                                 uint8_t             cmd
   1354                     )
   1355  {
   1356     phHciNfc_HCP_Packet_t   *hcp_packet = NULL;
   1357     phHciNfc_HCP_Message_t  *hcp_message = NULL;
   1358     phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
   1359     uint16_t                 length = 0;
   1360     uint16_t                 i=0;
   1361     NFCSTATUS               status = NFCSTATUS_SUCCESS;
   1362 
   1363     if((NULL == psHciContext)
   1364         || ( pipe_id > PHHCINFC_MAX_PIPE)
   1365         ||(NULL == psHciContext->p_pipe_list[pipe_id])
   1366       )
   1367     {
   1368         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
   1369         HCI_DEBUG("%s: Invalid Arguments passed \n",
   1370                                                 "phHciNfc_Send_Generic_Cmd");
   1371     }
   1372     else
   1373     {
   1374         p_pipe_info = (phHciNfc_Pipe_Info_t *)
   1375                                 psHciContext->p_pipe_list[pipe_id];
   1376         psHciContext->tx_total = 0 ;
   1377         length +=  HCP_HEADER_LEN ;
   1378         switch( cmd )
   1379         {
   1380             case ANY_SET_PARAMETER:
   1381             {
   1382 
   1383                 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
   1384                 /* Construct the HCP Frame */
   1385                 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
   1386                                         (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
   1387                 hcp_message = &(hcp_packet->msg.message);
   1388                 hcp_message->payload[i++] = p_pipe_info->reg_index ;
   1389                 phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
   1390                                             i, (uint8_t *)p_pipe_info->param_info,
   1391                                             p_pipe_info->param_length);
   1392                 length =(uint16_t)(length + i + p_pipe_info->param_length);
   1393                 break;
   1394             }
   1395             case ANY_GET_PARAMETER:
   1396             {
   1397 
   1398                 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
   1399                 /* Construct the HCP Frame */
   1400                 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
   1401                                         (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
   1402                 hcp_message = &(hcp_packet->msg.message);
   1403                 hcp_message->payload[i++] = p_pipe_info->reg_index ;
   1404                 length =(uint16_t)(length + i);
   1405                 break;
   1406             }
   1407             case ANY_OPEN_PIPE:
   1408             case ANY_CLOSE_PIPE:
   1409             {
   1410 
   1411                 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
   1412                 /* Construct the HCP Frame */
   1413                 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
   1414                                         (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
   1415                 break;
   1416             }
   1417             default:
   1418             {
   1419                 status = PHNFCSTVAL( CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED );
   1420                 HCI_DEBUG("%s: Statement Should Not Occur \n","phHciNfc_Send_Generic_Cmd");
   1421                 break;
   1422             }
   1423         }
   1424         if( NFCSTATUS_SUCCESS == status )
   1425         {
   1426             p_pipe_info->sent_msg_type = HCP_MSG_TYPE_COMMAND;
   1427             p_pipe_info->prev_msg = cmd;
   1428             psHciContext->tx_total = length;
   1429             psHciContext->response_pending = TRUE ;
   1430             /* Send the Constructed HCP packet to the lower layer */
   1431             status = phHciNfc_Send_HCP( psHciContext, pHwRef );
   1432             p_pipe_info->prev_status = NFCSTATUS_PENDING;
   1433         }
   1434     }
   1435 
   1436     return status;
   1437 }
   1438 
   1439 
   1440 /*!
   1441  * \brief Sets the parameter of the registers in a particular Pipe.
   1442  *
   1443  * This function configures the registers in a particular Pipe.
   1444  */
   1445 
   1446 NFCSTATUS
   1447 phHciNfc_Set_Param (
   1448                                 phHciNfc_sContext_t     *psHciContext,
   1449                                 void                    *pHwRef,
   1450                                 phHciNfc_Pipe_Info_t    *p_pipe_info,
   1451                                 uint8_t                 reg_index,
   1452                                 void                    *p_param,
   1453                                 uint16_t                 param_length
   1454                     )
   1455  {
   1456     NFCSTATUS               status = NFCSTATUS_SUCCESS ;
   1457 
   1458     if( (NULL == p_pipe_info)
   1459         || (NULL == p_param)
   1460         || (0 == param_length)
   1461         )
   1462     {
   1463         status = PHNFCSTVAL( CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION );
   1464     }
   1465     else
   1466     {
   1467         p_pipe_info->param_info = (uint8_t *)p_param;
   1468         p_pipe_info->param_length =  param_length;
   1469         p_pipe_info->reg_index = reg_index;
   1470         status = phHciNfc_Send_Generic_Cmd( psHciContext, pHwRef,
   1471             (uint8_t)p_pipe_info->pipe.pipe_id,
   1472                 (uint8_t)ANY_SET_PARAMETER);
   1473         p_pipe_info->prev_status = status;
   1474     }
   1475 
   1476     return status;
   1477  }
   1478 
   1479 
   1480 #if 0
   1481  /*!
   1482  * \brief Gets the parameter of the registers in a particular Pipe.
   1483  *
   1484  * This function configures the registers in a particular Pipe.
   1485  */
   1486 
   1487  NFCSTATUS
   1488  phHciNfc_Get_Param (
   1489                                 phHciNfc_sContext_t     *psHciContext,
   1490                                 void                    *pHwRef,
   1491                                 phHciNfc_Pipe_Info_t    *p_pipe_info,
   1492                                 uint8_t                 reg_index,
   1493                     )
   1494  {
   1495     NFCSTATUS               status = NFCSTATUS_SUCCESS ;
   1496 
   1497     return status;
   1498  }
   1499 #endif
   1500 
   1501 
   1502 void
   1503 phHciNfc_Send_Complete (
   1504                                 void                    *psContext,
   1505                                 void                    *pHwRef,
   1506                                 phNfc_sTransactionInfo_t *pInfo
   1507                        )
   1508 {
   1509     NFCSTATUS               status = NFCSTATUS_SUCCESS ;
   1510     uint16_t                length = 0;
   1511 
   1512     HCI_PRINT("HCI Send Completion....\n");
   1513     if ( (NULL != psContext)
   1514         && (NULL != pInfo)
   1515         && (NULL != pHwRef)
   1516         )
   1517     {
   1518         phHciNfc_sContext_t *psHciContext = (phHciNfc_sContext_t *)psContext;
   1519         status = pInfo->status ;
   1520         length = pInfo->length ;
   1521         /* HCI_DEBUG("HCI Lower Layer Send Completion Before Receive,\
   1522                                                 Status = %02X\n",status); */
   1523         if(status != NFCSTATUS_SUCCESS)
   1524         {
   1525             /* Handle the Error Scenario */
   1526             (void)memset(psHciContext->send_buffer,
   1527                                             FALSE, PHHCINFC_MAX_BUFFERSIZE);
   1528             /* psHciContext->hci_transact_state = NFC_TRANSACT_COMPLETE;*/
   1529             phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
   1530         }
   1531         else
   1532         {
   1533 	        HCI_DEBUG("HCI Send Completion... Length = %02X\n", length);
   1534             /* To complete the send complete with the send
   1535              * or receive with chaining.
   1536              */
   1537             if( (TRUE == psHciContext->tx_hcp_chaining)
   1538                 &&( psHciContext->tx_remain > HCP_ZERO_LEN ))
   1539             {
   1540                 /* Skip the HCP Header Byte Sent */
   1541                 psHciContext->tx_remain -= length - 1;
   1542 
   1543                 /* Skip the HCP Header Byte Sent */
   1544                 psHciContext->tx_hcp_frgmnt_index += length - 1;
   1545 
   1546                 /* Send the Remaining HCP Data Frames */
   1547                 status = phHciNfc_Send_HCP( psHciContext, pHwRef );
   1548 
   1549                 HCI_DEBUG("HCI (Chaining) Send Resume: Status = %02X\n", status);
   1550 
   1551                 if( ( NFCSTATUS_SUCCESS != status )
   1552                     && (NFCSTATUS_PENDING != status )
   1553                     )
   1554                 {
   1555                     phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
   1556                 }/* End of the Status check */
   1557             }
   1558             else
   1559             {
   1560                 psHciContext->tx_total = HCP_ZERO_LEN ;
   1561                 psHciContext->tx_remain = HCP_ZERO_LEN ;
   1562                 psHciContext->tx_hcp_frgmnt_index = HCP_ZERO_LEN ;
   1563                 HCI_DEBUG("HCI: In Function: %s \n", __FUNCTION__);
   1564                 HCI_DEBUG("HCI: Response Pending status --> %s \n",
   1565                     (psHciContext->response_pending)?"TRUE":"FALSE");
   1566                 HCI_DEBUG("HCI: Event Pending status --> %s \n",
   1567                     (psHciContext->event_pending)?"TRUE":"FALSE");
   1568                 if ((TRUE == psHciContext->response_pending)
   1569                     || (TRUE == psHciContext->event_pending))
   1570                 {
   1571                     (void) memset(psHciContext->recv_buffer,
   1572                         FALSE, PHHCINFC_MAX_BUFFERSIZE);
   1573                     (void) memset((void *)&psHciContext->rx_packet,
   1574                         FALSE, sizeof(phHciNfc_HCP_Packet_t));
   1575 
   1576                     /* Reset the Received Data Index */
   1577                     psHciContext->rx_index = ZERO;
   1578                     /* Reset the size of the total response data received */
   1579                     psHciContext->rx_total = ZERO;
   1580 
   1581                     /* psHciContext->hci_transact_state = NFC_TRANSACT_SEND_COMPLETE;*/
   1582                     /* Receive the Response Packet */
   1583                     status = phHciNfc_Receive( psHciContext, pHwRef,
   1584                                 (uint8_t *)(&psHciContext->rx_packet),
   1585                                 sizeof(phHciNfc_HCP_Packet_t) );
   1586 
   1587                     /* HCI_DEBUG("HCI Lower Layer Send Completion After Receive,\
   1588                     Status = %02X\n",status); */
   1589 
   1590                     if( ( NFCSTATUS_SUCCESS != status )
   1591                          && (NFCSTATUS_PENDING != status )
   1592                         )
   1593                     {
   1594                         phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
   1595                     }/* End of the Status check */
   1596                 }
   1597                 else
   1598                 {
   1599                     status = phHciNfc_Resume_Sequence(psHciContext, pHwRef );
   1600                 }
   1601             }
   1602 
   1603         } /* End of status != Success */
   1604 
   1605     } /* End of Context != NULL  */
   1606 }
   1607 
   1608 
   1609 void
   1610 phHciNfc_Receive_Complete (
   1611                                 void                    *psContext,
   1612                                 void                    *pHwRef,
   1613                                 phNfc_sTransactionInfo_t *pInfo
   1614                                 )
   1615 {
   1616     NFCSTATUS               status = NFCSTATUS_SUCCESS ;
   1617     void                    *pdata = NULL ;
   1618     uint16_t                length = 0 ;
   1619 
   1620     HCI_PRINT("HCI Receive Completion....\n");
   1621     if ( (NULL != psContext)
   1622         && (NULL != pInfo)
   1623         && (NULL != pHwRef)
   1624         )
   1625     {
   1626         phHciNfc_sContext_t *psHciContext = (phHciNfc_sContext_t *)psContext;
   1627 
   1628         status = pInfo->status ;
   1629         pdata = pInfo->buffer ;
   1630         length = pInfo->length ;
   1631         HCI_DEBUG("HCI Lower Layer Receive Completion, Status = %02X\n",status);
   1632         if( NFCSTATUS_SUCCESS != status )
   1633         {
   1634             /* Handle the Error Scenario */
   1635             /* psHciContext->hci_transact_state = NFC_TRANSACT_COMPLETE; */
   1636             phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, (uint8_t)length );
   1637         }
   1638         else
   1639         {
   1640              /* Receive the remaining Response Packet */
   1641             /* psHciContext->hci_transact_state = NFC_TRANSACT_RECV_COMPLETE; */
   1642             status = phHciNfc_Process_HCP( psHciContext, pHwRef, pdata,(uint8_t) length );
   1643             if( ( NFCSTATUS_SUCCESS != status )
   1644                 && (NFCSTATUS_PENDING != status )
   1645               )
   1646             {
   1647                 phHciNfc_Error_Sequence(psHciContext, pHwRef, status , pdata, (uint8_t) length );
   1648             }
   1649         }
   1650     }
   1651 }
   1652 
   1653 void
   1654 phHciNfc_Notify(
   1655                     pphNfcIF_Notification_CB_t  p_upper_notify,
   1656                     void                        *p_upper_context,
   1657                     void                        *pHwRef,
   1658                     uint8_t                     type,
   1659                     void                        *pInfo
   1660                )
   1661 {
   1662     if( ( NULL != p_upper_notify) )
   1663     {
   1664         /* Notify the to the Upper Layer */
   1665         (p_upper_notify)(p_upper_context, pHwRef, type, pInfo);
   1666     }
   1667 
   1668 }
   1669 
   1670 
   1671 void
   1672 phHciNfc_Tag_Notify(
   1673                             phHciNfc_sContext_t     *psHciContext,
   1674                             void                    *pHwRef,
   1675                             uint8_t                 type,
   1676                             void                    *pInfo
   1677                )
   1678 {
   1679     phNfc_sCompletionInfo_t *psCompInfo =
   1680                                 (phNfc_sCompletionInfo_t *)pInfo;
   1681     pphNfcIF_Notification_CB_t  p_upper_notify = psHciContext->p_upper_notify;
   1682     void                        *pcontext = psHciContext->p_upper_context;
   1683     NFCSTATUS               status = NFCSTATUS_SUCCESS;
   1684 
   1685     switch( psHciContext->hci_state.next_state )
   1686     {
   1687         case hciState_Activate:
   1688         {
   1689             /* Roll Back to the Select State */
   1690             phHciNfc_FSM_Rollback(psHciContext);
   1691             break;
   1692         }
   1693         case hciState_Select:
   1694         {
   1695             status = phHciNfc_FSM_Complete(psHciContext);
   1696             break;
   1697         }
   1698         default:
   1699         {
   1700             /* Roll Back to the Select State */
   1701             phHciNfc_FSM_Rollback(psHciContext);
   1702             break;
   1703         }
   1704 
   1705     }
   1706 
   1707     if(NFCSTATUS_SUCCESS == status )
   1708     {
   1709             /* Notify the Tag Events to the Upper layer */
   1710         phHciNfc_Notify( p_upper_notify, pcontext , pHwRef,
   1711                                 type, psCompInfo);
   1712     }
   1713     else
   1714     {
   1715         phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
   1716     }
   1717 }
   1718 
   1719 
   1720 void
   1721 phHciNfc_Target_Select_Notify(
   1722                             phHciNfc_sContext_t     *psHciContext,
   1723                             void                    *pHwRef,
   1724                             uint8_t                 type,
   1725                             void                    *pInfo
   1726                )
   1727 {
   1728     phNfc_sCompletionInfo_t *psCompInfo =
   1729                                 (phNfc_sCompletionInfo_t *)pInfo;
   1730     pphNfcIF_Notification_CB_t  p_upper_notify = psHciContext->p_upper_notify;
   1731     void                        *pcontext = psHciContext->p_upper_context;
   1732     NFCSTATUS               status = NFCSTATUS_SUCCESS;
   1733 
   1734     switch( psHciContext->hci_state.next_state )
   1735     {
   1736         case hciState_Listen:
   1737         {
   1738             /* Roll Back to the Select State */
   1739             status = phHciNfc_FSM_Complete(psHciContext);
   1740             break;
   1741         }
   1742         case hciState_Select:
   1743         {
   1744             status = phHciNfc_FSM_Complete(psHciContext);
   1745             break;
   1746         }
   1747         default:
   1748         {
   1749             /* Roll Back to the Select State */
   1750             phHciNfc_FSM_Rollback(psHciContext);
   1751             break;
   1752         }
   1753 
   1754     }
   1755 
   1756     if(NFCSTATUS_SUCCESS == status )
   1757     {
   1758             /* Notify the Tag Events to the Upper layer */
   1759         phHciNfc_Notify( p_upper_notify, pcontext , pHwRef,
   1760                                 type, psCompInfo);
   1761     }
   1762     else
   1763     {
   1764         phHciNfc_Error_Sequence( psHciContext, pHwRef, status, NULL, 0 );
   1765     }
   1766 
   1767 }
   1768 
   1769 
   1770 
   1771 
   1772 void
   1773 phHciNfc_Release_Notify(
   1774                             phHciNfc_sContext_t     *psHciContext,
   1775                             void                    *pHwRef,
   1776                             uint8_t                 type,
   1777                             void                    *pInfo
   1778                )
   1779 {
   1780     phNfc_sCompletionInfo_t *psCompInfo =
   1781                                 (phNfc_sCompletionInfo_t *)pInfo;
   1782     pphNfcIF_Notification_CB_t  p_upper_notify = psHciContext->p_upper_notify;
   1783     void                        *pcontext = psHciContext->p_upper_context;
   1784     phHciNfc_Release_Resources( &psHciContext );
   1785         /* Notify the Failure to the Upper Layer */
   1786     phHciNfc_Notify( p_upper_notify, pcontext , pHwRef,
   1787                             type, psCompInfo);
   1788 }
   1789 
   1790 
   1791 void
   1792 phHciNfc_Notify_Event(
   1793                             void                    *psContext,
   1794                             void                    *pHwRef,
   1795                             uint8_t                 type,
   1796                             void                    *pInfo
   1797                     )
   1798 {
   1799     NFCSTATUS            status = NFCSTATUS_SUCCESS;
   1800 
   1801     if ( (NULL != psContext)
   1802         && (NULL != pInfo)
   1803         && (NULL != pHwRef)
   1804         )
   1805     {
   1806         phHciNfc_sContext_t *psHciContext = (phHciNfc_sContext_t *)psContext;
   1807 
   1808         /* Process based on the Notification type */
   1809         switch(type)
   1810         {
   1811             case NFC_NOTIFY_INIT_COMPLETED:
   1812             {
   1813                 phNfc_sCompletionInfo_t *psCompInfo =
   1814                                             (phNfc_sCompletionInfo_t *)pInfo;
   1815                 if(NFCSTATUS_SUCCESS == psCompInfo->status)
   1816                 {
   1817 
   1818 #if  (NXP_NFC_HCI_TIMER == 1)
   1819                     if ( NXP_INVALID_TIMER_ID == hci_resp_timer_id )
   1820                     {
   1821                         /* Create and Intialise the Response Timer */
   1822                         hci_resp_timer_id = phOsalNfc_Timer_Create( );
   1823                         HCI_DEBUG(" HCI : Timer %X Created \n",
   1824                                                             hci_resp_timer_id);
   1825                     }
   1826                     else
   1827                     {
   1828                         HCI_DEBUG(" HCI : Timer Already Created, Timer ID : %X\n",
   1829                                                                 hci_resp_timer_id);
   1830                     }
   1831                     gpsHciContext = psHciContext;
   1832 
   1833 #endif /* (NXP_NFC_HCI_TIMER == 1) */
   1834 
   1835                      /* Complete the Initialisation Sequence */
   1836                     status = phHciNfc_Resume_Sequence(psContext ,pHwRef);
   1837                 }
   1838                 else
   1839                 {
   1840                     /* Notify the Error Scenario to the Upper Layer */
   1841                     phHciNfc_Notify(psHciContext->p_upper_notify,
   1842                                     psHciContext->p_upper_context, pHwRef,
   1843                                             NFC_NOTIFY_ERROR, psCompInfo);
   1844                 }
   1845                 break;
   1846             }
   1847             case NFC_NOTIFY_INIT_FAILED:
   1848             {
   1849                  /* Notify the Failure to the Upper Layer */
   1850                 phHciNfc_Release_Notify( psContext,pHwRef,
   1851                                         type, pInfo );
   1852                 break;
   1853             }
   1854             case NFC_NOTIFY_RECV_COMPLETED:
   1855             {
   1856                 /* Receive Completed from the Lower Layer */
   1857                 phHciNfc_Receive_Complete(psContext,pHwRef,pInfo);
   1858 
   1859                 break;
   1860             }
   1861             case NFC_NOTIFY_SEND_COMPLETED:
   1862             {
   1863                 /* Receive Completed from the Lower Layer */
   1864                 phHciNfc_Send_Complete(psContext,pHwRef,pInfo);
   1865 
   1866                 break;
   1867             }
   1868             case NFC_NOTIFY_TRANSCEIVE_COMPLETED:
   1869             {
   1870                 /* TODO: TO handle Both Send and Receive Complete */
   1871                 break;
   1872             }
   1873             case NFC_NOTIFY_TARGET_DISCOVERED:
   1874             {
   1875                 HCI_PRINT(" PICC Discovery ! Obtain PICC Info .... \n");
   1876                 /* psHciContext->hci_seq = PL_DURATION_SEQ; */
   1877                 if ( hciState_Unknown == psHciContext->hci_state.next_state )
   1878                 {
   1879 
   1880                     status = phHciNfc_FSM_Update ( psHciContext, hciState_Select );
   1881 
   1882 
   1883                     if (NFCSTATUS_SUCCESS != status)
   1884                     {
   1885                        status = phHciNfc_ReaderMgmt_Deselect(
   1886                             psHciContext, pHwRef, phHal_eISO14443_A_PICC, FALSE);
   1887                     }
   1888                 }
   1889                 else
   1890                 {
   1891 #ifdef SW_RELEASE_TARGET
   1892                     /*status = phHciNfc_ReaderMgmt_Deselect(
   1893                         psHciContext, pHwRef, phHal_eISO14443_A_PICC, FALSE); */
   1894                     psHciContext->target_release = TRUE;
   1895 #else
   1896                     status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_STATE);
   1897 #endif
   1898                 }
   1899                 break;
   1900             }
   1901             /* To Notify the Target Released Notification
   1902              * to the Above Layer */
   1903             case NFC_NOTIFY_TARGET_RELEASED:
   1904             /* To Notify the NFC Secure Element Transaction
   1905              * Information to the Above Layer */
   1906             /* case NFC_NOTIFY_TRANSACTION: */
   1907             /* To Notify the Generic Events To the Upper
   1908              * Layer */
   1909             case NFC_NOTIFY_EVENT:
   1910             /* To Notify the Data Receive  Notification
   1911              * to the Above Layer */
   1912             case NFC_NOTIFY_RECV_EVENT:
   1913             {
   1914                 phNfc_sCompletionInfo_t *psCompInfo =
   1915 		                (phNfc_sCompletionInfo_t *)pInfo;
   1916 
   1917                 if (((TRUE == psHciContext->event_pending) ||
   1918                     (NFCSTATUS_RF_TIMEOUT == psCompInfo->status))
   1919                     && ( hciState_Transact == psHciContext->hci_state.next_state))
   1920                 {
   1921                     /* Rollback due to Transmission Error */
   1922                     phHciNfc_FSM_Rollback(psHciContext);
   1923                 }
   1924                 psHciContext->event_pending = FALSE;
   1925                 phHciNfc_Notify(psHciContext->p_upper_notify,
   1926                             psHciContext->p_upper_context, pHwRef,
   1927                                 type, pInfo);
   1928                 break;
   1929             }
   1930             case NFC_NOTIFY_DEVICE_ACTIVATED:
   1931             {
   1932                 HCI_PRINT("  Device Activated! Obtaining Remote Reader Info .... \n");
   1933                 if ( hciState_Unknown == psHciContext->hci_state.next_state )
   1934                 {
   1935                     switch (psHciContext->host_rf_type)
   1936                     {
   1937                         case phHal_eISO14443_A_PCD:
   1938                         case phHal_eISO14443_B_PCD:
   1939                         case phHal_eISO14443_BPrime_PCD:
   1940                         case phHal_eFelica_PCD:
   1941                         {
   1942                             break;
   1943                         }
   1944                         case phHal_eNfcIP1_Initiator:
   1945                         case phHal_eNfcIP1_Target:
   1946                         {
   1947                             break;
   1948                         }
   1949                         case phHal_eUnknown_DevType:
   1950                         default:
   1951                         {
   1952                             status = PHNFCSTVAL(CID_NFC_HCI,
   1953                                         NFCSTATUS_INVALID_PARAMETER);
   1954                             break;
   1955                         }
   1956 
   1957                     }
   1958                     status = phHciNfc_FSM_Update ( psHciContext, hciState_Listen );
   1959                 }
   1960                 else
   1961                 {
   1962                     status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_STATE);
   1963                 }
   1964                 break;
   1965             }
   1966             case NFC_NOTIFY_DEVICE_DEACTIVATED:
   1967             {
   1968                 HCI_PRINT(" Device De-Activated! \n");
   1969                 if ( hciState_Unknown == psHciContext->hci_state.next_state )
   1970                 {
   1971                     status = phHciNfc_FSM_Update ( psHciContext, hciState_Initialise );
   1972                     if(NFCSTATUS_SUCCESS == status)
   1973                     {
   1974                         /* Complete to the Select State */
   1975                         status = phHciNfc_FSM_Complete(psHciContext);
   1976                     }
   1977                     else
   1978                     {
   1979                         HCI_PRINT(" Device Deactivated.. But Invalid State \n");
   1980                         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_STATE);
   1981                     }
   1982                 }
   1983                 else
   1984                 {
   1985                     status = phHciNfc_ReaderMgmt_Update_Sequence(
   1986                                                 psHciContext, INFO_SEQ );
   1987 
   1988                     if(( hciState_Listen == psHciContext->hci_state.next_state)
   1989                         || (hciState_Transact == psHciContext->hci_state.next_state))
   1990                     {
   1991                         psHciContext->hci_state.next_state = hciState_Initialise;
   1992                         /* Roll Back to the Default State */
   1993                         status = phHciNfc_FSM_Complete(psHciContext);
   1994                     }
   1995                 }
   1996                 psHciContext->event_pending = FALSE;
   1997                 phHciNfc_Notify(psHciContext->p_upper_notify,
   1998                             psHciContext->p_upper_context, pHwRef,
   1999                             NFC_NOTIFY_EVENT, pInfo);
   2000                 break;
   2001             }
   2002             case NFC_NOTIFY_DEVICE_ERROR:
   2003             {
   2004                 phNfc_sCompletionInfo_t *psCompInfo =
   2005                                             (phNfc_sCompletionInfo_t *)pInfo;
   2006 
   2007                 psCompInfo->status = ( NFCSTATUS_BOARD_COMMUNICATION_ERROR
   2008                                         != PHNFCSTATUS(psCompInfo->status))?
   2009                                             NFCSTATUS_BOARD_COMMUNICATION_ERROR:
   2010                                                 psCompInfo->status ;
   2011 
   2012 #if  (NXP_NFC_HCI_TIMER == 1)
   2013 
   2014                 if ( NXP_INVALID_TIMER_ID != hci_resp_timer_id )
   2015                 {
   2016                     HCI_DEBUG(" HCI : Response Timer Stop, Status:%02X : %X\n",
   2017                                                           psCompInfo->status);
   2018                     /* Stop and Un-Intialise the Response Timer */
   2019                     phOsalNfc_Timer_Stop( hci_resp_timer_id );
   2020                 }
   2021 
   2022 #endif /* (NXP_NFC_HCI_TIMER == 1) */
   2023 
   2024                 phHciNfc_Notify(psHciContext->p_upper_notify,
   2025                             psHciContext->p_upper_context, pHwRef,
   2026                             (uint8_t) NFC_NOTIFY_DEVICE_ERROR, pInfo);
   2027 
   2028                 break;
   2029             }
   2030 
   2031             case NFC_NOTIFY_ERROR:
   2032             default:
   2033             {
   2034                 phNfc_sCompletionInfo_t *psCompInfo =
   2035                                             (phNfc_sCompletionInfo_t *)pInfo;
   2036 
   2037 #if  (NXP_NFC_HCI_TIMER == 1)
   2038 
   2039                 if (( NFCSTATUS_BOARD_COMMUNICATION_ERROR == PHNFCSTATUS(psCompInfo->status))
   2040                         && ( NXP_INVALID_TIMER_ID != hci_resp_timer_id ))
   2041                 {
   2042                     HCI_DEBUG(" HCI : Response Timer Stop, Status:%02X : %X\n",
   2043                                                           psCompInfo->status);
   2044                     /* Stop the HCI Response Timer */
   2045                     phOsalNfc_Timer_Stop( hci_resp_timer_id );
   2046                 }
   2047 
   2048 #endif /* (NXP_NFC_HCI_TIMER == 1) */
   2049 
   2050                 phHciNfc_Error_Sequence( psHciContext, pHwRef,
   2051                                                         psCompInfo->status, NULL, 0);
   2052                 break;
   2053             }
   2054         } /* End of Switch */
   2055     } /* End of Context != NULL  */
   2056 }
   2057 
   2058