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

    p_pipe_info = (phHciNfc_Pipe_Info_t *) 
                    psHciContext->p_pipe_list[pipe_id];
    psHciContext->tx_total = 0 ;
    length = (length + HCP_HEADER_LEN);

    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_EVENT, event);

    hcp_message = &(hcp_packet->msg.message);

    phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
                            i, 
                            (uint8_t *)p_pipe_info->param_info,
                            p_pipe_info->param_length);
    length = (uint8_t)(length + i + p_pipe_info->param_length);

    p_pipe_info->sent_msg_type = HCP_MSG_TYPE_EVENT ;
    p_pipe_info->prev_msg = event ;
    psHciContext->tx_total = length;

    /* Send the Constructed HCP packet to the lower layer */
    status = phHciNfc_Send_HCP( psHciContext, pHwRef );
    if(NFCSTATUS_PENDING == status)
    {
        ((phHciNfc_SWP_Info_t *)psHciContext->p_swp_info)->current_seq = 
            ((phHciNfc_SWP_Info_t *)psHciContext->p_swp_info)->next_seq;
        p_pipe_info->prev_status = status;
    }

    return status;
}
NFCSTATUS
phHciNfc_Send_Felica_Command(
                              phHciNfc_sContext_t   *psContext,
                              void                  *pHwRef,
                              uint8_t               pipe_id,
                              uint8_t               cmd
                              )
{
    NFCSTATUS                   status = NFCSTATUS_SUCCESS;
    phHciNfc_sContext_t         *psHciContext = 
                                (phHciNfc_sContext_t *)psContext ;
    if( (NULL == psHciContext) || (NULL == pHwRef) )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    }
    else if((NULL == psHciContext->p_felica_info) || 
        (HCI_FELICA_ENABLE != 
        ((phHciNfc_Felica_Info_t *)(psHciContext->p_felica_info))->
        enable_felica_gate) || 
        (HCI_UNKNOWN_PIPE_ID == 
        ((phHciNfc_Felica_Info_t *)(psHciContext->p_felica_info))->
        pipe_id) || 
        (pipe_id != 
        ((phHciNfc_Felica_Info_t *)(psHciContext->p_felica_info))->
        pipe_id))
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
    }
    else
    {
        phHciNfc_Felica_Info_t     *p_fel_info=NULL;
        phHciNfc_Pipe_Info_t        *p_pipe_info=NULL;
        phHciNfc_HCP_Packet_t       *hcp_packet = NULL;
        phHciNfc_HCP_Message_t      *hcp_message = NULL;
        uint8_t                     i = 0, 
                                    length = HCP_HEADER_LEN;

        p_fel_info = (phHciNfc_Felica_Info_t *)
                            psHciContext->p_felica_info ;
        p_pipe_info = p_fel_info->p_pipe_info;
        if(NULL == p_pipe_info )
        {
            status = PHNFCSTVAL(CID_NFC_HCI, 
                                NFCSTATUS_INVALID_HCI_SEQUENCE);
        }
        else
        {
            psHciContext->tx_total = 0 ;
            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);
            switch(cmd)
            {
                case NXP_FELICA_RAW:
                {
                    /*  
                    Buffer shall be updated with 
                    TO -              Time out (1 byte)
                    Status -          b0 to b2 indicate valid bits (1 byte)
                    Data  -           params received from this function 
                    */
                    hcp_message = &(hcp_packet->msg.message);

                    /* Time out */
                    hcp_message->payload[i++] = FELICA_TIMEOUT ;
                    /* Status */
                    hcp_message->payload[i++] = FELICA_STATUS;

                    phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
                                        i, (uint8_t *)p_pipe_info->param_info,
                                        p_pipe_info->param_length);
                    length =(uint8_t)(length + i + p_pipe_info->param_length);
                    break;
                }
                case NXP_FELICA_CMD:
                {
                    /* 
                    Buffer shall be updated with 
                    Cmd -               Authentication A/B, read/write 
                    (1 byte)
                    Data  -             params received from this function 
                    */
                    hcp_message = &(hcp_packet->msg.message);

                    /* Command */
                    hcp_message->payload[i++] = 
                                 psHciContext->p_xchg_info->params.tag_info.cmd_type ;
                    phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
                                        i, (uint8_t *)p_pipe_info->param_info,
                                        p_pipe_info->param_length);
                    length =(uint8_t)(length + i + p_pipe_info->param_length);
                    break;
                }
                default:
                {
                    status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_COMMAND);
                    break;
                }
            }
            if (NFCSTATUS_SUCCESS == status)
            {
                p_pipe_info->sent_msg_type = (uint8_t)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 = status;
            }
        }
    }
    return status;
}
static 
NFCSTATUS
phHciNfc_Recv_Felica_Response(
                               void                *psContext,
                               void                *pHwRef,
                               uint8_t             *pResponse,
#ifdef ONE_BYTE_LEN
                               uint8_t          length
#else
                               uint16_t             length
#endif
                               )
{
    NFCSTATUS                   status = NFCSTATUS_SUCCESS;
    phHciNfc_sContext_t         *psHciContext = 
                                    (phHciNfc_sContext_t *)psContext ;


    if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pResponse)
        || (0 == length))
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    }
    else if(NULL == psHciContext->p_felica_info)
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
    }
    else
    {
        phHciNfc_Felica_Info_t     *p_fel_info=NULL;
        uint8_t                     prev_cmd = ANY_GET_PARAMETER;
        p_fel_info = (phHciNfc_Felica_Info_t *)
                        psHciContext->p_felica_info ;
        if( NULL == p_fel_info->p_pipe_info)
        {
            status = PHNFCSTVAL(CID_NFC_HCI, 
                                NFCSTATUS_INVALID_HCI_SEQUENCE);
        }
        else
        {
            prev_cmd = p_fel_info->p_pipe_info->prev_msg ;
            switch(prev_cmd)
            {
                case ANY_GET_PARAMETER:
                {
                    status = phHciNfc_Felica_InfoUpdate(psHciContext, 
                                        p_fel_info->p_pipe_info->reg_index, 
                                        &pResponse[HCP_HEADER_LEN],
                                        (uint8_t)(length - HCP_HEADER_LEN));
                    break;
                }
                case ANY_SET_PARAMETER:
                {
                    HCI_PRINT("Felica Parameter Set \n");
                    status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext, 
                                                                UPDATE_SEQ);
                    p_fel_info->next_seq = FELICA_SYSTEMCODE;
                    break;
                }
                case ANY_OPEN_PIPE:
                {
                    HCI_PRINT("Felica open pipe complete\n");
                    status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext, 
                                                                UPDATE_SEQ);
                    p_fel_info->next_seq = FELICA_SYSTEMCODE;
                    break;
                }
                case ANY_CLOSE_PIPE:
                {
                    HCI_PRINT("Felica close pipe complete\n");
                    status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext, 
                                                                UPDATE_SEQ);
                    break;
                }
            
                case NXP_FELICA_RAW:
                case NXP_FELICA_CMD:
                case WR_XCHGDATA:
                {
                    HCI_PRINT("Felica packet received \n");
                    if (length >= HCP_HEADER_LEN)
                    {
                        phHciNfc_Append_HCPFrame(psHciContext->recv_buffer, 
                                                    0, pResponse, length);
                        psHciContext->rx_total = length;
                        status = phHciNfc_Recv_Felica_Packet(psHciContext,
                                                    prev_cmd, 
                                                    &pResponse[HCP_HEADER_LEN],
                                                    (length - HCP_HEADER_LEN));
                    }
                    else
                    {
                        status = PHNFCSTVAL(CID_NFC_HCI, 
                                            NFCSTATUS_INVALID_HCI_RESPONSE);
                    }
                    break;
                }
                case NXP_WRA_CONTINUE_ACTIVATION:
                case NXP_WR_ACTIVATE_ID:
                {
                    HCI_PRINT("Felica continue activation or ");
                    HCI_PRINT("reactivation completed \n");
                    status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext, 
                                                    UPDATE_SEQ);
                    break;
                }
                case NXP_WR_PRESCHECK:
                {
                    HCI_PRINT("Presence check completed \n");
                    break;
                }
                case NXP_WR_ACTIVATE_NEXT:
                {
                    HCI_PRINT("Activate next completed \n");
                    if (length > HCP_HEADER_LEN)
                    {
                        if (FEL_MULTIPLE_TAGS_FOUND == pResponse[HCP_HEADER_LEN])
                        {
                            p_fel_info->multiple_tgts_found = 
                                            FEL_MULTIPLE_TAGS_FOUND;
                        }
                        else
                        {
                            p_fel_info->multiple_tgts_found = FALSE;
                        }
                    }
                    else
                    {
                        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
                    }
                    break;
                }
                case NXP_WR_DISPATCH_TO_UICC:
                {
                    switch(length)
                    {
                        case HCP_HEADER_LEN:
                        {
                            /* Optional error code, if no error code field 
                                in the response, then this command is 
                                successfully completed */
                            p_fel_info->uicc_activation = 
                                        UICC_CARD_ACTIVATION_SUCCESS;
                            break;
                        }
                        case (HCP_HEADER_LEN + 1):
                        {
                            p_fel_info->uicc_activation = 
                                        pResponse[HCP_HEADER_LEN];
                            break;
                        } /* End of case (HCP_HEADER_LEN + index) */
                        default:
                        {
                            status = PHNFCSTVAL(CID_NFC_HCI, 
                                                NFCSTATUS_INVALID_HCI_RESPONSE);
                            break;
                        }
                    }
                    if (NFCSTATUS_SUCCESS == status)
                    {
                        status = phHciNfc_ReaderMgmt_Update_Sequence(psHciContext, 
                                                                    UPDATE_SEQ);
                    }
                    break;
                }
                default:
                {
                    status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
                    break;
                }
            }
            if( NFCSTATUS_SUCCESS == status )
            {
                p_fel_info->p_pipe_info->prev_status = NFCSTATUS_SUCCESS;
                p_fel_info->current_seq = p_fel_info->next_seq;
            }
        }
    }
    return status;
}
NFCSTATUS
phHciNfc_Send_ReaderA_Command(
    phHciNfc_sContext_t   *psHciContext,
    void                  *pHwRef,
    uint8_t               pipe_id,
    uint8_t               cmd
)
{
    NFCSTATUS                   status = NFCSTATUS_SUCCESS;

    if( (NULL == psHciContext) || (NULL == pHwRef) )
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
    }
    else if((NULL == psHciContext->p_reader_a_info) ||
            (HCI_READER_A_ENABLE !=
             ((phHciNfc_ReaderA_Info_t *)(psHciContext->p_reader_a_info))->
             enable_rdr_a_gate) ||
            (HCI_UNKNOWN_PIPE_ID ==
             ((phHciNfc_ReaderA_Info_t *)(psHciContext->p_reader_a_info))->
             pipe_id) ||
            (pipe_id !=
             ((phHciNfc_ReaderA_Info_t *)(psHciContext->p_reader_a_info))->
             pipe_id))
    {
        status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
    }
    else
    {
        phHciNfc_ReaderA_Info_t     *p_rdr_a_info=NULL;
        phHciNfc_Pipe_Info_t        *p_pipe_info=NULL;
        phHciNfc_HCP_Packet_t       *hcp_packet = NULL;
        phHciNfc_HCP_Message_t      *hcp_message = NULL;
        uint8_t                     i = 0;
        uint16_t                    length = HCP_HEADER_LEN;

        p_rdr_a_info = (phHciNfc_ReaderA_Info_t *)
                       psHciContext->p_reader_a_info ;
        p_pipe_info = p_rdr_a_info->p_pipe_info;
        if(NULL == p_pipe_info )
        {
            status = PHNFCSTVAL(CID_NFC_HCI,
                                NFCSTATUS_INVALID_HCI_SEQUENCE);
        }
        else
        {
            psHciContext->tx_total = 0 ;
            hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
            /* Construct the HCP Frame */
            switch(cmd)
            {
            case NXP_WRA_CONTINUE_ACTIVATION:
            case NXP_WR_ACTIVATE_ID:
            {
                phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
                                        (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
                break;
            }

            case NXP_MIFARE_RAW:
            {
                if (p_pipe_info->param_length < RDR_A_MIFARE_RAW_LENGTH)
                {
                    status = PHNFCSTVAL(CID_NFC_HCI,
                                        NFCSTATUS_INVALID_PARAMETER);
                }
                else
                {
                    /*
                        Buffer shall be updated with
                        TO -              Time out (1 byte)
                        Status -          b0 to b2 indicate valid bits (1 byte)
                        Data (with CRC) - params received from this function
                    */
                    hcp_message = &(hcp_packet->msg.message);
#ifdef ENABLE_MIFARE_RAW
                    /* Time out */
                    hcp_message->payload[i++] = NXP_MIFARE_XCHG_TIMEOUT ;
                    /* Status */
                    hcp_message->payload[i++] = RDR_A_MIFARE_STATUS;
#else
                    cmd = NXP_MIFARE_CMD;
#endif /* #ifdef ENABLE_MIFARE_RAW */
                    phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
                                            (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);

                    phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
                                             i, (uint8_t *)p_pipe_info->param_info,
#ifdef ENABLE_MIFARE_RAW
                                             p_pipe_info->param_length);
#else
                                             (p_pipe_info->param_length - 2));
#endif /* #ifdef ENABLE_MIFARE_RAW */

#ifdef ENABLE_MIFARE_RAW
                    length =(uint16_t)(length + i + p_pipe_info->param_length);
#else
                    length =(uint16_t)(length + i + p_pipe_info->param_length - 2);
#endif /* #ifdef ENABLE_MIFARE_RAW */
                }
                break;
            }
            case NXP_MIFARE_CMD:
            {
                /*
                    Buffer shall be updated with
                    Cmd -               Authentication A/B, read/write
                                        (1 byte)
                    Addr -              Address associated with Mifare cmd
                                        (1 byte)
                    Data  -             params received from this function
                */
                phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
                                        (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
                hcp_message = &(hcp_packet->msg.message);

                /* Command */
                hcp_message->payload[i++] =
                    psHciContext->p_xchg_info->params.tag_info.cmd_type ;
                /* Address */
                hcp_message->payload[i++] =
                    psHciContext->p_xchg_info->params.tag_info.addr ;
                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;
            }
            default:
            {
                status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_COMMAND);
                break;
            }
            }
            if (NFCSTATUS_SUCCESS == status)
            {
                p_pipe_info->sent_msg_type = (uint8_t)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 = status;
            }
        }
    }
    return status;
}