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 credits */ 161 atomic_t credits; 162 }; 163 164 /** @brief LE L2CAP Channel structure. */ 165 struct bt_l2cap_le_chan { 166 /** Common L2CAP channel reference object */ 167 struct bt_l2cap_chan chan; 168 /** @brief Channel Receiving Endpoint. 169 * 170 * If the application has set an alloc_buf channel callback for the 171 * channel to support receiving segmented L2CAP SDUs the application 172 * should initialize the MTU of the Receiving Endpoint. Otherwise the 173 * MTU of the receiving endpoint will be initialized to 174 * @ref BT_L2CAP_SDU_RX_MTU by the stack. 175 * 176 * This is the source of the MTU, MPS and credit values when sending 177 * L2CAP_LE_CREDIT_BASED_CONNECTION_REQ/RSP and 178 * L2CAP_CONFIGURATION_REQ. 179 */ 180 struct bt_l2cap_le_endpoint rx; 181 182 /** Pending RX MTU on ECFC reconfigure, used internally by stack */ 183 uint16_t pending_rx_mtu; 184 185 /** Channel Transmission Endpoint. 186 * 187 * This is an image of the remote's rx. 188 * 189 * The MTU and MPS is controlled by the remote by 190 * L2CAP_LE_CREDIT_BASED_CONNECTION_REQ/RSP or L2CAP_CONFIGURATION_REQ. 191 */ 192 struct bt_l2cap_le_endpoint tx; 193 /** Channel Transmission queue (for SDUs) */ 194 struct k_fifo tx_queue; 195 #if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL) 196 /** Segment SDU packet from upper layer */ 197 struct net_buf *_sdu; 198 uint16_t _sdu_len; 199 #if defined(CONFIG_BT_L2CAP_SEG_RECV) 200 uint16_t _sdu_len_done; 201 #endif /* CONFIG_BT_L2CAP_SEG_RECV */ 202 203 struct k_work rx_work; 204 struct k_fifo rx_queue; 205 206 bt_l2cap_chan_state_t state; 207 /** Remote PSM to be connected */ 208 uint16_t psm; 209 /** Helps match request context during CoC */ 210 uint8_t ident; 211 bt_security_t required_sec_level; 212 213 /* Response Timeout eXpired (RTX) timer */ 214 struct k_work_delayable rtx_work; 215 struct k_work_sync rtx_sync; 216 #endif 217 218 /** @internal To be used with @ref bt_conn.upper_data_ready */ 219 sys_snode_t _pdu_ready; 220 /** @internal To be used with @ref bt_conn.upper_data_ready */ 221 atomic_t _pdu_ready_lock; 222 /** @internal Holds the length of the current PDU/segment */ 223 size_t _pdu_remaining; 224 }; 225 226 /** 227 * @brief Helper macro getting container object of type bt_l2cap_le_chan 228 * address having the same container chan member address as object in question. 229 * 230 * @param _ch Address of object of bt_l2cap_chan type 231 * 232 * @return Address of in memory bt_l2cap_le_chan object type containing 233 * the address of in question object. 234 */ 235 #define BT_L2CAP_LE_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan) 236 237 /** @brief BREDR L2CAP Endpoint structure. */ 238 struct bt_l2cap_br_endpoint { 239 /** Endpoint Channel Identifier (CID) */ 240 uint16_t cid; 241 /** Endpoint Maximum Transmission Unit */ 242 uint16_t mtu; 243 }; 244 245 /** @brief BREDR L2CAP Channel structure. */ 246 struct bt_l2cap_br_chan { 247 /** Common L2CAP channel reference object */ 248 struct bt_l2cap_chan chan; 249 /** Channel Receiving Endpoint */ 250 struct bt_l2cap_br_endpoint rx; 251 /** Channel Transmission Endpoint */ 252 struct bt_l2cap_br_endpoint tx; 253 /* For internal use only */ 254 atomic_t flags[1]; 255 256 bt_l2cap_chan_state_t state; 257 /** Remote PSM to be connected */ 258 uint16_t psm; 259 /** Helps match request context during CoC */ 260 uint8_t ident; 261 bt_security_t required_sec_level; 262 263 /* Response Timeout eXpired (RTX) timer */ 264 struct k_work_delayable rtx_work; 265 struct k_work_sync rtx_sync; 266 267 /** @internal To be used with @ref bt_conn.upper_data_ready */ 268 sys_snode_t _pdu_ready; 269 /** @internal To be used with @ref bt_conn.upper_data_ready */ 270 atomic_t _pdu_ready_lock; 271 /** @internal Queue of net bufs not yet sent to lower layer */ 272 struct k_fifo _pdu_tx_queue; 273 }; 274 275 /** @brief L2CAP Channel operations structure. */ 276 struct bt_l2cap_chan_ops { 277 /** @brief Channel connected callback 278 * 279 * If this callback is provided it will be called whenever the 280 * connection completes. 281 * 282 * @param chan The channel that has been connected 283 */ 284 void (*connected)(struct bt_l2cap_chan *chan); 285 286 /** @brief Channel disconnected callback 287 * 288 * If this callback is provided it will be called whenever the 289 * channel is disconnected, including when a connection gets 290 * rejected. 291 * 292 * @param chan The channel that has been Disconnected 293 */ 294 void (*disconnected)(struct bt_l2cap_chan *chan); 295 296 /** @brief Channel encrypt_change callback 297 * 298 * If this callback is provided it will be called whenever the 299 * security level changed (indirectly link encryption done) or 300 * authentication procedure fails. In both cases security initiator 301 * and responder got the final status (HCI status) passed by 302 * related to encryption and authentication events from local host's 303 * controller. 304 * 305 * @param chan The channel which has made encryption status changed. 306 * @param status HCI status of performed security procedure caused 307 * by channel security requirements. The value is populated 308 * by HCI layer and set to 0 when success and to non-zero (reference to 309 * HCI Error Codes) when security/authentication failed. 310 */ 311 void (*encrypt_change)(struct bt_l2cap_chan *chan, uint8_t hci_status); 312 313 /** @brief Channel alloc_seg callback 314 * 315 * If this callback is provided the channel will use it to allocate 316 * buffers to store segments. This avoids wasting big SDU buffers with 317 * potentially much smaller PDUs. If this callback is supplied, it must 318 * return a valid buffer. 319 * 320 * @param chan The channel requesting a buffer. 321 * 322 * @return Allocated buffer. 323 */ 324 struct net_buf *(*alloc_seg)(struct bt_l2cap_chan *chan); 325 326 /** @brief Channel alloc_buf callback 327 * 328 * If this callback is provided the channel will use it to allocate 329 * buffers to store incoming data. Channels that requires segmentation 330 * must set this callback. 331 * If the application has not set a callback the L2CAP SDU MTU will be 332 * truncated to @ref BT_L2CAP_SDU_RX_MTU. 333 * 334 * @param chan The channel requesting a buffer. 335 * 336 * @return Allocated buffer. 337 */ 338 struct net_buf *(*alloc_buf)(struct bt_l2cap_chan *chan); 339 340 /** @brief Channel recv callback 341 * 342 * @param chan The channel receiving data. 343 * @param buf Buffer containing incoming data. 344 * 345 * @return 0 in case of success or negative value in case of error. 346 * @return -EINPROGRESS in case where user has to confirm once the data 347 * has been processed by calling 348 * @ref bt_l2cap_chan_recv_complete passing back 349 * the buffer received with its original user_data 350 * which contains the number of segments/credits 351 * used by the packet. 352 */ 353 int (*recv)(struct bt_l2cap_chan *chan, struct net_buf *buf); 354 355 /** @brief Channel sent callback 356 * 357 * This callback will be called once the controller marks the SDU 358 * as completed. When the controller does so is implementation 359 * dependent. It could be after the SDU is enqueued for transmission, 360 * or after it is sent on air. 361 * 362 * @param chan The channel which has sent data. 363 */ 364 void (*sent)(struct bt_l2cap_chan *chan); 365 366 /** @brief Channel status callback 367 * 368 * If this callback is provided it will be called whenever the 369 * channel status changes. 370 * 371 * @param chan The channel which status changed 372 * @param status The channel status 373 */ 374 void (*status)(struct bt_l2cap_chan *chan, atomic_t *status); 375 376 /* @brief Channel released callback 377 * 378 * If this callback is set it is called when the stack has release all 379 * references to the channel object. 380 */ 381 void (*released)(struct bt_l2cap_chan *chan); 382 383 /** @brief Channel reconfigured callback 384 * 385 * If this callback is provided it will be called whenever peer or 386 * local device requested reconfiguration. Application may check 387 * updated MTU and MPS values by inspecting chan->le endpoints. 388 * 389 * @param chan The channel which was reconfigured 390 */ 391 void (*reconfigured)(struct bt_l2cap_chan *chan); 392 393 #if defined(CONFIG_BT_L2CAP_SEG_RECV) 394 /** @brief Handle L2CAP segments directly 395 * 396 * This is an alternative to @ref bt_l2cap_chan_ops.recv. They cannot 397 * be used together. 398 * 399 * This is called immediately for each received segment. 400 * 401 * Unlike with @ref bt_l2cap_chan_ops.recv, flow control is explicit. 402 * Each time this handler is invoked, the remote has permanently used 403 * up one credit. Use @ref bt_l2cap_chan_give_credits to give credits. 404 * 405 * The start of an SDU is marked by `seg_offset == 0`. The end of an 406 * SDU is marked by `seg_offset + seg->len == sdu_len`. 407 * 408 * The stack guarantees that: 409 * - The sender had the credit. 410 * - The SDU length does not exceed MTU. 411 * - The segment length does not exceed MPS. 412 * 413 * Additionally, the L2CAP protocol is such that: 414 * - Segments come in order. 415 * - SDUs cannot be interleaved or aborted halfway. 416 * 417 * @note With this alternative API, the application is responsible for 418 * setting the RX MTU and MPS. The MPS must not exceed @ref BT_L2CAP_RX_MTU. 419 * 420 * @param chan The receiving channel. 421 * @param sdu_len Byte length of the SDU this segment is part of. 422 * @param seg_offset The byte offset of this segment in the SDU. 423 * @param seg The segment payload. 424 */ 425 void (*seg_recv)(struct bt_l2cap_chan *chan, size_t sdu_len, 426 off_t seg_offset, struct net_buf_simple *seg); 427 #endif /* CONFIG_BT_L2CAP_SEG_RECV */ 428 }; 429 430 /** 431 * @brief Headroom needed for outgoing L2CAP PDUs. 432 */ 433 #define BT_L2CAP_CHAN_SEND_RESERVE (BT_L2CAP_BUF_SIZE(0)) 434 435 /** 436 * @brief Headroom needed for outgoing L2CAP SDUs. 437 */ 438 #define BT_L2CAP_SDU_CHAN_SEND_RESERVE (BT_L2CAP_SDU_BUF_SIZE(0)) 439 440 /** @brief L2CAP Server structure. */ 441 struct bt_l2cap_server { 442 /** @brief Server PSM. 443 * 444 * Possible values: 445 * 0 A dynamic value will be auto-allocated when 446 * bt_l2cap_server_register() is called. 447 * 448 * 0x0001-0x007f Standard, Bluetooth SIG-assigned fixed values. 449 * 450 * 0x0080-0x00ff Dynamically allocated. May be pre-set by the 451 * application before server registration (not 452 * recommended however), or auto-allocated by the 453 * stack if the app gave 0 as the value. 454 */ 455 uint16_t psm; 456 457 /** Required minimum security level */ 458 bt_security_t sec_level; 459 460 /** @brief Server accept callback 461 * 462 * This callback is called whenever a new incoming connection requires 463 * authorization. 464 * 465 * @param conn The connection that is requesting authorization 466 * @param server Pointer to the server structure this callback relates to 467 * @param chan Pointer to received the allocated channel 468 * 469 * @return 0 in case of success or negative value in case of error. 470 * @return -ENOMEM if no available space for new channel. 471 * @return -EACCES if application did not authorize the connection. 472 * @return -EPERM if encryption key size is too short. 473 */ 474 int (*accept)(struct bt_conn *conn, struct bt_l2cap_server *server, 475 struct bt_l2cap_chan **chan); 476 477 sys_snode_t node; 478 }; 479 480 /** @brief Register L2CAP server. 481 * 482 * Register L2CAP server for a PSM, each new connection is authorized using 483 * the accept() callback which in case of success shall allocate the channel 484 * structure to be used by the new connection. 485 * 486 * For fixed, SIG-assigned PSMs (in the range 0x0001-0x007f) the PSM should 487 * be assigned to server->psm before calling this API. For dynamic PSMs 488 * (in the range 0x0080-0x00ff) server->psm may be pre-set to a given value 489 * (this is however not recommended) or be left as 0, in which case upon 490 * return a newly allocated value will have been assigned to it. For 491 * dynamically allocated values the expectation is that it's exposed through 492 * a GATT service, and that's how L2CAP clients discover how to connect to 493 * the server. 494 * 495 * @param server Server structure. 496 * 497 * @return 0 in case of success or negative value in case of error. 498 */ 499 int bt_l2cap_server_register(struct bt_l2cap_server *server); 500 501 /** @brief Register L2CAP server on BR/EDR oriented connection. 502 * 503 * Register L2CAP server for a PSM, each new connection is authorized using 504 * the accept() callback which in case of success shall allocate the channel 505 * structure to be used by the new connection. 506 * 507 * @param server Server structure. 508 * 509 * @return 0 in case of success or negative value in case of error. 510 */ 511 int bt_l2cap_br_server_register(struct bt_l2cap_server *server); 512 513 /** @brief Connect Enhanced Credit Based L2CAP channels 514 * 515 * Connect up to 5 L2CAP channels by PSM, once the connection is completed 516 * each channel connected() callback will be called. If the connection is 517 * rejected disconnected() callback is called instead. 518 * 519 * @param conn Connection object. 520 * @param chans Array of channel objects. 521 * @param psm Channel PSM to connect to. 522 * 523 * @return 0 in case of success or negative value in case of error. 524 */ 525 int bt_l2cap_ecred_chan_connect(struct bt_conn *conn, 526 struct bt_l2cap_chan **chans, uint16_t psm); 527 528 /** @brief Reconfigure Enhanced Credit Based L2CAP channels 529 * 530 * Reconfigure up to 5 L2CAP channels. Channels must be from the same bt_conn. 531 * Once reconfiguration is completed each channel reconfigured() callback will 532 * be called. MTU cannot be decreased on any of provided channels. 533 * 534 * @param chans Array of channel objects. Null-terminated. Elements after the 535 * first 5 are silently ignored. 536 * @param mtu Channel MTU to reconfigure to. 537 * 538 * @return 0 in case of success or negative value in case of error. 539 */ 540 int bt_l2cap_ecred_chan_reconfigure(struct bt_l2cap_chan **chans, uint16_t mtu); 541 542 /** @brief Connect L2CAP channel 543 * 544 * Connect L2CAP channel by PSM, once the connection is completed channel 545 * connected() callback will be called. If the connection is rejected 546 * disconnected() callback is called instead. 547 * Channel object passed (over an address of it) as second parameter shouldn't 548 * be instantiated in application as standalone. Instead of, application should 549 * create transport dedicated L2CAP objects, i.e. type of bt_l2cap_le_chan for 550 * LE and/or type of bt_l2cap_br_chan for BR/EDR. Then pass to this API 551 * the location (address) of bt_l2cap_chan type object which is a member 552 * of both transport dedicated objects. 553 * 554 * @param conn Connection object. 555 * @param chan Channel object. 556 * @param psm Channel PSM to connect to. 557 * 558 * @return 0 in case of success or negative value in case of error. 559 */ 560 int bt_l2cap_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan, 561 uint16_t psm); 562 563 /** @brief Disconnect L2CAP channel 564 * 565 * Disconnect L2CAP channel, if the connection is pending it will be 566 * canceled and as a result the channel disconnected() callback is called. 567 * Regarding to input parameter, to get details see reference description 568 * to bt_l2cap_chan_connect() API above. 569 * 570 * @param chan Channel object. 571 * 572 * @return 0 in case of success or negative value in case of error. 573 */ 574 int bt_l2cap_chan_disconnect(struct bt_l2cap_chan *chan); 575 576 /** @brief Send data to L2CAP channel 577 * 578 * Send data from buffer to the channel. If credits are not available, buf will 579 * be queued and sent as and when credits are received from peer. 580 * Regarding to first input parameter, to get details see reference description 581 * to bt_l2cap_chan_connect() API above. 582 * 583 * Network buffer fragments (ie `buf->frags`) are not supported. 584 * 585 * When sending L2CAP data over an BR/EDR connection the application is sending 586 * L2CAP PDUs. The application is required to have reserved 587 * @ref BT_L2CAP_CHAN_SEND_RESERVE bytes in the buffer before sending. 588 * The application should use the BT_L2CAP_BUF_SIZE() helper to correctly 589 * size the buffers for the for the outgoing buffer pool. 590 * 591 * When sending L2CAP data over an LE connection the application is sending 592 * L2CAP SDUs. The application shall reserve 593 * @ref BT_L2CAP_SDU_CHAN_SEND_RESERVE bytes in the buffer before sending. 594 * 595 * The application can use the BT_L2CAP_SDU_BUF_SIZE() helper to correctly size 596 * the buffer to account for the reserved headroom. 597 * 598 * When segmenting an L2CAP SDU into L2CAP PDUs the stack will first attempt to 599 * allocate buffers from the channel's `alloc_seg` callback and will fallback 600 * on the stack's global buffer pool (sized 601 * @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}). 602 * 603 * @warning The buffer's user_data _will_ be overwritten by this function. Do 604 * not store anything in it. As soon as a call to this function has been made, 605 * consider ownership of user_data transferred into the stack. 606 * 607 * @note Buffer ownership is transferred to the stack in case of success, in 608 * case of an error the caller retains the ownership of the buffer. 609 * 610 * @return 0 in case of success or negative value in case of error. 611 * @return -EINVAL if `buf` or `chan` is NULL. 612 * @return -EINVAL if `chan` is not either BR/EDR or LE credit-based. 613 * @return -EINVAL if buffer doesn't have enough bytes reserved to fit header. 614 * @return -EINVAL if buffer's reference counter != 1 615 * @return -EMSGSIZE if `buf` is larger than `chan`'s MTU. 616 * @return -ENOTCONN if underlying conn is disconnected. 617 * @return -ESHUTDOWN if L2CAP channel is disconnected. 618 * @return -other (from lower layers) if chan is BR/EDR. 619 */ 620 int bt_l2cap_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf); 621 622 /** @brief Give credits to the remote 623 * 624 * Only available for channels using @ref bt_l2cap_chan_ops.seg_recv. 625 * @kconfig{CONFIG_BT_L2CAP_SEG_RECV} must be enabled to make this function 626 * available. 627 * 628 * Each credit given allows the peer to send one segment. 629 * 630 * This function depends on a valid @p chan object. Make sure to 631 * default-initialize or memset @p chan when allocating or reusing it for new 632 * connections. 633 * 634 * Adding zero credits is not allowed. 635 * 636 * Credits can be given before entering the @ref BT_L2CAP_CONNECTING state. 637 * Doing so will adjust the 'initial credits' sent in the connection PDU. 638 * 639 * Must not be called while the channel is in @ref BT_L2CAP_CONNECTING state. 640 * 641 * @return 0 in case of success or negative value in case of error. 642 */ 643 int bt_l2cap_chan_give_credits(struct bt_l2cap_chan *chan, uint16_t additional_credits); 644 645 /** @brief Complete receiving L2CAP channel data 646 * 647 * Complete the reception of incoming data. This shall only be called if the 648 * channel recv callback has returned -EINPROGRESS to process some incoming 649 * data. The buffer shall contain the original user_data as that is used for 650 * storing the credits/segments used by the packet. 651 * 652 * @param chan Channel object. 653 * @param buf Buffer containing the data. 654 * 655 * @return 0 in case of success or negative value in case of error. 656 */ 657 int bt_l2cap_chan_recv_complete(struct bt_l2cap_chan *chan, 658 struct net_buf *buf); 659 660 #ifdef __cplusplus 661 } 662 #endif 663 664 /** 665 * @} 666 */ 667 668 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_ */ 669