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