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 output status */
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 	 *  If this callback is provided it will be called whenever a SDU has
350 	 *  been completely sent.
351 	 *
352 	 *  @param chan The channel which has sent data.
353 	 */
354 	void (*sent)(struct bt_l2cap_chan *chan);
355 
356 	/** @brief Channel status callback
357 	 *
358 	 *  If this callback is provided it will be called whenever the
359 	 *  channel status changes.
360 	 *
361 	 *  @param chan The channel which status changed
362 	 *  @param status The channel status
363 	 */
364 	void (*status)(struct bt_l2cap_chan *chan, atomic_t *status);
365 
366 	/* @brief Channel released callback
367 	 *
368 	 * If this callback is set it is called when the stack has release all
369 	 * references to the channel object.
370 	 */
371 	void (*released)(struct bt_l2cap_chan *chan);
372 
373 	/** @brief Channel reconfigured callback
374 	 *
375 	 *  If this callback is provided it will be called whenever peer or
376 	 *  local device requested reconfiguration. Application may check
377 	 *  updated MTU and MPS values by inspecting chan->le endpoints.
378 	 *
379 	 *  @param chan The channel which was reconfigured
380 	 */
381 	void (*reconfigured)(struct bt_l2cap_chan *chan);
382 
383 #if defined(CONFIG_BT_L2CAP_SEG_RECV)
384 	/** @brief Handle L2CAP segments directly
385 	 *
386 	 *  This is an alternative to @ref bt_l2cap_chan_ops.recv. They cannot
387 	 *  be used together.
388 	 *
389 	 *  This is called immediately for each received segment.
390 	 *
391 	 *  Unlike with @ref bt_l2cap_chan_ops.recv, flow control is explicit.
392 	 *  Each time this handler is invoked, the remote has permanently used
393 	 *  up one credit. Use @ref bt_l2cap_chan_give_credits to give credits.
394 	 *
395 	 *  The start of an SDU is marked by `seg_offset == 0`. The end of an
396 	 *  SDU is marked by `seg_offset + seg->len == sdu_len`.
397 	 *
398 	 *  The stack guarantees that:
399 	 *    - The sender had the credit.
400 	 *    - The SDU length does not exceed MTU.
401 	 *    - The segment length does not exceed MPS.
402 	 *
403 	 *  Additionally, the L2CAP protocol is such that:
404 	 *    - Segments come in order.
405 	 *    - SDUs cannot be interleaved or aborted halfway.
406 	 *
407 	 *  @note With this alternative API, the application is responsible for
408 	 *  setting the RX MTU and MPS. The MPS must not exceed @ref BT_L2CAP_RX_MTU.
409 	 *
410 	 *  @param chan The receiving channel.
411 	 *  @param sdu_len Byte length of the SDU this segment is part of.
412 	 *  @param seg_offset The byte offset of this segment in the SDU.
413 	 *  @param seg The segment payload.
414 	 */
415 	void (*seg_recv)(struct bt_l2cap_chan *chan, size_t sdu_len,
416 			 off_t seg_offset, struct net_buf_simple *seg);
417 #endif /* CONFIG_BT_L2CAP_SEG_RECV */
418 };
419 
420 /**
421  *  @brief Headroom needed for outgoing L2CAP PDUs.
422  */
423 #define BT_L2CAP_CHAN_SEND_RESERVE (BT_L2CAP_BUF_SIZE(0))
424 
425 /**
426  * @brief Headroom needed for outgoing L2CAP SDUs.
427  */
428 #define BT_L2CAP_SDU_CHAN_SEND_RESERVE (BT_L2CAP_SDU_BUF_SIZE(0))
429 
430 /** @brief L2CAP Server structure. */
431 struct bt_l2cap_server {
432 	/** @brief Server PSM.
433 	 *
434 	 *  Possible values:
435 	 *  0               A dynamic value will be auto-allocated when
436 	 *                  bt_l2cap_server_register() is called.
437 	 *
438 	 *  0x0001-0x007f   Standard, Bluetooth SIG-assigned fixed values.
439 	 *
440 	 *  0x0080-0x00ff   Dynamically allocated. May be pre-set by the
441 	 *                  application before server registration (not
442 	 *                  recommended however), or auto-allocated by the
443 	 *                  stack if the app gave 0 as the value.
444 	 */
445 	uint16_t			psm;
446 
447 	/** Required minimum security level */
448 	bt_security_t		sec_level;
449 
450 	/** @brief Server accept callback
451 	 *
452 	 *  This callback is called whenever a new incoming connection requires
453 	 *  authorization.
454 	 *
455 	 *  @param conn The connection that is requesting authorization
456 	 *  @param server Pointer to the server structure this callback relates to
457 	 *  @param chan Pointer to received the allocated channel
458 	 *
459 	 *  @return 0 in case of success or negative value in case of error.
460 	 *  @return -ENOMEM if no available space for new channel.
461 	 *  @return -EACCES if application did not authorize the connection.
462 	 *  @return -EPERM if encryption key size is too short.
463 	 */
464 	int (*accept)(struct bt_conn *conn, struct bt_l2cap_server *server,
465 		      struct bt_l2cap_chan **chan);
466 
467 	sys_snode_t node;
468 };
469 
470 /** @brief Register L2CAP server.
471  *
472  *  Register L2CAP server for a PSM, each new connection is authorized using
473  *  the accept() callback which in case of success shall allocate the channel
474  *  structure to be used by the new connection.
475  *
476  *  For fixed, SIG-assigned PSMs (in the range 0x0001-0x007f) the PSM should
477  *  be assigned to server->psm before calling this API. For dynamic PSMs
478  *  (in the range 0x0080-0x00ff) server->psm may be pre-set to a given value
479  *  (this is however not recommended) or be left as 0, in which case upon
480  *  return a newly allocated value will have been assigned to it. For
481  *  dynamically allocated values the expectation is that it's exposed through
482  *  a GATT service, and that's how L2CAP clients discover how to connect to
483  *  the server.
484  *
485  *  @param server Server structure.
486  *
487  *  @return 0 in case of success or negative value in case of error.
488  */
489 int bt_l2cap_server_register(struct bt_l2cap_server *server);
490 
491 /** @brief Register L2CAP server on BR/EDR oriented connection.
492  *
493  *  Register L2CAP server for a PSM, each new connection is authorized using
494  *  the accept() callback which in case of success shall allocate the channel
495  *  structure to be used by the new connection.
496  *
497  *  @param server Server structure.
498  *
499  *  @return 0 in case of success or negative value in case of error.
500  */
501 int bt_l2cap_br_server_register(struct bt_l2cap_server *server);
502 
503 /** @brief Connect Enhanced Credit Based L2CAP channels
504  *
505  *  Connect up to 5 L2CAP channels by PSM, once the connection is completed
506  *  each channel connected() callback will be called. If the connection is
507  *  rejected disconnected() callback is called instead.
508  *
509  *  @param conn Connection object.
510  *  @param chans Array of channel objects.
511  *  @param psm Channel PSM to connect to.
512  *
513  *  @return 0 in case of success or negative value in case of error.
514  */
515 int bt_l2cap_ecred_chan_connect(struct bt_conn *conn,
516 				struct bt_l2cap_chan **chans, uint16_t psm);
517 
518 /** @brief Reconfigure Enhanced Credit Based L2CAP channels
519  *
520  *  Reconfigure up to 5 L2CAP channels. Channels must be from the same bt_conn.
521  *  Once reconfiguration is completed each channel reconfigured() callback will
522  *  be called. MTU cannot be decreased on any of provided channels.
523  *
524  *  @param chans Array of channel objects. Null-terminated. Elements after the
525  *               first 5 are silently ignored.
526  *  @param mtu Channel MTU to reconfigure to.
527  *
528  *  @return 0 in case of success or negative value in case of error.
529  */
530 int bt_l2cap_ecred_chan_reconfigure(struct bt_l2cap_chan **chans, uint16_t mtu);
531 
532 /** @brief Connect L2CAP channel
533  *
534  *  Connect L2CAP channel by PSM, once the connection is completed channel
535  *  connected() callback will be called. If the connection is rejected
536  *  disconnected() callback is called instead.
537  *  Channel object passed (over an address of it) as second parameter shouldn't
538  *  be instantiated in application as standalone. Instead of, application should
539  *  create transport dedicated L2CAP objects, i.e. type of bt_l2cap_le_chan for
540  *  LE and/or type of bt_l2cap_br_chan for BR/EDR. Then pass to this API
541  *  the location (address) of bt_l2cap_chan type object which is a member
542  *  of both transport dedicated objects.
543  *
544  *  @param conn Connection object.
545  *  @param chan Channel object.
546  *  @param psm Channel PSM to connect to.
547  *
548  *  @return 0 in case of success or negative value in case of error.
549  */
550 int bt_l2cap_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan,
551 			  uint16_t psm);
552 
553 /** @brief Disconnect L2CAP channel
554  *
555  *  Disconnect L2CAP channel, if the connection is pending it will be
556  *  canceled and as a result the channel disconnected() callback is called.
557  *  Regarding to input parameter, to get details see reference description
558  *  to bt_l2cap_chan_connect() API above.
559  *
560  *  @param chan Channel object.
561  *
562  *  @return 0 in case of success or negative value in case of error.
563  */
564 int bt_l2cap_chan_disconnect(struct bt_l2cap_chan *chan);
565 
566 /** @brief Send data to L2CAP channel
567  *
568  *  Send data from buffer to the channel. If credits are not available, buf will
569  *  be queued and sent as and when credits are received from peer.
570  *  Regarding to first input parameter, to get details see reference description
571  *  to bt_l2cap_chan_connect() API above.
572  *
573  *  When sending L2CAP data over an BR/EDR connection the application is sending
574  *  L2CAP PDUs. The application is required to have reserved
575  *  @ref BT_L2CAP_CHAN_SEND_RESERVE bytes in the buffer before sending.
576  *  The application should use the BT_L2CAP_BUF_SIZE() helper to correctly
577  *  size the buffers for the for the outgoing buffer pool.
578  *
579  *  When sending L2CAP data over an LE connection the application is sending
580  *  L2CAP SDUs. The application can optionally reserve
581  *  @ref BT_L2CAP_SDU_CHAN_SEND_RESERVE bytes in the buffer before sending.
582  *  By reserving bytes in the buffer the stack can use this buffer as a segment
583  *  directly, otherwise it will have to allocate a new segment for the first
584  *  segment.
585  *  If the application is reserving the bytes it should use the
586  *  BT_L2CAP_BUF_SIZE() helper to correctly size the buffers for the for the
587  *  outgoing buffer pool.
588  *  When segmenting an L2CAP SDU into L2CAP PDUs the stack will first attempt
589  *  to allocate buffers from the original buffer pool of the L2CAP SDU before
590  *  using the stacks own buffer pool.
591  *
592  *  @note Buffer ownership is transferred to the stack in case of success, in
593  *  case of an error the caller retains the ownership of the buffer.
594  *
595  *  @return Bytes sent in case of success or negative value in case of error.
596  */
597 int bt_l2cap_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf);
598 
599 /** @brief Give credits to the remote
600  *
601  *  Only available for channels using @ref bt_l2cap_chan_ops.seg_recv.
602  *  @kconfig{CONFIG_BT_L2CAP_SEG_RECV} must be enabled to make this function
603  *  available.
604  *
605  *  Each credit given allows the peer to send one segment.
606  *
607  *  This function depends on a valid @p chan object. Make sure to
608  *  default-initialize or memset @p chan when allocating or reusing it for new
609  *  connections.
610  *
611  *  Adding zero credits is not allowed.
612  *
613  *  Credits can be given before entering the @ref BT_L2CAP_CONNECTING state.
614  *  Doing so will adjust the 'initial credits' sent in the connection PDU.
615  *
616  *  Must not be called while the channel is in @ref BT_L2CAP_CONNECTING state.
617  *
618  *  @return 0 in case of success or negative value in case of error.
619  */
620 int bt_l2cap_chan_give_credits(struct bt_l2cap_chan *chan, uint16_t additional_credits);
621 
622 /** @brief Complete receiving L2CAP channel data
623  *
624  * Complete the reception of incoming data. This shall only be called if the
625  * channel recv callback has returned -EINPROGRESS to process some incoming
626  * data. The buffer shall contain the original user_data as that is used for
627  * storing the credits/segments used by the packet.
628  *
629  * @param chan Channel object.
630  * @param buf Buffer containing the data.
631  *
632  *  @return 0 in case of success or negative value in case of error.
633  */
634 int bt_l2cap_chan_recv_complete(struct bt_l2cap_chan *chan,
635 				struct net_buf *buf);
636 
637 #ifdef __cplusplus
638 }
639 #endif
640 
641 /**
642  * @}
643  */
644 
645 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_ */
646