Home | History | Annotate | Download | only in nfc
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 1999-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /******************************************************************************
     20  *
     21  *  This file contains functions that interface with the NFC NCI transport.
     22  *  On the receive side, it routes events to the appropriate handler
     23  *  (callback). On the transmit side, it manages the command transmission.
     24  *
     25  ******************************************************************************/
     26 #include "nfc_target.h"
     27 #include "bt_types.h"
     28 #include "nfc_api.h"
     29 
     30 #if (NFC_INCLUDED == TRUE)
     31 #include "nfc_int.h"
     32 
     33 
     34 /*******************************************************************************
     35 **
     36 ** Function         nfc_alloc_conn_cb
     37 **
     38 ** Description      This function is called to allocation a control block for
     39 **                  NCI logical connection
     40 **
     41 ** Returns          The allocated control block or NULL
     42 **
     43 *******************************************************************************/
     44 tNFC_CONN_CB * nfc_alloc_conn_cb (tNFC_CONN_CBACK *p_cback)
     45 {
     46     int xx, max = NCI_MAX_CONN_CBS;
     47     tNFC_CONN_CB *p_conn_cb = NULL;
     48 
     49     NFC_CHECK_MAX_CONN ();
     50     for (xx = 0; xx < max; xx++)
     51     {
     52         if (nfc_cb.conn_cb[xx].conn_id == NFC_ILLEGAL_CONN_ID)
     53         {
     54             nfc_cb.conn_cb[xx].conn_id  = NFC_PEND_CONN_ID; /* to indicate this cb is used */
     55             p_conn_cb                   = &nfc_cb.conn_cb[xx];
     56             p_conn_cb->p_cback          = p_cback;
     57             break;
     58         }
     59     }
     60     return p_conn_cb;
     61 }
     62 
     63 /*******************************************************************************
     64 **
     65 ** Function         nfc_set_conn_id
     66 **
     67 ** Description      This function is called to set the connection id to the
     68 **                  connection control block and the id mapping table
     69 **
     70 ** Returns          void
     71 **
     72 *******************************************************************************/
     73 void nfc_set_conn_id (tNFC_CONN_CB * p_cb, UINT8 conn_id)
     74 {
     75     UINT8   handle;
     76 
     77     if (p_cb == NULL)
     78         return;
     79 
     80     p_cb->conn_id           = conn_id;
     81     handle                  = (UINT8) (p_cb - nfc_cb.conn_cb + 1);
     82     nfc_cb.conn_id[conn_id] = handle;
     83     NFC_TRACE_DEBUG2 ("nfc_set_conn_id conn_id:%d, handle:%d", conn_id, handle);
     84 }
     85 
     86 /*******************************************************************************
     87 **
     88 ** Function         nfc_find_conn_cb_by_handle
     89 **
     90 ** Description      This function is called to locate the control block for
     91 **                  loopback test.
     92 **
     93 ** Returns          The loopback test control block or NULL
     94 **
     95 *******************************************************************************/
     96 tNFC_CONN_CB * nfc_find_conn_cb_by_handle (UINT8 id)
     97 {
     98     int xx;
     99     tNFC_CONN_CB *p_conn_cb = NULL;
    100 
    101     for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++)
    102     {
    103         if (nfc_cb.conn_cb[xx].id == id)
    104         {
    105             p_conn_cb = &nfc_cb.conn_cb[xx];
    106             break;
    107         }
    108     }
    109     return p_conn_cb;
    110 }
    111 
    112 /*******************************************************************************
    113 **
    114 ** Function         nfc_find_conn_cb_by_conn_id
    115 **
    116 ** Description      This function is called to locate the control block for
    117 **                  the given connection id
    118 **
    119 ** Returns          The control block or NULL
    120 **
    121 *******************************************************************************/
    122 tNFC_CONN_CB * nfc_find_conn_cb_by_conn_id (UINT8 conn_id)
    123 {
    124     tNFC_CONN_CB *p_conn_cb = NULL;
    125     UINT8   handle;
    126     UINT8   id;
    127     int     xx;
    128 
    129     if (conn_id == NFC_PEND_CONN_ID)
    130     {
    131         for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++)
    132         {
    133             if (nfc_cb.conn_cb[xx].conn_id == NFC_PEND_CONN_ID)
    134             {
    135                 p_conn_cb   = &nfc_cb.conn_cb[xx];
    136                 break;
    137             }
    138         }
    139     }
    140     else
    141     {
    142         id         = conn_id & NFC_CONN_ID_ID_MASK;
    143         if (id < NFC_MAX_CONN_ID)
    144         {
    145             handle = nfc_cb.conn_id[id];
    146             if (handle > 0)
    147                 p_conn_cb = &nfc_cb.conn_cb[handle - 1];
    148         }
    149     }
    150 
    151     return p_conn_cb;
    152 }
    153 
    154 /*******************************************************************************
    155 **
    156 ** Function         nfc_free_conn_cb
    157 **
    158 ** Description      This function is called to free the control block and
    159 **                  resources and id mapping table
    160 **
    161 ** Returns          void
    162 **
    163 *******************************************************************************/
    164 void nfc_free_conn_cb (tNFC_CONN_CB *p_cb)
    165 {
    166     void    *p_buf;
    167 
    168     if (p_cb == NULL)
    169         return;
    170 
    171     while ((p_buf = GKI_dequeue (&p_cb->rx_q)) != NULL)
    172         GKI_freebuf (p_buf);
    173 
    174     while ((p_buf = GKI_dequeue (&p_cb->tx_q)) != NULL)
    175         GKI_freebuf (p_buf);
    176 
    177     nfc_cb.conn_id[p_cb->conn_id]   = 0;
    178     p_cb->p_cback                   = NULL;
    179     p_cb->conn_id                   = NFC_ILLEGAL_CONN_ID;
    180 }
    181 
    182 /*******************************************************************************
    183 **
    184 ** Function         nfc_reset_all_conn_cbs
    185 **
    186 ** Description      This function is called to free all the control blocks and
    187 **                  resources and id mapping table
    188 **
    189 ** Returns          void
    190 **
    191 *******************************************************************************/
    192 NFC_API extern void nfc_reset_all_conn_cbs (void)
    193 {
    194     int xx;
    195     tNFC_CONN_CB *p_conn_cb = &nfc_cb.conn_cb[0];
    196     tNFC_DEACTIVATE_DEVT    deact;
    197 
    198     deact.status     = NFC_STATUS_NOT_INITIALIZED;
    199     deact.type       = NFC_DEACTIVATE_TYPE_IDLE;
    200     deact.is_ntf     = TRUE;
    201     for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++, p_conn_cb++)
    202     {
    203         if (p_conn_cb->conn_id != NFC_ILLEGAL_CONN_ID)
    204         {
    205             if (p_conn_cb->p_cback)
    206                 (*p_conn_cb->p_cback) (p_conn_cb->conn_id, NFC_DEACTIVATE_CEVT, (tNFC_CONN *) &deact);
    207             nfc_free_conn_cb (p_conn_cb);
    208         }
    209     }
    210 }
    211 
    212 
    213 #endif /* NFC_INCLUDED == TRUE */
    214