/*******************************************************************************
**
** Function         NFA_HciClosePipe
**
** Description      This function is called to close a dynamic pipe.
**                  When the dynamic pipe is closed (or
**                  if an error occurs), the app will be notified with
**                  NFA_HCI_CLOSE_PIPE_EVT with the pipe id.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciClosePipe (tNFA_HANDLE hci_handle, UINT8 pipe)
{
    tNFA_HCI_API_CLOSE_PIPE_EVT *p_msg;

    NFA_TRACE_API2 ("NFA_HciClosePipe (): hci_handle:0x%04x, pipe:0x%02X", hci_handle, pipe);

    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciClosePipe (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    if ((pipe < NFA_HCI_FIRST_DYNAMIC_PIPE) || (pipe > NFA_HCI_LAST_DYNAMIC_PIPE))
    {
        NFA_TRACE_API1 ("NFA_HciClosePipe (): Invalid Pipe:0x%02x", pipe);
        return (NFA_STATUS_FAILED);
    }

    /* Request HCI to close a pipe if it is in opened state */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&(!nfa_hci_cb.b_low_power_mode)
        &&((p_msg = (tNFA_HCI_API_CLOSE_PIPE_EVT *) GKI_getbuf (sizeof (tNFA_HCI_API_CLOSE_PIPE_EVT))) != NULL) )
    {
        p_msg->hdr.event    = NFA_HCI_API_CLOSE_PIPE_EVT;
        p_msg->hci_handle   = hci_handle;
        p_msg->pipe         = pipe;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }
    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciGetRegistry
