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