1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define __BTSTACK_FILE__ "rfcomm.c" 39 40 /* 41 * rfcomm.c 42 */ 43 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> // memcpy 47 #include <stdint.h> 48 49 #include "bluetooth_sdp.h" 50 #include "btstack_debug.h" 51 #include "btstack_event.h" 52 #include "btstack_memory.h" 53 #include "btstack_util.h" 54 #include "classic/core.h" 55 #include "classic/rfcomm.h" 56 #include "hci.h" 57 #include "hci_cmd.h" 58 #include "hci_dump.h" 59 #include "l2cap.h" 60 61 // workaround for missing PRIxPTR on mspgcc (16/20-bit MCU) 62 #ifndef PRIxPTR 63 #if defined(__MSP430X__) && defined(__MSP430X_LARGE__) 64 #define PRIxPTR "lx" 65 #else 66 #define PRIxPTR "x" 67 #endif 68 #endif 69 70 #define RFCOMM_MULIPLEXER_TIMEOUT_MS 60000 71 72 #define RFCOMM_CREDITS 10 73 74 // FCS calc 75 #define BT_RFCOMM_CODE_WORD 0xE0 // pol = x8+x2+x1+1 76 #define BT_RFCOMM_CRC_CHECK_LEN 3 77 #define BT_RFCOMM_UIHCRC_CHECK_LEN 2 78 79 80 typedef enum { 81 CH_EVT_RCVD_SABM = 1, 82 CH_EVT_RCVD_UA, 83 CH_EVT_RCVD_PN, 84 CH_EVT_RCVD_PN_RSP, 85 CH_EVT_RCVD_DISC, 86 CH_EVT_RCVD_DM, 87 CH_EVT_RCVD_MSC_CMD, 88 CH_EVT_RCVD_MSC_RSP, 89 CH_EVT_RCVD_NSC_RSP, 90 CH_EVT_RCVD_RLS_CMD, 91 CH_EVT_RCVD_RLS_RSP, 92 CH_EVT_RCVD_RPN_CMD, 93 CH_EVT_RCVD_RPN_REQ, 94 CH_EVT_RCVD_CREDITS, 95 CH_EVT_MULTIPLEXER_READY, 96 CH_EVT_READY_TO_SEND, 97 } RFCOMM_CHANNEL_EVENT; 98 99 typedef struct rfcomm_channel_event { 100 RFCOMM_CHANNEL_EVENT type; 101 uint16_t dummy; // force rfcomm_channel_event to be 2-byte aligned -> avoid -Wcast-align warning 102 } rfcomm_channel_event_t; 103 104 typedef struct rfcomm_channel_event_pn { 105 rfcomm_channel_event_t super; 106 uint16_t max_frame_size; 107 uint8_t priority; 108 uint8_t credits_outgoing; 109 } rfcomm_channel_event_pn_t; 110 111 typedef struct rfcomm_channel_event_rpn { 112 rfcomm_channel_event_t super; 113 rfcomm_rpn_data_t data; 114 } rfcomm_channel_event_rpn_t; 115 116 typedef struct rfcomm_channel_event_rls { 117 rfcomm_channel_event_t super; 118 uint8_t line_status; 119 } rfcomm_channel_event_rls_t; 120 121 typedef struct rfcomm_channel_event_msc { 122 rfcomm_channel_event_t super; 123 uint8_t modem_status; 124 } rfcomm_channel_event_msc_t; 125 126 127 // global rfcomm data 128 static uint16_t rfcomm_client_cid_generator; // used for client channel IDs 129 130 // linked lists for all 131 static btstack_linked_list_t rfcomm_multiplexers = NULL; 132 static btstack_linked_list_t rfcomm_channels = NULL; 133 static btstack_linked_list_t rfcomm_services = NULL; 134 135 static gap_security_level_t rfcomm_security_level; 136 137 static int rfcomm_channel_can_send(rfcomm_channel_t * channel); 138 static int rfcomm_channel_ready_for_open(rfcomm_channel_t *channel); 139 static int rfcomm_channel_ready_to_send(rfcomm_channel_t * channel); 140 static void rfcomm_channel_state_machine_with_channel(rfcomm_channel_t *channel, const rfcomm_channel_event_t *event, int * out_channel_valid); 141 static void rfcomm_channel_state_machine_with_dlci(rfcomm_multiplexer_t * multiplexer, uint8_t dlci, const rfcomm_channel_event_t *event); 142 static void rfcomm_emit_can_send_now(rfcomm_channel_t *channel); 143 static int rfcomm_multiplexer_ready_to_send(rfcomm_multiplexer_t * multiplexer); 144 static void rfcomm_multiplexer_state_machine(rfcomm_multiplexer_t * multiplexer, RFCOMM_MULTIPLEXER_EVENT event); 145 146 // MARK: RFCOMM CLIENT EVENTS 147 148 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 149 static void rfcomm_emit_connection_request(rfcomm_channel_t *channel) { 150 log_info("RFCOMM_EVENT_INCOMING_CONNECTION addr %s channel #%u cid 0x%02x", 151 bd_addr_to_str(channel->multiplexer->remote_addr), channel->dlci>>1, channel->rfcomm_cid); 152 uint8_t event[11]; 153 event[0] = RFCOMM_EVENT_INCOMING_CONNECTION; 154 event[1] = sizeof(event) - 2; 155 reverse_bd_addr(channel->multiplexer->remote_addr, &event[2]); 156 event[8] = channel->dlci >> 1; 157 little_endian_store_16(event, 9, channel->rfcomm_cid); 158 hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 159 (channel->packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 160 } 161 162 // API Change: BTstack-0.3.50x uses 163 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 164 // next Cydia release will use SVN version of this 165 // data: event(8), len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16) 166 static void rfcomm_emit_channel_opened(rfcomm_channel_t *channel, uint8_t status) { 167 log_info("RFCOMM_EVENT_CHANNEL_OPENED status 0x%x addr %s handle 0x%x channel #%u cid 0x%02x mtu %u", 168 status, bd_addr_to_str(channel->multiplexer->remote_addr), channel->multiplexer->con_handle, 169 channel->dlci>>1, channel->rfcomm_cid, channel->max_frame_size); 170 uint8_t event[18]; 171 uint8_t pos = 0; 172 event[pos++] = RFCOMM_EVENT_CHANNEL_OPENED; // 0 173 event[pos++] = sizeof(event) - 2; // 1 174 event[pos++] = status; // 2 175 reverse_bd_addr(channel->multiplexer->remote_addr, &event[pos]); pos += 6; // 3 176 little_endian_store_16(event, pos, channel->multiplexer->con_handle); pos += 2; // 9 177 event[pos++] = channel->dlci >> 1; // 11 178 little_endian_store_16(event, pos, channel->rfcomm_cid); pos += 2; // 12 - channel ID 179 little_endian_store_16(event, pos, channel->max_frame_size); pos += 2; // max frame size 180 event[pos++] = channel->service ? 1 : 0; // linked to service -> incoming 181 hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 182 (channel->packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 183 184 // if channel opened successfully, also send can send now if possible 185 if (status) return; 186 if (rfcomm_channel_can_send(channel)){ 187 rfcomm_emit_can_send_now(channel); 188 } 189 } 190 191 // data: event(8), len(8), rfcomm_cid(16) 192 static void rfcomm_emit_channel_closed(rfcomm_channel_t * channel) { 193 log_info("RFCOMM_EVENT_CHANNEL_CLOSED cid 0x%02x", channel->rfcomm_cid); 194 uint8_t event[4]; 195 event[0] = RFCOMM_EVENT_CHANNEL_CLOSED; 196 event[1] = sizeof(event) - 2; 197 little_endian_store_16(event, 2, channel->rfcomm_cid); 198 hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 199 (channel->packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 200 } 201 202 static void rfcomm_emit_remote_line_status(rfcomm_channel_t *channel, uint8_t line_status){ 203 log_info("RFCOMM_EVENT_REMOTE_LINE_STATUS cid 0x%02x c, line status 0x%x", channel->rfcomm_cid, line_status); 204 uint8_t event[5]; 205 event[0] = RFCOMM_EVENT_REMOTE_LINE_STATUS; 206 event[1] = sizeof(event) - 2; 207 little_endian_store_16(event, 2, channel->rfcomm_cid); 208 event[4] = line_status; 209 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 210 (channel->packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 211 } 212 213 static void rfcomm_emit_port_configuration(rfcomm_channel_t *channel){ 214 // notify client about new settings 215 uint8_t event[2+sizeof(rfcomm_rpn_data_t)]; 216 event[0] = RFCOMM_EVENT_PORT_CONFIGURATION; 217 event[1] = sizeof(rfcomm_rpn_data_t); 218 memcpy(&event[2], (uint8_t*) &channel->rpn_data, sizeof(rfcomm_rpn_data_t)); 219 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 220 (channel->packet_handler)(HCI_EVENT_PACKET, channel->rfcomm_cid, event, sizeof(event)); 221 } 222 223 static void rfcomm_emit_can_send_now(rfcomm_channel_t *channel) { 224 log_debug("RFCOMM_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel->rfcomm_cid); 225 uint8_t event[4]; 226 event[0] = RFCOMM_EVENT_CAN_SEND_NOW; 227 event[1] = sizeof(event) - 2; 228 little_endian_store_16(event, 2, channel->rfcomm_cid); 229 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 230 (channel->packet_handler)(HCI_EVENT_PACKET, channel->rfcomm_cid, event, sizeof(event)); 231 } 232 233 // MARK RFCOMM RPN DATA HELPER 234 static void rfcomm_rpn_data_set_defaults(rfcomm_rpn_data_t * rpn_data){ 235 rpn_data->baud_rate = RPN_BAUD_9600; /* 9600 bps */ 236 rpn_data->flags = 0x03; /* 8-n-1 */ 237 rpn_data->flow_control = 0; /* no flow control */ 238 rpn_data->xon = 0xd1; /* XON */ 239 rpn_data->xoff = 0xd3; /* XOFF */ 240 rpn_data->parameter_mask_0 = 0x7f; /* parameter mask, all values set */ 241 rpn_data->parameter_mask_1 = 0x3f; /* parameter mask, all values set */ 242 } 243 244 static void rfcomm_rpn_data_update(rfcomm_rpn_data_t * dest, rfcomm_rpn_data_t * src){ 245 if (src->parameter_mask_0 & RPN_PARAM_MASK_0_BAUD){ 246 dest->baud_rate = src->baud_rate; 247 } 248 if (src->parameter_mask_0 & RPN_PARAM_MASK_0_DATA_BITS){ 249 dest->flags = (dest->flags & 0xfc) | (src->flags & 0x03); 250 } 251 if (src->parameter_mask_0 & RPN_PARAM_MASK_0_STOP_BITS){ 252 dest->flags = (dest->flags & 0xfb) | (src->flags & 0x04); 253 } 254 if (src->parameter_mask_0 & RPN_PARAM_MASK_0_PARITY){ 255 dest->flags = (dest->flags & 0xf7) | (src->flags & 0x08); 256 } 257 if (src->parameter_mask_0 & RPN_PARAM_MASK_0_PARITY_TYPE){ 258 dest->flags = (dest->flags & 0xfc) | (src->flags & 0x30); 259 } 260 if (src->parameter_mask_0 & RPN_PARAM_MASK_0_XON_CHAR){ 261 dest->xon = src->xon; 262 } 263 if (src->parameter_mask_0 & RPN_PARAM_MASK_0_XOFF_CHAR){ 264 dest->xoff = src->xoff; 265 } 266 int i; 267 for (i=0; i < 6 ; i++){ 268 uint8_t mask = 1 << i; 269 if (src->parameter_mask_1 & mask){ 270 dest->flags = (dest->flags & ~mask) | (src->flags & mask); 271 } 272 } 273 // always copy parameter mask, too. informative for client, needed for response 274 dest->parameter_mask_0 = src->parameter_mask_0; 275 dest->parameter_mask_1 = src->parameter_mask_1; 276 } 277 // MARK: RFCOMM MULTIPLEXER HELPER 278 279 static uint16_t rfcomm_max_frame_size_for_l2cap_mtu(uint16_t l2cap_mtu){ 280 // Assume RFCOMM header without credits and 2 byte (14 bit) length field 281 uint16_t max_frame_size = l2cap_mtu - 5; 282 log_info("rfcomm_max_frame_size_for_l2cap_mtu: %u -> %u", l2cap_mtu, max_frame_size); 283 return max_frame_size; 284 } 285 286 static void rfcomm_multiplexer_initialize(rfcomm_multiplexer_t *multiplexer){ 287 288 memset(multiplexer, 0, sizeof(rfcomm_multiplexer_t)); 289 290 multiplexer->state = RFCOMM_MULTIPLEXER_CLOSED; 291 multiplexer->fcon = 1; 292 multiplexer->send_dm_for_dlci = 0; 293 multiplexer->max_frame_size = rfcomm_max_frame_size_for_l2cap_mtu(l2cap_max_mtu()); 294 multiplexer->test_data_len = 0; 295 multiplexer->nsc_command = 0; 296 } 297 298 static rfcomm_multiplexer_t * rfcomm_multiplexer_create_for_addr(bd_addr_t addr){ 299 300 // alloc structure 301 rfcomm_multiplexer_t * multiplexer = btstack_memory_rfcomm_multiplexer_get(); 302 if (!multiplexer) return NULL; 303 304 // fill in 305 rfcomm_multiplexer_initialize(multiplexer); 306 bd_addr_copy(multiplexer->remote_addr, addr); 307 308 // add to services list 309 btstack_linked_list_add(&rfcomm_multiplexers, (btstack_linked_item_t *) multiplexer); 310 311 return multiplexer; 312 } 313 314 static rfcomm_multiplexer_t * rfcomm_multiplexer_for_addr(bd_addr_t addr){ 315 btstack_linked_item_t *it; 316 for (it = (btstack_linked_item_t *) rfcomm_multiplexers; it ; it = it->next){ 317 rfcomm_multiplexer_t * multiplexer = ((rfcomm_multiplexer_t *) it); 318 // ignore multiplexer in shutdown 319 if (multiplexer->state == RFCOMM_MULTIPLEXER_SHUTTING_DOWN) continue; 320 if (bd_addr_cmp(addr, multiplexer->remote_addr) == 0) { 321 return multiplexer; 322 }; 323 } 324 return NULL; 325 } 326 327 static rfcomm_multiplexer_t * rfcomm_multiplexer_for_l2cap_cid(uint16_t l2cap_cid) { 328 btstack_linked_item_t *it; 329 for (it = (btstack_linked_item_t *) rfcomm_multiplexers; it ; it = it->next){ 330 rfcomm_multiplexer_t * multiplexer = ((rfcomm_multiplexer_t *) it); 331 if (multiplexer->l2cap_cid == l2cap_cid) { 332 return multiplexer; 333 }; 334 } 335 return NULL; 336 } 337 338 static int rfcomm_multiplexer_has_channels(rfcomm_multiplexer_t * multiplexer){ 339 btstack_linked_item_t *it; 340 for (it = (btstack_linked_item_t *) rfcomm_channels; it ; it = it->next){ 341 rfcomm_channel_t * channel = ((rfcomm_channel_t *) it); 342 if (channel->multiplexer == multiplexer) { 343 return 1; 344 } 345 } 346 return 0; 347 } 348 349 // MARK: RFCOMM CHANNEL HELPER 350 351 static void rfcomm_dump_channels(void){ 352 btstack_linked_item_t * it; 353 int channels = 0; 354 for (it = (btstack_linked_item_t *) rfcomm_channels; it ; it = it->next){ 355 rfcomm_channel_t * channel = (rfcomm_channel_t *) it; 356 log_info("Channel #%u: addr %p, state %u", channels, channel, channel->state); 357 channels++; 358 } 359 } 360 361 static void rfcomm_channel_initialize(rfcomm_channel_t *channel, rfcomm_multiplexer_t *multiplexer, 362 rfcomm_service_t *service, uint8_t server_channel){ 363 364 // don't use 0 as channel id 365 if (rfcomm_client_cid_generator == 0) ++rfcomm_client_cid_generator; 366 367 // setup channel 368 memset(channel, 0, sizeof(rfcomm_channel_t)); 369 370 // set defaults for port configuration (even for services) 371 rfcomm_rpn_data_set_defaults(&channel->rpn_data); 372 373 channel->state = RFCOMM_CHANNEL_CLOSED; 374 channel->state_var = RFCOMM_CHANNEL_STATE_VAR_NONE; 375 376 channel->multiplexer = multiplexer; 377 channel->rfcomm_cid = rfcomm_client_cid_generator++; 378 channel->max_frame_size = multiplexer->max_frame_size; 379 380 channel->credits_incoming = 0; 381 channel->credits_outgoing = 0; 382 383 // incoming flow control not active 384 channel->new_credits_incoming = RFCOMM_CREDITS; 385 channel->incoming_flow_control = 0; 386 387 channel->rls_line_status = RFCOMM_RLS_STATUS_INVALID; 388 389 channel->service = service; 390 if (service) { 391 // incoming connection 392 channel->dlci = (server_channel << 1) | multiplexer->outgoing; 393 if (channel->max_frame_size > service->max_frame_size) { 394 channel->max_frame_size = service->max_frame_size; 395 } 396 channel->incoming_flow_control = service->incoming_flow_control; 397 channel->new_credits_incoming = service->incoming_initial_credits; 398 channel->packet_handler = service->packet_handler; 399 } else { 400 // outgoing connection 401 channel->dlci = (server_channel << 1) | (multiplexer->outgoing ^ 1); 402 } 403 } 404 405 // service == NULL -> outgoing channel 406 static rfcomm_channel_t * rfcomm_channel_create(rfcomm_multiplexer_t * multiplexer, 407 rfcomm_service_t * service, uint8_t server_channel){ 408 409 log_info("rfcomm_channel_create for service %p, channel %u --- list of channels:", service, server_channel); 410 rfcomm_dump_channels(); 411 412 // alloc structure 413 rfcomm_channel_t * channel = btstack_memory_rfcomm_channel_get(); 414 if (!channel) return NULL; 415 416 // fill in 417 rfcomm_channel_initialize(channel, multiplexer, service, server_channel); 418 419 // add to services list 420 btstack_linked_list_add(&rfcomm_channels, (btstack_linked_item_t *) channel); 421 422 return channel; 423 } 424 425 static void rfcomm_notify_channel_can_send(void){ 426 btstack_linked_list_iterator_t it; 427 btstack_linked_list_iterator_init(&it, &rfcomm_channels); 428 while (btstack_linked_list_iterator_has_next(&it)){ 429 rfcomm_channel_t * channel = (rfcomm_channel_t *) btstack_linked_list_iterator_next(&it); 430 if (!channel->waiting_for_can_send_now) continue; // didn't try to send yet 431 if (!rfcomm_channel_can_send(channel)) continue; // or cannot yet either 432 433 channel->waiting_for_can_send_now = 0; 434 rfcomm_emit_can_send_now(channel); 435 } 436 } 437 438 static rfcomm_channel_t * rfcomm_channel_for_rfcomm_cid(uint16_t rfcomm_cid){ 439 btstack_linked_item_t *it; 440 for (it = (btstack_linked_item_t *) rfcomm_channels; it ; it = it->next){ 441 rfcomm_channel_t * channel = ((rfcomm_channel_t *) it); 442 if (channel->rfcomm_cid == rfcomm_cid) { 443 return channel; 444 }; 445 } 446 return NULL; 447 } 448 449 static rfcomm_channel_t * rfcomm_channel_for_multiplexer_and_dlci(rfcomm_multiplexer_t * multiplexer, uint8_t dlci){ 450 btstack_linked_item_t *it; 451 for (it = (btstack_linked_item_t *) rfcomm_channels; it ; it = it->next){ 452 rfcomm_channel_t * channel = ((rfcomm_channel_t *) it); 453 if (channel->dlci == dlci && channel->multiplexer == multiplexer) { 454 return channel; 455 }; 456 } 457 return NULL; 458 } 459 460 static rfcomm_service_t * rfcomm_service_for_channel(uint8_t server_channel){ 461 btstack_linked_item_t *it; 462 for (it = (btstack_linked_item_t *) rfcomm_services; it ; it = it->next){ 463 rfcomm_service_t * service = ((rfcomm_service_t *) it); 464 if ( service->server_channel == server_channel){ 465 return service; 466 }; 467 } 468 return NULL; 469 } 470 471 // MARK: RFCOMM SEND 472 473 /** 474 * @param credits - only used for RFCOMM flow control in UIH wiht P/F = 1 475 */ 476 static int rfcomm_send_packet_for_multiplexer(rfcomm_multiplexer_t *multiplexer, uint8_t address, uint8_t control, uint8_t credits, uint8_t *data, uint16_t len){ 477 478 if (!l2cap_can_send_packet_now(multiplexer->l2cap_cid)) return BTSTACK_ACL_BUFFERS_FULL; 479 480 l2cap_reserve_packet_buffer(); 481 uint8_t * rfcomm_out_buffer = l2cap_get_outgoing_buffer(); 482 483 uint16_t pos = 0; 484 uint8_t crc_fields = 3; 485 486 rfcomm_out_buffer[pos++] = address; 487 rfcomm_out_buffer[pos++] = control; 488 489 // length field can be 1 or 2 octets 490 if (len < 128){ 491 rfcomm_out_buffer[pos++] = (len << 1)| 1; // bits 0-6 492 } else { 493 rfcomm_out_buffer[pos++] = (len & 0x7f) << 1; // bits 0-6 494 rfcomm_out_buffer[pos++] = len >> 7; // bits 7-14 495 crc_fields++; 496 } 497 498 // add credits for UIH frames when PF bit is set 499 if (control == BT_RFCOMM_UIH_PF){ 500 rfcomm_out_buffer[pos++] = credits; 501 } 502 503 // copy actual data 504 if (len) { 505 memcpy(&rfcomm_out_buffer[pos], data, len); 506 pos += len; 507 } 508 509 // UIH frames only calc FCS over address + control (5.1.1) 510 if ((control & 0xef) == BT_RFCOMM_UIH){ 511 crc_fields = 2; 512 } 513 rfcomm_out_buffer[pos++] = btstack_crc8_calc(rfcomm_out_buffer, crc_fields); // calc fcs 514 515 int err = l2cap_send_prepared(multiplexer->l2cap_cid, pos); 516 517 return err; 518 } 519 520 // simplified version of rfcomm_send_packet_for_multiplexer for prepared rfcomm packet (UIH, 2 byte len, no credits) 521 static int rfcomm_send_uih_prepared(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint16_t len){ 522 523 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2); 524 uint8_t control = BT_RFCOMM_UIH; 525 526 uint8_t * rfcomm_out_buffer = l2cap_get_outgoing_buffer(); 527 528 uint16_t pos = 0; 529 rfcomm_out_buffer[pos++] = address; 530 rfcomm_out_buffer[pos++] = control; 531 rfcomm_out_buffer[pos++] = (len & 0x7f) << 1; // bits 0-6 532 rfcomm_out_buffer[pos++] = len >> 7; // bits 7-14 533 534 // actual data is already in place 535 pos += len; 536 537 // UIH frames only calc FCS over address + control (5.1.1) 538 rfcomm_out_buffer[pos++] = btstack_crc8_calc(rfcomm_out_buffer, 2); // calc fcs 539 540 int err = l2cap_send_prepared(multiplexer->l2cap_cid, pos); 541 542 return err; 543 } 544 545 // C/R Flag in Address 546 // - terms: initiator = station that creates multiplexer with SABM 547 // - terms: responder = station that responds to multiplexer setup with UA 548 // "For SABM, UA, DM and DISC frames C/R bit is set according to Table 1 in GSM 07.10, section 5.2.1.2" 549 // - command initiator = 1 /response responder = 1 550 // - command responder = 0 /response initiator = 0 551 // "For UIH frames, the C/R bit is always set according to section 5.4.3.1 in GSM 07.10. 552 // This applies independently of what is contained wthin the UIH frames, either data or control messages." 553 // - c/r = 1 for frames by initiating station, 0 = for frames by responding station 554 555 // C/R Flag in Message 556 // "In the message level, the C/R bit in the command type field is set as stated in section 5.4.6.2 in GSM 07.10." 557 // - If the C/R bit is set to 1 the message is a command 558 // - if it is set to 0 the message is a response. 559 560 // temp/old messge construction 561 562 // new object oriented version 563 static int rfcomm_send_sabm(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){ 564 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2); // command 565 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_SABM, 0, NULL, 0); 566 } 567 568 static int rfcomm_send_disc(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){ 569 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2); // command 570 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_DISC, 0, NULL, 0); 571 } 572 573 static int rfcomm_send_ua(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){ 574 uint8_t address = (1 << 0) | ((multiplexer->outgoing ^ 1) << 1) | (dlci << 2); // response 575 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UA, 0, NULL, 0); 576 } 577 578 static int rfcomm_send_dm_pf(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){ 579 uint8_t address = (1 << 0) | ((multiplexer->outgoing ^ 1) << 1) | (dlci << 2); // response 580 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_DM_PF, 0, NULL, 0); 581 } 582 583 static int rfcomm_send_uih_fc_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t fcon) { 584 uint8_t address = (1 << 0) | (multiplexer->outgoing<< 1); 585 uint8_t payload[2]; 586 uint8_t pos = 0; 587 payload[pos++] = fcon ? BT_RFCOMM_FCON_RSP : BT_RFCOMM_FCOFF_RSP; 588 payload[pos++] = (0 << 1) | 1; // len 589 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 590 } 591 592 // static int rfcomm_send_uih_test_cmd(rfcomm_multiplexer_t *multiplexer, uint8_t * data, uint16_t len) { 593 // uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 594 // uint8_t payload[2+len]; 595 // uint8_t pos = 0; 596 // payload[pos++] = BT_RFCOMM_TEST_CMD; 597 // payload[pos++] = (len + 1) << 1 | 1; // len 598 // memcpy(&payload[pos], data, len); 599 // pos += len; 600 // return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 601 // } 602 603 static int rfcomm_send_uih_test_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t * data, uint16_t len) { 604 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 605 uint8_t payload[2+RFCOMM_TEST_DATA_MAX_LEN]; 606 uint8_t pos = 0; 607 payload[pos++] = BT_RFCOMM_TEST_RSP; 608 if (len > RFCOMM_TEST_DATA_MAX_LEN) { 609 len = RFCOMM_TEST_DATA_MAX_LEN; 610 } 611 payload[pos++] = (len << 1) | 1; // len 612 memcpy(&payload[pos], data, len); 613 pos += len; 614 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 615 } 616 617 static int rfcomm_send_uih_msc_cmd(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t signals) { 618 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 619 uint8_t payload[4]; 620 uint8_t pos = 0; 621 payload[pos++] = BT_RFCOMM_MSC_CMD; 622 payload[pos++] = (2 << 1) | 1; // len 623 payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1 624 payload[pos++] = signals; 625 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 626 } 627 628 static int rfcomm_send_uih_msc_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t signals) { 629 uint8_t address = (1 << 0) | (multiplexer->outgoing<< 1); 630 uint8_t payload[4]; 631 uint8_t pos = 0; 632 payload[pos++] = BT_RFCOMM_MSC_RSP; 633 payload[pos++] = (2 << 1) | 1; // len 634 payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1 635 payload[pos++] = signals; 636 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 637 } 638 639 static int rfcomm_send_uih_nsc_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t command) { 640 uint8_t address = (1 << 0) | (multiplexer->outgoing<< 1); 641 uint8_t payload[3]; 642 uint8_t pos = 0; 643 payload[pos++] = BT_RFCOMM_NSC_RSP; 644 payload[pos++] = (1 << 1) | 1; // len 645 payload[pos++] = command; 646 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 647 } 648 649 static int rfcomm_send_uih_pn_command(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint16_t max_frame_size){ 650 uint8_t payload[10]; 651 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 652 uint8_t pos = 0; 653 payload[pos++] = BT_RFCOMM_PN_CMD; 654 payload[pos++] = (8 << 1) | 1; // len 655 payload[pos++] = dlci; 656 payload[pos++] = 0xf0; // pre-defined for Bluetooth, see 5.5.3 of TS 07.10 Adaption for RFCOMM 657 payload[pos++] = 0; // priority 658 payload[pos++] = 0; // max 60 seconds ack 659 payload[pos++] = max_frame_size & 0xff; // max framesize low 660 payload[pos++] = max_frame_size >> 8; // max framesize high 661 payload[pos++] = 0x00; // number of retransmissions 662 payload[pos++] = 0x00; // (unused error recovery window) initial number of credits 663 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 664 } 665 666 // "The response may not change the DLCI, the priority, the convergence layer, or the timer value." rfcomm_tutorial.pdf 667 static int rfcomm_send_uih_pn_response(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, 668 uint8_t priority, uint16_t max_frame_size){ 669 uint8_t payload[10]; 670 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 671 uint8_t pos = 0; 672 payload[pos++] = BT_RFCOMM_PN_RSP; 673 payload[pos++] = (8 << 1) | 1; // len 674 payload[pos++] = dlci; 675 payload[pos++] = 0xe0; // pre defined for Bluetooth, see 5.5.3 of TS 07.10 Adaption for RFCOMM 676 payload[pos++] = priority; // priority 677 payload[pos++] = 0; // max 60 seconds ack 678 payload[pos++] = max_frame_size & 0xff; // max framesize low 679 payload[pos++] = max_frame_size >> 8; // max framesize high 680 payload[pos++] = 0x00; // number of retransmissions 681 payload[pos++] = 0x00; // (unused error recovery window) initial number of credits 682 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 683 } 684 685 static int rfcomm_send_uih_rls_cmd(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t line_status) { 686 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 687 uint8_t payload[4]; 688 uint8_t pos = 0; 689 payload[pos++] = BT_RFCOMM_RLS_CMD; 690 payload[pos++] = (2 << 1) | 1; // len 691 payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1 692 payload[pos++] = line_status; 693 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 694 } 695 696 static int rfcomm_send_uih_rls_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t line_status) { 697 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 698 uint8_t payload[4]; 699 uint8_t pos = 0; 700 payload[pos++] = BT_RFCOMM_RLS_RSP; 701 payload[pos++] = (2 << 1) | 1; // len 702 payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1 703 payload[pos++] = line_status; 704 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 705 } 706 707 static int rfcomm_send_uih_rpn_cmd(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, rfcomm_rpn_data_t *rpn_data) { 708 uint8_t payload[10]; 709 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 710 uint8_t pos = 0; 711 payload[pos++] = BT_RFCOMM_RPN_CMD; 712 payload[pos++] = (8 << 1) | 1; // len 713 payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1 714 payload[pos++] = rpn_data->baud_rate; 715 payload[pos++] = rpn_data->flags; 716 payload[pos++] = rpn_data->flow_control; 717 payload[pos++] = rpn_data->xon; 718 payload[pos++] = rpn_data->xoff; 719 payload[pos++] = rpn_data->parameter_mask_0; 720 payload[pos++] = rpn_data->parameter_mask_1; 721 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 722 } 723 724 static int rfcomm_send_uih_rpn_req(rfcomm_multiplexer_t *multiplexer, uint8_t dlci) { 725 uint8_t payload[3]; 726 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 727 uint8_t pos = 0; 728 payload[pos++] = BT_RFCOMM_RPN_CMD; 729 payload[pos++] = (1 << 1) | 1; // len 730 payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1 731 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 732 } 733 734 static int rfcomm_send_uih_rpn_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, rfcomm_rpn_data_t *rpn_data) { 735 uint8_t payload[10]; 736 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1); 737 uint8_t pos = 0; 738 payload[pos++] = BT_RFCOMM_RPN_RSP; 739 payload[pos++] = (8 << 1) | 1; // len 740 payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1 741 payload[pos++] = rpn_data->baud_rate; 742 payload[pos++] = rpn_data->flags; 743 payload[pos++] = rpn_data->flow_control; 744 payload[pos++] = rpn_data->xon; 745 payload[pos++] = rpn_data->xoff; 746 payload[pos++] = rpn_data->parameter_mask_0; 747 payload[pos++] = rpn_data->parameter_mask_1; 748 return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos); 749 } 750 751 static void rfcomm_send_uih_credits(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t credits){ 752 uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2); 753 rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH_PF, credits, NULL, 0); 754 } 755 756 // MARK: RFCOMM MULTIPLEXER 757 static void rfcomm_multiplexer_stop_timer(rfcomm_multiplexer_t * multiplexer){ 758 if (multiplexer->timer_active) { 759 btstack_run_loop_remove_timer(&multiplexer->timer); 760 multiplexer->timer_active = 0; 761 } 762 } 763 static void rfcomm_multiplexer_free(rfcomm_multiplexer_t * multiplexer){ 764 btstack_linked_list_remove( &rfcomm_multiplexers, (btstack_linked_item_t *) multiplexer); 765 btstack_memory_rfcomm_multiplexer_free(multiplexer); 766 } 767 768 static void rfcomm_multiplexer_finalize(rfcomm_multiplexer_t * multiplexer){ 769 // remove (potential) timer 770 rfcomm_multiplexer_stop_timer(multiplexer); 771 772 // close and remove all channels 773 btstack_linked_item_t *it = (btstack_linked_item_t *) &rfcomm_channels; 774 while (it->next){ 775 rfcomm_channel_t * channel = (rfcomm_channel_t *) it->next; 776 if (channel->multiplexer == multiplexer) { 777 // emit appropriate events 778 switch(channel->state){ 779 case RFCOMM_CHANNEL_OPEN: 780 case RFCOMM_CHANNEL_W4_UA_AFTER_DISC: 781 rfcomm_emit_channel_closed(channel); 782 break; 783 case RFCOMM_CHANNEL_SEND_UA_AFTER_DISC: 784 // remote didn't wait until we send the UA disc 785 break; 786 default: 787 rfcomm_emit_channel_opened(channel, RFCOMM_MULTIPLEXER_STOPPED); 788 break; 789 } 790 // remove from list 791 it->next = it->next->next; 792 // free channel struct 793 btstack_memory_rfcomm_channel_free(channel); 794 } else { 795 it = it->next; 796 } 797 } 798 799 // remove mutliplexer 800 rfcomm_multiplexer_free(multiplexer); 801 } 802 803 static void rfcomm_multiplexer_timer_handler(btstack_timer_source_t *timer){ 804 rfcomm_multiplexer_t * multiplexer = (rfcomm_multiplexer_t*) btstack_run_loop_get_timer_context(timer); 805 if (rfcomm_multiplexer_has_channels(multiplexer)) return; 806 807 log_info("rfcomm_multiplexer_timer_handler timeout: shutting down multiplexer! (no channels)"); 808 uint16_t l2cap_cid = multiplexer->l2cap_cid; 809 rfcomm_multiplexer_finalize(multiplexer); 810 l2cap_disconnect(l2cap_cid, 0x13); 811 } 812 813 static void rfcomm_multiplexer_prepare_idle_timer(rfcomm_multiplexer_t * multiplexer){ 814 if (multiplexer->timer_active) { 815 btstack_run_loop_remove_timer(&multiplexer->timer); 816 multiplexer->timer_active = 0; 817 } 818 if (rfcomm_multiplexer_has_channels(multiplexer)) return; 819 820 // start idle timer for multiplexer timeout check as there are no rfcomm channels yet 821 btstack_run_loop_set_timer(&multiplexer->timer, RFCOMM_MULIPLEXER_TIMEOUT_MS); 822 btstack_run_loop_set_timer_handler(&multiplexer->timer, rfcomm_multiplexer_timer_handler); 823 btstack_run_loop_set_timer_context(&multiplexer->timer, multiplexer); 824 btstack_run_loop_add_timer(&multiplexer->timer); 825 multiplexer->timer_active = 1; 826 } 827 828 static void rfcomm_multiplexer_opened(rfcomm_multiplexer_t *multiplexer){ 829 log_info("Multiplexer up and running"); 830 multiplexer->state = RFCOMM_MULTIPLEXER_OPEN; 831 832 const rfcomm_channel_event_t event = { CH_EVT_MULTIPLEXER_READY, 0}; 833 834 // transition of channels that wait for multiplexer 835 btstack_linked_item_t *it; 836 for (it = (btstack_linked_item_t *) rfcomm_channels; it ; it = it->next){ 837 rfcomm_channel_t * channel = ((rfcomm_channel_t *) it); 838 if (channel->multiplexer != multiplexer) continue; 839 int rfcomm_channel_valid = 1; 840 rfcomm_channel_state_machine_with_channel(channel, &event, &rfcomm_channel_valid); 841 if (rfcomm_channel_valid && rfcomm_channel_ready_to_send(channel)){ 842 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 843 } 844 } 845 rfcomm_multiplexer_prepare_idle_timer(multiplexer); 846 847 // request can send now for multiplexer if ready 848 if (rfcomm_multiplexer_ready_to_send(multiplexer)){ 849 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 850 } 851 } 852 853 static void rfcomm_handle_can_send_now(uint16_t l2cap_cid){ 854 855 log_debug("rfcomm_handle_can_send_now enter: %u", l2cap_cid); 856 857 btstack_linked_list_iterator_t it; 858 int token_consumed = 0; 859 860 // forward token to multiplexer 861 btstack_linked_list_iterator_init(&it, &rfcomm_multiplexers); 862 while (!token_consumed && btstack_linked_list_iterator_has_next(&it)){ 863 rfcomm_multiplexer_t * multiplexer = (rfcomm_multiplexer_t *) btstack_linked_list_iterator_next(&it); 864 if (multiplexer->l2cap_cid != l2cap_cid) continue; 865 if (rfcomm_multiplexer_ready_to_send(multiplexer)){ 866 log_debug("rfcomm_handle_can_send_now enter: multiplexer token"); 867 token_consumed = 1; 868 rfcomm_multiplexer_state_machine(multiplexer, MULT_EV_READY_TO_SEND); 869 } 870 } 871 872 // forward token to channel state machine 873 btstack_linked_list_iterator_init(&it, &rfcomm_channels); 874 while (!token_consumed && btstack_linked_list_iterator_has_next(&it)){ 875 rfcomm_channel_t * channel = (rfcomm_channel_t *) btstack_linked_list_iterator_next(&it); 876 if (channel->multiplexer->l2cap_cid != l2cap_cid) continue; 877 // channel state machine 878 if (rfcomm_channel_ready_to_send(channel)){ 879 log_debug("rfcomm_handle_can_send_now enter: channel token"); 880 token_consumed = 1; 881 const rfcomm_channel_event_t event = { CH_EVT_READY_TO_SEND, 0 }; 882 int rfcomm_channel_valid = 1; 883 rfcomm_channel_state_machine_with_channel(channel, &event, &rfcomm_channel_valid); 884 } 885 } 886 887 // forward token to client 888 btstack_linked_list_iterator_init(&it, &rfcomm_channels); 889 while (!token_consumed && btstack_linked_list_iterator_has_next(&it)){ 890 rfcomm_channel_t * channel = (rfcomm_channel_t *) btstack_linked_list_iterator_next(&it); 891 if (channel->multiplexer->l2cap_cid != l2cap_cid) continue; 892 // client waiting for can send now 893 if (!channel->waiting_for_can_send_now) continue; 894 if ((channel->multiplexer->fcon & 1) == 0) continue; 895 if (!channel->credits_outgoing){ 896 log_debug("rfcomm_handle_can_send_now waiting to send but no credits (ignore)"); 897 continue; 898 } 899 900 log_debug("rfcomm_handle_can_send_now enter: client token"); 901 token_consumed = 1; 902 channel->waiting_for_can_send_now = 0; 903 rfcomm_emit_can_send_now(channel); 904 } 905 906 // if token was consumed, request another one 907 if (token_consumed) { 908 l2cap_request_can_send_now_event(l2cap_cid); 909 } 910 911 log_debug("rfcomm_handle_can_send_now exit"); 912 } 913 914 static void rfcomm_multiplexer_set_state_and_request_can_send_now_event(rfcomm_multiplexer_t * multiplexer, RFCOMM_MULTIPLEXER_STATE state){ 915 multiplexer->state = state; 916 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 917 } 918 919 /** 920 * @return handled packet 921 */ 922 static int rfcomm_hci_event_handler(uint8_t *packet, uint16_t size){ 923 924 UNUSED(size); // ok: handling own l2cap events 925 926 bd_addr_t event_addr; 927 uint16_t psm; 928 uint16_t l2cap_cid; 929 hci_con_handle_t con_handle; 930 rfcomm_multiplexer_t *multiplexer = NULL; 931 uint8_t status; 932 933 switch (hci_event_packet_get_type(packet)) { 934 935 // accept incoming rfcomm connection if no multiplexer exists yet 936 case L2CAP_EVENT_INCOMING_CONNECTION: 937 // data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16) 938 reverse_bd_addr(&packet[2], event_addr); 939 con_handle = little_endian_read_16(packet, 8); 940 psm = little_endian_read_16(packet, 10); 941 l2cap_cid = little_endian_read_16(packet, 12); 942 943 if (psm != BLUETOOTH_PROTOCOL_RFCOMM) break; 944 945 multiplexer = rfcomm_multiplexer_for_addr(event_addr); 946 947 if (multiplexer) { 948 log_info("INCOMING_CONNECTION (l2cap_cid 0x%02x) for BLUETOOTH_PROTOCOL_RFCOMM => decline - multiplexer already exists", l2cap_cid); 949 l2cap_decline_connection(l2cap_cid); 950 return 1; 951 } 952 953 // create and inititialize new multiplexer instance (incoming) 954 multiplexer = rfcomm_multiplexer_create_for_addr(event_addr); 955 if (!multiplexer){ 956 log_info("INCOMING_CONNECTION (l2cap_cid 0x%02x) for BLUETOOTH_PROTOCOL_RFCOMM => decline - no memory left", l2cap_cid); 957 l2cap_decline_connection(l2cap_cid); 958 return 1; 959 } 960 961 multiplexer->con_handle = con_handle; 962 multiplexer->l2cap_cid = l2cap_cid; 963 // 964 multiplexer->state = RFCOMM_MULTIPLEXER_W4_SABM_0; 965 log_info("L2CAP_EVENT_INCOMING_CONNECTION (l2cap_cid 0x%02x) for BLUETOOTH_PROTOCOL_RFCOMM => accept", l2cap_cid); 966 l2cap_accept_connection(l2cap_cid); 967 return 1; 968 969 // l2cap connection opened -> store l2cap_cid, remote_addr 970 case L2CAP_EVENT_CHANNEL_OPENED: 971 972 if (little_endian_read_16(packet, 11) != BLUETOOTH_PROTOCOL_RFCOMM) break; 973 974 status = packet[2]; 975 log_info("L2CAP_EVENT_CHANNEL_OPENED for BLUETOOTH_PROTOCOL_RFCOMM, status %u", status); 976 977 // get multiplexer for remote addr 978 con_handle = little_endian_read_16(packet, 9); 979 l2cap_cid = little_endian_read_16(packet, 13); 980 reverse_bd_addr(&packet[3], event_addr); 981 multiplexer = rfcomm_multiplexer_for_addr(event_addr); 982 if (!multiplexer) { 983 log_error("L2CAP_EVENT_CHANNEL_OPENED but no multiplexer prepared"); 984 return 1; 985 } 986 987 // on l2cap open error discard everything 988 if (status){ 989 990 // remove (potential) timer 991 rfcomm_multiplexer_stop_timer(multiplexer); 992 993 // mark multiplexer as shutting down 994 multiplexer->state = RFCOMM_MULTIPLEXER_SHUTTING_DOWN; 995 996 // emit rfcomm_channel_opened with status and free channel 997 // note: repeatedly go over list until full iteration causes no further change 998 int done; 999 do { 1000 done = 1; 1001 btstack_linked_item_t * it = (btstack_linked_item_t *) &rfcomm_channels; 1002 while (it->next) { 1003 rfcomm_channel_t * channel = (rfcomm_channel_t *) it->next; 1004 if (channel->multiplexer == multiplexer){ 1005 done = 0; 1006 rfcomm_emit_channel_opened(channel, status); 1007 btstack_linked_list_remove(&rfcomm_channels, (btstack_linked_item_t *) channel); 1008 btstack_memory_rfcomm_channel_free(channel); 1009 break; 1010 } else { 1011 it = it->next; 1012 } 1013 } 1014 } while (!done); 1015 1016 // free multiplexer 1017 rfcomm_multiplexer_free(multiplexer); 1018 return 1; 1019 } 1020 1021 // following could be: rfcom_multiplexer_state_machein(..., EVENT_L2CAP_OPENED) 1022 1023 if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_CONNECT) { 1024 log_info("L2CAP_EVENT_CHANNEL_OPENED: outgoing connection"); 1025 // wrong remote addr 1026 if (bd_addr_cmp(event_addr, multiplexer->remote_addr)) break; 1027 multiplexer->l2cap_cid = l2cap_cid; 1028 multiplexer->con_handle = con_handle; 1029 // send SABM #0 1030 rfcomm_multiplexer_set_state_and_request_can_send_now_event(multiplexer, RFCOMM_MULTIPLEXER_SEND_SABM_0); 1031 1032 } else { // multiplexer->state == RFCOMM_MULTIPLEXER_W4_SABM_0 1033 1034 // set max frame size based on l2cap MTU 1035 multiplexer->max_frame_size = rfcomm_max_frame_size_for_l2cap_mtu(little_endian_read_16(packet, 17)); 1036 } 1037 return 1; 1038 1039 // l2cap disconnect -> state = RFCOMM_MULTIPLEXER_CLOSED; 1040 1041 // Notify channel packet handler if they can send now 1042 case L2CAP_EVENT_CAN_SEND_NOW: 1043 l2cap_cid = l2cap_event_can_send_now_get_local_cid(packet); 1044 rfcomm_handle_can_send_now(l2cap_cid); 1045 return 1; 1046 1047 case L2CAP_EVENT_CHANNEL_CLOSED: 1048 // data: event (8), len(8), channel (16) 1049 l2cap_cid = little_endian_read_16(packet, 2); 1050 multiplexer = rfcomm_multiplexer_for_l2cap_cid(l2cap_cid); 1051 log_info("L2CAP_EVENT_CHANNEL_CLOSED cid 0x%0x, mult %p", l2cap_cid, multiplexer); 1052 if (!multiplexer) break; 1053 log_info("L2CAP_EVENT_CHANNEL_CLOSED state %u", multiplexer->state); 1054 // no need to call l2cap_disconnect here, as it's already closed 1055 rfcomm_multiplexer_finalize(multiplexer); 1056 return 1; 1057 1058 default: 1059 break; 1060 } 1061 return 0; 1062 } 1063 1064 static int rfcomm_multiplexer_l2cap_packet_handler(uint16_t channel, uint8_t *packet, uint16_t size){ 1065 // get or create a multiplexer for a certain device 1066 rfcomm_multiplexer_t *multiplexer = rfcomm_multiplexer_for_l2cap_cid(channel); 1067 if (!multiplexer) return 0; 1068 1069 uint16_t l2cap_cid = multiplexer->l2cap_cid; 1070 1071 // but only care for multiplexer control channel 1072 uint8_t frame_dlci = packet[0] >> 2; 1073 if (frame_dlci) return 0; 1074 const uint8_t length_offset = (packet[2] & 1) ^ 1; // to be used for pos >= 3 1075 const uint8_t credit_offset = ((packet[1] & BT_RFCOMM_UIH_PF) == BT_RFCOMM_UIH_PF) ? 1 : 0; // credits for uih_pf frames 1076 const uint8_t payload_offset = 3 + length_offset + credit_offset; 1077 switch (packet[1]){ 1078 1079 case BT_RFCOMM_SABM: 1080 if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_SABM_0){ 1081 log_info("Received SABM #0"); 1082 multiplexer->outgoing = 0; 1083 rfcomm_multiplexer_set_state_and_request_can_send_now_event(multiplexer, RFCOMM_MULTIPLEXER_SEND_UA_0); 1084 return 1; 1085 } 1086 break; 1087 1088 case BT_RFCOMM_UA: 1089 if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_UA_0) { 1090 // UA #0 -> send UA #0, state = RFCOMM_MULTIPLEXER_OPEN 1091 log_info("Received UA #0 "); 1092 rfcomm_multiplexer_opened(multiplexer); 1093 return 1; 1094 } 1095 break; 1096 1097 case BT_RFCOMM_DISC: 1098 // DISC #0 -> send UA #0, close multiplexer 1099 log_info("Received DISC #0, (ougoing = %u)", multiplexer->outgoing); 1100 rfcomm_multiplexer_set_state_and_request_can_send_now_event(multiplexer, RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC); 1101 return 1; 1102 1103 case BT_RFCOMM_DM: 1104 // DM #0 - we shouldn't get this, just give up 1105 log_info("Received DM #0"); 1106 log_info("-> Closing down multiplexer"); 1107 rfcomm_multiplexer_finalize(multiplexer); 1108 l2cap_disconnect(l2cap_cid, 0x13); 1109 return 1; 1110 1111 case BT_RFCOMM_UIH: 1112 if (packet[payload_offset] == BT_RFCOMM_CLD_CMD){ 1113 // Multiplexer close down (CLD) -> close mutliplexer 1114 log_info("Received Multiplexer close down command"); 1115 log_info("-> Closing down multiplexer"); 1116 rfcomm_multiplexer_finalize(multiplexer); 1117 l2cap_disconnect(l2cap_cid, 0x13); 1118 return 1; 1119 } 1120 switch (packet[payload_offset]){ 1121 case BT_RFCOMM_CLD_CMD: 1122 // Multiplexer close down (CLD) -> close mutliplexer 1123 log_info("Received Multiplexer close down command"); 1124 log_info("-> Closing down multiplexer"); 1125 rfcomm_multiplexer_finalize(multiplexer); 1126 l2cap_disconnect(l2cap_cid, 0x13); 1127 return 1; 1128 1129 case BT_RFCOMM_FCON_CMD: 1130 multiplexer->fcon = 0x81; 1131 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1132 return 1; 1133 1134 case BT_RFCOMM_FCOFF_CMD: 1135 multiplexer->fcon = 0x80; 1136 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1137 return 1; 1138 1139 case BT_RFCOMM_TEST_CMD: { 1140 log_info("Received test command"); 1141 int len = packet[payload_offset+1] >> 1; // length < 125 1142 if (len > RFCOMM_TEST_DATA_MAX_LEN){ 1143 len = RFCOMM_TEST_DATA_MAX_LEN; 1144 } 1145 len = btstack_min(len, size - 1 - payload_offset); // avoid information leak 1146 multiplexer->test_data_len = len; 1147 memcpy(multiplexer->test_data, &packet[payload_offset + 2], len); 1148 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1149 return 1; 1150 } 1151 default: 1152 break; 1153 } 1154 break; 1155 1156 default: 1157 break; 1158 1159 } 1160 return 0; 1161 } 1162 1163 static int rfcomm_multiplexer_ready_to_send(rfcomm_multiplexer_t * multiplexer){ 1164 if (multiplexer->send_dm_for_dlci) return 1; 1165 if (multiplexer->nsc_command) return 1; 1166 if (multiplexer->fcon & 0x80) return 1; 1167 switch (multiplexer->state){ 1168 case RFCOMM_MULTIPLEXER_SEND_SABM_0: 1169 case RFCOMM_MULTIPLEXER_SEND_UA_0: 1170 case RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC: 1171 return 1; 1172 case RFCOMM_MULTIPLEXER_OPEN: 1173 if (multiplexer->test_data_len) { 1174 return 1; 1175 } 1176 break; 1177 default: 1178 break; 1179 } 1180 return 0; 1181 } 1182 1183 static void rfcomm_multiplexer_state_machine(rfcomm_multiplexer_t * multiplexer, RFCOMM_MULTIPLEXER_EVENT event){ 1184 1185 if (event != MULT_EV_READY_TO_SEND) return; 1186 1187 uint16_t l2cap_cid = multiplexer->l2cap_cid; 1188 1189 // process stored DM responses 1190 if (multiplexer->send_dm_for_dlci){ 1191 uint8_t dlci = multiplexer->send_dm_for_dlci; 1192 multiplexer->send_dm_for_dlci = 0; 1193 rfcomm_send_dm_pf(multiplexer, dlci); 1194 return; 1195 } 1196 1197 if (multiplexer->nsc_command){ 1198 uint8_t command = multiplexer->nsc_command; 1199 multiplexer->nsc_command = 0; 1200 rfcomm_send_uih_nsc_rsp(multiplexer, command); 1201 return; 1202 } 1203 1204 if (multiplexer->fcon & 0x80){ 1205 multiplexer->fcon &= 0x01; 1206 rfcomm_send_uih_fc_rsp(multiplexer, multiplexer->fcon); 1207 1208 if (multiplexer->fcon == 0) return; 1209 // trigger client to send again after sending FCon Response 1210 rfcomm_notify_channel_can_send(); 1211 return; 1212 } 1213 1214 switch (multiplexer->state) { 1215 case RFCOMM_MULTIPLEXER_SEND_SABM_0: 1216 log_info("Sending SABM #0 - (multi 0x%p)", multiplexer); 1217 multiplexer->state = RFCOMM_MULTIPLEXER_W4_UA_0; 1218 rfcomm_send_sabm(multiplexer, 0); 1219 break; 1220 case RFCOMM_MULTIPLEXER_SEND_UA_0: 1221 log_info("Sending UA #0"); 1222 multiplexer->state = RFCOMM_MULTIPLEXER_OPEN; 1223 rfcomm_send_ua(multiplexer, 0); 1224 1225 rfcomm_multiplexer_opened(multiplexer); 1226 break; 1227 case RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC: 1228 log_info("Sending UA #0"); 1229 log_info("Closing down multiplexer"); 1230 multiplexer->state = RFCOMM_MULTIPLEXER_CLOSED; 1231 rfcomm_send_ua(multiplexer, 0); 1232 1233 rfcomm_multiplexer_finalize(multiplexer); 1234 l2cap_disconnect(l2cap_cid, 0x13); 1235 break; 1236 case RFCOMM_MULTIPLEXER_OPEN: 1237 // respond to test command 1238 if (multiplexer->test_data_len){ 1239 int len = multiplexer->test_data_len; 1240 log_info("Sending TEST Response with %u bytes", len); 1241 multiplexer->test_data_len = 0; 1242 rfcomm_send_uih_test_rsp(multiplexer, multiplexer->test_data, len); 1243 return; 1244 } 1245 break; 1246 default: 1247 break; 1248 } 1249 } 1250 1251 // MARK: RFCOMM CHANNEL 1252 1253 static void rfcomm_channel_send_credits(rfcomm_channel_t *channel, uint8_t credits){ 1254 channel->credits_incoming += credits; 1255 rfcomm_send_uih_credits(channel->multiplexer, channel->dlci, credits); 1256 } 1257 1258 static int rfcomm_channel_can_send(rfcomm_channel_t * channel){ 1259 if (!channel->credits_outgoing) return 0; 1260 if ((channel->multiplexer->fcon & 1) == 0) return 0; 1261 return l2cap_can_send_packet_now(channel->multiplexer->l2cap_cid); 1262 } 1263 1264 static void rfcomm_channel_opened(rfcomm_channel_t *rfChannel){ 1265 1266 log_info("rfcomm_channel_opened!"); 1267 1268 rfChannel->state = RFCOMM_CHANNEL_OPEN; 1269 rfcomm_emit_channel_opened(rfChannel, 0); 1270 rfcomm_emit_port_configuration(rfChannel); 1271 1272 // remove (potential) timer 1273 rfcomm_multiplexer_t *multiplexer = rfChannel->multiplexer; 1274 if (multiplexer->timer_active) { 1275 btstack_run_loop_remove_timer(&multiplexer->timer); 1276 multiplexer->timer_active = 0; 1277 } 1278 // hack for problem detecting authentication failure 1279 multiplexer->at_least_one_connection = 1; 1280 1281 // request can send now if channel ready 1282 if (rfcomm_channel_ready_to_send(rfChannel)){ 1283 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1284 } 1285 } 1286 1287 static void rfcomm_channel_packet_handler_uih(rfcomm_multiplexer_t *multiplexer, uint8_t * packet, uint16_t size){ 1288 const uint8_t frame_dlci = packet[0] >> 2; 1289 const uint8_t length_offset = (packet[2] & 1) ^ 1; // to be used for pos >= 3 1290 const uint8_t credit_offset = ((packet[1] & BT_RFCOMM_UIH_PF) == BT_RFCOMM_UIH_PF) ? 1 : 0; // credits for uih_pf frames 1291 const uint8_t payload_offset = 3 + length_offset + credit_offset; 1292 int request_can_send_now = 0; 1293 1294 rfcomm_channel_t * channel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, frame_dlci); 1295 if (!channel) return; 1296 1297 // handle new outgoing credits 1298 if (packet[1] == BT_RFCOMM_UIH_PF) { 1299 1300 // add them 1301 uint16_t new_credits = packet[3+length_offset]; 1302 channel->credits_outgoing += new_credits; 1303 log_info( "RFCOMM data UIH_PF, new credits channel 0x%02x: %u, now %u", channel->rfcomm_cid, new_credits, channel->credits_outgoing); 1304 1305 // notify channel statemachine 1306 rfcomm_channel_event_t channel_event = { CH_EVT_RCVD_CREDITS, 0 }; 1307 log_debug("rfcomm_channel_state_machine_with_channel, waiting_for_can_send_now %u", channel->waiting_for_can_send_now); 1308 int rfcomm_channel_valid = 1; 1309 rfcomm_channel_state_machine_with_channel(channel, &channel_event, &rfcomm_channel_valid); 1310 if (rfcomm_channel_valid){ 1311 if (rfcomm_channel_ready_to_send(channel) || channel->waiting_for_can_send_now){ 1312 request_can_send_now = 1; 1313 } 1314 } 1315 } 1316 1317 // contains payload? 1318 if (size - 1 > payload_offset){ 1319 1320 // log_info( "RFCOMM data UIH_PF, size %u, channel %p", size-payload_offset-1, rfChannel->connection); 1321 1322 // decrease incoming credit counter 1323 if (channel->credits_incoming > 0){ 1324 channel->credits_incoming--; 1325 } 1326 1327 // deliver payload 1328 (channel->packet_handler)(RFCOMM_DATA_PACKET, channel->rfcomm_cid, 1329 &packet[payload_offset], size-payload_offset-1); 1330 } 1331 1332 // automatically provide new credits to remote device, if no incoming flow control 1333 if (!channel->incoming_flow_control && channel->credits_incoming < 5){ 1334 channel->new_credits_incoming = RFCOMM_CREDITS; 1335 request_can_send_now = 1; 1336 } 1337 1338 if (request_can_send_now){ 1339 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1340 } 1341 } 1342 1343 static void rfcomm_channel_accept_pn(rfcomm_channel_t *channel, rfcomm_channel_event_pn_t *event){ 1344 // priority of client request 1345 channel->pn_priority = event->priority; 1346 1347 // new credits 1348 channel->credits_outgoing = event->credits_outgoing; 1349 1350 // negotiate max frame size 1351 if (channel->max_frame_size > channel->multiplexer->max_frame_size) { 1352 channel->max_frame_size = channel->multiplexer->max_frame_size; 1353 } 1354 if (channel->max_frame_size > event->max_frame_size) { 1355 channel->max_frame_size = event->max_frame_size; 1356 } 1357 1358 } 1359 1360 static void rfcomm_channel_finalize(rfcomm_channel_t *channel){ 1361 1362 rfcomm_multiplexer_t *multiplexer = channel->multiplexer; 1363 1364 // remove from list 1365 btstack_linked_list_remove( &rfcomm_channels, (btstack_linked_item_t *) channel); 1366 1367 // free channel 1368 btstack_memory_rfcomm_channel_free(channel); 1369 1370 // update multiplexer timeout after channel was removed from list 1371 rfcomm_multiplexer_prepare_idle_timer(multiplexer); 1372 } 1373 1374 static void rfcomm_channel_state_machine_with_dlci(rfcomm_multiplexer_t * multiplexer, uint8_t dlci, const rfcomm_channel_event_t *event){ 1375 1376 // TODO: if client max frame size is smaller than RFCOMM_DEFAULT_SIZE, send PN 1377 1378 1379 // lookup existing channel 1380 rfcomm_channel_t * channel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, dlci); 1381 1382 // log_info("rfcomm_channel_state_machine_with_dlci lookup dlci #%u = 0x%08x - event %u", dlci, (int) channel, event->type); 1383 1384 if (channel) { 1385 int rfcomm_channel_valid = 1; 1386 rfcomm_channel_state_machine_with_channel(channel, event, &rfcomm_channel_valid); 1387 if (rfcomm_channel_valid && rfcomm_channel_ready_to_send(channel)){ 1388 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1389 } 1390 return; 1391 } 1392 1393 // service registered? 1394 rfcomm_service_t * service = rfcomm_service_for_channel(dlci >> 1); 1395 // log_info("rfcomm_channel_state_machine_with_dlci service dlci #%u = 0x%08x", dlci, (int) service); 1396 if (!service) { 1397 // discard request by sending disconnected mode 1398 multiplexer->send_dm_for_dlci = dlci; 1399 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1400 return; 1401 } 1402 1403 // create channel for some events 1404 switch (event->type) { 1405 case CH_EVT_RCVD_SABM: 1406 case CH_EVT_RCVD_PN: 1407 case CH_EVT_RCVD_RPN_REQ: 1408 case CH_EVT_RCVD_RPN_CMD: 1409 // setup incoming channel 1410 channel = rfcomm_channel_create(multiplexer, service, dlci >> 1); 1411 if (!channel){ 1412 // discard request by sending disconnected mode 1413 multiplexer->send_dm_for_dlci = dlci; 1414 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1415 } 1416 break; 1417 default: 1418 break; 1419 } 1420 1421 if (!channel) { 1422 // discard request by sending disconnected mode 1423 multiplexer->send_dm_for_dlci = dlci; 1424 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1425 return; 1426 } 1427 1428 int rfcomm_channel_valid = 1; 1429 rfcomm_channel_state_machine_with_channel(channel, event, &rfcomm_channel_valid); 1430 if (rfcomm_channel_valid && rfcomm_channel_ready_to_send(channel)){ 1431 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1432 } 1433 } 1434 1435 static void rfcomm_channel_packet_handler(rfcomm_multiplexer_t * multiplexer, uint8_t *packet, uint16_t size){ 1436 1437 UNUSED(size); // ok: fixed format messages 1438 1439 // rfcomm: (0) addr [76543 server channel] [2 direction: initiator uses 1] [1 C/R: CMD by initiator = 1] [0 EA=1] 1440 const uint8_t frame_dlci = packet[0] >> 2; 1441 uint8_t message_dlci; // used by commands in UIH(_PF) packets 1442 uint8_t message_len; // " 1443 1444 // rfcomm: (1) command/control 1445 // -- credits_offset = 1 if command == BT_RFCOMM_UIH_PF 1446 const uint8_t credit_offset = ((packet[1] & BT_RFCOMM_UIH_PF) == BT_RFCOMM_UIH_PF) ? 1 : 0; // credits for uih_pf frames 1447 // rfcomm: (2) length. if bit 0 is cleared, 2 byte length is used. (little endian) 1448 const uint8_t length_offset = (packet[2] & 1) ^ 1; // to be used for pos >= 3 1449 // rfcomm: (3+length_offset) credits if credits_offset == 1 1450 // rfcomm: (3+length_offest+credits_offset) 1451 const uint8_t payload_offset = 3 + length_offset + credit_offset; 1452 1453 rfcomm_channel_event_t event; 1454 rfcomm_channel_event_pn_t event_pn; 1455 rfcomm_channel_event_rpn_t event_rpn; 1456 rfcomm_channel_event_msc_t event_msc; 1457 1458 // switch by rfcomm message type 1459 switch(packet[1]) { 1460 1461 case BT_RFCOMM_SABM: 1462 event.type = CH_EVT_RCVD_SABM; 1463 log_info("Received SABM #%u", frame_dlci); 1464 rfcomm_channel_state_machine_with_dlci(multiplexer, frame_dlci, &event); 1465 break; 1466 1467 case BT_RFCOMM_UA: 1468 event.type = CH_EVT_RCVD_UA; 1469 log_info("Received UA #%u",frame_dlci); 1470 rfcomm_channel_state_machine_with_dlci(multiplexer, frame_dlci, &event); 1471 break; 1472 1473 case BT_RFCOMM_DISC: 1474 event.type = CH_EVT_RCVD_DISC; 1475 rfcomm_channel_state_machine_with_dlci(multiplexer, frame_dlci, &event); 1476 break; 1477 1478 case BT_RFCOMM_DM: 1479 case BT_RFCOMM_DM_PF: 1480 event.type = CH_EVT_RCVD_DM; 1481 rfcomm_channel_state_machine_with_dlci(multiplexer, frame_dlci, &event); 1482 break; 1483 1484 case BT_RFCOMM_UIH_PF: 1485 case BT_RFCOMM_UIH: 1486 1487 message_len = packet[payload_offset+1] >> 1; 1488 1489 switch (packet[payload_offset]) { 1490 case BT_RFCOMM_PN_CMD: 1491 message_dlci = packet[payload_offset+2]; 1492 event_pn.super.type = CH_EVT_RCVD_PN; 1493 event_pn.priority = packet[payload_offset+4]; 1494 event_pn.max_frame_size = little_endian_read_16(packet, payload_offset+6); 1495 event_pn.credits_outgoing = packet[payload_offset+9]; 1496 log_info("Received UIH Parameter Negotiation Command for #%u, credits %u", 1497 message_dlci, event_pn.credits_outgoing); 1498 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_pn); 1499 break; 1500 1501 case BT_RFCOMM_PN_RSP: 1502 message_dlci = packet[payload_offset+2]; 1503 event_pn.super.type = CH_EVT_RCVD_PN_RSP; 1504 event_pn.priority = packet[payload_offset+4]; 1505 event_pn.max_frame_size = little_endian_read_16(packet, payload_offset+6); 1506 event_pn.credits_outgoing = packet[payload_offset+9]; 1507 log_info("Received UIH Parameter Negotiation Response max frame %u, credits %u", 1508 event_pn.max_frame_size, event_pn.credits_outgoing); 1509 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_pn); 1510 break; 1511 1512 case BT_RFCOMM_MSC_CMD: 1513 message_dlci = packet[payload_offset+2] >> 2; 1514 event_msc.super.type = CH_EVT_RCVD_MSC_CMD; 1515 event_msc.modem_status = packet[payload_offset+3]; 1516 log_info("Received MSC CMD for #%u, ", message_dlci); 1517 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_msc); 1518 break; 1519 1520 case BT_RFCOMM_MSC_RSP: 1521 message_dlci = packet[payload_offset+2] >> 2; 1522 event.type = CH_EVT_RCVD_MSC_RSP; 1523 log_info("Received MSC RSP for #%u", message_dlci); 1524 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, &event); 1525 break; 1526 1527 case BT_RFCOMM_RPN_CMD: 1528 message_dlci = packet[payload_offset+2] >> 2; 1529 switch (message_len){ 1530 case 1: 1531 log_info("Received Remote Port Negotiation Request for #%u", message_dlci); 1532 event.type = CH_EVT_RCVD_RPN_REQ; 1533 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, &event); 1534 break; 1535 case 8: 1536 log_info("Received Remote Port Negotiation Update for #%u", message_dlci); 1537 event_rpn.super.type = CH_EVT_RCVD_RPN_CMD; 1538 event_rpn.data = *(rfcomm_rpn_data_t*) &packet[payload_offset+3]; 1539 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_rpn); 1540 break; 1541 default: 1542 break; 1543 } 1544 break; 1545 1546 case BT_RFCOMM_RPN_RSP: 1547 log_info("Received RPN response"); 1548 break; 1549 1550 case BT_RFCOMM_RLS_CMD: { 1551 log_info("Received RLS command"); 1552 message_dlci = packet[payload_offset+2] >> 2; 1553 rfcomm_channel_event_rls_t event_rls; 1554 event_rls.super.type = CH_EVT_RCVD_RLS_CMD; 1555 event_rls.line_status = packet[payload_offset+3]; 1556 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_rls); 1557 break; 1558 } 1559 1560 case BT_RFCOMM_RLS_RSP: 1561 log_info("Received RLS response"); 1562 break; 1563 1564 // Following commands are handled by rfcomm_multiplexer_l2cap_packet_handler 1565 // case BT_RFCOMM_TEST_CMD: 1566 // case BT_RFCOMM_FCOFF_CMD: 1567 // case BT_RFCOMM_FCON_CMD: 1568 // everything else is an not supported command 1569 default: { 1570 log_error("Received unknown UIH command packet - 0x%02x", packet[payload_offset]); 1571 multiplexer->nsc_command = packet[payload_offset]; 1572 break; 1573 } 1574 } 1575 break; 1576 1577 default: 1578 log_error("Received unknown RFCOMM message type %x", packet[1]); 1579 break; 1580 } 1581 1582 // trigger next action - example W4_PN_RSP: transition to SEND_SABM which only depends on "can send" 1583 if (rfcomm_multiplexer_ready_to_send(multiplexer)){ 1584 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1585 } 1586 } 1587 1588 static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1589 1590 if (packet_type == HCI_EVENT_PACKET){ 1591 rfcomm_hci_event_handler(packet, size); 1592 return; 1593 } 1594 1595 // we only handle l2cap packets for: 1596 if (packet_type != L2CAP_DATA_PACKET) return; 1597 1598 // - multiplexer itself 1599 int handled = rfcomm_multiplexer_l2cap_packet_handler(channel, packet, size); 1600 1601 if (handled) return; 1602 1603 // - channel over open mutliplexer 1604 rfcomm_multiplexer_t * multiplexer = rfcomm_multiplexer_for_l2cap_cid(channel); 1605 if (!multiplexer || multiplexer->state != RFCOMM_MULTIPLEXER_OPEN) return; 1606 1607 // channel data ? 1608 // rfcomm: (0) addr [76543 server channel] [2 direction: initiator uses 1] [1 C/R: CMD by initiator = 1] [0 EA=1] 1609 const uint8_t frame_dlci = packet[0] >> 2; 1610 1611 if (frame_dlci && (packet[1] == BT_RFCOMM_UIH || packet[1] == BT_RFCOMM_UIH_PF)) { 1612 rfcomm_channel_packet_handler_uih(multiplexer, packet, size); 1613 return; 1614 } 1615 1616 rfcomm_channel_packet_handler(multiplexer, packet, size); 1617 } 1618 1619 static int rfcomm_channel_ready_for_open(rfcomm_channel_t *channel){ 1620 // note: exchanging MSC isn't neccessary to consider channel open 1621 // note: having outgoing credits is also not necessary to consider channel open 1622 // log_info("rfcomm_channel_ready_for_open state %u, flags needed %04x, current %04x, rf credits %u, l2cap credits %u ", channel->state, RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP|RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP|RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS, channel->state_var, channel->credits_outgoing, channel->multiplexer->l2cap_credits); 1623 // if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP) == 0) return 0; 1624 // if (channel->credits_outgoing == 0) return 0; 1625 log_info("rfcomm_channel_ready_for_open state %u, flags needed %04x, current %04x, rf credits %u", 1626 channel->state, RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP, channel->state_var, channel->credits_outgoing); 1627 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP) == 0) return 0; 1628 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS) == 0) return 0; 1629 1630 return 1; 1631 } 1632 1633 static int rfcomm_channel_ready_for_incoming_dlc_setup(rfcomm_channel_t * channel){ 1634 log_info("rfcomm_channel_ready_for_incoming_dlc_setup state var %04x", channel->state_var); 1635 // Client accept and SABM/UA is required, PN RSP is needed if PN was received 1636 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED) == 0) return 0; 1637 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM ) == 0) return 0; 1638 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_UA ) != 0) return 0; 1639 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP ) != 0) return 0; 1640 return 1; 1641 } 1642 1643 inline static void rfcomm_channel_state_add(rfcomm_channel_t *channel, RFCOMM_CHANNEL_STATE_VAR event){ 1644 channel->state_var = (RFCOMM_CHANNEL_STATE_VAR) (channel->state_var | event); 1645 } 1646 inline static void rfcomm_channel_state_remove(rfcomm_channel_t *channel, RFCOMM_CHANNEL_STATE_VAR event){ 1647 channel->state_var = (RFCOMM_CHANNEL_STATE_VAR) (channel->state_var & ~event); 1648 } 1649 1650 static int rfcomm_channel_ready_to_send(rfcomm_channel_t * channel){ 1651 switch (channel->state){ 1652 case RFCOMM_CHANNEL_SEND_UIH_PN: 1653 log_debug("ch-ready: state %u", channel->state); 1654 return 1; 1655 case RFCOMM_CHANNEL_SEND_SABM_W4_UA: 1656 log_debug("ch-ready: state %u", channel->state); 1657 return 1; 1658 case RFCOMM_CHANNEL_SEND_UA_AFTER_DISC: 1659 log_debug("ch-ready: state %u", channel->state); 1660 return 1; 1661 case RFCOMM_CHANNEL_SEND_DISC: 1662 log_debug("ch-ready: state %u", channel->state); 1663 return 1; 1664 case RFCOMM_CHANNEL_SEND_DM: 1665 log_debug("ch-ready: state %u", channel->state); 1666 return 1; 1667 case RFCOMM_CHANNEL_OPEN: 1668 if (channel->new_credits_incoming) { 1669 log_debug("ch-ready: channel open & new_credits_incoming") ; 1670 return 1; 1671 } 1672 break; 1673 case RFCOMM_CHANNEL_DLC_SETUP: 1674 if (channel->state_var & ( 1675 RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD | 1676 RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS 1677 )) { 1678 log_debug("ch-ready: channel dlc setup & send msc cmd or send credits") ; 1679 return 1; 1680 } 1681 break; 1682 1683 default: 1684 break; 1685 } 1686 1687 if (channel->state_var & ( 1688 RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP | 1689 RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_INFO | 1690 RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP | 1691 RFCOMM_CHANNEL_STATE_VAR_SEND_UA | 1692 RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP 1693 )){ 1694 log_debug("ch-ready: state %x, state var %x", channel->state, channel->state_var); 1695 return 1; 1696 } 1697 1698 if (channel->rls_line_status != RFCOMM_RLS_STATUS_INVALID) { 1699 log_debug("ch-ready: rls_line_status"); 1700 return 1; 1701 } 1702 1703 return 0; 1704 } 1705 1706 1707 static void rfcomm_channel_state_machine_with_channel(rfcomm_channel_t *channel, const rfcomm_channel_event_t *event, int * out_channel_valid){ 1708 1709 // log_info("rfcomm_channel_state_machine_with_channel: state %u, state_var %04x, event %u", channel->state, channel->state_var ,event->type); 1710 1711 // channel != NULL -> channel valid 1712 *out_channel_valid = 1; 1713 1714 rfcomm_multiplexer_t *multiplexer = channel->multiplexer; 1715 1716 // TODO: integrate in common switch 1717 if (event->type == CH_EVT_RCVD_DISC){ 1718 rfcomm_emit_channel_closed(channel); 1719 channel->state = RFCOMM_CHANNEL_SEND_UA_AFTER_DISC; 1720 return; 1721 } 1722 1723 // TODO: integrate in common switch 1724 if (event->type == CH_EVT_RCVD_DM){ 1725 log_info("Received DM message for #%u", channel->dlci); 1726 log_info("-> Closing channel locally for #%u", channel->dlci); 1727 rfcomm_emit_channel_closed(channel); 1728 rfcomm_channel_finalize(channel); 1729 *out_channel_valid = 0; 1730 return; 1731 } 1732 1733 // remote port negotiation command - just accept everything for now 1734 // 1735 // "The RPN command can be used before a new DLC is opened and should be used whenever the port settings change." 1736 // "The RPN command is specified as optional in TS 07.10, but it is mandatory to recognize and respond to it in RFCOMM. 1737 // (Although the handling of individual settings are implementation-dependent.)" 1738 // 1739 1740 // TODO: integrate in common switch 1741 if (event->type == CH_EVT_RCVD_RPN_CMD){ 1742 // control port parameters 1743 rfcomm_channel_event_rpn_t *event_rpn = (rfcomm_channel_event_rpn_t*) event; 1744 rfcomm_rpn_data_update(&channel->rpn_data, &event_rpn->data); 1745 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP); 1746 // notify client about new settings 1747 rfcomm_emit_port_configuration(channel); 1748 return; 1749 } 1750 1751 // TODO: integrate in common switch 1752 if (event->type == CH_EVT_RCVD_RPN_REQ){ 1753 // no values got accepted (no values have beens sent) 1754 channel->rpn_data.parameter_mask_0 = 0x00; 1755 channel->rpn_data.parameter_mask_1 = 0x00; 1756 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP); 1757 return; 1758 } 1759 1760 if (event->type == CH_EVT_RCVD_RLS_CMD){ 1761 rfcomm_channel_event_rls_t * event_rls = (rfcomm_channel_event_rls_t*) event; 1762 channel->rls_line_status = event_rls->line_status & 0x0f; 1763 log_info("CH_EVT_RCVD_RLS_CMD setting line status to 0x%0x", channel->rls_line_status); 1764 rfcomm_emit_remote_line_status(channel, event_rls->line_status); 1765 return; 1766 } 1767 1768 // TODO: integrate in common switch 1769 if (event->type == CH_EVT_READY_TO_SEND){ 1770 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP){ 1771 log_info("Sending Remote Port Negotiation RSP for #%u", channel->dlci); 1772 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP); 1773 rfcomm_send_uih_rpn_rsp(multiplexer, channel->dlci, &channel->rpn_data); 1774 return; 1775 } 1776 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP){ 1777 log_info("Sending MSC RSP for #%u", channel->dlci); 1778 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP); 1779 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP); 1780 rfcomm_send_uih_msc_rsp(multiplexer, channel->dlci, 0x8d); // ea=1,fc=0,rtc=1,rtr=1,ic=0,dv=1 1781 return; 1782 } 1783 if (channel->rls_line_status != RFCOMM_RLS_STATUS_INVALID){ 1784 log_info("Sending RLS RSP 0x%0x", channel->rls_line_status); 1785 uint8_t line_status = channel->rls_line_status; 1786 channel->rls_line_status = RFCOMM_RLS_STATUS_INVALID; 1787 rfcomm_send_uih_rls_rsp(multiplexer, channel->dlci, line_status); 1788 return; 1789 } 1790 } 1791 1792 // emit MSC status to app 1793 if (event->type == CH_EVT_RCVD_MSC_CMD){ 1794 // notify client about new settings 1795 rfcomm_channel_event_msc_t *event_msc = (rfcomm_channel_event_msc_t*) event; 1796 uint8_t modem_status_event[2+1]; 1797 modem_status_event[0] = RFCOMM_EVENT_REMOTE_MODEM_STATUS; 1798 modem_status_event[1] = 1; 1799 modem_status_event[2] = event_msc->modem_status; 1800 (channel->packet_handler)(HCI_EVENT_PACKET, channel->rfcomm_cid, (uint8_t*)&modem_status_event, sizeof(modem_status_event)); 1801 // no return, MSC_CMD will be handled by state machine below 1802 } 1803 1804 rfcomm_channel_event_pn_t * event_pn = (rfcomm_channel_event_pn_t*) event; 1805 1806 switch (channel->state) { 1807 case RFCOMM_CHANNEL_CLOSED: 1808 switch (event->type){ 1809 case CH_EVT_RCVD_SABM: 1810 log_info("-> Inform app"); 1811 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM); 1812 channel->state = RFCOMM_CHANNEL_INCOMING_SETUP; 1813 rfcomm_emit_connection_request(channel); 1814 break; 1815 case CH_EVT_RCVD_PN: 1816 rfcomm_channel_accept_pn(channel, event_pn); 1817 log_info("-> Inform app"); 1818 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_PN); 1819 channel->state = RFCOMM_CHANNEL_INCOMING_SETUP; 1820 rfcomm_emit_connection_request(channel); 1821 break; 1822 default: 1823 break; 1824 } 1825 break; 1826 1827 case RFCOMM_CHANNEL_INCOMING_SETUP: 1828 switch (event->type){ 1829 case CH_EVT_RCVD_SABM: 1830 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM); 1831 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED) { 1832 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_UA); 1833 } 1834 break; 1835 case CH_EVT_RCVD_PN: 1836 rfcomm_channel_accept_pn(channel, event_pn); 1837 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_PN); 1838 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED) { 1839 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP); 1840 } 1841 break; 1842 case CH_EVT_READY_TO_SEND: 1843 // if / else if is used to check for state transition after sending 1844 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP){ 1845 log_info("Sending UIH Parameter Negotiation Respond for #%u", channel->dlci); 1846 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP); 1847 rfcomm_send_uih_pn_response(multiplexer, channel->dlci, channel->pn_priority, channel->max_frame_size); 1848 } else if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_UA){ 1849 log_info("Sending UA #%u", channel->dlci); 1850 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_UA); 1851 rfcomm_send_ua(multiplexer, channel->dlci); 1852 } 1853 if (rfcomm_channel_ready_for_incoming_dlc_setup(channel)){ 1854 log_info("Incomping setup done, requesting send MSC CMD and send Credits"); 1855 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD); 1856 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS); 1857 channel->state = RFCOMM_CHANNEL_DLC_SETUP; 1858 } 1859 break; 1860 default: 1861 break; 1862 } 1863 break; 1864 1865 case RFCOMM_CHANNEL_W4_MULTIPLEXER: 1866 switch (event->type) { 1867 case CH_EVT_MULTIPLEXER_READY: 1868 log_info("Muliplexer opened, sending UIH PN next"); 1869 channel->state = RFCOMM_CHANNEL_SEND_UIH_PN; 1870 break; 1871 default: 1872 break; 1873 } 1874 break; 1875 1876 case RFCOMM_CHANNEL_SEND_UIH_PN: 1877 switch (event->type) { 1878 case CH_EVT_READY_TO_SEND: 1879 log_info("Sending UIH Parameter Negotiation Command for #%u (channel 0x%p)", channel->dlci, channel ); 1880 channel->state = RFCOMM_CHANNEL_W4_PN_RSP; 1881 rfcomm_send_uih_pn_command(multiplexer, channel->dlci, channel->max_frame_size); 1882 break; 1883 default: 1884 break; 1885 } 1886 break; 1887 1888 case RFCOMM_CHANNEL_W4_PN_RSP: 1889 switch (event->type){ 1890 case CH_EVT_RCVD_PN_RSP: 1891 // update max frame size 1892 if (channel->max_frame_size > event_pn->max_frame_size) { 1893 channel->max_frame_size = event_pn->max_frame_size; 1894 } 1895 // new credits 1896 channel->credits_outgoing = event_pn->credits_outgoing; 1897 channel->state = RFCOMM_CHANNEL_SEND_SABM_W4_UA; 1898 break; 1899 default: 1900 break; 1901 } 1902 break; 1903 1904 case RFCOMM_CHANNEL_SEND_SABM_W4_UA: 1905 switch (event->type) { 1906 case CH_EVT_READY_TO_SEND: 1907 log_info("Sending SABM #%u", channel->dlci); 1908 channel->state = RFCOMM_CHANNEL_W4_UA; 1909 rfcomm_send_sabm(multiplexer, channel->dlci); 1910 break; 1911 default: 1912 break; 1913 } 1914 break; 1915 1916 case RFCOMM_CHANNEL_W4_UA: 1917 switch (event->type){ 1918 case CH_EVT_RCVD_UA: 1919 channel->state = RFCOMM_CHANNEL_DLC_SETUP; 1920 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD); 1921 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS); 1922 break; 1923 default: 1924 break; 1925 } 1926 break; 1927 1928 case RFCOMM_CHANNEL_DLC_SETUP: 1929 switch (event->type){ 1930 case CH_EVT_RCVD_MSC_CMD: 1931 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_CMD); 1932 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP); 1933 break; 1934 case CH_EVT_RCVD_MSC_RSP: 1935 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP); 1936 break; 1937 1938 case CH_EVT_READY_TO_SEND: 1939 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD){ 1940 log_info("Sending MSC CMD for #%u", channel->dlci); 1941 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD); 1942 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_CMD); 1943 rfcomm_send_uih_msc_cmd(multiplexer, channel->dlci , 0x8d); // ea=1,fc=0,rtc=1,rtr=1,ic=0,dv=1 1944 break; 1945 } 1946 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS){ 1947 log_info("Providing credits for #%u", channel->dlci); 1948 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS); 1949 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS); 1950 1951 if (channel->new_credits_incoming) { 1952 uint8_t new_credits = channel->new_credits_incoming; 1953 channel->new_credits_incoming = 0; 1954 rfcomm_channel_send_credits(channel, new_credits); 1955 } 1956 break; 1957 1958 } 1959 break; 1960 default: 1961 break; 1962 } 1963 // finally done? 1964 if (rfcomm_channel_ready_for_open(channel)){ 1965 channel->state = RFCOMM_CHANNEL_OPEN; 1966 rfcomm_channel_opened(channel); 1967 } 1968 break; 1969 1970 case RFCOMM_CHANNEL_OPEN: 1971 switch (event->type){ 1972 case CH_EVT_RCVD_MSC_CMD: 1973 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP); 1974 break; 1975 case CH_EVT_READY_TO_SEND: 1976 if (channel->new_credits_incoming) { 1977 uint8_t new_credits = channel->new_credits_incoming; 1978 channel->new_credits_incoming = 0; 1979 rfcomm_channel_send_credits(channel, new_credits); 1980 break; 1981 } 1982 break; 1983 case CH_EVT_RCVD_CREDITS: 1984 rfcomm_notify_channel_can_send(); 1985 break; 1986 default: 1987 break; 1988 } 1989 break; 1990 1991 case RFCOMM_CHANNEL_SEND_DM: 1992 switch (event->type) { 1993 case CH_EVT_READY_TO_SEND: 1994 log_info("Sending DM_PF for #%u", channel->dlci); 1995 // don't emit channel closed - channel was never open 1996 channel->state = RFCOMM_CHANNEL_CLOSED; 1997 rfcomm_send_dm_pf(multiplexer, channel->dlci); 1998 rfcomm_channel_finalize(channel); 1999 *out_channel_valid = 0; 2000 break; 2001 default: 2002 break; 2003 } 2004 break; 2005 2006 case RFCOMM_CHANNEL_SEND_DISC: 2007 switch (event->type) { 2008 case CH_EVT_READY_TO_SEND: 2009 channel->state = RFCOMM_CHANNEL_W4_UA_AFTER_DISC; 2010 rfcomm_send_disc(multiplexer, channel->dlci); 2011 break; 2012 default: 2013 break; 2014 } 2015 break; 2016 2017 case RFCOMM_CHANNEL_W4_UA_AFTER_DISC: 2018 switch (event->type){ 2019 case CH_EVT_RCVD_UA: 2020 channel->state = RFCOMM_CHANNEL_CLOSED; 2021 rfcomm_emit_channel_closed(channel); 2022 rfcomm_channel_finalize(channel); 2023 *out_channel_valid = 0; 2024 break; 2025 default: 2026 break; 2027 } 2028 break; 2029 2030 case RFCOMM_CHANNEL_SEND_UA_AFTER_DISC: 2031 switch (event->type) { 2032 case CH_EVT_READY_TO_SEND: 2033 log_info("Sending UA after DISC for #%u", channel->dlci); 2034 channel->state = RFCOMM_CHANNEL_CLOSED; 2035 rfcomm_send_ua(multiplexer, channel->dlci); 2036 rfcomm_channel_finalize(channel); 2037 *out_channel_valid = 0; 2038 break; 2039 default: 2040 break; 2041 } 2042 break; 2043 2044 default: 2045 break; 2046 } 2047 } 2048 2049 // MARK: RFCOMM BTstack API 2050 2051 void rfcomm_init(void){ 2052 rfcomm_client_cid_generator = 0; 2053 rfcomm_multiplexers = NULL; 2054 rfcomm_services = NULL; 2055 rfcomm_channels = NULL; 2056 rfcomm_security_level = LEVEL_2; 2057 } 2058 2059 void rfcomm_set_required_security_level(gap_security_level_t security_level){ 2060 rfcomm_security_level = security_level; 2061 } 2062 2063 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){ 2064 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2065 if (!channel){ 2066 log_error("rfcomm_send cid 0x%02x doesn't exist!", rfcomm_cid); 2067 return 0; 2068 } 2069 return rfcomm_channel_can_send(channel); 2070 } 2071 2072 void rfcomm_request_can_send_now_event(uint16_t rfcomm_cid){ 2073 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2074 if (!channel){ 2075 log_error("rfcomm_send cid 0x%02x doesn't exist!", rfcomm_cid); 2076 return; 2077 } 2078 channel->waiting_for_can_send_now = 1; 2079 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2080 } 2081 2082 static int rfcomm_assert_send_valid(rfcomm_channel_t * channel , uint16_t len){ 2083 if (len > channel->max_frame_size){ 2084 log_error("rfcomm_send cid 0x%02x, rfcomm data lenght exceeds MTU!", channel->rfcomm_cid); 2085 return RFCOMM_DATA_LEN_EXCEEDS_MTU; 2086 } 2087 2088 if (!channel->credits_outgoing){ 2089 log_info("rfcomm_send cid 0x%02x, no rfcomm outgoing credits!", channel->rfcomm_cid); 2090 return RFCOMM_NO_OUTGOING_CREDITS; 2091 } 2092 2093 if ((channel->multiplexer->fcon & 1) == 0){ 2094 log_info("rfcomm_send cid 0x%02x, aggregate flow off!", channel->rfcomm_cid); 2095 return RFCOMM_AGGREGATE_FLOW_OFF; 2096 } 2097 return 0; 2098 } 2099 2100 // pre: rfcomm_can_send_packet_now(rfcomm_cid) == true 2101 int rfcomm_reserve_packet_buffer(void){ 2102 return l2cap_reserve_packet_buffer(); 2103 } 2104 2105 void rfcomm_release_packet_buffer(void){ 2106 l2cap_release_packet_buffer(); 2107 } 2108 2109 uint8_t * rfcomm_get_outgoing_buffer(void){ 2110 uint8_t * rfcomm_out_buffer = l2cap_get_outgoing_buffer(); 2111 // address + control + length (16) + no credit field 2112 return &rfcomm_out_buffer[4]; 2113 } 2114 2115 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid){ 2116 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2117 if (!channel){ 2118 log_error("rfcomm_get_max_frame_size cid 0x%02x doesn't exist!", rfcomm_cid); 2119 return 0; 2120 } 2121 return channel->max_frame_size; 2122 } 2123 2124 int rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len){ 2125 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2126 if (!channel){ 2127 log_error("rfcomm_send_prepared cid 0x%02x doesn't exist!", rfcomm_cid); 2128 return 0; 2129 } 2130 2131 int err = rfcomm_assert_send_valid(channel, len); 2132 if (err) return err; 2133 if (!l2cap_can_send_prepared_packet_now(channel->multiplexer->l2cap_cid)){ 2134 log_error("rfcomm_send_prepared: l2cap cannot send now"); 2135 return BTSTACK_ACL_BUFFERS_FULL; 2136 } 2137 2138 // send might cause l2cap to emit new credits, update counters first 2139 if (len){ 2140 channel->credits_outgoing--; 2141 } else { 2142 log_info("sending empty RFCOMM packet for cid %02x", rfcomm_cid); 2143 } 2144 2145 int result = rfcomm_send_uih_prepared(channel->multiplexer, channel->dlci, len); 2146 2147 if (result != 0) { 2148 if (len) { 2149 channel->credits_outgoing++; 2150 } 2151 log_error("rfcomm_send_prepared: error %d", result); 2152 return result; 2153 } 2154 2155 return result; 2156 } 2157 2158 int rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){ 2159 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2160 if (!channel){ 2161 log_error("cid 0x%02x doesn't exist!", rfcomm_cid); 2162 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2163 } 2164 2165 int err = rfcomm_assert_send_valid(channel, len); 2166 if (err) return err; 2167 if (!l2cap_can_send_packet_now(channel->multiplexer->l2cap_cid)){ 2168 log_error("rfcomm_send_internal: l2cap cannot send now"); 2169 return BTSTACK_ACL_BUFFERS_FULL; 2170 } 2171 2172 rfcomm_reserve_packet_buffer(); 2173 uint8_t * rfcomm_payload = rfcomm_get_outgoing_buffer(); 2174 memcpy(rfcomm_payload, data, len); 2175 err = rfcomm_send_prepared(rfcomm_cid, len); 2176 if (err){ 2177 rfcomm_release_packet_buffer(); 2178 } 2179 return err; 2180 } 2181 2182 // Sends Local Lnie Status, see LINE_STATUS_.. 2183 int rfcomm_send_local_line_status(uint16_t rfcomm_cid, uint8_t line_status){ 2184 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2185 if (!channel){ 2186 log_error("rfcomm_send_local_line_status cid 0x%02x doesn't exist!", rfcomm_cid); 2187 return 0; 2188 } 2189 return rfcomm_send_uih_rls_cmd(channel->multiplexer, channel->dlci, line_status); 2190 } 2191 2192 // Sned local modem status. see MODEM_STAUS_.. 2193 int rfcomm_send_modem_status(uint16_t rfcomm_cid, uint8_t modem_status){ 2194 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2195 if (!channel){ 2196 log_error("rfcomm_send_modem_status cid 0x%02x doesn't exist!", rfcomm_cid); 2197 return 0; 2198 } 2199 return rfcomm_send_uih_msc_cmd(channel->multiplexer, channel->dlci, modem_status); 2200 } 2201 2202 // Configure remote port 2203 int rfcomm_send_port_configuration(uint16_t rfcomm_cid, rpn_baud_t baud_rate, rpn_data_bits_t data_bits, rpn_stop_bits_t stop_bits, rpn_parity_t parity, rpn_flow_control_t flow_control){ 2204 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2205 if (!channel){ 2206 log_error("rfcomm_send_port_configuration cid 0x%02x doesn't exist!", rfcomm_cid); 2207 return 0; 2208 } 2209 rfcomm_rpn_data_t rpn_data; 2210 rpn_data.baud_rate = baud_rate; 2211 rpn_data.flags = data_bits | (stop_bits << 2) | (parity << 3); 2212 rpn_data.flow_control = flow_control; 2213 rpn_data.xon = 0; 2214 rpn_data.xoff = 0; 2215 rpn_data.parameter_mask_0 = 0x1f; // all but xon/xoff 2216 rpn_data.parameter_mask_1 = 0x3f; // all flow control options 2217 return rfcomm_send_uih_rpn_cmd(channel->multiplexer, channel->dlci, &rpn_data); 2218 } 2219 2220 // Query remote port 2221 int rfcomm_query_port_configuration(uint16_t rfcomm_cid){ 2222 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2223 if (!channel){ 2224 log_error("rfcomm_query_port_configuration cid 0x%02x doesn't exist!", rfcomm_cid); 2225 return 0; 2226 } 2227 return rfcomm_send_uih_rpn_req(channel->multiplexer, channel->dlci); 2228 } 2229 2230 2231 static uint8_t rfcomm_channel_create_internal(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint8_t incoming_flow_control, uint8_t initial_credits, uint16_t * out_rfcomm_cid){ 2232 log_info("RFCOMM_CREATE_CHANNEL addr %s channel #%u init credits %u", bd_addr_to_str(addr), server_channel, initial_credits); 2233 2234 // create new multiplexer if necessary 2235 uint8_t status = 0; 2236 uint8_t dlci = 0; 2237 int new_multiplexer = 0; 2238 rfcomm_channel_t * channel = NULL; 2239 rfcomm_multiplexer_t * multiplexer = rfcomm_multiplexer_for_addr(addr); 2240 if (!multiplexer) { 2241 multiplexer = rfcomm_multiplexer_create_for_addr(addr); 2242 if (!multiplexer){ 2243 status = BTSTACK_MEMORY_ALLOC_FAILED; 2244 goto fail; 2245 } 2246 multiplexer->outgoing = 1; 2247 multiplexer->state = RFCOMM_MULTIPLEXER_W4_CONNECT; 2248 new_multiplexer = 1; 2249 } 2250 2251 // check if channel for this remote service already exists 2252 dlci = (server_channel << 1) | (multiplexer->outgoing ^ 1); 2253 channel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, dlci); 2254 if (channel){ 2255 status = RFCOMM_CHANNEL_ALREADY_REGISTERED; 2256 goto fail; 2257 } 2258 2259 // prepare channel 2260 channel = rfcomm_channel_create(multiplexer, NULL, server_channel); 2261 if (!channel){ 2262 status = BTSTACK_MEMORY_ALLOC_FAILED; 2263 goto fail; 2264 } 2265 2266 // rfcomm_cid is already assigned by rfcomm_channel_create 2267 channel->incoming_flow_control = incoming_flow_control; 2268 channel->new_credits_incoming = initial_credits; 2269 channel->packet_handler = packet_handler; 2270 2271 // return rfcomm_cid 2272 if (out_rfcomm_cid){ 2273 *out_rfcomm_cid = channel->rfcomm_cid; 2274 } 2275 2276 // start multiplexer setup 2277 if (multiplexer->state != RFCOMM_MULTIPLEXER_OPEN) { 2278 channel->state = RFCOMM_CHANNEL_W4_MULTIPLEXER; 2279 uint16_t l2cap_cid = 0; 2280 status = l2cap_create_channel(rfcomm_packet_handler, addr, BLUETOOTH_PROTOCOL_RFCOMM, l2cap_max_mtu(), &l2cap_cid); 2281 if (status) goto fail; 2282 multiplexer->l2cap_cid = l2cap_cid; 2283 return 0; 2284 } 2285 2286 channel->state = RFCOMM_CHANNEL_SEND_UIH_PN; 2287 2288 // start connecting, if multiplexer is already up and running 2289 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 2290 return 0; 2291 2292 fail: 2293 if (new_multiplexer) btstack_memory_rfcomm_multiplexer_free(multiplexer); 2294 if (channel) btstack_memory_rfcomm_channel_free(channel); 2295 return status; 2296 } 2297 2298 uint8_t rfcomm_create_channel_with_initial_credits(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint8_t initial_credits, uint16_t * out_rfcomm_cid){ 2299 return rfcomm_channel_create_internal(packet_handler, addr, server_channel, 1, initial_credits, out_rfcomm_cid); 2300 } 2301 2302 uint8_t rfcomm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint16_t * out_rfcomm_cid){ 2303 return rfcomm_channel_create_internal(packet_handler, addr, server_channel, 0, RFCOMM_CREDITS, out_rfcomm_cid); 2304 } 2305 2306 void rfcomm_disconnect(uint16_t rfcomm_cid){ 2307 log_info("RFCOMM_DISCONNECT cid 0x%02x", rfcomm_cid); 2308 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2309 if (!channel) return; 2310 2311 channel->state = RFCOMM_CHANNEL_SEND_DISC; 2312 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2313 } 2314 2315 static uint8_t rfcomm_register_service_internal(btstack_packet_handler_t packet_handler, 2316 uint8_t channel, uint16_t max_frame_size, uint8_t incoming_flow_control, uint8_t initial_credits){ 2317 2318 log_info("RFCOMM_REGISTER_SERVICE channel #%u mtu %u flow_control %u credits %u", 2319 channel, max_frame_size, incoming_flow_control, initial_credits); 2320 2321 // check if already registered 2322 rfcomm_service_t * service = rfcomm_service_for_channel(channel); 2323 if (service){ 2324 return RFCOMM_CHANNEL_ALREADY_REGISTERED; 2325 } 2326 2327 // alloc structure 2328 service = btstack_memory_rfcomm_service_get(); 2329 if (!service) { 2330 return BTSTACK_MEMORY_ALLOC_FAILED; 2331 } 2332 2333 // register with l2cap if not registered before, max MTU 2334 if (btstack_linked_list_empty(&rfcomm_services)){ 2335 l2cap_register_service(rfcomm_packet_handler, BLUETOOTH_PROTOCOL_RFCOMM, 0xffff, rfcomm_security_level); 2336 } 2337 2338 // fill in 2339 service->packet_handler = packet_handler; 2340 service->server_channel = channel; 2341 service->max_frame_size = max_frame_size; 2342 service->incoming_flow_control = incoming_flow_control; 2343 service->incoming_initial_credits = initial_credits; 2344 2345 // add to services list 2346 btstack_linked_list_add(&rfcomm_services, (btstack_linked_item_t *) service); 2347 2348 return 0; 2349 } 2350 2351 uint8_t rfcomm_register_service_with_initial_credits(btstack_packet_handler_t packet_handler, 2352 uint8_t channel, uint16_t max_frame_size, uint8_t initial_credits){ 2353 2354 return rfcomm_register_service_internal(packet_handler, channel, max_frame_size, 1, initial_credits); 2355 } 2356 2357 uint8_t rfcomm_register_service(btstack_packet_handler_t packet_handler, uint8_t channel, 2358 uint16_t max_frame_size){ 2359 2360 return rfcomm_register_service_internal(packet_handler, channel, max_frame_size, 0,RFCOMM_CREDITS); 2361 } 2362 2363 void rfcomm_unregister_service(uint8_t service_channel){ 2364 log_info("RFCOMM_UNREGISTER_SERVICE #%u", service_channel); 2365 rfcomm_service_t *service = rfcomm_service_for_channel(service_channel); 2366 if (!service) return; 2367 btstack_linked_list_remove(&rfcomm_services, (btstack_linked_item_t *) service); 2368 btstack_memory_rfcomm_service_free(service); 2369 2370 // unregister if no services active 2371 if (btstack_linked_list_empty(&rfcomm_services)){ 2372 // bt_send_cmd(&l2cap_unregister_service, BLUETOOTH_PROTOCOL_RFCOMM); 2373 l2cap_unregister_service(BLUETOOTH_PROTOCOL_RFCOMM); 2374 } 2375 } 2376 2377 void rfcomm_accept_connection(uint16_t rfcomm_cid){ 2378 log_info("RFCOMM_ACCEPT_CONNECTION cid 0x%02x", rfcomm_cid); 2379 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2380 if (!channel) return; 2381 switch (channel->state) { 2382 case RFCOMM_CHANNEL_INCOMING_SETUP: 2383 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED); 2384 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_RCVD_PN){ 2385 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP); 2386 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2387 } 2388 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM){ 2389 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_UA); 2390 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2391 } 2392 // at least one of { PN RSP, UA } needs to be sent 2393 // state transistion incoming setup -> dlc setup happens in rfcomm_run after these have been sent 2394 break; 2395 default: 2396 break; 2397 } 2398 2399 } 2400 2401 void rfcomm_decline_connection(uint16_t rfcomm_cid){ 2402 log_info("RFCOMM_DECLINE_CONNECTION cid 0x%02x", rfcomm_cid); 2403 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2404 if (!channel) return; 2405 switch (channel->state) { 2406 case RFCOMM_CHANNEL_INCOMING_SETUP: 2407 channel->state = RFCOMM_CHANNEL_SEND_DM; 2408 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2409 break; 2410 default: 2411 break; 2412 } 2413 } 2414 2415 void rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits){ 2416 log_info("RFCOMM_GRANT_CREDITS cid 0x%02x credits %u", rfcomm_cid, credits); 2417 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2418 if (!channel) return; 2419 if (!channel->incoming_flow_control) return; 2420 channel->new_credits_incoming += credits; 2421 2422 // process 2423 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2424 } 2425 2426 2427