2009-10-29 20:25:42 +00:00
|
|
|
|
/*
|
2015-02-06 16:19:27 +00:00
|
|
|
|
* Copyright (C) 2014 BlueKitchen GmbH
|
2009-10-29 20:25:42 +00:00
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
|
* from this software without specific prior written permission.
|
2012-05-07 21:54:09 +00:00
|
|
|
|
* 4. Any redistribution, use, or modification is done solely for
|
|
|
|
|
* personal benefit and not for any commercial purpose or for
|
|
|
|
|
* monetary gain.
|
2009-10-29 20:25:42 +00:00
|
|
|
|
*
|
2015-02-06 16:19:27 +00:00
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
2009-10-29 20:25:42 +00:00
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
|
|
|
|
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*
|
2015-02-06 16:19:27 +00:00
|
|
|
|
* Please inquire about commercial licensing options at
|
|
|
|
|
* contact@bluekitchen-gmbh.com
|
2012-05-07 21:54:09 +00:00
|
|
|
|
*
|
2009-10-29 20:25:42 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2019-05-11 19:31:55 +02:00
|
|
|
|
#define BTSTACK_FILE__ "l2cap.c"
|
2017-03-24 23:39:20 +01:00
|
|
|
|
|
2009-05-16 21:29:51 +00:00
|
|
|
|
/*
|
|
|
|
|
* l2cap.c
|
|
|
|
|
*
|
|
|
|
|
* Logical Link Control and Adaption Protocl (L2CAP)
|
|
|
|
|
*
|
|
|
|
|
* Created by Matthias Ringwald on 5/16/09.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "l2cap.h"
|
2010-01-25 22:22:55 +00:00
|
|
|
|
#include "hci.h"
|
2010-02-13 15:13:07 +00:00
|
|
|
|
#include "hci_dump.h"
|
2017-03-26 22:19:18 +02:00
|
|
|
|
#include "bluetooth_sdp.h"
|
2016-01-20 14:52:45 +01:00
|
|
|
|
#include "btstack_debug.h"
|
2016-02-19 14:52:36 +01:00
|
|
|
|
#include "btstack_event.h"
|
2011-07-24 09:22:31 +00:00
|
|
|
|
#include "btstack_memory.h"
|
2009-05-16 21:29:51 +00:00
|
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2010-08-16 17:04:39 +00:00
|
|
|
|
// nr of buffered acl packets in outgoing queue to get max performance
|
|
|
|
|
#define NR_BUFFERED_ACL_PACKETS 3
|
|
|
|
|
|
2011-07-08 15:51:20 +00:00
|
|
|
|
// used to cache l2cap rejects, echo, and informational requests
|
2011-08-20 10:44:21 +00:00
|
|
|
|
#define NR_PENDING_SIGNALING_RESPONSES 3
|
2011-07-08 15:51:20 +00:00
|
|
|
|
|
2016-10-05 22:09:32 +02:00
|
|
|
|
// nr of credits provided to remote if credits fall below watermark
|
|
|
|
|
#define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK 5
|
|
|
|
|
#define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT 5
|
|
|
|
|
|
2010-06-08 18:45:45 +00:00
|
|
|
|
// offsets for L2CAP SIGNALING COMMANDS
|
|
|
|
|
#define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0
|
|
|
|
|
#define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1
|
|
|
|
|
#define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
|
|
|
|
|
#define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4
|
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#if defined(ENABLE_LE_DATA_CHANNELS) || defined(ENABLE_CLASSIC)
|
|
|
|
|
#define L2CAP_USES_CHANNELS
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-02-05 17:37:51 +01:00
|
|
|
|
// prototypes
|
2017-07-17 15:06:38 +02:00
|
|
|
|
static void l2cap_run(void);
|
2016-10-19 14:27:25 +02:00
|
|
|
|
static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
|
|
|
|
static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size );
|
|
|
|
|
static void l2cap_notify_channel_can_send(void);
|
|
|
|
|
static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
|
2019-09-02 11:39:26 +02:00
|
|
|
|
static uint16_t l2cap_next_local_cid(void);
|
|
|
|
|
static uint8_t l2cap_next_sig_id(void);
|
2018-02-02 20:14:47 +01:00
|
|
|
|
static l2cap_fixed_channel_t * l2cap_fixed_channel_for_channel_id(uint16_t local_cid);
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2017-07-24 17:31:05 +02:00
|
|
|
|
static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel);
|
|
|
|
|
static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel);
|
2016-02-05 17:37:51 +01:00
|
|
|
|
static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
|
|
|
|
|
static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
|
|
|
|
|
static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
|
|
|
|
|
static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
|
2016-10-18 11:49:04 +02:00
|
|
|
|
static void l2cap_emit_incoming_connection(l2cap_channel_t *channel);
|
2016-02-05 17:37:51 +01:00
|
|
|
|
static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2016-10-14 16:00:59 +02:00
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
2016-10-18 12:27:22 +02:00
|
|
|
|
static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status);
|
2018-01-30 11:30:12 +01:00
|
|
|
|
static void l2cap_emit_le_channel_closed(l2cap_channel_t * channel);
|
2016-10-18 12:27:22 +02:00
|
|
|
|
static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel);
|
2016-10-18 11:49:04 +02:00
|
|
|
|
static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel);
|
2016-10-05 11:58:27 +02:00
|
|
|
|
static void l2cap_le_finialize_channel_close(l2cap_channel_t *channel);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm);
|
|
|
|
|
#endif
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
|
|
|
|
static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size);
|
|
|
|
|
static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid);
|
2018-02-02 15:18:59 +01:00
|
|
|
|
static l2cap_channel_t * l2cap_create_channel_entry(btstack_packet_handler_t packet_handler, l2cap_channel_type_t channel_type, bd_addr_t address, bd_addr_type_t address_type,
|
2017-07-24 17:31:05 +02:00
|
|
|
|
uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
static void l2cap_free_channel_entry(l2cap_channel_t * channel);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#endif
|
2017-07-17 15:51:39 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
2017-07-24 16:48:44 +02:00
|
|
|
|
static void l2cap_ertm_notify_channel_can_send(l2cap_channel_t * channel);
|
2017-07-24 17:08:23 +02:00
|
|
|
|
static void l2cap_ertm_monitor_timeout_callback(btstack_timer_source_t * ts);
|
2017-07-24 17:12:27 +02:00
|
|
|
|
static void l2cap_ertm_retransmission_timeout_callback(btstack_timer_source_t * ts);
|
2017-07-17 15:51:39 +02:00
|
|
|
|
#endif
|
2016-02-05 17:37:51 +01:00
|
|
|
|
|
2018-02-02 17:52:13 +01:00
|
|
|
|
// l2cap_fixed_channel_t entries
|
2018-02-05 15:57:21 +01:00
|
|
|
|
#ifdef ENABLE_BLE
|
2018-02-02 17:52:13 +01:00
|
|
|
|
static l2cap_fixed_channel_t l2cap_fixed_channel_att;
|
|
|
|
|
static l2cap_fixed_channel_t l2cap_fixed_channel_sm;
|
2018-02-05 15:57:21 +01:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2018-02-02 17:52:13 +01:00
|
|
|
|
static l2cap_fixed_channel_t l2cap_fixed_channel_connectionless;
|
2018-02-05 15:57:21 +01:00
|
|
|
|
#endif
|
2016-02-05 19:59:31 +01:00
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2016-02-05 19:59:31 +01:00
|
|
|
|
static btstack_linked_list_t l2cap_services;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
static uint8_t require_security_level2_for_outgoing_sdp;
|
2018-09-03 19:20:27 +02:00
|
|
|
|
static bd_addr_t l2cap_outgoing_classic_addr;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2016-10-18 12:27:22 +02:00
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
2016-02-05 19:59:31 +01:00
|
|
|
|
static btstack_linked_list_t l2cap_le_services;
|
2016-10-18 12:27:22 +02:00
|
|
|
|
#endif
|
2011-07-08 15:51:20 +00:00
|
|
|
|
|
2018-02-02 17:52:13 +01:00
|
|
|
|
// single list of channels for Classic Channels, LE Data Channels, Classic Connectionless, ATT, and SM
|
2018-02-02 16:27:59 +01:00
|
|
|
|
static btstack_linked_list_t l2cap_channels;
|
2019-09-02 11:39:26 +02:00
|
|
|
|
// next channel id for new connections
|
|
|
|
|
static uint16_t local_source_cid = 0x40;
|
|
|
|
|
// next signaling sequence number
|
|
|
|
|
static uint8_t sig_seq_nr = 0xff;
|
2018-02-02 16:27:59 +01:00
|
|
|
|
|
2011-07-08 15:51:20 +00:00
|
|
|
|
// used to cache l2cap rejects, echo, and informational requests
|
2011-06-21 17:51:29 +00:00
|
|
|
|
static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
|
|
|
|
|
static int signaling_responses_pending;
|
2016-02-03 14:09:26 +01:00
|
|
|
|
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
|
|
|
|
|
2017-05-18 21:59:28 +02:00
|
|
|
|
#ifdef ENABLE_BLE
|
|
|
|
|
// only used for connection parameter update events
|
2016-02-05 17:37:51 +01:00
|
|
|
|
static btstack_packet_handler_t l2cap_event_packet_handler;
|
2018-01-30 19:01:33 +01:00
|
|
|
|
static uint16_t l2cap_le_custom_max_mtu;
|
2017-05-18 21:59:28 +02:00
|
|
|
|
#endif
|
2011-07-08 15:51:20 +00:00
|
|
|
|
|
2017-07-12 17:24:42 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
|
2018-10-23 15:49:16 +02:00
|
|
|
|
// enable for testing
|
|
|
|
|
// #define L2CAP_ERTM_SIMULATE_FCS_ERROR_INTERVAL 16
|
|
|
|
|
|
2017-07-12 17:24:42 +02:00
|
|
|
|
/*
|
|
|
|
|
* CRC lookup table for generator polynom D^16 + D^15 + D^2 + 1
|
|
|
|
|
*/
|
|
|
|
|
static const uint16_t crc16_table[256] = {
|
|
|
|
|
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
|
|
|
|
|
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40, 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
|
|
|
|
|
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40, 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
|
|
|
|
|
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641, 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
|
|
|
|
|
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240, 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
|
|
|
|
|
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41, 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
|
|
|
|
|
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41, 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
|
|
|
|
|
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640, 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
|
|
|
|
|
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240, 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
|
|
|
|
|
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41, 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
|
|
|
|
|
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41, 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
|
|
|
|
|
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640, 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
|
|
|
|
|
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241, 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
|
|
|
|
|
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40, 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
|
|
|
|
|
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40, 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
|
|
|
|
|
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
static uint16_t crc16_calc(uint8_t * data, uint16_t len){
|
|
|
|
|
uint16_t crc = 0; // initial value = 0
|
|
|
|
|
while (len--){
|
|
|
|
|
crc = (crc >> 8) ^ crc16_table[ (crc ^ ((uint16_t) *data++)) & 0x00FF ];
|
|
|
|
|
}
|
|
|
|
|
return crc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint16_t l2cap_encanced_control_field_for_information_frame(uint8_t tx_seq, int final, uint8_t req_seq, l2cap_segmentation_and_reassembly_t sar){
|
|
|
|
|
return (((uint16_t) sar) << 14) | (req_seq << 8) | (final << 7) | (tx_seq << 1) | 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint16_t l2cap_encanced_control_field_for_supevisor_frame(l2cap_supervisory_function_t supervisory_function, int poll, int final, uint8_t req_seq){
|
|
|
|
|
return (req_seq << 8) | (final << 7) | (poll << 4) | (((int) supervisory_function) << 2) | 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int l2cap_next_ertm_seq_nr(int seq_nr){
|
|
|
|
|
return (seq_nr + 1) & 0x3f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int l2cap_ertm_can_store_packet_now(l2cap_channel_t * channel){
|
2018-10-12 22:06:01 +02:00
|
|
|
|
// get num free tx buffers
|
|
|
|
|
int num_free_tx_buffers = channel->num_tx_buffers - channel->num_stored_tx_frames;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// calculate num tx buffers for remote MTU
|
|
|
|
|
int num_tx_buffers_for_max_remote_mtu;
|
2019-08-07 22:43:27 +02:00
|
|
|
|
uint16_t effective_mps = btstack_min(channel->remote_mps, channel->local_mps);
|
|
|
|
|
if (channel->remote_mtu <= effective_mps){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// MTU fits into single packet
|
|
|
|
|
num_tx_buffers_for_max_remote_mtu = 1;
|
|
|
|
|
} else {
|
|
|
|
|
// include SDU Length
|
2019-08-07 22:43:27 +02:00
|
|
|
|
num_tx_buffers_for_max_remote_mtu = (channel->remote_mtu + 2 + (effective_mps - 1)) / effective_mps;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
}
|
2018-10-12 22:06:01 +02:00
|
|
|
|
log_debug("num_free_tx_buffers %u, num_tx_buffers_for_max_remote_mtu %u", num_free_tx_buffers, num_tx_buffers_for_max_remote_mtu);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
return num_tx_buffers_for_max_remote_mtu <= num_free_tx_buffers;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 16:30:27 +02:00
|
|
|
|
static void l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel_t * l2cap_channel){
|
|
|
|
|
log_info("Retransmit unacknowleged frames");
|
|
|
|
|
l2cap_channel->unacked_frames = 0;;
|
|
|
|
|
l2cap_channel->tx_send_index = l2cap_channel->tx_read_index;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
static void l2cap_ertm_next_tx_write_index(l2cap_channel_t * channel){
|
|
|
|
|
channel->tx_write_index++;
|
|
|
|
|
if (channel->tx_write_index < channel->num_tx_buffers) return;
|
|
|
|
|
channel->tx_write_index = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_ertm_start_monitor_timer(l2cap_channel_t * channel){
|
2017-07-24 18:10:22 +02:00
|
|
|
|
log_info("Start Monitor timer");
|
2017-07-24 17:31:05 +02:00
|
|
|
|
btstack_run_loop_remove_timer(&channel->monitor_timer);
|
|
|
|
|
btstack_run_loop_set_timer_handler(&channel->monitor_timer, &l2cap_ertm_monitor_timeout_callback);
|
|
|
|
|
btstack_run_loop_set_timer_context(&channel->monitor_timer, channel);
|
|
|
|
|
btstack_run_loop_set_timer(&channel->monitor_timer, channel->local_monitor_timeout_ms);
|
|
|
|
|
btstack_run_loop_add_timer(&channel->monitor_timer);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 18:10:22 +02:00
|
|
|
|
static void l2cap_ertm_stop_monitor_timer(l2cap_channel_t * channel){
|
|
|
|
|
log_info("Stop Monitor timer");
|
|
|
|
|
btstack_run_loop_remove_timer(&channel->monitor_timer);
|
|
|
|
|
}
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
static void l2cap_ertm_start_retransmission_timer(l2cap_channel_t * channel){
|
2017-07-24 18:10:22 +02:00
|
|
|
|
log_info("Start Retransmission timer");
|
2017-07-24 17:31:05 +02:00
|
|
|
|
btstack_run_loop_remove_timer(&channel->retransmission_timer);
|
|
|
|
|
btstack_run_loop_set_timer_handler(&channel->retransmission_timer, &l2cap_ertm_retransmission_timeout_callback);
|
|
|
|
|
btstack_run_loop_set_timer_context(&channel->retransmission_timer, channel);
|
|
|
|
|
btstack_run_loop_set_timer(&channel->retransmission_timer, channel->local_retransmission_timeout_ms);
|
|
|
|
|
btstack_run_loop_add_timer(&channel->retransmission_timer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_ertm_stop_retransmission_timer(l2cap_channel_t * l2cap_channel){
|
2017-07-24 18:10:22 +02:00
|
|
|
|
log_info("Stop Retransmission timer");
|
2017-07-24 17:31:05 +02:00
|
|
|
|
btstack_run_loop_remove_timer(&l2cap_channel->retransmission_timer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_ertm_monitor_timeout_callback(btstack_timer_source_t * ts){
|
2017-07-24 18:10:22 +02:00
|
|
|
|
log_info("Monitor timeout");
|
2017-07-24 17:31:05 +02:00
|
|
|
|
l2cap_channel_t * l2cap_channel = (l2cap_channel_t *) btstack_run_loop_get_timer_context(ts);
|
|
|
|
|
|
|
|
|
|
// TODO: we assume that it's the oldest packet
|
|
|
|
|
l2cap_ertm_tx_packet_state_t * tx_state;
|
|
|
|
|
tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index];
|
|
|
|
|
|
|
|
|
|
// check retry count
|
|
|
|
|
if (tx_state->retry_count < l2cap_channel->remote_max_transmit){
|
|
|
|
|
// increment retry count
|
|
|
|
|
tx_state->retry_count++;
|
|
|
|
|
|
2018-10-23 16:30:27 +02:00
|
|
|
|
// start retransmit
|
|
|
|
|
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
|
|
|
|
|
|
|
|
|
|
// start monitor timer
|
2017-07-24 17:31:05 +02:00
|
|
|
|
l2cap_ertm_start_monitor_timer(l2cap_channel);
|
|
|
|
|
|
|
|
|
|
// send RR/P=1
|
|
|
|
|
l2cap_channel->send_supervisor_frame_receiver_ready_poll = 1;
|
|
|
|
|
} else {
|
|
|
|
|
log_info("Monitor timer expired & retry count >= max transmit -> disconnect");
|
|
|
|
|
l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
|
|
|
|
|
}
|
|
|
|
|
l2cap_run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_ertm_retransmission_timeout_callback(btstack_timer_source_t * ts){
|
2017-07-24 18:10:22 +02:00
|
|
|
|
log_info("Retransmission timeout");
|
2017-07-24 17:31:05 +02:00
|
|
|
|
l2cap_channel_t * l2cap_channel = (l2cap_channel_t *) btstack_run_loop_get_timer_context(ts);
|
|
|
|
|
|
|
|
|
|
// TODO: we assume that it's the oldest packet
|
|
|
|
|
l2cap_ertm_tx_packet_state_t * tx_state;
|
|
|
|
|
tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index];
|
|
|
|
|
|
|
|
|
|
// set retry count = 1
|
|
|
|
|
tx_state->retry_count = 1;
|
|
|
|
|
|
2018-10-23 16:30:27 +02:00
|
|
|
|
// start retransmit
|
|
|
|
|
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
|
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// start monitor timer
|
|
|
|
|
l2cap_ertm_start_monitor_timer(l2cap_channel);
|
|
|
|
|
|
|
|
|
|
// send RR/P=1
|
|
|
|
|
l2cap_channel->send_supervisor_frame_receiver_ready_poll = 1;
|
|
|
|
|
l2cap_run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int l2cap_ertm_send_information_frame(l2cap_channel_t * channel, int index, int final){
|
|
|
|
|
l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[index];
|
|
|
|
|
hci_reserve_packet_buffer();
|
|
|
|
|
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
|
|
|
|
|
uint16_t control = l2cap_encanced_control_field_for_information_frame(tx_state->tx_seq, final, channel->req_seq, tx_state->sar);
|
|
|
|
|
log_info("I-Frame: control 0x%04x", control);
|
|
|
|
|
little_endian_store_16(acl_buffer, 8, control);
|
2018-10-04 14:15:17 +02:00
|
|
|
|
memcpy(&acl_buffer[8+2], &channel->tx_packets_data[index * channel->local_mps], tx_state->len);
|
2017-07-24 17:51:03 +02:00
|
|
|
|
// (re-)start retransmission timer on
|
|
|
|
|
l2cap_ertm_start_retransmission_timer(channel);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// send
|
|
|
|
|
return l2cap_send_prepared(channel->local_cid, 2 + tx_state->len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_ertm_store_fragment(l2cap_channel_t * channel, l2cap_segmentation_and_reassembly_t sar, uint16_t sdu_length, uint8_t * data, uint16_t len){
|
|
|
|
|
// get next index for storing packets
|
|
|
|
|
int index = channel->tx_write_index;
|
|
|
|
|
|
|
|
|
|
l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[index];
|
|
|
|
|
tx_state->tx_seq = channel->next_tx_seq;
|
|
|
|
|
tx_state->sar = sar;
|
|
|
|
|
tx_state->retry_count = 0;
|
|
|
|
|
|
2018-10-04 14:15:17 +02:00
|
|
|
|
uint8_t * tx_packet = &channel->tx_packets_data[index * channel->local_mps];
|
2019-08-07 22:43:27 +02:00
|
|
|
|
log_debug("index %u, local mps %u, remote mps %u, packet tx %p, len %u", index, channel->local_mps, channel->remote_mps, tx_packet, len);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
int pos = 0;
|
|
|
|
|
if (sar == L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU){
|
|
|
|
|
little_endian_store_16(tx_packet, 0, sdu_length);
|
|
|
|
|
pos += 2;
|
|
|
|
|
}
|
|
|
|
|
memcpy(&tx_packet[pos], data, len);
|
2019-08-07 22:43:27 +02:00
|
|
|
|
tx_state->len = pos + len;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
// update
|
2018-10-12 22:06:01 +02:00
|
|
|
|
channel->num_stored_tx_frames++;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
channel->next_tx_seq = l2cap_next_ertm_seq_nr(channel->next_tx_seq);
|
|
|
|
|
l2cap_ertm_next_tx_write_index(channel);
|
2017-07-25 14:04:11 +02:00
|
|
|
|
|
2018-10-12 22:06:01 +02:00
|
|
|
|
log_info("l2cap_ertm_store_fragment: tx_read_index %u, tx_write_index %u, num stored %u", channel->tx_read_index, channel->tx_write_index, channel->num_stored_tx_frames);
|
2017-07-25 14:04:11 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int l2cap_ertm_send(l2cap_channel_t * channel, uint8_t * data, uint16_t len){
|
|
|
|
|
if (len > channel->remote_mtu){
|
2018-10-12 22:06:01 +02:00
|
|
|
|
log_error("l2cap_ertm_send cid 0x%02x, data length exceeds remote MTU.", channel->local_cid);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 22:06:01 +02:00
|
|
|
|
if (!l2cap_ertm_can_store_packet_now(channel)){
|
|
|
|
|
log_error("l2cap_ertm_send cid 0x%02x, fragment store full", channel->local_cid);
|
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// check if it needs to get fragmented
|
2019-08-07 22:43:27 +02:00
|
|
|
|
uint16_t effective_mps = btstack_min(channel->remote_mps, channel->local_mps);
|
|
|
|
|
if (len > effective_mps){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// fragmentation needed.
|
|
|
|
|
l2cap_segmentation_and_reassembly_t sar = L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU;
|
|
|
|
|
int chunk_len;
|
|
|
|
|
while (len){
|
|
|
|
|
switch (sar){
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU:
|
2019-08-07 22:43:27 +02:00
|
|
|
|
chunk_len = effective_mps - 2; // sdu_length
|
2017-07-24 17:31:05 +02:00
|
|
|
|
l2cap_ertm_store_fragment(channel, sar, len, data, chunk_len);
|
|
|
|
|
len -= chunk_len;
|
|
|
|
|
sar = L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU;
|
|
|
|
|
break;
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU:
|
2019-08-07 22:43:27 +02:00
|
|
|
|
chunk_len = effective_mps;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
if (chunk_len >= len){
|
|
|
|
|
sar = L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU;
|
|
|
|
|
chunk_len = len;
|
|
|
|
|
}
|
|
|
|
|
l2cap_ertm_store_fragment(channel, sar, len, data, chunk_len);
|
|
|
|
|
len -= chunk_len;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
l2cap_ertm_store_fragment(channel, L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU, 0, data, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to send
|
|
|
|
|
l2cap_run();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 21:03:07 +02:00
|
|
|
|
static uint16_t l2cap_setup_options_ertm_request(l2cap_channel_t * channel, uint8_t * config_options){
|
2017-12-13 17:33:52 +01:00
|
|
|
|
int pos = 0;
|
|
|
|
|
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL;
|
|
|
|
|
config_options[pos++] = 9; // length
|
|
|
|
|
config_options[pos++] = (uint8_t) channel->mode;
|
|
|
|
|
config_options[pos++] = channel->num_rx_buffers; // == TxWindows size
|
|
|
|
|
config_options[pos++] = channel->local_max_transmit;
|
|
|
|
|
little_endian_store_16( config_options, pos, channel->local_retransmission_timeout_ms);
|
|
|
|
|
pos += 2;
|
|
|
|
|
little_endian_store_16( config_options, pos, channel->local_monitor_timeout_ms);
|
|
|
|
|
pos += 2;
|
|
|
|
|
little_endian_store_16( config_options, pos, channel->local_mps);
|
|
|
|
|
pos += 2;
|
2017-11-29 11:39:11 +01:00
|
|
|
|
//
|
2017-12-13 17:33:52 +01:00
|
|
|
|
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT;
|
|
|
|
|
config_options[pos++] = 2; // length
|
|
|
|
|
little_endian_store_16(config_options, pos, channel->local_mtu);
|
|
|
|
|
pos += 2;
|
2019-01-08 09:18:09 +01:00
|
|
|
|
|
|
|
|
|
// Issue: iOS (e.g. 10.2) uses "No FCS" as default while Core 5.0 specifies "FCS" as default
|
|
|
|
|
// Workaround: try to actively negotiate FCS option
|
2017-12-13 17:33:52 +01:00
|
|
|
|
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_FRAME_CHECK_SEQUENCE;
|
|
|
|
|
config_options[pos++] = 1; // length
|
|
|
|
|
config_options[pos++] = channel->fcs_option;
|
2018-01-21 05:45:24 +03:00
|
|
|
|
return pos; // 11+4+3=18
|
2017-07-24 17:31:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 21:03:07 +02:00
|
|
|
|
static uint16_t l2cap_setup_options_ertm_response(l2cap_channel_t * channel, uint8_t * config_options){
|
2017-12-13 17:33:52 +01:00
|
|
|
|
int pos = 0;
|
|
|
|
|
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL;
|
|
|
|
|
config_options[pos++] = 9; // length
|
|
|
|
|
config_options[pos++] = (uint8_t) channel->mode;
|
2017-10-08 21:03:07 +02:00
|
|
|
|
// less or equal to remote tx window size
|
2017-12-13 17:33:52 +01:00
|
|
|
|
config_options[pos++] = btstack_min(channel->num_tx_buffers, channel->remote_tx_window_size);
|
2017-10-08 21:03:07 +02:00
|
|
|
|
// max transmit in response shall be ignored -> use sender values
|
2017-12-13 17:33:52 +01:00
|
|
|
|
config_options[pos++] = channel->remote_max_transmit;
|
2017-10-08 21:03:07 +02:00
|
|
|
|
// A value for the Retransmission time-out shall be sent in a positive Configuration Response
|
|
|
|
|
// and indicates the value that will be used by the sender of the Configuration Response -> use our value
|
2017-12-13 17:33:52 +01:00
|
|
|
|
little_endian_store_16( config_options, pos, channel->local_retransmission_timeout_ms);
|
|
|
|
|
pos += 2;
|
2017-10-08 21:03:07 +02:00
|
|
|
|
// A value for the Monitor time-out shall be sent in a positive Configuration Response
|
|
|
|
|
// and indicates the value that will be used by the sender of the Configuration Response -> use our value
|
2017-12-13 17:33:52 +01:00
|
|
|
|
little_endian_store_16( config_options, pos, channel->local_monitor_timeout_ms);
|
|
|
|
|
pos += 2;
|
2017-10-08 21:03:07 +02:00
|
|
|
|
// less or equal to remote mps
|
2019-08-07 22:43:27 +02:00
|
|
|
|
uint16_t effective_mps = btstack_min(channel->remote_mps, channel->local_mps);
|
|
|
|
|
little_endian_store_16( config_options, pos, effective_mps);
|
2017-12-13 17:33:52 +01:00
|
|
|
|
pos += 2;
|
2017-11-10 16:03:53 +01:00
|
|
|
|
//
|
2017-12-13 17:33:52 +01:00
|
|
|
|
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT; // MTU
|
|
|
|
|
config_options[pos++] = 2; // length
|
|
|
|
|
little_endian_store_16(config_options, pos, channel->remote_mtu);
|
|
|
|
|
pos += 2;
|
|
|
|
|
#if 0
|
2017-11-29 11:39:11 +01:00
|
|
|
|
//
|
2017-12-13 17:33:52 +01:00
|
|
|
|
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_FRAME_CHECK_SEQUENCE;
|
|
|
|
|
config_options[pos++] = 1; // length
|
|
|
|
|
config_options[pos++] = channel->fcs_option;
|
|
|
|
|
#endif
|
2018-01-21 05:45:24 +03:00
|
|
|
|
return pos; // 11+4=15
|
2017-10-08 21:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
static int l2cap_ertm_send_supervisor_frame(l2cap_channel_t * channel, uint16_t control){
|
|
|
|
|
hci_reserve_packet_buffer();
|
|
|
|
|
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
|
|
|
|
|
log_info("S-Frame: control 0x%04x", control);
|
|
|
|
|
little_endian_store_16(acl_buffer, 8, control);
|
|
|
|
|
return l2cap_send_prepared(channel->local_cid, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 14:34:31 +02:00
|
|
|
|
static uint8_t l2cap_ertm_validate_local_config(l2cap_ertm_config_t * ertm_config){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
uint8_t result = ERROR_CODE_SUCCESS;
|
2017-07-25 15:03:09 +02:00
|
|
|
|
if (ertm_config->max_transmit < 1){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
log_error("max_transmit must be >= 1");
|
|
|
|
|
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
|
|
|
|
|
}
|
2017-07-25 15:03:09 +02:00
|
|
|
|
if (ertm_config->retransmission_timeout_ms < 2000){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
log_error("retransmission_timeout_ms must be >= 2000 ms");
|
|
|
|
|
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
|
|
|
|
|
}
|
2017-07-25 15:03:09 +02:00
|
|
|
|
if (ertm_config->monitor_timeout_ms < 12000){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
log_error("monitor_timeout_ms must be >= 12000 ms");
|
|
|
|
|
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
|
|
|
|
|
}
|
2017-07-25 15:03:09 +02:00
|
|
|
|
if (ertm_config->local_mtu < 48){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
log_error("local_mtu must be >= 48");
|
|
|
|
|
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
|
|
|
|
|
}
|
2017-07-25 15:03:09 +02:00
|
|
|
|
if (ertm_config->num_rx_buffers < 1){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
log_error("num_rx_buffers must be >= 1");
|
|
|
|
|
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
|
|
|
|
|
}
|
2017-07-25 15:03:09 +02:00
|
|
|
|
if (ertm_config->num_tx_buffers < 1){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
log_error("num_rx_buffers must be >= 1");
|
|
|
|
|
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-25 15:03:09 +02:00
|
|
|
|
static void l2cap_ertm_configure_channel(l2cap_channel_t * channel, l2cap_ertm_config_t * ertm_config, uint8_t * buffer, uint32_t size){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
channel->mode = L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION;
|
2017-07-25 15:03:09 +02:00
|
|
|
|
channel->ertm_mandatory = ertm_config->ertm_mandatory;
|
|
|
|
|
channel->local_max_transmit = ertm_config->max_transmit;
|
|
|
|
|
channel->local_retransmission_timeout_ms = ertm_config->retransmission_timeout_ms;
|
|
|
|
|
channel->local_monitor_timeout_ms = ertm_config->monitor_timeout_ms;
|
|
|
|
|
channel->local_mtu = ertm_config->local_mtu;
|
|
|
|
|
channel->num_rx_buffers = ertm_config->num_rx_buffers;
|
|
|
|
|
channel->num_tx_buffers = ertm_config->num_tx_buffers;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
2019-01-10 15:50:37 +01:00
|
|
|
|
// align buffer to 16-byte boundary to assert l2cap_ertm_rx_packet_state_t is aligned
|
2017-07-24 17:31:05 +02:00
|
|
|
|
int bytes_till_alignment = 16 - (((uintptr_t) buffer) & 0x0f);
|
|
|
|
|
buffer += bytes_till_alignment;
|
|
|
|
|
size -= bytes_till_alignment;
|
|
|
|
|
|
2019-01-10 15:50:37 +01:00
|
|
|
|
// setup state buffers - use void cast to avoid -Wcast-align warning
|
2017-07-24 17:31:05 +02:00
|
|
|
|
uint32_t pos = 0;
|
2019-01-10 15:50:37 +01:00
|
|
|
|
channel->rx_packets_state = (l2cap_ertm_rx_packet_state_t *) (void *) &buffer[pos];
|
2017-07-25 15:03:09 +02:00
|
|
|
|
pos += ertm_config->num_rx_buffers * sizeof(l2cap_ertm_rx_packet_state_t);
|
2019-01-10 15:50:37 +01:00
|
|
|
|
channel->tx_packets_state = (l2cap_ertm_tx_packet_state_t *) (void *) &buffer[pos];
|
2017-07-25 15:03:09 +02:00
|
|
|
|
pos += ertm_config->num_tx_buffers * sizeof(l2cap_ertm_tx_packet_state_t);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
// setup reassembly buffer
|
|
|
|
|
channel->reassembly_buffer = &buffer[pos];
|
2017-07-25 15:03:09 +02:00
|
|
|
|
pos += ertm_config->local_mtu;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
// divide rest of data equally
|
2017-07-25 15:03:09 +02:00
|
|
|
|
channel->local_mps = (size - pos) / (ertm_config->num_rx_buffers + ertm_config->num_tx_buffers);
|
2017-10-08 21:09:35 +02:00
|
|
|
|
log_info("Local MPS: %u", channel->local_mps);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
channel->rx_packets_data = &buffer[pos];
|
2017-10-08 21:09:35 +02:00
|
|
|
|
pos += ertm_config->num_rx_buffers * channel->local_mps;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
channel->tx_packets_data = &buffer[pos];
|
2017-11-10 16:03:53 +01:00
|
|
|
|
|
2019-01-08 09:18:09 +01:00
|
|
|
|
channel->fcs_option = ertm_config->fcs_option;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t l2cap_create_ertm_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm,
|
2017-07-25 15:03:09 +02:00
|
|
|
|
l2cap_ertm_config_t * ertm_config, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
2017-07-25 15:03:09 +02:00
|
|
|
|
log_info("L2CAP_CREATE_ERTM_CHANNEL addr %s, psm 0x%x, local mtu %u", bd_addr_to_str(address), psm, ertm_config->local_mtu);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
// validate local config
|
2017-09-18 14:34:31 +02:00
|
|
|
|
uint8_t result = l2cap_ertm_validate_local_config(ertm_config);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
if (result) return result;
|
|
|
|
|
|
2018-02-02 15:18:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, L2CAP_CHANNEL_TYPE_CLASSIC, address, BD_ADDR_TYPE_CLASSIC, psm, ertm_config->local_mtu, LEVEL_0);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
return BTSTACK_MEMORY_ALLOC_FAILED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// configure ERTM
|
2017-07-25 15:03:09 +02:00
|
|
|
|
l2cap_ertm_configure_channel(channel, ertm_config, buffer, size);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
// add to connections list
|
|
|
|
|
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
|
|
|
|
|
|
|
|
|
|
// store local_cid
|
|
|
|
|
if (out_local_cid){
|
|
|
|
|
*out_local_cid = channel->local_cid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if hci connection is already usable
|
|
|
|
|
hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
|
|
|
|
|
if (conn){
|
|
|
|
|
log_info("l2cap_create_channel, hci connection already exists");
|
|
|
|
|
l2cap_handle_connection_complete(conn->con_handle, channel);
|
|
|
|
|
// check if remote supported fearures are already received
|
|
|
|
|
if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
|
|
|
|
|
l2cap_handle_remote_supported_features_received(channel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l2cap_run();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_ertm_notify_channel_can_send(l2cap_channel_t * channel){
|
|
|
|
|
if (l2cap_ertm_can_store_packet_now(channel)){
|
|
|
|
|
channel->waiting_for_can_send_now = 0;
|
|
|
|
|
l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-25 15:03:09 +02:00
|
|
|
|
uint8_t l2cap_accept_ertm_connection(uint16_t local_cid, l2cap_ertm_config_t * ertm_config, uint8_t * buffer, uint32_t size){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
log_info("L2CAP_ACCEPT_ERTM_CONNECTION local_cid 0x%x", local_cid);
|
2018-02-05 15:30:55 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
|
|
|
|
|
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate local config
|
2017-09-18 14:34:31 +02:00
|
|
|
|
uint8_t result = l2cap_ertm_validate_local_config(ertm_config);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
if (result) return result;
|
|
|
|
|
|
|
|
|
|
// configure L2CAP ERTM
|
2017-07-25 15:03:09 +02:00
|
|
|
|
l2cap_ertm_configure_channel(channel, ertm_config, buffer, size);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
// continue
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
|
|
|
|
|
|
|
|
|
|
// process
|
|
|
|
|
l2cap_run();
|
|
|
|
|
|
|
|
|
|
return ERROR_CODE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t l2cap_ertm_set_busy(uint16_t local_cid){
|
2018-02-05 15:30:55 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
|
|
|
|
|
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
|
|
|
|
|
}
|
|
|
|
|
if (!channel->local_busy){
|
|
|
|
|
channel->local_busy = 1;
|
|
|
|
|
channel->send_supervisor_frame_receiver_not_ready = 1;
|
|
|
|
|
l2cap_run();
|
|
|
|
|
}
|
|
|
|
|
return ERROR_CODE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t l2cap_ertm_set_ready(uint16_t local_cid){
|
2018-02-05 15:30:55 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
|
|
|
|
|
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
|
|
|
|
|
}
|
|
|
|
|
if (channel->local_busy){
|
|
|
|
|
channel->local_busy = 0;
|
|
|
|
|
channel->send_supervisor_frame_receiver_ready_poll = 1;
|
|
|
|
|
l2cap_run();
|
|
|
|
|
}
|
|
|
|
|
return ERROR_CODE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:51:03 +02:00
|
|
|
|
// Process-ReqSeq
|
|
|
|
|
static void l2cap_ertm_process_req_seq(l2cap_channel_t * l2cap_channel, uint8_t req_seq){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
int num_buffers_acked = 0;
|
|
|
|
|
l2cap_ertm_tx_packet_state_t * tx_state;
|
2017-07-25 14:04:11 +02:00
|
|
|
|
log_info("l2cap_ertm_process_req_seq: tx_read_index %u, tx_write_index %u, req_seq %u", l2cap_channel->tx_read_index, l2cap_channel->tx_write_index, req_seq);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
while (1){
|
2017-07-24 17:51:03 +02:00
|
|
|
|
|
2017-07-25 14:04:11 +02:00
|
|
|
|
// no unack packets left
|
|
|
|
|
if (l2cap_channel->unacked_frames == 0) {
|
2017-07-24 17:51:03 +02:00
|
|
|
|
// stop retransmission timer
|
|
|
|
|
l2cap_ertm_stop_retransmission_timer(l2cap_channel);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index];
|
|
|
|
|
// calc delta
|
|
|
|
|
int delta = (req_seq - tx_state->tx_seq) & 0x03f;
|
|
|
|
|
if (delta == 0) break; // all packets acknowledged
|
|
|
|
|
if (delta > l2cap_channel->remote_tx_window_size) break;
|
|
|
|
|
|
|
|
|
|
num_buffers_acked++;
|
2018-10-12 22:06:01 +02:00
|
|
|
|
l2cap_channel->num_stored_tx_frames--;
|
2017-07-25 14:04:11 +02:00
|
|
|
|
l2cap_channel->unacked_frames--;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
log_info("RR seq %u => packet with tx_seq %u done", req_seq, tx_state->tx_seq);
|
|
|
|
|
|
|
|
|
|
l2cap_channel->tx_read_index++;
|
|
|
|
|
if (l2cap_channel->tx_read_index >= l2cap_channel->num_rx_buffers){
|
|
|
|
|
l2cap_channel->tx_read_index = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (num_buffers_acked){
|
2018-10-12 22:06:01 +02:00
|
|
|
|
log_info("num_buffers_acked %u", num_buffers_acked);
|
2018-10-23 15:43:44 +02:00
|
|
|
|
l2cap_ertm_notify_channel_can_send(l2cap_channel);
|
|
|
|
|
}
|
2017-07-24 17:31:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static l2cap_ertm_tx_packet_state_t * l2cap_ertm_get_tx_state(l2cap_channel_t * l2cap_channel, uint8_t tx_seq){
|
|
|
|
|
int i;
|
|
|
|
|
for (i=0;i<l2cap_channel->num_tx_buffers;i++){
|
|
|
|
|
l2cap_ertm_tx_packet_state_t * tx_state = &l2cap_channel->tx_packets_state[i];
|
|
|
|
|
if (tx_state->tx_seq == tx_seq) return tx_state;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @param delta number of frames in the future, >= 1
|
2017-10-09 11:27:39 +02:00
|
|
|
|
// @assumption size <= l2cap_channel->local_mps (checked in l2cap_acl_classic_handler)
|
2017-10-09 11:31:43 +02:00
|
|
|
|
static void l2cap_ertm_handle_out_of_sequence_sdu(l2cap_channel_t * l2cap_channel, l2cap_segmentation_and_reassembly_t sar, int delta, const uint8_t * payload, uint16_t size){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
log_info("Store SDU with delta %u", delta);
|
|
|
|
|
// get rx state for packet to store
|
|
|
|
|
int index = l2cap_channel->rx_store_index + delta - 1;
|
|
|
|
|
if (index > l2cap_channel->num_rx_buffers){
|
|
|
|
|
index -= l2cap_channel->num_rx_buffers;
|
|
|
|
|
}
|
|
|
|
|
log_info("Index of packet to store %u", index);
|
|
|
|
|
l2cap_ertm_rx_packet_state_t * rx_state = &l2cap_channel->rx_packets_state[index];
|
|
|
|
|
// check if buffer is free
|
|
|
|
|
if (rx_state->valid){
|
|
|
|
|
log_error("Packet buffer already used");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rx_state->valid = 1;
|
|
|
|
|
rx_state->sar = sar;
|
|
|
|
|
rx_state->len = size;
|
|
|
|
|
uint8_t * rx_buffer = &l2cap_channel->rx_packets_data[index];
|
|
|
|
|
memcpy(rx_buffer, payload, size);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 11:27:39 +02:00
|
|
|
|
// @assumption size <= l2cap_channel->local_mps (checked in l2cap_acl_classic_handler)
|
2017-10-09 11:31:43 +02:00
|
|
|
|
static void l2cap_ertm_handle_in_sequence_sdu(l2cap_channel_t * l2cap_channel, l2cap_segmentation_and_reassembly_t sar, const uint8_t * payload, uint16_t size){
|
2017-10-09 11:27:39 +02:00
|
|
|
|
uint16_t reassembly_sdu_length;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
switch (sar){
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU:
|
2017-10-09 11:27:39 +02:00
|
|
|
|
// assert total packet size <= our mtu
|
|
|
|
|
if (size > l2cap_channel->local_mtu) break;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// packet complete -> disapatch
|
2017-10-09 11:31:43 +02:00
|
|
|
|
l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, (uint8_t*) payload, size);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
break;
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU:
|
2017-10-09 11:27:39 +02:00
|
|
|
|
// read SDU len
|
|
|
|
|
reassembly_sdu_length = little_endian_read_16(payload, 0);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
payload += 2;
|
|
|
|
|
size -= 2;
|
2017-10-09 11:27:39 +02:00
|
|
|
|
// assert reassembled size <= our mtu
|
|
|
|
|
if (reassembly_sdu_length > l2cap_channel->local_mtu) break;
|
|
|
|
|
// store start segment
|
|
|
|
|
l2cap_channel->reassembly_sdu_length = reassembly_sdu_length;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
memcpy(&l2cap_channel->reassembly_buffer[0], payload, size);
|
|
|
|
|
l2cap_channel->reassembly_pos = size;
|
|
|
|
|
break;
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU:
|
2017-10-09 11:27:39 +02:00
|
|
|
|
// assert size of reassembled data <= our mtu
|
|
|
|
|
if (l2cap_channel->reassembly_pos + size > l2cap_channel->local_mtu) break;
|
|
|
|
|
// store continuation segment
|
2017-07-24 17:31:05 +02:00
|
|
|
|
memcpy(&l2cap_channel->reassembly_buffer[l2cap_channel->reassembly_pos], payload, size);
|
|
|
|
|
l2cap_channel->reassembly_pos += size;
|
|
|
|
|
break;
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU:
|
2017-10-09 11:27:39 +02:00
|
|
|
|
// assert size of reassembled data <= our mtu
|
|
|
|
|
if (l2cap_channel->reassembly_pos + size > l2cap_channel->local_mtu) break;
|
|
|
|
|
// store continuation segment
|
2017-07-24 17:31:05 +02:00
|
|
|
|
memcpy(&l2cap_channel->reassembly_buffer[l2cap_channel->reassembly_pos], payload, size);
|
|
|
|
|
l2cap_channel->reassembly_pos += size;
|
2017-10-09 11:27:39 +02:00
|
|
|
|
// assert size of reassembled data matches announced sdu length
|
|
|
|
|
if (l2cap_channel->reassembly_pos != l2cap_channel->reassembly_sdu_length) break;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// packet complete -> disapatch
|
|
|
|
|
l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->reassembly_buffer, l2cap_channel->reassembly_pos);
|
|
|
|
|
l2cap_channel->reassembly_pos = 0;
|
|
|
|
|
break;
|
2017-07-12 17:24:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-09-02 11:39:26 +02:00
|
|
|
|
static uint16_t l2cap_next_local_cid(void){
|
|
|
|
|
return local_source_cid++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint8_t l2cap_next_sig_id(void){
|
|
|
|
|
if (sig_seq_nr == 0xff) {
|
|
|
|
|
sig_seq_nr = 1;
|
|
|
|
|
} else {
|
|
|
|
|
sig_seq_nr++;
|
|
|
|
|
}
|
|
|
|
|
return sig_seq_nr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-13 10:30:46 +02:00
|
|
|
|
void l2cap_init(void){
|
2011-06-21 17:51:29 +00:00
|
|
|
|
signaling_responses_pending = 0;
|
|
|
|
|
|
2011-10-19 17:28:46 +00:00
|
|
|
|
l2cap_channels = NULL;
|
2018-02-02 17:52:13 +01:00
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2011-10-19 17:28:46 +00:00
|
|
|
|
l2cap_services = NULL;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
require_security_level2_for_outgoing_sdp = 0;
|
2018-02-02 17:52:13 +01:00
|
|
|
|
|
|
|
|
|
// Setup Connectionless Channel
|
|
|
|
|
l2cap_fixed_channel_connectionless.local_cid = L2CAP_CID_CONNECTIONLESS_CHANNEL;
|
2018-02-02 20:14:47 +01:00
|
|
|
|
l2cap_fixed_channel_connectionless.channel_type = L2CAP_CHANNEL_TYPE_CONNECTIONLESS;
|
2018-02-02 17:52:13 +01:00
|
|
|
|
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) &l2cap_fixed_channel_connectionless);
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2016-10-18 15:51:58 +02:00
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
2015-08-12 15:14:59 +02:00
|
|
|
|
l2cap_le_services = NULL;
|
2016-10-18 15:51:58 +02:00
|
|
|
|
#endif
|
2011-10-19 17:28:46 +00:00
|
|
|
|
|
2017-05-18 21:59:28 +02:00
|
|
|
|
#ifdef ENABLE_BLE
|
2016-02-05 17:37:51 +01:00
|
|
|
|
l2cap_event_packet_handler = NULL;
|
2018-01-30 19:01:33 +01:00
|
|
|
|
l2cap_le_custom_max_mtu = 0;
|
2015-02-20 14:21:01 +00:00
|
|
|
|
|
2018-02-02 17:52:13 +01:00
|
|
|
|
// Setup fixed ATT Channel
|
|
|
|
|
l2cap_fixed_channel_att.local_cid = L2CAP_CID_ATTRIBUTE_PROTOCOL;
|
2018-02-02 20:14:47 +01:00
|
|
|
|
l2cap_fixed_channel_att.channel_type = L2CAP_CHANNEL_TYPE_LE_FIXED;
|
2018-02-02 17:52:13 +01:00
|
|
|
|
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) &l2cap_fixed_channel_att);
|
|
|
|
|
|
|
|
|
|
// Setup fixed SM Channel
|
|
|
|
|
l2cap_fixed_channel_sm.local_cid = L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
|
2018-02-02 20:14:47 +01:00
|
|
|
|
l2cap_fixed_channel_sm.channel_type = L2CAP_CHANNEL_TYPE_LE_FIXED;
|
2018-02-02 17:52:13 +01:00
|
|
|
|
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) &l2cap_fixed_channel_sm);
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-08-02 10:51:41 +00:00
|
|
|
|
//
|
2010-07-18 16:30:16 +00:00
|
|
|
|
// register callback with HCI
|
2009-08-02 10:51:41 +00:00
|
|
|
|
//
|
2016-02-04 17:45:02 +01:00
|
|
|
|
hci_event_callback_registration.callback = &l2cap_hci_event_handler;
|
2016-02-03 14:09:26 +01:00
|
|
|
|
hci_add_event_handler(&hci_event_callback_registration);
|
|
|
|
|
|
2016-02-04 17:45:02 +01:00
|
|
|
|
hci_register_acl_packet_handler(&l2cap_acl_handler);
|
2016-02-03 14:09:26 +01:00
|
|
|
|
|
2016-10-19 15:39:31 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2016-02-18 17:12:57 +01:00
|
|
|
|
gap_connectable_control(0); // no services yet
|
2016-10-19 15:39:31 +02:00
|
|
|
|
#endif
|
2009-08-02 10:51:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 21:57:19 +01:00
|
|
|
|
void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
|
2017-05-18 21:59:28 +02:00
|
|
|
|
#ifdef ENABLE_BLE
|
2016-02-05 17:37:51 +01:00
|
|
|
|
l2cap_event_packet_handler = handler;
|
2017-05-18 21:59:28 +02:00
|
|
|
|
#else
|
2017-09-18 14:34:31 +02:00
|
|
|
|
UNUSED(handler); // ok: no code
|
2017-05-18 21:59:28 +02:00
|
|
|
|
#endif
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
|
2018-02-02 17:52:13 +01:00
|
|
|
|
UNUSED(con_handle); // ok: there is no con handle
|
2017-11-13 13:41:14 +01:00
|
|
|
|
|
2018-02-05 15:30:55 +01:00
|
|
|
|
l2cap_fixed_channel_t * channel = l2cap_fixed_channel_for_channel_id(channel_id);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!channel) return;
|
|
|
|
|
channel->waiting_for_can_send_now = 1;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
l2cap_notify_channel_can_send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
|
2017-09-18 14:34:31 +02:00
|
|
|
|
UNUSED(channel_id); // ok: only depends on Controller LE buffers
|
2016-12-22 22:16:16 +01:00
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
return hci_can_send_acl_packet_now(con_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t *l2cap_get_outgoing_buffer(void){
|
|
|
|
|
return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 11:51:28 +02:00
|
|
|
|
// only for L2CAP Basic Channels
|
2016-10-19 14:27:25 +02:00
|
|
|
|
int l2cap_reserve_packet_buffer(void){
|
|
|
|
|
return hci_reserve_packet_buffer();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 11:51:28 +02:00
|
|
|
|
// only for L2CAP Basic Channels
|
2016-10-19 14:27:25 +02:00
|
|
|
|
void l2cap_release_packet_buffer(void){
|
|
|
|
|
hci_release_packet_buffer();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 11:36:14 +01:00
|
|
|
|
static void l2cap_setup_header(uint8_t * acl_buffer, hci_con_handle_t con_handle, uint8_t packet_boundary, uint16_t remote_cid, uint16_t len){
|
2016-10-19 14:27:25 +02:00
|
|
|
|
// 0 - Connection handle : PB=pb : BC=00
|
2016-12-02 11:36:14 +01:00
|
|
|
|
little_endian_store_16(acl_buffer, 0, con_handle | (packet_boundary << 12) | (0 << 14));
|
2016-10-19 14:27:25 +02:00
|
|
|
|
// 2 - ACL length
|
|
|
|
|
little_endian_store_16(acl_buffer, 2, len + 4);
|
|
|
|
|
// 4 - L2CAP packet length
|
|
|
|
|
little_endian_store_16(acl_buffer, 4, len + 0);
|
|
|
|
|
// 6 - L2CAP channel DEST
|
|
|
|
|
little_endian_store_16(acl_buffer, 6, remote_cid);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 11:36:14 +01:00
|
|
|
|
// assumption - only on LE connections
|
2016-10-19 14:27:25 +02:00
|
|
|
|
int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
|
|
|
|
|
|
|
|
|
|
if (!hci_is_packet_buffer_reserved()){
|
|
|
|
|
log_error("l2cap_send_prepared_connectionless called without reserving packet first");
|
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hci_can_send_prepared_acl_packet_now(con_handle)){
|
|
|
|
|
log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
|
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
|
|
|
|
|
|
|
|
|
|
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
|
2016-12-02 11:36:14 +01:00
|
|
|
|
l2cap_setup_header(acl_buffer, con_handle, 0, cid, len);
|
2016-10-19 14:27:25 +02:00
|
|
|
|
// send
|
|
|
|
|
return hci_send_acl_packet_buffer(len+8);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 11:36:14 +01:00
|
|
|
|
// assumption - only on LE connections
|
2016-10-19 14:27:25 +02:00
|
|
|
|
int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
|
|
|
|
|
|
|
|
|
|
if (!hci_can_send_acl_packet_now(con_handle)){
|
|
|
|
|
log_info("l2cap_send cid 0x%02x, cannot send", cid);
|
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hci_reserve_packet_buffer();
|
|
|
|
|
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
|
|
|
|
|
|
|
|
|
|
memcpy(&acl_buffer[8], data, len);
|
|
|
|
|
|
|
|
|
|
return l2cap_send_prepared_connectionless(con_handle, cid, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
|
2017-03-02 16:48:13 +01:00
|
|
|
|
log_debug("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
|
2016-10-19 14:27:25 +02:00
|
|
|
|
uint8_t event[4];
|
|
|
|
|
event[0] = L2CAP_EVENT_CAN_SEND_NOW;
|
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
|
little_endian_store_16(event, 2, channel);
|
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
|
packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
2016-02-03 20:21:19 +01:00
|
|
|
|
static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
|
|
|
|
|
(* (channel->packet_handler))(type, channel->local_cid, data, size);
|
2010-06-09 16:05:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 11:49:04 +02:00
|
|
|
|
static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){
|
|
|
|
|
uint8_t event[4];
|
|
|
|
|
event[0] = event_code;
|
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
|
little_endian_store_16(event, 2, channel->local_cid);
|
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
|
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
|
|
|
|
|
}
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2016-10-18 11:49:04 +02:00
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2010-06-09 16:05:50 +00:00
|
|
|
|
void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
|
2014-01-10 17:35:17 +00:00
|
|
|
|
log_info("L2CAP_EVENT_CHANNEL_OPENED status 0x%x addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u, flush_timeout %u",
|
2016-02-19 13:57:49 +01:00
|
|
|
|
status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
|
2014-01-10 17:35:17 +00:00
|
|
|
|
channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
|
2019-01-07 20:55:28 +01:00
|
|
|
|
uint8_t event[26];
|
2010-06-09 16:05:50 +00:00
|
|
|
|
event[0] = L2CAP_EVENT_CHANNEL_OPENED;
|
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
|
event[2] = status;
|
2016-02-11 15:20:08 +01:00
|
|
|
|
reverse_bd_addr(channel->address, &event[3]);
|
2016-02-19 13:57:49 +01:00
|
|
|
|
little_endian_store_16(event, 9, channel->con_handle);
|
2016-01-31 00:07:32 +01:00
|
|
|
|
little_endian_store_16(event, 11, channel->psm);
|
|
|
|
|
little_endian_store_16(event, 13, channel->local_cid);
|
|
|
|
|
little_endian_store_16(event, 15, channel->remote_cid);
|
|
|
|
|
little_endian_store_16(event, 17, channel->local_mtu);
|
|
|
|
|
little_endian_store_16(event, 19, channel->remote_mtu);
|
|
|
|
|
little_endian_store_16(event, 21, channel->flush_timeout);
|
2016-10-23 22:08:44 +02:00
|
|
|
|
event[23] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0;
|
2019-01-07 20:55:28 +01:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
log_info("ERTM mode %u, fcs enabled %u", channel->mode, channel->fcs_option);
|
|
|
|
|
event[24] = channel->mode;
|
|
|
|
|
event[25] = channel->fcs_option;
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
event[24] = L2CAP_CHANNEL_MODE_BASIC;
|
|
|
|
|
event[25] = 0;
|
|
|
|
|
#endif
|
2010-06-09 16:05:50 +00:00
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2016-02-03 20:21:19 +01:00
|
|
|
|
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
|
2010-06-09 16:05:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 11:49:04 +02:00
|
|
|
|
static void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
|
2012-07-30 18:58:08 +00:00
|
|
|
|
log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
|
2016-10-18 11:49:04 +02:00
|
|
|
|
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) {
|
2012-07-30 18:58:08 +00:00
|
|
|
|
log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
|
2016-02-19 13:57:49 +01:00
|
|
|
|
bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid);
|
2010-06-09 16:05:50 +00:00
|
|
|
|
uint8_t event[16];
|
|
|
|
|
event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
|
|
|
|
|
event[1] = sizeof(event) - 2;
|
2016-02-11 15:20:08 +01:00
|
|
|
|
reverse_bd_addr(channel->address, &event[2]);
|
2016-02-19 13:57:49 +01:00
|
|
|
|
little_endian_store_16(event, 8, channel->con_handle);
|
2016-01-31 00:07:32 +01:00
|
|
|
|
little_endian_store_16(event, 10, channel->psm);
|
|
|
|
|
little_endian_store_16(event, 12, channel->local_cid);
|
|
|
|
|
little_endian_store_16(event, 14, channel->remote_cid);
|
2010-06-09 16:05:50 +00:00
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2016-02-03 20:21:19 +01:00
|
|
|
|
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
|
2009-07-20 21:39:43 +00:00
|
|
|
|
}
|
2010-06-09 16:05:50 +00:00
|
|
|
|
|
2019-01-07 21:47:59 +01:00
|
|
|
|
static void l2cap_handle_channel_open_failed(l2cap_channel_t * channel, uint8_t status){
|
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// emit ertm buffer released, as it's not needed. if in basic mode, it was either not allocated or already released
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
|
|
|
|
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2019-01-07 21:33:58 +01:00
|
|
|
|
l2cap_emit_channel_opened(channel, status);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-07 21:47:59 +01:00
|
|
|
|
static void l2cap_handle_channel_closed(l2cap_channel_t * channel){
|
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// emit ertm buffer released, as it's not needed anymore. if in basic mode, it was either not allocated or already released
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
|
|
|
|
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
l2cap_emit_channel_closed(channel);
|
|
|
|
|
}
|
2019-02-08 16:07:44 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static l2cap_fixed_channel_t * l2cap_channel_item_by_cid(uint16_t cid){
|
|
|
|
|
btstack_linked_list_iterator_t it;
|
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_fixed_channel_t * channel = (l2cap_fixed_channel_t*) btstack_linked_list_iterator_next(&it);
|
|
|
|
|
if (channel->local_cid == cid) {
|
|
|
|
|
return channel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-01-07 21:47:59 +01:00
|
|
|
|
|
2018-02-02 17:52:13 +01:00
|
|
|
|
// used for fixed channels in LE (ATT/SM) and Classic (Connectionless Channel). CID < 0x04
|
|
|
|
|
static l2cap_fixed_channel_t * l2cap_fixed_channel_for_channel_id(uint16_t local_cid){
|
|
|
|
|
if (local_cid >= 0x40) return NULL;
|
|
|
|
|
return (l2cap_fixed_channel_t*) l2cap_channel_item_by_cid(local_cid);
|
|
|
|
|
}
|
2018-02-05 15:57:21 +01:00
|
|
|
|
|
|
|
|
|
// used for Classic Channels + LE Data Channels. local_cid >= 0x40
|
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
|
|
|
|
static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
|
|
|
|
|
if (local_cid < 0x40) return NULL;
|
|
|
|
|
return (l2cap_channel_t*) l2cap_channel_item_by_cid(local_cid);
|
|
|
|
|
}
|
2016-04-01 14:15:39 +02:00
|
|
|
|
|
2016-03-31 15:12:40 +02:00
|
|
|
|
void l2cap_request_can_send_now_event(uint16_t local_cid){
|
|
|
|
|
l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
|
|
|
|
|
if (!channel) return;
|
|
|
|
|
channel->waiting_for_can_send_now = 1;
|
2017-07-24 16:48:44 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
|
|
|
|
l2cap_ertm_notify_channel_can_send(channel);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-03-31 15:12:40 +02:00
|
|
|
|
l2cap_notify_channel_can_send();
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-23 20:30:02 +00:00
|
|
|
|
int l2cap_can_send_packet_now(uint16_t local_cid){
|
2014-04-03 15:16:45 +00:00
|
|
|
|
l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
|
|
|
|
|
if (!channel) return 0;
|
2017-07-24 16:32:23 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
2017-07-24 16:48:44 +02:00
|
|
|
|
return l2cap_ertm_can_store_packet_now(channel);
|
2017-07-24 16:32:23 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-04-01 14:15:39 +02:00
|
|
|
|
return hci_can_send_acl_packet_now(channel->con_handle);
|
2014-04-03 15:16:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 14:54:03 +01:00
|
|
|
|
int l2cap_can_send_prepared_packet_now(uint16_t local_cid){
|
|
|
|
|
l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
|
|
|
|
|
if (!channel) return 0;
|
2017-07-24 16:32:23 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-04-01 14:15:39 +02:00
|
|
|
|
return hci_can_send_prepared_acl_packet_now(channel->con_handle);
|
2016-01-28 14:54:03 +01:00
|
|
|
|
}
|
2017-07-24 16:32:23 +02:00
|
|
|
|
|
2010-06-18 20:31:05 +00:00
|
|
|
|
uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
|
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
|
|
|
|
if (channel) {
|
|
|
|
|
return channel->remote_mtu;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-02-05 15:57:21 +01:00
|
|
|
|
#endif
|
2010-06-18 20:31:05 +00:00
|
|
|
|
|
2018-02-05 15:57:21 +01:00
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
2018-02-02 17:52:13 +01:00
|
|
|
|
static int l2cap_is_dynamic_channel_type(l2cap_channel_type_t channel_type){
|
|
|
|
|
switch (channel_type){
|
|
|
|
|
case L2CAP_CHANNEL_TYPE_CLASSIC:
|
|
|
|
|
case L2CAP_CHANNEL_TYPE_LE_DATA_CHANNEL:
|
|
|
|
|
return 1;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-05 15:57:21 +01:00
|
|
|
|
#endif
|
2018-02-02 17:52:13 +01:00
|
|
|
|
|
2018-02-02 20:14:47 +01:00
|
|
|
|
static int l2cap_is_le_channel_type(l2cap_channel_type_t channel_type){
|
|
|
|
|
switch (channel_type){
|
|
|
|
|
case L2CAP_CHANNEL_TYPE_LE_FIXED:
|
|
|
|
|
case L2CAP_CHANNEL_TYPE_LE_DATA_CHANNEL:
|
|
|
|
|
return 1;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 15:57:21 +01:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2018-02-02 17:52:13 +01:00
|
|
|
|
// RTX Timer only exist for dynamic channels
|
2016-01-20 16:00:45 +01:00
|
|
|
|
static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_t it;
|
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
|
|
|
|
if (&channel->rtx == ts) {
|
2014-01-10 15:42:59 +00:00
|
|
|
|
return channel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 16:00:45 +01:00
|
|
|
|
static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
|
2014-01-10 15:42:59 +00:00
|
|
|
|
l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
|
2016-07-01 11:30:53 +02:00
|
|
|
|
if (!channel) return;
|
2014-01-10 15:42:59 +00:00
|
|
|
|
|
|
|
|
|
log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
|
|
|
|
|
|
|
|
|
|
// "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
|
|
|
|
|
// and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
|
|
|
|
|
// notify client
|
2019-01-07 21:47:59 +01:00
|
|
|
|
l2cap_handle_channel_open_failed(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
|
|
|
|
|
// discard channel
|
2014-07-31 20:14:15 +00:00
|
|
|
|
// no need to stop timer here, it is removed from list during timer callback
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 10:39:39 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
2014-01-10 15:42:59 +00:00
|
|
|
|
static void l2cap_stop_rtx(l2cap_channel_t * channel){
|
|
|
|
|
log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
|
2016-01-20 15:58:46 +01:00
|
|
|
|
btstack_run_loop_remove_timer(&channel->rtx);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
}
|
2018-04-04 10:39:39 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2014-01-10 15:42:59 +00:00
|
|
|
|
|
|
|
|
|
static void l2cap_start_rtx(l2cap_channel_t * channel){
|
|
|
|
|
l2cap_stop_rtx(channel);
|
2014-12-19 16:21:39 +00:00
|
|
|
|
log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
|
2016-01-20 15:58:46 +01:00
|
|
|
|
btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
|
|
|
|
|
btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
|
|
|
|
|
btstack_run_loop_add_timer(&channel->rtx);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_start_ertx(l2cap_channel_t * channel){
|
|
|
|
|
log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
|
|
|
|
|
l2cap_stop_rtx(channel);
|
2016-01-20 15:58:46 +01:00
|
|
|
|
btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
|
|
|
|
|
btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
|
|
|
|
|
btstack_run_loop_add_timer(&channel->rtx);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-13 10:30:46 +02:00
|
|
|
|
void l2cap_require_security_level_2_for_outgoing_sdp(void){
|
2014-01-28 21:53:35 +00:00
|
|
|
|
require_security_level2_for_outgoing_sdp = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-17 16:30:31 +00:00
|
|
|
|
static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
|
2017-03-26 22:19:18 +02:00
|
|
|
|
return (psm == BLUETOOTH_PROTOCOL_SDP) && (!require_security_level2_for_outgoing_sdp);
|
2014-01-17 16:30:31 +00:00
|
|
|
|
}
|
2014-01-10 15:42:59 +00:00
|
|
|
|
|
2017-09-25 21:50:25 +02:00
|
|
|
|
static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, int identifier, ...){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
if (!hci_can_send_acl_packet_now(handle)){
|
|
|
|
|
log_info("l2cap_send_signaling_packet, cannot send");
|
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// log_info("l2cap_send_signaling_packet type %u", cmd);
|
|
|
|
|
hci_reserve_packet_buffer();
|
|
|
|
|
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
|
|
|
|
|
va_list argptr;
|
|
|
|
|
va_start(argptr, identifier);
|
|
|
|
|
uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
|
|
|
|
|
va_end(argptr);
|
|
|
|
|
// log_info("l2cap_send_signaling_packet con %u!", handle);
|
|
|
|
|
return hci_send_acl_packet_buffer(len);
|
|
|
|
|
}
|
2017-07-21 17:13:55 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// assumption - only on Classic connections
|
2018-04-30 11:51:28 +02:00
|
|
|
|
// cannot be used for L2CAP ERTM
|
2017-07-24 17:31:05 +02:00
|
|
|
|
int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
|
|
|
|
|
|
|
|
|
|
if (!hci_is_packet_buffer_reserved()){
|
|
|
|
|
log_error("l2cap_send_prepared called without reserving packet first");
|
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
2017-07-21 18:33:27 +02:00
|
|
|
|
}
|
2017-07-21 17:13:55 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
|
|
|
|
|
return -1; // TODO: define error
|
|
|
|
|
}
|
2017-07-21 17:13:55 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
|
|
|
|
|
log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
|
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
|
|
|
|
|
|
|
|
|
|
int fcs_size = 0;
|
2017-07-21 18:33:27 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
2017-11-10 16:03:53 +01:00
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION && channel->fcs_option){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
fcs_size = 2;
|
2017-07-21 18:33:27 +02:00
|
|
|
|
}
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#endif
|
2017-07-21 18:33:27 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// set non-flushable packet boundary flag if supported on Controller
|
|
|
|
|
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
|
|
|
|
|
uint8_t packet_boundary_flag = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
|
|
|
|
|
l2cap_setup_header(acl_buffer, channel->con_handle, packet_boundary_flag, channel->remote_cid, len + fcs_size);
|
2017-07-21 18:33:27 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
2017-12-13 17:38:14 +01:00
|
|
|
|
if (fcs_size){
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// calculate FCS over l2cap data
|
|
|
|
|
uint16_t fcs = crc16_calc(acl_buffer + 4, 4 + len);
|
|
|
|
|
log_info("I-Frame: fcs 0x%04x", fcs);
|
|
|
|
|
little_endian_store_16(acl_buffer, 8 + len, fcs);
|
2017-07-21 18:33:27 +02:00
|
|
|
|
}
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#endif
|
2017-07-21 17:13:55 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// send
|
|
|
|
|
return hci_send_acl_packet_buffer(len+8+fcs_size);
|
2017-07-17 11:05:48 +02:00
|
|
|
|
}
|
2011-06-24 18:37:10 +00:00
|
|
|
|
|
2016-12-02 11:36:14 +01:00
|
|
|
|
// assumption - only on Classic connections
|
2016-01-21 12:03:39 +01:00
|
|
|
|
int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
|
2014-08-11 21:40:02 +00:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
|
|
|
|
if (!channel) {
|
2016-01-21 12:03:39 +01:00
|
|
|
|
log_error("l2cap_send no channel for cid 0x%02x", local_cid);
|
2014-08-11 21:40:02 +00:00
|
|
|
|
return -1; // TODO: define error
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 11:05:48 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// send in ERTM
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
|
|
|
|
return l2cap_ertm_send(channel, data, len);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-10-23 19:43:04 +00:00
|
|
|
|
if (len > channel->remote_mtu){
|
2016-01-21 12:03:39 +01:00
|
|
|
|
log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
|
2014-10-23 19:43:04 +00:00
|
|
|
|
return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)){
|
2016-01-21 12:03:39 +01:00
|
|
|
|
log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
|
2011-07-29 21:04:41 +00:00
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 15:02:14 +00:00
|
|
|
|
hci_reserve_packet_buffer();
|
2014-04-03 19:51:07 +00:00
|
|
|
|
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
|
2017-07-17 11:05:48 +02:00
|
|
|
|
memcpy(&acl_buffer[8], data, len);
|
|
|
|
|
return l2cap_send_prepared(local_cid, len);
|
2011-07-29 21:04:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 13:57:49 +01:00
|
|
|
|
int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
|
|
|
|
|
return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
|
2014-01-22 22:59:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-05 22:24:51 +00:00
|
|
|
|
static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
|
|
|
|
|
channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
|
|
|
|
|
channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
|
|
|
|
|
}
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_BLE
|
2017-09-25 21:50:25 +02:00
|
|
|
|
static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, int identifier, ...){
|
2016-10-19 14:27:25 +02:00
|
|
|
|
|
|
|
|
|
if (!hci_can_send_acl_packet_now(handle)){
|
|
|
|
|
log_info("l2cap_send_le_signaling_packet, cannot send");
|
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// log_info("l2cap_send_le_signaling_packet type %u", cmd);
|
|
|
|
|
hci_reserve_packet_buffer();
|
|
|
|
|
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
|
|
|
|
|
va_list argptr;
|
|
|
|
|
va_start(argptr, identifier);
|
|
|
|
|
uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
|
|
|
|
|
va_end(argptr);
|
|
|
|
|
// log_info("l2cap_send_le_signaling_packet con %u!", handle);
|
|
|
|
|
return hci_send_acl_packet_buffer(len);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2012-01-05 22:24:51 +00:00
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
uint16_t l2cap_max_mtu(void){
|
|
|
|
|
return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
|
|
|
|
|
}
|
2012-01-05 22:24:51 +00:00
|
|
|
|
|
2018-01-21 17:01:12 +03:00
|
|
|
|
#ifdef ENABLE_BLE
|
2016-10-19 14:27:25 +02:00
|
|
|
|
uint16_t l2cap_max_le_mtu(void){
|
2018-01-30 19:01:33 +01:00
|
|
|
|
if (l2cap_le_custom_max_mtu != 0) return l2cap_le_custom_max_mtu;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
return l2cap_max_mtu();
|
|
|
|
|
}
|
2011-07-29 21:04:41 +00:00
|
|
|
|
|
2018-01-30 19:01:33 +01:00
|
|
|
|
void l2cap_set_max_le_mtu(uint16_t max_mtu){
|
|
|
|
|
if (max_mtu < l2cap_max_mtu()){
|
|
|
|
|
l2cap_le_custom_max_mtu = max_mtu;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-21 17:01:12 +03:00
|
|
|
|
#endif
|
2018-01-30 19:01:33 +01:00
|
|
|
|
|
2017-07-21 12:27:17 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
|
|
|
|
|
2017-10-08 14:34:51 +02:00
|
|
|
|
static uint16_t l2cap_setup_options_mtu(uint8_t * config_options, uint16_t mtu){
|
2017-11-29 11:39:11 +01:00
|
|
|
|
config_options[0] = L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT; // MTU
|
2017-07-21 12:27:17 +02:00
|
|
|
|
config_options[1] = 2; // len param
|
2017-10-08 14:34:51 +02:00
|
|
|
|
little_endian_store_16(config_options, 2, mtu);
|
2017-07-21 12:27:17 +02:00
|
|
|
|
return 4;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 15:34:04 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
2017-10-08 14:34:51 +02:00
|
|
|
|
static int l2cap_ertm_mode(l2cap_channel_t * channel){
|
2017-07-12 22:03:37 +02:00
|
|
|
|
hci_connection_t * connection = hci_connection_for_handle(channel->con_handle);
|
2017-10-08 14:34:51 +02:00
|
|
|
|
return ((connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_DONE)
|
|
|
|
|
&& (connection->l2cap_state.extended_feature_mask & 0x08));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static uint16_t l2cap_setup_options_request(l2cap_channel_t * channel, uint8_t * config_options){
|
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// use ERTM options if supported
|
|
|
|
|
if (l2cap_ertm_mode(channel)){
|
2017-10-08 21:03:07 +02:00
|
|
|
|
return l2cap_setup_options_ertm_request(channel, config_options);
|
2017-10-08 14:34:51 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
uint16_t mtu = channel->local_mtu;
|
|
|
|
|
return l2cap_setup_options_mtu(config_options, mtu);
|
|
|
|
|
}
|
2017-07-12 22:03:37 +02:00
|
|
|
|
|
2017-10-08 14:34:51 +02:00
|
|
|
|
static uint16_t l2cap_setup_options_response(l2cap_channel_t * channel, uint8_t * config_options){
|
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// use ERTM options if supported
|
|
|
|
|
if (l2cap_ertm_mode(channel)){
|
2017-10-08 21:03:07 +02:00
|
|
|
|
return l2cap_setup_options_ertm_response(channel, config_options);
|
2017-07-11 15:34:04 +02:00
|
|
|
|
}
|
2017-07-12 22:03:37 +02:00
|
|
|
|
#endif
|
2017-10-08 14:52:02 +02:00
|
|
|
|
uint16_t mtu = btstack_min(channel->local_mtu, channel->remote_mtu);
|
2017-10-08 14:34:51 +02:00
|
|
|
|
return l2cap_setup_options_mtu(config_options, mtu);
|
2017-07-11 15:34:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 22:20:52 +02:00
|
|
|
|
static uint32_t l2cap_extended_features_mask(void){
|
|
|
|
|
// extended features request supported, features: fixed channels, unicast connectionless data reception
|
|
|
|
|
uint32_t features = 0x280;
|
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
2017-12-13 17:58:30 +01:00
|
|
|
|
features |= 0x0028;
|
2017-07-11 22:20:52 +02:00
|
|
|
|
#endif
|
|
|
|
|
return features;
|
|
|
|
|
}
|
2017-07-21 12:27:17 +02:00
|
|
|
|
#endif
|
2017-07-11 22:20:52 +02:00
|
|
|
|
|
2011-06-24 18:37:10 +00:00
|
|
|
|
// MARK: L2CAP_RUN
|
2011-06-20 19:41:49 +00:00
|
|
|
|
// process outstanding signaling tasks
|
2015-11-06 19:43:35 +01:00
|
|
|
|
static void l2cap_run(void){
|
2011-06-21 17:51:29 +00:00
|
|
|
|
|
2015-04-07 22:43:51 +02:00
|
|
|
|
// log_info("l2cap_run: entered");
|
|
|
|
|
|
2011-06-21 17:51:29 +00:00
|
|
|
|
// check pending signaling responses
|
|
|
|
|
while (signaling_responses_pending){
|
|
|
|
|
|
|
|
|
|
hci_con_handle_t handle = signaling_responses[0].handle;
|
2014-08-11 21:40:02 +00:00
|
|
|
|
|
|
|
|
|
if (!hci_can_send_acl_packet_now(handle)) break;
|
|
|
|
|
|
2017-02-02 12:16:04 +01:00
|
|
|
|
uint8_t sig_id = signaling_responses[0].sig_id;
|
2014-10-31 15:46:38 +00:00
|
|
|
|
uint8_t response_code = signaling_responses[0].code;
|
2017-02-02 12:16:04 +01:00
|
|
|
|
uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT
|
2017-04-26 15:38:12 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2017-09-18 14:34:31 +02:00
|
|
|
|
uint16_t info_type = signaling_responses[0].data; // INFORMATION_REQUEST
|
2017-04-26 15:38:12 +02:00
|
|
|
|
uint16_t source_cid = signaling_responses[0].cid; // CONNECTION_REQUEST
|
|
|
|
|
#endif
|
2016-10-19 14:27:25 +02:00
|
|
|
|
|
2014-10-31 15:46:38 +00:00
|
|
|
|
// remove first item before sending (to avoid sending response mutliple times)
|
|
|
|
|
signaling_responses_pending--;
|
|
|
|
|
int i;
|
|
|
|
|
for (i=0; i < signaling_responses_pending; i++){
|
|
|
|
|
memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (response_code){
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2011-07-25 19:51:44 +00:00
|
|
|
|
case CONNECTION_REQUEST:
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, source_cid, 0, result, 0);
|
2014-01-16 22:42:21 +00:00
|
|
|
|
// also disconnect if result is 0x0003 - security blocked
|
2014-10-24 10:30:56 +00:00
|
|
|
|
if (result == 0x0003){
|
|
|
|
|
hci_disconnect_security_block(handle);
|
|
|
|
|
}
|
2011-07-25 19:51:44 +00:00
|
|
|
|
break;
|
2011-06-21 17:51:29 +00:00
|
|
|
|
case ECHO_REQUEST:
|
|
|
|
|
l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
|
|
|
|
|
break;
|
|
|
|
|
case INFORMATION_REQUEST:
|
2017-07-25 14:15:02 +02:00
|
|
|
|
switch (info_type){
|
|
|
|
|
case L2CAP_INFO_TYPE_CONNECTIONLESS_MTU: {
|
2016-10-19 10:49:33 +02:00
|
|
|
|
uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
|
2017-07-25 14:15:02 +02:00
|
|
|
|
l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, info_type, 0, sizeof(connectionless_mtu), &connectionless_mtu);
|
2016-10-19 10:49:33 +02:00
|
|
|
|
}
|
2014-01-10 10:26:25 +00:00
|
|
|
|
break;
|
2017-07-25 14:15:02 +02:00
|
|
|
|
case L2CAP_INFO_TYPE_EXTENDED_FEATURES_SUPPORTED: {
|
2017-07-11 22:20:52 +02:00
|
|
|
|
uint32_t features = l2cap_extended_features_mask();
|
2017-07-25 14:15:02 +02:00
|
|
|
|
l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, info_type, 0, sizeof(features), &features);
|
2016-10-19 10:49:33 +02:00
|
|
|
|
}
|
2014-01-10 10:26:25 +00:00
|
|
|
|
break;
|
2017-07-25 14:15:02 +02:00
|
|
|
|
case L2CAP_INFO_TYPE_FIXED_CHANNELS_SUPPORTED: {
|
2016-10-19 10:49:33 +02:00
|
|
|
|
uint8_t map[8];
|
|
|
|
|
memset(map, 0, 8);
|
|
|
|
|
map[0] = 0x06; // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
|
2017-07-25 14:15:02 +02:00
|
|
|
|
l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, info_type, 0, sizeof(map), &map);
|
2016-10-19 10:49:33 +02:00
|
|
|
|
}
|
2014-01-10 10:26:25 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// all other types are not supported
|
2017-07-25 14:15:02 +02:00
|
|
|
|
l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, info_type, 1, 0, NULL);
|
2014-01-10 10:26:25 +00:00
|
|
|
|
break;
|
2011-06-21 17:51:29 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2012-10-30 19:55:24 +00:00
|
|
|
|
case COMMAND_REJECT:
|
2014-01-24 15:58:42 +00:00
|
|
|
|
l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
|
2016-10-06 16:09:54 +02:00
|
|
|
|
break;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2016-01-21 15:33:36 +01:00
|
|
|
|
#ifdef ENABLE_BLE
|
2016-10-06 16:09:54 +02:00
|
|
|
|
case LE_CREDIT_BASED_CONNECTION_REQUEST:
|
|
|
|
|
l2cap_send_le_signaling_packet(handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, sig_id, 0, 0, 0, 0, result);
|
|
|
|
|
break;
|
2014-02-18 21:26:46 +00:00
|
|
|
|
case COMMAND_REJECT_LE:
|
|
|
|
|
l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
|
2012-10-30 19:55:24 +00:00
|
|
|
|
break;
|
2014-02-18 21:26:46 +00:00
|
|
|
|
#endif
|
2011-06-21 17:51:29 +00:00
|
|
|
|
default:
|
|
|
|
|
// should not happen
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 21:00:25 +02:00
|
|
|
|
#if defined(ENABLE_CLASSIC) || defined(ENABLE_BLE)
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_t it;
|
2017-09-18 14:34:31 +02:00
|
|
|
|
#endif
|
2016-10-19 14:27:25 +02:00
|
|
|
|
|
2017-07-11 22:20:52 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// send l2cap information request if neccessary
|
|
|
|
|
hci_connections_get_iterator(&it);
|
|
|
|
|
while(btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
|
|
|
|
|
if (connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST){
|
2018-09-25 15:45:35 +02:00
|
|
|
|
if (!hci_can_send_acl_packet_now(connection->con_handle)) break;
|
2017-07-11 22:20:52 +02:00
|
|
|
|
connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W4_EXTENDED_FEATURE_RESPONSE;
|
|
|
|
|
uint8_t sig_id = l2cap_next_sig_id();
|
2017-07-25 14:15:02 +02:00
|
|
|
|
uint8_t info_type = L2CAP_INFO_TYPE_EXTENDED_FEATURES_SUPPORTED;
|
2017-07-11 22:20:52 +02:00
|
|
|
|
l2cap_send_signaling_packet(connection->con_handle, INFORMATION_REQUEST, sig_id, info_type);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2018-01-21 05:45:24 +03:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
uint8_t config_options[18];
|
|
|
|
|
#else
|
2017-07-11 12:18:35 +02:00
|
|
|
|
uint8_t config_options[10];
|
2018-01-21 05:45:24 +03:00
|
|
|
|
#endif
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
2011-11-09 16:36:59 +00:00
|
|
|
|
|
2016-01-20 14:50:38 +01:00
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 16:27:59 +01:00
|
|
|
|
|
|
|
|
|
if (channel->channel_type != L2CAP_CHANNEL_TYPE_CLASSIC) continue;
|
|
|
|
|
|
2015-04-07 22:43:51 +02:00
|
|
|
|
// log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
|
2011-06-20 19:41:49 +00:00
|
|
|
|
switch (channel->state){
|
|
|
|
|
|
2014-01-17 16:30:31 +00:00
|
|
|
|
case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
|
2014-01-16 20:11:45 +00:00
|
|
|
|
case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
2014-01-17 10:37:42 +00:00
|
|
|
|
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
|
2014-01-16 20:11:45 +00:00
|
|
|
|
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
|
2016-02-19 13:57:49 +01:00
|
|
|
|
l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
|
2014-01-16 20:11:45 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2011-06-21 18:50:39 +00:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
|
2014-08-20 20:15:50 +00:00
|
|
|
|
if (!hci_can_send_command_packet_now()) break;
|
2011-06-21 20:28:09 +00:00
|
|
|
|
// send connection request - set state first
|
|
|
|
|
channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
|
2011-06-21 18:50:39 +00:00
|
|
|
|
// BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
|
2018-09-03 19:20:27 +02:00
|
|
|
|
memcpy(l2cap_outgoing_classic_addr, channel->address, 6);
|
|
|
|
|
hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
|
2011-06-21 18:50:39 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2011-06-20 19:58:46 +00:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
2015-04-07 22:43:51 +02:00
|
|
|
|
channel->state = L2CAP_STATE_INVALID;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
|
2011-06-20 19:58:46 +00:00
|
|
|
|
// discard channel - l2cap_finialize_channel_close without sending l2cap close event
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_remove(&it);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2011-06-20 19:58:46 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2011-06-20 21:03:40 +00:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
2011-07-08 16:30:00 +00:00
|
|
|
|
channel->state = L2CAP_STATE_CONFIG;
|
2012-01-05 22:24:51 +00:00
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
|
2016-02-19 13:57:49 +01:00
|
|
|
|
l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
|
2011-06-20 21:03:40 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2011-06-20 19:47:05 +00:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
2011-06-20 19:47:05 +00:00
|
|
|
|
// success, start l2cap handshake
|
2011-06-20 21:15:46 +00:00
|
|
|
|
channel->local_sig_id = l2cap_next_sig_id();
|
2011-06-20 19:47:05 +00:00
|
|
|
|
channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
l2cap_start_rtx(channel);
|
2011-06-20 19:47:05 +00:00
|
|
|
|
break;
|
2011-07-08 16:30:00 +00:00
|
|
|
|
|
|
|
|
|
case L2CAP_STATE_CONFIG:
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
2019-01-10 15:42:45 +01:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// fallback to basic mode if ERTM requested but not not supported by remote
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
|
|
|
|
if (!l2cap_ertm_mode(channel)){
|
|
|
|
|
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
|
|
|
|
|
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2011-07-24 08:49:09 +00:00
|
|
|
|
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
|
2012-10-30 19:55:24 +00:00
|
|
|
|
uint16_t flags = 0;
|
2012-01-05 22:24:51 +00:00
|
|
|
|
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
|
2012-10-30 19:55:24 +00:00
|
|
|
|
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
|
|
|
|
|
flags = 1;
|
|
|
|
|
} else {
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
|
|
|
|
|
}
|
|
|
|
|
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
|
2017-07-12 21:52:48 +02:00
|
|
|
|
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
|
2016-02-19 13:57:49 +01:00
|
|
|
|
l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL);
|
2017-07-12 21:52:48 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
} else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED){
|
|
|
|
|
channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED);
|
|
|
|
|
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
|
2017-10-08 14:34:51 +02:00
|
|
|
|
uint16_t options_size = l2cap_setup_options_response(channel, config_options);
|
2017-07-12 21:52:48 +02:00
|
|
|
|
l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS, options_size, &config_options);
|
|
|
|
|
#endif
|
|
|
|
|
} else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
|
2012-10-30 19:55:24 +00:00
|
|
|
|
channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
|
2017-10-08 14:34:51 +02:00
|
|
|
|
uint16_t options_size = l2cap_setup_options_response(channel, config_options);
|
2017-07-12 21:52:48 +02:00
|
|
|
|
l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_SUCCESS, options_size, &config_options);
|
2012-10-30 19:55:24 +00:00
|
|
|
|
} else {
|
2017-07-12 21:52:48 +02:00
|
|
|
|
l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_SUCCESS, 0, NULL);
|
2012-10-30 19:55:24 +00:00
|
|
|
|
}
|
|
|
|
|
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
|
2011-07-08 16:30:00 +00:00
|
|
|
|
}
|
2011-07-24 08:49:09 +00:00
|
|
|
|
else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
|
2012-01-05 22:24:51 +00:00
|
|
|
|
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
|
2011-07-08 16:30:00 +00:00
|
|
|
|
channel->local_sig_id = l2cap_next_sig_id();
|
2017-10-08 14:34:51 +02:00
|
|
|
|
uint16_t options_size = l2cap_setup_options_request(channel, config_options);
|
2017-07-11 15:34:04 +02:00
|
|
|
|
l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, options_size, &config_options);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
l2cap_start_rtx(channel);
|
2011-07-08 16:30:00 +00:00
|
|
|
|
}
|
|
|
|
|
if (l2cap_channel_ready_for_open(channel)){
|
|
|
|
|
channel->state = L2CAP_STATE_OPEN;
|
|
|
|
|
l2cap_emit_channel_opened(channel, 0); // success
|
|
|
|
|
}
|
2011-06-20 21:03:40 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2011-06-20 19:58:46 +00:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
2015-04-07 22:43:51 +02:00
|
|
|
|
channel->state = L2CAP_STATE_INVALID;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
// we don't start an RTX timer for a disconnect - there's no point in closing the channel if the other side doesn't respond :)
|
2011-11-09 16:36:59 +00:00
|
|
|
|
l2cap_finialize_channel_close(channel); // -- remove from list
|
2018-11-22 21:55:08 +01:00
|
|
|
|
channel = NULL;
|
2011-06-20 19:58:46 +00:00
|
|
|
|
break;
|
2011-06-20 19:47:05 +00:00
|
|
|
|
|
2011-06-20 19:58:46 +00:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
2011-06-20 21:15:46 +00:00
|
|
|
|
channel->local_sig_id = l2cap_next_sig_id();
|
2011-06-20 19:41:49 +00:00
|
|
|
|
channel->state = L2CAP_STATE_WAIT_DISCONNECT;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
|
2011-06-20 19:41:49 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-16 18:33:01 +02:00
|
|
|
|
|
2018-12-13 14:39:43 +01:00
|
|
|
|
|
2017-07-16 18:33:01 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
2018-11-22 21:55:08 +01:00
|
|
|
|
|
2018-12-13 14:39:43 +01:00
|
|
|
|
// handle channel finalize on L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE
|
2018-11-22 21:55:08 +01:00
|
|
|
|
if (!channel) continue;
|
2017-07-17 15:06:38 +02:00
|
|
|
|
|
2018-12-13 14:39:43 +01:00
|
|
|
|
// ERTM mode
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
|
|
|
|
|
|
|
|
|
// check if we can still send
|
|
|
|
|
if (channel->con_handle == HCI_CON_HANDLE_INVALID) continue;
|
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
|
|
|
|
|
|
|
|
|
|
// send if we have more data and remote windows isn't full yet
|
|
|
|
|
log_debug("unacked_frames %u < min( stored frames %u, remote tx window size %u)?", channel->unacked_frames, channel->num_stored_tx_frames, channel->remote_tx_window_size);
|
|
|
|
|
if (channel->unacked_frames < btstack_min(channel->num_stored_tx_frames, channel->remote_tx_window_size)){
|
|
|
|
|
channel->unacked_frames++;
|
|
|
|
|
int index = channel->tx_send_index;
|
|
|
|
|
channel->tx_send_index++;
|
|
|
|
|
if (channel->tx_send_index >= channel->num_tx_buffers){
|
|
|
|
|
channel->tx_send_index = 0;
|
2017-07-18 16:21:14 +02:00
|
|
|
|
}
|
2018-12-13 14:39:43 +01:00
|
|
|
|
l2cap_ertm_send_information_frame(channel, index, 0); // final = 0
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (channel->send_supervisor_frame_receiver_ready){
|
|
|
|
|
channel->send_supervisor_frame_receiver_ready = 0;
|
|
|
|
|
log_info("Send S-Frame: RR %u, final %u", channel->req_seq, channel->set_final_bit_after_packet_with_poll_bit_set);
|
|
|
|
|
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY, 0, channel->set_final_bit_after_packet_with_poll_bit_set, channel->req_seq);
|
|
|
|
|
channel->set_final_bit_after_packet_with_poll_bit_set = 0;
|
|
|
|
|
l2cap_ertm_send_supervisor_frame(channel, control);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (channel->send_supervisor_frame_receiver_ready_poll){
|
|
|
|
|
channel->send_supervisor_frame_receiver_ready_poll = 0;
|
|
|
|
|
log_info("Send S-Frame: RR %u with poll=1 ", channel->req_seq);
|
|
|
|
|
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY, 1, 0, channel->req_seq);
|
|
|
|
|
l2cap_ertm_send_supervisor_frame(channel, control);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (channel->send_supervisor_frame_receiver_not_ready){
|
|
|
|
|
channel->send_supervisor_frame_receiver_not_ready = 0;
|
|
|
|
|
log_info("Send S-Frame: RNR %u", channel->req_seq);
|
|
|
|
|
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY, 0, 0, channel->req_seq);
|
|
|
|
|
l2cap_ertm_send_supervisor_frame(channel, control);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (channel->send_supervisor_frame_reject){
|
|
|
|
|
channel->send_supervisor_frame_reject = 0;
|
|
|
|
|
log_info("Send S-Frame: REJ %u", channel->req_seq);
|
|
|
|
|
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT, 0, 0, channel->req_seq);
|
|
|
|
|
l2cap_ertm_send_supervisor_frame(channel, control);
|
|
|
|
|
continue;
|
2017-07-18 16:21:14 +02:00
|
|
|
|
}
|
2018-12-13 14:39:43 +01:00
|
|
|
|
if (channel->send_supervisor_frame_selective_reject){
|
|
|
|
|
channel->send_supervisor_frame_selective_reject = 0;
|
|
|
|
|
log_info("Send S-Frame: SREJ %u", channel->expected_tx_seq);
|
|
|
|
|
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT, 0, channel->set_final_bit_after_packet_with_poll_bit_set, channel->expected_tx_seq);
|
|
|
|
|
channel->set_final_bit_after_packet_with_poll_bit_set = 0;
|
|
|
|
|
l2cap_ertm_send_supervisor_frame(channel, control);
|
2017-07-18 16:21:14 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-12-13 14:39:43 +01:00
|
|
|
|
|
|
|
|
|
if (channel->srej_active){
|
|
|
|
|
int i;
|
|
|
|
|
for (i=0;i<channel->num_tx_buffers;i++){
|
|
|
|
|
l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[i];
|
|
|
|
|
if (tx_state->retransmission_requested) {
|
|
|
|
|
tx_state->retransmission_requested = 0;
|
|
|
|
|
uint8_t final = channel->set_final_bit_after_packet_with_poll_bit_set;
|
|
|
|
|
channel->set_final_bit_after_packet_with_poll_bit_set = 0;
|
|
|
|
|
l2cap_ertm_send_information_frame(channel, i, final);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i == channel->num_tx_buffers){
|
|
|
|
|
// no retransmission request found
|
|
|
|
|
channel->srej_active = 0;
|
|
|
|
|
} else {
|
|
|
|
|
// packet was sent
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-18 16:21:14 +02:00
|
|
|
|
}
|
2017-07-16 18:33:01 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2011-06-20 19:41:49 +00:00
|
|
|
|
}
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2014-10-10 15:20:54 +00:00
|
|
|
|
|
2016-10-14 16:00:59 +02:00
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
2018-02-02 16:27:59 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
2016-07-08 22:22:38 +02:00
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
2016-10-05 15:49:18 +02:00
|
|
|
|
uint8_t * acl_buffer;
|
|
|
|
|
uint8_t * l2cap_payload;
|
|
|
|
|
uint16_t pos;
|
|
|
|
|
uint16_t payload_size;
|
2018-01-13 22:24:02 +01:00
|
|
|
|
uint16_t mps;
|
2016-07-08 22:22:38 +02:00
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 16:27:59 +01:00
|
|
|
|
|
|
|
|
|
if (channel->channel_type != L2CAP_CHANNEL_TYPE_LE_DATA_CHANNEL) continue;
|
|
|
|
|
|
2016-07-08 22:22:38 +02:00
|
|
|
|
// log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
|
|
|
|
|
switch (channel->state){
|
2016-07-22 23:23:19 +02:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
|
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
|
|
|
|
channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
|
|
|
|
|
// le psm, source cid, mtu, mps, initial credits
|
2016-10-04 15:48:27 +02:00
|
|
|
|
channel->local_sig_id = l2cap_next_sig_id();
|
2016-10-06 16:09:54 +02:00
|
|
|
|
channel->credits_incoming = channel->new_credits_incoming;
|
|
|
|
|
channel->new_credits_incoming = 0;
|
2018-01-13 22:24:02 +01:00
|
|
|
|
mps = btstack_min(l2cap_max_le_mtu(), channel->local_mtu);
|
|
|
|
|
l2cap_send_le_signaling_packet( channel->con_handle, LE_CREDIT_BASED_CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid, channel->local_mtu, mps, channel->credits_incoming);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
break;
|
2016-10-04 16:05:27 +02:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
|
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
|
|
|
|
// TODO: support larger MPS
|
|
|
|
|
channel->state = L2CAP_STATE_OPEN;
|
2016-10-06 16:09:54 +02:00
|
|
|
|
channel->credits_incoming = channel->new_credits_incoming;
|
|
|
|
|
channel->new_credits_incoming = 0;
|
2018-01-13 22:24:02 +01:00
|
|
|
|
mps = btstack_min(l2cap_max_le_mtu(), channel->local_mtu);
|
|
|
|
|
l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->local_mtu, mps, channel->credits_incoming, 0);
|
2016-10-05 11:01:26 +02:00
|
|
|
|
// notify client
|
2016-10-18 11:49:04 +02:00
|
|
|
|
l2cap_emit_le_channel_opened(channel, 0);
|
2016-10-04 16:05:27 +02:00
|
|
|
|
break;
|
2016-07-27 16:44:25 +02:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
|
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
|
|
|
|
channel->state = L2CAP_STATE_INVALID;
|
2016-10-06 16:09:54 +02:00
|
|
|
|
l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, 0, 0, channel->reason);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
// discard channel - l2cap_finialize_channel_close without sending l2cap close event
|
|
|
|
|
btstack_linked_list_iterator_remove(&it);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
break;
|
2016-10-05 15:49:18 +02:00
|
|
|
|
case L2CAP_STATE_OPEN:
|
2016-10-05 22:09:32 +02:00
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
|
|
|
|
|
|
|
|
|
// send credits
|
|
|
|
|
if (channel->new_credits_incoming){
|
|
|
|
|
log_info("l2cap: sending %u credits", channel->new_credits_incoming);
|
|
|
|
|
channel->local_sig_id = l2cap_next_sig_id();
|
|
|
|
|
uint16_t new_credits = channel->new_credits_incoming;
|
|
|
|
|
channel->new_credits_incoming = 0;
|
|
|
|
|
channel->credits_incoming += new_credits;
|
|
|
|
|
l2cap_send_le_signaling_packet(channel->con_handle, LE_FLOW_CONTROL_CREDIT, channel->local_sig_id, channel->remote_cid, new_credits);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send data
|
2016-10-05 15:49:18 +02:00
|
|
|
|
if (!channel->send_sdu_buffer) break;
|
|
|
|
|
if (!channel->credits_outgoing) break;
|
2016-10-05 22:09:32 +02:00
|
|
|
|
|
2016-10-05 15:49:18 +02:00
|
|
|
|
// send part of SDU
|
|
|
|
|
hci_reserve_packet_buffer();
|
|
|
|
|
acl_buffer = hci_get_outgoing_packet_buffer();
|
|
|
|
|
l2cap_payload = acl_buffer + 8;
|
|
|
|
|
pos = 0;
|
|
|
|
|
if (!channel->send_sdu_pos){
|
|
|
|
|
// store SDU len
|
|
|
|
|
channel->send_sdu_pos += 2;
|
|
|
|
|
little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len);
|
|
|
|
|
pos += 2;
|
|
|
|
|
}
|
|
|
|
|
payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos);
|
2016-10-05 22:09:32 +02:00
|
|
|
|
log_info("len %u, pos %u => payload %u, credits %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size, channel->credits_outgoing);
|
2016-10-05 15:49:18 +02:00
|
|
|
|
memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len
|
|
|
|
|
pos += payload_size;
|
|
|
|
|
channel->send_sdu_pos += payload_size;
|
2016-12-02 11:36:14 +01:00
|
|
|
|
l2cap_setup_header(acl_buffer, channel->con_handle, 0, channel->remote_cid, pos);
|
2016-10-05 15:49:18 +02:00
|
|
|
|
// done
|
|
|
|
|
|
2016-10-05 22:09:32 +02:00
|
|
|
|
channel->credits_outgoing--;
|
2016-10-05 15:49:18 +02:00
|
|
|
|
|
|
|
|
|
if (channel->send_sdu_pos >= channel->send_sdu_len + 2){
|
|
|
|
|
channel->send_sdu_buffer = NULL;
|
2016-10-18 11:49:04 +02:00
|
|
|
|
// send done event
|
|
|
|
|
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_PACKET_SENT);
|
|
|
|
|
// inform about can send now
|
|
|
|
|
l2cap_le_notify_channel_can_send(channel);
|
2016-10-05 15:49:18 +02:00
|
|
|
|
}
|
|
|
|
|
hci_send_acl_packet_buffer(8 + pos);
|
|
|
|
|
break;
|
2016-10-05 11:58:27 +02:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
|
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
|
|
|
|
channel->local_sig_id = l2cap_next_sig_id();
|
|
|
|
|
channel->state = L2CAP_STATE_WAIT_DISCONNECT;
|
|
|
|
|
l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
|
|
|
|
|
break;
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
|
|
|
|
|
if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
|
|
|
|
|
channel->state = L2CAP_STATE_INVALID;
|
|
|
|
|
l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
|
|
|
|
|
l2cap_le_finialize_channel_close(channel); // -- remove from list
|
|
|
|
|
break;
|
2016-07-08 22:22:38 +02:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2016-07-08 22:22:38 +02:00
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#ifdef ENABLE_BLE
|
2014-10-10 15:20:54 +00:00
|
|
|
|
// send l2cap con paramter update if necessary
|
|
|
|
|
hci_connections_get_iterator(&it);
|
2016-01-20 14:50:38 +01:00
|
|
|
|
while(btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
|
2015-11-03 21:24:12 +01:00
|
|
|
|
if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
|
2015-08-12 14:27:30 +02:00
|
|
|
|
if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
|
2014-10-10 15:20:54 +00:00
|
|
|
|
switch (connection->le_con_parameter_update_state){
|
2015-08-12 14:27:30 +02:00
|
|
|
|
case CON_PARAMETER_UPDATE_SEND_REQUEST:
|
|
|
|
|
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
|
2018-09-12 16:21:14 +02:00
|
|
|
|
l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, l2cap_next_sig_id(),
|
2015-08-12 14:27:30 +02:00
|
|
|
|
connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
|
|
|
|
|
break;
|
2014-10-10 15:20:54 +00:00
|
|
|
|
case CON_PARAMETER_UPDATE_SEND_RESPONSE:
|
2015-08-12 14:27:30 +02:00
|
|
|
|
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
|
|
|
|
|
l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
|
2014-10-10 15:20:54 +00:00
|
|
|
|
break;
|
|
|
|
|
case CON_PARAMETER_UPDATE_DENY:
|
2015-08-12 14:27:30 +02:00
|
|
|
|
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
|
|
|
|
|
l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
|
2014-10-10 15:20:54 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-13 19:40:50 +00:00
|
|
|
|
#endif
|
2014-10-10 15:20:54 +00:00
|
|
|
|
|
2015-04-07 22:43:51 +02:00
|
|
|
|
// log_info("l2cap_run: exit");
|
2011-06-20 19:41:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2016-02-19 13:57:49 +01:00
|
|
|
|
static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
|
2014-01-23 14:32:23 +00:00
|
|
|
|
if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
|
2018-10-02 14:12:55 +02:00
|
|
|
|
log_info("connection complete con_handle %04x - for channel %p cid 0x%04x", (int) con_handle, channel, channel->local_cid);
|
2014-01-23 14:32:23 +00:00
|
|
|
|
// success, start l2cap handshake
|
2016-02-19 13:57:49 +01:00
|
|
|
|
channel->con_handle = con_handle;
|
2014-01-23 14:32:23 +00:00
|
|
|
|
// check remote SSP feature first
|
|
|
|
|
channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 22:20:52 +02:00
|
|
|
|
static void l2cap_ready_to_connect(l2cap_channel_t * channel){
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
2017-07-13 14:21:57 +02:00
|
|
|
|
// assumption: outgoing connection
|
|
|
|
|
hci_connection_t * connection = hci_connection_for_handle(channel->con_handle);
|
|
|
|
|
if (connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_IDLE){
|
|
|
|
|
connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST;
|
|
|
|
|
channel->state = L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES;
|
|
|
|
|
return;
|
2017-07-11 22:20:52 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// fine, go ahead
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-23 14:32:23 +00:00
|
|
|
|
static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
|
2014-01-28 18:43:31 +00:00
|
|
|
|
if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
|
2014-01-23 14:32:23 +00:00
|
|
|
|
|
|
|
|
|
// we have been waiting for remote supported features, if both support SSP,
|
2014-01-28 21:53:35 +00:00
|
|
|
|
log_info("l2cap received remote supported features, sec_level_0_allowed for psm %u = %u", channel->psm, l2cap_security_level_0_allowed_for_PSM(channel->psm));
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
|
2014-01-23 14:32:23 +00:00
|
|
|
|
// request security level 2
|
|
|
|
|
channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
|
2017-08-23 17:37:38 +02:00
|
|
|
|
channel->required_security_level = LEVEL_2;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
gap_request_security_level(channel->con_handle, LEVEL_2);
|
2017-07-24 17:31:05 +02:00
|
|
|
|
return;
|
2017-07-14 14:58:50 +02:00
|
|
|
|
}
|
2017-07-24 17:31:05 +02:00
|
|
|
|
|
|
|
|
|
l2cap_ready_to_connect(channel);
|
2017-07-14 14:58:50 +02:00
|
|
|
|
}
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#endif
|
2017-07-14 14:58:50 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
2018-02-02 15:18:59 +01:00
|
|
|
|
static l2cap_channel_t * l2cap_create_channel_entry(btstack_packet_handler_t packet_handler, l2cap_channel_type_t channel_type, bd_addr_t address, bd_addr_type_t address_type,
|
2017-07-24 17:31:05 +02:00
|
|
|
|
uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
|
2017-07-13 16:17:43 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
|
|
|
|
|
if (!channel) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fill in
|
|
|
|
|
channel->packet_handler = packet_handler;
|
2018-02-02 15:18:59 +01:00
|
|
|
|
channel->channel_type = channel_type;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
bd_addr_copy(channel->address, address);
|
|
|
|
|
channel->address_type = address_type;
|
|
|
|
|
channel->psm = psm;
|
|
|
|
|
channel->local_mtu = local_mtu;
|
2017-11-24 17:02:34 +01:00
|
|
|
|
channel->remote_mtu = L2CAP_DEFAULT_MTU;
|
2017-07-24 17:31:05 +02:00
|
|
|
|
channel->required_security_level = security_level;
|
2017-07-21 17:03:44 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
//
|
|
|
|
|
channel->local_cid = l2cap_next_local_cid();
|
2018-04-11 12:38:31 +02:00
|
|
|
|
channel->con_handle = HCI_CON_HANDLE_INVALID;
|
2017-07-21 17:03:44 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
// set initial state
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
|
|
|
|
|
channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
|
|
|
|
|
channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
|
|
|
|
|
channel->local_sig_id = L2CAP_SIG_ID_INVALID;
|
2017-07-21 17:03:44 +02:00
|
|
|
|
|
2019-08-06 17:38:15 +02:00
|
|
|
|
log_info("create channel %p, local_cid 0x%04x", channel, channel->local_cid);
|
2018-10-02 14:12:55 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
return channel;
|
2017-07-13 16:17:43 +02:00
|
|
|
|
}
|
2019-08-06 17:38:15 +02:00
|
|
|
|
|
|
|
|
|
static void l2cap_free_channel_entry(l2cap_channel_t * channel){
|
|
|
|
|
log_info("free channel %p, local_cid 0x%04x", channel, channel->local_cid);
|
2019-08-06 17:59:22 +02:00
|
|
|
|
// assert all timers are stopped
|
2019-08-06 17:52:45 +02:00
|
|
|
|
l2cap_stop_rtx(channel);
|
2019-08-06 17:59:22 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
l2cap_ertm_stop_retransmission_timer(channel);
|
|
|
|
|
l2cap_ertm_stop_monitor_timer(channel);
|
|
|
|
|
#endif
|
2019-08-06 17:52:45 +02:00
|
|
|
|
// free memory
|
2019-08-06 17:38:15 +02:00
|
|
|
|
btstack_memory_l2cap_channel_free(channel);
|
|
|
|
|
}
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#endif
|
2017-07-13 16:17:43 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2017-07-11 22:20:52 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
/**
|
|
|
|
|
* @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
|
|
|
|
|
* @param packet_handler
|
|
|
|
|
* @param address
|
|
|
|
|
* @param psm
|
|
|
|
|
* @param mtu
|
|
|
|
|
* @param local_cid
|
|
|
|
|
*/
|
2017-07-11 22:20:52 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid){
|
|
|
|
|
// limit MTU to the size of our outtgoing HCI buffer
|
|
|
|
|
uint16_t local_mtu = btstack_min(mtu, l2cap_max_mtu());
|
2017-07-14 14:58:50 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u -> local mtu %u", bd_addr_to_str(address), psm, mtu, local_mtu);
|
|
|
|
|
|
2018-02-02 15:18:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, L2CAP_CHANNEL_TYPE_CLASSIC, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
|
2017-07-11 22:20:52 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
return BTSTACK_MEMORY_ALLOC_FAILED;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
|
|
|
|
|
#endif
|
2017-07-12 11:43:46 +02:00
|
|
|
|
|
2017-07-11 22:20:52 +02:00
|
|
|
|
// add to connections list
|
|
|
|
|
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
|
|
|
|
|
|
|
|
|
|
// store local_cid
|
|
|
|
|
if (out_local_cid){
|
|
|
|
|
*out_local_cid = channel->local_cid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if hci connection is already usable
|
|
|
|
|
hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
|
|
|
|
|
if (conn){
|
2018-10-02 14:12:55 +02:00
|
|
|
|
log_info("l2cap_create_channel, hci connection 0x%04x already exists", conn->con_handle);
|
2017-07-11 22:20:52 +02:00
|
|
|
|
l2cap_handle_connection_complete(conn->con_handle, channel);
|
|
|
|
|
// check if remote supported fearures are already received
|
|
|
|
|
if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
|
|
|
|
|
l2cap_handle_remote_supported_features_received(channel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
l2cap_run();
|
2017-07-11 22:20:52 +02:00
|
|
|
|
|
2017-07-24 17:31:05 +02:00
|
|
|
|
return 0;
|
2017-07-11 22:20:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 10:17:05 +02:00
|
|
|
|
void l2cap_disconnect(uint16_t local_cid, uint8_t reason){
|
2012-07-30 18:58:08 +00:00
|
|
|
|
log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
|
2010-03-04 23:03:40 +00:00
|
|
|
|
// find channel for local_cid
|
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2009-08-08 21:29:38 +00:00
|
|
|
|
if (channel) {
|
2011-06-20 19:58:46 +00:00
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
|
2009-08-08 21:29:38 +00:00
|
|
|
|
}
|
2011-06-20 19:41:49 +00:00
|
|
|
|
// process
|
|
|
|
|
l2cap_run();
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-14 22:42:14 +00:00
|
|
|
|
static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
|
2017-11-17 17:41:37 +01:00
|
|
|
|
// mark all channels before emitting open events as these could trigger new connetion requests to the same device
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_t it;
|
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
|
|
|
|
if (bd_addr_cmp( channel->address, address) != 0) continue;
|
2014-07-31 21:18:43 +00:00
|
|
|
|
// channel for this address found
|
|
|
|
|
switch (channel->state){
|
|
|
|
|
case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
|
2017-11-17 17:41:37 +01:00
|
|
|
|
channel->state = L2CAP_STATE_EMIT_OPEN_FAILED_AND_DISCARD;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// emit and free marked entries. restart loop to deal with list changes
|
|
|
|
|
int done = 0;
|
|
|
|
|
while (!done) {
|
|
|
|
|
done = 1;
|
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
2017-11-17 17:41:37 +01:00
|
|
|
|
if (channel->state == L2CAP_STATE_EMIT_OPEN_FAILED_AND_DISCARD){
|
|
|
|
|
done = 0;
|
2009-12-14 22:42:14 +00:00
|
|
|
|
// failure, forward error code
|
2019-01-07 21:47:59 +01:00
|
|
|
|
l2cap_handle_channel_open_failed(channel, status);
|
2009-12-14 22:42:14 +00:00
|
|
|
|
// discard channel
|
2017-11-17 17:41:37 +01:00
|
|
|
|
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2014-07-31 21:18:43 +00:00
|
|
|
|
break;
|
2017-11-17 17:41:37 +01:00
|
|
|
|
}
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-17 17:41:37 +01:00
|
|
|
|
|
2009-12-14 22:42:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_t it;
|
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
2016-02-10 17:09:24 +01:00
|
|
|
|
if ( ! bd_addr_cmp( channel->address, address) ){
|
2014-01-23 14:32:23 +00:00
|
|
|
|
l2cap_handle_connection_complete(handle, channel);
|
2009-08-08 21:55:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-20 19:47:05 +00:00
|
|
|
|
// process
|
|
|
|
|
l2cap_run();
|
2009-12-14 22:42:14 +00:00
|
|
|
|
}
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2009-12-14 22:42:14 +00:00
|
|
|
|
|
2016-02-05 17:37:51 +01:00
|
|
|
|
static void l2cap_notify_channel_can_send(void){
|
2018-02-02 20:14:47 +01:00
|
|
|
|
int done = 0;
|
|
|
|
|
while (!done){
|
|
|
|
|
done = 1;
|
|
|
|
|
btstack_linked_list_iterator_t it;
|
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
|
|
|
|
if (!channel->waiting_for_can_send_now) continue;
|
|
|
|
|
int can_send = 0;
|
|
|
|
|
if (l2cap_is_le_channel_type(channel->channel_type)){
|
|
|
|
|
#ifdef ENABLE_BLE
|
|
|
|
|
can_send = hci_can_send_acl_le_packet_now();
|
|
|
|
|
#endif
|
|
|
|
|
} else {
|
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2018-10-12 22:06:01 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// skip ertm channels as they only depend on free buffers in storage
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_BASIC){
|
|
|
|
|
can_send = hci_can_send_acl_classic_packet_now();
|
|
|
|
|
}
|
|
|
|
|
#else
|
2018-02-02 20:14:47 +01:00
|
|
|
|
can_send = hci_can_send_acl_classic_packet_now();
|
2018-10-12 22:06:01 +02:00
|
|
|
|
#endif /* ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE */
|
|
|
|
|
#endif /* ENABLE_CLASSIC */
|
2018-02-02 20:14:47 +01:00
|
|
|
|
}
|
|
|
|
|
if (!can_send) continue;
|
|
|
|
|
// requeue for fairness
|
|
|
|
|
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
|
|
|
|
|
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
|
|
|
|
|
// emit can send
|
|
|
|
|
channel->waiting_for_can_send_now = 0;
|
|
|
|
|
l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
|
|
|
|
|
// exit inner loop as we just broke the iterator, but try again
|
|
|
|
|
done = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-02-05 17:37:51 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-08 16:08:03 +02:00
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
|
|
|
|
|
|
|
|
|
static int l2cap_send_open_failed_on_hci_disconnect(l2cap_channel_t * channel){
|
|
|
|
|
// open cannot fail for for incoming connections
|
|
|
|
|
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) return 0;
|
|
|
|
|
|
|
|
|
|
// check state
|
|
|
|
|
switch (channel->state){
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
|
|
|
|
|
case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
|
|
|
|
|
case L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES:
|
|
|
|
|
case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
|
|
|
|
|
case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
|
|
|
|
|
case L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES:
|
|
|
|
|
case L2CAP_STATE_WAIT_CONNECT_RSP:
|
|
|
|
|
case L2CAP_STATE_CONFIG:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
|
|
|
|
|
case L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE:
|
2017-11-17 17:41:37 +01:00
|
|
|
|
case L2CAP_STATE_EMIT_OPEN_FAILED_AND_DISCARD:
|
2017-08-08 16:08:03 +02:00
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
case L2CAP_STATE_OPEN:
|
|
|
|
|
case L2CAP_STATE_CLOSED:
|
|
|
|
|
case L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES:
|
|
|
|
|
case L2CAP_STATE_WAIT_DISCONNECT:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
|
|
|
|
|
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
|
|
|
|
|
case L2CAP_STATE_INVALID:
|
|
|
|
|
case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
|
|
|
|
|
return 0;
|
2017-08-08 16:38:06 +02:00
|
|
|
|
// no default here, to get a warning about new states
|
2017-08-08 16:08:03 +02:00
|
|
|
|
}
|
2017-08-08 16:38:06 +02:00
|
|
|
|
// still, the compiler insists on a return value
|
|
|
|
|
return 0;
|
2017-08-08 16:08:03 +02:00
|
|
|
|
}
|
2018-04-04 10:39:39 +02:00
|
|
|
|
#endif
|
2017-08-08 16:08:03 +02:00
|
|
|
|
|
2018-04-04 10:39:39 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2017-08-08 16:08:03 +02:00
|
|
|
|
static void l2cap_handle_hci_disconnect_event(l2cap_channel_t * channel){
|
|
|
|
|
if (l2cap_send_open_failed_on_hci_disconnect(channel)){
|
2019-01-07 21:47:59 +01:00
|
|
|
|
l2cap_handle_channel_open_failed(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
|
2017-08-08 16:08:03 +02:00
|
|
|
|
} else {
|
2019-01-07 21:47:59 +01:00
|
|
|
|
l2cap_handle_channel_closed(channel);
|
2017-08-08 16:08:03 +02:00
|
|
|
|
}
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2017-08-08 16:08:03 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-01-26 14:19:39 +01:00
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
|
|
|
|
static void l2cap_handle_hci_le_disconnect_event(l2cap_channel_t * channel){
|
|
|
|
|
if (l2cap_send_open_failed_on_hci_disconnect(channel)){
|
|
|
|
|
l2cap_emit_le_channel_opened(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
|
|
|
|
|
} else {
|
|
|
|
|
l2cap_emit_le_channel_closed(channel);
|
|
|
|
|
}
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2018-01-26 14:19:39 +01:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2017-08-08 16:08:03 +02:00
|
|
|
|
|
2016-02-04 17:45:02 +01:00
|
|
|
|
static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
|
2016-12-22 22:16:16 +01:00
|
|
|
|
|
2017-09-18 14:34:31 +02:00
|
|
|
|
UNUSED(packet_type); // ok: registered with hci_event_callback_registration
|
|
|
|
|
UNUSED(cid); // ok: there is no channel
|
|
|
|
|
UNUSED(size); // ok: fixed format events read from HCI buffer
|
2009-12-14 22:42:14 +00:00
|
|
|
|
|
2017-09-18 14:34:31 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2009-12-14 22:42:14 +00:00
|
|
|
|
bd_addr_t address;
|
2011-07-22 10:14:38 +00:00
|
|
|
|
int hci_con_used;
|
2017-09-18 14:34:31 +02:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
|
|
|
|
hci_con_handle_t handle;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
btstack_linked_list_iterator_t it;
|
2017-09-18 14:34:31 +02:00
|
|
|
|
#endif
|
2016-10-19 14:27:25 +02:00
|
|
|
|
|
2016-02-19 14:52:36 +01:00
|
|
|
|
switch(hci_event_packet_get_type(packet)){
|
2009-12-14 22:42:14 +00:00
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
// Notify channel packet handler if they can send now
|
|
|
|
|
case HCI_EVENT_TRANSPORT_PACKET_SENT:
|
|
|
|
|
case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
|
2018-09-11 08:27:33 +02:00
|
|
|
|
case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED:
|
2016-10-19 14:27:25 +02:00
|
|
|
|
l2cap_run(); // try sending signaling packets first
|
|
|
|
|
l2cap_notify_channel_can_send();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case HCI_EVENT_COMMAND_STATUS:
|
2018-09-03 19:20:27 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
|
|
|
|
// check command status for create connection for errors
|
|
|
|
|
if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_create_connection)){
|
|
|
|
|
// cache outgoing address and reset
|
|
|
|
|
memcpy(address, l2cap_outgoing_classic_addr, 6);
|
|
|
|
|
memset(l2cap_outgoing_classic_addr, 0, 6);
|
|
|
|
|
// error => outgoing connection failed
|
|
|
|
|
uint8_t status = hci_event_command_status_get_status(packet);
|
|
|
|
|
if (status){
|
|
|
|
|
l2cap_handle_connection_failed_for_addr(address, status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-10-19 14:27:25 +02:00
|
|
|
|
l2cap_run(); // try sending signaling packets first
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2009-12-14 22:42:14 +00:00
|
|
|
|
// handle connection complete events
|
|
|
|
|
case HCI_EVENT_CONNECTION_COMPLETE:
|
2016-02-11 15:20:08 +01:00
|
|
|
|
reverse_bd_addr(&packet[5], address);
|
2009-12-14 22:42:14 +00:00
|
|
|
|
if (packet[2] == 0){
|
2016-01-31 00:07:32 +01:00
|
|
|
|
handle = little_endian_read_16(packet, 3);
|
2009-12-14 22:42:14 +00:00
|
|
|
|
l2cap_handle_connection_success_for_addr(address, handle);
|
|
|
|
|
} else {
|
|
|
|
|
l2cap_handle_connection_failed_for_addr(address, packet[2]);
|
2009-08-09 18:39:53 +00:00
|
|
|
|
}
|
2009-12-14 22:42:14 +00:00
|
|
|
|
break;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
|
2009-12-14 22:42:14 +00:00
|
|
|
|
// handle successful create connection cancel command
|
|
|
|
|
case HCI_EVENT_COMMAND_COMPLETE:
|
2016-02-11 16:12:55 +01:00
|
|
|
|
if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
|
2009-12-14 22:42:14 +00:00
|
|
|
|
if (packet[5] == 0){
|
2016-02-11 15:20:08 +01:00
|
|
|
|
reverse_bd_addr(&packet[6], address);
|
2009-12-14 22:42:14 +00:00
|
|
|
|
// CONNECTION TERMINATED BY LOCAL HOST (0X16)
|
|
|
|
|
l2cap_handle_connection_failed_for_addr(address, 0x16);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-22 15:31:50 +00:00
|
|
|
|
l2cap_run(); // try sending signaling packets first
|
|
|
|
|
break;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2009-12-14 22:42:14 +00:00
|
|
|
|
|
2017-08-08 16:08:03 +02:00
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
2009-12-14 22:42:14 +00:00
|
|
|
|
// handle disconnection complete events
|
|
|
|
|
case HCI_EVENT_DISCONNECTION_COMPLETE:
|
2016-12-22 22:44:50 +01:00
|
|
|
|
handle = little_endian_read_16(packet, 3);
|
2017-08-08 16:08:03 +02:00
|
|
|
|
// send l2cap open failed or closed events for all channels on this handle and free them
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (channel->con_handle != handle) continue;
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_remove(&it);
|
2018-02-02 16:27:59 +01:00
|
|
|
|
switch(channel->channel_type){
|
|
|
|
|
#ifdef ENABLE_CLASSIC
|
|
|
|
|
case L2CAP_CHANNEL_TYPE_CLASSIC:
|
|
|
|
|
l2cap_handle_hci_disconnect_event(channel);
|
|
|
|
|
break;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2016-10-18 15:51:58 +02:00
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
2018-02-02 16:27:59 +01:00
|
|
|
|
case L2CAP_CHANNEL_TYPE_LE_DATA_CHANNEL:
|
|
|
|
|
l2cap_handle_hci_le_disconnect_event(channel);
|
|
|
|
|
break;
|
2016-10-04 16:32:28 +02:00
|
|
|
|
#endif
|
2018-02-02 16:27:59 +01:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-17 16:42:06 +02:00
|
|
|
|
break;
|
2017-08-08 16:08:03 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2018-05-17 16:42:06 +02:00
|
|
|
|
|
2009-12-14 22:42:14 +00:00
|
|
|
|
// HCI Connection Timeouts
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2009-12-14 22:42:14 +00:00
|
|
|
|
case L2CAP_EVENT_TIMEOUT_CHECK:
|
2016-01-31 00:07:32 +01:00
|
|
|
|
handle = little_endian_read_16(packet, 2);
|
2015-11-11 11:52:36 +01:00
|
|
|
|
if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
|
2010-08-26 20:26:48 +00:00
|
|
|
|
if (hci_authentication_active_for_handle(handle)) break;
|
2011-07-22 10:14:38 +00:00
|
|
|
|
hci_con_used = 0;
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (channel->con_handle != handle) continue;
|
2014-07-31 21:18:43 +00:00
|
|
|
|
hci_con_used = 1;
|
|
|
|
|
break;
|
2009-12-14 22:42:14 +00:00
|
|
|
|
}
|
2011-07-22 10:14:38 +00:00
|
|
|
|
if (hci_con_used) break;
|
2014-08-11 21:20:07 +00:00
|
|
|
|
if (!hci_can_send_command_packet_now()) break;
|
2011-06-21 19:30:58 +00:00
|
|
|
|
hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
|
2009-12-14 22:42:14 +00:00
|
|
|
|
break;
|
2011-07-16 15:19:19 +00:00
|
|
|
|
|
2014-01-17 16:30:31 +00:00
|
|
|
|
case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
|
2016-01-31 00:07:32 +01:00
|
|
|
|
handle = little_endian_read_16(packet, 3);
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (channel->con_handle != handle) continue;
|
2018-10-02 14:12:55 +02:00
|
|
|
|
log_info("remote supported features, channel %p, cid %04x - state %u", channel, channel->local_cid, channel->state);
|
2014-01-23 14:32:23 +00:00
|
|
|
|
l2cap_handle_remote_supported_features_received(channel);
|
2014-07-31 21:18:43 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2014-01-17 16:30:31 +00:00
|
|
|
|
|
2016-01-30 23:58:36 +01:00
|
|
|
|
case GAP_EVENT_SECURITY_LEVEL:
|
2016-01-31 00:07:32 +01:00
|
|
|
|
handle = little_endian_read_16(packet, 2);
|
2018-10-02 14:12:55 +02:00
|
|
|
|
log_info("l2cap - security level update for handle 0x%04x", handle);
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (channel->con_handle != handle) continue;
|
2014-01-28 18:43:31 +00:00
|
|
|
|
|
2015-05-13 10:34:21 +02:00
|
|
|
|
gap_security_level_t actual_level = (gap_security_level_t) packet[4];
|
2014-01-28 18:43:31 +00:00
|
|
|
|
gap_security_level_t required_level = channel->required_security_level;
|
|
|
|
|
|
2018-10-02 14:12:55 +02:00
|
|
|
|
log_info("channel %p, cid %04x - state %u: actual %u >= required %u?", channel, channel->local_cid, channel->state, actual_level, required_level);
|
2017-08-23 17:37:38 +02:00
|
|
|
|
|
2014-01-17 16:30:31 +00:00
|
|
|
|
switch (channel->state){
|
|
|
|
|
case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
|
2014-01-28 18:43:31 +00:00
|
|
|
|
if (actual_level >= required_level){
|
2017-07-13 12:12:03 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// we need to know if ERTM is supported before sending a config response
|
|
|
|
|
hci_connection_t * connection = hci_connection_for_handle(channel->con_handle);
|
2018-09-25 16:25:01 +02:00
|
|
|
|
connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST;
|
|
|
|
|
channel->state = L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES;
|
2017-07-13 12:12:03 +02:00
|
|
|
|
#else
|
2014-01-17 16:30:31 +00:00
|
|
|
|
channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
|
2017-07-13 12:12:03 +02:00
|
|
|
|
l2cap_emit_incoming_connection(channel);
|
|
|
|
|
#endif
|
2014-01-17 16:30:31 +00:00
|
|
|
|
} else {
|
2015-05-14 22:59:36 +02:00
|
|
|
|
channel->reason = 0x0003; // security block
|
2014-01-17 16:30:31 +00:00
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
|
2014-01-28 18:43:31 +00:00
|
|
|
|
if (actual_level >= required_level){
|
2017-07-11 22:20:52 +02:00
|
|
|
|
l2cap_ready_to_connect(channel);
|
2014-01-17 16:30:31 +00:00
|
|
|
|
} else {
|
|
|
|
|
// disconnnect, authentication not good enough
|
|
|
|
|
hci_disconnect_security_block(handle);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2014-07-31 21:18:43 +00:00
|
|
|
|
}
|
2014-01-16 23:15:21 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2016-07-22 23:23:19 +02:00
|
|
|
|
|
2009-12-14 22:42:14 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
2009-08-09 18:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 15:20:40 +00:00
|
|
|
|
l2cap_run();
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-02 12:16:04 +01:00
|
|
|
|
static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t cid, uint16_t data){
|
2012-05-10 20:32:39 +00:00
|
|
|
|
// Vol 3, Part A, 4.3: "The DCID and SCID fields shall be ignored when the result field indi- cates the connection was refused."
|
2011-07-25 19:51:44 +00:00
|
|
|
|
if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
|
|
|
|
|
signaling_responses[signaling_responses_pending].handle = handle;
|
|
|
|
|
signaling_responses[signaling_responses_pending].code = code;
|
|
|
|
|
signaling_responses[signaling_responses_pending].sig_id = sig_id;
|
2017-02-02 12:16:04 +01:00
|
|
|
|
signaling_responses[signaling_responses_pending].cid = cid;
|
2011-07-25 19:51:44 +00:00
|
|
|
|
signaling_responses[signaling_responses_pending].data = data;
|
|
|
|
|
signaling_responses_pending++;
|
|
|
|
|
l2cap_run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
|
|
|
|
static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
|
|
|
|
|
channel->remote_sig_id = identifier;
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
|
|
|
|
|
l2cap_run();
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-04 23:03:40 +00:00
|
|
|
|
static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
|
2010-01-25 22:22:55 +00:00
|
|
|
|
|
2014-08-15 21:26:50 +00:00
|
|
|
|
// log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
|
2010-01-25 22:22:55 +00:00
|
|
|
|
l2cap_service_t *service = l2cap_get_service(psm);
|
|
|
|
|
if (!service) {
|
|
|
|
|
// 0x0002 PSM not supported
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, 0x0002);
|
2010-01-25 22:22:55 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-01 21:50:25 +00:00
|
|
|
|
hci_connection_t * hci_connection = hci_connection_for_handle( handle );
|
2010-01-25 22:22:55 +00:00
|
|
|
|
if (!hci_connection) {
|
2011-07-25 19:51:44 +00:00
|
|
|
|
//
|
2014-08-15 21:26:50 +00:00
|
|
|
|
log_error("no hci_connection for handle %u", handle);
|
2010-01-25 22:22:55 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2014-01-16 22:42:21 +00:00
|
|
|
|
|
2010-01-25 22:22:55 +00:00
|
|
|
|
// alloc structure
|
2014-08-15 21:26:50 +00:00
|
|
|
|
// log_info("l2cap_handle_connection_request register channel");
|
2018-02-02 15:18:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, L2CAP_CHANNEL_TYPE_CLASSIC, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
|
2016-07-07 17:01:49 +02:00
|
|
|
|
psm, service->mtu, service->required_security_level);
|
2011-07-25 19:51:44 +00:00
|
|
|
|
if (!channel){
|
|
|
|
|
// 0x0004 No resources available
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, 0x0004);
|
2011-07-25 19:51:44 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-07-07 17:01:49 +02:00
|
|
|
|
|
2016-02-19 13:57:49 +01:00
|
|
|
|
channel->con_handle = handle;
|
2010-03-04 23:03:40 +00:00
|
|
|
|
channel->remote_cid = source_cid;
|
2011-06-20 21:15:46 +00:00
|
|
|
|
channel->remote_sig_id = sig_id;
|
2011-06-18 11:03:00 +00:00
|
|
|
|
|
2014-10-31 15:46:38 +00:00
|
|
|
|
// limit local mtu to max acl packet length - l2cap header
|
2011-07-30 16:02:57 +00:00
|
|
|
|
if (channel->local_mtu > l2cap_max_mtu()) {
|
|
|
|
|
channel->local_mtu = l2cap_max_mtu();
|
2010-09-03 20:17:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-25 22:22:55 +00:00
|
|
|
|
// set initial state
|
2016-10-23 22:08:44 +02:00
|
|
|
|
channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
|
2016-10-23 22:11:16 +02:00
|
|
|
|
channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND | L2CAP_CHANNEL_STATE_VAR_INCOMING);
|
2010-01-26 20:49:02 +00:00
|
|
|
|
|
2010-01-25 22:22:55 +00:00
|
|
|
|
// add to connections list
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2014-01-16 20:11:45 +00:00
|
|
|
|
|
2014-01-16 23:15:21 +00:00
|
|
|
|
// assert security requirements
|
2014-01-17 11:33:22 +00:00
|
|
|
|
gap_request_security_level(handle, channel->required_security_level);
|
2010-01-26 20:49:02 +00:00
|
|
|
|
}
|
2010-01-25 22:22:55 +00:00
|
|
|
|
|
2016-01-21 12:03:39 +01:00
|
|
|
|
void l2cap_accept_connection(uint16_t local_cid){
|
2012-07-30 18:58:08 +00:00
|
|
|
|
log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
|
2010-03-04 23:03:40 +00:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2010-01-26 20:49:02 +00:00
|
|
|
|
if (!channel) {
|
2016-01-21 12:03:39 +01:00
|
|
|
|
log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
|
2010-01-26 20:49:02 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 12:18:35 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// configure L2CAP Basic mode
|
|
|
|
|
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-06-20 21:03:40 +00:00
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
|
2010-01-25 22:22:55 +00:00
|
|
|
|
|
2011-06-20 21:03:40 +00:00
|
|
|
|
// process
|
|
|
|
|
l2cap_run();
|
2010-01-26 20:49:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-17 22:49:15 +02:00
|
|
|
|
void l2cap_decline_connection(uint16_t local_cid){
|
|
|
|
|
log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
|
2010-03-04 23:03:40 +00:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
|
2010-01-26 20:49:02 +00:00
|
|
|
|
if (!channel) {
|
2016-01-21 12:03:39 +01:00
|
|
|
|
log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
|
2010-01-26 20:49:02 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2011-06-20 19:58:46 +00:00
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
|
2016-07-17 22:49:15 +02:00
|
|
|
|
channel->reason = 0x04; // no resources available
|
2011-06-20 19:58:46 +00:00
|
|
|
|
l2cap_run();
|
2010-01-25 22:22:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 12:20:26 +02:00
|
|
|
|
// @pre command len is valid, see check in l2cap_signaling_handler_channel
|
2015-11-06 19:43:35 +01:00
|
|
|
|
static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
|
2011-06-20 21:15:46 +00:00
|
|
|
|
|
2017-12-13 17:33:52 +01:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
uint8_t use_fcs = 1;
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-06-20 21:15:46 +00:00
|
|
|
|
channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
|
|
|
|
|
|
2016-01-31 00:07:32 +01:00
|
|
|
|
uint16_t flags = little_endian_read_16(command, 6);
|
2012-10-30 19:55:24 +00:00
|
|
|
|
if (flags & 1) {
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-18 20:00:05 +00:00
|
|
|
|
// accept the other's configuration options
|
2016-01-31 00:07:32 +01:00
|
|
|
|
uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
|
2010-06-18 21:36:14 +00:00
|
|
|
|
uint16_t pos = 8;
|
|
|
|
|
while (pos < end_pos){
|
2012-10-30 19:55:24 +00:00
|
|
|
|
uint8_t option_hint = command[pos] >> 7;
|
|
|
|
|
uint8_t option_type = command[pos] & 0x7f;
|
2017-07-21 15:36:26 +02:00
|
|
|
|
// log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
|
2012-10-30 19:55:24 +00:00
|
|
|
|
pos++;
|
2010-06-18 20:21:52 +00:00
|
|
|
|
uint8_t length = command[pos++];
|
|
|
|
|
// MTU { type(8): 1, len(8):2, MTU(16) }
|
2017-07-25 14:22:40 +02:00
|
|
|
|
if (option_type == L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT && length == 2){
|
2016-01-31 00:07:32 +01:00
|
|
|
|
channel->remote_mtu = little_endian_read_16(command, pos);
|
2017-07-21 15:36:26 +02:00
|
|
|
|
log_info("Remote MTU %u", channel->remote_mtu);
|
2017-07-03 13:48:43 +02:00
|
|
|
|
if (channel->remote_mtu > l2cap_max_mtu()){
|
|
|
|
|
log_info("Remote MTU %u larger than outgoing buffer, only using MTU = %u", channel->remote_mtu, l2cap_max_mtu());
|
|
|
|
|
channel->remote_mtu = l2cap_max_mtu();
|
|
|
|
|
}
|
2012-10-30 19:55:24 +00:00
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
|
|
|
|
|
}
|
2014-01-10 17:32:29 +00:00
|
|
|
|
// Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
|
2017-07-25 14:22:40 +02:00
|
|
|
|
if (option_type == L2CAP_CONFIG_OPTION_TYPE_FLUSH_TIMEOUT && length == 2){
|
2016-01-31 00:07:32 +01:00
|
|
|
|
channel->flush_timeout = little_endian_read_16(command, pos);
|
2017-07-21 15:36:26 +02:00
|
|
|
|
log_info("Flush timeout: %u ms", channel->flush_timeout);
|
2014-01-10 17:32:29 +00:00
|
|
|
|
}
|
2017-07-12 12:56:01 +02:00
|
|
|
|
|
2017-07-11 15:34:04 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// Retransmission and Flow Control Option
|
2017-07-25 14:22:40 +02:00
|
|
|
|
if (option_type == L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL && length == 9){
|
2017-07-12 21:52:48 +02:00
|
|
|
|
l2cap_channel_mode_t mode = (l2cap_channel_mode_t) command[pos];
|
|
|
|
|
switch(channel->mode){
|
|
|
|
|
case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION:
|
2017-07-16 21:56:34 +02:00
|
|
|
|
// Store remote config
|
|
|
|
|
channel->remote_tx_window_size = command[pos+1];
|
|
|
|
|
channel->remote_max_transmit = command[pos+2];
|
|
|
|
|
channel->remote_retransmission_timeout_ms = little_endian_read_16(command, pos + 3);
|
|
|
|
|
channel->remote_monitor_timeout_ms = little_endian_read_16(command, pos + 5);
|
2017-07-21 15:36:26 +02:00
|
|
|
|
channel->remote_mps = little_endian_read_16(command, pos + 7);
|
|
|
|
|
log_info("FC&C config: tx window: %u, max transmit %u, retrans timeout %u, monitor timeout %u, mps %u",
|
2017-07-16 21:56:34 +02:00
|
|
|
|
channel->remote_tx_window_size,
|
|
|
|
|
channel->remote_max_transmit,
|
|
|
|
|
channel->remote_retransmission_timeout_ms,
|
2017-07-21 15:36:26 +02:00
|
|
|
|
channel->remote_monitor_timeout_ms,
|
|
|
|
|
channel->remote_mps);
|
2017-07-16 21:56:34 +02:00
|
|
|
|
// If ERTM mandatory, but remote doens't offer ERTM -> disconnect
|
|
|
|
|
if (channel->ertm_mandatory && mode != L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
|
2017-07-12 21:52:48 +02:00
|
|
|
|
} else {
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case L2CAP_CHANNEL_MODE_BASIC:
|
|
|
|
|
switch (mode){
|
|
|
|
|
case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION:
|
|
|
|
|
// remote asks for ERTM, but we want basic mode. disconnect if this happens a second time
|
|
|
|
|
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED){
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
|
|
|
|
|
}
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED);
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED);
|
|
|
|
|
break;
|
|
|
|
|
default: // case L2CAP_CHANNEL_MODE_BASIC:
|
|
|
|
|
// TODO store and evaluate configuration
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2017-07-12 11:59:22 +02:00
|
|
|
|
}
|
2017-11-10 16:03:53 +01:00
|
|
|
|
}
|
|
|
|
|
if (option_type == L2CAP_CONFIG_OPTION_TYPE_FRAME_CHECK_SEQUENCE && length == 1){
|
2017-12-13 17:33:52 +01:00
|
|
|
|
use_fcs = command[pos];
|
2017-07-11 15:34:04 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2012-10-30 19:55:24 +00:00
|
|
|
|
// check for unknown options
|
2017-07-25 14:22:40 +02:00
|
|
|
|
if (option_hint == 0 && (option_type < L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT || option_type > L2CAP_CONFIG_OPTION_TYPE_EXTENDED_WINDOW_SIZE)){
|
2013-02-19 19:47:54 +00:00
|
|
|
|
log_info("l2cap cid %u, unknown options", channel->local_cid);
|
2012-10-30 19:55:24 +00:00
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
|
2010-06-18 20:21:52 +00:00
|
|
|
|
}
|
|
|
|
|
pos += length;
|
|
|
|
|
}
|
2017-12-13 17:33:52 +01:00
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
// "FCS" has precedence over "No FCS"
|
|
|
|
|
uint8_t update = channel->fcs_option || use_fcs;
|
|
|
|
|
log_info("local fcs: %u, remote fcs: %u -> %u", channel->fcs_option, use_fcs, update);
|
|
|
|
|
channel->fcs_option = update;
|
|
|
|
|
#endif
|
2010-06-18 20:00:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 12:20:26 +02:00
|
|
|
|
// @pre command len is valid, see check in l2cap_signaling_handler_channel
|
2017-07-12 12:56:01 +02:00
|
|
|
|
static void l2cap_signaling_handle_configure_response(l2cap_channel_t *channel, uint8_t result, uint8_t *command){
|
|
|
|
|
log_info("l2cap_signaling_handle_configure_response");
|
2017-07-12 11:59:22 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
|
2017-07-12 12:56:01 +02:00
|
|
|
|
uint16_t pos = 10;
|
2017-07-12 11:59:22 +02:00
|
|
|
|
while (pos < end_pos){
|
|
|
|
|
uint8_t option_hint = command[pos] >> 7;
|
|
|
|
|
uint8_t option_type = command[pos] & 0x7f;
|
2017-12-13 17:33:52 +01:00
|
|
|
|
// log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
|
2017-07-12 11:59:22 +02:00
|
|
|
|
pos++;
|
|
|
|
|
uint8_t length = command[pos++];
|
|
|
|
|
|
|
|
|
|
// Retransmission and Flow Control Option
|
2017-07-25 14:22:40 +02:00
|
|
|
|
if (option_type == L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL && length == 9){
|
2017-07-12 21:52:48 +02:00
|
|
|
|
switch (channel->mode){
|
|
|
|
|
case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION:
|
|
|
|
|
if (channel->ertm_mandatory){
|
|
|
|
|
// ??
|
|
|
|
|
} else {
|
|
|
|
|
// On 'Reject - Unacceptable Parameters' to our optional ERTM request, fall back to BASIC mode
|
|
|
|
|
if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS){
|
2019-01-07 21:47:59 +01:00
|
|
|
|
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
|
2017-07-12 21:52:48 +02:00
|
|
|
|
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
|
|
|
|
|
}
|
2017-07-12 12:56:01 +02:00
|
|
|
|
}
|
2017-07-12 21:52:48 +02:00
|
|
|
|
break;
|
|
|
|
|
case L2CAP_CHANNEL_MODE_BASIC:
|
|
|
|
|
if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS){
|
|
|
|
|
// On 'Reject - Unacceptable Parameters' to our Basic mode request, disconnect
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2017-07-12 11:59:22 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check for unknown options
|
2017-07-25 14:22:40 +02:00
|
|
|
|
if (option_hint == 0 && (option_type < L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT || option_type > L2CAP_CONFIG_OPTION_TYPE_EXTENDED_WINDOW_SIZE)){
|
2017-07-12 11:59:22 +02:00
|
|
|
|
log_info("l2cap cid %u, unknown options", channel->local_cid);
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pos += length;
|
|
|
|
|
}
|
2017-07-25 15:25:58 +02:00
|
|
|
|
#else
|
2017-09-18 14:34:31 +02:00
|
|
|
|
UNUSED(channel); // ok: no code
|
|
|
|
|
UNUSED(result); // ok: no code
|
|
|
|
|
UNUSED(command); // ok: no code
|
2017-07-12 11:59:22 +02:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-08 16:30:00 +00:00
|
|
|
|
static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
|
2014-08-15 21:26:50 +00:00
|
|
|
|
// log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
|
2011-07-24 08:49:09 +00:00
|
|
|
|
if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
|
|
|
|
|
if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
|
2015-07-12 00:00:31 +02:00
|
|
|
|
// addition check that fixes re-entrance issue causing l2cap event channel opened twice
|
|
|
|
|
if (channel->state == L2CAP_STATE_OPEN) return 0;
|
2011-07-08 16:30:00 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-09-18 12:20:26 +02:00
|
|
|
|
// @pre command len is valid, see check in l2cap_signaling_handler_dispatch
|
2015-11-06 19:43:35 +01:00
|
|
|
|
static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
|
2010-06-08 18:45:45 +00:00
|
|
|
|
|
2010-08-10 19:29:16 +00:00
|
|
|
|
uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
|
|
|
|
|
uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
|
2017-09-18 12:20:26 +02:00
|
|
|
|
uint16_t cmd_len = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
|
2009-10-23 16:39:31 +00:00
|
|
|
|
uint16_t result = 0;
|
2010-08-10 19:29:16 +00:00
|
|
|
|
|
2014-08-15 21:26:50 +00:00
|
|
|
|
log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
|
2009-07-31 21:41:15 +00:00
|
|
|
|
|
2011-06-21 17:23:03 +00:00
|
|
|
|
// handle DISCONNECT REQUESTS seperately
|
|
|
|
|
if (code == DISCONNECTION_REQUEST){
|
|
|
|
|
switch (channel->state){
|
2011-07-08 16:30:00 +00:00
|
|
|
|
case L2CAP_STATE_CONFIG:
|
2011-06-21 17:23:03 +00:00
|
|
|
|
case L2CAP_STATE_OPEN:
|
2011-06-21 17:51:29 +00:00
|
|
|
|
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
|
2011-06-21 17:23:03 +00:00
|
|
|
|
case L2CAP_STATE_WAIT_DISCONNECT:
|
|
|
|
|
l2cap_handle_disconnect_request(channel, identifier);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// ignore in other states
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-24 19:58:25 +00:00
|
|
|
|
// @STATEMACHINE(l2cap)
|
2009-07-31 21:41:15 +00:00
|
|
|
|
switch (channel->state) {
|
|
|
|
|
|
|
|
|
|
case L2CAP_STATE_WAIT_CONNECT_RSP:
|
|
|
|
|
switch (code){
|
|
|
|
|
case CONNECTION_RESPONSE:
|
2017-09-18 12:20:26 +02:00
|
|
|
|
if (cmd_len < 8){
|
|
|
|
|
// command imcomplete
|
|
|
|
|
l2cap_register_signaling_response(channel->con_handle, COMMAND_REJECT, identifier, 0, L2CAP_REJ_CMD_UNKNOWN);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-01-10 15:42:59 +00:00
|
|
|
|
l2cap_stop_rtx(channel);
|
2016-01-31 00:07:32 +01:00
|
|
|
|
result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
|
2009-10-23 16:39:31 +00:00
|
|
|
|
switch (result) {
|
|
|
|
|
case 0:
|
2010-01-26 18:28:21 +00:00
|
|
|
|
// successful connection
|
2016-01-31 00:07:32 +01:00
|
|
|
|
channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
|
2011-07-08 16:30:00 +00:00
|
|
|
|
channel->state = L2CAP_STATE_CONFIG;
|
2012-01-05 22:24:51 +00:00
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
|
2009-10-23 16:39:31 +00:00
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2014-01-10 15:42:59 +00:00
|
|
|
|
// connection pending. get some coffee, but start the ERTX
|
|
|
|
|
l2cap_start_ertx(channel);
|
2009-10-23 16:39:31 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2010-08-30 19:06:13 +00:00
|
|
|
|
// channel closed
|
|
|
|
|
channel->state = L2CAP_STATE_CLOSED;
|
2009-10-23 16:39:31 +00:00
|
|
|
|
// map l2cap connection response result to BTstack status enumeration
|
2019-01-07 21:47:59 +01:00
|
|
|
|
l2cap_handle_channel_open_failed(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
|
2010-08-30 19:06:13 +00:00
|
|
|
|
|
|
|
|
|
// drop link key if security block
|
|
|
|
|
if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
|
2016-02-18 17:12:57 +01:00
|
|
|
|
gap_drop_link_key_for_bd_addr(channel->address);
|
2010-08-30 19:06:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// discard channel
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2009-10-23 16:39:31 +00:00
|
|
|
|
break;
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2009-12-02 20:07:41 +00:00
|
|
|
|
|
2009-10-23 16:39:31 +00:00
|
|
|
|
default:
|
2009-07-31 21:41:15 +00:00
|
|
|
|
//@TODO: implement other signaling packets
|
2009-10-23 16:39:31 +00:00
|
|
|
|
break;
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2009-11-28 15:16:29 +00:00
|
|
|
|
|
2011-07-08 16:30:00 +00:00
|
|
|
|
case L2CAP_STATE_CONFIG:
|
2011-06-20 20:23:13 +00:00
|
|
|
|
switch (code) {
|
|
|
|
|
case CONFIGURE_REQUEST:
|
2017-09-18 12:20:26 +02:00
|
|
|
|
if (cmd_len < 4){
|
|
|
|
|
// command incomplete
|
|
|
|
|
l2cap_register_signaling_response(channel->con_handle, COMMAND_REJECT, identifier, 0, L2CAP_REJ_CMD_UNKNOWN);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-01-05 22:24:51 +00:00
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
|
2011-06-20 20:23:13 +00:00
|
|
|
|
l2cap_signaling_handle_configure_request(channel, command);
|
2012-10-30 19:55:24 +00:00
|
|
|
|
if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
|
|
|
|
|
// only done if continuation not set
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
|
|
|
|
|
}
|
2011-06-20 20:23:13 +00:00
|
|
|
|
break;
|
2009-07-31 21:41:15 +00:00
|
|
|
|
case CONFIGURE_RESPONSE:
|
2017-09-18 12:20:26 +02:00
|
|
|
|
if (cmd_len < 6){
|
|
|
|
|
// command incomplete
|
|
|
|
|
l2cap_register_signaling_response(channel->con_handle, COMMAND_REJECT, identifier, 0, L2CAP_REJ_CMD_UNKNOWN);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
l2cap_stop_rtx(channel);
|
2017-07-12 12:56:01 +02:00
|
|
|
|
l2cap_signaling_handle_configure_response(channel, result, command);
|
2014-01-10 15:42:59 +00:00
|
|
|
|
switch (result){
|
|
|
|
|
case 0: // success
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
|
|
|
|
|
break;
|
|
|
|
|
case 4: // pending
|
|
|
|
|
l2cap_start_ertx(channel);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2017-07-12 11:43:46 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION && channel->ertm_mandatory){
|
|
|
|
|
// remote does not offer ertm but it's required
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2014-01-10 15:42:59 +00:00
|
|
|
|
// retry on negative result
|
|
|
|
|
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
|
|
|
|
|
break;
|
2014-01-08 10:24:16 +00:00
|
|
|
|
}
|
2009-11-28 15:16:29 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
2011-07-08 16:30:00 +00:00
|
|
|
|
if (l2cap_channel_ready_for_open(channel)){
|
|
|
|
|
// for open:
|
|
|
|
|
channel->state = L2CAP_STATE_OPEN;
|
|
|
|
|
l2cap_emit_channel_opened(channel, 0);
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2009-08-08 21:29:38 +00:00
|
|
|
|
|
|
|
|
|
case L2CAP_STATE_WAIT_DISCONNECT:
|
|
|
|
|
switch (code) {
|
|
|
|
|
case DISCONNECTION_RESPONSE:
|
2009-08-08 21:55:55 +00:00
|
|
|
|
l2cap_finialize_channel_close(channel);
|
2009-08-08 21:29:38 +00:00
|
|
|
|
break;
|
2009-11-28 15:16:29 +00:00
|
|
|
|
default:
|
|
|
|
|
//@TODO: implement other signaling packets
|
|
|
|
|
break;
|
2009-08-08 21:29:38 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2009-12-02 20:07:41 +00:00
|
|
|
|
|
|
|
|
|
case L2CAP_STATE_CLOSED:
|
|
|
|
|
// @TODO handle incoming requests
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case L2CAP_STATE_OPEN:
|
2011-06-21 17:23:03 +00:00
|
|
|
|
//@TODO: implement other signaling packets, e.g. re-configure
|
2009-11-28 15:16:29 +00:00
|
|
|
|
break;
|
2010-07-04 16:25:56 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
2014-08-15 21:26:50 +00:00
|
|
|
|
// log_info("new state %u", channel->state);
|
2009-07-31 21:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-06-08 18:45:45 +00:00
|
|
|
|
|
2017-09-18 11:32:36 +02:00
|
|
|
|
// @pre command len is valid, see check in l2cap_acl_classic_handler
|
|
|
|
|
static void l2cap_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command){
|
2010-06-08 18:45:45 +00:00
|
|
|
|
|
2017-07-11 22:20:52 +02:00
|
|
|
|
btstack_linked_list_iterator_t it;
|
|
|
|
|
|
2010-06-08 18:45:45 +00:00
|
|
|
|
// get code, signalind identifier and command len
|
2017-09-18 11:48:12 +02:00
|
|
|
|
uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
|
|
|
|
|
uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
|
|
|
|
|
uint16_t cmd_len = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
|
|
|
|
|
|
2017-07-11 22:20:52 +02:00
|
|
|
|
// not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_RESPONSE
|
|
|
|
|
if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_RESPONSE){
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN);
|
2010-06-08 18:45:45 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// general commands without an assigned channel
|
|
|
|
|
switch(code) {
|
|
|
|
|
|
2017-09-18 11:48:12 +02:00
|
|
|
|
case CONNECTION_REQUEST:
|
|
|
|
|
if (cmd_len == 4){
|
|
|
|
|
uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
|
|
|
|
|
uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
|
|
|
|
|
l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
|
|
|
|
|
} else {
|
|
|
|
|
l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN);
|
|
|
|
|
}
|
2011-06-21 17:51:29 +00:00
|
|
|
|
return;
|
2010-06-08 18:45:45 +00:00
|
|
|
|
|
2011-07-25 19:51:44 +00:00
|
|
|
|
case ECHO_REQUEST:
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, code, sig_id, 0, 0);
|
2011-06-21 17:51:29 +00:00
|
|
|
|
return;
|
2010-06-08 18:45:45 +00:00
|
|
|
|
|
2017-09-18 11:48:12 +02:00
|
|
|
|
case INFORMATION_REQUEST:
|
|
|
|
|
if (cmd_len == 2) {
|
|
|
|
|
uint16_t info_type = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
|
|
|
|
|
l2cap_register_signaling_response(handle, code, sig_id, 0, info_type);
|
|
|
|
|
} else {
|
|
|
|
|
l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN);
|
|
|
|
|
}
|
2011-06-21 17:51:29 +00:00
|
|
|
|
return;
|
2017-07-11 22:20:52 +02:00
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
case INFORMATION_RESPONSE: {
|
|
|
|
|
hci_connection_t * connection = hci_connection_for_handle(handle);
|
|
|
|
|
if (!connection) return;
|
2019-08-08 12:24:41 +02:00
|
|
|
|
if (connection->l2cap_state.information_state != L2CAP_INFORMATION_STATE_W4_EXTENDED_FEATURE_RESPONSE) return;
|
|
|
|
|
|
|
|
|
|
// get extended features from response if valid
|
|
|
|
|
connection->l2cap_state.extended_feature_mask = 0;
|
|
|
|
|
if (cmd_len >= 6) {
|
2017-09-18 11:48:12 +02:00
|
|
|
|
uint16_t info_type = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
|
2019-08-08 12:24:41 +02:00
|
|
|
|
uint16_t result = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
|
|
|
|
|
if (result == 0 && info_type == L2CAP_INFO_TYPE_EXTENDED_FEATURES_SUPPORTED) {
|
2017-09-18 11:48:12 +02:00
|
|
|
|
connection->l2cap_state.extended_feature_mask = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
|
2019-08-08 12:24:41 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_DONE;
|
|
|
|
|
log_info("extended features mask 0x%02x", connection->l2cap_state.extended_feature_mask);
|
|
|
|
|
|
|
|
|
|
// trigger connection request
|
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
|
|
|
|
if (channel->con_handle != handle) continue;
|
|
|
|
|
// bail if ERTM was requested but is not supported
|
|
|
|
|
if ((channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) && ((connection->l2cap_state.extended_feature_mask & 0x08) == 0)){
|
|
|
|
|
if (channel->ertm_mandatory){
|
|
|
|
|
// channel closed
|
|
|
|
|
channel->state = L2CAP_STATE_CLOSED;
|
|
|
|
|
// map l2cap connection response result to BTstack status enumeration
|
|
|
|
|
l2cap_handle_channel_open_failed(channel, L2CAP_CONNECTION_RESPONSE_RESULT_ERTM_NOT_SUPPORTED);
|
|
|
|
|
// discard channel
|
|
|
|
|
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
|
|
|
|
|
l2cap_free_channel_entry(channel);
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
// fallback to Basic mode
|
|
|
|
|
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
|
|
|
|
|
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
|
2017-07-12 13:06:18 +02:00
|
|
|
|
}
|
2019-08-08 12:24:41 +02:00
|
|
|
|
}
|
|
|
|
|
// start connecting
|
|
|
|
|
if (channel->state == L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES){
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
|
|
|
|
|
}
|
|
|
|
|
// respond to connection request
|
|
|
|
|
if (channel->state == L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES){
|
|
|
|
|
channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
|
|
|
|
|
l2cap_emit_incoming_connection(channel);
|
2017-07-13 12:12:03 +02:00
|
|
|
|
}
|
2017-07-11 22:20:52 +02:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-06-08 18:45:45 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get potential destination CID
|
2016-01-31 00:07:32 +01:00
|
|
|
|
uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
|
2010-06-08 18:45:45 +00:00
|
|
|
|
|
|
|
|
|
// Find channel for this sig_id and connection handle
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
|
2016-02-19 13:57:49 +01:00
|
|
|
|
if (channel->con_handle != handle) continue;
|
2014-07-31 21:18:43 +00:00
|
|
|
|
if (code & 1) {
|
|
|
|
|
// match odd commands (responses) by previous signaling identifier
|
|
|
|
|
if (channel->local_sig_id == sig_id) {
|
|
|
|
|
l2cap_signaling_handler_channel(channel, command);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// match even commands (requests) by local channel id
|
|
|
|
|
if (channel->local_cid == dest_cid) {
|
|
|
|
|
l2cap_signaling_handler_channel(channel, command);
|
|
|
|
|
break;
|
2010-06-08 18:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2010-06-08 18:45:45 +00:00
|
|
|
|
|
2016-07-27 16:44:25 +02:00
|
|
|
|
#ifdef ENABLE_BLE
|
2016-10-19 14:27:25 +02:00
|
|
|
|
|
|
|
|
|
static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
|
|
|
|
|
uint8_t event[6];
|
|
|
|
|
event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
|
|
|
|
|
event[1] = 4;
|
|
|
|
|
little_endian_store_16(event, 2, con_handle);
|
|
|
|
|
little_endian_store_16(event, 4, result);
|
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
|
if (!l2cap_event_packet_handler) return;
|
|
|
|
|
(*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 11:09:21 +02:00
|
|
|
|
// @returns valid
|
|
|
|
|
static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
|
2016-07-27 16:44:25 +02:00
|
|
|
|
hci_connection_t * connection;
|
2016-07-27 11:09:21 +02:00
|
|
|
|
uint16_t result;
|
2018-08-23 12:08:14 +02:00
|
|
|
|
uint8_t event[12];
|
2016-10-14 15:31:14 +02:00
|
|
|
|
|
2016-10-14 16:00:59 +02:00
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
2016-10-18 15:51:58 +02:00
|
|
|
|
btstack_linked_list_iterator_t it;
|
|
|
|
|
l2cap_channel_t * channel;
|
|
|
|
|
uint16_t local_cid;
|
2016-10-14 15:31:14 +02:00
|
|
|
|
uint16_t le_psm;
|
2016-10-06 16:09:54 +02:00
|
|
|
|
uint16_t new_credits;
|
|
|
|
|
uint16_t credits_before;
|
2016-07-27 16:44:25 +02:00
|
|
|
|
l2cap_service_t * service;
|
2017-04-04 11:24:24 +02:00
|
|
|
|
uint16_t source_cid;
|
2016-10-14 15:31:14 +02:00
|
|
|
|
#endif
|
2016-10-04 15:48:27 +02:00
|
|
|
|
|
|
|
|
|
uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
|
2017-09-18 14:07:01 +02:00
|
|
|
|
uint16_t len = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
|
2017-10-02 11:50:32 +02:00
|
|
|
|
log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u, len %u", code, sig_id, len);
|
2016-07-27 11:09:21 +02:00
|
|
|
|
|
2016-10-04 15:48:27 +02:00
|
|
|
|
switch (code){
|
2016-07-27 11:09:21 +02:00
|
|
|
|
|
|
|
|
|
case CONNECTION_PARAMETER_UPDATE_REQUEST:
|
2017-09-18 14:07:01 +02:00
|
|
|
|
// check size
|
2017-10-02 11:50:32 +02:00
|
|
|
|
if (len < 8) return 0;
|
2016-07-27 16:44:25 +02:00
|
|
|
|
connection = hci_connection_for_handle(handle);
|
2016-07-27 11:09:21 +02:00
|
|
|
|
if (connection){
|
|
|
|
|
if (connection->role != HCI_ROLE_MASTER){
|
|
|
|
|
// reject command without notifying upper layer when not in master role
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
le_connection_parameter_range_t existing_range;
|
|
|
|
|
gap_get_connection_parameter_range(&existing_range);
|
2018-01-17 12:05:42 +01:00
|
|
|
|
uint16_t le_conn_interval_min = little_endian_read_16(command,L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
|
|
|
|
|
uint16_t le_conn_interval_max = little_endian_read_16(command,L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
|
|
|
|
|
uint16_t le_conn_latency = little_endian_read_16(command,L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
|
|
|
|
|
uint16_t le_supervision_timeout = little_endian_read_16(command,L2CAP_SIGNALING_COMMAND_DATA_OFFSET+6);
|
2016-07-27 11:09:21 +02:00
|
|
|
|
|
2018-04-06 14:40:38 +02:00
|
|
|
|
int update_parameter = gap_connection_parameter_range_included(&existing_range, le_conn_interval_min, le_conn_interval_max, le_conn_latency, le_supervision_timeout);
|
2016-07-27 11:09:21 +02:00
|
|
|
|
if (update_parameter){
|
|
|
|
|
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
|
|
|
|
|
connection->le_conn_interval_min = le_conn_interval_min;
|
|
|
|
|
connection->le_conn_interval_max = le_conn_interval_max;
|
|
|
|
|
connection->le_conn_latency = le_conn_latency;
|
|
|
|
|
connection->le_supervision_timeout = le_supervision_timeout;
|
|
|
|
|
} else {
|
|
|
|
|
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
|
|
|
|
|
}
|
|
|
|
|
connection->le_con_param_update_identifier = sig_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!l2cap_event_packet_handler) break;
|
|
|
|
|
|
|
|
|
|
event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
|
|
|
|
|
event[1] = 8;
|
2018-08-23 12:08:14 +02:00
|
|
|
|
little_endian_store_16(event, 2, handle);
|
|
|
|
|
memcpy(&event[4], &command[4], 8);
|
2016-07-27 11:09:21 +02:00
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
|
(*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
|
break;
|
|
|
|
|
|
2017-10-02 11:50:32 +02:00
|
|
|
|
case CONNECTION_PARAMETER_UPDATE_RESPONSE:
|
|
|
|
|
// check size
|
|
|
|
|
if (len < 2) return 0;
|
|
|
|
|
result = little_endian_read_16(command, 4);
|
|
|
|
|
l2cap_emit_connection_parameter_update_response(handle, result);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 15:51:58 +02:00
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
|
|
|
|
|
2016-10-06 16:09:54 +02:00
|
|
|
|
case COMMAND_REJECT:
|
|
|
|
|
// Find channel for this sig_id and connection handle
|
|
|
|
|
channel = NULL;
|
2018-02-02 16:27:59 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
2016-10-06 16:09:54 +02:00
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(a_channel->channel_type)) continue;
|
2016-10-06 16:09:54 +02:00
|
|
|
|
if (a_channel->con_handle != handle) continue;
|
|
|
|
|
if (a_channel->local_sig_id != sig_id) continue;
|
|
|
|
|
channel = a_channel;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!channel) break;
|
|
|
|
|
|
|
|
|
|
// if received while waiting for le connection response, assume legacy device
|
|
|
|
|
if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE){
|
|
|
|
|
channel->state = L2CAP_STATE_CLOSED;
|
|
|
|
|
// no official value for this, use: Connection refused – LE_PSM not supported - 0x0002
|
2016-10-18 11:49:04 +02:00
|
|
|
|
l2cap_emit_le_channel_opened(channel, 0x0002);
|
2016-10-06 16:09:54 +02:00
|
|
|
|
|
|
|
|
|
// discard channel
|
2018-02-02 16:27:59 +01:00
|
|
|
|
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2016-10-06 16:09:54 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2016-07-27 16:44:25 +02:00
|
|
|
|
case LE_CREDIT_BASED_CONNECTION_REQUEST:
|
2017-09-18 14:07:01 +02:00
|
|
|
|
// check size
|
|
|
|
|
if (len < 10) return 0;
|
|
|
|
|
|
2016-07-27 16:44:25 +02:00
|
|
|
|
// get hci connection, bail if not found (must not happen)
|
|
|
|
|
connection = hci_connection_for_handle(handle);
|
|
|
|
|
if (!connection) return 0;
|
|
|
|
|
|
|
|
|
|
// check if service registered
|
|
|
|
|
le_psm = little_endian_read_16(command, 4);
|
|
|
|
|
service = l2cap_le_get_service(le_psm);
|
2017-04-04 11:24:24 +02:00
|
|
|
|
source_cid = little_endian_read_16(command, 6);
|
|
|
|
|
|
2016-07-27 16:44:25 +02:00
|
|
|
|
if (service){
|
|
|
|
|
if (source_cid < 0x40){
|
|
|
|
|
// 0x0009 Connection refused - Invalid Source CID
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0009);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// go through list of channels for this ACL connection and check if we get a match
|
2018-02-02 16:27:59 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
2016-10-04 15:48:27 +02:00
|
|
|
|
l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(a_channel->channel_type)) continue;
|
2016-10-04 15:48:27 +02:00
|
|
|
|
if (a_channel->con_handle != handle) continue;
|
|
|
|
|
if (a_channel->remote_cid != source_cid) continue;
|
2016-07-27 16:44:25 +02:00
|
|
|
|
// 0x000a Connection refused - Source CID already allocated
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x000a);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-14 15:31:14 +02:00
|
|
|
|
// security: check encryption
|
|
|
|
|
if (service->required_security_level >= LEVEL_2){
|
2018-01-19 16:08:14 +01:00
|
|
|
|
if (gap_encryption_key_size(handle) == 0){
|
2016-10-14 15:31:14 +02:00
|
|
|
|
// 0x0008 Connection refused - insufficient encryption
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0008);
|
2016-10-14 15:31:14 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
// anything less than 16 byte key size is insufficient
|
2018-01-19 16:08:14 +01:00
|
|
|
|
if (gap_encryption_key_size(handle) < 16){
|
2016-10-14 15:31:14 +02:00
|
|
|
|
// 0x0007 Connection refused – insufficient encryption key size
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0007);
|
2016-10-14 15:31:14 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// security: check authencation
|
|
|
|
|
if (service->required_security_level >= LEVEL_3){
|
2018-01-19 16:08:14 +01:00
|
|
|
|
if (!gap_authenticated(handle)){
|
2016-10-14 15:31:14 +02:00
|
|
|
|
// 0x0005 Connection refused – insufficient authentication
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0005);
|
2016-10-14 15:31:14 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// security: check authorization
|
|
|
|
|
if (service->required_security_level >= LEVEL_4){
|
2018-01-19 16:08:14 +01:00
|
|
|
|
if (gap_authorization_state(handle) != AUTHORIZATION_GRANTED){
|
2016-10-14 15:31:14 +02:00
|
|
|
|
// 0x0006 Connection refused – insufficient authorization
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0006);
|
2016-10-14 15:31:14 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-27 16:44:25 +02:00
|
|
|
|
|
|
|
|
|
// allocate channel
|
2018-02-02 15:18:59 +01:00
|
|
|
|
channel = l2cap_create_channel_entry(service->packet_handler, L2CAP_CHANNEL_TYPE_LE_DATA_CHANNEL, connection->address,
|
2016-07-27 16:44:25 +02:00
|
|
|
|
BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level);
|
|
|
|
|
if (!channel){
|
|
|
|
|
// 0x0004 Connection refused – no resources available
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0004);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
channel->con_handle = handle;
|
|
|
|
|
channel->remote_cid = source_cid;
|
|
|
|
|
channel->remote_sig_id = sig_id;
|
2016-10-05 15:49:18 +02:00
|
|
|
|
channel->remote_mtu = little_endian_read_16(command, 8);
|
|
|
|
|
channel->remote_mps = little_endian_read_16(command, 10);
|
|
|
|
|
channel->credits_outgoing = little_endian_read_16(command, 12);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
|
|
|
|
|
// set initial state
|
2016-10-18 15:24:09 +02:00
|
|
|
|
channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
|
|
|
|
|
channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING;
|
|
|
|
|
|
2016-07-27 16:44:25 +02:00
|
|
|
|
// add to connections list
|
2018-02-02 16:27:59 +01:00
|
|
|
|
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
|
|
|
|
|
// post connection request event
|
2016-10-18 11:49:04 +02:00
|
|
|
|
l2cap_emit_le_incoming_connection(channel);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// Connection refused – LE_PSM not supported
|
2017-02-02 12:16:04 +01:00
|
|
|
|
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0002);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
2016-10-04 15:48:27 +02:00
|
|
|
|
|
|
|
|
|
case LE_CREDIT_BASED_CONNECTION_RESPONSE:
|
2017-09-18 14:07:01 +02:00
|
|
|
|
// check size
|
|
|
|
|
if (len < 10) return 0;
|
|
|
|
|
|
2016-10-04 15:48:27 +02:00
|
|
|
|
// Find channel for this sig_id and connection handle
|
|
|
|
|
channel = NULL;
|
2018-02-02 16:27:59 +01:00
|
|
|
|
btstack_linked_list_iterator_init(&it, &l2cap_channels);
|
2016-10-04 15:48:27 +02:00
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
|
2018-02-02 17:52:13 +01:00
|
|
|
|
if (!l2cap_is_dynamic_channel_type(a_channel->channel_type)) continue;
|
2016-10-04 15:48:27 +02:00
|
|
|
|
if (a_channel->con_handle != handle) continue;
|
|
|
|
|
if (a_channel->local_sig_id != sig_id) continue;
|
|
|
|
|
channel = a_channel;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!channel) break;
|
|
|
|
|
|
|
|
|
|
// cid + 0
|
|
|
|
|
result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8);
|
|
|
|
|
if (result){
|
|
|
|
|
channel->state = L2CAP_STATE_CLOSED;
|
|
|
|
|
// map l2cap connection response result to BTstack status enumeration
|
2016-10-18 11:49:04 +02:00
|
|
|
|
l2cap_emit_le_channel_opened(channel, result);
|
2016-10-04 15:48:27 +02:00
|
|
|
|
|
|
|
|
|
// discard channel
|
2018-02-02 16:27:59 +01:00
|
|
|
|
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2016-10-04 15:48:27 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// success
|
|
|
|
|
channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
|
|
|
|
|
channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
|
|
|
|
|
channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
|
|
|
|
|
channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
|
|
|
|
|
channel->state = L2CAP_STATE_OPEN;
|
2016-10-18 11:49:04 +02:00
|
|
|
|
l2cap_emit_le_channel_opened(channel, result);
|
2016-10-04 15:48:27 +02:00
|
|
|
|
break;
|
|
|
|
|
|
2016-10-05 22:09:32 +02:00
|
|
|
|
case LE_FLOW_CONTROL_CREDIT:
|
2017-09-18 14:07:01 +02:00
|
|
|
|
// check size
|
|
|
|
|
if (len < 4) return 0;
|
|
|
|
|
|
2016-10-05 22:09:32 +02:00
|
|
|
|
// find channel
|
|
|
|
|
local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
|
2018-02-02 16:27:59 +01:00
|
|
|
|
channel = l2cap_get_channel_for_local_cid(local_cid);
|
2016-10-06 16:09:54 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error("l2cap: no channel for cid 0x%02x", local_cid);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
new_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
|
|
|
|
|
credits_before = channel->credits_outgoing;
|
|
|
|
|
channel->credits_outgoing += new_credits;
|
|
|
|
|
// check for credit overrun
|
|
|
|
|
if (credits_before > channel->credits_outgoing){
|
|
|
|
|
log_error("l2cap: new credits caused overrrun for cid 0x%02x, disconnecting", local_cid);
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
log_info("l2cap: %u credits for 0x%02x, now %u", new_credits, local_cid, channel->credits_outgoing);
|
2016-10-05 22:09:32 +02:00
|
|
|
|
break;
|
|
|
|
|
|
2016-10-05 11:58:27 +02:00
|
|
|
|
case DISCONNECTION_REQUEST:
|
2017-09-18 14:07:01 +02:00
|
|
|
|
|
|
|
|
|
// check size
|
|
|
|
|
if (len < 4) return 0;
|
|
|
|
|
|
2016-10-05 11:58:27 +02:00
|
|
|
|
// find channel
|
|
|
|
|
local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
|
2018-02-02 16:27:59 +01:00
|
|
|
|
channel = l2cap_get_channel_for_local_cid(local_cid);
|
2016-10-07 20:52:46 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error("l2cap: no channel for cid 0x%02x", local_cid);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-10-05 11:58:27 +02:00
|
|
|
|
channel->remote_sig_id = sig_id;
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 15:51:58 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2016-10-06 16:09:54 +02:00
|
|
|
|
case DISCONNECTION_RESPONSE:
|
|
|
|
|
break;
|
|
|
|
|
|
2016-07-27 11:09:21 +02:00
|
|
|
|
default:
|
|
|
|
|
// command unknown -> reject command
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2016-07-27 16:44:25 +02:00
|
|
|
|
#endif
|
2016-07-27 11:09:21 +02:00
|
|
|
|
|
2017-09-18 11:32:36 +02:00
|
|
|
|
static void l2cap_acl_classic_handler(hci_con_handle_t handle, uint8_t *packet, uint16_t size){
|
2017-09-17 22:08:29 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2016-10-19 14:27:25 +02:00
|
|
|
|
l2cap_channel_t * l2cap_channel;
|
2018-02-02 17:52:13 +01:00
|
|
|
|
l2cap_fixed_channel_t * l2cap_fixed_channel;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
|
2010-06-08 18:45:45 +00:00
|
|
|
|
uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
|
2012-01-29 17:10:28 +00:00
|
|
|
|
switch (channel_id) {
|
|
|
|
|
|
|
|
|
|
case L2CAP_CID_SIGNALING: {
|
2019-07-17 11:28:06 +02:00
|
|
|
|
uint32_t command_offset = 8;
|
|
|
|
|
while ((command_offset + L2CAP_SIGNALING_COMMAND_DATA_OFFSET) < size) {
|
2017-09-18 11:32:36 +02:00
|
|
|
|
// assert signaling command is fully inside packet
|
|
|
|
|
uint16_t data_len = little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
|
2019-07-17 11:28:06 +02:00
|
|
|
|
uint32_t next_command_offset = command_offset + L2CAP_SIGNALING_COMMAND_DATA_OFFSET + data_len;
|
2017-09-18 11:32:36 +02:00
|
|
|
|
if (next_command_offset > size){
|
|
|
|
|
log_error("l2cap signaling command len invalid -> drop");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// handle signaling command
|
2012-01-29 17:10:28 +00:00
|
|
|
|
l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
|
2017-09-18 11:32:36 +02:00
|
|
|
|
// go to next command
|
2019-07-17 11:28:06 +02:00
|
|
|
|
command_offset = next_command_offset;
|
2012-01-29 17:10:28 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-02-20 14:21:01 +00:00
|
|
|
|
case L2CAP_CID_CONNECTIONLESS_CHANNEL:
|
2018-02-02 17:52:13 +01:00
|
|
|
|
l2cap_fixed_channel = l2cap_fixed_channel_for_channel_id(L2CAP_CID_CONNECTIONLESS_CHANNEL);
|
|
|
|
|
if (!l2cap_fixed_channel) break;
|
|
|
|
|
if (!l2cap_fixed_channel->packet_handler) break;
|
|
|
|
|
(*l2cap_fixed_channel->packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
|
2015-02-20 14:21:01 +00:00
|
|
|
|
break;
|
2014-02-18 21:34:14 +00:00
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
default:
|
2012-01-29 17:10:28 +00:00
|
|
|
|
// Find channel for this channel_id and connection handle
|
2016-10-05 11:01:26 +02:00
|
|
|
|
l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
|
|
|
|
|
if (l2cap_channel) {
|
2017-07-13 14:21:57 +02:00
|
|
|
|
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
|
|
|
|
|
if (l2cap_channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
|
|
|
|
|
|
2017-11-10 16:03:53 +01:00
|
|
|
|
int fcs_size = l2cap_channel->fcs_option ? 2 : 0;
|
|
|
|
|
|
2017-10-09 11:12:53 +02:00
|
|
|
|
// assert control + FCS fields are inside
|
2017-11-10 16:03:53 +01:00
|
|
|
|
if (size < COMPLETE_L2CAP_HEADER+2+fcs_size) break;
|
|
|
|
|
|
|
|
|
|
if (l2cap_channel->fcs_option){
|
2017-12-13 17:33:52 +01:00
|
|
|
|
// verify FCS (required if one side requested it)
|
2017-11-10 16:03:53 +01:00
|
|
|
|
uint16_t fcs_calculated = crc16_calc(&packet[4], size - (4+2));
|
|
|
|
|
uint16_t fcs_packet = little_endian_read_16(packet, size-2);
|
2018-10-23 15:49:16 +02:00
|
|
|
|
|
|
|
|
|
#ifdef L2CAP_ERTM_SIMULATE_FCS_ERROR_INTERVAL
|
|
|
|
|
// simulate fcs error
|
|
|
|
|
static int counter = 0;
|
|
|
|
|
if (++counter == L2CAP_ERTM_SIMULATE_FCS_ERROR_INTERVAL) {
|
|
|
|
|
log_info("Simulate fcs error");
|
|
|
|
|
fcs_calculated++;
|
|
|
|
|
counter = 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-11-10 16:03:53 +01:00
|
|
|
|
if (fcs_calculated == fcs_packet){
|
|
|
|
|
log_info("Packet FCS 0x%04x verified", fcs_packet);
|
|
|
|
|
} else {
|
|
|
|
|
log_error("FCS mismatch! Packet 0x%04x, calculated 0x%04x", fcs_packet, fcs_calculated);
|
2018-10-23 16:30:27 +02:00
|
|
|
|
// ERTM State Machine in Bluetooth Spec does not handle 'I-Frame with invalid FCS'
|
2017-11-10 16:03:53 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-13 14:21:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// switch on packet type
|
|
|
|
|
uint16_t control = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER);
|
2017-07-16 18:33:01 +02:00
|
|
|
|
uint8_t req_seq = (control >> 8) & 0x3f;
|
2017-07-21 15:16:35 +02:00
|
|
|
|
int final = (control >> 7) & 0x01;
|
2017-07-13 14:21:57 +02:00
|
|
|
|
if (control & 1){
|
|
|
|
|
// S-Frame
|
2017-07-21 14:54:45 +02:00
|
|
|
|
int poll = (control >> 4) & 0x01;
|
2017-07-17 15:21:52 +02:00
|
|
|
|
l2cap_supervisory_function_t s = (l2cap_supervisory_function_t) ((control >> 2) & 0x03);
|
2017-07-18 11:40:46 +02:00
|
|
|
|
log_info("Control: 0x%04x => Supervisory function %u, ReqSeq %02u", control, (int) s, req_seq);
|
2017-07-18 16:21:14 +02:00
|
|
|
|
l2cap_ertm_tx_packet_state_t * tx_state;
|
2017-07-17 15:21:52 +02:00
|
|
|
|
switch (s){
|
|
|
|
|
case L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY:
|
2017-07-18 11:40:46 +02:00
|
|
|
|
log_info("L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY");
|
2017-07-24 17:51:03 +02:00
|
|
|
|
l2cap_ertm_process_req_seq(l2cap_channel, req_seq);
|
2017-07-21 14:54:45 +02:00
|
|
|
|
if (poll && final){
|
|
|
|
|
// S-frames shall not be transmitted with both the F-bit and the P-bit set to 1 at the same time.
|
|
|
|
|
log_error("P=F=1 in S-Frame");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-17 18:33:30 +02:00
|
|
|
|
if (poll){
|
2017-07-24 15:44:49 +02:00
|
|
|
|
// check if we did request selective retransmission before <==> we have stored SDU segments
|
|
|
|
|
int i;
|
|
|
|
|
int num_stored_out_of_order_packets = 0;
|
|
|
|
|
for (i=0;i<l2cap_channel->num_rx_buffers;i++){
|
|
|
|
|
int index = l2cap_channel->rx_store_index + i;
|
|
|
|
|
if (index >= l2cap_channel->num_rx_buffers){
|
|
|
|
|
index -= l2cap_channel->num_rx_buffers;
|
|
|
|
|
}
|
|
|
|
|
l2cap_ertm_rx_packet_state_t * rx_state = &l2cap_channel->rx_packets_state[index];
|
|
|
|
|
if (!rx_state->valid) continue;
|
|
|
|
|
num_stored_out_of_order_packets++;
|
|
|
|
|
}
|
|
|
|
|
if (num_stored_out_of_order_packets){
|
|
|
|
|
l2cap_channel->send_supervisor_frame_selective_reject = 1;
|
|
|
|
|
} else {
|
|
|
|
|
l2cap_channel->send_supervisor_frame_receiver_ready = 1;
|
|
|
|
|
}
|
2017-07-21 12:27:17 +02:00
|
|
|
|
l2cap_channel->set_final_bit_after_packet_with_poll_bit_set = 1;
|
2017-07-21 14:54:45 +02:00
|
|
|
|
}
|
|
|
|
|
if (final){
|
2017-07-24 18:10:22 +02:00
|
|
|
|
// Stop-MonitorTimer
|
|
|
|
|
l2cap_ertm_stop_monitor_timer(l2cap_channel);
|
|
|
|
|
// If UnackedFrames > 0 then Start-RetransTimer
|
2017-07-25 14:04:11 +02:00
|
|
|
|
if (l2cap_channel->unacked_frames){
|
2017-07-24 18:10:22 +02:00
|
|
|
|
l2cap_ertm_start_retransmission_timer(l2cap_channel);
|
|
|
|
|
}
|
2017-07-21 14:54:45 +02:00
|
|
|
|
// final bit set <- response to RR with poll bit set. All not acknowledged packets need to be retransmitted
|
2018-10-23 16:30:27 +02:00
|
|
|
|
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
|
2017-07-21 14:54:45 +02:00
|
|
|
|
}
|
2017-07-17 15:21:52 +02:00
|
|
|
|
break;
|
2017-07-18 11:40:46 +02:00
|
|
|
|
case L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT:
|
|
|
|
|
log_info("L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT");
|
2017-07-24 17:51:03 +02:00
|
|
|
|
l2cap_ertm_process_req_seq(l2cap_channel, req_seq);
|
2018-04-24 15:55:40 +02:00
|
|
|
|
// restart transmittion from last unacknowledted packet (earlier packets already freed in l2cap_ertm_process_req_seq)
|
2018-10-23 16:30:27 +02:00
|
|
|
|
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
|
2017-07-18 11:40:46 +02:00
|
|
|
|
break;
|
|
|
|
|
case L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY:
|
|
|
|
|
log_error("L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY");
|
|
|
|
|
break;
|
|
|
|
|
case L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT:
|
2017-07-18 16:21:14 +02:00
|
|
|
|
log_info("L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT");
|
|
|
|
|
if (poll){
|
2017-07-24 17:51:03 +02:00
|
|
|
|
l2cap_ertm_process_req_seq(l2cap_channel, req_seq);
|
2017-07-18 16:21:14 +02:00
|
|
|
|
}
|
|
|
|
|
// find requested i-frame
|
|
|
|
|
tx_state = l2cap_ertm_get_tx_state(l2cap_channel, req_seq);
|
|
|
|
|
if (tx_state){
|
|
|
|
|
log_info("Retransmission for tx_seq %u requested", req_seq);
|
2017-07-21 12:27:17 +02:00
|
|
|
|
l2cap_channel->set_final_bit_after_packet_with_poll_bit_set = poll;
|
2017-07-18 16:21:14 +02:00
|
|
|
|
tx_state->retransmission_requested = 1;
|
|
|
|
|
l2cap_channel->srej_active = 1;
|
|
|
|
|
}
|
2017-07-18 11:40:46 +02:00
|
|
|
|
break;
|
2017-07-17 15:21:52 +02:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-13 14:21:57 +02:00
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
// I-Frame
|
|
|
|
|
// get control
|
|
|
|
|
l2cap_segmentation_and_reassembly_t sar = (l2cap_segmentation_and_reassembly_t) (control >> 14);
|
2017-07-16 18:33:01 +02:00
|
|
|
|
uint8_t tx_seq = (control >> 1) & 0x3f;
|
|
|
|
|
log_info("Control: 0x%04x => SAR %u, ReqSeq %02u, R?, TxSeq %02u", control, (int) sar, req_seq, tx_seq);
|
2017-07-24 14:20:31 +02:00
|
|
|
|
log_info("SAR: pos %u", l2cap_channel->reassembly_pos);
|
2017-07-16 18:33:01 +02:00
|
|
|
|
log_info("State: expected_tx_seq %02u, req_seq %02u", l2cap_channel->expected_tx_seq, l2cap_channel->req_seq);
|
2017-07-24 17:51:03 +02:00
|
|
|
|
l2cap_ertm_process_req_seq(l2cap_channel, req_seq);
|
2017-07-21 15:04:37 +02:00
|
|
|
|
if (final){
|
|
|
|
|
// final bit set <- response to RR with poll bit set. All not acknowledged packets need to be retransmitted
|
2018-10-23 16:30:27 +02:00
|
|
|
|
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
|
2017-07-21 15:04:37 +02:00
|
|
|
|
}
|
2017-10-09 11:12:53 +02:00
|
|
|
|
|
|
|
|
|
// get SDU
|
2018-10-03 17:21:11 +02:00
|
|
|
|
const uint8_t * payload_data = &packet[COMPLETE_L2CAP_HEADER+2];
|
|
|
|
|
uint16_t payload_len = size-(COMPLETE_L2CAP_HEADER+2+fcs_size);
|
2017-10-09 11:12:53 +02:00
|
|
|
|
|
|
|
|
|
// assert SDU size is smaller or equal to our buffers
|
2018-10-03 17:21:11 +02:00
|
|
|
|
uint16_t max_payload_size = 0;
|
|
|
|
|
switch (sar){
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU:
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU:
|
|
|
|
|
// SDU Length + MPS
|
|
|
|
|
max_payload_size = l2cap_channel->local_mps + 2;
|
|
|
|
|
break;
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU:
|
|
|
|
|
case L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU:
|
|
|
|
|
max_payload_size = l2cap_channel->local_mps;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (payload_len > max_payload_size){
|
|
|
|
|
log_info("payload len %u > max payload %u -> drop packet", payload_len, max_payload_size);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-10-09 11:12:53 +02:00
|
|
|
|
|
2017-07-16 18:33:01 +02:00
|
|
|
|
// check ordering
|
|
|
|
|
if (l2cap_channel->expected_tx_seq == tx_seq){
|
|
|
|
|
log_info("Received expected frame with TxSeq == ExpectedTxSeq == %02u", tx_seq);
|
|
|
|
|
l2cap_channel->expected_tx_seq = l2cap_next_ertm_seq_nr(l2cap_channel->expected_tx_seq);
|
2017-07-24 15:44:49 +02:00
|
|
|
|
l2cap_channel->req_seq = l2cap_channel->expected_tx_seq;
|
|
|
|
|
|
2017-07-24 14:39:04 +02:00
|
|
|
|
// process SDU
|
2018-10-03 17:21:11 +02:00
|
|
|
|
l2cap_ertm_handle_in_sequence_sdu(l2cap_channel, sar, payload_data, payload_len);
|
2017-07-24 14:11:44 +02:00
|
|
|
|
|
2017-07-24 15:05:57 +02:00
|
|
|
|
// process stored segments
|
|
|
|
|
while (1){
|
|
|
|
|
int index = l2cap_channel->rx_store_index;
|
|
|
|
|
l2cap_ertm_rx_packet_state_t * rx_state = &l2cap_channel->rx_packets_state[index];
|
|
|
|
|
if (!rx_state->valid) break;
|
2017-07-24 15:44:49 +02:00
|
|
|
|
|
|
|
|
|
log_info("Processing stored frame with TxSeq == ExpectedTxSeq == %02u", l2cap_channel->expected_tx_seq);
|
|
|
|
|
l2cap_channel->expected_tx_seq = l2cap_next_ertm_seq_nr(l2cap_channel->expected_tx_seq);
|
|
|
|
|
l2cap_channel->req_seq = l2cap_channel->expected_tx_seq;
|
|
|
|
|
|
2017-07-24 15:05:57 +02:00
|
|
|
|
rx_state->valid = 0;
|
|
|
|
|
l2cap_ertm_handle_in_sequence_sdu(l2cap_channel, rx_state->sar, &l2cap_channel->rx_packets_data[index], rx_state->len);
|
2017-07-24 15:44:49 +02:00
|
|
|
|
|
|
|
|
|
// update rx store index
|
2017-07-24 15:05:57 +02:00
|
|
|
|
index++;
|
|
|
|
|
if (index >= l2cap_channel->num_rx_buffers){
|
|
|
|
|
index = 0;
|
|
|
|
|
}
|
|
|
|
|
l2cap_channel->rx_store_index = index;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 15:44:49 +02:00
|
|
|
|
//
|
|
|
|
|
l2cap_channel->send_supervisor_frame_receiver_ready = 1;
|
|
|
|
|
|
2017-07-18 17:11:33 +02:00
|
|
|
|
} else {
|
2017-07-18 17:26:26 +02:00
|
|
|
|
int delta = (tx_seq - l2cap_channel->expected_tx_seq) & 0x3f;
|
|
|
|
|
if (delta < 2){
|
2017-07-24 15:05:57 +02:00
|
|
|
|
// store segment
|
2018-10-03 17:21:11 +02:00
|
|
|
|
l2cap_ertm_handle_out_of_sequence_sdu(l2cap_channel, sar, delta, payload_data, payload_len);
|
2017-07-24 15:05:57 +02:00
|
|
|
|
|
2017-07-18 17:26:26 +02:00
|
|
|
|
log_info("Received unexpected frame TxSeq %u but expected %u -> send S-SREJ", tx_seq, l2cap_channel->expected_tx_seq);
|
|
|
|
|
l2cap_channel->send_supervisor_frame_selective_reject = 1;
|
|
|
|
|
} else {
|
|
|
|
|
log_info("Received unexpected frame TxSeq %u but expected %u -> send S-REJ", tx_seq, l2cap_channel->expected_tx_seq);
|
|
|
|
|
l2cap_channel->send_supervisor_frame_reject = 1;
|
|
|
|
|
}
|
2017-07-16 18:33:01 +02:00
|
|
|
|
}
|
2017-07-18 17:11:33 +02:00
|
|
|
|
}
|
2017-07-13 14:21:57 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-10-05 11:01:26 +02:00
|
|
|
|
l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
|
|
|
|
|
}
|
2017-09-17 22:08:29 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
UNUSED(handle); // ok: no code
|
|
|
|
|
UNUSED(packet); // ok: no code
|
|
|
|
|
UNUSED(size); // ok: no code
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 14:07:01 +02:00
|
|
|
|
static void l2cap_acl_le_handler(hci_con_handle_t handle, uint8_t *packet, uint16_t size){
|
2017-09-17 22:08:29 +02:00
|
|
|
|
#ifdef ENABLE_BLE
|
|
|
|
|
|
2018-02-02 17:52:13 +01:00
|
|
|
|
l2cap_fixed_channel_t * l2cap_fixed_channel;
|
|
|
|
|
|
2017-09-17 22:08:29 +02:00
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
|
|
|
|
l2cap_channel_t * l2cap_channel;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2017-09-17 22:08:29 +02:00
|
|
|
|
uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
|
|
|
|
|
switch (channel_id) {
|
|
|
|
|
|
|
|
|
|
case L2CAP_CID_SIGNALING_LE: {
|
|
|
|
|
uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
|
2017-09-18 14:07:01 +02:00
|
|
|
|
uint16_t len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER + 2);
|
|
|
|
|
if (COMPLETE_L2CAP_HEADER + 4 + len > size) break;
|
2017-09-17 22:08:29 +02:00
|
|
|
|
int valid = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
|
|
|
|
|
if (!valid){
|
|
|
|
|
l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case L2CAP_CID_ATTRIBUTE_PROTOCOL:
|
2018-02-02 17:52:13 +01:00
|
|
|
|
l2cap_fixed_channel = l2cap_fixed_channel_for_channel_id(L2CAP_CID_ATTRIBUTE_PROTOCOL);
|
|
|
|
|
if (!l2cap_fixed_channel) break;
|
|
|
|
|
if (!l2cap_fixed_channel->packet_handler) break;
|
|
|
|
|
(*l2cap_fixed_channel->packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
|
2017-09-17 22:08:29 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
|
2018-02-02 17:52:13 +01:00
|
|
|
|
l2cap_fixed_channel = l2cap_fixed_channel_for_channel_id(L2CAP_CID_SECURITY_MANAGER_PROTOCOL);
|
|
|
|
|
if (!l2cap_fixed_channel) break;
|
|
|
|
|
if (!l2cap_fixed_channel->packet_handler) break;
|
|
|
|
|
(*l2cap_fixed_channel->packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
|
2017-09-17 22:08:29 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
2016-10-18 15:51:58 +02:00
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
2018-02-02 16:27:59 +01:00
|
|
|
|
l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
|
2016-04-21 21:23:31 +02:00
|
|
|
|
if (l2cap_channel) {
|
2016-10-05 22:09:32 +02:00
|
|
|
|
// credit counting
|
|
|
|
|
if (l2cap_channel->credits_incoming == 0){
|
|
|
|
|
log_error("LE Data Channel packet received but no incoming credits");
|
|
|
|
|
l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
l2cap_channel->credits_incoming--;
|
|
|
|
|
|
|
|
|
|
// automatic credits
|
|
|
|
|
if (l2cap_channel->credits_incoming < L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK && l2cap_channel->automatic_credits){
|
|
|
|
|
l2cap_channel->new_credits_incoming = L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 16:33:46 +02:00
|
|
|
|
// first fragment
|
|
|
|
|
uint16_t pos = 0;
|
|
|
|
|
if (!l2cap_channel->receive_sdu_len){
|
2017-09-18 13:52:45 +02:00
|
|
|
|
uint16_t sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER);
|
|
|
|
|
if(sdu_len > l2cap_channel->local_mtu) break; // SDU would be larger than our buffer
|
|
|
|
|
l2cap_channel->receive_sdu_len = sdu_len;
|
2016-10-05 16:33:46 +02:00
|
|
|
|
l2cap_channel->receive_sdu_pos = 0;
|
|
|
|
|
pos += 2;
|
|
|
|
|
size -= 2;
|
|
|
|
|
}
|
2017-09-18 13:52:45 +02:00
|
|
|
|
uint16_t fragment_size = size-COMPLETE_L2CAP_HEADER;
|
|
|
|
|
uint16_t remaining_space = l2cap_channel->local_mtu - l2cap_channel->receive_sdu_pos;
|
|
|
|
|
if (fragment_size > remaining_space) break; // SDU would cause buffer overrun
|
|
|
|
|
memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], fragment_size);
|
2016-10-05 16:33:46 +02:00
|
|
|
|
l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER;
|
|
|
|
|
// done?
|
2018-01-17 14:01:43 +01:00
|
|
|
|
log_debug("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len);
|
2016-10-05 16:33:46 +02:00
|
|
|
|
if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){
|
|
|
|
|
l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len);
|
|
|
|
|
l2cap_channel->receive_sdu_len = 0;
|
|
|
|
|
}
|
2016-10-06 16:09:54 +02:00
|
|
|
|
} else {
|
|
|
|
|
log_error("LE Data Channel packet received but no channel found for cid 0x%02x", channel_id);
|
2012-01-29 17:10:28 +00:00
|
|
|
|
}
|
2016-10-05 11:01:26 +02:00
|
|
|
|
#endif
|
2012-01-29 17:10:28 +00:00
|
|
|
|
break;
|
2010-06-08 18:45:45 +00:00
|
|
|
|
}
|
2017-09-17 22:08:29 +02:00
|
|
|
|
#else
|
|
|
|
|
UNUSED(handle); // ok: no code
|
|
|
|
|
UNUSED(packet); // ok: no code
|
|
|
|
|
UNUSED(size); // ok: no code
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
|
|
|
|
UNUSED(packet_type); // ok: registered with hci_register_acl_packet_handler
|
|
|
|
|
UNUSED(channel); // ok: there is no channel
|
|
|
|
|
|
2017-09-18 14:07:01 +02:00
|
|
|
|
// Assert full L2CAP header present
|
|
|
|
|
if (size < COMPLETE_L2CAP_HEADER) return;
|
|
|
|
|
|
2017-09-17 22:08:29 +02:00
|
|
|
|
// Dispatch to Classic or LE handler
|
|
|
|
|
hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
|
|
|
|
|
hci_connection_t *conn = hci_connection_for_handle(handle);
|
|
|
|
|
if (!conn) return;
|
|
|
|
|
if (conn->address_type == BD_ADDR_TYPE_CLASSIC){
|
|
|
|
|
l2cap_acl_classic_handler(handle, packet, size);
|
|
|
|
|
} else {
|
|
|
|
|
l2cap_acl_le_handler(handle, packet, size);
|
|
|
|
|
}
|
2010-06-08 18:45:45 +00:00
|
|
|
|
|
2014-01-17 11:33:22 +00:00
|
|
|
|
l2cap_run();
|
2010-07-18 16:30:16 +00:00
|
|
|
|
}
|
2010-06-08 18:45:45 +00:00
|
|
|
|
|
2016-10-19 14:27:25 +02:00
|
|
|
|
// Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
|
|
|
|
|
void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
|
2018-02-02 17:52:13 +01:00
|
|
|
|
l2cap_fixed_channel_t * channel = l2cap_fixed_channel_for_channel_id(channel_id);
|
|
|
|
|
if (!channel) return;
|
|
|
|
|
channel->packet_handler = the_packet_handler;
|
2016-10-19 14:27:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2010-08-23 21:03:40 +00:00
|
|
|
|
// finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
|
2016-04-21 20:47:37 +02:00
|
|
|
|
void l2cap_finialize_channel_close(l2cap_channel_t * channel){
|
2009-08-08 21:55:55 +00:00
|
|
|
|
channel->state = L2CAP_STATE_CLOSED;
|
2019-01-07 21:47:59 +01:00
|
|
|
|
l2cap_handle_channel_closed(channel);
|
2009-08-08 21:55:55 +00:00
|
|
|
|
// discard channel
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2009-08-08 21:55:55 +00:00
|
|
|
|
}
|
2018-04-04 10:39:39 +02:00
|
|
|
|
#endif
|
2009-08-08 21:55:55 +00:00
|
|
|
|
|
2018-04-04 10:39:39 +02:00
|
|
|
|
#ifdef L2CAP_USES_CHANNELS
|
2016-01-20 15:38:37 +01:00
|
|
|
|
static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_iterator_t it;
|
|
|
|
|
btstack_linked_list_iterator_init(&it, services);
|
|
|
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
|
|
|
l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
|
2010-01-25 18:29:06 +00:00
|
|
|
|
if ( service->psm == psm){
|
|
|
|
|
return service;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2018-04-04 10:39:39 +02:00
|
|
|
|
#endif
|
2010-01-25 18:29:06 +00:00
|
|
|
|
|
2018-04-04 10:39:39 +02:00
|
|
|
|
#ifdef ENABLE_CLASSIC
|
2015-08-12 15:14:59 +02:00
|
|
|
|
static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
|
|
|
|
|
return l2cap_get_service_internal(&l2cap_services, psm);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 21:32:29 +01:00
|
|
|
|
uint8_t l2cap_register_service(btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){
|
2012-07-30 18:58:08 +00:00
|
|
|
|
|
2015-11-13 21:32:29 +01:00
|
|
|
|
log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
|
2012-07-30 18:58:08 +00:00
|
|
|
|
|
2011-07-25 20:47:30 +00:00
|
|
|
|
// check for alread registered psm
|
2010-01-25 18:29:06 +00:00
|
|
|
|
l2cap_service_t *service = l2cap_get_service(psm);
|
2011-07-25 19:55:47 +00:00
|
|
|
|
if (service) {
|
2015-11-13 21:32:29 +01:00
|
|
|
|
log_error("l2cap_register_service: PSM %u already registered", psm);
|
|
|
|
|
return L2CAP_SERVICE_ALREADY_REGISTERED;
|
2011-07-25 19:55:47 +00:00
|
|
|
|
}
|
2010-01-25 18:29:06 +00:00
|
|
|
|
|
2011-07-25 20:47:30 +00:00
|
|
|
|
// alloc structure
|
2014-09-04 20:35:14 +00:00
|
|
|
|
service = btstack_memory_l2cap_service_get();
|
2011-07-25 19:55:47 +00:00
|
|
|
|
if (!service) {
|
2015-11-13 21:32:29 +01:00
|
|
|
|
log_error("l2cap_register_service: no memory for l2cap_service_t");
|
|
|
|
|
return BTSTACK_MEMORY_ALLOC_FAILED;
|
2011-07-25 19:55:47 +00:00
|
|
|
|
}
|
2010-01-25 18:29:06 +00:00
|
|
|
|
|
|
|
|
|
// fill in
|
|
|
|
|
service->psm = psm;
|
|
|
|
|
service->mtu = mtu;
|
2015-11-06 20:53:12 +01:00
|
|
|
|
service->packet_handler = service_packet_handler;
|
2014-01-17 16:30:31 +00:00
|
|
|
|
service->required_security_level = security_level;
|
|
|
|
|
|
2010-01-25 18:29:06 +00:00
|
|
|
|
// add to services list
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
|
2011-10-31 18:59:34 +00:00
|
|
|
|
|
|
|
|
|
// enable page scan
|
2016-02-18 17:12:57 +01:00
|
|
|
|
gap_connectable_control(1);
|
2011-11-09 16:11:06 +00:00
|
|
|
|
|
2015-11-13 21:32:29 +01:00
|
|
|
|
return 0;
|
2010-01-25 18:29:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-17 22:43:06 +02:00
|
|
|
|
uint8_t l2cap_unregister_service(uint16_t psm){
|
2012-07-30 18:58:08 +00:00
|
|
|
|
|
|
|
|
|
log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
|
|
|
|
|
|
2010-01-25 18:29:06 +00:00
|
|
|
|
l2cap_service_t *service = l2cap_get_service(psm);
|
2016-07-17 22:43:06 +02:00
|
|
|
|
if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
|
2011-07-24 09:22:31 +00:00
|
|
|
|
btstack_memory_l2cap_service_free(service);
|
2011-10-31 18:59:34 +00:00
|
|
|
|
|
|
|
|
|
// disable page scan when no services registered
|
2016-07-17 22:43:06 +02:00
|
|
|
|
if (btstack_linked_list_empty(&l2cap_services)) {
|
|
|
|
|
gap_connectable_control(0);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2010-01-25 18:29:06 +00:00
|
|
|
|
}
|
2016-10-19 14:27:25 +02:00
|
|
|
|
#endif
|
2010-01-25 18:29:06 +00:00
|
|
|
|
|
2015-08-12 15:14:59 +02:00
|
|
|
|
|
2016-10-14 16:00:59 +02:00
|
|
|
|
#ifdef ENABLE_LE_DATA_CHANNELS
|
2016-10-14 15:31:14 +02:00
|
|
|
|
|
2016-10-18 12:27:22 +02:00
|
|
|
|
static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel){
|
|
|
|
|
if (!channel->waiting_for_can_send_now) return;
|
|
|
|
|
if (channel->send_sdu_buffer) return;
|
|
|
|
|
channel->waiting_for_can_send_now = 0;
|
2018-01-17 14:01:43 +01:00
|
|
|
|
log_debug("L2CAP_EVENT_CHANNEL_LE_CAN_SEND_NOW local_cid 0x%x", channel->local_cid);
|
2016-10-18 12:27:22 +02:00
|
|
|
|
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_CAN_SEND_NOW);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1BH2222
|
|
|
|
|
static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel) {
|
|
|
|
|
log_info("L2CAP_EVENT_LE_INCOMING_CONNECTION addr_type %u, addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x, remote_mtu %u",
|
|
|
|
|
channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu);
|
|
|
|
|
uint8_t event[19];
|
|
|
|
|
event[0] = L2CAP_EVENT_LE_INCOMING_CONNECTION;
|
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
|
event[2] = channel->address_type;
|
|
|
|
|
reverse_bd_addr(channel->address, &event[3]);
|
|
|
|
|
little_endian_store_16(event, 9, channel->con_handle);
|
|
|
|
|
little_endian_store_16(event, 11, channel->psm);
|
|
|
|
|
little_endian_store_16(event, 13, channel->local_cid);
|
|
|
|
|
little_endian_store_16(event, 15, channel->remote_cid);
|
|
|
|
|
little_endian_store_16(event, 17, channel->remote_mtu);
|
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
|
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
|
|
|
|
|
}
|
|
|
|
|
// 11BH22222
|
|
|
|
|
static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status) {
|
|
|
|
|
log_info("L2CAP_EVENT_LE_CHANNEL_OPENED status 0x%x addr_type %u addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u",
|
|
|
|
|
status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
|
|
|
|
|
channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
|
2016-10-18 15:24:09 +02:00
|
|
|
|
uint8_t event[23];
|
2016-10-18 12:27:22 +02:00
|
|
|
|
event[0] = L2CAP_EVENT_LE_CHANNEL_OPENED;
|
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
|
event[2] = status;
|
|
|
|
|
event[3] = channel->address_type;
|
|
|
|
|
reverse_bd_addr(channel->address, &event[4]);
|
|
|
|
|
little_endian_store_16(event, 10, channel->con_handle);
|
2016-10-18 15:24:09 +02:00
|
|
|
|
event[12] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0;
|
|
|
|
|
little_endian_store_16(event, 13, channel->psm);
|
|
|
|
|
little_endian_store_16(event, 15, channel->local_cid);
|
|
|
|
|
little_endian_store_16(event, 17, channel->remote_cid);
|
|
|
|
|
little_endian_store_16(event, 19, channel->local_mtu);
|
|
|
|
|
little_endian_store_16(event, 21, channel->remote_mtu);
|
2016-10-18 12:27:22 +02:00
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
|
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
|
|
|
|
|
}
|
2018-01-30 11:30:12 +01:00
|
|
|
|
// 2
|
|
|
|
|
static void l2cap_emit_le_channel_closed(l2cap_channel_t * channel){
|
|
|
|
|
log_info("L2CAP_EVENT_LE_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
|
|
|
|
|
uint8_t event[4];
|
|
|
|
|
event[0] = L2CAP_EVENT_LE_CHANNEL_CLOSED;
|
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
|
little_endian_store_16(event, 2, channel->local_cid);
|
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
|
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 12:27:22 +02:00
|
|
|
|
// finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
|
|
|
|
|
void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){
|
|
|
|
|
channel->state = L2CAP_STATE_CLOSED;
|
|
|
|
|
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
|
|
|
|
|
// discard channel
|
2018-02-02 16:27:59 +01:00
|
|
|
|
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2019-08-06 17:38:15 +02:00
|
|
|
|
l2cap_free_channel_entry(channel);
|
2016-10-18 12:27:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 16:44:25 +02:00
|
|
|
|
static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){
|
|
|
|
|
return l2cap_get_service_internal(&l2cap_le_services, le_psm);
|
2015-08-12 15:14:59 +02:00
|
|
|
|
}
|
2016-07-08 22:22:38 +02:00
|
|
|
|
|
2016-07-07 17:01:49 +02:00
|
|
|
|
uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
|
2015-08-12 15:14:59 +02:00
|
|
|
|
|
2016-07-07 17:01:49 +02:00
|
|
|
|
log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
|
2015-08-12 15:14:59 +02:00
|
|
|
|
|
|
|
|
|
// check for alread registered psm
|
|
|
|
|
l2cap_service_t *service = l2cap_le_get_service(psm);
|
|
|
|
|
if (service) {
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return L2CAP_SERVICE_ALREADY_REGISTERED;
|
2015-08-12 15:14:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// alloc structure
|
|
|
|
|
service = btstack_memory_l2cap_service_get();
|
|
|
|
|
if (!service) {
|
|
|
|
|
log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return BTSTACK_MEMORY_ALLOC_FAILED;
|
2015-08-12 15:14:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fill in
|
|
|
|
|
service->psm = psm;
|
2016-07-07 17:01:49 +02:00
|
|
|
|
service->mtu = 0;
|
2015-08-12 15:14:59 +02:00
|
|
|
|
service->packet_handler = packet_handler;
|
|
|
|
|
service->required_security_level = security_level;
|
|
|
|
|
|
|
|
|
|
// add to services list
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
|
2015-08-12 15:14:59 +02:00
|
|
|
|
|
|
|
|
|
// done
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return 0;
|
2015-08-12 15:14:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 17:01:49 +02:00
|
|
|
|
uint8_t l2cap_le_unregister_service(uint16_t psm) {
|
2015-08-12 15:14:59 +02:00
|
|
|
|
log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
|
|
|
|
|
l2cap_service_t *service = l2cap_le_get_service(psm);
|
2016-07-22 16:50:18 +02:00
|
|
|
|
if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
|
2016-07-07 17:01:49 +02:00
|
|
|
|
|
2016-01-20 14:50:38 +01:00
|
|
|
|
btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
|
2015-08-12 15:14:59 +02:00
|
|
|
|
btstack_memory_l2cap_service_free(service);
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return 0;
|
2015-08-12 15:14:59 +02:00
|
|
|
|
}
|
2016-07-07 17:01:49 +02:00
|
|
|
|
|
|
|
|
|
uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
|
2016-07-27 16:44:25 +02:00
|
|
|
|
// get channel
|
2018-02-02 16:27:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
|
|
|
|
|
|
|
|
|
|
// validate state
|
|
|
|
|
if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
|
|
|
|
|
return ERROR_CODE_COMMAND_DISALLOWED;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 22:22:38 +02:00
|
|
|
|
// set state accept connection
|
2016-10-04 16:05:27 +02:00
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT;
|
|
|
|
|
channel->receive_sdu_buffer = receive_sdu_buffer;
|
|
|
|
|
channel->local_mtu = mtu;
|
2016-10-05 22:09:32 +02:00
|
|
|
|
channel->new_credits_incoming = initial_credits;
|
|
|
|
|
channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
|
|
|
|
|
|
|
|
|
|
// test
|
2016-10-06 16:09:54 +02:00
|
|
|
|
// channel->new_credits_incoming = 1;
|
2016-07-27 16:44:25 +02:00
|
|
|
|
|
|
|
|
|
// go
|
|
|
|
|
l2cap_run();
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Deny incoming LE Data Channel connection due to resource constraints
|
|
|
|
|
* @param local_cid L2CAP LE Data Channel Identifier
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
uint8_t l2cap_le_decline_connection(uint16_t local_cid){
|
2016-07-27 16:44:25 +02:00
|
|
|
|
// get channel
|
2018-02-02 16:27:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2016-07-27 16:44:25 +02:00
|
|
|
|
if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
|
|
|
|
|
|
|
|
|
|
// validate state
|
|
|
|
|
if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
|
|
|
|
|
return ERROR_CODE_COMMAND_DISALLOWED;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 22:22:38 +02:00
|
|
|
|
// set state decline connection
|
2016-07-27 16:44:25 +02:00
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE;
|
|
|
|
|
channel->reason = 0x04; // no resources available
|
|
|
|
|
l2cap_run();
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-12 12:27:20 +02:00
|
|
|
|
uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle,
|
2016-07-07 17:01:49 +02:00
|
|
|
|
uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
|
2016-07-08 22:22:38 +02:00
|
|
|
|
uint16_t * out_local_cid) {
|
|
|
|
|
|
2016-10-12 12:27:20 +02:00
|
|
|
|
log_info("L2CAP_LE_CREATE_CHANNEL handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hci_connection_t * connection = hci_connection_for_handle(con_handle);
|
|
|
|
|
if (!connection) {
|
|
|
|
|
log_error("no hci_connection for handle 0x%04x", con_handle);
|
|
|
|
|
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 15:18:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, L2CAP_CHANNEL_TYPE_LE_DATA_CHANNEL, connection->address, connection->address_type, psm, mtu, security_level);
|
2016-07-07 17:01:49 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
return BTSTACK_MEMORY_ALLOC_FAILED;
|
|
|
|
|
}
|
2016-07-27 16:44:25 +02:00
|
|
|
|
log_info("l2cap_le_create_channel %p", channel);
|
2016-07-07 17:01:49 +02:00
|
|
|
|
|
|
|
|
|
// store local_cid
|
|
|
|
|
if (out_local_cid){
|
|
|
|
|
*out_local_cid = channel->local_cid;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-12 12:27:20 +02:00
|
|
|
|
// provide buffer
|
|
|
|
|
channel->con_handle = con_handle;
|
2016-10-05 16:33:46 +02:00
|
|
|
|
channel->receive_sdu_buffer = receive_sdu_buffer;
|
2016-10-12 12:27:20 +02:00
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
|
2016-10-05 22:09:32 +02:00
|
|
|
|
channel->new_credits_incoming = initial_credits;
|
|
|
|
|
channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
|
|
|
|
|
|
2016-07-08 22:22:38 +02:00
|
|
|
|
// add to connections list
|
2018-02-02 16:27:59 +01:00
|
|
|
|
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
|
2016-07-08 22:22:38 +02:00
|
|
|
|
|
2016-10-12 12:27:20 +02:00
|
|
|
|
// go
|
|
|
|
|
l2cap_run();
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Provide credtis for LE Data Channel
|
|
|
|
|
* @param local_cid L2CAP LE Data Channel Identifier
|
|
|
|
|
* @param credits Number additional credits for peer
|
|
|
|
|
*/
|
2016-10-05 11:01:26 +02:00
|
|
|
|
uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){
|
2016-10-06 16:09:54 +02:00
|
|
|
|
|
2018-02-02 16:27:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2016-10-06 16:09:54 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid);
|
|
|
|
|
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 22:22:38 +02:00
|
|
|
|
// check state
|
2016-10-06 16:09:54 +02:00
|
|
|
|
if (channel->state != L2CAP_STATE_OPEN){
|
|
|
|
|
log_error("l2cap_le_provide_credits but channel 0x%02x not open yet", local_cid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// assert incoming credits + credits <= 0xffff
|
|
|
|
|
uint32_t total_credits = channel->credits_incoming;
|
|
|
|
|
total_credits += channel->new_credits_incoming;
|
|
|
|
|
total_credits += credits;
|
|
|
|
|
if (total_credits > 0xffff){
|
|
|
|
|
log_error("l2cap_le_provide_credits overrun: current %u, scheduled %u, additional %u", channel->credits_incoming,
|
|
|
|
|
channel->new_credits_incoming, credits);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 22:22:38 +02:00
|
|
|
|
// set credits_granted
|
2016-10-06 16:09:54 +02:00
|
|
|
|
channel->new_credits_incoming += credits;
|
|
|
|
|
|
|
|
|
|
// go
|
|
|
|
|
l2cap_run();
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
|
|
|
|
|
* @param local_cid L2CAP LE Data Channel Identifier
|
|
|
|
|
*/
|
2016-10-05 11:01:26 +02:00
|
|
|
|
int l2cap_le_can_send_now(uint16_t local_cid){
|
2018-02-02 16:27:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2016-10-18 11:49:04 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check state
|
|
|
|
|
if (channel->state != L2CAP_STATE_OPEN) return 0;
|
|
|
|
|
|
|
|
|
|
// check queue
|
|
|
|
|
if (channel->send_sdu_buffer) return 0;
|
|
|
|
|
|
|
|
|
|
// fine, go ahead
|
|
|
|
|
return 1;
|
2016-07-07 17:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
|
|
|
|
|
* @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
|
|
|
|
|
* so packet handler should be ready to handle it
|
|
|
|
|
* @param local_cid L2CAP LE Data Channel Identifier
|
|
|
|
|
*/
|
2016-10-05 11:01:26 +02:00
|
|
|
|
uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){
|
2018-02-02 16:27:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2016-10-18 11:49:04 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error("l2cap_le_request_can_send_now_event no channel for cid 0x%02x", local_cid);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
channel->waiting_for_can_send_now = 1;
|
|
|
|
|
l2cap_le_notify_channel_can_send(channel);
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Send data via LE Data Channel
|
|
|
|
|
* @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
|
|
|
|
|
* @param local_cid L2CAP LE Data Channel Identifier
|
|
|
|
|
* @param data data to send
|
|
|
|
|
* @param size data size
|
|
|
|
|
*/
|
2016-10-05 11:01:26 +02:00
|
|
|
|
uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){
|
|
|
|
|
|
2018-02-02 16:27:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2016-10-05 11:01:26 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error("l2cap_send no channel for cid 0x%02x", local_cid);
|
2016-10-05 11:58:27 +02:00
|
|
|
|
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
|
2016-10-05 11:01:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (len > channel->remote_mtu){
|
|
|
|
|
log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
|
|
|
|
|
return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 15:49:18 +02:00
|
|
|
|
if (channel->send_sdu_buffer){
|
2016-10-05 11:01:26 +02:00
|
|
|
|
log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
|
|
|
|
|
return BTSTACK_ACL_BUFFERS_FULL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 15:49:18 +02:00
|
|
|
|
channel->send_sdu_buffer = data;
|
|
|
|
|
channel->send_sdu_len = len;
|
|
|
|
|
channel->send_sdu_pos = 0;
|
2016-10-05 11:01:26 +02:00
|
|
|
|
|
2016-10-05 15:49:18 +02:00
|
|
|
|
l2cap_run();
|
|
|
|
|
return 0;
|
2016-07-07 17:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Disconnect from LE Data Channel
|
|
|
|
|
* @param local_cid L2CAP LE Data Channel Identifier
|
|
|
|
|
*/
|
2016-10-05 11:58:27 +02:00
|
|
|
|
uint8_t l2cap_le_disconnect(uint16_t local_cid)
|
2016-07-07 17:01:49 +02:00
|
|
|
|
{
|
2018-02-02 16:27:59 +01:00
|
|
|
|
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
|
2016-10-05 11:58:27 +02:00
|
|
|
|
if (!channel) {
|
|
|
|
|
log_error("l2cap_send no channel for cid 0x%02x", local_cid);
|
|
|
|
|
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
|
|
|
|
|
l2cap_run();
|
2016-07-07 17:01:49 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 19:43:35 +01:00
|
|
|
|
#endif
|