/*******************************************************************************
**
** Function         nci_proc_rf_management_rsp
**
** Description      Process NCI responses in the RF Management group
**
** Returns          void
**
*******************************************************************************/
void nci_proc_rf_management_rsp (BT_HDR *p_msg)
{
    UINT8   *p;
    UINT8   *pp, len, op_code;
    UINT8   *p_old = nfc_cb.last_cmd;

    /* find the start of the NCI message and parse the NCI header */
    p   = (UINT8 *) (p_msg + 1) + p_msg->offset;
    pp  = p+1;
    NCI_MSG_PRS_HDR1 (pp, op_code);
    len = *pp++;

    switch (op_code)
    {
    case NCI_MSG_RF_DISCOVER:
        nfc_ncif_rf_management_status (NFC_START_DEVT, *pp);
        break;

    case NCI_MSG_RF_DISCOVER_SELECT:
        nfc_ncif_rf_management_status (NFC_SELECT_DEVT, *pp);
        break;

    case NCI_MSG_RF_T3T_POLLING:
        break;

    case NCI_MSG_RF_DISCOVER_MAP:
        nfc_ncif_rf_management_status (NFC_MAP_DEVT, *pp);
        break;

    case NCI_MSG_RF_DEACTIVATE:
        nfc_ncif_proc_deactivate (*pp, *p_old, FALSE);
        break;

#if (NFC_NFCEE_INCLUDED == TRUE)
#if (NFC_RW_ONLY == FALSE)

    case NCI_MSG_RF_SET_ROUTING:
        nfc_ncif_event_status (NFC_SET_ROUTING_REVT, *pp);
        break;

    case NCI_MSG_RF_GET_ROUTING:
        if (*pp != NFC_STATUS_OK)
            nfc_ncif_event_status (NFC_GET_ROUTING_REVT, *pp);
        break;
#endif
#endif

    case NCI_MSG_RF_PARAMETER_UPDATE:
        nfc_ncif_event_status (NFC_RF_COMM_PARAMS_UPDATE_REVT, *pp);
        break;

    default:
        NFC_TRACE_ERROR1 ("unknown opcode:0x%x", op_code);
        break;
    }
}
/*******************************************************************************
**
** Function         nci_proc_rf_management_ntf
**
** Description      Process NCI notifications in the RF Management group
**
** Returns          void
**
*******************************************************************************/
void nci_proc_rf_management_ntf (BT_HDR *p_msg)
{
    UINT8   *p;
    UINT8   *pp, len, op_code;

    /* find the start of the NCI message and parse the NCI header */
    p   = (UINT8 *) (p_msg + 1) + p_msg->offset;
    pp  = p+1;
    NCI_MSG_PRS_HDR1 (pp, op_code);
    len = *pp++;

    switch (op_code)
    {
    case NCI_MSG_RF_DISCOVER :
        nfc_ncif_proc_discover_ntf (p, p_msg->len);
        break;

    case NCI_MSG_RF_DEACTIVATE:
        nfc_ncif_proc_deactivate (NFC_STATUS_OK, *pp, TRUE);
        break;

    case NCI_MSG_RF_INTF_ACTIVATED:
        nfc_ncif_proc_activate (pp, len);
        break;

    case NCI_MSG_RF_FIELD:
        nfc_ncif_proc_rf_field_ntf (*pp);
        break;

    case NCI_MSG_RF_T3T_POLLING:
        nfc_ncif_proc_t3t_polling_ntf (pp, len);
        break;

#if (NFC_NFCEE_INCLUDED == TRUE)
#if (NFC_RW_ONLY == FALSE)

    case NCI_MSG_RF_GET_ROUTING:
        nfc_ncif_proc_get_routing (pp, len);
        break;

    case NCI_MSG_RF_EE_ACTION:
        nfc_ncif_proc_ee_action (pp, len);
        break;

    case NCI_MSG_RF_EE_DISCOVERY_REQ:
        nfc_ncif_proc_ee_discover_req (pp, len);
        break;
#endif
#endif

    default:
        NFC_TRACE_ERROR1 ("unknown opcode:0x%x", op_code);
        break;
    }
}
/*******************************************************************************
**
** Function         NFC_NfceeModeSet
**
** Description      This function is called to activate or de-activate an NFCEE
**                  connected to the NFCC.
**                  The response from NFCC is reported by tNFC_RESPONSE_CBACK
**                  as NFC_NFCEE_MODE_SET_REVT.
**
** Parameters       nfcee_id - the NFCEE to activate or de-activate.
**                  mode - NFC_MODE_ACTIVATE to activate NFCEE,
**                         NFC_MODE_DEACTIVATE to de-activate.
**
** Returns          tNFC_STATUS
**
*******************************************************************************/
tNFC_STATUS NFC_NfceeModeSet (UINT8              nfcee_id,
                              tNFC_NFCEE_MODE    mode)
{
    if (mode >= NCI_NUM_NFCEE_MODE)
    {
        NFC_TRACE_ERROR1 ("NFC_NfceeModeSet bad mode:%d", mode);
        return NFC_STATUS_FAILED;
    }

    return nci_snd_nfcee_mode_set (nfcee_id, mode);
}
/*******************************************************************************
**
** Function         nci_proc_ee_management_rsp
**
** Description      Process NCI responses in the NFCEE Management group
**
** Returns          void
**
*******************************************************************************/
void nci_proc_ee_management_rsp (BT_HDR *p_msg)
{
    UINT8   *p;
    UINT8   *pp, len, op_code;
    tNFC_RESPONSE_CBACK *p_cback = nfc_cb.p_resp_cback;
    tNFC_NFCEE_DISCOVER_REVT    nfcee_discover;
    tNFC_NFCEE_INFO_REVT        nfcee_info;
    tNFC_NFCEE_MODE_SET_REVT    mode_set;
    tNFC_RESPONSE   *p_evt = (tNFC_RESPONSE *) &nfcee_info;
    tNFC_RESPONSE_EVT event = NFC_NFCEE_INFO_REVT;
    UINT8   *p_old = nfc_cb.last_cmd;

    /* find the start of the NCI message and parse the NCI header */
    p   = (UINT8 *) (p_msg + 1) + p_msg->offset;
    pp  = p+1;
    NCI_MSG_PRS_HDR1 (pp, op_code);
    NFC_TRACE_DEBUG1 ("nci_proc_ee_management_rsp opcode:0x%x", op_code);
    len = *pp++;

    switch (op_code)
    {
    case NCI_MSG_NFCEE_DISCOVER:
        p_evt                       = (tNFC_RESPONSE *) &nfcee_discover;
        nfcee_discover.status       = *pp++;
        nfcee_discover.num_nfcee    = *pp++;

        if (nfcee_discover.status != NFC_STATUS_OK)
            nfcee_discover.num_nfcee    = 0;

        event                       = NFC_NFCEE_DISCOVER_REVT;
        break;

    case NCI_MSG_NFCEE_MODE_SET:
        p_evt                   = (tNFC_RESPONSE *) &mode_set;
        mode_set.status         = *pp;
        mode_set.nfcee_id       = 0;
        event                   = NFC_NFCEE_MODE_SET_REVT;
        mode_set.nfcee_id       = *p_old++;
        mode_set.mode           = *p_old++;
        break;

    default:
        p_cback = NULL;
        NFC_TRACE_ERROR1 ("unknown opcode:0x%x", op_code);
        break;
    }

    if (p_cback)
        (*p_cback) (event, p_evt);
}
/*******************************************************************************
**
** Function         nci_proc_core_rsp
**
** Description      Process NCI responses in the CORE group
**
** Returns          TRUE-caller of this function to free the GKI buffer p_msg
**
*******************************************************************************/
BOOLEAN nci_proc_core_rsp (BT_HDR *p_msg)
{
    UINT8   *p;
    UINT8   *pp, len, op_code;
    BOOLEAN free = TRUE;
    UINT8   *p_old = nfc_cb.last_cmd;

    /* find the start of the NCI message and parse the NCI header */
    p   = (UINT8 *) (p_msg + 1) + p_msg->offset;
    pp  = p+1;
    NCI_MSG_PRS_HDR1 (pp, op_code);
    NFC_TRACE_DEBUG1 ("nci_proc_core_rsp opcode:0x%x", op_code);
    len = *pp++;

    /* process the message based on the opcode and message type */
    switch (op_code)
    {
    case NCI_MSG_CORE_RESET:
        nfc_ncif_proc_reset_rsp (pp, FALSE);
        break;

    case NCI_MSG_CORE_INIT:
        nfc_ncif_proc_init_rsp (p_msg);
        free = FALSE;
        break;

    case NCI_MSG_CORE_GET_CONFIG:
        nfc_ncif_proc_get_config_rsp (p_msg);
        break;

    case NCI_MSG_CORE_SET_CONFIG:
        nfc_ncif_set_config_status (pp, len);
        break;

    case NCI_MSG_CORE_CONN_CREATE:
        nfc_ncif_proc_conn_create_rsp (p, p_msg->len, *p_old);
        break;

    case NCI_MSG_CORE_CONN_CLOSE:
        nfc_ncif_report_conn_close_evt (*p_old, *pp);
        break;

    default:
        NFC_TRACE_ERROR1 ("unknown opcode:0x%x", op_code);
        break;
    }

    return free;
}
/*******************************************************************************
**
** Function         nci_proc_core_ntf
**
** Description      Process NCI notifications in the CORE group
**
** Returns          void
**
*******************************************************************************/
void nci_proc_core_ntf (BT_HDR *p_msg)
{
    UINT8   *p;
    UINT8   *pp, len, op_code;
    UINT8   conn_id;

    /* find the start of the NCI message and parse the NCI header */
    p   = (UINT8 *) (p_msg + 1) + p_msg->offset;
    pp  = p+1;
    NCI_MSG_PRS_HDR1 (pp, op_code);
    NFC_TRACE_DEBUG1 ("nci_proc_core_ntf opcode:0x%x", op_code);
    len = *pp++;

    /* process the message based on the opcode and message type */
    switch (op_code)
    {
    case NCI_MSG_CORE_RESET:
        nfc_ncif_proc_reset_rsp (pp, TRUE);
        break;

    case NCI_MSG_CORE_GEN_ERR_STATUS:
        /* process the error ntf */
        /* in case of timeout: notify the static connection callback */
        nfc_ncif_event_status (NFC_GEN_ERROR_REVT, *pp);
        nfc_ncif_error_status (NFC_RF_CONN_ID, *pp);
        break;

    case NCI_MSG_CORE_INTF_ERR_STATUS:
        conn_id = *(pp+1);
        nfc_ncif_error_status (conn_id, *pp);
        break;

    case NCI_MSG_CORE_CONN_CREDITS:
        nfc_ncif_proc_credits(pp, len);
        break;

    default:
        NFC_TRACE_ERROR1 ("unknown opcode:0x%x", op_code);
        break;
    }
}
/*******************************************************************************
**
** Function         NFC_SendVsCommand
**
** Description      This function is called to send the given vendor specific
**                  command to NFCC. The response from NFCC is reported to the
**                  given tNFC_VS_CBACK as (oid).
**
** Parameters       oid - The opcode of the VS command.
**                  p_data - The parameters for the VS command
**
** Returns          tNFC_STATUS
**
*******************************************************************************/
tNFC_STATUS NFC_SendVsCommand (UINT8          oid,
                               BT_HDR        *p_data,
                               tNFC_VS_CBACK *p_cback)
{
    tNFC_STATUS     status = NFC_STATUS_OK;
    UINT8           *pp;

    /* Allow VSC with 0-length payload */
    if (p_data == NULL)
    {
        p_data = NCI_GET_CMD_BUF (0);
        if (p_data)
        {
            p_data->offset  = NCI_VSC_MSG_HDR_SIZE;
            p_data->len     = 0;
        }
    }

    /* Validate parameters */
    if ((p_data == NULL) || (p_data->offset < NCI_VSC_MSG_HDR_SIZE) || (p_data->len > NCI_MAX_VSC_SIZE))
    {
        NFC_TRACE_ERROR1 ("buffer offset must be >= %d", NCI_VSC_MSG_HDR_SIZE);
        if (p_data)
            GKI_freebuf (p_data);
        return NFC_STATUS_INVALID_PARAM;
    }

    p_data->event           = BT_EVT_TO_NFC_NCI;
    p_data->layer_specific  = NFC_WAIT_RSP_VSC;
    /* save the callback function in the BT_HDR, to receive the response */
    ((tNFC_NCI_VS_MSG *) p_data)->p_cback = p_cback;

    p_data->offset -= NCI_MSG_HDR_SIZE;
    pp              = (UINT8 *) (p_data + 1) + p_data->offset;
    NCI_MSG_BLD_HDR0 (pp, NCI_MT_CMD, NCI_GID_PROP);
    NCI_MSG_BLD_HDR1 (pp, oid);
    *pp             = (UINT8) p_data->len;
    p_data->len    += NCI_MSG_HDR_SIZE;
    nfc_ncif_check_cmd_queue (p_data);
    return status;
}
/*******************************************************************************
**
** Function         NFC_SendNxpNciCommand
**
** Description      This function is called to send the given nxp specific
**                  command to NFCC. The response from NFCC is reported to the
**                  given tNFC_VS_CBACK.
**
** Parameters       p_data - The command buffer
**
** Returns          tNFC_STATUS
**
*******************************************************************************/
tNFC_STATUS NFC_SendNxpNciCommand (BT_HDR        *p_data,
                               tNFC_VS_CBACK *p_cback)
{
    tNFC_STATUS     status = NFC_STATUS_OK;
    UINT8           *pp;

    /* Validate parameters */
    if ((p_data == NULL) || (p_data->len > NCI_MAX_VSC_SIZE))
    {
        NFC_TRACE_ERROR1 ("buffer offset must be >= %d", NCI_VSC_MSG_HDR_SIZE);
        if (p_data)
            GKI_freebuf (p_data);
        return NFC_STATUS_INVALID_PARAM;
    }

    p_data->event           = BT_EVT_TO_NFC_NCI;
    p_data->layer_specific  = NFC_WAIT_RSP_NXP;
    /* save the callback function in the BT_HDR, to receive the response */
    ((tNFC_NCI_VS_MSG *) p_data)->p_cback = p_cback;
    pp              = (UINT8 *) (p_data + 1) + p_data->offset;

    nfc_ncif_check_cmd_queue (p_data);
    return status;
}
/*******************************************************************************
**
** Function         nci_proc_ee_management_ntf
**
** Description      Process NCI notifications in the NFCEE Management group
**
** Returns          void
**
*******************************************************************************/
void nci_proc_ee_management_ntf (BT_HDR *p_msg)
{
    UINT8                 *p;
    UINT8                 *pp, len, op_code;
    tNFC_RESPONSE_CBACK   *p_cback = nfc_cb.p_resp_cback;
    tNFC_NFCEE_INFO_REVT  nfcee_info;
    tNFC_RESPONSE         *p_evt   = (tNFC_RESPONSE *) &nfcee_info;
    tNFC_RESPONSE_EVT     event    = NFC_NFCEE_INFO_REVT;
    UINT8                 xx;
    UINT8                 yy;
    UINT8                 ee_status;
    tNFC_NFCEE_TLV        *p_tlv;

    /* find the start of the NCI message and parse the NCI header */
    p   = (UINT8 *) (p_msg + 1) + p_msg->offset;
    pp  = p+1;
    NCI_MSG_PRS_HDR1 (pp, op_code);
    NFC_TRACE_DEBUG1 ("nci_proc_ee_management_ntf opcode:0x%x", op_code);
    len = *pp++;

    if (op_code == NCI_MSG_NFCEE_DISCOVER)
    {
        nfcee_info.nfcee_id    = *pp++;
        ee_status                   = *pp++;

        nfcee_info.ee_status        = ee_status;
        yy                          = *pp;
        nfcee_info.num_interface    = *pp++;
        p                           = pp;

        if (nfcee_info.num_interface > NFC_MAX_EE_INTERFACE)
            nfcee_info.num_interface = NFC_MAX_EE_INTERFACE;

        for (xx = 0; xx < nfcee_info.num_interface; xx++)
        {
            nfcee_info.ee_interface[xx] = *pp++;
        }

        pp                              = p + yy;
        nfcee_info.num_tlvs             = *pp++;
        NFC_TRACE_DEBUG4 ("nfcee_id: 0x%x num_interface:0x%x/0x%x, num_tlvs:0x%x",
            nfcee_info.nfcee_id, nfcee_info.num_interface, yy, nfcee_info.num_tlvs);

        if (nfcee_info.num_tlvs > NFC_MAX_EE_TLVS)
            nfcee_info.num_tlvs = NFC_MAX_EE_TLVS;

        p_tlv = &nfcee_info.ee_tlv[0];

        for (xx = 0; xx < nfcee_info.num_tlvs; xx++, p_tlv++)
        {
            p_tlv->tag  = *pp++;
            p_tlv->len  = yy = *pp++;
            NFC_TRACE_DEBUG2 ("tag:0x%x, len:0x%x", p_tlv->tag, p_tlv->len);
            if (p_tlv->len > NFC_MAX_EE_INFO)
                p_tlv->len = NFC_MAX_EE_INFO;
            p   = pp;
            STREAM_TO_ARRAY (p_tlv->info, pp, p_tlv->len);
            pp  = p += yy;
        }
    }
    else
    {
        p_cback = NULL;
        NFC_TRACE_ERROR1 ("unknown opcode:0x%x", op_code);
    }

    if (p_cback)
        (*p_cback) (event, p_evt);
}