1 /** @file
2  *  @brief Bluetooth L2CAP handling
3  */
4 
5 /*
6  * Copyright (c) 2015-2016 Intel Corporation
7  * Copyright (c) 2023 Nordic Semiconductor
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_
12 #define ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_
13 
14 /**
15  * @brief L2CAP
16  * @defgroup bt_l2cap L2CAP
17  * @ingroup bluetooth
18  * @{
19  */
20 
21 #include <sys/types.h>
22 
23 #include <zephyr/sys/atomic.h>
24 #include <zephyr/bluetooth/buf.h>
25 #include <zephyr/bluetooth/conn.h>
26 #include <zephyr/bluetooth/hci.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 /** L2CAP PDU header size, used for buffer size calculations */
33 #define BT_L2CAP_HDR_SIZE               4
34 
35 /** Maximum Transmission Unit (MTU) for an outgoing L2CAP PDU. */
36 #define BT_L2CAP_TX_MTU (CONFIG_BT_L2CAP_TX_MTU)
37 
38 /** Maximum Transmission Unit (MTU) for an incoming L2CAP PDU. */
39 #define BT_L2CAP_RX_MTU (CONFIG_BT_BUF_ACL_RX_SIZE - BT_L2CAP_HDR_SIZE)
40 
41 /** @brief Helper to calculate needed buffer size for L2CAP PDUs.
42  *         Useful for creating buffer pools.
43  *
44  *  @param mtu Needed L2CAP PDU MTU.
45  *
46  *  @return Needed buffer size to match the requested L2CAP PDU MTU.
47  */
48 #define BT_L2CAP_BUF_SIZE(mtu) BT_BUF_ACL_SIZE(BT_L2CAP_HDR_SIZE + (mtu))
49 
50 /** L2CAP SDU header size, used for buffer size calculations */
51 #define BT_L2CAP_SDU_HDR_SIZE           2
52 
53 /** @brief Maximum Transmission Unit for an unsegmented outgoing L2CAP SDU.
54  *
55  *  The Maximum Transmission Unit for an outgoing L2CAP SDU when sent without
56  *  segmentation, i.e. a single L2CAP SDU will fit inside a single L2CAP PDU.
57  *
58  *  The MTU for outgoing L2CAP SDUs with segmentation is defined by the
59  *  size of the application buffer pool.
60  */
61 #define BT_L2CAP_SDU_TX_MTU (BT_L2CAP_TX_MTU - BT_L2CAP_SDU_HDR_SIZE)
62 
63 /** @brief Maximum Transmission Unit for an unsegmented incoming L2CAP SDU.
64  *
65  *  The Maximum Transmission Unit for an incoming L2CAP SDU when sent without
66  *  segmentation, i.e. a single L2CAP SDU will fit inside a single L2CAP PDU.
67  *
68  *  The MTU for incoming L2CAP SDUs with segmentation is defined by the
69  *  size of the application buffer pool. The application will have to define
70  *  an alloc_buf callback for the channel in order to support receiving
71  *  segmented L2CAP SDUs.
72  */
73 #define BT_L2CAP_SDU_RX_MTU (BT_L2CAP_RX_MTU - BT_L2CAP_SDU_HDR_SIZE)
74 
75 /**
76  *
77  *  @brief Helper to calculate needed buffer size for L2CAP SDUs.
78  *         Useful for creating buffer pools.
79  *
80  *  @param mtu Required BT_L2CAP_*_SDU.
81  *
82  *  @return Needed buffer size to match the requested L2CAP SDU MTU.
83  */
84 #define BT_L2CAP_SDU_BUF_SIZE(mtu) BT_L2CAP_BUF_SIZE(BT_L2CAP_SDU_HDR_SIZE + (mtu))
85 
86 struct bt_l2cap_chan;
87 
88 /** @typedef bt_l2cap_chan_destroy_t
89  *  @brief Channel destroy callback
90  *
91  *  @param chan Channel object.
92  */
93 typedef void (*bt_l2cap_chan_destroy_t)(struct bt_l2cap_chan *chan);
94 
95 /** @brief Life-span states of L2CAP CoC channel.
96  *
97  *  Used only by internal APIs dealing with setting channel to proper state
98  *  depending on operational context.
99  *
100  *  A channel enters the @ref BT_L2CAP_CONNECTING state upon @ref
101  *  bt_l2cap_chan_connect, @ref bt_l2cap_ecred_chan_connect or upon returning
102  *  from @ref bt_l2cap_server.accept.
103  *
104  *  When a channel leaves the @ref BT_L2CAP_CONNECTING state, @ref
105  *  bt_l2cap_chan_ops.connected is called.
106  */
107 typedef enum bt_l2cap_chan_state {
108 	/** Channel disconnected */
109 	BT_L2CAP_DISCONNECTED,
110 	/** Channel in connecting state */
111 	BT_L2CAP_CONNECTING,
112 	/** Channel in config state, BR/EDR specific */
113 	BT_L2CAP_CONFIG,
114 	/** Channel ready for upper layer traffic on it */
115 	BT_L2CAP_CONNECTED,
116 	/** Channel in disconnecting state */
117 	BT_L2CAP_DISCONNECTING,
118 
119 } __packed bt_l2cap_chan_state_t;
120 
121 /** @brief Status of L2CAP channel. */
122 typedef enum bt_l2cap_chan_status {
123 	/** Channel can send at least one PDU */
124 	BT_L2CAP_STATUS_OUT,
125 
126 	/** @brief Channel shutdown status
127 	 *
128 	 * Once this status is notified it means the channel will no longer be
129 	 * able to transmit or receive data.
130 	 */
131 	BT_L2CAP_STATUS_SHUTDOWN,
132 
133 	/** @brief Channel encryption pending status */
134 	BT_L2CAP_STATUS_ENCRYPT_PENDING,
135 
136 	/* Total number of status - must be at the end of the enum */
137 	BT_L2CAP_NUM_STATUS,
138 } __packed bt_l2cap_chan_status_t;
139 
140 /** @brief L2CAP Channel structure. */
141 struct bt_l2cap_chan {
142 	/** Channel connection reference */
143 	struct bt_conn			*conn;
144 	/** Channel operations reference */
145 	const struct bt_l2cap_chan_ops	*ops;
146 	sys_snode_t			node;
147 	bt_l2cap_chan_destroy_t		destroy;
148 
149 	ATOMIC_DEFINE(status, BT_L2CAP_NUM_STATUS);
150 };
151 
152 /** @brief LE L2CAP Endpoint structure. */
153 struct bt_l2cap_le_endpoint {
154 	/** Endpoint Channel Identifier (CID) */
155 	uint16_t				cid;
156 	/** Endpoint Maximum Transmission Unit */
157 	uint16_t				mtu;
158 	/** Endpoint Maximum PDU payload Size */
159 	uint16_t				mps;
160 	/** Endpoint initial credits */
161 	uint16_t				init_credits;
162 	/** Endpoint credits */
163 	atomic_t			credits;
164 };
165 
166 /** @brief LE L2CAP Channel structure. */
167 struct bt_l2cap_le_chan {
168 	/** Common L2CAP channel reference object */
169 	struct bt_l2cap_chan		chan;
170 	/** @brief Channel Receiving Endpoint.
171 	 *
172 	 *  If the application has set an alloc_buf channel callback for the
173 	 *  channel to support receiving segmented L2CAP SDUs the application
174 	 *  should inititalize the MTU of the Receiving Endpoint. Otherwise the
175 	 *  MTU of the receiving endpoint will be initialized to
176 	 *  @ref BT_L2CAP_SDU_RX_MTU by the stack.
177 	 *
178 	 *  This is the source of the MTU, MPS and credit values when sending
179 	 *  L2CAP_LE_CREDIT_BASED_CONNECTION_REQ/RSP and
180 	 *  L2CAP_CONFIGURATION_REQ.
181 	 */
182 	struct bt_l2cap_le_endpoint	rx;
183 
184 	/** Pending RX MTU on ECFC reconfigure, used internally by stack */
185 	uint16_t pending_rx_mtu;
186 
187 	/** Channel Transmission Endpoint.
188 	 *
189 	 * This is an image of the remote's rx.
190 	 *
191 	 * The MTU and MPS is controlled by the remote by
192 	 * L2CAP_LE_CREDIT_BASED_CONNECTION_REQ/RSP or L2CAP_CONFIGURATION_REQ.
193 	 */
194 	struct bt_l2cap_le_endpoint	tx;
195 #if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
196 	/** Channel Transmission queue */
197 	struct k_fifo                   tx_queue;
198 	/** Channel Pending Transmission buffer  */
199 	struct net_buf                  *tx_buf;
200 	/** Channel Transmission work  */
201 	struct k_work_delayable		tx_work;
202 	/** Segment SDU packet from upper layer */
203 	struct net_buf			*_sdu;
204 	uint16_t			_sdu_len;
205 #if defined(CONFIG_BT_L2CAP_SEG_RECV)
206 	uint16_t			_sdu_len_done;
207 #endif /* CONFIG_BT_L2CAP_SEG_RECV */
208 
209 	struct k_work			rx_work;
210 	struct k_fifo			rx_queue;
211 
212 	bt_l2cap_chan_state_t		state;
213 	/** Remote PSM to be connected */
214 	uint16_t			psm;
215 	/** Helps match request context during CoC */
216 	uint8_t				ident;
217 	bt_security_t			required_sec_level;
218 
219 	/* Response Timeout eXpired (RTX) timer */
220 	struct k_work_delayable		rtx_work;
221 	struct k_work_sync		rtx_sync;
222 #endif
223 };
224 
225 /**
226  *  @brief Helper macro getting container object of type bt_l2cap_le_chan
227  *  address having the same container chan member address as object in question.
228  *
229  *  @param _ch Address of object of bt_l2cap_chan type
230  *
231  *  @return Address of in memory bt_l2cap_le_chan object type containing
232  *          the address of in question object.
233  */
234 #define BT_L2CAP_LE_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan)
235 
236 /** @brief BREDR L2CAP Endpoint structure. */
237 struct bt_l2cap_br_endpoint {
238 	/** Endpoint Channel Identifier (CID) */
239 	uint16_t				cid;
240 	/** Endpoint Maximum Transmission Unit */
241 	uint16_t				mtu;
242 };
243 
244 /** @brief BREDR L2CAP Channel structure. */
245 struct bt_l2cap_br_chan {
246 	/** Common L2CAP channel reference object */
247 	struct bt_l2cap_chan		chan;
248 	/** Channel Receiving Endpoint */
249 	struct bt_l2cap_br_endpoint	rx;
250 	/** Channel Transmission Endpoint */
251 	struct bt_l2cap_br_endpoint	tx;
252 	/* For internal use only */
253 	atomic_t			flags[1];
254 
255 	bt_l2cap_chan_state_t		state;
256 	/** Remote PSM to be connected */
257 	uint16_t			psm;
258 	/** Helps match request context during CoC */
259 	uint8_t				ident;
260 	bt_security_t			required_sec_level;
261 
262 	/* Response Timeout eXpired (RTX) timer */
263 	struct k_work_delayable		rtx_work;
264 	struct k_work_sync		rtx_sync;
265 };
266 
267 /** @brief L2CAP Channel operations structure. */
268 struct bt_l2cap_chan_ops {
269 	/** @brief Channel connected callback
270 	 *
271 	 *  If this callback is provided it will be called whenever the
272 	 *  connection completes.
273 	 *
274 	 *  @param chan The channel that has been connected
275 	 */
276 	void (*connected)(struct bt_l2cap_chan *chan);
277 
278 	/** @brief Channel disconnected callback
279 	 *
280 	 *  If this callback is provided it will be called whenever the
281 	 *  channel is disconnected, including when a connection gets
282 	 *  rejected.
283 	 *
284 	 *  @param chan The channel that has been Disconnected
285 	 */
286 	void (*disconnected)(struct bt_l2cap_chan *chan);
287 
288 	/** @brief Channel encrypt_change callback
289 	 *
290 	 *  If this callback is provided it will be called whenever the
291 	 *  security level changed (indirectly link encryption done) or
292 	 *  authentication procedure fails. In both cases security initiator
293 	 *  and responder got the final status (HCI status) passed by
294 	 *  related to encryption and authentication events from local host's
295 	 *  controller.
296 	 *
297 	 *  @param chan The channel which has made encryption status changed.
298 	 *  @param status HCI status of performed security procedure caused
299 	 *  by channel security requirements. The value is populated
300 	 *  by HCI layer and set to 0 when success and to non-zero (reference to
301 	 *  HCI Error Codes) when security/authentication failed.
302 	 */
303 	void (*encrypt_change)(struct bt_l2cap_chan *chan, uint8_t hci_status);
304 
305 	/** @brief Channel alloc_seg callback
306 	 *
307 	 *  If this callback is provided the channel will use it to allocate
308 	 *  buffers to store segments. This avoids wasting big SDU buffers with
309 	 *  potentially much smaller PDUs. If this callback is supplied, it must
310 	 *  return a valid buffer.
311 	 *
312 	 *  @param chan The channel requesting a buffer.
313 	 *
314 	 *  @return Allocated buffer.
315 	 */
316 	struct net_buf *(*alloc_seg)(struct bt_l2cap_chan *chan);
317 
318 	/** @brief Channel alloc_buf callback
319 	 *
320 	 *  If this callback is provided the channel will use it to allocate
321 	 *  buffers to store incoming data. Channels that requires segmentation
322 	 *  must set this callback.
323 	 *  If the application has not set a callback the L2CAP SDU MTU will be
324 	 *  truncated to @ref BT_L2CAP_SDU_RX_MTU.
325 	 *
326 	 *  @param chan The channel requesting a buffer.
327 	 *
328 	 *  @return Allocated buffer.
329 	 */
330 	struct net_buf *(*alloc_buf)(struct bt_l2cap_chan *chan);
331 
332 	/** @brief Channel recv callback
333 	 *
334 	 *  @param chan The channel receiving data.
335 	 *  @param buf Buffer containing incoming data.
336 	 *
337 	 *  @return 0 in case of success or negative value in case of error.
338 	 *  @return -EINPROGRESS in case where user has to confirm once the data
339 	 *                       has been processed by calling
340 	 *                       @ref bt_l2cap_chan_recv_complete passing back
341 	 *                       the buffer received with its original user_data
342 	 *                       which contains the number of segments/credits
343 	 *                       used by the packet.
344 	 */
345 	int (*recv)(struct bt_l2cap_chan *chan, struct net_buf *buf);
346 
347 	/** @brief Channel sent callback
348 	 *
349 	 *  This callback will be called once the controller marks the SDU
350 	 *  as completed. When the controller does so is implementation
351 	 *  dependent. It could be after the SDU is enqueued for transmission,
352 	 *  or after it is sent on air.
353 	 *
354 	 *  @param chan The channel which has sent data.
355 	 */
356 	void (*sent)(struct bt_l2cap_chan *chan);
357 
358 	/** @brief Channel status callback
359 	 *
360 	 *  If this callback is provided it will be called whenever the
361 	 *  channel status changes.
362 	 *
363 	 *  @param chan The channel which status changed
364 	 *  @param status The channel status
365 	 */
366 	void (*status)(struct bt_l2cap_chan *chan, atomic_t *status);
367 
368 	/* @brief Channel released callback
369 	 *
370 	 * If this callback is set it is called when the stack has release all
371 	 * references to the channel object.
372 	 */
373 	void (*released)(struct bt_l2cap_chan *chan);
374 
375 	/** @brief Channel reconfigured callback
376 	 *
377 	 *  If this callback is provided it will be called whenever peer or
378 	 *  local device requested reconfiguration. Application may check
379 	 *  updated MTU and MPS values by inspecting chan->le endpoints.
380 	 *
381 	 *  @param chan The channel which was reconfigured
382 	 */
383 	void (*reconfigured)(struct bt_l2cap_chan *chan);
384 
385 #if defined(CONFIG_BT_L2CAP_SEG_RECV)
386 	/** @brief Handle L2CAP segments directly
387 	 *
388 	 *  This is an alternative to @ref bt_l2cap_chan_ops.recv. They cannot
389 	 *  be used together.
390 	 *
391 	 *  This is called immediately for each received segment.
392 	 *
393 	 *  Unlike with @ref bt_l2cap_chan_ops.recv, flow control is explicit.
394 	 *  Each time this handler is invoked, the remote has permanently used
395 	 *  up one credit. Use @ref bt_l2cap_chan_give_credits to give credits.
396 	 *
397 	 *  The start of an SDU is marked by `seg_offset == 0`. The end of an
398 	 *  SDU is marked by `seg_offset + seg->len == sdu_len`.
399 	 *
400 	 *  The stack guarantees that:
401 	 *    - The sender had the credit.
402 	 *    - The SDU length does not exceed MTU.
403 	 *    - The segment length does not exceed MPS.
404 	 *
405 	 *  Additionally, the L2CAP protocol is such that:
406 	 *    - Segments come in order.
407 	 *    - SDUs cannot be interleaved or aborted halfway.
408 	 *
409 	 *  @note With this alternative API, the application is responsible for
410 	 *  setting the RX MTU and MPS. The MPS must not exceed @ref BT_L2CAP_RX_MTU.
411 	 *
412 	 *  @param chan The receiving channel.
413 	 *  @param sdu_len Byte length of the SDU this segment is part of.
414 	 *  @param seg_offset The byte offset of this segment in the SDU.
415 	 *  @param seg The segment payload.
416 	 */
417 	void (*seg_recv)(struct bt_l2cap_chan *chan, size_t sdu_len,
418 			 off_t seg_offset, struct net_buf_simple *seg);
419 #endif /* CONFIG_BT_L2CAP_SEG_RECV */
420 };
421 
422 /**
423  *  @brief Headroom needed for outgoing L2CAP PDUs.
424  */
425 #define BT_L2CAP_CHAN_SEND_RESERVE (BT_L2CAP_BUF_SIZE(0))
426 
427 /**
428  * @brief Headroom needed for outgoing L2CAP SDUs.
429  */
430 #define BT_L2CAP_SDU_CHAN_SEND_RESERVE (BT_L2CAP_SDU_BUF_SIZE(0))
431 
432 /** @brief L2CAP Server structure. */
433 struct bt_l2cap_server {
434 	/** @brief Server PSM.
435 	 *
436 	 *  Possible values:
437 	 *  0               A dynamic value will be auto-allocated when
438 	 *                  bt_l2cap_server_register() is called.
439 	 *
440 	 *  0x0001-0x007f   Standard, Bluetooth SIG-assigned fixed values.
441 	 *
442 	 *  0x0080-0x00ff   Dynamically allocated. May be pre-set by the
443 	 *                  application before server registration (not
444 	 *                  recommended however), or auto-allocated by the
445 	 *                  stack if the app gave 0 as the value.
446 	 */
447 	uint16_t			psm;
448 
449 	/** Required minimum security level */
450 	bt_security_t		sec_level;
451 
452 	/** @brief Server accept callback
453 	 *
454 	 *  This callback is called whenever a new incoming connection requires
455 	 *  authorization.
456 	 *
457 	 *  @param conn The connection that is requesting authorization
458 	 *  @param server Pointer to the server structure this callback relates to
459 	 *  @param chan Pointer to received the allocated channel
460 	 *
461 	 *  @return 0 in case of success or negative value in case of error.
462 	 *  @return -ENOMEM if no available space for new channel.
463 	 *  @return -EACCES if application did not authorize the connection.
464 	 *  @return -EPERM if encryption key size is too short.
465 	 */
466 	int (*accept)(struct bt_conn *conn, struct bt_l2cap_server *server,
467 		      struct bt_l2cap_chan **chan);
468 
469 	sys_snode_t node;
470 };
471 
472 /** @brief Register L2CAP server.
473  *
474  *  Register L2CAP server for a PSM, each new connection is authorized using
475  *  the accept() callback which in case of success shall allocate the channel
476  *  structure to be used by the new connection.
477  *
478  *  For fixed, SIG-assigned PSMs (in the range 0x0001-0x007f) the PSM should
479  *  be assigned to server->psm before calling this API. For dynamic PSMs
480  *  (in the range 0x0080-0x00ff) server->psm may be pre-set to a given value
481  *  (this is however not recommended) or be left as 0, in which case upon
482  *  return a newly allocated value will have been assigned to it. For
483  *  dynamically allocated values the expectation is that it's exposed through
484  *  a GATT service, and that's how L2CAP clients discover how to connect to
485  *  the server.
486  *
487  *  @param server Server structure.
488  *
489  *  @return 0 in case of success or negative value in case of error.
490  */
491 int bt_l2cap_server_register(struct bt_l2cap_server *server);
492 
493 /** @brief Register L2CAP server on BR/EDR oriented connection.
494  *
495  *  Register L2CAP server for a PSM, each new connection is authorized using
496  *  the accept() callback which in case of success shall allocate the channel
497  *  structure to be used by the new connection.
498  *
499  *  @param server Server structure.
500  *
501  *  @return 0 in case of success or negative value in case of error.
502  */
503 int bt_l2cap_br_server_register(struct bt_l2cap_server *server);
504 
505 /** @brief Connect Enhanced Credit Based L2CAP channels
506  *
507  *  Connect up to 5 L2CAP channels by PSM, once the connection is completed
508  *  each channel connected() callback will be called. If the connection is
509  *  rejected disconnected() callback is called instead.
510  *
511  *  @param conn Connection object.
512  *  @param chans Array of channel objects.
513  *  @param psm Channel PSM to connect to.
514  *
515  *  @return 0 in case of success or negative value in case of error.
516  */
517 int bt_l2cap_ecred_chan_connect(struct bt_conn *conn,
518 				struct bt_l2cap_chan **chans, uint16_t psm);
519 
520 /** @brief Reconfigure Enhanced Credit Based L2CAP channels
521  *
522  *  Reconfigure up to 5 L2CAP channels. Channels must be from the same bt_conn.
523  *  Once reconfiguration is completed each channel reconfigured() callback will
524  *  be called. MTU cannot be decreased on any of provided channels.
525  *
526  *  @param chans Array of channel objects. Null-terminated. Elements after the
527  *               first 5 are silently ignored.
528  *  @param mtu Channel MTU to reconfigure to.
529  *
530  *  @return 0 in case of success or negative value in case of error.
531  */
532 int bt_l2cap_ecred_chan_reconfigure(struct bt_l2cap_chan **chans, uint16_t mtu);
533 
534 /** @brief Connect L2CAP channel
535  *
536  *  Connect L2CAP channel by PSM, once the connection is completed channel
537  *  connected() callback will be called. If the connection is rejected
538  *  disconnected() callback is called instead.
539  *  Channel object passed (over an address of it) as second parameter shouldn't
540  *  be instantiated in application as standalone. Instead of, application should
541  *  create transport dedicated L2CAP objects, i.e. type of bt_l2cap_le_chan for
542  *  LE and/or type of bt_l2cap_br_chan for BR/EDR. Then pass to this API
543  *  the location (address) of bt_l2cap_chan type object which is a member
544  *  of both transport dedicated objects.
545  *
546  *  @param conn Connection object.
547  *  @param chan Channel object.
548  *  @param psm Channel PSM to connect to.
549  *
550  *  @return 0 in case of success or negative value in case of error.
551  */
552 int bt_l2cap_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan,
553 			  uint16_t psm);
554 
555 /** @brief Disconnect L2CAP channel
556  *
557  *  Disconnect L2CAP channel, if the connection is pending it will be
558  *  canceled and as a result the channel disconnected() callback is called.
559  *  Regarding to input parameter, to get details see reference description
560  *  to bt_l2cap_chan_connect() API above.
561  *
562  *  @param chan Channel object.
563  *
564  *  @return 0 in case of success or negative value in case of error.
565  */
566 int bt_l2cap_chan_disconnect(struct bt_l2cap_chan *chan);
567 
568 /** @brief Send data to L2CAP channel
569  *
570  *  Send data from buffer to the channel. If credits are not available, buf will
571  *  be queued and sent as and when credits are received from peer.
572  *  Regarding to first input parameter, to get details see reference description
573  *  to bt_l2cap_chan_connect() API above.
574  *
575  *  When sending L2CAP data over an BR/EDR connection the application is sending
576  *  L2CAP PDUs. The application is required to have reserved
577  *  @ref BT_L2CAP_CHAN_SEND_RESERVE bytes in the buffer before sending.
578  *  The application should use the BT_L2CAP_BUF_SIZE() helper to correctly
579  *  size the buffers for the for the outgoing buffer pool.
580  *
581  *  When sending L2CAP data over an LE connection the application is sending
582  *  L2CAP SDUs. The application shall reserve
583  *  @ref BT_L2CAP_SDU_CHAN_SEND_RESERVE bytes in the buffer before sending.
584  *
585  *  The application can use the BT_L2CAP_SDU_BUF_SIZE() helper to correctly size
586  *  the buffer to account for the reserved headroom.
587  *
588  *  When segmenting an L2CAP SDU into L2CAP PDUs the stack will first attempt to
589  *  allocate buffers from the channel's `alloc_seg` callback and will fallback
590  *  on the stack's global buffer pool (sized
591  *  @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}).
592  *
593  *  @note Buffer ownership is transferred to the stack in case of success, in
594  *  case of an error the caller retains the ownership of the buffer.
595  *
596  *  @return 0 in case of success or negative value in case of error.
597  *  @return -EINVAL if `buf` or `chan` is NULL.
598  *  @return -EINVAL if `chan` is not either BR/EDR or LE credit-based.
599  *  @return -EINVAL if buffer doesn't have enough bytes reserved to fit header.
600  *  @return -EMSGSIZE if `buf` is larger than `chan`'s MTU.
601  *  @return -ENOTCONN if underlying conn is disconnected.
602  *  @return -ESHUTDOWN if L2CAP channel is disconnected.
603  *  @return -other (from lower layers) if chan is BR/EDR.
604  */
605 int bt_l2cap_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf);
606 
607 /** @brief Give credits to the remote
608  *
609  *  Only available for channels using @ref bt_l2cap_chan_ops.seg_recv.
610  *  @kconfig{CONFIG_BT_L2CAP_SEG_RECV} must be enabled to make this function
611  *  available.
612  *
613  *  Each credit given allows the peer to send one segment.
614  *
615  *  This function depends on a valid @p chan object. Make sure to
616  *  default-initialize or memset @p chan when allocating or reusing it for new
617  *  connections.
618  *
619  *  Adding zero credits is not allowed.
620  *
621  *  Credits can be given before entering the @ref BT_L2CAP_CONNECTING state.
622  *  Doing so will adjust the 'initial credits' sent in the connection PDU.
623  *
624  *  Must not be called while the channel is in @ref BT_L2CAP_CONNECTING state.
625  *
626  *  @return 0 in case of success or negative value in case of error.
627  */
628 int bt_l2cap_chan_give_credits(struct bt_l2cap_chan *chan, uint16_t additional_credits);
629 
630 /** @brief Complete receiving L2CAP channel data
631  *
632  * Complete the reception of incoming data. This shall only be called if the
633  * channel recv callback has returned -EINPROGRESS to process some incoming
634  * data. The buffer shall contain the original user_data as that is used for
635  * storing the credits/segments used by the packet.
636  *
637  * @param chan Channel object.
638  * @param buf Buffer containing the data.
639  *
640  *  @return 0 in case of success or negative value in case of error.
641  */
642 int bt_l2cap_chan_recv_complete(struct bt_l2cap_chan *chan,
643 				struct net_buf *buf);
644 
645 #ifdef __cplusplus
646 }
647 #endif
648 
649 /**
650  * @}
651  */
652 
653 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_ */
654