1/******************************************************************************
2 *
3 *  Copyright (C) 2009-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 *  Filename:      bta_pan_co.c
22 *
23 *  Description:   PAN stack callout api
24 *
25 *
26 ******************************************************************************/
27#include "bta_api.h"
28#include "bta_pan_api.h"
29#include "bta_pan_ci.h"
30#include "bta_pan_co.h"
31#include "pan_api.h"
32#include "gki.h"
33#include <hardware/bluetooth.h>
34#include <hardware/bt_pan.h>
35#include "btif_pan_internal.h"
36#include "bd.h"
37#include <string.h>
38
39
40/*******************************************************************************
41**
42** Function         bta_pan_co_init
43**
44** Description
45**
46**
47** Returns          Data flow mask.
48**
49*******************************************************************************/
50UINT8 bta_pan_co_init(UINT8 *q_level)
51{
52    BTIF_TRACE_API0("bta_pan_co_init");
53
54    /* set the q_level to 30 buffers */
55    *q_level = 30;
56
57    //return (BTA_PAN_RX_PULL | BTA_PAN_TX_PULL);
58    return (BTA_PAN_RX_PUSH_BUF | BTA_PAN_RX_PUSH | BTA_PAN_TX_PULL);
59}
60
61/******************************************************************************
62**
63** Function         bta_pan_co_open
64**
65** Description
66**
67**
68**
69**
70**
71** Returns          void
72**
73*******************************************************************************/
74void bta_pan_co_open(UINT16 handle, UINT8 app_id, tBTA_PAN_ROLE local_role,
75                            tBTA_PAN_ROLE peer_role, BD_ADDR peer_addr)
76{
77    BTIF_TRACE_API4("bta_pan_co_open:app_id:%d, local_role:%d, peer_role:%d, "
78                    "handle:%d", app_id, local_role, peer_role, handle);
79    btpan_conn_t* conn = btpan_find_conn_addr(peer_addr);
80    if(conn == NULL)
81        conn = btpan_new_conn(handle, peer_addr, local_role, peer_role);
82    if(conn)
83    {
84        BTIF_TRACE_DEBUG6("bta_pan_co_open:tap_fd:%d, open_count:%d, "
85            "conn->handle:%d should = handle:%d, local_role:%d, remote_role:%d",
86             btpan_cb.tap_fd, btpan_cb.open_count, conn->handle, handle,
87             conn->local_role, conn->remote_role);
88        //refresh the role & bt address
89
90        btpan_cb.open_count++;
91        conn->handle = handle;
92        //bdcpy(conn->peer, peer_addr);
93        if(btpan_cb.tap_fd < 0)
94        {
95            btpan_cb.tap_fd = btpan_tap_open();
96            if(btpan_cb.tap_fd >= 0)
97                create_tap_read_thread(btpan_cb.tap_fd);
98        }
99        if(btpan_cb.tap_fd >= 0)
100        {
101            conn->state = PAN_STATE_OPEN;
102            bta_pan_ci_rx_ready(handle);
103        }
104    }
105}
106
107/*******************************************************************************
108**
109** Function         bta_pan_co_close
110**
111** Description      This function is called by PAN when a connection to a
112**                  peer is closed.
113**
114**
115** Returns          void
116**
117*******************************************************************************/
118void bta_pan_co_close(UINT16 handle, UINT8 app_id)
119{
120    BTIF_TRACE_API2("bta_pan_co_close:app_id:%d, handle:%d", app_id, handle);
121    btpan_conn_t* conn = btpan_find_conn_handle(handle);
122    if(conn && conn->state == PAN_STATE_OPEN)
123    {
124        BTIF_TRACE_DEBUG0("bta_pan_co_close");
125
126        // let bta close event reset this handle as it needs
127        // the handle to find the connection upon CLOSE
128        //conn->handle = -1;
129        conn->state = PAN_STATE_CLOSE;
130        btpan_cb.open_count--;
131
132        if(btpan_cb.open_count == 0)
133        {
134            destroy_tap_read_thread();
135            if(btpan_cb.tap_fd != -1)
136            {
137                btpan_tap_close(btpan_cb.tap_fd);
138                btpan_cb.tap_fd = -1;
139            }
140        }
141    }
142}
143
144/*******************************************************************************
145**
146** Function         bta_pan_co_tx_path
147**
148** Description      This function is called by PAN to transfer data on the
149**                  TX path; that is, data being sent from BTA to the phone.
150**                  This function is used when the TX data path is configured
151**                  to use the pull interface.  The implementation of this
152**                  function will typically call Bluetooth stack functions
153**                  PORT_Read() or PORT_ReadData() to read data from RFCOMM
154**                  and then a platform-specific function to send data that
155**                  data to the phone.
156**
157**
158** Returns          void
159**
160*******************************************************************************/
161void bta_pan_co_tx_path(UINT16 handle, UINT8 app_id)
162{
163    BT_HDR          *p_buf;
164    UINT8           i;
165    BD_ADDR            src;
166    BD_ADDR            dst;
167    UINT16            protocol;
168    BOOLEAN            ext;
169    BOOLEAN         forward;
170
171    BTIF_TRACE_API2("bta_pan_co_tx_path, handle:%d, app_id:%d", handle, app_id);
172
173    btpan_conn_t* conn = btpan_find_conn_handle(handle);
174    if(conn && conn->state != PAN_STATE_OPEN)
175    {
176        BTIF_TRACE_ERROR2("bta_pan_co_tx_path: cannot find pan connction or conn"
177            "is not opened, conn:%p, conn->state:%d", conn, conn->state);
178        return;
179    }
180    do
181    {
182        /* read next data buffer from pan */
183        if ((p_buf = bta_pan_ci_readbuf(handle, src, dst, &protocol,
184                                 &ext, &forward)))
185        {
186            BTIF_TRACE_DEBUG2("bta_pan_co_tx_path, calling btapp_tap_send, "
187                "p_buf->len:%d, offset:%d", p_buf->len, p_buf->offset);
188            if(is_empty_eth_addr(conn->eth_addr) && is_valid_bt_eth_addr(src))
189            {
190                BTIF_TRACE_DEBUG6("pan bt peer addr: %02x:%02x:%02x:%02x:%02x:%02x",
191                    conn->peer[0], conn->peer[1], conn->peer[2],
192                    conn->peer[3],conn->peer[4], conn->peer[5]);
193                BTIF_TRACE_DEBUG6("     update its ethernet addr: "
194                    "%02x:%02x:%02x:%02x:%02x:%02x", src[0], src[1], src[2],
195                    src[3],src[4], src[5]);
196                memcpy(conn->eth_addr, src, sizeof(conn->eth_addr));
197
198            }
199            btpan_tap_send(btpan_cb.tap_fd, src, dst, protocol,
200                (char*)(p_buf + 1) + p_buf->offset, p_buf->len, ext, forward);
201            GKI_freebuf(p_buf);
202        }
203
204    } while (p_buf != NULL);
205}
206
207/*******************************************************************************
208**
209** Function         bta_pan_co_rx_path
210**
211** Description
212**
213**
214**
215**
216** Returns          void
217**
218*******************************************************************************/
219void bta_pan_co_rx_path(UINT16 handle, UINT8 app_id)
220{
221    BTIF_TRACE_API0("bta_pan_co_rx_path not used");
222}
223
224/*******************************************************************************
225**
226** Function         bta_pan_co_tx_write
227**
228** Description      This function is called by PAN to send data to the phone
229**                  when the TX path is configured to use a push interface.
230**                  The implementation of this function must copy the data to
231**                  the phone's memory.
232**
233**
234** Returns          void
235**
236*******************************************************************************/
237void bta_pan_co_tx_write(UINT16 handle, UINT8 app_id, BD_ADDR src, BD_ADDR dst,
238                                UINT16 protocol, UINT8 *p_data,
239                                UINT16 len, BOOLEAN ext, BOOLEAN forward)
240{
241     BTIF_TRACE_API0("bta_pan_co_tx_write not used");
242}
243
244/*******************************************************************************
245**
246** Function         bta_pan_co_tx_writebuf
247**
248** Description      This function is called by PAN to send data to the phone
249**                  when the TX path is configured to use a push interface with
250**                  zero copy.  The phone must free the buffer using function
251**                  GKI_freebuf() when it is through processing the buffer.
252**
253**
254** Returns          TRUE if flow enabled
255**
256*******************************************************************************/
257void  bta_pan_co_tx_writebuf(UINT16 handle, UINT8 app_id, BD_ADDR src,
258                                    BD_ADDR dst, UINT16 protocol, BT_HDR *p_buf,
259                                    BOOLEAN ext, BOOLEAN forward)
260{
261    BTIF_TRACE_API0("bta_pan_co_tx_writebuf not used");
262}
263
264/*******************************************************************************
265**
266** Function         bta_pan_co_rx_flow
267**
268** Description      This function is called by PAN to enable or disable
269**                  data flow on the RX path when it is configured to use
270**                  a push interface.  If data flow is disabled the phone must
271**                  not call bta_pan_ci_rx_write() or bta_pan_ci_rx_writebuf()
272**                  until data flow is enabled again.
273**
274**
275** Returns          void
276**
277*******************************************************************************/
278void bta_pan_co_rx_flow(UINT16 handle, UINT8 app_id, BOOLEAN enable)
279{
280    BTIF_TRACE_API1("bta_pan_co_rx_flow, enabled:%d, not used", enable);
281}
282
283/*******************************************************************************
284**
285** Function         bta_pan_co_filt_ind
286**
287** Description      protocol filter indication from peer device
288**
289** Returns          void
290**
291*******************************************************************************/
292void bta_pan_co_pfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
293                                    UINT16 len, UINT8 *p_filters)
294{
295    BTIF_TRACE_API0("bta_pan_co_pfilt_ind");
296}
297
298/*******************************************************************************
299**
300** Function         bta_pan_co_mfilt_ind
301**
302** Description      multicast filter indication from peer device
303**
304** Returns          void
305**
306*******************************************************************************/
307void bta_pan_co_mfilt_ind(UINT16 handle, BOOLEAN indication, tBTA_PAN_STATUS result,
308                                    UINT16 len, UINT8 *p_filters)
309{
310    BTIF_TRACE_API0("bta_pan_co_mfilt_ind");
311}
312
313