BLE demo for mbed Ported RunningElectronics's SBDBT firmware for BLE. It can communicate with iOS

Dependencies:   FatFileSystem mbed

Fork of BTstack by Norimasa Okamoto

Committer:
todotani
Date:
Wed Feb 20 14:18:38 2013 +0000
Revision:
6:cf06ba884429
Parent:
0:1ed23ab1345f
Change tick timer to 1ms. Change attribute 0xFFF1 as read of DigitalIn p5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:1ed23ab1345f 1 /*
va009039 0:1ed23ab1345f 2 * Copyright (C) 2009-2012 by Matthias Ringwald
va009039 0:1ed23ab1345f 3 *
va009039 0:1ed23ab1345f 4 * Redistribution and use in source and binary forms, with or without
va009039 0:1ed23ab1345f 5 * modification, are permitted provided that the following conditions
va009039 0:1ed23ab1345f 6 * are met:
va009039 0:1ed23ab1345f 7 *
va009039 0:1ed23ab1345f 8 * 1. Redistributions of source code must retain the above copyright
va009039 0:1ed23ab1345f 9 * notice, this list of conditions and the following disclaimer.
va009039 0:1ed23ab1345f 10 * 2. Redistributions in binary form must reproduce the above copyright
va009039 0:1ed23ab1345f 11 * notice, this list of conditions and the following disclaimer in the
va009039 0:1ed23ab1345f 12 * documentation and/or other materials provided with the distribution.
va009039 0:1ed23ab1345f 13 * 3. Neither the name of the copyright holders nor the names of
va009039 0:1ed23ab1345f 14 * contributors may be used to endorse or promote products derived
va009039 0:1ed23ab1345f 15 * from this software without specific prior written permission.
va009039 0:1ed23ab1345f 16 * 4. Any redistribution, use, or modification is done solely for
va009039 0:1ed23ab1345f 17 * personal benefit and not for any commercial purpose or for
va009039 0:1ed23ab1345f 18 * monetary gain.
va009039 0:1ed23ab1345f 19 *
va009039 0:1ed23ab1345f 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
va009039 0:1ed23ab1345f 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
va009039 0:1ed23ab1345f 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
va009039 0:1ed23ab1345f 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
va009039 0:1ed23ab1345f 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
va009039 0:1ed23ab1345f 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
va009039 0:1ed23ab1345f 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
va009039 0:1ed23ab1345f 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
va009039 0:1ed23ab1345f 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
va009039 0:1ed23ab1345f 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
va009039 0:1ed23ab1345f 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
va009039 0:1ed23ab1345f 31 * SUCH DAMAGE.
va009039 0:1ed23ab1345f 32 *
va009039 0:1ed23ab1345f 33 * Please inquire about commercial licensing options at btstack@ringwald.ch
va009039 0:1ed23ab1345f 34 *
va009039 0:1ed23ab1345f 35 */
va009039 0:1ed23ab1345f 36
va009039 0:1ed23ab1345f 37 /*
va009039 0:1ed23ab1345f 38 * l2cap.c
va009039 0:1ed23ab1345f 39 *
va009039 0:1ed23ab1345f 40 * Logical Link Control and Adaption Protocl (L2CAP)
va009039 0:1ed23ab1345f 41 *
va009039 0:1ed23ab1345f 42 * Created by Matthias Ringwald on 5/16/09.
va009039 0:1ed23ab1345f 43 */
va009039 0:1ed23ab1345f 44
va009039 0:1ed23ab1345f 45 #include "l2cap.h"
va009039 0:1ed23ab1345f 46 #include "hci.h"
va009039 0:1ed23ab1345f 47 #include "hci_dump.h"
va009039 0:1ed23ab1345f 48 #include "debug.h"
va009039 0:1ed23ab1345f 49 #include "btstack_memory.h"
va009039 0:1ed23ab1345f 50
va009039 0:1ed23ab1345f 51 #include <stdarg.h>
va009039 0:1ed23ab1345f 52 #include <string.h>
va009039 0:1ed23ab1345f 53
va009039 0:1ed23ab1345f 54 #include <stdio.h>
va009039 0:1ed23ab1345f 55
va009039 0:1ed23ab1345f 56 // nr of buffered acl packets in outgoing queue to get max performance
va009039 0:1ed23ab1345f 57 #define NR_BUFFERED_ACL_PACKETS 3
va009039 0:1ed23ab1345f 58
va009039 0:1ed23ab1345f 59 // used to cache l2cap rejects, echo, and informational requests
va009039 0:1ed23ab1345f 60 #define NR_PENDING_SIGNALING_RESPONSES 3
va009039 0:1ed23ab1345f 61
va009039 0:1ed23ab1345f 62 // offsets for L2CAP SIGNALING COMMANDS
va009039 0:1ed23ab1345f 63 #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0
va009039 0:1ed23ab1345f 64 #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1
va009039 0:1ed23ab1345f 65 #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
va009039 0:1ed23ab1345f 66 #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4
va009039 0:1ed23ab1345f 67
va009039 0:1ed23ab1345f 68 static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
va009039 0:1ed23ab1345f 69 static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
va009039 0:1ed23ab1345f 70
va009039 0:1ed23ab1345f 71 // used to cache l2cap rejects, echo, and informational requests
va009039 0:1ed23ab1345f 72 static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
va009039 0:1ed23ab1345f 73 static int signaling_responses_pending;
va009039 0:1ed23ab1345f 74
va009039 0:1ed23ab1345f 75 static linked_list_t l2cap_channels = NULL;
va009039 0:1ed23ab1345f 76 static linked_list_t l2cap_services = NULL;
va009039 0:1ed23ab1345f 77 static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler;
va009039 0:1ed23ab1345f 78 static int new_credits_blocked = 0;
va009039 0:1ed23ab1345f 79
va009039 0:1ed23ab1345f 80 static btstack_packet_handler_t attribute_protocol_packet_handler = NULL;
va009039 0:1ed23ab1345f 81 static btstack_packet_handler_t security_protocol_packet_handler = NULL;
va009039 0:1ed23ab1345f 82
va009039 0:1ed23ab1345f 83 // prototypes
va009039 0:1ed23ab1345f 84 static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
va009039 0:1ed23ab1345f 85 static l2cap_service_t * l2cap_get_service(uint16_t psm);
va009039 0:1ed23ab1345f 86 static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
va009039 0:1ed23ab1345f 87 static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
va009039 0:1ed23ab1345f 88 static void l2cap_emit_connection_request(l2cap_channel_t *channel);
va009039 0:1ed23ab1345f 89 static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
va009039 0:1ed23ab1345f 90
va009039 0:1ed23ab1345f 91
va009039 0:1ed23ab1345f 92 void l2cap_init(){
va009039 0:1ed23ab1345f 93 new_credits_blocked = 0;
va009039 0:1ed23ab1345f 94 signaling_responses_pending = 0;
va009039 0:1ed23ab1345f 95
va009039 0:1ed23ab1345f 96 l2cap_channels = NULL;
va009039 0:1ed23ab1345f 97 l2cap_services = NULL;
va009039 0:1ed23ab1345f 98
va009039 0:1ed23ab1345f 99 packet_handler = null_packet_handler;
va009039 0:1ed23ab1345f 100
va009039 0:1ed23ab1345f 101 //
va009039 0:1ed23ab1345f 102 // register callback with HCI
va009039 0:1ed23ab1345f 103 //
va009039 0:1ed23ab1345f 104 hci_register_packet_handler(&l2cap_packet_handler);
va009039 0:1ed23ab1345f 105 hci_connectable_control(0); // no services yet
va009039 0:1ed23ab1345f 106 }
va009039 0:1ed23ab1345f 107
va009039 0:1ed23ab1345f 108
va009039 0:1ed23ab1345f 109 /** Register L2CAP packet handlers */
va009039 0:1ed23ab1345f 110 static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
va009039 0:1ed23ab1345f 111 }
va009039 0:1ed23ab1345f 112 void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
va009039 0:1ed23ab1345f 113 packet_handler = handler;
va009039 0:1ed23ab1345f 114 }
va009039 0:1ed23ab1345f 115
va009039 0:1ed23ab1345f 116 // notify client/protocol handler
va009039 0:1ed23ab1345f 117 void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
va009039 0:1ed23ab1345f 118 if (channel->packet_handler) {
va009039 0:1ed23ab1345f 119 (* (channel->packet_handler))(type, channel->local_cid, data, size);
va009039 0:1ed23ab1345f 120 } else {
va009039 0:1ed23ab1345f 121 (*packet_handler)(channel->connection, type, channel->local_cid, data, size);
va009039 0:1ed23ab1345f 122 }
va009039 0:1ed23ab1345f 123 }
va009039 0:1ed23ab1345f 124
va009039 0:1ed23ab1345f 125 void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
va009039 0:1ed23ab1345f 126 uint8_t event[21];
va009039 0:1ed23ab1345f 127 event[0] = L2CAP_EVENT_CHANNEL_OPENED;
va009039 0:1ed23ab1345f 128 event[1] = sizeof(event) - 2;
va009039 0:1ed23ab1345f 129 event[2] = status;
va009039 0:1ed23ab1345f 130 bt_flip_addr(&event[3], channel->address);
va009039 0:1ed23ab1345f 131 bt_store_16(event, 9, channel->handle);
va009039 0:1ed23ab1345f 132 bt_store_16(event, 11, channel->psm);
va009039 0:1ed23ab1345f 133 bt_store_16(event, 13, channel->local_cid);
va009039 0:1ed23ab1345f 134 bt_store_16(event, 15, channel->remote_cid);
va009039 0:1ed23ab1345f 135 bt_store_16(event, 17, channel->local_mtu);
va009039 0:1ed23ab1345f 136 bt_store_16(event, 19, channel->remote_mtu);
va009039 0:1ed23ab1345f 137 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
va009039 0:1ed23ab1345f 138 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
va009039 0:1ed23ab1345f 139 }
va009039 0:1ed23ab1345f 140
va009039 0:1ed23ab1345f 141 void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
va009039 0:1ed23ab1345f 142 uint8_t event[4];
va009039 0:1ed23ab1345f 143 event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
va009039 0:1ed23ab1345f 144 event[1] = sizeof(event) - 2;
va009039 0:1ed23ab1345f 145 bt_store_16(event, 2, channel->local_cid);
va009039 0:1ed23ab1345f 146 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
va009039 0:1ed23ab1345f 147 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
va009039 0:1ed23ab1345f 148 }
va009039 0:1ed23ab1345f 149
va009039 0:1ed23ab1345f 150 void l2cap_emit_connection_request(l2cap_channel_t *channel) {
va009039 0:1ed23ab1345f 151 uint8_t event[16];
va009039 0:1ed23ab1345f 152 event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
va009039 0:1ed23ab1345f 153 event[1] = sizeof(event) - 2;
va009039 0:1ed23ab1345f 154 bt_flip_addr(&event[2], channel->address);
va009039 0:1ed23ab1345f 155 bt_store_16(event, 8, channel->handle);
va009039 0:1ed23ab1345f 156 bt_store_16(event, 10, channel->psm);
va009039 0:1ed23ab1345f 157 bt_store_16(event, 12, channel->local_cid);
va009039 0:1ed23ab1345f 158 bt_store_16(event, 14, channel->remote_cid);
va009039 0:1ed23ab1345f 159 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
va009039 0:1ed23ab1345f 160 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
va009039 0:1ed23ab1345f 161 }
va009039 0:1ed23ab1345f 162
va009039 0:1ed23ab1345f 163 static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){
va009039 0:1ed23ab1345f 164 uint8_t event[5];
va009039 0:1ed23ab1345f 165 event[0] = L2CAP_EVENT_SERVICE_REGISTERED;
va009039 0:1ed23ab1345f 166 event[1] = sizeof(event) - 2;
va009039 0:1ed23ab1345f 167 event[2] = status;
va009039 0:1ed23ab1345f 168 bt_store_16(event, 3, psm);
va009039 0:1ed23ab1345f 169 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
va009039 0:1ed23ab1345f 170 (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event));
va009039 0:1ed23ab1345f 171 }
va009039 0:1ed23ab1345f 172
va009039 0:1ed23ab1345f 173 void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) {
va009039 0:1ed23ab1345f 174 // track credits
va009039 0:1ed23ab1345f 175 channel->packets_granted += credits;
va009039 0:1ed23ab1345f 176 // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits);
va009039 0:1ed23ab1345f 177
va009039 0:1ed23ab1345f 178 uint8_t event[5];
va009039 0:1ed23ab1345f 179 event[0] = L2CAP_EVENT_CREDITS;
va009039 0:1ed23ab1345f 180 event[1] = sizeof(event) - 2;
va009039 0:1ed23ab1345f 181 bt_store_16(event, 2, channel->local_cid);
va009039 0:1ed23ab1345f 182 event[4] = credits;
va009039 0:1ed23ab1345f 183 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
va009039 0:1ed23ab1345f 184 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event));
va009039 0:1ed23ab1345f 185 }
va009039 0:1ed23ab1345f 186
va009039 0:1ed23ab1345f 187 void l2cap_block_new_credits(uint8_t blocked){
va009039 0:1ed23ab1345f 188 new_credits_blocked = blocked;
va009039 0:1ed23ab1345f 189 }
va009039 0:1ed23ab1345f 190
va009039 0:1ed23ab1345f 191 void l2cap_hand_out_credits(void){
va009039 0:1ed23ab1345f 192
va009039 0:1ed23ab1345f 193 if (new_credits_blocked) return; // we're told not to. used by daemon
va009039 0:1ed23ab1345f 194
va009039 0:1ed23ab1345f 195 linked_item_t *it;
va009039 0:1ed23ab1345f 196 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
va009039 0:1ed23ab1345f 197 if (!hci_number_free_acl_slots()) return;
va009039 0:1ed23ab1345f 198 l2cap_channel_t * channel = (l2cap_channel_t *) it;
va009039 0:1ed23ab1345f 199 if (channel->state != L2CAP_STATE_OPEN) continue;
va009039 0:1ed23ab1345f 200 if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) {
va009039 0:1ed23ab1345f 201 l2cap_emit_credits(channel, 1);
va009039 0:1ed23ab1345f 202 }
va009039 0:1ed23ab1345f 203 }
va009039 0:1ed23ab1345f 204 }
va009039 0:1ed23ab1345f 205
va009039 0:1ed23ab1345f 206 l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
va009039 0:1ed23ab1345f 207 linked_item_t *it;
va009039 0:1ed23ab1345f 208 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
va009039 0:1ed23ab1345f 209 l2cap_channel_t * channel = (l2cap_channel_t *) it;
va009039 0:1ed23ab1345f 210 if ( channel->local_cid == local_cid) {
va009039 0:1ed23ab1345f 211 return channel;
va009039 0:1ed23ab1345f 212 }
va009039 0:1ed23ab1345f 213 }
va009039 0:1ed23ab1345f 214 return NULL;
va009039 0:1ed23ab1345f 215 }
va009039 0:1ed23ab1345f 216
va009039 0:1ed23ab1345f 217 int l2cap_can_send_packet_now(uint16_t local_cid){
va009039 0:1ed23ab1345f 218 l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
va009039 0:1ed23ab1345f 219 if (!channel) return 0;
va009039 0:1ed23ab1345f 220 if (!channel->packets_granted) return 0;
va009039 0:1ed23ab1345f 221 return hci_can_send_packet_now(HCI_ACL_DATA_PACKET);
va009039 0:1ed23ab1345f 222 }
va009039 0:1ed23ab1345f 223
va009039 0:1ed23ab1345f 224 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
va009039 0:1ed23ab1345f 225 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
va009039 0:1ed23ab1345f 226 if (channel) {
va009039 0:1ed23ab1345f 227 return channel->remote_mtu;
va009039 0:1ed23ab1345f 228 }
va009039 0:1ed23ab1345f 229 return 0;
va009039 0:1ed23ab1345f 230 }
va009039 0:1ed23ab1345f 231
va009039 0:1ed23ab1345f 232 int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
va009039 0:1ed23ab1345f 233
va009039 0:1ed23ab1345f 234 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
va009039 0:1ed23ab1345f 235 log_info("l2cap_send_signaling_packet, cannot send\n");
va009039 0:1ed23ab1345f 236 return BTSTACK_ACL_BUFFERS_FULL;
va009039 0:1ed23ab1345f 237 }
va009039 0:1ed23ab1345f 238
va009039 0:1ed23ab1345f 239 // log_info("l2cap_send_signaling_packet type %u\n", cmd);
va009039 0:1ed23ab1345f 240 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
va009039 0:1ed23ab1345f 241 va_list argptr;
va009039 0:1ed23ab1345f 242 va_start(argptr, identifier);
va009039 0:1ed23ab1345f 243 uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr);
va009039 0:1ed23ab1345f 244 va_end(argptr);
va009039 0:1ed23ab1345f 245 // log_info("l2cap_send_signaling_packet con %u!\n", handle);
va009039 0:1ed23ab1345f 246 return hci_send_acl_packet(acl_buffer, len);
va009039 0:1ed23ab1345f 247 }
va009039 0:1ed23ab1345f 248
va009039 0:1ed23ab1345f 249 uint8_t *l2cap_get_outgoing_buffer(void){
va009039 0:1ed23ab1345f 250 return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
va009039 0:1ed23ab1345f 251 }
va009039 0:1ed23ab1345f 252
va009039 0:1ed23ab1345f 253 int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
va009039 0:1ed23ab1345f 254
va009039 0:1ed23ab1345f 255 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
va009039 0:1ed23ab1345f 256 log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
va009039 0:1ed23ab1345f 257 return BTSTACK_ACL_BUFFERS_FULL;
va009039 0:1ed23ab1345f 258 }
va009039 0:1ed23ab1345f 259
va009039 0:1ed23ab1345f 260 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
va009039 0:1ed23ab1345f 261 if (!channel) {
va009039 0:1ed23ab1345f 262 log_error("l2cap_send_internal no channel for cid %u\n", local_cid);
va009039 0:1ed23ab1345f 263 return -1; // TODO: define error
va009039 0:1ed23ab1345f 264 }
va009039 0:1ed23ab1345f 265
va009039 0:1ed23ab1345f 266 if (channel->packets_granted == 0){
va009039 0:1ed23ab1345f 267 log_error("l2cap_send_internal cid %u, no credits!\n", local_cid);
va009039 0:1ed23ab1345f 268 return -1; // TODO: define error
va009039 0:1ed23ab1345f 269 }
va009039 0:1ed23ab1345f 270
va009039 0:1ed23ab1345f 271 --channel->packets_granted;
va009039 0:1ed23ab1345f 272
va009039 0:1ed23ab1345f 273 log_debug("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n",
va009039 0:1ed23ab1345f 274 local_cid, channel->handle, channel->packets_granted);
va009039 0:1ed23ab1345f 275
va009039 0:1ed23ab1345f 276 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
va009039 0:1ed23ab1345f 277
va009039 0:1ed23ab1345f 278 // 0 - Connection handle : PB=10 : BC=00
va009039 0:1ed23ab1345f 279 bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14));
va009039 0:1ed23ab1345f 280 // 2 - ACL length
va009039 0:1ed23ab1345f 281 bt_store_16(acl_buffer, 2, len + 4);
va009039 0:1ed23ab1345f 282 // 4 - L2CAP packet length
va009039 0:1ed23ab1345f 283 bt_store_16(acl_buffer, 4, len + 0);
va009039 0:1ed23ab1345f 284 // 6 - L2CAP channel DEST
va009039 0:1ed23ab1345f 285 bt_store_16(acl_buffer, 6, channel->remote_cid);
va009039 0:1ed23ab1345f 286 // send
va009039 0:1ed23ab1345f 287 int err = hci_send_acl_packet(acl_buffer, len+8);
va009039 0:1ed23ab1345f 288
va009039 0:1ed23ab1345f 289 l2cap_hand_out_credits();
va009039 0:1ed23ab1345f 290
va009039 0:1ed23ab1345f 291 return err;
va009039 0:1ed23ab1345f 292 }
va009039 0:1ed23ab1345f 293
va009039 0:1ed23ab1345f 294 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
va009039 0:1ed23ab1345f 295
va009039 0:1ed23ab1345f 296 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
va009039 0:1ed23ab1345f 297 log_info("l2cap_send_prepared_to_handle cid %u, cannot send\n", cid);
va009039 0:1ed23ab1345f 298 return BTSTACK_ACL_BUFFERS_FULL;
va009039 0:1ed23ab1345f 299 }
va009039 0:1ed23ab1345f 300
va009039 0:1ed23ab1345f 301 log_debug("l2cap_send_prepared_to_handle cid %u, handle %u\n", cid, handle);
va009039 0:1ed23ab1345f 302
va009039 0:1ed23ab1345f 303 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
va009039 0:1ed23ab1345f 304
va009039 0:1ed23ab1345f 305 // 0 - Connection handle : PB=10 : BC=00
va009039 0:1ed23ab1345f 306 bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14));
va009039 0:1ed23ab1345f 307 // 2 - ACL length
va009039 0:1ed23ab1345f 308 bt_store_16(acl_buffer, 2, len + 4);
va009039 0:1ed23ab1345f 309 // 4 - L2CAP packet length
va009039 0:1ed23ab1345f 310 bt_store_16(acl_buffer, 4, len + 0);
va009039 0:1ed23ab1345f 311 // 6 - L2CAP channel DEST
va009039 0:1ed23ab1345f 312 bt_store_16(acl_buffer, 6, cid);
va009039 0:1ed23ab1345f 313 // send
va009039 0:1ed23ab1345f 314 int err = hci_send_acl_packet(acl_buffer, len+8);
va009039 0:1ed23ab1345f 315
va009039 0:1ed23ab1345f 316 l2cap_hand_out_credits();
va009039 0:1ed23ab1345f 317
va009039 0:1ed23ab1345f 318 return err;
va009039 0:1ed23ab1345f 319 }
va009039 0:1ed23ab1345f 320
va009039 0:1ed23ab1345f 321 int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){
va009039 0:1ed23ab1345f 322
va009039 0:1ed23ab1345f 323 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
va009039 0:1ed23ab1345f 324 log_info("l2cap_send_internal cid %u, cannot send\n", local_cid);
va009039 0:1ed23ab1345f 325 return BTSTACK_ACL_BUFFERS_FULL;
va009039 0:1ed23ab1345f 326 }
va009039 0:1ed23ab1345f 327
va009039 0:1ed23ab1345f 328 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
va009039 0:1ed23ab1345f 329
va009039 0:1ed23ab1345f 330 memcpy(&acl_buffer[8], data, len);
va009039 0:1ed23ab1345f 331
va009039 0:1ed23ab1345f 332 return l2cap_send_prepared(local_cid, len);
va009039 0:1ed23ab1345f 333 }
va009039 0:1ed23ab1345f 334
va009039 0:1ed23ab1345f 335 int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){
va009039 0:1ed23ab1345f 336
va009039 0:1ed23ab1345f 337 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){
va009039 0:1ed23ab1345f 338 log_info("l2cap_send_internal cid %u, cannot send\n", cid);
va009039 0:1ed23ab1345f 339 return BTSTACK_ACL_BUFFERS_FULL;
va009039 0:1ed23ab1345f 340 }
va009039 0:1ed23ab1345f 341
va009039 0:1ed23ab1345f 342 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer();
va009039 0:1ed23ab1345f 343
va009039 0:1ed23ab1345f 344 memcpy(&acl_buffer[8], data, len);
va009039 0:1ed23ab1345f 345
va009039 0:1ed23ab1345f 346 return l2cap_send_prepared_connectionless(handle, cid, len);
va009039 0:1ed23ab1345f 347 }
va009039 0:1ed23ab1345f 348
va009039 0:1ed23ab1345f 349 static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
va009039 0:1ed23ab1345f 350 channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
va009039 0:1ed23ab1345f 351 }
va009039 0:1ed23ab1345f 352
va009039 0:1ed23ab1345f 353 static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
va009039 0:1ed23ab1345f 354 channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
va009039 0:1ed23ab1345f 355 }
va009039 0:1ed23ab1345f 356
va009039 0:1ed23ab1345f 357
va009039 0:1ed23ab1345f 358
va009039 0:1ed23ab1345f 359 // MARK: L2CAP_RUN
va009039 0:1ed23ab1345f 360 // process outstanding signaling tasks
va009039 0:1ed23ab1345f 361 void l2cap_run(void){
va009039 0:1ed23ab1345f 362
va009039 0:1ed23ab1345f 363 // check pending signaling responses
va009039 0:1ed23ab1345f 364 while (signaling_responses_pending){
va009039 0:1ed23ab1345f 365
va009039 0:1ed23ab1345f 366 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
va009039 0:1ed23ab1345f 367
va009039 0:1ed23ab1345f 368 hci_con_handle_t handle = signaling_responses[0].handle;
va009039 0:1ed23ab1345f 369 uint8_t sig_id = signaling_responses[0].sig_id;
va009039 0:1ed23ab1345f 370 uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST
va009039 0:1ed23ab1345f 371 uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST
va009039 0:1ed23ab1345f 372
va009039 0:1ed23ab1345f 373 switch (signaling_responses[0].code){
va009039 0:1ed23ab1345f 374 case CONNECTION_REQUEST:
va009039 0:1ed23ab1345f 375 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
va009039 0:1ed23ab1345f 376 break;
va009039 0:1ed23ab1345f 377 case ECHO_REQUEST:
va009039 0:1ed23ab1345f 378 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
va009039 0:1ed23ab1345f 379 break;
va009039 0:1ed23ab1345f 380 case INFORMATION_REQUEST:
va009039 0:1ed23ab1345f 381 if (infoType == 2) {
va009039 0:1ed23ab1345f 382 uint32_t features = 0;
va009039 0:1ed23ab1345f 383 // extended features request supported, however no features present
va009039 0:1ed23ab1345f 384 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features);
va009039 0:1ed23ab1345f 385 } else {
va009039 0:1ed23ab1345f 386 // all other types are not supported
va009039 0:1ed23ab1345f 387 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
va009039 0:1ed23ab1345f 388 }
va009039 0:1ed23ab1345f 389 break;
va009039 0:1ed23ab1345f 390 default:
va009039 0:1ed23ab1345f 391 // should not happen
va009039 0:1ed23ab1345f 392 break;
va009039 0:1ed23ab1345f 393 }
va009039 0:1ed23ab1345f 394
va009039 0:1ed23ab1345f 395 // remove first item
va009039 0:1ed23ab1345f 396 signaling_responses_pending--;
va009039 0:1ed23ab1345f 397 int i;
va009039 0:1ed23ab1345f 398 for (i=0; i < signaling_responses_pending; i++){
va009039 0:1ed23ab1345f 399 memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
va009039 0:1ed23ab1345f 400 }
va009039 0:1ed23ab1345f 401 }
va009039 0:1ed23ab1345f 402
va009039 0:1ed23ab1345f 403 uint8_t config_options[4];
va009039 0:1ed23ab1345f 404 linked_item_t *it;
va009039 0:1ed23ab1345f 405 linked_item_t *next;
va009039 0:1ed23ab1345f 406 for (it = (linked_item_t *) l2cap_channels; it ; it = next){
va009039 0:1ed23ab1345f 407 next = it->next; // cache next item as current item might get freed
va009039 0:1ed23ab1345f 408
va009039 0:1ed23ab1345f 409 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
va009039 0:1ed23ab1345f 410 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break;
va009039 0:1ed23ab1345f 411
va009039 0:1ed23ab1345f 412 l2cap_channel_t * channel = (l2cap_channel_t *) it;
va009039 0:1ed23ab1345f 413
va009039 0:1ed23ab1345f 414 // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var);
va009039 0:1ed23ab1345f 415
va009039 0:1ed23ab1345f 416
va009039 0:1ed23ab1345f 417 switch (channel->state){
va009039 0:1ed23ab1345f 418
va009039 0:1ed23ab1345f 419 case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
va009039 0:1ed23ab1345f 420 // send connection request - set state first
va009039 0:1ed23ab1345f 421 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
va009039 0:1ed23ab1345f 422 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
va009039 0:1ed23ab1345f 423 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
va009039 0:1ed23ab1345f 424 break;
va009039 0:1ed23ab1345f 425
va009039 0:1ed23ab1345f 426 case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
va009039 0:1ed23ab1345f 427 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0);
va009039 0:1ed23ab1345f 428 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
va009039 0:1ed23ab1345f 429 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); // -- remove from list
va009039 0:1ed23ab1345f 430 btstack_memory_l2cap_channel_free(channel);
va009039 0:1ed23ab1345f 431 break;
va009039 0:1ed23ab1345f 432
va009039 0:1ed23ab1345f 433 case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
va009039 0:1ed23ab1345f 434 channel->state = L2CAP_STATE_CONFIG;
va009039 0:1ed23ab1345f 435 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
va009039 0:1ed23ab1345f 436 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
va009039 0:1ed23ab1345f 437 break;
va009039 0:1ed23ab1345f 438
va009039 0:1ed23ab1345f 439 case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
va009039 0:1ed23ab1345f 440 // success, start l2cap handshake
va009039 0:1ed23ab1345f 441 channel->local_sig_id = l2cap_next_sig_id();
va009039 0:1ed23ab1345f 442 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
va009039 0:1ed23ab1345f 443 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
va009039 0:1ed23ab1345f 444 break;
va009039 0:1ed23ab1345f 445
va009039 0:1ed23ab1345f 446 case L2CAP_STATE_CONFIG:
va009039 0:1ed23ab1345f 447 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
va009039 0:1ed23ab1345f 448 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
va009039 0:1ed23ab1345f 449 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
va009039 0:1ed23ab1345f 450 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL);
va009039 0:1ed23ab1345f 451 }
va009039 0:1ed23ab1345f 452 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
va009039 0:1ed23ab1345f 453 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
va009039 0:1ed23ab1345f 454 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
va009039 0:1ed23ab1345f 455 channel->local_sig_id = l2cap_next_sig_id();
va009039 0:1ed23ab1345f 456 config_options[0] = 1; // MTU
va009039 0:1ed23ab1345f 457 config_options[1] = 2; // len param
va009039 0:1ed23ab1345f 458 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
va009039 0:1ed23ab1345f 459 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
va009039 0:1ed23ab1345f 460 }
va009039 0:1ed23ab1345f 461 if (l2cap_channel_ready_for_open(channel)){
va009039 0:1ed23ab1345f 462 channel->state = L2CAP_STATE_OPEN;
va009039 0:1ed23ab1345f 463 l2cap_emit_channel_opened(channel, 0); // success
va009039 0:1ed23ab1345f 464 l2cap_emit_credits(channel, 1);
va009039 0:1ed23ab1345f 465 }
va009039 0:1ed23ab1345f 466 break;
va009039 0:1ed23ab1345f 467
va009039 0:1ed23ab1345f 468 case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
va009039 0:1ed23ab1345f 469 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
va009039 0:1ed23ab1345f 470 l2cap_finialize_channel_close(channel); // -- remove from list
va009039 0:1ed23ab1345f 471 break;
va009039 0:1ed23ab1345f 472
va009039 0:1ed23ab1345f 473 case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
va009039 0:1ed23ab1345f 474 channel->local_sig_id = l2cap_next_sig_id();
va009039 0:1ed23ab1345f 475 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
va009039 0:1ed23ab1345f 476 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
va009039 0:1ed23ab1345f 477 break;
va009039 0:1ed23ab1345f 478 default:
va009039 0:1ed23ab1345f 479 break;
va009039 0:1ed23ab1345f 480 }
va009039 0:1ed23ab1345f 481 }
va009039 0:1ed23ab1345f 482 }
va009039 0:1ed23ab1345f 483
va009039 0:1ed23ab1345f 484 uint16_t l2cap_max_mtu(void){
va009039 0:1ed23ab1345f 485 return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE;
va009039 0:1ed23ab1345f 486 }
va009039 0:1ed23ab1345f 487
va009039 0:1ed23ab1345f 488 // open outgoing L2CAP channel
va009039 0:1ed23ab1345f 489 void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler,
va009039 0:1ed23ab1345f 490 bd_addr_t address, uint16_t psm, uint16_t mtu){
va009039 0:1ed23ab1345f 491
va009039 0:1ed23ab1345f 492 // alloc structure
va009039 0:1ed23ab1345f 493 l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
va009039 0:1ed23ab1345f 494 if (!chan) {
va009039 0:1ed23ab1345f 495 // emit error event
va009039 0:1ed23ab1345f 496 l2cap_channel_t dummy_channel;
va009039 0:1ed23ab1345f 497 BD_ADDR_COPY(dummy_channel.address, address);
va009039 0:1ed23ab1345f 498 dummy_channel.psm = psm;
va009039 0:1ed23ab1345f 499 l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED);
va009039 0:1ed23ab1345f 500 return;
va009039 0:1ed23ab1345f 501 }
va009039 0:1ed23ab1345f 502 // limit local mtu to max acl packet length
va009039 0:1ed23ab1345f 503 if (mtu > l2cap_max_mtu()) {
va009039 0:1ed23ab1345f 504 mtu = l2cap_max_mtu();
va009039 0:1ed23ab1345f 505 }
va009039 0:1ed23ab1345f 506
va009039 0:1ed23ab1345f 507 // fill in
va009039 0:1ed23ab1345f 508 BD_ADDR_COPY(chan->address, address);
va009039 0:1ed23ab1345f 509 chan->psm = psm;
va009039 0:1ed23ab1345f 510 chan->handle = 0;
va009039 0:1ed23ab1345f 511 chan->connection = connection;
va009039 0:1ed23ab1345f 512 chan->packet_handler = packet_handler;
va009039 0:1ed23ab1345f 513 chan->remote_mtu = L2CAP_MINIMAL_MTU;
va009039 0:1ed23ab1345f 514 chan->local_mtu = mtu;
va009039 0:1ed23ab1345f 515 chan->packets_granted = 0;
va009039 0:1ed23ab1345f 516
va009039 0:1ed23ab1345f 517 // set initial state
va009039 0:1ed23ab1345f 518 chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
va009039 0:1ed23ab1345f 519 chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
va009039 0:1ed23ab1345f 520 chan->remote_sig_id = L2CAP_SIG_ID_INVALID;
va009039 0:1ed23ab1345f 521 chan->local_sig_id = L2CAP_SIG_ID_INVALID;
va009039 0:1ed23ab1345f 522
va009039 0:1ed23ab1345f 523 // add to connections list
va009039 0:1ed23ab1345f 524 linked_list_add(&l2cap_channels, (linked_item_t *) chan);
va009039 0:1ed23ab1345f 525
va009039 0:1ed23ab1345f 526 l2cap_run();
va009039 0:1ed23ab1345f 527 }
va009039 0:1ed23ab1345f 528
va009039 0:1ed23ab1345f 529 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){
va009039 0:1ed23ab1345f 530 // find channel for local_cid
va009039 0:1ed23ab1345f 531 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
va009039 0:1ed23ab1345f 532 if (channel) {
va009039 0:1ed23ab1345f 533 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
va009039 0:1ed23ab1345f 534 }
va009039 0:1ed23ab1345f 535 // process
va009039 0:1ed23ab1345f 536 l2cap_run();
va009039 0:1ed23ab1345f 537 }
va009039 0:1ed23ab1345f 538
va009039 0:1ed23ab1345f 539 static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
va009039 0:1ed23ab1345f 540 linked_item_t *it = (linked_item_t *) &l2cap_channels;
va009039 0:1ed23ab1345f 541 while (it->next){
va009039 0:1ed23ab1345f 542 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
va009039 0:1ed23ab1345f 543 if ( ! BD_ADDR_CMP( channel->address, address) ){
va009039 0:1ed23ab1345f 544 if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
va009039 0:1ed23ab1345f 545 // failure, forward error code
va009039 0:1ed23ab1345f 546 l2cap_emit_channel_opened(channel, status);
va009039 0:1ed23ab1345f 547 // discard channel
va009039 0:1ed23ab1345f 548 it->next = it->next->next;
va009039 0:1ed23ab1345f 549 btstack_memory_l2cap_channel_free(channel);
va009039 0:1ed23ab1345f 550 }
va009039 0:1ed23ab1345f 551 } else {
va009039 0:1ed23ab1345f 552 it = it->next;
va009039 0:1ed23ab1345f 553 }
va009039 0:1ed23ab1345f 554 }
va009039 0:1ed23ab1345f 555 }
va009039 0:1ed23ab1345f 556
va009039 0:1ed23ab1345f 557 static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
va009039 0:1ed23ab1345f 558 linked_item_t *it;
va009039 0:1ed23ab1345f 559 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
va009039 0:1ed23ab1345f 560 l2cap_channel_t * channel = (l2cap_channel_t *) it;
va009039 0:1ed23ab1345f 561 if ( ! BD_ADDR_CMP( channel->address, address) ){
va009039 0:1ed23ab1345f 562 if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
va009039 0:1ed23ab1345f 563 // success, start l2cap handshake
va009039 0:1ed23ab1345f 564 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
va009039 0:1ed23ab1345f 565 channel->handle = handle;
va009039 0:1ed23ab1345f 566 channel->local_cid = l2cap_next_local_cid();
va009039 0:1ed23ab1345f 567 }
va009039 0:1ed23ab1345f 568 }
va009039 0:1ed23ab1345f 569 }
va009039 0:1ed23ab1345f 570 // process
va009039 0:1ed23ab1345f 571 l2cap_run();
va009039 0:1ed23ab1345f 572 }
va009039 0:1ed23ab1345f 573
va009039 0:1ed23ab1345f 574 void l2cap_event_handler( uint8_t *packet, uint16_t size ){
va009039 0:1ed23ab1345f 575
va009039 0:1ed23ab1345f 576 bd_addr_t address;
va009039 0:1ed23ab1345f 577 hci_con_handle_t handle;
va009039 0:1ed23ab1345f 578 l2cap_channel_t * channel;
va009039 0:1ed23ab1345f 579 linked_item_t *it;
va009039 0:1ed23ab1345f 580 int hci_con_used;
va009039 0:1ed23ab1345f 581
va009039 0:1ed23ab1345f 582 switch(packet[0]){
va009039 0:1ed23ab1345f 583
va009039 0:1ed23ab1345f 584 // handle connection complete events
va009039 0:1ed23ab1345f 585 case HCI_EVENT_CONNECTION_COMPLETE:
va009039 0:1ed23ab1345f 586 bt_flip_addr(address, &packet[5]);
va009039 0:1ed23ab1345f 587 if (packet[2] == 0){
va009039 0:1ed23ab1345f 588 handle = READ_BT_16(packet, 3);
va009039 0:1ed23ab1345f 589 l2cap_handle_connection_success_for_addr(address, handle);
va009039 0:1ed23ab1345f 590 } else {
va009039 0:1ed23ab1345f 591 l2cap_handle_connection_failed_for_addr(address, packet[2]);
va009039 0:1ed23ab1345f 592 }
va009039 0:1ed23ab1345f 593 break;
va009039 0:1ed23ab1345f 594
va009039 0:1ed23ab1345f 595 // handle successful create connection cancel command
va009039 0:1ed23ab1345f 596 case HCI_EVENT_COMMAND_COMPLETE:
va009039 0:1ed23ab1345f 597 if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) {
va009039 0:1ed23ab1345f 598 if (packet[5] == 0){
va009039 0:1ed23ab1345f 599 bt_flip_addr(address, &packet[6]);
va009039 0:1ed23ab1345f 600 // CONNECTION TERMINATED BY LOCAL HOST (0X16)
va009039 0:1ed23ab1345f 601 l2cap_handle_connection_failed_for_addr(address, 0x16);
va009039 0:1ed23ab1345f 602 }
va009039 0:1ed23ab1345f 603 }
va009039 0:1ed23ab1345f 604 l2cap_run(); // try sending signaling packets first
va009039 0:1ed23ab1345f 605 break;
va009039 0:1ed23ab1345f 606
va009039 0:1ed23ab1345f 607 case HCI_EVENT_COMMAND_STATUS:
va009039 0:1ed23ab1345f 608 l2cap_run(); // try sending signaling packets first
va009039 0:1ed23ab1345f 609 break;
va009039 0:1ed23ab1345f 610
va009039 0:1ed23ab1345f 611 // handle disconnection complete events
va009039 0:1ed23ab1345f 612 case HCI_EVENT_DISCONNECTION_COMPLETE:
va009039 0:1ed23ab1345f 613 // send l2cap disconnect events for all channels on this handle
va009039 0:1ed23ab1345f 614 handle = READ_BT_16(packet, 3);
va009039 0:1ed23ab1345f 615 it = (linked_item_t *) &l2cap_channels;
va009039 0:1ed23ab1345f 616 while (it->next){
va009039 0:1ed23ab1345f 617 l2cap_channel_t * channel = (l2cap_channel_t *) it->next;
va009039 0:1ed23ab1345f 618 if ( channel->handle == handle ){
va009039 0:1ed23ab1345f 619 // update prev item before free'ing next element - don't call l2cap_finalize_channel_close
va009039 0:1ed23ab1345f 620 it->next = it->next->next;
va009039 0:1ed23ab1345f 621 l2cap_emit_channel_closed(channel);
va009039 0:1ed23ab1345f 622 btstack_memory_l2cap_channel_free(channel);
va009039 0:1ed23ab1345f 623 } else {
va009039 0:1ed23ab1345f 624 it = it->next;
va009039 0:1ed23ab1345f 625 }
va009039 0:1ed23ab1345f 626 }
va009039 0:1ed23ab1345f 627 break;
va009039 0:1ed23ab1345f 628
va009039 0:1ed23ab1345f 629 case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
va009039 0:1ed23ab1345f 630 l2cap_run(); // try sending signaling packets first
va009039 0:1ed23ab1345f 631 l2cap_hand_out_credits();
va009039 0:1ed23ab1345f 632 break;
va009039 0:1ed23ab1345f 633
va009039 0:1ed23ab1345f 634 // HCI Connection Timeouts
va009039 0:1ed23ab1345f 635 case L2CAP_EVENT_TIMEOUT_CHECK:
va009039 0:1ed23ab1345f 636 handle = READ_BT_16(packet, 2);
va009039 0:1ed23ab1345f 637 if (hci_authentication_active_for_handle(handle)) break;
va009039 0:1ed23ab1345f 638 hci_con_used = 0;
va009039 0:1ed23ab1345f 639 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
va009039 0:1ed23ab1345f 640 channel = (l2cap_channel_t *) it;
va009039 0:1ed23ab1345f 641 if (channel->handle == handle) {
va009039 0:1ed23ab1345f 642 hci_con_used = 1;
va009039 0:1ed23ab1345f 643 }
va009039 0:1ed23ab1345f 644 }
va009039 0:1ed23ab1345f 645 if (hci_con_used) break;
va009039 0:1ed23ab1345f 646 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break;
va009039 0:1ed23ab1345f 647 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
va009039 0:1ed23ab1345f 648 break;
va009039 0:1ed23ab1345f 649
va009039 0:1ed23ab1345f 650 case DAEMON_EVENT_HCI_PACKET_SENT:
va009039 0:1ed23ab1345f 651 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
va009039 0:1ed23ab1345f 652 channel = (l2cap_channel_t *) it;
va009039 0:1ed23ab1345f 653 if (channel->packet_handler) {
va009039 0:1ed23ab1345f 654 (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size);
va009039 0:1ed23ab1345f 655 }
va009039 0:1ed23ab1345f 656 }
va009039 0:1ed23ab1345f 657 if (attribute_protocol_packet_handler) {
va009039 0:1ed23ab1345f 658 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
va009039 0:1ed23ab1345f 659 }
va009039 0:1ed23ab1345f 660 if (security_protocol_packet_handler) {
va009039 0:1ed23ab1345f 661 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size);
va009039 0:1ed23ab1345f 662 }
va009039 0:1ed23ab1345f 663 break;
va009039 0:1ed23ab1345f 664
va009039 0:1ed23ab1345f 665 default:
va009039 0:1ed23ab1345f 666 break;
va009039 0:1ed23ab1345f 667 }
va009039 0:1ed23ab1345f 668
va009039 0:1ed23ab1345f 669 // pass on
va009039 0:1ed23ab1345f 670 (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size);
va009039 0:1ed23ab1345f 671 }
va009039 0:1ed23ab1345f 672
va009039 0:1ed23ab1345f 673 static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
va009039 0:1ed23ab1345f 674 channel->remote_sig_id = identifier;
va009039 0:1ed23ab1345f 675 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
va009039 0:1ed23ab1345f 676 l2cap_run();
va009039 0:1ed23ab1345f 677 }
va009039 0:1ed23ab1345f 678
va009039 0:1ed23ab1345f 679 static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
va009039 0:1ed23ab1345f 680 // Vol 3, Part A, 4.3: "The DCID and SCID fields shall be ignored when the result field indi- cates the connection was refused."
va009039 0:1ed23ab1345f 681 if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
va009039 0:1ed23ab1345f 682 signaling_responses[signaling_responses_pending].handle = handle;
va009039 0:1ed23ab1345f 683 signaling_responses[signaling_responses_pending].code = code;
va009039 0:1ed23ab1345f 684 signaling_responses[signaling_responses_pending].sig_id = sig_id;
va009039 0:1ed23ab1345f 685 signaling_responses[signaling_responses_pending].data = data;
va009039 0:1ed23ab1345f 686 signaling_responses_pending++;
va009039 0:1ed23ab1345f 687 l2cap_run();
va009039 0:1ed23ab1345f 688 }
va009039 0:1ed23ab1345f 689 }
va009039 0:1ed23ab1345f 690
va009039 0:1ed23ab1345f 691 static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
va009039 0:1ed23ab1345f 692
va009039 0:1ed23ab1345f 693 // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid);
va009039 0:1ed23ab1345f 694 l2cap_service_t *service = l2cap_get_service(psm);
va009039 0:1ed23ab1345f 695 if (!service) {
va009039 0:1ed23ab1345f 696 // 0x0002 PSM not supported
va009039 0:1ed23ab1345f 697 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
va009039 0:1ed23ab1345f 698 return;
va009039 0:1ed23ab1345f 699 }
va009039 0:1ed23ab1345f 700
va009039 0:1ed23ab1345f 701 hci_connection_t * hci_connection = connection_for_handle( handle );
va009039 0:1ed23ab1345f 702 if (!hci_connection) {
va009039 0:1ed23ab1345f 703 //
va009039 0:1ed23ab1345f 704 log_error("no hci_connection for handle %u\n", handle);
va009039 0:1ed23ab1345f 705 return;
va009039 0:1ed23ab1345f 706 }
va009039 0:1ed23ab1345f 707 // alloc structure
va009039 0:1ed23ab1345f 708 // log_info("l2cap_handle_connection_request register channel\n");
va009039 0:1ed23ab1345f 709 l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get();
va009039 0:1ed23ab1345f 710 if (!channel){
va009039 0:1ed23ab1345f 711 // 0x0004 No resources available
va009039 0:1ed23ab1345f 712 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
va009039 0:1ed23ab1345f 713 return;
va009039 0:1ed23ab1345f 714 }
va009039 0:1ed23ab1345f 715
va009039 0:1ed23ab1345f 716 // fill in
va009039 0:1ed23ab1345f 717 BD_ADDR_COPY(channel->address, hci_connection->address);
va009039 0:1ed23ab1345f 718 channel->psm = psm;
va009039 0:1ed23ab1345f 719 channel->handle = handle;
va009039 0:1ed23ab1345f 720 channel->connection = service->connection;
va009039 0:1ed23ab1345f 721 channel->packet_handler = service->packet_handler;
va009039 0:1ed23ab1345f 722 channel->local_cid = l2cap_next_local_cid();
va009039 0:1ed23ab1345f 723 channel->remote_cid = source_cid;
va009039 0:1ed23ab1345f 724 channel->local_mtu = service->mtu;
va009039 0:1ed23ab1345f 725 channel->remote_mtu = L2CAP_DEFAULT_MTU;
va009039 0:1ed23ab1345f 726 channel->packets_granted = 0;
va009039 0:1ed23ab1345f 727 channel->remote_sig_id = sig_id;
va009039 0:1ed23ab1345f 728
va009039 0:1ed23ab1345f 729 // limit local mtu to max acl packet length
va009039 0:1ed23ab1345f 730 if (channel->local_mtu > l2cap_max_mtu()) {
va009039 0:1ed23ab1345f 731 channel->local_mtu = l2cap_max_mtu();
va009039 0:1ed23ab1345f 732 }
va009039 0:1ed23ab1345f 733
va009039 0:1ed23ab1345f 734 // set initial state
va009039 0:1ed23ab1345f 735 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
va009039 0:1ed23ab1345f 736 channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
va009039 0:1ed23ab1345f 737
va009039 0:1ed23ab1345f 738 // add to connections list
va009039 0:1ed23ab1345f 739 linked_list_add(&l2cap_channels, (linked_item_t *) channel);
va009039 0:1ed23ab1345f 740
va009039 0:1ed23ab1345f 741 // emit incoming connection request
va009039 0:1ed23ab1345f 742 l2cap_emit_connection_request(channel);
va009039 0:1ed23ab1345f 743 }
va009039 0:1ed23ab1345f 744
va009039 0:1ed23ab1345f 745 void l2cap_accept_connection_internal(uint16_t local_cid){
va009039 0:1ed23ab1345f 746 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
va009039 0:1ed23ab1345f 747 if (!channel) {
va009039 0:1ed23ab1345f 748 log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid);
va009039 0:1ed23ab1345f 749 return;
va009039 0:1ed23ab1345f 750 }
va009039 0:1ed23ab1345f 751
va009039 0:1ed23ab1345f 752 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
va009039 0:1ed23ab1345f 753
va009039 0:1ed23ab1345f 754 // process
va009039 0:1ed23ab1345f 755 l2cap_run();
va009039 0:1ed23ab1345f 756 }
va009039 0:1ed23ab1345f 757
va009039 0:1ed23ab1345f 758 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){
va009039 0:1ed23ab1345f 759 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
va009039 0:1ed23ab1345f 760 if (!channel) {
va009039 0:1ed23ab1345f 761 log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid);
va009039 0:1ed23ab1345f 762 return;
va009039 0:1ed23ab1345f 763 }
va009039 0:1ed23ab1345f 764 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
va009039 0:1ed23ab1345f 765 channel->reason = reason;
va009039 0:1ed23ab1345f 766 l2cap_run();
va009039 0:1ed23ab1345f 767 }
va009039 0:1ed23ab1345f 768
va009039 0:1ed23ab1345f 769 void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
va009039 0:1ed23ab1345f 770
va009039 0:1ed23ab1345f 771 channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
va009039 0:1ed23ab1345f 772
va009039 0:1ed23ab1345f 773 // accept the other's configuration options
va009039 0:1ed23ab1345f 774 uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
va009039 0:1ed23ab1345f 775 uint16_t pos = 8;
va009039 0:1ed23ab1345f 776 while (pos < end_pos){
va009039 0:1ed23ab1345f 777 uint8_t type = command[pos++];
va009039 0:1ed23ab1345f 778 uint8_t length = command[pos++];
va009039 0:1ed23ab1345f 779 // MTU { type(8): 1, len(8):2, MTU(16) }
va009039 0:1ed23ab1345f 780 if ((type & 0x7f) == 1 && length == 2){
va009039 0:1ed23ab1345f 781 channel->remote_mtu = READ_BT_16(command, pos);
va009039 0:1ed23ab1345f 782 // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu);
va009039 0:1ed23ab1345f 783 }
va009039 0:1ed23ab1345f 784 pos += length;
va009039 0:1ed23ab1345f 785 }
va009039 0:1ed23ab1345f 786 }
va009039 0:1ed23ab1345f 787
va009039 0:1ed23ab1345f 788 static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
va009039 0:1ed23ab1345f 789 // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var);
va009039 0:1ed23ab1345f 790 if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
va009039 0:1ed23ab1345f 791 if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
va009039 0:1ed23ab1345f 792 return 1;
va009039 0:1ed23ab1345f 793 }
va009039 0:1ed23ab1345f 794
va009039 0:1ed23ab1345f 795
va009039 0:1ed23ab1345f 796 void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
va009039 0:1ed23ab1345f 797
va009039 0:1ed23ab1345f 798 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
va009039 0:1ed23ab1345f 799 uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
va009039 0:1ed23ab1345f 800 uint16_t result = 0;
va009039 0:1ed23ab1345f 801
va009039 0:1ed23ab1345f 802 log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state);
va009039 0:1ed23ab1345f 803
va009039 0:1ed23ab1345f 804 // handle DISCONNECT REQUESTS seperately
va009039 0:1ed23ab1345f 805 if (code == DISCONNECTION_REQUEST){
va009039 0:1ed23ab1345f 806 switch (channel->state){
va009039 0:1ed23ab1345f 807 case L2CAP_STATE_CONFIG:
va009039 0:1ed23ab1345f 808 case L2CAP_STATE_OPEN:
va009039 0:1ed23ab1345f 809 case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
va009039 0:1ed23ab1345f 810 case L2CAP_STATE_WAIT_DISCONNECT:
va009039 0:1ed23ab1345f 811 l2cap_handle_disconnect_request(channel, identifier);
va009039 0:1ed23ab1345f 812 break;
va009039 0:1ed23ab1345f 813
va009039 0:1ed23ab1345f 814 default:
va009039 0:1ed23ab1345f 815 // ignore in other states
va009039 0:1ed23ab1345f 816 break;
va009039 0:1ed23ab1345f 817 }
va009039 0:1ed23ab1345f 818 return;
va009039 0:1ed23ab1345f 819 }
va009039 0:1ed23ab1345f 820
va009039 0:1ed23ab1345f 821 // @STATEMACHINE(l2cap)
va009039 0:1ed23ab1345f 822 switch (channel->state) {
va009039 0:1ed23ab1345f 823
va009039 0:1ed23ab1345f 824 case L2CAP_STATE_WAIT_CONNECT_RSP:
va009039 0:1ed23ab1345f 825 switch (code){
va009039 0:1ed23ab1345f 826 case CONNECTION_RESPONSE:
va009039 0:1ed23ab1345f 827 result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
va009039 0:1ed23ab1345f 828 switch (result) {
va009039 0:1ed23ab1345f 829 case 0:
va009039 0:1ed23ab1345f 830 // successful connection
va009039 0:1ed23ab1345f 831 channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
va009039 0:1ed23ab1345f 832 channel->state = L2CAP_STATE_CONFIG;
va009039 0:1ed23ab1345f 833 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
va009039 0:1ed23ab1345f 834 break;
va009039 0:1ed23ab1345f 835 case 1:
va009039 0:1ed23ab1345f 836 // connection pending. get some coffee
va009039 0:1ed23ab1345f 837 break;
va009039 0:1ed23ab1345f 838 default:
va009039 0:1ed23ab1345f 839 // channel closed
va009039 0:1ed23ab1345f 840 channel->state = L2CAP_STATE_CLOSED;
va009039 0:1ed23ab1345f 841
va009039 0:1ed23ab1345f 842 // map l2cap connection response result to BTstack status enumeration
va009039 0:1ed23ab1345f 843 l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
va009039 0:1ed23ab1345f 844
va009039 0:1ed23ab1345f 845 // drop link key if security block
va009039 0:1ed23ab1345f 846 if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
va009039 0:1ed23ab1345f 847 hci_drop_link_key_for_bd_addr(&channel->address);
va009039 0:1ed23ab1345f 848 }
va009039 0:1ed23ab1345f 849
va009039 0:1ed23ab1345f 850 // discard channel
va009039 0:1ed23ab1345f 851 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
va009039 0:1ed23ab1345f 852 btstack_memory_l2cap_channel_free(channel);
va009039 0:1ed23ab1345f 853 break;
va009039 0:1ed23ab1345f 854 }
va009039 0:1ed23ab1345f 855 break;
va009039 0:1ed23ab1345f 856
va009039 0:1ed23ab1345f 857 default:
va009039 0:1ed23ab1345f 858 //@TODO: implement other signaling packets
va009039 0:1ed23ab1345f 859 break;
va009039 0:1ed23ab1345f 860 }
va009039 0:1ed23ab1345f 861 break;
va009039 0:1ed23ab1345f 862
va009039 0:1ed23ab1345f 863 case L2CAP_STATE_CONFIG:
va009039 0:1ed23ab1345f 864 switch (code) {
va009039 0:1ed23ab1345f 865 case CONFIGURE_REQUEST:
va009039 0:1ed23ab1345f 866 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
va009039 0:1ed23ab1345f 867 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
va009039 0:1ed23ab1345f 868 l2cap_signaling_handle_configure_request(channel, command);
va009039 0:1ed23ab1345f 869 break;
va009039 0:1ed23ab1345f 870 case CONFIGURE_RESPONSE:
va009039 0:1ed23ab1345f 871 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
va009039 0:1ed23ab1345f 872 break;
va009039 0:1ed23ab1345f 873 default:
va009039 0:1ed23ab1345f 874 break;
va009039 0:1ed23ab1345f 875 }
va009039 0:1ed23ab1345f 876 if (l2cap_channel_ready_for_open(channel)){
va009039 0:1ed23ab1345f 877 // for open:
va009039 0:1ed23ab1345f 878 channel->state = L2CAP_STATE_OPEN;
va009039 0:1ed23ab1345f 879 l2cap_emit_channel_opened(channel, 0);
va009039 0:1ed23ab1345f 880 l2cap_emit_credits(channel, 1);
va009039 0:1ed23ab1345f 881 }
va009039 0:1ed23ab1345f 882 break;
va009039 0:1ed23ab1345f 883
va009039 0:1ed23ab1345f 884 case L2CAP_STATE_WAIT_DISCONNECT:
va009039 0:1ed23ab1345f 885 switch (code) {
va009039 0:1ed23ab1345f 886 case DISCONNECTION_RESPONSE:
va009039 0:1ed23ab1345f 887 l2cap_finialize_channel_close(channel);
va009039 0:1ed23ab1345f 888 break;
va009039 0:1ed23ab1345f 889 default:
va009039 0:1ed23ab1345f 890 //@TODO: implement other signaling packets
va009039 0:1ed23ab1345f 891 break;
va009039 0:1ed23ab1345f 892 }
va009039 0:1ed23ab1345f 893 break;
va009039 0:1ed23ab1345f 894
va009039 0:1ed23ab1345f 895 case L2CAP_STATE_CLOSED:
va009039 0:1ed23ab1345f 896 // @TODO handle incoming requests
va009039 0:1ed23ab1345f 897 break;
va009039 0:1ed23ab1345f 898
va009039 0:1ed23ab1345f 899 case L2CAP_STATE_OPEN:
va009039 0:1ed23ab1345f 900 //@TODO: implement other signaling packets, e.g. re-configure
va009039 0:1ed23ab1345f 901 break;
va009039 0:1ed23ab1345f 902 default:
va009039 0:1ed23ab1345f 903 break;
va009039 0:1ed23ab1345f 904 }
va009039 0:1ed23ab1345f 905 // log_info("new state %u\n", channel->state);
va009039 0:1ed23ab1345f 906 }
va009039 0:1ed23ab1345f 907
va009039 0:1ed23ab1345f 908
va009039 0:1ed23ab1345f 909 void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
va009039 0:1ed23ab1345f 910
va009039 0:1ed23ab1345f 911 // get code, signalind identifier and command len
va009039 0:1ed23ab1345f 912 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
va009039 0:1ed23ab1345f 913 uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
va009039 0:1ed23ab1345f 914
va009039 0:1ed23ab1345f 915 // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
va009039 0:1ed23ab1345f 916 if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
va009039 0:1ed23ab1345f 917 return;
va009039 0:1ed23ab1345f 918 }
va009039 0:1ed23ab1345f 919
va009039 0:1ed23ab1345f 920 // general commands without an assigned channel
va009039 0:1ed23ab1345f 921 switch(code) {
va009039 0:1ed23ab1345f 922
va009039 0:1ed23ab1345f 923 case CONNECTION_REQUEST: {
va009039 0:1ed23ab1345f 924 uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
va009039 0:1ed23ab1345f 925 uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
va009039 0:1ed23ab1345f 926 l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
va009039 0:1ed23ab1345f 927 return;
va009039 0:1ed23ab1345f 928 }
va009039 0:1ed23ab1345f 929
va009039 0:1ed23ab1345f 930 case ECHO_REQUEST:
va009039 0:1ed23ab1345f 931 l2cap_register_signaling_response(handle, code, sig_id, 0);
va009039 0:1ed23ab1345f 932 return;
va009039 0:1ed23ab1345f 933
va009039 0:1ed23ab1345f 934 case INFORMATION_REQUEST: {
va009039 0:1ed23ab1345f 935 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
va009039 0:1ed23ab1345f 936 l2cap_register_signaling_response(handle, code, sig_id, infoType);
va009039 0:1ed23ab1345f 937 return;
va009039 0:1ed23ab1345f 938 }
va009039 0:1ed23ab1345f 939
va009039 0:1ed23ab1345f 940 default:
va009039 0:1ed23ab1345f 941 break;
va009039 0:1ed23ab1345f 942 }
va009039 0:1ed23ab1345f 943
va009039 0:1ed23ab1345f 944
va009039 0:1ed23ab1345f 945 // Get potential destination CID
va009039 0:1ed23ab1345f 946 uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
va009039 0:1ed23ab1345f 947
va009039 0:1ed23ab1345f 948 // Find channel for this sig_id and connection handle
va009039 0:1ed23ab1345f 949 linked_item_t *it;
va009039 0:1ed23ab1345f 950 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
va009039 0:1ed23ab1345f 951 l2cap_channel_t * channel = (l2cap_channel_t *) it;
va009039 0:1ed23ab1345f 952 if (channel->handle == handle) {
va009039 0:1ed23ab1345f 953 if (code & 1) {
va009039 0:1ed23ab1345f 954 // match odd commands (responses) by previous signaling identifier
va009039 0:1ed23ab1345f 955 if (channel->local_sig_id == sig_id) {
va009039 0:1ed23ab1345f 956 l2cap_signaling_handler_channel(channel, command);
va009039 0:1ed23ab1345f 957 break;
va009039 0:1ed23ab1345f 958 }
va009039 0:1ed23ab1345f 959 } else {
va009039 0:1ed23ab1345f 960 // match even commands (requests) by local channel id
va009039 0:1ed23ab1345f 961 if (channel->local_cid == dest_cid) {
va009039 0:1ed23ab1345f 962 l2cap_signaling_handler_channel(channel, command);
va009039 0:1ed23ab1345f 963 break;
va009039 0:1ed23ab1345f 964 }
va009039 0:1ed23ab1345f 965 }
va009039 0:1ed23ab1345f 966 }
va009039 0:1ed23ab1345f 967 }
va009039 0:1ed23ab1345f 968 }
va009039 0:1ed23ab1345f 969
va009039 0:1ed23ab1345f 970 void l2cap_acl_handler( uint8_t *packet, uint16_t size ){
va009039 0:1ed23ab1345f 971
va009039 0:1ed23ab1345f 972 // Get Channel ID
va009039 0:1ed23ab1345f 973 uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
va009039 0:1ed23ab1345f 974 hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
va009039 0:1ed23ab1345f 975
va009039 0:1ed23ab1345f 976 switch (channel_id) {
va009039 0:1ed23ab1345f 977
va009039 0:1ed23ab1345f 978 case L2CAP_CID_SIGNALING: {
va009039 0:1ed23ab1345f 979
va009039 0:1ed23ab1345f 980 uint16_t command_offset = 8;
va009039 0:1ed23ab1345f 981 while (command_offset < size) {
va009039 0:1ed23ab1345f 982
va009039 0:1ed23ab1345f 983 // handle signaling commands
va009039 0:1ed23ab1345f 984 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
va009039 0:1ed23ab1345f 985
va009039 0:1ed23ab1345f 986 // increment command_offset
va009039 0:1ed23ab1345f 987 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
va009039 0:1ed23ab1345f 988 }
va009039 0:1ed23ab1345f 989 break;
va009039 0:1ed23ab1345f 990 }
va009039 0:1ed23ab1345f 991
va009039 0:1ed23ab1345f 992 case L2CAP_CID_ATTRIBUTE_PROTOCOL:
va009039 0:1ed23ab1345f 993 if (attribute_protocol_packet_handler) {
va009039 0:1ed23ab1345f 994 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
va009039 0:1ed23ab1345f 995 }
va009039 0:1ed23ab1345f 996 break;
va009039 0:1ed23ab1345f 997
va009039 0:1ed23ab1345f 998 case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
va009039 0:1ed23ab1345f 999 if (security_protocol_packet_handler) {
va009039 0:1ed23ab1345f 1000 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
va009039 0:1ed23ab1345f 1001 }
va009039 0:1ed23ab1345f 1002 break;
va009039 0:1ed23ab1345f 1003
va009039 0:1ed23ab1345f 1004 default: {
va009039 0:1ed23ab1345f 1005 // Find channel for this channel_id and connection handle
va009039 0:1ed23ab1345f 1006 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id);
va009039 0:1ed23ab1345f 1007 if (channel) {
va009039 0:1ed23ab1345f 1008 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
va009039 0:1ed23ab1345f 1009 }
va009039 0:1ed23ab1345f 1010 break;
va009039 0:1ed23ab1345f 1011 }
va009039 0:1ed23ab1345f 1012 }
va009039 0:1ed23ab1345f 1013
va009039 0:1ed23ab1345f 1014 l2cap_run();
va009039 0:1ed23ab1345f 1015 }
va009039 0:1ed23ab1345f 1016
va009039 0:1ed23ab1345f 1017 static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
va009039 0:1ed23ab1345f 1018 switch (packet_type) {
va009039 0:1ed23ab1345f 1019 case HCI_EVENT_PACKET:
va009039 0:1ed23ab1345f 1020 l2cap_event_handler(packet, size);
va009039 0:1ed23ab1345f 1021 break;
va009039 0:1ed23ab1345f 1022 case HCI_ACL_DATA_PACKET:
va009039 0:1ed23ab1345f 1023 l2cap_acl_handler(packet, size);
va009039 0:1ed23ab1345f 1024 break;
va009039 0:1ed23ab1345f 1025 default:
va009039 0:1ed23ab1345f 1026 break;
va009039 0:1ed23ab1345f 1027 }
va009039 0:1ed23ab1345f 1028 }
va009039 0:1ed23ab1345f 1029
va009039 0:1ed23ab1345f 1030 // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
va009039 0:1ed23ab1345f 1031 void l2cap_finialize_channel_close(l2cap_channel_t *channel){
va009039 0:1ed23ab1345f 1032 channel->state = L2CAP_STATE_CLOSED;
va009039 0:1ed23ab1345f 1033 l2cap_emit_channel_closed(channel);
va009039 0:1ed23ab1345f 1034 // discard channel
va009039 0:1ed23ab1345f 1035 linked_list_remove(&l2cap_channels, (linked_item_t *) channel);
va009039 0:1ed23ab1345f 1036 btstack_memory_l2cap_channel_free(channel);
va009039 0:1ed23ab1345f 1037 }
va009039 0:1ed23ab1345f 1038
va009039 0:1ed23ab1345f 1039 l2cap_service_t * l2cap_get_service(uint16_t psm){
va009039 0:1ed23ab1345f 1040 linked_item_t *it;
va009039 0:1ed23ab1345f 1041
va009039 0:1ed23ab1345f 1042 // close open channels
va009039 0:1ed23ab1345f 1043 for (it = (linked_item_t *) l2cap_services; it ; it = it->next){
va009039 0:1ed23ab1345f 1044 l2cap_service_t * service = ((l2cap_service_t *) it);
va009039 0:1ed23ab1345f 1045 if ( service->psm == psm){
va009039 0:1ed23ab1345f 1046 return service;
va009039 0:1ed23ab1345f 1047 };
va009039 0:1ed23ab1345f 1048 }
va009039 0:1ed23ab1345f 1049 return NULL;
va009039 0:1ed23ab1345f 1050 }
va009039 0:1ed23ab1345f 1051
va009039 0:1ed23ab1345f 1052 void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){
va009039 0:1ed23ab1345f 1053 // check for alread registered psm
va009039 0:1ed23ab1345f 1054 // TODO: emit error event
va009039 0:1ed23ab1345f 1055 l2cap_service_t *service = l2cap_get_service(psm);
va009039 0:1ed23ab1345f 1056 if (service) {
va009039 0:1ed23ab1345f 1057 log_error("l2cap_register_service_internal: PSM %u already registered\n", psm);
va009039 0:1ed23ab1345f 1058 l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm);
va009039 0:1ed23ab1345f 1059 return;
va009039 0:1ed23ab1345f 1060 }
va009039 0:1ed23ab1345f 1061
va009039 0:1ed23ab1345f 1062 // alloc structure
va009039 0:1ed23ab1345f 1063 // TODO: emit error event
va009039 0:1ed23ab1345f 1064 service = (l2cap_service_t *) btstack_memory_l2cap_service_get();
va009039 0:1ed23ab1345f 1065 if (!service) {
va009039 0:1ed23ab1345f 1066 log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n");
va009039 0:1ed23ab1345f 1067 l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm);
va009039 0:1ed23ab1345f 1068 return;
va009039 0:1ed23ab1345f 1069 }
va009039 0:1ed23ab1345f 1070
va009039 0:1ed23ab1345f 1071 // fill in
va009039 0:1ed23ab1345f 1072 service->psm = psm;
va009039 0:1ed23ab1345f 1073 service->mtu = mtu;
va009039 0:1ed23ab1345f 1074 service->connection = connection;
va009039 0:1ed23ab1345f 1075 service->packet_handler = packet_handler;
va009039 0:1ed23ab1345f 1076
va009039 0:1ed23ab1345f 1077 // add to services list
va009039 0:1ed23ab1345f 1078 linked_list_add(&l2cap_services, (linked_item_t *) service);
va009039 0:1ed23ab1345f 1079
va009039 0:1ed23ab1345f 1080 // enable page scan
va009039 0:1ed23ab1345f 1081 hci_connectable_control(1);
va009039 0:1ed23ab1345f 1082
va009039 0:1ed23ab1345f 1083 // done
va009039 0:1ed23ab1345f 1084 l2cap_emit_service_registered(connection, 0, psm);
va009039 0:1ed23ab1345f 1085 }
va009039 0:1ed23ab1345f 1086
va009039 0:1ed23ab1345f 1087 void l2cap_unregister_service_internal(void *connection, uint16_t psm){
va009039 0:1ed23ab1345f 1088 l2cap_service_t *service = l2cap_get_service(psm);
va009039 0:1ed23ab1345f 1089 if (!service) return;
va009039 0:1ed23ab1345f 1090 linked_list_remove(&l2cap_services, (linked_item_t *) service);
va009039 0:1ed23ab1345f 1091 btstack_memory_l2cap_service_free(service);
va009039 0:1ed23ab1345f 1092
va009039 0:1ed23ab1345f 1093 // disable page scan when no services registered
va009039 0:1ed23ab1345f 1094 if (!linked_list_empty(&l2cap_services)) return;
va009039 0:1ed23ab1345f 1095 hci_connectable_control(0);
va009039 0:1ed23ab1345f 1096 }
va009039 0:1ed23ab1345f 1097
va009039 0:1ed23ab1345f 1098 //
va009039 0:1ed23ab1345f 1099 void l2cap_close_connection(void *connection){
va009039 0:1ed23ab1345f 1100 linked_item_t *it;
va009039 0:1ed23ab1345f 1101
va009039 0:1ed23ab1345f 1102 // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks
va009039 0:1ed23ab1345f 1103 l2cap_channel_t * channel;
va009039 0:1ed23ab1345f 1104 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){
va009039 0:1ed23ab1345f 1105 channel = (l2cap_channel_t *) it;
va009039 0:1ed23ab1345f 1106 if (channel->connection == connection) {
va009039 0:1ed23ab1345f 1107 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
va009039 0:1ed23ab1345f 1108 }
va009039 0:1ed23ab1345f 1109 }
va009039 0:1ed23ab1345f 1110
va009039 0:1ed23ab1345f 1111 // unregister services
va009039 0:1ed23ab1345f 1112 it = (linked_item_t *) &l2cap_services;
va009039 0:1ed23ab1345f 1113 while (it->next) {
va009039 0:1ed23ab1345f 1114 l2cap_service_t * service = (l2cap_service_t *) it->next;
va009039 0:1ed23ab1345f 1115 if (service->connection == connection){
va009039 0:1ed23ab1345f 1116 it->next = it->next->next;
va009039 0:1ed23ab1345f 1117 btstack_memory_l2cap_service_free(service);
va009039 0:1ed23ab1345f 1118 } else {
va009039 0:1ed23ab1345f 1119 it = it->next;
va009039 0:1ed23ab1345f 1120 }
va009039 0:1ed23ab1345f 1121 }
va009039 0:1ed23ab1345f 1122
va009039 0:1ed23ab1345f 1123 // process
va009039 0:1ed23ab1345f 1124 l2cap_run();
va009039 0:1ed23ab1345f 1125 }
va009039 0:1ed23ab1345f 1126
va009039 0:1ed23ab1345f 1127 // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
va009039 0:1ed23ab1345f 1128 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
va009039 0:1ed23ab1345f 1129 switch(channel_id){
va009039 0:1ed23ab1345f 1130 case L2CAP_CID_ATTRIBUTE_PROTOCOL:
va009039 0:1ed23ab1345f 1131 attribute_protocol_packet_handler = packet_handler;
va009039 0:1ed23ab1345f 1132 break;
va009039 0:1ed23ab1345f 1133 case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
va009039 0:1ed23ab1345f 1134 security_protocol_packet_handler = packet_handler;
va009039 0:1ed23ab1345f 1135 break;
va009039 0:1ed23ab1345f 1136 }
va009039 0:1ed23ab1345f 1137 }
va009039 0:1ed23ab1345f 1138