**
** Description      This function requests a peer host to return the desired
**                  registry field value for the gate that the pipe is on.
**
**                  When the peer host responds,the app is notified with
**                  NFA_HCI_GET_REG_RSP_EVT or
**                  if an error occurs in sending the command the app will be
**                  notified by NFA_HCI_CMD_SENT_EVT
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciGetRegistry (tNFA_HANDLE hci_handle, UINT8 pipe, UINT8 reg_inx)
{
    tNFA_HCI_API_GET_REGISTRY *p_msg;

    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciGetRegistry (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    if (pipe < NFA_HCI_FIRST_DYNAMIC_PIPE)
    {
        NFA_TRACE_API1 ("NFA_HciGetRegistry (): Invalid Pipe:0x%02x", pipe);
        return (NFA_STATUS_FAILED);
    }

    NFA_TRACE_API2 ("NFA_HciGetRegistry (): hci_handle:0x%04x  Pipe: 0x%02x", hci_handle, pipe);

    /* Request HCI to get list of gates supported by the specified host */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_GET_REGISTRY *) GKI_getbuf (sizeof (tNFA_HCI_API_GET_REGISTRY))) != NULL) )
    {
        p_msg->hdr.event    = NFA_HCI_API_GET_REGISTRY_EVT;
        p_msg->hci_handle   = hci_handle;
        p_msg->pipe         = pipe;
        p_msg->reg_inx      = reg_inx;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciGetHostList
**
** Description      This function will request the host controller to return the
**                  list of hosts that are present in the host network. When
**                  host controller responds with the host list (or if an error
**                  occurs), the app will be notified with NFA_HCI_HOST_LIST_EVT
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciGetHostList (tNFA_HANDLE hci_handle)
{
    tNFA_HCI_API_GET_HOST_LIST *p_msg;


    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciGetHostList (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    NFA_TRACE_API1 ("NFA_HciGetHostList (): hci_handle:0x%04x",hci_handle);

    /* Request HCI to get list of host in the hci network */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_GET_HOST_LIST *) GKI_getbuf (sizeof (tNFA_HCI_API_GET_HOST_LIST))) != NULL) )
    {
        p_msg->hdr.event    = NFA_HCI_API_GET_HOST_LIST_EVT;
        p_msg->hci_handle   = hci_handle;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciDeletePipe
**
** Description      This function is called to delete a particular dynamic pipe.
**                  When the dynamic pipe is deleted (or if an error occurs),
**                  the app will be notified with NFA_HCI_DELETE_PIPE_EVT with
**                  the pipe id. After successful deletion of pipe, registry
**                  entry will be deleted for the dynamic pipe and all
**                  information related to the pipe will be deleted from non
**                  volatile memory.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciDeletePipe (tNFA_HANDLE  hci_handle, UINT8 pipe)
{
    tNFA_HCI_API_DELETE_PIPE_EVT *p_msg;

    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciDeletePipe (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    if ((pipe < NFA_HCI_FIRST_DYNAMIC_PIPE) || (pipe > NFA_HCI_LAST_DYNAMIC_PIPE))
    {
        NFA_TRACE_API1 ("NFA_HciDeletePipe (): Invalid Pipe:0x%02x", pipe);
        return (NFA_STATUS_FAILED);
    }

    NFA_TRACE_API2 ("NFA_HciDeletePipe (): hci_handle:0x%04x, pipe:0x%02X", hci_handle, pipe);

    /* Request HCI to delete a pipe created by the application identified by hci handle */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&(!nfa_hci_cb.b_low_power_mode)
        &&((p_msg = (tNFA_HCI_API_DELETE_PIPE_EVT *) GKI_getbuf (sizeof (tNFA_HCI_API_DELETE_PIPE_EVT))) != NULL) )
    {
        p_msg->hdr.event    = NFA_HCI_API_DELETE_PIPE_EVT;
        p_msg->hci_handle   = hci_handle;
        p_msg->pipe         = pipe;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }
    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_RwSetTagReadOnly
**
** Description:
**      Sets tag as read only.
**
**      When tag is set as read only, or if an error occurs, the app will be
**      notified with NFA_SET_TAG_RO_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_REJECTED if protocol is not T1/T2/ISO15693
**                 (or) if hard lock is not requested for protocol ISO15693
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwSetTagReadOnly (BOOLEAN b_hard_lock)
{
    tNFA_RW_OPERATION *p_msg;
    tNFC_PROTOCOL      protocol = nfa_rw_cb.protocol;

    if ((protocol != NFC_PROTOCOL_T1T) && (protocol != NFC_PROTOCOL_T2T) && (protocol != NFC_PROTOCOL_15693) && (protocol != NFC_PROTOCOL_ISO_DEP) && (protocol != NFC_PROTOCOL_T3T))
    {
        NFA_TRACE_API1 ("NFA_RwSetTagReadOnly (): Cannot Configure as read only for Protocol: %d", protocol);
        return (NFA_STATUS_REJECTED);
    }

    if (  (!b_hard_lock && (protocol == NFC_PROTOCOL_15693))
        ||(b_hard_lock && (protocol == NFC_PROTOCOL_ISO_DEP))  )
    {
        NFA_TRACE_API2 ("NFA_RwSetTagReadOnly (): Cannot %s for Protocol: %d", b_hard_lock ? "Hard lock" : "Soft lock", protocol);
        return (NFA_STATUS_REJECTED);
    }

    NFA_TRACE_API1 ("NFA_RwSetTagReadOnly (): %s", b_hard_lock ? "Hard lock" : "Soft lock");

    if ((p_msg = (tNFA_RW_OPERATION *) GKI_getbuf ((UINT16) (sizeof (tNFA_RW_OPERATION)))) != NULL)
    {
        /* Fill in tNFA_RW_OPERATION struct */
        p_msg->hdr.event                       = NFA_RW_OP_REQUEST_EVT;
        p_msg->op                              = NFA_RW_OP_SET_TAG_RO;
        p_msg->params.set_readonly.b_hard_lock = b_hard_lock;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }
    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciDeallocGate
**
** Description      This function will release the specified gate that was
**                  previously allocated to the application. When the generic
**                  gate is released (or if an error occurs), the app will be
**                  notified with NFA_HCI_DEALLOCATE_GATE_EVT with the gate id.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciDeallocGate (tNFA_HANDLE hci_handle, UINT8 gate)
{
    tNFA_HCI_API_DEALLOC_GATE *p_msg;

    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciDeallocGate (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    if ((gate < NFA_HCI_FIRST_HOST_SPECIFIC_GENERIC_GATE) || (gate > NFA_HCI_LAST_PROP_GATE) || (gate == NFA_HCI_CONNECTIVITY_GATE))
    {
        NFA_TRACE_API1 ("NFA_HciDeallocGate (): Cannot deallocate the gate:0x%02x", gate);
        return (NFA_STATUS_FAILED);
    }

    NFA_TRACE_API2 ("NFA_HciDeallocGate (): hci_handle:0x%04x, gate:0x%02X", hci_handle, gate);

    /* Request HCI to deallocate the gate that was previously allocated to the application */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_DEALLOC_GATE *) GKI_getbuf (sizeof (tNFA_HCI_API_DEALLOC_GATE))) != NULL) )
    {
        p_msg->hdr.event  = NFA_HCI_API_DEALLOC_GATE_EVT;
        p_msg->hci_handle = hci_handle;
        p_msg->gate       = gate;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }
    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciGetGateAndPipeList
**
** Description      This function will get the list of gates allocated to the
**                  application and list of dynamic pipes created by the
**                  application. The app will be notified with
**                  NFA_HCI_GET_GATE_PIPE_LIST_EVT. List of allocated dynamic
**                  gates to the application and list of pipes created by the
**                  application will be returned as part of
**                  tNFA_HCI_GET_GATE_PIPE_LIST data.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciGetGateAndPipeList (tNFA_HANDLE hci_handle)
{
    tNFA_HCI_API_GET_APP_GATE_PIPE *p_msg;

    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciGetGateAndPipeList (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    NFA_TRACE_API1 ("NFA_HciGetGateAndPipeList (): hci_handle:0x%04x", hci_handle);

    /* Register the application with HCI */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_GET_APP_GATE_PIPE *) GKI_getbuf (sizeof (tNFA_HCI_API_GET_APP_GATE_PIPE))) != NULL))
    {
        p_msg->hdr.event  = NFA_HCI_API_GET_APP_GATE_PIPE_EVT;
        p_msg->hci_handle = hci_handle;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciSendEvent
**
** Description      This function is called to send any event on a pipe created
**                  by the application.
**                  The app will be notified by NFA_HCI_EVENT_SENT_EVT
**                  after successfully sending the event on the specified pipe
**                  or if an error occurs. The application should wait for this
**                  event before releasing event buffer passed as argument.
**                  If the app is expecting a response to the event then it can
**                  provide response buffer for collecting the response. If it
**                  provides a response buffer it can also provide response
**                  timeout indicating maximum timeout for the response.
**                  Maximum of NFA_MAX_HCI_EVENT_LEN bytes APDU can be received
**                  using internal buffer if no response buffer is provided by
**                  the application. The app will be notified by
**                  NFA_HCI_EVENT_RCVD_EVT after receiving the response event
**                  or on timeout if app provided response buffer and response
**                  timeout. If response buffer and response timeout is provided
**                  by the application, it should wait for this event before
**                  releasing the response buffer. If the application did not
**                  provide response timeout then it should not release the
**                  response buffer until it receives NFA_HCI_EVENT_RCVD_EVT or
**                  after timeout it sends next event on the same pipe
**                  and receives NFA_HCI_EVENT_SENT_EVT for that event.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciSendEvent (tNFA_HANDLE  hci_handle,
                              UINT8        pipe,
                              UINT8        evt_code,
                              UINT16       evt_size,
                              UINT8        *p_data,
                              UINT16       rsp_size,
                              UINT8        *p_rsp_buf,
                              UINT16       rsp_timeout)
{
    tNFA_HCI_API_SEND_EVENT_EVT *p_msg;

    NFA_TRACE_API3 ("NFA_HciSendEvent(): hci_handle:0x%04x, pipe:0x%02x  Code: 0x%02x", hci_handle, pipe, evt_code);


    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciSendEvent (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    if (pipe < NFA_HCI_FIRST_DYNAMIC_PIPE)
    {
        NFA_TRACE_API1 ("NFA_HciSendEvent (): Invalid Pipe:0x%02x", pipe);
        return (NFA_STATUS_FAILED);
    }

    if (evt_size && (p_data == NULL))
    {
        NFA_TRACE_API1 ("NFA_HciSendEvent (): Invalid Event size:0x%02x", evt_size);
        return (NFA_STATUS_FAILED);
    }

    if (rsp_size && (p_rsp_buf == NULL))
    {
        NFA_TRACE_API1 ("NFA_HciSendEvent (): No Event buffer, but invalid event buffer size :%u", rsp_size);
        return (NFA_STATUS_FAILED);
    }

    /* Request HCI to post event data on a particular pipe */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_SEND_EVENT_EVT *) GKI_getbuf (sizeof (tNFA_HCI_API_SEND_EVENT_EVT))) != NULL) )
    {
        p_msg->hdr.event    = NFA_HCI_API_SEND_EVENT_EVT;
        p_msg->hci_handle   = hci_handle;
        p_msg->pipe         = pipe;
        p_msg->evt_code     = evt_code;
        p_msg->evt_len      = evt_size;
        p_msg->p_evt_buf    = p_data;
        p_msg->rsp_len      = rsp_size;
        p_msg->p_rsp_buf    = p_rsp_buf;
        p_msg->rsp_timeout  = rsp_timeout;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciCreatePipe
**
** Description      This function is called to create a dynamic pipe with the
**                  specified host. When the dynamic pipe is created (or
**                  if an error occurs), the app will be notified with
**                  NFA_HCI_CREATE_PIPE_EVT with the pipe id. If a pipe exists
**                  between the two gates passed as argument and if it was
**                  created earlier by the calling application then the pipe
**                  id of the existing pipe will be returned and a new pipe
**                  will not be created. After successful creation of pipe,
**                  registry entry will be created for the dynamic pipe and
**                  all information related to the pipe will be stored in non
**                  volatile memory.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciCreatePipe (tNFA_HANDLE  hci_handle,
                               UINT8        source_gate_id,
                               UINT8        dest_host,
                               UINT8        dest_gate)
{
    tNFA_HCI_API_CREATE_PIPE_EVT *p_msg;
    UINT8                        xx;

    NFA_TRACE_API4 ("NFA_HciCreatePipe (): hci_handle:0x%04x, source gate:0x%02X, destination host:0x%02X , destination gate:0x%02X",
                                         hci_handle, source_gate_id, dest_host, dest_gate);

    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciCreatePipe (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    if ((source_gate_id < NFA_HCI_FIRST_HOST_SPECIFIC_GENERIC_GATE) || (source_gate_id > NFA_HCI_LAST_PROP_GATE))
    {
        NFA_TRACE_API1 ("NFA_HciCreatePipe (): Invalid local Gate:0x%02x", source_gate_id);
        return (NFA_STATUS_FAILED);
    }

    if (  ((dest_gate < NFA_HCI_FIRST_HOST_SPECIFIC_GENERIC_GATE) && (dest_gate != NFA_HCI_LOOP_BACK_GATE) && (dest_gate != NFA_HCI_IDENTITY_MANAGEMENT_GATE))
        ||(dest_gate > NFA_HCI_LAST_PROP_GATE))
    {
        NFA_TRACE_API1 ("NFA_HciCreatePipe (): Invalid Destination Gate:0x%02x", dest_gate);
        return (NFA_STATUS_FAILED);
    }

    for (xx = 0; xx < NFA_HCI_MAX_HOST_IN_NETWORK; xx++)
        if (nfa_hci_cb.inactive_host[xx] == dest_host)
            break;

    if (xx != NFA_HCI_MAX_HOST_IN_NETWORK)
    {
        NFA_TRACE_API1 ("NFA_HciCreatePipe (): Host not active:0x%02x", dest_host);
        return (NFA_STATUS_FAILED);
    }

    /* Request HCI to create a pipe between two specified gates */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&(!nfa_hci_cb.b_low_power_mode)
        &&((p_msg = (tNFA_HCI_API_CREATE_PIPE_EVT *) GKI_getbuf (sizeof (tNFA_HCI_API_CREATE_PIPE_EVT))) != NULL) )
    {
        p_msg->hdr.event    = NFA_HCI_API_CREATE_PIPE_EVT;
        p_msg->hci_handle   = hci_handle;
        p_msg->source_gate  = source_gate_id;
        p_msg->dest_host    = dest_host;        /* Host id of the destination host */
        p_msg->dest_gate    = dest_gate;        /* Gate id of the destination gate */

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }
    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciAddStaticPipe
**
** Description      This function is called to add a static pipe for sending
**                  7816 APDUs. When the static pipe is added (or if an error occurs),
**                  the app will be notified with NFA_HCI_ADD_STATIC_PIPE_EVT with
**                  the status.
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciAddStaticPipe (tNFA_HANDLE hci_handle, UINT8 host, UINT8 gate, UINT8 pipe)
{
    tNFA_HCI_API_ADD_STATIC_PIPE_EVT *p_msg;
    UINT8                            xx;

    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciAddStaticPipe (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    for (xx = 0; xx < NFA_HCI_MAX_HOST_IN_NETWORK; xx++)
        if (nfa_hci_cb.inactive_host[xx] == host)
            break;

    if (xx != NFA_HCI_MAX_HOST_IN_NETWORK)
    {
        NFA_TRACE_API1 ("NFA_HciAddStaticPipe (): Host not active:0x%02x", host);
        return (NFA_STATUS_FAILED);
    }

    if (gate <= NFA_HCI_LAST_HOST_SPECIFIC_GATE)
    {
        NFA_TRACE_API1 ("NFA_HciAddStaticPipe (): Invalid Gate:0x%02x", gate);
        return (NFA_STATUS_FAILED);
    }
#if(NFC_NXP_NOT_OPEN_INCLUDED != TRUE)
    if (pipe <= NFA_HCI_LAST_DYNAMIC_PIPE)
    {
        NFA_TRACE_API1 ("NFA_HciAddStaticPipe (): Invalid Pipe:0x%02x", pipe);
        return (NFA_STATUS_FAILED);
    }
#endif
    NFA_TRACE_API2 ("NFA_HciAddStaticPipe (): hci_handle:0x%04x, pipe:0x%02X", hci_handle, pipe);

    /* Request HCI to delete a pipe created by the application identified by hci handle */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_ADD_STATIC_PIPE_EVT *) GKI_getbuf (sizeof (tNFA_HCI_API_ADD_STATIC_PIPE_EVT))) != NULL)  )
    {
        p_msg->hdr.event    = NFA_HCI_API_ADD_STATIC_PIPE_EVT;
        p_msg->hci_handle   = hci_handle;
        p_msg->host         = host;
        p_msg->gate         = gate;
        p_msg->pipe         = pipe;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }
    /* Unable to add static pipe */
    return (NFA_STATUS_FAILED);
}
Esempio n. 11
0
/*******************************************************************************
**
** Function         NFA_EeDisconnect
**
** Description      Disconnect (if a connection is currently open) from an
**                  NFCEE interface. The result of this operation is reported
**                  with the NFA_EE_DISCONNECT_EVT.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**                  NFA_STATUS_INVALID_PARAM If bad parameter
**
*******************************************************************************/
tNFA_STATUS NFA_EeDisconnect(tNFA_HANDLE ee_handle)
{
    tNFA_EE_API_DISCONNECT *p_msg;
    tNFA_STATUS status = NFA_STATUS_FAILED;
    UINT8       nfcee_id = (UINT8)(ee_handle & 0xFF);
    tNFA_EE_ECB *p_cb;

    NFA_TRACE_API1 ("NFA_EeDisconnect(): handle:<0x%x>", ee_handle);
    p_cb = nfa_ee_find_ecb (nfcee_id);

    if ((p_cb == NULL) || (p_cb->conn_st != NFA_EE_CONN_ST_CONN))
    {
        NFA_TRACE_ERROR0 ("NFA_EeDisconnect() Bad ee_handle");
        status = NFA_STATUS_INVALID_PARAM;
    }
    else if ((p_msg = (tNFA_EE_API_DISCONNECT *) GKI_getbuf (sizeof(tNFA_EE_API_DISCONNECT))) != NULL)
    {
        p_msg->hdr.event        = NFA_EE_API_DISCONNECT_EVT;
        p_msg->nfcee_id         = nfcee_id;
        p_msg->p_cb             = p_cb;

        nfa_sys_sendmsg (p_msg);

        status = NFA_STATUS_OK;
    }

    return status;
}
/*******************************************************************************
**
** Function         NFA_RegVSCback
**
** Description      This function is called to register or de-register a callback
**                  function to receive Proprietary NCI response and notification
**                  events.
**                  The maximum number of callback functions allowed is NFC_NUM_VS_CBACKS
**
** Returns          tNFC_STATUS
**
*******************************************************************************/
tNFC_STATUS NFA_RegVSCback (BOOLEAN          is_register,
                            tNFA_VSC_CBACK   *p_cback)
{
    tNFA_DM_API_REG_VSC *p_msg;

    NFA_TRACE_API1 ("NFA_RegVSCback() is_register=%d", is_register);

    if (p_cback == NULL)
    {
        NFA_TRACE_ERROR0 ("NFA_RegVSCback() requires a valid callback function");
        return (NFA_STATUS_FAILED);
    }

    if ((p_msg = (tNFA_DM_API_REG_VSC *) GKI_getbuf (sizeof(tNFA_DM_API_REG_VSC))) != NULL)
    {
        p_msg->hdr.event        = NFA_DM_API_REG_VSC_EVT;
        p_msg->is_register      = is_register;
        p_msg->p_cback          = p_cback;

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
Esempio n. 13
0
/*******************************************************************************
**
** Function         NFA_EeSendData
**
** Description      Send data to the given NFCEE.
**                  This function shall be called after NFA_EE_CONNECT_EVT is reported
**                  and before NFA_EeDisconnect is called on the given ee_handle.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**                  NFA_STATUS_INVALID_PARAM If bad parameter
**
*******************************************************************************/
tNFA_STATUS NFA_EeSendData (tNFA_HANDLE  ee_handle,
                            UINT16       data_len,
                            UINT8       *p_data)
{
    tNFA_EE_API_SEND_DATA *p_msg;
    tNFA_STATUS status = NFA_STATUS_FAILED;
    UINT8       nfcee_id = (UINT8)(ee_handle & 0xFF);
    tNFA_EE_ECB *p_cb;

    NFA_TRACE_API1 ("NFA_EeSendData(): handle:<0x%x>", ee_handle);

    p_cb = nfa_ee_find_ecb (nfcee_id);

    if ((p_cb == NULL) || (p_cb->conn_st != NFA_EE_CONN_ST_CONN) || (p_data == NULL))
    {
        NFA_TRACE_ERROR0 ("Bad ee_handle or NULL data");
        status = NFA_STATUS_INVALID_PARAM;
    }
    else if ((p_msg = (tNFA_EE_API_SEND_DATA *) GKI_getbuf ((UINT16)(sizeof(tNFA_EE_API_SEND_DATA) + data_len))) != NULL)
    {
        p_msg->hdr.event        = NFA_EE_API_SEND_DATA_EVT;
        p_msg->nfcee_id         = nfcee_id;
        p_msg->p_cb             = p_cb;
        p_msg->data_len         = data_len;
        p_msg->p_data           = (UINT8 *)(p_msg + 1);
        memcpy(p_msg->p_data, p_data, data_len);

        nfa_sys_sendmsg (p_msg);

        status = NFA_STATUS_OK;
    }

    return status;
}
/*******************************************************************************
**
** Function         NFA_SendRawFrame
**
** Description      Send a raw frame over the activated interface with the NFCC.
**                  This function can only be called after NFC link is activated.
**
**                  If the activated interface is a tag and auto-presence check is
**                  enabled then presence_check_start_delay can be used to indicate
**                  the delay in msec after which the next auto presence check
**                  command can be sent. NFA_DM_DEFAULT_PRESENCE_CHECK_START_DELAY
**                  can be used as the default value for the delay.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_SendRawFrame (UINT8  *p_raw_data,
                              UINT16  data_len,
                              UINT16  presence_check_start_delay)
{
    BT_HDR *p_msg;
    UINT16  size;
    UINT8  *p;

    NFA_TRACE_API1 ("NFA_SendRawFrame () data_len:%d", data_len);

    /* Validate parameters */
    if ((data_len == 0) || (p_raw_data == NULL))
        return (NFA_STATUS_INVALID_PARAM);

    size = BT_HDR_SIZE + NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE + data_len;
    if ((p_msg = (BT_HDR *) GKI_getbuf (size)) != NULL)
    {
        p_msg->event  = NFA_DM_API_RAW_FRAME_EVT;
        p_msg->layer_specific = presence_check_start_delay;
        p_msg->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
        p_msg->len    = data_len;

        p = (UINT8 *) (p_msg + 1) + p_msg->offset;
        memcpy (p, p_raw_data, data_len);

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_RequestExclusiveRfControl
**
** Description      Request exclusive control of NFC.
**                  - Previous behavior (polling/tag reading, DH card emulation)
**                    will be suspended .
**                  - Polling and listening will be done based on the specified
**                    params
**
**                  The NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT event of
**                  tNFA_CONN_CBACK indicates the status of the operation.
**
**                  NFA_ACTIVATED_EVT and NFA_DEACTIVATED_EVT indicates link
**                  activation/deactivation.
**
**                  NFA_SendRawFrame is used to send data to the peer. NFA_DATA_EVT
**                  indicates data from the peer.
**
**                  If a tag is activated, then the NFA_RW APIs may be used to
**                  send commands to the tag. Incoming NDEF messages are sent to
**                  the NDEF callback.
**
**                  Once exclusive RF control has started, NFA will not activate
**                  LLCP internally. The application has exclusive control of
**                  the link.
**
** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
**                  should happen before calling this function
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RequestExclusiveRfControl  (tNFA_TECHNOLOGY_MASK poll_mask,
                                            tNFA_LISTEN_CFG      *p_listen_cfg,
                                            tNFA_CONN_CBACK      *p_conn_cback,
                                            tNFA_NDEF_CBACK      *p_ndef_cback)
{
    tNFA_DM_API_REQ_EXCL_RF_CTRL *p_msg;

    NFA_TRACE_API1 ("NFA_RequestExclusiveRfControl () poll_mask=0x%x", poll_mask);

    if (!p_conn_cback)
    {
        NFA_TRACE_ERROR0 ("NFA_RequestExclusiveRfControl (): error null callback");
        return (NFA_STATUS_FAILED);
    }

    if ((p_msg = (tNFA_DM_API_REQ_EXCL_RF_CTRL *) GKI_getbuf (sizeof (tNFA_DM_API_REQ_EXCL_RF_CTRL))) != NULL)
    {
        p_msg->hdr.event = NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT;
        p_msg->poll_mask    = poll_mask;
        p_msg->p_conn_cback = p_conn_cback;
        p_msg->p_ndef_cback = p_ndef_cback;

        if (p_listen_cfg)
            memcpy (&p_msg->listen_cfg, p_listen_cfg, sizeof (tNFA_LISTEN_CFG));
        else
            memset (&p_msg->listen_cfg, 0x00, sizeof (tNFA_LISTEN_CFG));

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_GetConfig
**
** Description      Get the configuration parameters from NFCC. The result is
**                  reported with an NFA_DM_GET_CONFIG_EVT in the tNFA_DM_CBACK
**                  callback.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_GetConfig (UINT8 num_ids,
                           tNFA_PMID *p_param_ids)
{
    tNFA_DM_API_GET_CONFIG *p_msg;

    NFA_TRACE_API1 ("NFA_GetConfig (): num_ids: %i", num_ids);


    if ((p_msg = (tNFA_DM_API_GET_CONFIG *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_GET_CONFIG) + num_ids))) != NULL)
    {
        p_msg->hdr.event = NFA_DM_API_GET_CONFIG_EVT;

        p_msg->num_ids = num_ids;
        p_msg->p_pmids = (tNFA_PMID *) (p_msg+1);

        /* Copy the param IDs */
        memcpy (p_msg->p_pmids, p_param_ids, num_ids);

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_SetConfig
**
** Description      Set the configuration parameters to NFCC. The result is
**                  reported with an NFA_DM_SET_CONFIG_EVT in the tNFA_DM_CBACK
**                  callback.
**
** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
**                  should happen before calling this function. Most Configuration
**                  parameters are related to RF discovery.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_BUSY if previous setting is on-going
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_SetConfig (tNFA_PMID param_id,
                           UINT8     length,
                           UINT8    *p_data)
{
    tNFA_DM_API_SET_CONFIG *p_msg;

    NFA_TRACE_API1 ("NFA_SetConfig (): param_id:0x%X", param_id);

    if ((p_msg = (tNFA_DM_API_SET_CONFIG *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_SET_CONFIG) + length))) != NULL)
    {
        p_msg->hdr.event = NFA_DM_API_SET_CONFIG_EVT;

        p_msg->param_id = param_id;
        p_msg->length   = length;
        p_msg->p_data   = (UINT8 *) (p_msg + 1);

        /* Copy parameter data */
        memcpy (p_msg->p_data, p_data, length);

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_SendVsCommand
**
** Description      This function is called to send an NCI Vendor Specific
**                  command to NFCC.
**
**                  oid             - The opcode of the VS command.
**                  cmd_params_len  - The command parameter len
**                  p_cmd_params    - The command parameter
**                  p_cback         - The callback function to receive the command
**                                    status
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_SendVsCommand (UINT8            oid,
                               UINT8            cmd_params_len,
                               UINT8            *p_cmd_params,
                               tNFA_VSC_CBACK    *p_cback)
{
    tNFA_DM_API_SEND_VSC *p_msg;
    UINT16  size = sizeof(tNFA_DM_API_SEND_VSC) + cmd_params_len;

    NFA_TRACE_API1 ("NFA_SendVsCommand() oid=0x%x", oid);

    if ((p_msg = (tNFA_DM_API_SEND_VSC *) GKI_getbuf (size)) != NULL)
    {
        p_msg->hdr.event        = NFA_DM_API_SEND_VSC_EVT;
        p_msg->oid              = oid;
        p_msg->p_cback          = p_cback;
        if (cmd_params_len && p_cmd_params)
        {
            p_msg->cmd_params_len   = cmd_params_len;
            p_msg->p_cmd_params     = (UINT8 *)(p_msg + 1);
            memcpy (p_msg->p_cmd_params, p_cmd_params, cmd_params_len);
        }
        else
        {
            p_msg->cmd_params_len   = 0;
            p_msg->p_cmd_params     = NULL;
        }

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
Esempio n. 19
0
/*******************************************************************************
**
** Function         NFA_CeSetIsoDepListenTech
**
** Description      Set the technologies (NFC-A and/or NFC-B) to listen for when
**                  NFA_CeConfigureLocalTag or NFA_CeDeregisterAidOnDH are called.
**
**                  By default (if this API is not called), NFA will listen
**                  for both NFC-A and NFC-B for ISODEP.
**
** Note:            If listening for ISODEP on UICC, the DH listen callbacks
**                  may still get activate notifications for ISODEP if the reader/
**                  writer selects an AID that is not routed to the UICC (regardless
**                  of whether A or B was disabled using NFA_CeSetIsoDepListenTech)
**
** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
**                  should happen before calling this function
**
** Returns:
**                  NFA_STATUS_OK, if command accepted
**                  NFA_STATUS_FAILED: otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_CeSetIsoDepListenTech (tNFA_TECHNOLOGY_MASK tech_mask)
{
    tNFA_CE_MSG *p_msg;
    tNFA_TECHNOLOGY_MASK    use_mask = (NFA_TECHNOLOGY_MASK_A | NFA_TECHNOLOGY_MASK_B);

    NFA_TRACE_API1 ("NFA_CeSetIsoDepListenTech (): 0x%x", tech_mask);
    if (((tech_mask & use_mask) == 0) ||
        ((tech_mask & ~use_mask) != 0) )
    {
        NFA_TRACE_ERROR0 ("NFA_CeSetIsoDepListenTech: Invalid technology mask");
        return (NFA_STATUS_INVALID_PARAM);
    }

    if ((p_msg = (tNFA_CE_MSG *) GKI_getbuf ((UINT16) sizeof(tNFA_CE_MSG))) != NULL)
    {
        p_msg->hdr.event            = NFA_CE_API_CFG_ISODEP_TECH_EVT;
        p_msg->hdr.layer_specific   = tech_mask & use_mask;

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
Esempio n. 20
0
/*******************************************************************************
**
** Function         NFA_CeConfigureEseListenTech
**
** Description      Configure listening for the Ese, using the specified
**                  technologies.
**
**                  Events will be notifed using the tNFA_CONN_CBACK
**                  (registered during NFA_Enable)
**
**                  The NFA_CE_ESE_LISTEN_CONFIGURED_EVT reports the status of the
**                  operation.
**
**                  Activation and deactivation are reported using the
**                  NFA_ACTIVATED_EVT and NFA_DEACTIVATED_EVT events
**
** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
**                  should happen before calling this function
**
** Returns:
**                  NFA_STATUS_OK, if command accepted
**                  NFA_STATUS_FAILED: otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_CeConfigureEseListenTech (tNFA_HANDLE ee_handle,
                                           tNFA_TECHNOLOGY_MASK tech_mask)
{
#if (NFC_NFCEE_INCLUDED == TRUE)
    tNFA_CE_MSG *p_msg;

    NFA_TRACE_API1 ("NFA_CeConfigureEseListenTech () ee_handle = 0x%x", ee_handle);

    /* If tech_mask is zero, then app is disabling listening for specified uicc */
    if (tech_mask == 0)
    {
        return (nfa_ce_api_deregister_listen (ee_handle, NFA_CE_LISTEN_INFO_ESE));
    }

    /* Otherwise then app is configuring ese listen for the specificed technologies */
    if ((p_msg = (tNFA_CE_MSG *) GKI_getbuf ((UINT16) sizeof(tNFA_CE_MSG))) != NULL)
    {
        p_msg->reg_listen.hdr.event   = NFA_CE_API_REG_LISTEN_EVT;
        p_msg->reg_listen.listen_type = NFA_CE_REG_TYPE_ESE;

        p_msg->reg_listen.ee_handle   = ee_handle;
        p_msg->reg_listen.tech_mask   = tech_mask;

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }
#else
    NFA_TRACE_ERROR0 ("NFA_CeConfigureEseListenTech () NFCEE related functions are not enabled!");
#endif
    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_PowerOffSleepMode
**
** Description      This function is called to enter or leave NFCC Power Off Sleep mode
**                  NFA_DM_PWR_MODE_CHANGE_EVT will be sent to indicate status.
**
**                  start_stop : TRUE if entering Power Off Sleep mode
**                               FALSE if leaving Power Off Sleep mode
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_PowerOffSleepMode (BOOLEAN start_stop)
{
    BT_HDR *p_msg;

    NFA_TRACE_API1 ("NFA_PowerOffSleepState () start_stop=%d", start_stop);

    if (nfa_dm_cb.flags & NFA_DM_FLAGS_SETTING_PWR_MODE)
    {
        NFA_TRACE_ERROR0 ("NFA_PowerOffSleepState (): NFA DM is busy to update power mode");
        return (NFA_STATUS_FAILED);
    }
    else
    {
        nfa_dm_cb.flags |= NFA_DM_FLAGS_SETTING_PWR_MODE;
    }

    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
    {
        p_msg->event          = NFA_DM_API_POWER_OFF_SLEEP_EVT;
        p_msg->layer_specific = start_stop;

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_RwT3tRead
**
** Description:
**      Send a CHECK (read) command to the activated Type 3 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the read
**      operation has completed, or if an error occurs, the app will be notified with
**      NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT3tRead (UINT8 num_blocks, tNFA_T3T_BLOCK_DESC *t3t_blocks)
{
    tNFA_RW_OPERATION *p_msg;
    UINT8 *p_block_desc;

    NFA_TRACE_API1 ("NFA_RwT3tRead (): num_blocks to read: %i", num_blocks);

    /* Validate parameters */
    if ((num_blocks == 0) || (t3t_blocks == NULL))
        return (NFA_STATUS_INVALID_PARAM);

    if ((p_msg = (tNFA_RW_OPERATION *) GKI_getbuf ((UINT16) (sizeof (tNFA_RW_OPERATION) + (num_blocks * sizeof (tNFA_T3T_BLOCK_DESC))))) != NULL)
    {
        /* point to area after tNFA_RW_OPERATION */
        p_block_desc = (UINT8 *) (p_msg+1);

        /* Fill in tNFA_RW_OPERATION struct */
        p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
        p_msg->op        = NFA_RW_OP_T3T_READ;

        p_msg->params.t3t_read.num_blocks   = num_blocks;
        p_msg->params.t3t_read.p_block_desc = (tNFA_T3T_BLOCK_DESC *) p_block_desc;

        /* Copy block descriptor list */
        memcpy (p_block_desc, t3t_blocks, (num_blocks * sizeof (tNFA_T3T_BLOCK_DESC)));

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_RwI93ReadSingleBlock
**
** Description:
**      Send Read Single Block command to the activated ISO 15693 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the read
**      operation has completed, or if an error occurs, the app will be notified with
**      NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: ISO 15693 tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93ReadSingleBlock (UINT8 block_number)
{
    tNFA_RW_OPERATION *p_msg;

    NFA_TRACE_API1 ("NFA_RwI93ReadSingleBlock (): block_number: 0x%02X", block_number);

    if (nfa_rw_cb.protocol != NFC_PROTOCOL_15693)
    {
        return (NFA_STATUS_WRONG_PROTOCOL);
    }

    if ((p_msg = (tNFA_RW_OPERATION *) GKI_getbuf ((UINT16) (sizeof (tNFA_RW_OPERATION)))) != NULL)
    {
        /* Fill in tNFA_RW_OPERATION struct */
        p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
        p_msg->op        = NFA_RW_OP_I93_READ_SINGLE_BLOCK;

        p_msg->params.i93_cmd.first_block_number = block_number;

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_RwI93WriteDSFID
**
** Description:
**      Send Write DSFID command to the activated ISO 15693 tag.
**
**      When the operation has completed (or if an error occurs), the
**      app will be notified with NFA_I93_CMD_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_WRONG_PROTOCOL: ISO 15693 tag not activated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwI93WriteDSFID (UINT8 dsfid)
{
    tNFA_RW_OPERATION *p_msg;

    NFA_TRACE_API1 ("NFA_RwI93WriteDSFID (): DSFID: 0x%02X", dsfid);

    if (nfa_rw_cb.protocol != NFC_PROTOCOL_15693)
    {
        return (NFA_STATUS_WRONG_PROTOCOL);
    }

    if ((p_msg = (tNFA_RW_OPERATION *) GKI_getbuf ((UINT16) (sizeof (tNFA_RW_OPERATION)))) != NULL)
    {
        /* Fill in tNFA_RW_OPERATION struct */
        p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
        p_msg->op        = NFA_RW_OP_I93_WRITE_DSFID;

        p_msg->params.i93_cmd.dsfid = dsfid;

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciSendCommand
**
** Description      This function is called to send a command on a pipe created
**                  by the application.
**                  The app will be notified by NFA_HCI_CMD_SENT_EVT if an error
**                  occurs.
**                  When the peer host responds,the app is notified with
**                  NFA_HCI_RSP_RCVD_EVT
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_HciSendCommand (tNFA_HANDLE  hci_handle,
                              UINT8        pipe,
                              UINT8        cmd_code,
                              UINT16       cmd_size,
                              UINT8        *p_data)
{
    tNFA_HCI_API_SEND_CMD_EVT *p_msg;

    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciSendCommand (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    if (pipe < NFA_HCI_FIRST_DYNAMIC_PIPE)
    {
        NFA_TRACE_API1 ("NFA_HciSendCommand (): Invalid Pipe:0x%02x", pipe);
        return (NFA_STATUS_FAILED);
    }

    if ((cmd_size && (p_data == NULL)) || (cmd_size > NFA_MAX_HCI_CMD_LEN))
    {
        NFA_TRACE_API1 ("NFA_HciSendCommand (): Invalid cmd size:0x%02x", cmd_size);
        return (NFA_STATUS_FAILED);
    }

    NFA_TRACE_API3 ("NFA_HciSendCommand (): hci_handle:0x%04x, pipe:0x%02x  Code: 0x%02x", hci_handle, pipe, cmd_code);

    /* Request HCI to post event data on a particular pipe */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_SEND_CMD_EVT *) GKI_getbuf (sizeof (tNFA_HCI_API_SEND_CMD_EVT))) != NULL) )
    {
        p_msg->hdr.event    = NFA_HCI_API_SEND_CMD_EVT;
        p_msg->hci_handle   = hci_handle;
        p_msg->pipe         = pipe;
        p_msg->cmd_code     = cmd_code;
        p_msg->cmd_len      = cmd_size;

        if (cmd_size)
            memcpy (p_msg->data, p_data, cmd_size);

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciSetRegistry
**
** Description      This function requests a peer host to set the desired
**                  registry field value for the gate that the pipe is on.
**
**                  When the peer host responds,the app is notified with
**                  NFA_HCI_SET_REG_RSP_EVT or
**                  if an error occurs in sending the command the app will be
**                  notified by NFA_HCI_CMD_SENT_EVT
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
NFC_API extern tNFA_STATUS NFA_HciSetRegistry (tNFA_HANDLE   hci_handle,
                                               UINT8         pipe,
                                               UINT8         reg_inx,
                                               UINT8         data_size,
                                               UINT8         *p_data)
{
    tNFA_HCI_API_SET_REGISTRY *p_msg;


    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciSetRegistry (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    if (pipe < NFA_HCI_FIRST_DYNAMIC_PIPE)
    {
        NFA_TRACE_API1 ("NFA_HciSetRegistry (): Invalid Pipe:0x%02x", pipe);
        return (NFA_STATUS_FAILED);
    }

    if ((data_size == 0) || (p_data == NULL) || (data_size > NFA_MAX_HCI_CMD_LEN))
    {
        NFA_TRACE_API1 ("NFA_HciSetRegistry (): Invalid data size:0x%02x", data_size);
        return (NFA_STATUS_FAILED);
    }

    NFA_TRACE_API2 ("NFA_HciSetRegistry (): hci_handle:0x%04x  Pipe: 0x%02x", hci_handle, pipe);

    /* Request HCI to get list of gates supported by the specified host */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_SET_REGISTRY *) GKI_getbuf (sizeof (tNFA_HCI_API_SET_REGISTRY))) != NULL) )
    {
        p_msg->hdr.event    = NFA_HCI_API_SET_REGISTRY_EVT;
        p_msg->hci_handle   = hci_handle;
        p_msg->pipe         = pipe;
        p_msg->reg_inx      = reg_inx;
        p_msg->size         = data_size;

        memcpy (p_msg->data, p_data, data_size);
        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciSendResponse
**
** Description      This function is called to send a response on a pipe created
**                  by the application.
**                  The app will be notified by NFA_HCI_RSP_SENT_EVT if an error
**                  occurs.
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
NFC_API extern tNFA_STATUS NFA_HciSendResponse (tNFA_HANDLE   hci_handle,
                                                UINT8         pipe,
                                                UINT8         response,
                                                UINT8         data_size,
                                                UINT8         *p_data)
{
    tNFA_HCI_API_SEND_RSP_EVT *p_msg;

    if ((NFA_HANDLE_GROUP_MASK & hci_handle) != NFA_HANDLE_GROUP_HCI)
    {
        NFA_TRACE_API1 ("NFA_HciSendResponse (): Invalid hci_handle:0x%04x", hci_handle);
        return (NFA_STATUS_FAILED);
    }

    if (pipe < NFA_HCI_FIRST_DYNAMIC_PIPE)
    {
        NFA_TRACE_API1 ("NFA_HciSendResponse (): Invalid Pipe:0x%02x", pipe);
        return (NFA_STATUS_FAILED);
    }

    if ((data_size && (p_data == NULL)) || (data_size > NFA_MAX_HCI_RSP_LEN))
    {
        NFA_TRACE_API1 ("NFA_HciSendResponse (): Invalid data size:0x%02x", data_size);
        return (NFA_STATUS_FAILED);
    }

    NFA_TRACE_API3 ("NFA_HciSendResponse (): hci_handle:0x%04x  Pipe: 0x%02x  Response: 0x%02x", hci_handle, pipe, response);

    /* Request HCI to get list of gates supported by the specified host */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_SEND_RSP_EVT *) GKI_getbuf (sizeof (tNFA_HCI_API_SEND_RSP_EVT))) != NULL) )
    {
        p_msg->hdr.event    = NFA_HCI_API_SEND_RSP_EVT;
        p_msg->hci_handle   = hci_handle;
        p_msg->response     = response;
        p_msg->size         = data_size;

        if (data_size)
            memcpy (p_msg->data, p_data, data_size);

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_HciDeregister
**
** Description      This function is called to deregister an application
**                  from HCI. The app will be notified by NFA_HCI_DEREGISTER_EVT
**                  after deleting all the pipes owned by the app and deallocating
**                  all the gates allocated to the app or if an error occurs.
**                  Even if deregistration fails, the app has to register again
**                  to provide a new cback function.
**
** Returns          NFA_STATUS_OK if the application is deregistered successfully
**                  NFA_STATUS_FAILED otherwise

*******************************************************************************/
tNFA_STATUS NFA_HciDeregister (char *p_app_name)
{
    tNFA_HCI_API_DEREGISTER_APP *p_msg;
    int                         xx;
    UINT8                       app_name_len;

    if (p_app_name == NULL)
    {
        NFA_TRACE_API0 ("NFA_HciDeregister (): Invalid Application");
        return (NFA_STATUS_FAILED);
    }

    NFA_TRACE_API1 ("NFA_HciDeregister (): Application Name: %s", p_app_name);
    app_name_len = (UINT8) strlen (p_app_name);

    if (app_name_len > NFA_MAX_HCI_APP_NAME_LEN)
        return (NFA_STATUS_FAILED);

    /* Find the application registration */
    for (xx = 0; xx < NFA_HCI_MAX_APP_CB; xx++)
    {
        if (  (nfa_hci_cb.cfg.reg_app_names[xx][0] != 0)
            &&(!strncmp (p_app_name, &nfa_hci_cb.cfg.reg_app_names[xx][0], app_name_len)) )
            break;
    }

    if (xx == NFA_HCI_MAX_APP_CB)
    {
        NFA_TRACE_ERROR1 ("NFA_HciDeregister (): Application Name: %s  NOT FOUND", p_app_name);
        return (NFA_STATUS_FAILED);
    }

    /* Deregister the application with HCI */
    if (  (nfa_hci_cb.hci_state != NFA_HCI_STATE_DISABLED)
        &&((p_msg = (tNFA_HCI_API_DEREGISTER_APP *) GKI_getbuf (sizeof (tNFA_HCI_API_DEREGISTER_APP))) != NULL) )
    {
        p_msg->hdr.event  = NFA_HCI_API_DEREGISTER_APP_EVT;

        memset (p_msg->app_name, 0, sizeof (p_msg->app_name));
        BCM_STRNCPY_S (p_msg->app_name, sizeof (p_msg->app_name), p_app_name, NFA_MAX_HCI_APP_NAME_LEN);

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_RwT2tRead
**
** Description:
**      Send a READ command to the activated Type 2 tag.
**
**      Data is returned to the application using the NFA_DATA_EVT. When the read
**      operation has completed, or if an error occurs, the app will be notified with
**      NFA_READ_CPLT_EVT.
**
** Returns:
**      NFA_STATUS_OK if successfully initiated
**      NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
tNFA_STATUS NFA_RwT2tRead (UINT8 block_number)
{
    tNFA_RW_OPERATION *p_msg;

    NFA_TRACE_API1 ("NFA_RwT2tRead (): Block to read: %d", block_number);

    if ((p_msg = (tNFA_RW_OPERATION *) GKI_getbuf ((UINT16) (sizeof (tNFA_RW_OPERATION)))) != NULL)
    {
        /* Fill in tNFA_RW_OPERATION struct */
        p_msg->hdr.event                    = NFA_RW_OP_REQUEST_EVT;
        p_msg->op                           = NFA_RW_OP_T2T_READ;
        p_msg->params.t2t_read.block_number = block_number;

        nfa_sys_sendmsg (p_msg);
        return (NFA_STATUS_OK);
    }
    return (NFA_STATUS_FAILED);
}
/*******************************************************************************
**
** Function         NFA_Deactivate
**
** Description
**                  If sleep_mode=TRUE:
**                      Deselect the activated device by deactivating into sleep mode.
**
**                      An NFA_DEACTIVATE_FAIL_EVT indicates that selection was not successful.
**                      Application can select another discovered device or deactivate by NFA_Deactivate ()
**                      after receiving NFA_DEACTIVATED_EVT.
**
**                      Deactivating to sleep mode is not allowed when NFCC is in wait-for-host-select
**                      mode, or in listen-sleep states; NFA will deactivate to idle or discovery state
**                      for these cases respectively.
**
**
**                  If sleep_mode=FALSE:
**                      Deactivate the connection (e.g. as a result of presence check failure)
**                      NFA_DEACTIVATED_EVT will indicate that link is deactivated.
**                      Polling/listening will resume (unless the nfcc is in wait_for-all-discoveries state)
**
**
** Returns          NFA_STATUS_OK if successfully initiated
**                  NFA_STATUS_FAILED otherwise
**
*******************************************************************************/
NFC_API extern tNFA_STATUS NFA_Deactivate (BOOLEAN sleep_mode)
{
    tNFA_DM_API_DEACTIVATE *p_msg;

    NFA_TRACE_API1 ("NFA_Deactivate (): sleep_mode:%i", sleep_mode);

    if ((p_msg = (tNFA_DM_API_DEACTIVATE *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_DEACTIVATE)))) != NULL)
    {
        p_msg->hdr.event    = NFA_DM_API_DEACTIVATE_EVT;
        p_msg->sleep_mode   = sleep_mode;

        nfa_sys_sendmsg (p_msg);

        return (NFA_STATUS_OK);
    }

    return (NFA_STATUS_FAILED);
}