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