nfc_utils.c revision 45faad0ff5deeb0c676356345d99398cc4ab695a
1/*****************************************************************************
2**
3**  Name:          nfc_utils.c
4**
5**  Description:   This file contains functions that interface with the NFC NCI
6**                 transport. On the receive side, it routes events to
7**                 the appropriate handler (callback). On the
8**                 transmit side, it manages the command transmission.
9**
10**
11**  Copyright (c) 1999-2011, Broadcom Corp., All Rights Reserved.
12**  Broadcom Bluetooth Core. Proprietary and confidential.
13**
14******************************************************************************/
15#include "nfc_target.h"
16#include "bt_types.h"
17#include "nfc_api.h"
18
19#if (NFC_INCLUDED == TRUE)
20#include "nfc_int.h"
21
22#if (NFC_RW_ONLY == FALSE)
23    /* only allow the entries that our NFCC can support */
24#define NFC_CHECK_MAX_CONN()    { if (max > nfc_cb.max_conn) max = nfc_cb.max_conn;}
25#else
26#define NFC_CHECK_MAX_CONN()
27#endif
28
29/*******************************************************************************
30**
31** Function         nfc_alloc_conn_cb
32**
33** Description      This function is called to allocation a control block for
34**                  NCI logical connection
35**
36** Returns          The allocated control block or NULL
37**
38*******************************************************************************/
39tNFC_CONN_CB * nfc_alloc_conn_cb( tNFC_CONN_CBACK *p_cback)
40{
41    int xx, max = NCI_MAX_CONN_CBS;
42    tNFC_CONN_CB *p_conn_cb = NULL;
43
44    NFC_CHECK_MAX_CONN();
45    for (xx=0; xx<max; xx++)
46    {
47        if (nfc_cb.conn_cb[xx].conn_id == NFC_ILLEGAL_CONN_ID)
48        {
49            nfc_cb.conn_cb[xx].conn_id  = NFC_PEND_CONN_ID; /* to indicate this cb is used */
50            p_conn_cb                   = &nfc_cb.conn_cb[xx];
51            p_conn_cb->p_cback          = p_cback;
52            break;
53        }
54    }
55    return p_conn_cb;
56}
57
58/*******************************************************************************
59**
60** Function         nfc_set_conn_id
61**
62** Description      This function is called to set the connection id to the
63**                  connection control block and the id mapping table
64**
65** Returns          void
66**
67*******************************************************************************/
68void nfc_set_conn_id(tNFC_CONN_CB * p_cb, UINT8 conn_id)
69{
70    UINT8   handle;
71
72    if (p_cb == NULL)
73        return;
74
75    p_cb->conn_id           = conn_id;
76    handle                  = (UINT8)(p_cb - nfc_cb.conn_cb + 1);
77    nfc_cb.conn_id[conn_id] = handle;
78    NFC_TRACE_DEBUG2("nfc_set_conn_id conn_id:%d, handle:%d", conn_id, handle);
79}
80
81/*******************************************************************************
82**
83** Function         nfc_find_conn_cb_by_handle
84**
85** Description      This function is called to locate the control block for
86**                  loopback test.
87**
88** Returns          The loopback test control block or NULL
89**
90*******************************************************************************/
91tNFC_CONN_CB * nfc_find_conn_cb_by_handle(UINT8 id)
92{
93    int xx;
94    tNFC_CONN_CB *p_conn_cb = NULL;
95
96    for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++)
97    {
98        if (nfc_cb.conn_cb[xx].id == id)
99        {
100            p_conn_cb = &nfc_cb.conn_cb[xx];
101            break;
102        }
103    }
104    return p_conn_cb;
105}
106
107/*******************************************************************************
108**
109** Function         nfc_find_conn_cb_by_handle
110**
111** Description      This function is called to locate the control block for
112**                  the given connection id
113**
114** Returns          The control block or NULL
115**
116*******************************************************************************/
117tNFC_CONN_CB * nfc_find_conn_cb_by_conn_id(UINT8 conn_id)
118{
119    tNFC_CONN_CB *p_conn_cb = NULL;
120    UINT8   handle;
121    UINT8   id;
122    int     xx;
123
124    if (conn_id == NFC_PEND_CONN_ID)
125    {
126        for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++)
127        {
128            if (nfc_cb.conn_cb[xx].conn_id == NFC_PEND_CONN_ID)
129            {
130                p_conn_cb   = &nfc_cb.conn_cb[xx];
131                break;
132            }
133        }
134    }
135    else
136    {
137        id         = conn_id & NFC_CONN_ID_ID_MASK;
138        if (id < NFC_MAX_CONN_ID)
139        {
140            handle = nfc_cb.conn_id[id];
141            if (handle > 0)
142                p_conn_cb = &nfc_cb.conn_cb[handle - 1];
143        }
144    }
145
146    return p_conn_cb;
147}
148
149/*******************************************************************************
150**
151** Function         nfc_free_conn_cb
152**
153** Description      This function is called to free the control block and
154**                  resources and id mapping table
155**
156** Returns          void
157**
158*******************************************************************************/
159void nfc_free_conn_cb( tNFC_CONN_CB *p_cb)
160{
161    UINT8   handle;
162    void    *p_buf;
163
164    if (p_cb == NULL)
165        return;
166
167    while ( (p_buf = GKI_dequeue(&p_cb->tx_q)) != NULL)
168        GKI_freebuf (p_buf);
169    while ( (p_buf = GKI_dequeue(&p_cb->rx_q)) != NULL)
170        GKI_freebuf (p_buf);
171    handle                  = (UINT8)(p_cb - nfc_cb.conn_cb);
172    nfc_cb.conn_id[handle]  = 0;
173    p_cb->p_cback           = NULL;
174    p_cb->conn_id           = NFC_ILLEGAL_CONN_ID;
175}
176
177/*******************************************************************************
178**
179** Function         nfc_reset_all_conn_cbs
180**
181** Description      This function is called to free all the control blocks and
182**                  resources and id mapping table
183**
184** Returns          void
185**
186*******************************************************************************/
187NFC_API extern void nfc_reset_all_conn_cbs( void)
188{
189    int xx;
190    tNFC_CONN_CB *p_conn_cb = &nfc_cb.conn_cb[0];
191    tNFC_DEACTIVATE_DEVT    deact;
192
193    deact.status     = NFC_STATUS_NOT_INITIALIZED;
194    deact.type       = NFC_DEACTIVATE_TYPE_IDLE;
195    deact.is_ntf     = TRUE;
196    for (xx=0; xx<NCI_MAX_CONN_CBS; xx++,p_conn_cb++)
197    {
198        if (p_conn_cb->conn_id != NFC_ILLEGAL_CONN_ID)
199        {
200            if (p_conn_cb->p_cback)
201                (*p_conn_cb->p_cback)(p_conn_cb->conn_id, NFC_DEACTIVATE_CEVT, (tNFC_CONN *) &deact);
202            nfc_free_conn_cb(p_conn_cb);
203        }
204    }
205}
206
207
208#endif /* NFC_INCLUDED == TRUE */
209