1 /******************************************************************************
2  *
3  *  Copyright 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_pan_co.h"
28 
29 #include <bluetooth/log.h>
30 
31 #include <cstdint>
32 #include <cstring>
33 
34 #include "bta/include/bta_pan_api.h"
35 #include "bta_pan_ci.h"
36 #include "btif_pan_internal.h"
37 #include "osi/include/allocator.h"
38 #include "stack/include/bt_hdr.h"
39 #include "types/raw_address.h"
40 
41 using namespace bluetooth;
42 
43 /*******************************************************************************
44  *
45  * Function         bta_pan_co_init
46  *
47  * Description
48  *
49  *
50  * Returns          Data flow mask.
51  *
52  ******************************************************************************/
bta_pan_co_init(uint8_t * q_level)53 uint8_t bta_pan_co_init(uint8_t* q_level) {
54   log::verbose("bta_pan_co_init");
55 
56   /* set the q_level to 30 buffers */
57   *q_level = 30;
58 
59   // return (BTA_PAN_RX_PULL | BTA_PAN_TX_PULL);
60   return BTA_PAN_RX_PUSH_BUF | BTA_PAN_RX_PUSH | BTA_PAN_TX_PULL;
61 }
62 
63 /*******************************************************************************
64  *
65  * Function         bta_pan_co_close
66  *
67  * Description      This function is called by PAN when a connection to a
68  *                  peer is closed.
69  *
70  *
71  * Returns          void
72  *
73  ******************************************************************************/
bta_pan_co_close(uint16_t handle,uint8_t app_id)74 void bta_pan_co_close(uint16_t handle, uint8_t app_id) {
75   log::verbose("bta_pan_co_close:app_id:{}, handle:{}", app_id, handle);
76   btpan_conn_t* conn = btpan_find_conn_handle(handle);
77   if (conn && conn->state == PAN_STATE_OPEN) {
78     log::verbose("bta_pan_co_close");
79 
80     // let bta close event reset this handle as it needs
81     // the handle to find the connection upon CLOSE
82     // conn->handle = -1;
83     conn->state = PAN_STATE_CLOSE;
84     btpan_cb.open_count--;
85 
86     if (btpan_cb.open_count == 0 && btpan_cb.tap_fd != -1) {
87       btpan_tap_close(btpan_cb.tap_fd);
88       btpan_cb.tap_fd = -1;
89     }
90   }
91 }
92 
93 /*******************************************************************************
94  *
95  * Function         bta_pan_co_tx_path
96  *
97  * Description      This function is called by PAN to transfer data on the
98  *                  TX path; that is, data being sent from BTA to the phone.
99  *                  This function is used when the TX data path is configured
100  *                  to use the pull interface.  The implementation of this
101  *                  function will typically call Bluetooth stack functions
102  *                  PORT_Read() or PORT_ReadData() to read data from RFCOMM
103  *                  and then a platform-specific function to send data that
104  *                  data to the phone.
105  *
106  *
107  * Returns          void
108  *
109  ******************************************************************************/
bta_pan_co_tx_path(uint16_t handle,uint8_t app_id)110 void bta_pan_co_tx_path(uint16_t handle, uint8_t app_id) {
111   BT_HDR* p_buf;
112   RawAddress src;
113   RawAddress dst;
114   uint16_t protocol;
115   bool ext;
116   bool forward;
117 
118   log::verbose("handle:{}, app_id:{}", handle, app_id);
119 
120   btpan_conn_t* conn = btpan_find_conn_handle(handle);
121   if (!conn) {
122     log::error("cannot find pan connection");
123     return;
124   } else if (conn->state != PAN_STATE_OPEN) {
125     log::error("conn is not opened, conn:{}, conn->state:{}", std::format_ptr(conn), conn->state);
126     return;
127   }
128 
129   do {
130     /* read next data buffer from pan */
131     p_buf = bta_pan_ci_readbuf(handle, src, dst, &protocol, &ext, &forward);
132     if (p_buf) {
133       log::verbose("calling btapp_tap_send, p_buf->len:{}, offset:{}", p_buf->len, p_buf->offset);
134       if (is_empty_eth_addr(conn->eth_addr) && is_valid_bt_eth_addr(src)) {
135         log::verbose("pan bt peer addr: {} update its ethernet addr: {}", conn->peer, src);
136         conn->eth_addr = src;
137       }
138       btpan_tap_send(btpan_cb.tap_fd, src, dst, protocol, (char*)(p_buf + 1) + p_buf->offset,
139                      p_buf->len, ext, forward);
140       osi_free(p_buf);
141     }
142   } while (p_buf != NULL);
143 }
144 
145 /*******************************************************************************
146  *
147  * Function         bta_pan_co_rx_path
148  *
149  * Description
150  *
151  *
152  *
153  *
154  * Returns          void
155  *
156  ******************************************************************************/
bta_pan_co_rx_path(uint16_t,uint8_t)157 void bta_pan_co_rx_path(uint16_t /* handle */, uint8_t /* app_id */) {
158   log::verbose("bta_pan_co_rx_path not used");
159 }
160 
161 /*******************************************************************************
162  *
163  * Function         bta_pan_co_rx_flow
164  *
165  * Description      This function is called by PAN to enable or disable
166  *                  data flow on the RX path when it is configured to use
167  *                  a push interface.  If data flow is disabled the phone must
168  *                  not call bta_pan_ci_rx_write() or bta_pan_ci_rx_writebuf()
169  *                  until data flow is enabled again.
170  *
171  *
172  * Returns          void
173  *
174  ******************************************************************************/
bta_pan_co_rx_flow(uint16_t handle,uint8_t,bool enable)175 void bta_pan_co_rx_flow(uint16_t handle, uint8_t /* app_id */, bool enable) {
176   log::verbose("bta_pan_co_rx_flow, enabled:{}, not used", enable);
177   btpan_conn_t* conn = btpan_find_conn_handle(handle);
178   if (!conn || conn->state != PAN_STATE_OPEN) {
179     return;
180   }
181   btpan_set_flow_control(enable);
182 }
183 
184 /*******************************************************************************
185  *
186  * Function         bta_pan_co_filt_ind
187  *
188  * Description      protocol filter indication from peer device
189  *
190  * Returns          void
191  *
192  ******************************************************************************/
bta_pan_co_pfilt_ind(uint16_t,bool,tBTA_PAN_STATUS,uint16_t,uint8_t *)193 void bta_pan_co_pfilt_ind(uint16_t /* handle */, bool /* indication */,
194                           tBTA_PAN_STATUS /* result */, uint16_t /* len */,
195                           uint8_t* /* p_filters */) {
196   log::verbose("bta_pan_co_pfilt_ind");
197 }
198 
199 /*******************************************************************************
200  *
201  * Function         bta_pan_co_mfilt_ind
202  *
203  * Description      multicast filter indication from peer device
204  *
205  * Returns          void
206  *
207  ******************************************************************************/
bta_pan_co_mfilt_ind(uint16_t,bool,tBTA_PAN_STATUS,uint16_t,uint8_t *)208 void bta_pan_co_mfilt_ind(uint16_t /* handle */, bool /* indication */,
209                           tBTA_PAN_STATUS /* result */, uint16_t /* len */,
210                           uint8_t* /* p_filters */) {
211   log::verbose("bta_pan_co_mfilt_ind");
212 }
213