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