1 /** @file 2 * @brief Bluetooth RFCOMM handling 3 */ 4 5 /* 6 * Copyright (c) 2015-2016 Intel Corporation 7 * 8 * SPDX-License-Identifier: Apache-2.0 9 */ 10 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_RFCOMM_H_ 11 #define ZEPHYR_INCLUDE_BLUETOOTH_RFCOMM_H_ 12 13 /** 14 * @brief RFCOMM 15 * @defgroup bt_rfcomm RFCOMM 16 * @ingroup bluetooth 17 * @{ 18 */ 19 20 #include <zephyr/bluetooth/buf.h> 21 #include <zephyr/bluetooth/conn.h> 22 #include <zephyr/bluetooth/l2cap.h> 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 /** RFCOMM Maximum Header Size. The length could be 2 bytes, it depends on information length. */ 29 #define BT_RFCOMM_HDR_MAX_SIZE 4 30 /** RFCOMM FCS Size */ 31 #define BT_RFCOMM_FCS_SIZE 1 32 33 /** @brief Helper to calculate needed buffer size for RFCOMM PDUs. 34 * Useful for creating buffer pools. 35 * 36 * @param mtu Needed RFCOMM PDU MTU. 37 * 38 * @return Needed buffer size to match the requested RFCOMM PDU MTU. 39 */ 40 #define BT_RFCOMM_BUF_SIZE(mtu) \ 41 BT_L2CAP_BUF_SIZE(BT_RFCOMM_HDR_MAX_SIZE + BT_RFCOMM_FCS_SIZE + (mtu)) 42 43 /* RFCOMM channels (1-30): pre-allocated for profiles to avoid conflicts */ 44 enum { 45 BT_RFCOMM_CHAN_HFP_HF = 1, 46 BT_RFCOMM_CHAN_HFP_AG, 47 BT_RFCOMM_CHAN_HSP_AG, 48 BT_RFCOMM_CHAN_HSP_HS, 49 BT_RFCOMM_CHAN_SPP, 50 BT_RFCOMM_CHAN_DYNAMIC_START, 51 }; 52 53 struct bt_rfcomm_dlc; 54 55 /** @brief RFCOMM DLC operations structure. */ 56 struct bt_rfcomm_dlc_ops { 57 /** DLC connected callback 58 * 59 * If this callback is provided it will be called whenever the 60 * connection completes. 61 * 62 * @param dlc The dlc that has been connected 63 */ 64 void (*connected)(struct bt_rfcomm_dlc *dlc); 65 66 /** DLC disconnected callback 67 * 68 * If this callback is provided it will be called whenever the 69 * dlc is disconnected, including when a connection gets 70 * rejected or cancelled (both incoming and outgoing) 71 * 72 * @param dlc The dlc that has been Disconnected 73 */ 74 void (*disconnected)(struct bt_rfcomm_dlc *dlc); 75 76 /** DLC recv callback 77 * 78 * @param dlc The dlc receiving data. 79 * @param buf Buffer containing incoming data. 80 */ 81 void (*recv)(struct bt_rfcomm_dlc *dlc, struct net_buf *buf); 82 83 /** DLC sent callback 84 * 85 * @param dlc The dlc which has sent data. 86 * @param err Sent result. 87 */ 88 void (*sent)(struct bt_rfcomm_dlc *dlc, int err); 89 }; 90 91 /** @brief Role of RFCOMM session and dlc. Used only by internal APIs 92 */ 93 typedef enum bt_rfcomm_role { 94 BT_RFCOMM_ROLE_ACCEPTOR, 95 BT_RFCOMM_ROLE_INITIATOR 96 } __packed bt_rfcomm_role_t; 97 98 /** @brief RFCOMM DLC structure. */ 99 struct bt_rfcomm_dlc { 100 /* Response Timeout eXpired (RTX) timer */ 101 struct k_work_delayable rtx_work; 102 103 /* Queue for outgoing data */ 104 struct k_fifo tx_queue; 105 106 /* TX credits, Reuse as a binary sem for MSC FC if CFC is not enabled */ 107 struct k_sem tx_credits; 108 109 struct bt_rfcomm_session *session; 110 struct bt_rfcomm_dlc_ops *ops; 111 struct bt_rfcomm_dlc *_next; 112 113 bt_security_t required_sec_level; 114 bt_rfcomm_role_t role; 115 116 uint16_t mtu; 117 uint8_t dlci; 118 uint8_t state; 119 uint8_t rx_credit; 120 121 /* Stack & kernel data for TX thread */ 122 struct k_thread tx_thread; 123 #if defined(CONFIG_BT_RFCOMM_DLC_STACK_SIZE) 124 K_KERNEL_STACK_MEMBER(stack, CONFIG_BT_RFCOMM_DLC_STACK_SIZE); 125 #endif /* CONFIG_BT_RFCOMM_DLC_STACK_SIZE */ 126 }; 127 128 struct bt_rfcomm_server { 129 /** Server Channel 130 * 131 * Possible values: 132 * 0 A dynamic value will be auto-allocated when bt_rfcomm_server_register() is 133 * called. 134 * 135 * 0x01 - 0x1e Dynamically allocated. May be pre-set by the application before server 136 * registration (not recommended however), or auto-allocated by the stack 137 * if the 0 is passed. 138 */ 139 uint8_t channel; 140 141 /** Server accept callback 142 * 143 * This callback is called whenever a new incoming connection requires 144 * authorization. 145 * 146 * @param conn The connection that is requesting authorization 147 * @param server Pointer to the server structure this callback relates to 148 * @param dlc Pointer to received the allocated dlc 149 * 150 * @return 0 in case of success or negative value in case of error. 151 */ 152 int (*accept)(struct bt_conn *conn, struct bt_rfcomm_server *server, 153 struct bt_rfcomm_dlc **dlc); 154 155 struct bt_rfcomm_server *_next; 156 }; 157 158 /** @brief Register RFCOMM server 159 * 160 * Register RFCOMM server for a channel, each new connection is authorized 161 * using the accept() callback which in case of success shall allocate the dlc 162 * structure to be used by the new connection. 163 * 164 * @param server Server structure. 165 * 166 * @return 0 in case of success or negative value in case of error. 167 */ 168 int bt_rfcomm_server_register(struct bt_rfcomm_server *server); 169 170 /** @brief Connect RFCOMM channel 171 * 172 * Connect RFCOMM dlc by channel, once the connection is completed dlc 173 * connected() callback will be called. If the connection is rejected 174 * disconnected() callback is called instead. 175 * 176 * @param conn Connection object. 177 * @param dlc Dlc object. 178 * @param channel Server channel to connect to. 179 * 180 * @return 0 in case of success or negative value in case of error. 181 */ 182 int bt_rfcomm_dlc_connect(struct bt_conn *conn, struct bt_rfcomm_dlc *dlc, 183 uint8_t channel); 184 185 /** @brief Send data to RFCOMM 186 * 187 * Send data from buffer to the dlc. Length should be less than or equal to 188 * mtu. 189 * 190 * @param dlc Dlc object. 191 * @param buf Data buffer. 192 * 193 * @return Bytes sent in case of success or negative value in case of error. 194 */ 195 int bt_rfcomm_dlc_send(struct bt_rfcomm_dlc *dlc, struct net_buf *buf); 196 197 /** @brief Disconnect RFCOMM dlc 198 * 199 * Disconnect RFCOMM dlc, if the connection is pending it will be 200 * canceled and as a result the dlc disconnected() callback is called. 201 * 202 * @param dlc Dlc object. 203 * 204 * @return 0 in case of success or negative value in case of error. 205 */ 206 int bt_rfcomm_dlc_disconnect(struct bt_rfcomm_dlc *dlc); 207 208 /** @brief Allocate the buffer from pool after reserving head room for RFCOMM, 209 * L2CAP and ACL headers. 210 * 211 * @param pool Which pool to take the buffer from. 212 * 213 * @return New buffer. 214 */ 215 struct net_buf *bt_rfcomm_create_pdu(struct net_buf_pool *pool); 216 217 #ifdef __cplusplus 218 } 219 #endif 220 221 /** 222 * @} 223 */ 224 225 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_RFCOMM_H_ */ 226