1 /******************************************************************************
2  *
3  *  Copyright 2001-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  *  This interface file contains the interface to the Bluetooth Network
22  *  Encapsilation Protocol (BNEP).
23  *
24  ******************************************************************************/
25 #ifndef BNEP_API_H
26 #define BNEP_API_H
27 
28 #include <cstdint>
29 
30 #include "stack/include/bt_hdr.h"
31 #include "stack/include/l2cap_types.h"
32 #include "types/bluetooth/uuid.h"
33 #include "types/raw_address.h"
34 
35 /*****************************************************************************
36  *  Constants
37  ****************************************************************************/
38 
39 /* Define the minimum offset needed in a GKI buffer for
40  * sending BNEP packets. Note, we are currently not sending
41  * extension headers, but may in the future, so allow
42  * space for them
43  */
44 #define BNEP_MINIMUM_OFFSET (15 + L2CAP_MIN_OFFSET)
45 #define BNEP_INVALID_HANDLE 0xFFFF
46 
47 /*****************************************************************************
48  *  Type Definitions
49  ****************************************************************************/
50 
51 /* Define the result codes from BNEP
52  */
53 enum {
54   BNEP_SUCCESS,               /* Success                           */
55   BNEP_CONN_DISCONNECTED,     /* Connection terminated   */
56   BNEP_NO_RESOURCES,          /* No resources                      */
57   BNEP_MTU_EXCEEDED,          /* Attempt to write long data        */
58   BNEP_INVALID_OFFSET,        /* Insufficient offset in GKI buffer */
59   BNEP_CONN_FAILED,           /* Connection failed                 */
60   BNEP_CONN_FAILED_CFG,       /* Connection failed cos of config   */
61   BNEP_CONN_FAILED_SRC_UUID,  /* Connection failed wrong source UUID   */
62   BNEP_CONN_FAILED_DST_UUID,  /* Connection failed wrong destination UUID   */
63   BNEP_CONN_FAILED_UUID_SIZE, /* Connection failed wrong size UUID   */
64   BNEP_Q_SIZE_EXCEEDED,       /* Too many buffers to dest          */
65   BNEP_TOO_MANY_FILTERS,      /* Too many local filters specified  */
66   BNEP_SET_FILTER_FAIL,       /* Set Filter failed  */
67   BNEP_WRONG_HANDLE,          /* Wrong handle for the connection  */
68   BNEP_WRONG_STATE,           /* Connection is in wrong state */
69   BNEP_SECURITY_FAIL,         /* Failed because of security */
70   BNEP_IGNORE_CMD,            /* To ignore the rcvd command */
71   BNEP_TX_FLOW_ON,            /* tx data flow enabled */
72   BNEP_TX_FLOW_OFF            /* tx data flow disabled */
73 };
74 typedef uint8_t tBNEP_RESULT;
75 
76 /***************************
77  *  Callback Functions
78  ***************************/
79 
80 /* Connection state change callback prototype. Parameters are
81  *              Connection handle
82  *              BD Address of remote
83  *              Connection state change result
84  *                  BNEP_SUCCESS indicates connection is success
85  *                  All values are used to indicate the reason for failure
86  *              Flag to indicate if it is just a role change
87  */
88 typedef void(tBNEP_CONN_STATE_CB)(uint16_t handle, const RawAddress& rem_bda, tBNEP_RESULT result,
89                                   bool is_role_change);
90 
91 /* Connection indication callback prototype. Parameters are
92  *              BD Address of remote, remote UUID and local UUID
93  *              and flag to indicate role change and handle to the connection
94  *              When BNEP calls this function profile should
95  *              use BNEP_ConnectResp call to accept or reject the request
96  */
97 typedef void(tBNEP_CONNECT_IND_CB)(uint16_t handle, const RawAddress& bd_addr,
98                                    const bluetooth::Uuid& remote_uuid,
99                                    const bluetooth::Uuid& local_uuid, bool is_role_change);
100 
101 /* Data buffer received indication callback prototype. Parameters are
102  *              Handle to the connection
103  *              Source BD/Ethernet Address
104  *              Dest BD/Ethernet address
105  *              Protocol
106  *              Pointer to the buffer
107  *              Flag to indicate whether extension headers to be forwarded are
108  *                present
109  */
110 typedef void(tBNEP_DATA_BUF_CB)(uint16_t handle, const RawAddress& src, const RawAddress& dst,
111                                 uint16_t protocol, BT_HDR* p_buf, bool fw_ext_present);
112 
113 /* Data received indication callback prototype. Parameters are
114  *              Handle to the connection
115  *              Source BD/Ethernet Address
116  *              Dest BD/Ethernet address
117  *              Protocol
118  *              Pointer to the beginning of the data
119  *              Length of data
120  *              Flag to indicate whether extension headers to be forwarded are
121  *                present
122  */
123 typedef void(tBNEP_DATA_IND_CB)(uint16_t handle, const RawAddress& src, const RawAddress& dst,
124                                 uint16_t protocol, uint8_t* p_data, uint16_t len,
125                                 bool fw_ext_present);
126 
127 /* Flow control callback for TX data. Parameters are
128  *              Handle to the connection
129  *              Event  flow status
130  */
131 typedef void(tBNEP_TX_DATA_FLOW_CB)(uint16_t handle, tBNEP_RESULT event);
132 
133 /* Filters received indication callback prototype. Parameters are
134  *              Handle to the connection
135  *              true if the cb is called for indication
136  *              Ignore this if it is indication, otherwise it is the result
137  *                      for the filter set operation performed by the local
138  *                      device
139  *              Number of protocol filters present
140  *              Pointer to the filters start. Filters are present in pairs
141  *                      of start of the range and end of the range.
142  *                      They will be present in big endian order. First
143  *                      two bytes will be starting of the first range and
144  *                      next two bytes will be ending of the range.
145  */
146 typedef void(tBNEP_FILTER_IND_CB)(uint16_t handle, bool indication, tBNEP_RESULT result,
147                                   uint16_t num_filters, uint8_t* p_filters);
148 
149 /* Multicast Filters received indication callback prototype. Parameters are
150  *              Handle to the connection
151  *              true if the cb is called for indication
152  *              Ignore this if it is indication, otherwise it is the result
153  *                      for the filter set operation performed by the local
154  *                      device
155  *              Number of multicast filters present
156  *              Pointer to the filters start. Filters are present in pairs
157  *                      of start of the range and end of the range.
158  *                      First six bytes will be starting of the first range and
159  *                      next six bytes will be ending of the range.
160  */
161 typedef void(tBNEP_MFILTER_IND_CB)(uint16_t handle, bool indication, tBNEP_RESULT result,
162                                    uint16_t num_mfilters, uint8_t* p_mfilters);
163 
164 /* This is the structure used by profile to register with BNEP */
165 typedef struct {
166   tBNEP_CONNECT_IND_CB* p_conn_ind_cb;      /* To indicate the conn request */
167   tBNEP_CONN_STATE_CB* p_conn_state_cb;     /* To indicate conn state change */
168   tBNEP_DATA_IND_CB* p_data_ind_cb;         /* To pass the data received */
169   tBNEP_DATA_BUF_CB* p_data_buf_cb;         /* To pass the data buffer received */
170   tBNEP_TX_DATA_FLOW_CB* p_tx_data_flow_cb; /* data flow callback */
171   tBNEP_FILTER_IND_CB* p_filter_ind_cb;     /* To indicate that peer set protocol filters */
172   tBNEP_MFILTER_IND_CB* p_mfilter_ind_cb;   /* To indicate that peer set mcast filters */
173 } tBNEP_REGISTER;
174 
175 /*****************************************************************************
176  *  External Function Declarations
177  ****************************************************************************/
178 /*******************************************************************************
179  *
180  * Function         BNEP_Register
181  *
182  * Description      This function is called by the upper layer to register
183  *                  its callbacks with BNEP
184  *
185  * Parameters:      p_reg_info - contains all callback function pointers
186  *
187  *
188  * Returns          BNEP_SUCCESS        if registered successfully
189  *                  BNEP_FAILURE        if connection state callback is missing
190  *
191  ******************************************************************************/
192 tBNEP_RESULT BNEP_Register(tBNEP_REGISTER* p_reg_info);
193 
194 /*******************************************************************************
195  *
196  * Function         BNEP_Deregister
197  *
198  * Description      This function is called by the upper layer to de-register
199  *                  its callbacks.
200  *
201  * Parameters:      void
202  *
203  *
204  * Returns          void
205  *
206  ******************************************************************************/
207 void BNEP_Deregister(void);
208 
209 /*******************************************************************************
210  *
211  * Function         BNEP_Connect
212  *
213  * Description      This function creates a BNEP connection to a remote
214  *                  device.
215  *
216  * Parameters:      p_rem_addr - BD_ADDR of the peer
217  *                  src_uuid   - source uuid for the connection
218  *                  dst_uuid   - destination uuid for the connection
219  *                  p_handle   - pointer to return the handle for the connection
220  *
221  * Returns          BNEP_SUCCESS                if connection started
222  *                  BNEP_NO_RESOURCES           if no resources
223  *
224  ******************************************************************************/
225 tBNEP_RESULT BNEP_Connect(const RawAddress& p_rem_bda, const bluetooth::Uuid& src_uuid,
226                           const bluetooth::Uuid& dst_uuid, uint16_t* p_handle, uint32_t mx_chan_id);
227 
228 /*******************************************************************************
229  *
230  * Function         BNEP_ConnectResp
231  *
232  * Description      This function is called in response to connection indication
233  *
234  *
235  * Parameters:      handle  - handle given in the connection indication
236  *                  resp    - response for the connection indication
237  *
238  * Returns          BNEP_SUCCESS                if connection started
239  *                  BNEP_WRONG_HANDLE           if the connection is not found
240  *                  BNEP_WRONG_STATE            if the response is not expected
241  *
242  ******************************************************************************/
243 tBNEP_RESULT BNEP_ConnectResp(uint16_t handle, tBNEP_RESULT resp);
244 
245 /*******************************************************************************
246  *
247  * Function         BNEP_Disconnect
248  *
249  * Description      This function is called to close the specified connection.
250  *
251  * Parameters:      handle   - handle of the connection
252  *
253  * Returns          BNEP_SUCCESS                if connection is disconnected
254  *                  BNEP_WRONG_HANDLE           if no connection is not found
255  *
256  ******************************************************************************/
257 tBNEP_RESULT BNEP_Disconnect(uint16_t handle);
258 
259 /*******************************************************************************
260  *
261  * Function         BNEP_WriteBuf
262  *
263  * Description      This function sends data in a GKI buffer on BNEP connection
264  *
265  * Parameters:      handle       - handle of the connection to write
266  *                  dest_addr    - BD_ADDR/Ethernet addr of the destination
267  *                  p_buf        - pointer to address of buffer with data
268  *                  protocol     - protocol type of the packet
269  *                  src_addr     - (optional) BD_ADDR/ethernet address of the
270  *                                 source (should be kEmpty if it is the local
271  *BD Addr) fw_ext_present - forwarded extensions present
272  *
273  * Returns:         BNEP_WRONG_HANDLE       - if passed handle is not valid
274  *                  BNEP_MTU_EXCEEDED       - If the data length is greater
275  *                                            than MTU
276  *                  BNEP_IGNORE_CMD         - If the packet is filtered out
277  *                  BNEP_Q_SIZE_EXCEEDED    - If the Tx Q is full
278  *                  BNEP_SUCCESS            - If written successfully
279  *
280  ******************************************************************************/
281 tBNEP_RESULT BNEP_WriteBuf(uint16_t handle, const RawAddress& dest_addr, BT_HDR* p_buf,
282                            uint16_t protocol, const RawAddress& src_addr, bool fw_ext_present);
283 
284 /*******************************************************************************
285  *
286  * Function         BNEP_Write
287  *
288  * Description      This function sends data over a BNEP connection
289  *
290  * Parameters:      handle       - handle of the connection to write
291  *                  dest_addr    - BD_ADDR/Ethernet addr of the destination
292  *                  p_data       - pointer to data start
293  *                  protocol     - protocol type of the packet
294  *                  src_addr     - (optional) BD_ADDR/ethernet address of the
295  *                                 source (should be kEmpty if it is the local
296  *BD Addr) fw_ext_present - forwarded extensions present
297  *
298  * Returns:         BNEP_WRONG_HANDLE       - if passed handle is not valid
299  *                  BNEP_MTU_EXCEEDED       - If the data length is greater than
300  *                                            the MTU
301  *                  BNEP_IGNORE_CMD         - If the packet is filtered out
302  *                  BNEP_Q_SIZE_EXCEEDED    - If the Tx Q is full
303  *                  BNEP_NO_RESOURCES       - If not able to allocate a buffer
304  *                  BNEP_SUCCESS            - If written successfully
305  *
306  ******************************************************************************/
307 tBNEP_RESULT BNEP_Write(uint16_t handle, const RawAddress& dest_addr, uint8_t* p_data, uint16_t len,
308                         uint16_t protocol, const RawAddress& src_addr, bool fw_ext_present);
309 
310 /*******************************************************************************
311  *
312  * Function         BNEP_SetProtocolFilters
313  *
314  * Description      This function sets the protocol filters on peer device
315  *
316  * Parameters:      handle        - Handle for the connection
317  *                  num_filters   - total number of filter ranges
318  *                  p_start_array - Array of beginings of all protocol ranges
319  *                  p_end_array   - Array of ends of all protocol ranges
320  *
321  * Returns          BNEP_WRONG_HANDLE           - if the connection handle is
322  *                                                not valid
323  *                  BNEP_SET_FILTER_FAIL        - if the connection is in the
324  *                                                wrong state
325  *                  BNEP_TOO_MANY_FILTERS       - if too many filters
326  *                  BNEP_SUCCESS                - if request sent successfully
327  *
328  ******************************************************************************/
329 tBNEP_RESULT BNEP_SetProtocolFilters(uint16_t handle, uint16_t num_filters, uint16_t* p_start_array,
330                                      uint16_t* p_end_array);
331 
332 /*******************************************************************************
333  *
334  * Function         BNEP_SetMulticastFilters
335  *
336  * Description      This function sets the filters for multicast addresses for
337  *                  BNEP.
338  *
339  * Parameters:      handle        - Handle for the connection
340  *                  num_filters   - total number of filter ranges
341  *                  p_start_array - Pointer to sequence of beginings of all
342  *                                         multicast address ranges
343  *                  p_end_array   - Pointer to sequence of ends of all
344  *                                         multicast address ranges
345  *
346  * Returns          BNEP_WRONG_HANDLE           - if the connection handle is
347  *                                                not valid
348  *                  BNEP_SET_FILTER_FAIL        - if the connection is in the
349  *                                                wrong state
350  *                  BNEP_TOO_MANY_FILTERS       - if too many filters
351  *                  BNEP_SUCCESS                - if request sent successfully
352  *
353  ******************************************************************************/
354 tBNEP_RESULT BNEP_SetMulticastFilters(uint16_t handle, uint16_t num_filters, uint8_t* p_start_array,
355                                       uint8_t* p_end_array);
356 
357 /*******************************************************************************
358  *
359  * Function         BNEP_Init
360  *
361  * Description      This function initializes the BNEP unit. It should be called
362  *                  before accessing any other APIs to initialize the control
363  *                  block
364  *
365  * Returns          void
366  *
367  ******************************************************************************/
368 void BNEP_Init(void);
369 
370 #endif
371