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 /* This is the structure used by profile to get the status of BNEP */
180 typedef struct {
181 #define BNEP_STATUS_FAILE 0
182 #define BNEP_STATUS_CONNECTED 1
183   uint8_t con_status;
184 
185   uint16_t l2cap_cid;
186   RawAddress rem_bda;
187   uint16_t rem_mtu_size;
188   uint16_t xmit_q_depth;
189 
190   uint16_t sent_num_filters;
191   uint16_t sent_mcast_filters;
192   uint16_t rcvd_num_filters;
193   uint16_t rcvd_mcast_filters;
194   bluetooth::Uuid src_uuid;
195   bluetooth::Uuid dst_uuid;
196 
197 } tBNEP_STATUS;
198 
199 /*****************************************************************************
200  *  External Function Declarations
201  ****************************************************************************/
202 /*******************************************************************************
203  *
204  * Function         BNEP_Register
205  *
206  * Description      This function is called by the upper layer to register
207  *                  its callbacks with BNEP
208  *
209  * Parameters:      p_reg_info - contains all callback function pointers
210  *
211  *
212  * Returns          BNEP_SUCCESS        if registered successfully
213  *                  BNEP_FAILURE        if connection state callback is missing
214  *
215  ******************************************************************************/
216 extern tBNEP_RESULT BNEP_Register(tBNEP_REGISTER* p_reg_info);
217 
218 /*******************************************************************************
219  *
220  * Function         BNEP_Deregister
221  *
222  * Description      This function is called by the upper layer to de-register
223  *                  its callbacks.
224  *
225  * Parameters:      void
226  *
227  *
228  * Returns          void
229  *
230  ******************************************************************************/
231 extern void BNEP_Deregister(void);
232 
233 /*******************************************************************************
234  *
235  * Function         BNEP_Connect
236  *
237  * Description      This function creates a BNEP connection to a remote
238  *                  device.
239  *
240  * Parameters:      p_rem_addr - BD_ADDR of the peer
241  *                  src_uuid   - source uuid for the connection
242  *                  dst_uuid   - destination uuid for the connection
243  *                  p_handle   - pointer to return the handle for the connection
244  *
245  * Returns          BNEP_SUCCESS                if connection started
246  *                  BNEP_NO_RESOURCES           if no resources
247  *
248  ******************************************************************************/
249 extern tBNEP_RESULT BNEP_Connect(const RawAddress& p_rem_bda,
250                                  const bluetooth::Uuid& src_uuid,
251                                  const bluetooth::Uuid& dst_uuid,
252                                  uint16_t* p_handle);
253 
254 /*******************************************************************************
255  *
256  * Function         BNEP_ConnectResp
257  *
258  * Description      This function is called in responce to connection indication
259  *
260  *
261  * Parameters:      handle  - handle given in the connection indication
262  *                  resp    - responce for the connection indication
263  *
264  * Returns          BNEP_SUCCESS                if connection started
265  *                  BNEP_WRONG_HANDLE           if the connection is not found
266  *                  BNEP_WRONG_STATE            if the responce is not expected
267  *
268  ******************************************************************************/
269 extern tBNEP_RESULT BNEP_ConnectResp(uint16_t handle, tBNEP_RESULT resp);
270 
271 /*******************************************************************************
272  *
273  * Function         BNEP_Disconnect
274  *
275  * Description      This function is called to close the specified connection.
276  *
277  * Parameters:      handle   - handle of the connection
278  *
279  * Returns          BNEP_SUCCESS                if connection is disconnected
280  *                  BNEP_WRONG_HANDLE           if no connection is not found
281  *
282  ******************************************************************************/
283 extern tBNEP_RESULT BNEP_Disconnect(uint16_t handle);
284 
285 /*******************************************************************************
286  *
287  * Function         BNEP_WriteBuf
288  *
289  * Description      This function sends data in a GKI buffer on BNEP connection
290  *
291  * Parameters:      handle       - handle of the connection to write
292  *                  p_dest_addr  - BD_ADDR/Ethernet addr of the destination
293  *                  p_buf        - pointer to address of buffer with data
294  *                  protocol     - protocol type of the packet
295  *                  p_src_addr   - (optional) BD_ADDR/ethernet address of the
296  *                                 source (should be NULL if it is the local BD
297  *                                         Addr)
298  *                  fw_ext_present - forwarded extensions present
299  *
300  * Returns:         BNEP_WRONG_HANDLE       - if passed handle is not valid
301  *                  BNEP_MTU_EXCEDED        - If the data length is greater
302  *                                            than MTU
303  *                  BNEP_IGNORE_CMD         - If the packet is filtered out
304  *                  BNEP_Q_SIZE_EXCEEDED    - If the Tx Q is full
305  *                  BNEP_SUCCESS            - If written successfully
306  *
307  ******************************************************************************/
308 extern tBNEP_RESULT BNEP_WriteBuf(uint16_t handle,
309                                   const RawAddress& p_dest_addr, BT_HDR* p_buf,
310                                   uint16_t protocol,
311                                   const RawAddress* p_src_addr,
312                                   bool fw_ext_present);
313 
314 /*******************************************************************************
315  *
316  * Function         BNEP_Write
317  *
318  * Description      This function sends data over a BNEP connection
319  *
320  * Parameters:      handle       - handle of the connection to write
321  *                  p_dest_addr  - BD_ADDR/Ethernet addr of the destination
322  *                  p_data       - pointer to data start
323  *                  protocol     - protocol type of the packet
324  *                  p_src_addr   - (optional) BD_ADDR/ethernet address of the
325  *                                 source (should be NULL if it is the local BD
326  *                                 Addr)
327  *                  fw_ext_present - forwarded extensions present
328  *
329  * Returns:         BNEP_WRONG_HANDLE       - if passed handle is not valid
330  *                  BNEP_MTU_EXCEDED        - If the data length is greater than
331  *                                            the MTU
332  *                  BNEP_IGNORE_CMD         - If the packet is filtered out
333  *                  BNEP_Q_SIZE_EXCEEDED    - If the Tx Q is full
334  *                  BNEP_NO_RESOURCES       - If not able to allocate a buffer
335  *                  BNEP_SUCCESS            - If written successfully
336  *
337  ******************************************************************************/
338 extern tBNEP_RESULT BNEP_Write(uint16_t handle, const RawAddress& p_dest_addr,
339                                uint8_t* p_data, uint16_t len, uint16_t protocol,
340                                const RawAddress* p_src_addr,
341                                bool fw_ext_present);
342 
343 /*******************************************************************************
344  *
345  * Function         BNEP_SetProtocolFilters
346  *
347  * Description      This function sets the protocol filters on peer device
348  *
349  * Parameters:      handle        - Handle for the connection
350  *                  num_filters   - total number of filter ranges
351  *                  p_start_array - Array of beginings of all protocol ranges
352  *                  p_end_array   - Array of ends of all protocol ranges
353  *
354  * Returns          BNEP_WRONG_HANDLE           - if the connection handle is
355  *                                                not valid
356  *                  BNEP_SET_FILTER_FAIL        - if the connection is in the
357  *                                                wrong state
358  *                  BNEP_TOO_MANY_FILTERS       - if too many filters
359  *                  BNEP_SUCCESS                - if request sent successfully
360  *
361  ******************************************************************************/
362 extern tBNEP_RESULT BNEP_SetProtocolFilters(uint16_t handle,
363                                             uint16_t num_filters,
364                                             uint16_t* p_start_array,
365                                             uint16_t* p_end_array);
366 
367 /*******************************************************************************
368  *
369  * Function         BNEP_SetMulticastFilters
370  *
371  * Description      This function sets the filters for multicast addresses for
372  *                  BNEP.
373  *
374  * Parameters:      handle        - Handle for the connection
375  *                  num_filters   - total number of filter ranges
376  *                  p_start_array - Pointer to sequence of beginings of all
377  *                                         multicast address ranges
378  *                  p_end_array   - Pointer to sequence of ends of all
379  *                                         multicast address ranges
380  *
381  * Returns          BNEP_WRONG_HANDLE           - if the connection handle is
382  *                                                not valid
383  *                  BNEP_SET_FILTER_FAIL        - if the connection is in the
384  *                                                wrong state
385  *                  BNEP_TOO_MANY_FILTERS       - if too many filters
386  *                  BNEP_SUCCESS                - if request sent successfully
387  *
388  ******************************************************************************/
389 extern tBNEP_RESULT BNEP_SetMulticastFilters(uint16_t handle,
390                                              uint16_t num_filters,
391                                              uint8_t* p_start_array,
392                                              uint8_t* p_end_array);
393 
394 /*******************************************************************************
395  *
396  * Function         BNEP_SetTraceLevel
397  *
398  * Description      This function sets the trace level for BNEP. If called with
399  *                  a value of 0xFF, it simply reads the current trace level.
400  *
401  * Returns          the new (current) trace level
402  *
403  ******************************************************************************/
404 extern uint8_t BNEP_SetTraceLevel(uint8_t new_level);
405 
406 /*******************************************************************************
407  *
408  * Function         BNEP_Init
409  *
410  * Description      This function initializes the BNEP unit. It should be called
411  *                  before accessing any other APIs to initialize the control
412  *                  block
413  *
414  * Returns          void
415  *
416  ******************************************************************************/
417 extern void BNEP_Init(void);
418 
419 /*******************************************************************************
420  *
421  * Function         BNEP_GetStatus
422  *
423  * Description      This function gets the status information for BNEP
424  *                  connection
425  *
426  * Returns          BNEP_SUCCESS            - if the status is available
427  *                  BNEP_NO_RESOURCES       - if no structure is passed for
428  *                                            output
429  *                  BNEP_WRONG_HANDLE       - if the handle is invalid
430  *                  BNEP_WRONG_STATE        - if not in connected state
431  *
432  ******************************************************************************/
433 extern tBNEP_RESULT BNEP_GetStatus(uint16_t handle, tBNEP_STATUS* p_status);
434 
435 #endif
436