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 #include "btstack.h" 53 54 #if defined __cplusplus 55 extern "C" { 56 #endif 57 58 #define L2CAP_SIG_ID_INVALID 0 59 60 #define L2CAP_HEADER_SIZE 4 61 62 // size of HCI ACL + L2CAP Header for regular data packets (8) 63 #define COMPLETE_L2CAP_HEADER (HCI_ACL_HEADER_SIZE + L2CAP_HEADER_SIZE) 64 65 // minimum signaling MTU 66 #define L2CAP_MINIMAL_MTU 48 67 #define L2CAP_DEFAULT_MTU 672 68 69 // Minimum/default MTU 70 #define L2CAP_LE_DEFAULT_MTU 23 71 72 // check L2CAP MTU 73 #if (L2CAP_MINIMAL_MTU + L2CAP_HEADER_SIZE) > HCI_ACL_PAYLOAD_SIZE 74 #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP MTU of 48 bytes" 75 #endif 76 77 // L2CAP Fixed Channel IDs 78 #define L2CAP_CID_SIGNALING 0x0001 79 #define L2CAP_CID_CONNECTIONLESS_CHANNEL 0x0002 80 #define L2CAP_CID_ATTRIBUTE_PROTOCOL 0x0004 81 #define L2CAP_CID_SIGNALING_LE 0x0005 82 #define L2CAP_CID_SECURITY_MANAGER_PROTOCOL 0x0006 83 84 // L2CAP Configuration Result Codes 85 #define L2CAP_CONF_RESULT_UNKNOWN_OPTIONS 0x0003 86 87 // L2CAP Reject Result Codes 88 #define L2CAP_REJ_CMD_UNKNOWN 0x0000 89 90 // Response Timeout eXpired 91 #define L2CAP_RTX_TIMEOUT_MS 10000 92 93 // Extended Response Timeout eXpired 94 #define L2CAP_ERTX_TIMEOUT_MS 120000 95 96 // private structs 97 typedef enum { 98 L2CAP_STATE_CLOSED = 1, // no baseband 99 L2CAP_STATE_WILL_SEND_CREATE_CONNECTION, 100 L2CAP_STATE_WAIT_CONNECTION_COMPLETE, 101 L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES, 102 L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE, 103 L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE, 104 L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT, 105 L2CAP_STATE_WAIT_CONNECT_RSP, // from peer 106 L2CAP_STATE_CONFIG, 107 L2CAP_STATE_OPEN, 108 L2CAP_STATE_WAIT_DISCONNECT, // from application 109 L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST, 110 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY, 111 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE, 112 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT, 113 L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST, 114 L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE, 115 L2CAP_STATE_INVALID, 116 } L2CAP_STATE; 117 118 typedef enum { 119 L2CAP_CHANNEL_STATE_VAR_NONE = 0, 120 L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ = 1 << 0, 121 L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP = 1 << 1, 122 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ = 1 << 2, 123 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP = 1 << 3, 124 L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ = 1 << 4, 125 L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP = 1 << 5, 126 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU = 1 << 6, // in CONF RSP, add MTU field 127 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT = 1 << 7, // in CONF RSP, set CONTINUE flag 128 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID = 1 << 8, // in CONF RSP, send UNKNOWN OPTIONS 129 L2CAP_CHANNEL_STATE_VAR_SEND_CMD_REJ_UNKNOWN = 1 << 9, // send CMD_REJ with reason unknown 130 L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND = 1 << 10, // send Connection Respond with pending 131 } L2CAP_CHANNEL_STATE_VAR; 132 133 // info regarding an actual connection 134 typedef struct { 135 // linked list - assert: first field 136 linked_item_t item; 137 138 L2CAP_STATE state; 139 L2CAP_CHANNEL_STATE_VAR state_var; 140 141 bd_addr_t address; 142 hci_con_handle_t handle; 143 144 uint8_t remote_sig_id; // used by other side, needed for delayed response 145 uint8_t local_sig_id; // own signaling identifier 146 147 uint16_t local_cid; 148 uint16_t remote_cid; 149 150 uint16_t local_mtu; 151 uint16_t remote_mtu; 152 153 uint16_t flush_timeout; // default 0xffff 154 155 uint16_t psm; 156 157 gap_security_level_t required_security_level; 158 159 uint8_t packets_granted; // number of L2CAP/ACL packets client is allowed to send 160 161 uint8_t reason; // used in decline internal 162 163 timer_source_t rtx; // also used for ertx 164 165 // client connection 166 void * connection; 167 168 // internal connection 169 btstack_packet_handler_t packet_handler; 170 171 } l2cap_channel_t; 172 173 // info regarding potential connections 174 typedef struct { 175 // linked list - assert: first field 176 linked_item_t item; 177 178 // service id 179 uint16_t psm; 180 181 // incoming MTU 182 uint16_t mtu; 183 184 // incoming MPS 185 uint16_t mps; 186 187 // client connection 188 void *connection; 189 190 // internal connection 191 btstack_packet_handler_t packet_handler; 192 193 // required security level 194 gap_security_level_t required_security_level; 195 } l2cap_service_t; 196 197 198 typedef struct l2cap_signaling_response { 199 hci_con_handle_t handle; 200 uint8_t sig_id; 201 uint8_t code; 202 uint16_t data; // infoType for INFORMATION REQUEST, result for CONNECTION request and command unknown 203 } l2cap_signaling_response_t; 204 205 206 void l2cap_block_new_credits(uint8_t blocked); 207 208 int l2cap_can_send_fixed_channel_packet_now(uint16_t handle); 209 210 // @deprecated use l2cap_can_send_fixed_channel_packet_now instead 211 int l2cap_can_send_connectionless_packet_now(void); 212 213 int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len); 214 215 void l2cap_require_security_level_2_for_outgoing_sdp(void); // for PTS testing only 216 217 /* API_START */ 218 219 /** 220 * @brief Set up L2CAP and register L2CAP with HCI layer. 221 */ 222 void l2cap_init(void); 223 224 /** 225 * @brief Registers a packet handler that handles HCI and general BTstack events. 226 */ 227 void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)); 228 229 /** 230 * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 231 */ 232 void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu); 233 234 /** 235 * @brief Disconnects L2CAP channel with given identifier. 236 */ 237 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason); 238 239 /** 240 * @brief Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier. 241 */ 242 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid); 243 244 /** 245 * @brief Sends L2CAP data packet to the channel with given identifier. 246 */ 247 int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len); 248 249 /** 250 * @brief Registers L2CAP service with given PSM and MTU, and assigns a packet handler. On embedded systems, use NULL for connection parameter. 251 */ 252 void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level); 253 254 /** 255 * @brief Unregisters L2CAP service with given PSM. On embedded systems, use NULL for connection parameter. 256 */ 257 void l2cap_unregister_service_internal(void *connection, uint16_t psm); 258 259 /** 260 * @brief Accepts/Deny incoming L2CAP connection. 261 */ 262 void l2cap_accept_connection_internal(uint16_t local_cid); 263 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason); 264 265 /** 266 * @brief Non-blocking UART write 267 */ 268 int l2cap_can_send_packet_now(uint16_t local_cid); 269 int l2cap_reserve_packet_buffer(void); 270 void l2cap_release_packet_buffer(void); 271 272 /** 273 * @brief Get outgoing buffer and prepare data. 274 */ 275 uint8_t *l2cap_get_outgoing_buffer(void); 276 277 int l2cap_send_prepared(uint16_t local_cid, uint16_t len); 278 279 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len); 280 281 /** 282 * @brief Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol. 283 */ 284 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id); 285 286 uint16_t l2cap_max_mtu(void); 287 uint16_t l2cap_max_le_mtu(void); 288 289 int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len); 290 291 /* API_END */ 292 293 #if 0 294 295 // to be implemented soon 296 /** 297 * @brief Regster L2CAP LE Credit Based Flow Control Mode service 298 * @param 299 */ 300 void l2cap_le_register_service_internal(void * connection, btstack_packet_handler_t packet_handler, uint16_t psm, 301 uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level); 302 void l2cap_le_unregister_service_internal(void * connection, uint16_t psm); 303 #endif 304 305 #if defined __cplusplus 306 } 307 #endif 308 309 #endif // __L2CAP_H 310