1 /* 2 * Copyright (C) 2009-2012 by Matthias Ringwald 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 MATTHIAS RINGWALD 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 [email protected] 34 * 35 */ 36 37 /* 38 * l2cap.h 39 * 40 * Logical Link Control and Adaption Protocl (L2CAP) 41 * 42 * Created by Matthias Ringwald on 5/16/09. 43 */ 44 45 #pragma once 46 47 #include "hci.h" 48 #include "l2cap_signaling.h" 49 #include <btstack/utils.h> 50 #include <btstack/btstack.h> 51 52 #if defined __cplusplus 53 extern "C" { 54 #endif 55 56 #define L2CAP_SIG_ID_INVALID 0 57 58 #define L2CAP_HEADER_SIZE 4 59 60 // size of HCI ACL + L2CAP Header for regular data packets (8) 61 #define COMPLETE_L2CAP_HEADER (HCI_ACL_HEADER_SIZE + L2CAP_HEADER_SIZE) 62 63 // minimum signaling MTU 64 #define L2CAP_MINIMAL_MTU 48 65 #define L2CAP_DEFAULT_MTU 672 66 67 // check L2CAP MTU 68 #if (L2CAP_MINIMAL_MTU + L2CAP_HEADER_SIZE) > HCI_ACL_PAYLOAD_SIZE 69 #error "HCI_ACL_PAYLOAD_SIZE too small for minimal L2CAP MTU of 48 bytes" 70 #endif 71 72 // L2CAP Fixed Channel IDs 73 #define L2CAP_CID_SIGNALING 0x0001 74 #define L2CAP_CID_CONNECTIONLESS_CHANNEL 0x0002 75 #define L2CAP_CID_ATTRIBUTE_PROTOCOL 0x0004 76 #define L2CAP_CID_SIGNALING_LE 0x0005 77 #define L2CAP_CID_SECURITY_MANAGER_PROTOCOL 0x0006 78 79 // L2CAP Configuration Result Codes 80 #define L2CAP_CONF_RESULT_UNKNOWN_OPTIONS 0x0003 81 82 // L2CAP Reject Result Codes 83 #define L2CAP_REJ_CMD_UNKNOWN 0x0000 84 85 // Response Timeout eXpired 86 #define L2CAP_RTX_TIMEOUT_MS 2000 87 88 // Extended Response Timeout eXpired 89 #define L2CAP_ERTX_TIMEOUT_MS 120000 90 91 // private structs 92 typedef enum { 93 L2CAP_STATE_CLOSED = 1, // no baseband 94 L2CAP_STATE_WILL_SEND_CREATE_CONNECTION, 95 L2CAP_STATE_WAIT_CONNECTION_COMPLETE, 96 L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES, 97 L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE, 98 L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE, 99 L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT, 100 L2CAP_STATE_WAIT_CONNECT_RSP, // from peer 101 L2CAP_STATE_CONFIG, 102 L2CAP_STATE_OPEN, 103 L2CAP_STATE_WAIT_DISCONNECT, // from application 104 L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST, 105 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY, 106 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE, 107 L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT, 108 L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST, 109 L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE, 110 } L2CAP_STATE; 111 112 typedef enum { 113 L2CAP_CHANNEL_STATE_VAR_NONE = 0, 114 L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ = 1 << 0, 115 L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP = 1 << 1, 116 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ = 1 << 2, 117 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP = 1 << 3, 118 L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ = 1 << 4, 119 L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP = 1 << 5, 120 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU = 1 << 6, // in CONF RSP, add MTU field 121 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT = 1 << 7, // in CONF RSP, set CONTINUE flag 122 L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID = 1 << 8, // in CONF RSP, send UNKNOWN OPTIONS 123 L2CAP_CHANNEL_STATE_VAR_SEND_CMD_REJ_UNKNOWN = 1 << 9, // send CMD_REJ with reason unknown 124 L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND = 1 << 10, // send Connection Respond with pending 125 } L2CAP_CHANNEL_STATE_VAR; 126 127 // info regarding an actual coneection 128 typedef struct { 129 // linked list - assert: first field 130 linked_item_t item; 131 132 L2CAP_STATE state; 133 L2CAP_CHANNEL_STATE_VAR state_var; 134 135 bd_addr_t address; 136 hci_con_handle_t handle; 137 138 uint8_t remote_sig_id; // used by other side, needed for delayed response 139 uint8_t local_sig_id; // own signaling identifier 140 141 uint16_t local_cid; 142 uint16_t remote_cid; 143 144 uint16_t local_mtu; 145 uint16_t remote_mtu; 146 147 uint16_t flush_timeout; // default 0xffff 148 149 uint16_t psm; 150 151 gap_security_level_t required_security_level; 152 153 uint8_t packets_granted; // number of L2CAP/ACL packets client is allowed to send 154 155 uint8_t reason; // used in decline internal 156 157 timer_source_t rtx; // also used for ertx 158 159 // client connection 160 void * connection; 161 162 // internal connection 163 btstack_packet_handler_t packet_handler; 164 165 } l2cap_channel_t; 166 167 // info regarding potential connections 168 typedef struct { 169 // linked list - assert: first field 170 linked_item_t item; 171 172 // service id 173 uint16_t psm; 174 175 // incoming MTU 176 uint16_t mtu; 177 178 // client connection 179 void *connection; 180 181 // internal connection 182 btstack_packet_handler_t packet_handler; 183 184 // required security level 185 gap_security_level_t required_security_level; 186 } l2cap_service_t; 187 188 189 typedef struct l2cap_signaling_response { 190 hci_con_handle_t handle; 191 uint8_t sig_id; 192 uint8_t code; 193 uint16_t data; // infoType for INFORMATION REQUEST, result for CONNECTION request and command unknown 194 } l2cap_signaling_response_t; 195 196 197 void l2cap_block_new_credits(uint8_t blocked); 198 int l2cap_can_send_packet_now(uint16_t local_cid); // non-blocking UART write 199 200 // get outgoing buffer and prepare data 201 uint8_t *l2cap_get_outgoing_buffer(void); 202 203 int l2cap_send_prepared(uint16_t local_cid, uint16_t len); 204 205 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len); 206 207 // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 208 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id); 209 210 uint16_t l2cap_max_mtu(void); 211 212 int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len); 213 214 void l2cap_close_connection(void *connection); 215 216 217 /** Embedded API **/ 218 219 // Set up L2CAP and register L2CAP with HCI layer. 220 void l2cap_init(void); 221 222 // Registers a packet handler that handles HCI and general BTstack events. 223 void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)); 224 225 // Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 226 void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu); 227 228 // Disconencts L2CAP channel with given identifier. 229 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason); 230 231 // Queries the maximal transfer unit (MTU) for L2CAP channel with given identifier. 232 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid); 233 234 // Sends L2CAP data packet to the channel with given identifier. 235 int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len); 236 237 // Registers L2CAP service with given PSM and MTU, and assigns a packet handler. On embedded systems, use NULL for connection parameter. 238 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); 239 240 // Unregisters L2CAP service with given PSM. On embedded systems, use NULL for connection parameter. 241 void l2cap_unregister_service_internal(void *connection, uint16_t psm); 242 243 // Accepts/Deny incoming L2CAP connection. 244 void l2cap_accept_connection_internal(uint16_t local_cid); 245 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason); 246 247 // Request LE connection parameter update 248 int l2cap_le_request_connection_parameter_update(uint16_t handle, uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency, uint16_t timeout_multiplier); 249 250 #if defined __cplusplus 251 } 252 #endif 253