123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- #ifndef ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_
- #define ZEPHYR_INCLUDE_BLUETOOTH_L2CAP_H_
- #include <sys/atomic.h>
- #include <bluetooth/buf.h>
- #include <bluetooth/conn.h>
- #include <bluetooth/hci.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- #define BT_L2CAP_HDR_SIZE 4
- #define BT_L2CAP_TX_MTU (CONFIG_BT_L2CAP_TX_MTU)
- #define BT_L2CAP_RX_MTU (CONFIG_BT_BUF_ACL_RX_SIZE - BT_L2CAP_HDR_SIZE)
- #define BT_L2CAP_BUF_SIZE(mtu) BT_BUF_ACL_SIZE(BT_L2CAP_HDR_SIZE + (mtu))
- #define BT_L2CAP_SDU_HDR_SIZE 2
- #define BT_L2CAP_SDU_TX_MTU (BT_L2CAP_TX_MTU - BT_L2CAP_SDU_HDR_SIZE)
- #define BT_L2CAP_SDU_RX_MTU (BT_L2CAP_RX_MTU - BT_L2CAP_SDU_HDR_SIZE)
- #define BT_L2CAP_SDU_BUF_SIZE(mtu) BT_L2CAP_BUF_SIZE(BT_L2CAP_SDU_HDR_SIZE + (mtu))
- struct bt_l2cap_chan;
- typedef void (*bt_l2cap_chan_destroy_t)(struct bt_l2cap_chan *chan);
- typedef enum bt_l2cap_chan_state {
-
- BT_L2CAP_DISCONNECTED,
-
- BT_L2CAP_CONNECT,
-
- BT_L2CAP_CONFIG,
-
- BT_L2CAP_CONNECTED,
-
- BT_L2CAP_DISCONNECT,
- } __packed bt_l2cap_chan_state_t;
- typedef enum bt_l2cap_chan_status {
-
- BT_L2CAP_STATUS_OUT,
-
- BT_L2CAP_STATUS_SHUTDOWN,
-
- BT_L2CAP_STATUS_ENCRYPT_PENDING,
-
- BT_L2CAP_NUM_STATUS,
- } __packed bt_l2cap_chan_status_t;
- struct bt_l2cap_chan {
-
- struct bt_conn *conn;
-
- const struct bt_l2cap_chan_ops *ops;
- sys_snode_t node;
- bt_l2cap_chan_destroy_t destroy;
-
- struct k_work_delayable rtx_work;
- struct k_work_sync rtx_sync;
- ATOMIC_DEFINE(status, BT_L2CAP_NUM_STATUS);
- #if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
- bt_l2cap_chan_state_t state;
-
- uint16_t psm;
-
- uint8_t ident;
- bt_security_t required_sec_level;
- #endif
- };
- struct bt_l2cap_le_endpoint {
-
- uint16_t cid;
-
- uint16_t mtu;
-
- uint16_t mps;
-
- uint16_t init_credits;
-
- atomic_t credits;
- };
- struct bt_l2cap_le_chan {
-
- struct bt_l2cap_chan chan;
-
- struct bt_l2cap_le_endpoint rx;
-
- uint16_t pending_rx_mtu;
-
- struct bt_l2cap_le_endpoint tx;
-
- struct k_fifo tx_queue;
-
- struct net_buf *tx_buf;
-
- struct k_work tx_work;
-
- struct net_buf *_sdu;
- uint16_t _sdu_len;
- struct k_work rx_work;
- struct k_fifo rx_queue;
- };
- #define BT_L2CAP_LE_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_le_chan, chan)
- struct bt_l2cap_br_endpoint {
-
- uint16_t cid;
-
- uint16_t mtu;
- };
- struct bt_l2cap_br_chan {
-
- struct bt_l2cap_chan chan;
-
- struct bt_l2cap_br_endpoint rx;
-
- struct bt_l2cap_br_endpoint tx;
-
- atomic_t flags[1];
- };
- struct bt_l2cap_chan_ops {
-
- void (*connected)(struct bt_l2cap_chan *chan);
-
- void (*disconnected)(struct bt_l2cap_chan *chan);
-
- void (*encrypt_change)(struct bt_l2cap_chan *chan, uint8_t hci_status);
-
- struct net_buf *(*alloc_buf)(struct bt_l2cap_chan *chan);
-
- int (*recv)(struct bt_l2cap_chan *chan, struct net_buf *buf);
-
- void (*sent)(struct bt_l2cap_chan *chan);
-
- void (*status)(struct bt_l2cap_chan *chan, atomic_t *status);
-
- void (*released)(struct bt_l2cap_chan *chan);
-
- void (*reconfigured)(struct bt_l2cap_chan *chan);
- };
- #define BT_L2CAP_CHAN_SEND_RESERVE (BT_L2CAP_BUF_SIZE(0))
- #define BT_L2CAP_SDU_CHAN_SEND_RESERVE (BT_L2CAP_SDU_BUF_SIZE(0))
- struct bt_l2cap_server {
-
- uint16_t psm;
-
- bt_security_t sec_level;
-
- int (*accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan);
- sys_snode_t node;
- };
- int bt_l2cap_server_register(struct bt_l2cap_server *server);
- int bt_l2cap_br_server_register(struct bt_l2cap_server *server);
- int bt_l2cap_ecred_chan_connect(struct bt_conn *conn,
- struct bt_l2cap_chan **chans, uint16_t psm);
- int bt_l2cap_ecred_chan_reconfigure(struct bt_l2cap_chan **chans, uint16_t mtu);
- int bt_l2cap_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan,
- uint16_t psm);
- int bt_l2cap_chan_disconnect(struct bt_l2cap_chan *chan);
- int bt_l2cap_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf);
- int bt_l2cap_chan_recv_complete(struct bt_l2cap_chan *chan,
- struct net_buf *buf);
- #ifdef __cplusplus
- }
- #endif
- #endif
|