1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /* 39 * l2cap.h 40 * 41 * Logical Link Control and Adaption Protocol (L2CAP) 42 * 43 * Created by Matthias Ringwald on 5/16/09. 44 */ 45 46 #ifndef __L2CAP_H 47 #define __L2CAP_H 48 49 #include "hci.h" 50 #include "l2cap_signaling.h" 51 #include "utils.h" 52 53 #if defined __cplusplus 54 extern "C" { 55 #endif 56 57 #define L2CAP_SIG_ID_INVALID 0 58 59 #define L2CAP_HEADER_SIZE 4 60 61 // size of HCI ACL + L2CAP Header for regular data packets (8) 62 #define COMPLETE_L2CAP_HEADER (HCI_ACL_HEADER_SIZE + L2CAP_HEADER_SIZE) 63 64 // minimum signaling MTU 65 #define L2CAP_MINIMAL_MTU 48 66 #define L2CAP_DEFAULT_MTU 672 67 68 // Minimum/default MTU 69 #define L2CAP_LE_DEFAULT_MTU 23 70 71 // check L2CAP MTU 72 #if (L2CAP_MINIMAL_MTU + L2CAP_HEADER_SIZE) > HCI_ACL_PAYLOAD_SIZE 73 #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP MTU of 48 bytes" 74 #endif 75 76 // L2CAP Fixed Channel IDs 77 #define L2CAP_CID_SIGNALING 0x0001 78 #define L2CAP_CID_CONNECTIONLESS_CHANNEL 0x0002 79 #define L2CAP_CID_ATTRIBUTE_PROTOCOL 0x0004 80 #define L2CAP_CID_SIGNALING_LE 0x0005 81 #define L2CAP_CID_SECURITY_MANAGER_PROTOCOL 0x0006 82 83 // L2CAP Configuration Result Codes 84 #define L2CAP_CONF_RESULT_UNKNOWN_OPTIONS 0x0003 85 86 // L2CAP Reject Result Codes 87 #define L2CAP_REJ_CMD_UNKNOWN 0x0000 88 89 // Response Timeout eXpired 90 #define L2CAP_RTX_TIMEOUT_MS 10000 91 92 // Extended Response Timeout eXpired 93 #define L2CAP_ERTX_TIMEOUT_MS 120000 94 95 // private structs 96 typedef enum { 97 L2CAP_STATE_CLOSED = 1, // no baseband 98 L2CAP_STATE_WILL_SEND_CREATE_CONNECTION, 99 L2CAP_STATE_WAIT_CONNECTION_COMPLETE, 100 L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES, 101 L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE, 102 L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE, 103 L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT, 104 L2CAP_STATE_WAIT_CONNECT_RSP, // from peer 105 L2CAP_STATE_CONFIG, 106 L2CAP_STATE_OPEN, 107 L2CAP_STATE_WAIT_DISCONNECT, // from application 108 L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST, 109 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY, 110 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE, 111 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT, 112 L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST, 113 L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE, 114 L2CAP_STATE_INVALID, 115 } L2CAP_STATE; 116 117 typedef enum { 118 L2CAP_CHANNEL_STATE_VAR_NONE = 0, 119 L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ = 1 << 0, 120 L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP = 1 << 1, 121 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ = 1 << 2, 122 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP = 1 << 3, 123 L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ = 1 << 4, 124 L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP = 1 << 5, 125 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU = 1 << 6, // in CONF RSP, add MTU field 126 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT = 1 << 7, // in CONF RSP, set CONTINUE flag 127 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID = 1 << 8, // in CONF RSP, send UNKNOWN OPTIONS 128 L2CAP_CHANNEL_STATE_VAR_SEND_CMD_REJ_UNKNOWN = 1 << 9, // send CMD_REJ with reason unknown 129 L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND = 1 << 10, // send Connection Respond with pending 130 } L2CAP_CHANNEL_STATE_VAR; 131 132 // info regarding an actual connection 133 typedef struct { 134 // linked list - assert: first field 135 linked_item_t item; 136 137 L2CAP_STATE state; 138 L2CAP_CHANNEL_STATE_VAR state_var; 139 140 bd_addr_t address; 141 hci_con_handle_t handle; 142 143 uint8_t remote_sig_id; // used by other side, needed for delayed response 144 uint8_t local_sig_id; // own signaling identifier 145 146 uint16_t local_cid; 147 uint16_t remote_cid; 148 149 uint16_t local_mtu; 150 uint16_t remote_mtu; 151 152 uint16_t flush_timeout; // default 0xffff 153 154 uint16_t psm; 155 156 gap_security_level_t required_security_level; 157 158 uint8_t packets_granted; // number of L2CAP/ACL packets client is allowed to send 159 160 uint8_t reason; // used in decline internal 161 162 timer_source_t rtx; // also used for ertx 163 164 // internal connection 165 btstack_packet_handler_t packet_handler; 166 167 } l2cap_channel_t; 168 169 // info regarding potential connections 170 typedef struct { 171 // linked list - assert: first field 172 linked_item_t item; 173 174 // service id 175 uint16_t psm; 176 177 // incoming MTU 178 uint16_t mtu; 179 180 // incoming MPS 181 uint16_t mps; 182 183 // internal connection 184 btstack_packet_handler_t packet_handler; 185 186 // required security level 187 gap_security_level_t required_security_level; 188 } l2cap_service_t; 189 190 191 typedef struct l2cap_signaling_response { 192 hci_con_handle_t handle; 193 uint8_t sig_id; 194 uint8_t code; 195 uint16_t data; // infoType for INFORMATION REQUEST, result for CONNECTION request and command unknown 196 } l2cap_signaling_response_t; 197 198 199 void l2cap_block_new_credits(uint8_t blocked); 200 201 int l2cap_can_send_fixed_channel_packet_now(uint16_t handle); 202 203 // @deprecated use l2cap_can_send_fixed_channel_packet_now instead 204 int l2cap_can_send_connectionless_packet_now(void); 205 206 int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len); 207 208 void l2cap_require_security_level_2_for_outgoing_sdp(void); // for PTS testing only 209 210 /* API_START */ 211 212 /** 213 * @brief Set up L2CAP and register L2CAP with HCI layer. 214 */ 215 void l2cap_init(void); 216 217 /** 218 * @brief Registers a packet handler that handles HCI and general BTstack events. 219 */ 220 void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)); 221 222 /** 223 * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 224 * @param packet_handler 225 * @param address 226 * @param psm 227 * @param mtu 228 * @param local_cid 229 * @param status 230 */ 231 uint8_t l2cap_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid); 232 233 /** 234 * @brief Disconnects L2CAP channel with given identifier. 235 */ 236 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason); 237 238 /** 239 * @brief Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier. 240 */ 241 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid); 242 243 /** 244 * @brief Sends L2CAP data packet to the channel with given identifier. 245 */ 246 int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len); 247 248 /** 249 * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler. 250 */ 251 uint8_t l2cap_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level); 252 253 /** 254 * @brief Unregisters L2CAP service with given PSM. On embedded systems, use NULL for connection parameter. 255 */ 256 void l2cap_unregister_service(uint16_t psm); 257 258 /** 259 * @brief Accepts/Deny incoming L2CAP connection. 260 */ 261 void l2cap_accept_connection_internal(uint16_t local_cid); 262 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason); 263 264 /** 265 * @brief Non-blocking UART write 266 */ 267 int l2cap_can_send_packet_now(uint16_t local_cid); 268 int l2cap_reserve_packet_buffer(void); 269 void l2cap_release_packet_buffer(void); 270 271 /** 272 * @brief Get outgoing buffer and prepare data. 273 */ 274 uint8_t *l2cap_get_outgoing_buffer(void); 275 276 int l2cap_send_prepared(uint16_t local_cid, uint16_t len); 277 278 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len); 279 280 /** 281 * @brief Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol. 282 */ 283 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id); 284 285 uint16_t l2cap_max_mtu(void); 286 uint16_t l2cap_max_le_mtu(void); 287 288 int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len); 289 290 /* API_END */ 291 292 #if 0 293 294 // to be implemented soon 295 /** 296 * @brief Regster L2CAP LE Credit Based Flow Control Mode service 297 * @param 298 */ 299 void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, 300 uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level); 301 void l2cap_le_unregister_service(uint16_t psm); 302 #endif 303 304 #if defined __cplusplus 305 } 306 #endif 307 308 #endif // __L2CAP_H 309