zephyr/subsys/bluetooth/host/l2cap_internal.h

339 lines
8.7 KiB
C
Raw Normal View History

/** @file
* @brief Internal APIs for Bluetooth L2CAP handling.
*/
/*
* Copyright (c) 2015-2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <bluetooth/l2cap.h>
enum l2cap_conn_list_action {
BT_L2CAP_CHAN_LOOKUP,
BT_L2CAP_CHAN_DETACH,
};
#define BT_L2CAP_CID_BR_SIG 0x0001
#define BT_L2CAP_CID_ATT 0x0004
#define BT_L2CAP_CID_LE_SIG 0x0005
#define BT_L2CAP_CID_SMP 0x0006
#define BT_L2CAP_CID_BR_SMP 0x0007
#define BT_L2CAP_PSM_RFCOMM 0x0003
struct bt_l2cap_hdr {
u16_t len;
u16_t cid;
} __packed;
struct bt_l2cap_sig_hdr {
u8_t code;
u8_t ident;
u16_t len;
} __packed;
#define BT_L2CAP_REJ_NOT_UNDERSTOOD 0x0000
#define BT_L2CAP_REJ_MTU_EXCEEDED 0x0001
#define BT_L2CAP_REJ_INVALID_CID 0x0002
#define BT_L2CAP_CMD_REJECT 0x01
struct bt_l2cap_cmd_reject {
u16_t reason;
u8_t data[0];
} __packed;
struct bt_l2cap_cmd_reject_cid_data {
u16_t scid;
u16_t dcid;
} __packed;
#define BT_L2CAP_CONN_REQ 0x02
struct bt_l2cap_conn_req {
u16_t psm;
u16_t scid;
} __packed;
/* command statuses in reposnse */
#define BT_L2CAP_CS_NO_INFO 0x0000
#define BT_L2CAP_CS_AUTHEN_PEND 0x0001
/* valid results in conn response on BR/EDR */
#define BT_L2CAP_BR_SUCCESS 0x0000
#define BT_L2CAP_BR_PENDING 0x0001
#define BT_L2CAP_BR_ERR_PSM_NOT_SUPP 0x0002
#define BT_L2CAP_BR_ERR_SEC_BLOCK 0x0003
#define BT_L2CAP_BR_ERR_NO_RESOURCES 0x0004
#define BT_L2CAP_BR_ERR_INVALID_SCID 0x0006
#define BT_L2CAP_BR_ERR_SCID_IN_USE 0x0007
#define BT_L2CAP_CONN_RSP 0x03
struct bt_l2cap_conn_rsp {
u16_t dcid;
u16_t scid;
u16_t result;
u16_t status;
} __packed;
#define BT_L2CAP_CONF_SUCCESS 0x0000
Bluetooth: L2CAP: Start servicing configuration request from remote To make channel operational in basic mode a process of BR/EDR CoC channel configuration needs to be performed in both directions. Remote peer is mandated as well to inform local about its segmentation requirements. Here is added such request handler focused on grabbing remote MTU which is mandatory in channel setup. > ACL Data RX: Handle 12 flags 0x02 dlen 12 [hci1] 12.133539 L2CAP: Connection Request (0x02) ident 3 len 4 PSM: 1 (0x0001) Source CID: 64 < HCI Command: Host Number of Completed Packets (0x03|0x0035) plen 5 [hci1] 12.133631 Num handles: 1 Handle: 12 Count: 1 > HCI Event: Number of Completed Packets (0x13) plen 5 [hci1] 12.133792 Num handles: 1 Handle: 12 Count: 2 < ACL Data TX: Handle 12 flags 0x00 dlen 16 [hci1] 12.141644 L2CAP: Connection Response (0x03) ident 3 len 8 Destination CID: 64 Source CID: 64 Result: Connection successful (0x0000) Status: No further information available (0x0000) < HCI Command: Host Number of Completed Packets (0x03|0x0035) plen 5 [hci1] 12.142054 Num handles: 1 Handle: 12 Count: 1 < ACL Data TX: Handle 12 flags 0x00 dlen 16 [hci1] 12.143276 L2CAP: Configure Request (0x04) ident 6 len 8 Destination CID: 64 Flags: 0x0000 Option: Maximum Transmission Unit (0x01) [mandatory] MTU: 48 > HCI Event: Number of Completed Packets (0x13) plen 5 [hci1] 12.150945 Num handles: 1 Handle: 12 Count: 2 > ACL Data RX: Handle 12 flags 0x02 dlen 16 [hci1] 12.150959 L2CAP: Configure Request (0x04) ident 4 len 8 Destination CID: 64 Flags: 0x0000 Option: Maximum Transmission Unit (0x01) [mandatory] MTU: 128 > ACL Data RX: Handle 12 flags 0x02 dlen 18 [hci1] 12.152160 L2CAP: Configure Response (0x05) ident 6 len 10 Source CID: 64 Flags: 0x0000 Result: Success (0x0000) Option: Maximum Transmission Unit (0x01) [mandatory] MTU: 48 < HCI Command: Host Number of Completed Packets (0x03|0x0035) plen 5 [hci1] 12.158806 Num handles: 1 Handle: 12 Count: 1 < ACL Data TX: Handle 12 flags 0x00 dlen 14 [hci1] 12.161822 L2CAP: Configure Response (0x05) ident 4 len 6 Source CID: 64 Flags: 0x0000 Result: Success (0x0000) < HCI Command: Host Number of Completed Packets (0x03|0x0035) plen 5 [hci1] 12.163197 Num handles: 1 Handle: 12 Count: 1 Change-Id: I7919fe180cf1d761023bb1478d8e1fb2d21a94d9 Signed-off-by: Arkadiusz Lichwa <arkadiusz.lichwa@tieto.com>
2016-06-21 14:02:30 +02:00
#define BT_L2CAP_CONF_UNACCEPT 0x0001
#define BT_L2CAP_CONF_REJECT 0x0002
#define BT_L2CAP_CONF_REQ 0x04
struct bt_l2cap_conf_req {
u16_t dcid;
u16_t flags;
u8_t data[0];
} __packed;
#define BT_L2CAP_CONF_RSP 0x05
struct bt_l2cap_conf_rsp {
u16_t scid;
u16_t flags;
u16_t result;
u8_t data[0];
} __packed;
/* Option type used by MTU config request data */
#define BT_L2CAP_CONF_OPT_MTU 0x01
Bluetooth: L2CAP: Start servicing configuration request from remote To make channel operational in basic mode a process of BR/EDR CoC channel configuration needs to be performed in both directions. Remote peer is mandated as well to inform local about its segmentation requirements. Here is added such request handler focused on grabbing remote MTU which is mandatory in channel setup. > ACL Data RX: Handle 12 flags 0x02 dlen 12 [hci1] 12.133539 L2CAP: Connection Request (0x02) ident 3 len 4 PSM: 1 (0x0001) Source CID: 64 < HCI Command: Host Number of Completed Packets (0x03|0x0035) plen 5 [hci1] 12.133631 Num handles: 1 Handle: 12 Count: 1 > HCI Event: Number of Completed Packets (0x13) plen 5 [hci1] 12.133792 Num handles: 1 Handle: 12 Count: 2 < ACL Data TX: Handle 12 flags 0x00 dlen 16 [hci1] 12.141644 L2CAP: Connection Response (0x03) ident 3 len 8 Destination CID: 64 Source CID: 64 Result: Connection successful (0x0000) Status: No further information available (0x0000) < HCI Command: Host Number of Completed Packets (0x03|0x0035) plen 5 [hci1] 12.142054 Num handles: 1 Handle: 12 Count: 1 < ACL Data TX: Handle 12 flags 0x00 dlen 16 [hci1] 12.143276 L2CAP: Configure Request (0x04) ident 6 len 8 Destination CID: 64 Flags: 0x0000 Option: Maximum Transmission Unit (0x01) [mandatory] MTU: 48 > HCI Event: Number of Completed Packets (0x13) plen 5 [hci1] 12.150945 Num handles: 1 Handle: 12 Count: 2 > ACL Data RX: Handle 12 flags 0x02 dlen 16 [hci1] 12.150959 L2CAP: Configure Request (0x04) ident 4 len 8 Destination CID: 64 Flags: 0x0000 Option: Maximum Transmission Unit (0x01) [mandatory] MTU: 128 > ACL Data RX: Handle 12 flags 0x02 dlen 18 [hci1] 12.152160 L2CAP: Configure Response (0x05) ident 6 len 10 Source CID: 64 Flags: 0x0000 Result: Success (0x0000) Option: Maximum Transmission Unit (0x01) [mandatory] MTU: 48 < HCI Command: Host Number of Completed Packets (0x03|0x0035) plen 5 [hci1] 12.158806 Num handles: 1 Handle: 12 Count: 1 < ACL Data TX: Handle 12 flags 0x00 dlen 14 [hci1] 12.161822 L2CAP: Configure Response (0x05) ident 4 len 6 Source CID: 64 Flags: 0x0000 Result: Success (0x0000) < HCI Command: Host Number of Completed Packets (0x03|0x0035) plen 5 [hci1] 12.163197 Num handles: 1 Handle: 12 Count: 1 Change-Id: I7919fe180cf1d761023bb1478d8e1fb2d21a94d9 Signed-off-by: Arkadiusz Lichwa <arkadiusz.lichwa@tieto.com>
2016-06-21 14:02:30 +02:00
/* Options bits selecting most significant bit (hint) in type field */
#define BT_L2CAP_CONF_HINT 0x80
#define BT_L2CAP_CONF_MASK 0x7f
struct bt_l2cap_conf_opt {
u8_t type;
u8_t len;
u8_t data[0];
} __packed;
#define BT_L2CAP_DISCONN_REQ 0x06
struct bt_l2cap_disconn_req {
u16_t dcid;
u16_t scid;
} __packed;
#define BT_L2CAP_DISCONN_RSP 0x07
struct bt_l2cap_disconn_rsp {
u16_t dcid;
u16_t scid;
} __packed;
#define BT_L2CAP_INFO_FEAT_MASK 0x0002
#define BT_L2CAP_INFO_FIXED_CHAN 0x0003
#define BT_L2CAP_INFO_REQ 0x0a
struct bt_l2cap_info_req {
u16_t type;
} __packed;
/* info result */
#define BT_L2CAP_INFO_SUCCESS 0x0000
#define BT_L2CAP_INFO_NOTSUPP 0x0001
#define BT_L2CAP_INFO_RSP 0x0b
struct bt_l2cap_info_rsp {
u16_t type;
u16_t result;
u8_t data[0];
} __packed;
#define BT_L2CAP_CONN_PARAM_REQ 0x12
struct bt_l2cap_conn_param_req {
u16_t min_interval;
u16_t max_interval;
u16_t latency;
u16_t timeout;
} __packed;
Bluetooth: Refactor LE Connection Params validator Move le_validate_conn_params() interface to internal HCI header to be commonly shared between HCI and L2CAP layers. Refactor the interface and its users and rename related to Connection Parameters Update procedure values used for validation. < ACL Data TX: Handle 42 flags 0x00 dlen 16 [hci1] 2832.387859 LE L2CAP: Connection Parameter Update Request (0x12) ident 1 len 8 Min interval: 40 Max interval: 56 Slave latency: 0 Timeout multiplier: 0 @ Device Connected: 00:AA:01:00:00:23 (1) flags 0x0000 < HCI Command: LE Read Remote Used Features (0x08|0x0016) plen 2 [hci0] 2832.389374 Handle: 42 > ACL Data RX: Handle 42 flags 0x02 dlen 16 [hci0] 2832.389437 LE L2CAP: Connection Parameter Update Request (0x12) ident 1 len 8 Min interval: 40 Max interval: 56 Slave latency: 0 Timeout multiplier: 0 > HCI Event: Number of Completed Packets (0x13) plen 5 [hci1] 2832.389440 Num handles: 1 Handle: 42 Count: 1 > HCI Event: Command Status (0x0f) plen 4 [hci0] 2832.389443 LE Read Remote Used Features (0x08|0x0016) ncmd 1 Status: Success (0x00) > HCI Event: LE Meta Event (0x3e) plen 12 [hci0] 2832.389444 LE Read Remote Used Features (0x04) Status: Success (0x00) Handle: 42 Features: 0x03 0x00 0x00 0x00 0x00 0x00 0x00 0x00 LE Encryption Connection Parameter Request Procedure < ACL Data TX: Handle 42 flags 0x00 dlen 10 [hci0] 2832.393154 LE L2CAP: Connection Parameter Update Response (0x13) ident 1 len 2 Result: Connection Parameters rejected (0x0001) > ACL Data RX: Handle 42 flags 0x02 dlen 10 [hci1] 2832.393181 LE L2CAP: Connection Parameter Update Response (0x13) ident 1 len 2 Result: Connection Parameters rejected (0x0001) Rejected due to Timeout multiplier: 0. Change-Id: Idaf4f452f560ff71d9637ec5d793154190e94b35 Signed-off-by: Arkadiusz Lichwa <arkadiusz.lichwa@tieto.com> Signed-off-by: Mariusz Skamra <mariusz.skamra@tieto.com>
2015-07-29 13:45:33 +03:00
#define BT_L2CAP_CONN_PARAM_ACCEPTED 0x0000
#define BT_L2CAP_CONN_PARAM_REJECTED 0x0001
#define BT_L2CAP_CONN_PARAM_RSP 0x13
struct bt_l2cap_conn_param_rsp {
u16_t result;
} __packed;
#define BT_L2CAP_LE_CONN_REQ 0x14
struct bt_l2cap_le_conn_req {
u16_t psm;
u16_t scid;
u16_t mtu;
u16_t mps;
u16_t credits;
} __packed;
/* valid results in conn response on LE */
#define BT_L2CAP_LE_SUCCESS 0x0000
#define BT_L2CAP_LE_ERR_PSM_NOT_SUPP 0x0002
#define BT_L2CAP_LE_ERR_NO_RESOURCES 0x0004
#define BT_L2CAP_LE_ERR_AUTHENTICATION 0x0005
#define BT_L2CAP_LE_ERR_AUTHORIZATION 0x0006
#define BT_L2CAP_LE_ERR_KEY_SIZE 0x0007
#define BT_L2CAP_LE_ERR_ENCRYPTION 0x0008
#define BT_L2CAP_LE_ERR_INVALID_SCID 0x0009
#define BT_L2CAP_LE_ERR_SCID_IN_USE 0x000A
#define BT_L2CAP_LE_ERR_UNACCEPT_PARAMS 0x000B
#define BT_L2CAP_LE_CONN_RSP 0x15
struct bt_l2cap_le_conn_rsp {
u16_t dcid;
u16_t mtu;
u16_t mps;
u16_t credits;
u16_t result;
};
#define BT_L2CAP_LE_CREDITS 0x16
struct bt_l2cap_le_credits {
u16_t cid;
u16_t credits;
} __packed;
#define BT_L2CAP_SDU_HDR_LEN 2
#if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
#define BT_L2CAP_RX_MTU CONFIG_BT_L2CAP_RX_MTU
#else
#define BT_L2CAP_RX_MTU (CONFIG_BT_RX_BUF_LEN - \
BT_HCI_ACL_HDR_SIZE - BT_L2CAP_HDR_SIZE)
#endif
struct bt_l2cap_fixed_chan {
u16_t cid;
int (*accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan);
bt_l2cap_chan_destroy_t destroy;
};
#define BT_L2CAP_CHANNEL_DEFINE(_name, _cid, _accept, _destroy) \
const Z_STRUCT_SECTION_ITERABLE(bt_l2cap_fixed_chan, _name) = { \
.cid = _cid, \
.accept = _accept, \
.destroy = _destroy, \
}
/* Need a name different than bt_l2cap_fixed_chan for a different section */
struct bt_l2cap_br_fixed_chan {
u16_t cid;
int (*accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan);
};
#define BT_L2CAP_BR_CHANNEL_DEFINE(_name, _cid, _accept) \
const Z_STRUCT_SECTION_ITERABLE(bt_l2cap_br_fixed_chan, _name) = { \
.cid = _cid, \
.accept = _accept, \
}
/* Notify L2CAP channels of a new connection */
void bt_l2cap_connected(struct bt_conn *conn);
/* Notify L2CAP channels of a disconnect event */
void bt_l2cap_disconnected(struct bt_conn *conn);
/* Add channel to the connection */
void bt_l2cap_chan_add(struct bt_conn *conn, struct bt_l2cap_chan *chan,
bt_l2cap_chan_destroy_t destroy);
/* Remove channel from the connection */
void bt_l2cap_chan_remove(struct bt_conn *conn, struct bt_l2cap_chan *chan);
/* Delete channel */
void bt_l2cap_chan_del(struct bt_l2cap_chan *chan);
const char *bt_l2cap_chan_state_str(bt_l2cap_chan_state_t state);
#if defined(CONFIG_BT_DEBUG_L2CAP)
void bt_l2cap_chan_set_state_debug(struct bt_l2cap_chan *chan,
bt_l2cap_chan_state_t state,
const char *func, int line);
#define bt_l2cap_chan_set_state(_chan, _state) \
bt_l2cap_chan_set_state_debug(_chan, _state, __func__, __LINE__)
#else
void bt_l2cap_chan_set_state(struct bt_l2cap_chan *chan,
bt_l2cap_chan_state_t state);
#endif /* CONFIG_BT_DEBUG_L2CAP */
/*
* Notify L2CAP channels of a change in encryption state passing additionally
* HCI status of performed security procedure.
*/
void bt_l2cap_encrypt_change(struct bt_conn *conn, u8_t hci_status);
/* Prepare an L2CAP PDU to be sent over a connection */
struct net_buf *bt_l2cap_create_pdu_timeout(struct net_buf_pool *pool,
size_t reserve,
k_timeout_t timeout);
#define bt_l2cap_create_pdu(_pool, _reserve) \
bt_l2cap_create_pdu_timeout(_pool, _reserve, K_FOREVER)
/* Prepare a L2CAP Response PDU to be sent over a connection */
struct net_buf *bt_l2cap_create_rsp(struct net_buf *buf, size_t reserve);
/* Send L2CAP PDU over a connection
*
* Buffer ownership is transferred to stack so either in case of success
* or error the buffer will be unref internally.
*
* Calling this from RX thread is assumed to never fail so the return can be
* ignored.
*/
int bt_l2cap_send_cb(struct bt_conn *conn, u16_t cid, struct net_buf *buf,
bt_conn_tx_cb_t cb, void *user_data);
static inline void bt_l2cap_send(struct bt_conn *conn, u16_t cid,
struct net_buf *buf)
{
bt_l2cap_send_cb(conn, cid, buf, NULL, NULL);
}
/* Receive a new L2CAP PDU from a connection */
void bt_l2cap_recv(struct bt_conn *conn, struct net_buf *buf);
/* Perform connection parameter update request */
int bt_l2cap_update_conn_param(struct bt_conn *conn,
const struct bt_le_conn_param *param);
/* Initialize L2CAP and supported channels */
void bt_l2cap_init(void);
/* Lookup channel by Transmission CID */
struct bt_l2cap_chan *bt_l2cap_le_lookup_tx_cid(struct bt_conn *conn,
u16_t cid);
/* Lookup channel by Receiver CID */
struct bt_l2cap_chan *bt_l2cap_le_lookup_rx_cid(struct bt_conn *conn,
u16_t cid);
/* Initialize BR/EDR L2CAP signal layer */
void bt_l2cap_br_init(void);
/* Register fixed channel */
void bt_l2cap_br_fixed_chan_register(struct bt_l2cap_fixed_chan *chan);
/* Notify BR/EDR L2CAP channels about established new ACL connection */
void bt_l2cap_br_connected(struct bt_conn *conn);
/* Lookup BR/EDR L2CAP channel by Receiver CID */
struct bt_l2cap_chan *bt_l2cap_br_lookup_rx_cid(struct bt_conn *conn,
u16_t cid);
/* Disconnects dynamic channel */
int bt_l2cap_br_chan_disconnect(struct bt_l2cap_chan *chan);
/* Make connection to peer psm server */
int bt_l2cap_br_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan,
u16_t psm);
/* Send packet data to connected peer */
int bt_l2cap_br_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf);
/*
* Handle security level changed on link passing HCI status of performed
* security procedure.
*/
void l2cap_br_encrypt_change(struct bt_conn *conn, u8_t hci_status);
/* Handle received data */
void bt_l2cap_br_recv(struct bt_conn *conn, struct net_buf *buf);