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); 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 rfcomm_channel_state_machine_with_channel(channel, &event); 840 if (rfcomm_channel_ready_to_send(channel)){ 841 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 842 } 843 } 844 rfcomm_multiplexer_prepare_idle_timer(multiplexer); 845 846 // request can send now for multiplexer if ready 847 if (rfcomm_multiplexer_ready_to_send(multiplexer)){ 848 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 849 } 850 } 851 852 static void rfcomm_handle_can_send_now(uint16_t l2cap_cid){ 853 854 log_debug("rfcomm_handle_can_send_now enter: %u", l2cap_cid); 855 856 btstack_linked_list_iterator_t it; 857 int token_consumed = 0; 858 859 // forward token to multiplexer 860 btstack_linked_list_iterator_init(&it, &rfcomm_multiplexers); 861 while (!token_consumed && btstack_linked_list_iterator_has_next(&it)){ 862 rfcomm_multiplexer_t * multiplexer = (rfcomm_multiplexer_t *) btstack_linked_list_iterator_next(&it); 863 if (multiplexer->l2cap_cid != l2cap_cid) continue; 864 if (rfcomm_multiplexer_ready_to_send(multiplexer)){ 865 log_debug("rfcomm_handle_can_send_now enter: multiplexer token"); 866 token_consumed = 1; 867 rfcomm_multiplexer_state_machine(multiplexer, MULT_EV_READY_TO_SEND); 868 } 869 } 870 871 // forward token to channel state machine 872 btstack_linked_list_iterator_init(&it, &rfcomm_channels); 873 while (!token_consumed && btstack_linked_list_iterator_has_next(&it)){ 874 rfcomm_channel_t * channel = (rfcomm_channel_t *) btstack_linked_list_iterator_next(&it); 875 if (channel->multiplexer->l2cap_cid != l2cap_cid) continue; 876 // channel state machine 877 if (rfcomm_channel_ready_to_send(channel)){ 878 log_debug("rfcomm_handle_can_send_now enter: channel token"); 879 token_consumed = 1; 880 const rfcomm_channel_event_t event = { CH_EVT_READY_TO_SEND, 0 }; 881 rfcomm_channel_state_machine_with_channel(channel, &event); 882 } 883 } 884 885 // forward token to client 886 btstack_linked_list_iterator_init(&it, &rfcomm_channels); 887 while (!token_consumed && btstack_linked_list_iterator_has_next(&it)){ 888 rfcomm_channel_t * channel = (rfcomm_channel_t *) btstack_linked_list_iterator_next(&it); 889 if (channel->multiplexer->l2cap_cid != l2cap_cid) continue; 890 // client waiting for can send now 891 if (!channel->waiting_for_can_send_now) continue; 892 if (!channel->credits_outgoing) continue; 893 if ((channel->multiplexer->fcon & 1) == 0) continue; 894 895 log_debug("rfcomm_handle_can_send_now enter: client token"); 896 token_consumed = 1; 897 channel->waiting_for_can_send_now = 0; 898 rfcomm_emit_can_send_now(channel); 899 } 900 901 // if token was consumed, request another one 902 if (token_consumed) { 903 l2cap_request_can_send_now_event(l2cap_cid); 904 } 905 906 log_debug("rfcomm_handle_can_send_now exit"); 907 } 908 909 static void rfcomm_multiplexer_set_state_and_request_can_send_now_event(rfcomm_multiplexer_t * multiplexer, RFCOMM_MULTIPLEXER_STATE state){ 910 multiplexer->state = state; 911 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 912 } 913 914 /** 915 * @return handled packet 916 */ 917 static int rfcomm_hci_event_handler(uint8_t *packet, uint16_t size){ 918 919 UNUSED(size); // ok: handling own l2cap events 920 921 bd_addr_t event_addr; 922 uint16_t psm; 923 uint16_t l2cap_cid; 924 hci_con_handle_t con_handle; 925 rfcomm_multiplexer_t *multiplexer = NULL; 926 uint8_t status; 927 928 switch (hci_event_packet_get_type(packet)) { 929 930 // accept incoming rfcomm connection if no multiplexer exists yet 931 case L2CAP_EVENT_INCOMING_CONNECTION: 932 // data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16) 933 reverse_bd_addr(&packet[2], event_addr); 934 con_handle = little_endian_read_16(packet, 8); 935 psm = little_endian_read_16(packet, 10); 936 l2cap_cid = little_endian_read_16(packet, 12); 937 938 if (psm != BLUETOOTH_PROTOCOL_RFCOMM) break; 939 940 multiplexer = rfcomm_multiplexer_for_addr(event_addr); 941 942 if (multiplexer) { 943 log_info("INCOMING_CONNECTION (l2cap_cid 0x%02x) for BLUETOOTH_PROTOCOL_RFCOMM => decline - multiplexer already exists", l2cap_cid); 944 l2cap_decline_connection(l2cap_cid); 945 return 1; 946 } 947 948 // create and inititialize new multiplexer instance (incoming) 949 multiplexer = rfcomm_multiplexer_create_for_addr(event_addr); 950 if (!multiplexer){ 951 log_info("INCOMING_CONNECTION (l2cap_cid 0x%02x) for BLUETOOTH_PROTOCOL_RFCOMM => decline - no memory left", l2cap_cid); 952 l2cap_decline_connection(l2cap_cid); 953 return 1; 954 } 955 956 multiplexer->con_handle = con_handle; 957 multiplexer->l2cap_cid = l2cap_cid; 958 // 959 multiplexer->state = RFCOMM_MULTIPLEXER_W4_SABM_0; 960 log_info("L2CAP_EVENT_INCOMING_CONNECTION (l2cap_cid 0x%02x) for BLUETOOTH_PROTOCOL_RFCOMM => accept", l2cap_cid); 961 l2cap_accept_connection(l2cap_cid); 962 return 1; 963 964 // l2cap connection opened -> store l2cap_cid, remote_addr 965 case L2CAP_EVENT_CHANNEL_OPENED: 966 967 if (little_endian_read_16(packet, 11) != BLUETOOTH_PROTOCOL_RFCOMM) break; 968 969 status = packet[2]; 970 log_info("L2CAP_EVENT_CHANNEL_OPENED for BLUETOOTH_PROTOCOL_RFCOMM, status %u", status); 971 972 // get multiplexer for remote addr 973 con_handle = little_endian_read_16(packet, 9); 974 l2cap_cid = little_endian_read_16(packet, 13); 975 reverse_bd_addr(&packet[3], event_addr); 976 multiplexer = rfcomm_multiplexer_for_addr(event_addr); 977 if (!multiplexer) { 978 log_error("L2CAP_EVENT_CHANNEL_OPENED but no multiplexer prepared"); 979 return 1; 980 } 981 982 // on l2cap open error discard everything 983 if (status){ 984 985 // remove (potential) timer 986 rfcomm_multiplexer_stop_timer(multiplexer); 987 988 // mark multiplexer as shutting down 989 multiplexer->state = RFCOMM_MULTIPLEXER_SHUTTING_DOWN; 990 991 // emit rfcomm_channel_opened with status and free channel 992 // note: repeatedly go over list until full iteration causes no further change 993 int done; 994 do { 995 done = 1; 996 btstack_linked_item_t * it = (btstack_linked_item_t *) &rfcomm_channels; 997 while (it->next) { 998 rfcomm_channel_t * channel = (rfcomm_channel_t *) it->next; 999 if (channel->multiplexer == multiplexer){ 1000 done = 0; 1001 rfcomm_emit_channel_opened(channel, status); 1002 btstack_linked_list_remove(&rfcomm_channels, (btstack_linked_item_t *) channel); 1003 btstack_memory_rfcomm_channel_free(channel); 1004 break; 1005 } else { 1006 it = it->next; 1007 } 1008 } 1009 } while (!done); 1010 1011 // free multiplexer 1012 rfcomm_multiplexer_free(multiplexer); 1013 return 1; 1014 } 1015 1016 // following could be: rfcom_multiplexer_state_machein(..., EVENT_L2CAP_OPENED) 1017 1018 if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_CONNECT) { 1019 log_info("L2CAP_EVENT_CHANNEL_OPENED: outgoing connection"); 1020 // wrong remote addr 1021 if (bd_addr_cmp(event_addr, multiplexer->remote_addr)) break; 1022 multiplexer->l2cap_cid = l2cap_cid; 1023 multiplexer->con_handle = con_handle; 1024 // send SABM #0 1025 rfcomm_multiplexer_set_state_and_request_can_send_now_event(multiplexer, RFCOMM_MULTIPLEXER_SEND_SABM_0); 1026 1027 } else { // multiplexer->state == RFCOMM_MULTIPLEXER_W4_SABM_0 1028 1029 // set max frame size based on l2cap MTU 1030 multiplexer->max_frame_size = rfcomm_max_frame_size_for_l2cap_mtu(little_endian_read_16(packet, 17)); 1031 } 1032 return 1; 1033 1034 // l2cap disconnect -> state = RFCOMM_MULTIPLEXER_CLOSED; 1035 1036 // Notify channel packet handler if they can send now 1037 case L2CAP_EVENT_CAN_SEND_NOW: 1038 l2cap_cid = l2cap_event_can_send_now_get_local_cid(packet); 1039 rfcomm_handle_can_send_now(l2cap_cid); 1040 return 1; 1041 1042 case L2CAP_EVENT_CHANNEL_CLOSED: 1043 // data: event (8), len(8), channel (16) 1044 l2cap_cid = little_endian_read_16(packet, 2); 1045 multiplexer = rfcomm_multiplexer_for_l2cap_cid(l2cap_cid); 1046 log_info("L2CAP_EVENT_CHANNEL_CLOSED cid 0x%0x, mult %p", l2cap_cid, multiplexer); 1047 if (!multiplexer) break; 1048 log_info("L2CAP_EVENT_CHANNEL_CLOSED state %u", multiplexer->state); 1049 // no need to call l2cap_disconnect here, as it's already closed 1050 rfcomm_multiplexer_finalize(multiplexer); 1051 return 1; 1052 1053 default: 1054 break; 1055 } 1056 return 0; 1057 } 1058 1059 static int rfcomm_multiplexer_l2cap_packet_handler(uint16_t channel, uint8_t *packet, uint16_t size){ 1060 // get or create a multiplexer for a certain device 1061 rfcomm_multiplexer_t *multiplexer = rfcomm_multiplexer_for_l2cap_cid(channel); 1062 if (!multiplexer) return 0; 1063 1064 uint16_t l2cap_cid = multiplexer->l2cap_cid; 1065 1066 // but only care for multiplexer control channel 1067 uint8_t frame_dlci = packet[0] >> 2; 1068 if (frame_dlci) return 0; 1069 const uint8_t length_offset = (packet[2] & 1) ^ 1; // to be used for pos >= 3 1070 const uint8_t credit_offset = ((packet[1] & BT_RFCOMM_UIH_PF) == BT_RFCOMM_UIH_PF) ? 1 : 0; // credits for uih_pf frames 1071 const uint8_t payload_offset = 3 + length_offset + credit_offset; 1072 switch (packet[1]){ 1073 1074 case BT_RFCOMM_SABM: 1075 if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_SABM_0){ 1076 log_info("Received SABM #0"); 1077 multiplexer->outgoing = 0; 1078 rfcomm_multiplexer_set_state_and_request_can_send_now_event(multiplexer, RFCOMM_MULTIPLEXER_SEND_UA_0); 1079 return 1; 1080 } 1081 break; 1082 1083 case BT_RFCOMM_UA: 1084 if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_UA_0) { 1085 // UA #0 -> send UA #0, state = RFCOMM_MULTIPLEXER_OPEN 1086 log_info("Received UA #0 "); 1087 rfcomm_multiplexer_opened(multiplexer); 1088 return 1; 1089 } 1090 break; 1091 1092 case BT_RFCOMM_DISC: 1093 // DISC #0 -> send UA #0, close multiplexer 1094 log_info("Received DISC #0, (ougoing = %u)", multiplexer->outgoing); 1095 rfcomm_multiplexer_set_state_and_request_can_send_now_event(multiplexer, RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC); 1096 return 1; 1097 1098 case BT_RFCOMM_DM: 1099 // DM #0 - we shouldn't get this, just give up 1100 log_info("Received DM #0"); 1101 log_info("-> Closing down multiplexer"); 1102 rfcomm_multiplexer_finalize(multiplexer); 1103 l2cap_disconnect(l2cap_cid, 0x13); 1104 return 1; 1105 1106 case BT_RFCOMM_UIH: 1107 if (packet[payload_offset] == BT_RFCOMM_CLD_CMD){ 1108 // Multiplexer close down (CLD) -> close mutliplexer 1109 log_info("Received Multiplexer close down command"); 1110 log_info("-> Closing down multiplexer"); 1111 rfcomm_multiplexer_finalize(multiplexer); 1112 l2cap_disconnect(l2cap_cid, 0x13); 1113 return 1; 1114 } 1115 switch (packet[payload_offset]){ 1116 case BT_RFCOMM_CLD_CMD: 1117 // Multiplexer close down (CLD) -> close mutliplexer 1118 log_info("Received Multiplexer close down command"); 1119 log_info("-> Closing down multiplexer"); 1120 rfcomm_multiplexer_finalize(multiplexer); 1121 l2cap_disconnect(l2cap_cid, 0x13); 1122 return 1; 1123 1124 case BT_RFCOMM_FCON_CMD: 1125 multiplexer->fcon = 0x81; 1126 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1127 return 1; 1128 1129 case BT_RFCOMM_FCOFF_CMD: 1130 multiplexer->fcon = 0x80; 1131 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1132 return 1; 1133 1134 case BT_RFCOMM_TEST_CMD: { 1135 log_info("Received test command"); 1136 int len = packet[payload_offset+1] >> 1; // length < 125 1137 if (len > RFCOMM_TEST_DATA_MAX_LEN){ 1138 len = RFCOMM_TEST_DATA_MAX_LEN; 1139 } 1140 len = btstack_min(len, size - 1 - payload_offset); // avoid information leak 1141 multiplexer->test_data_len = len; 1142 memcpy(multiplexer->test_data, &packet[payload_offset + 2], len); 1143 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1144 return 1; 1145 } 1146 default: 1147 break; 1148 } 1149 break; 1150 1151 default: 1152 break; 1153 1154 } 1155 return 0; 1156 } 1157 1158 static int rfcomm_multiplexer_ready_to_send(rfcomm_multiplexer_t * multiplexer){ 1159 if (multiplexer->send_dm_for_dlci) return 1; 1160 if (multiplexer->nsc_command) return 1; 1161 if (multiplexer->fcon & 0x80) return 1; 1162 switch (multiplexer->state){ 1163 case RFCOMM_MULTIPLEXER_SEND_SABM_0: 1164 case RFCOMM_MULTIPLEXER_SEND_UA_0: 1165 case RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC: 1166 return 1; 1167 case RFCOMM_MULTIPLEXER_OPEN: 1168 if (multiplexer->test_data_len) { 1169 return 1; 1170 } 1171 break; 1172 default: 1173 break; 1174 } 1175 return 0; 1176 } 1177 1178 static void rfcomm_multiplexer_state_machine(rfcomm_multiplexer_t * multiplexer, RFCOMM_MULTIPLEXER_EVENT event){ 1179 1180 if (event != MULT_EV_READY_TO_SEND) return; 1181 1182 uint16_t l2cap_cid = multiplexer->l2cap_cid; 1183 1184 // process stored DM responses 1185 if (multiplexer->send_dm_for_dlci){ 1186 uint8_t dlci = multiplexer->send_dm_for_dlci; 1187 multiplexer->send_dm_for_dlci = 0; 1188 rfcomm_send_dm_pf(multiplexer, dlci); 1189 return; 1190 } 1191 1192 if (multiplexer->nsc_command){ 1193 uint8_t command = multiplexer->nsc_command; 1194 multiplexer->nsc_command = 0; 1195 rfcomm_send_uih_nsc_rsp(multiplexer, command); 1196 return; 1197 } 1198 1199 if (multiplexer->fcon & 0x80){ 1200 multiplexer->fcon &= 0x01; 1201 rfcomm_send_uih_fc_rsp(multiplexer, multiplexer->fcon); 1202 1203 if (multiplexer->fcon == 0) return; 1204 // trigger client to send again after sending FCon Response 1205 rfcomm_notify_channel_can_send(); 1206 return; 1207 } 1208 1209 switch (multiplexer->state) { 1210 case RFCOMM_MULTIPLEXER_SEND_SABM_0: 1211 log_info("Sending SABM #0 - (multi 0x%p)", multiplexer); 1212 multiplexer->state = RFCOMM_MULTIPLEXER_W4_UA_0; 1213 rfcomm_send_sabm(multiplexer, 0); 1214 break; 1215 case RFCOMM_MULTIPLEXER_SEND_UA_0: 1216 log_info("Sending UA #0"); 1217 multiplexer->state = RFCOMM_MULTIPLEXER_OPEN; 1218 rfcomm_send_ua(multiplexer, 0); 1219 1220 rfcomm_multiplexer_opened(multiplexer); 1221 break; 1222 case RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC: 1223 // try to detect authentication errors: drop link key if multiplexer closed before first channel got opened 1224 if (!multiplexer->at_least_one_connection){ 1225 log_info("TODO: no connections established - delete link key prophylactically"); 1226 // hci_send_cmd(&hci_delete_stored_link_key, multiplexer->remote_addr); 1227 } 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 1293 rfcomm_channel_t * channel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, frame_dlci); 1294 if (!channel) return; 1295 1296 // handle new outgoing credits 1297 if (packet[1] == BT_RFCOMM_UIH_PF) { 1298 1299 // add them 1300 uint16_t new_credits = packet[3+length_offset]; 1301 channel->credits_outgoing += new_credits; 1302 log_info( "RFCOMM data UIH_PF, new credits: %u, now %u", new_credits, channel->credits_outgoing); 1303 1304 // notify channel statemachine 1305 rfcomm_channel_event_t channel_event = { CH_EVT_RCVD_CREDITS, 0 }; 1306 rfcomm_channel_state_machine_with_channel(channel, &channel_event); 1307 if (rfcomm_channel_ready_to_send(channel)){ 1308 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1309 } 1310 } 1311 1312 // contains payload? 1313 if (size - 1 > payload_offset){ 1314 1315 // log_info( "RFCOMM data UIH_PF, size %u, channel %p", size-payload_offset-1, rfChannel->connection); 1316 1317 // decrease incoming credit counter 1318 if (channel->credits_incoming > 0){ 1319 channel->credits_incoming--; 1320 } 1321 1322 // deliver payload 1323 (channel->packet_handler)(RFCOMM_DATA_PACKET, channel->rfcomm_cid, 1324 &packet[payload_offset], size-payload_offset-1); 1325 } 1326 1327 // automatically provide new credits to remote device, if no incoming flow control 1328 if (!channel->incoming_flow_control && channel->credits_incoming < 5){ 1329 channel->new_credits_incoming = RFCOMM_CREDITS; 1330 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1331 } 1332 } 1333 1334 static void rfcomm_channel_accept_pn(rfcomm_channel_t *channel, rfcomm_channel_event_pn_t *event){ 1335 // priority of client request 1336 channel->pn_priority = event->priority; 1337 1338 // new credits 1339 channel->credits_outgoing = event->credits_outgoing; 1340 1341 // negotiate max frame size 1342 if (channel->max_frame_size > channel->multiplexer->max_frame_size) { 1343 channel->max_frame_size = channel->multiplexer->max_frame_size; 1344 } 1345 if (channel->max_frame_size > event->max_frame_size) { 1346 channel->max_frame_size = event->max_frame_size; 1347 } 1348 1349 } 1350 1351 static void rfcomm_channel_finalize(rfcomm_channel_t *channel){ 1352 1353 rfcomm_multiplexer_t *multiplexer = channel->multiplexer; 1354 1355 // remove from list 1356 btstack_linked_list_remove( &rfcomm_channels, (btstack_linked_item_t *) channel); 1357 1358 // free channel 1359 btstack_memory_rfcomm_channel_free(channel); 1360 1361 // update multiplexer timeout after channel was removed from list 1362 rfcomm_multiplexer_prepare_idle_timer(multiplexer); 1363 } 1364 1365 static void rfcomm_channel_state_machine_with_dlci(rfcomm_multiplexer_t * multiplexer, uint8_t dlci, const rfcomm_channel_event_t *event){ 1366 1367 // TODO: if client max frame size is smaller than RFCOMM_DEFAULT_SIZE, send PN 1368 1369 1370 // lookup existing channel 1371 rfcomm_channel_t * channel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, dlci); 1372 1373 // log_info("rfcomm_channel_state_machine_with_dlci lookup dlci #%u = 0x%08x - event %u", dlci, (int) channel, event->type); 1374 1375 if (channel) { 1376 rfcomm_channel_state_machine_with_channel(channel, event); 1377 if (rfcomm_channel_ready_to_send(channel)){ 1378 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1379 } 1380 return; 1381 } 1382 1383 // service registered? 1384 rfcomm_service_t * service = rfcomm_service_for_channel(dlci >> 1); 1385 // log_info("rfcomm_channel_state_machine_with_dlci service dlci #%u = 0x%08x", dlci, (int) service); 1386 if (!service) { 1387 // discard request by sending disconnected mode 1388 multiplexer->send_dm_for_dlci = dlci; 1389 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1390 return; 1391 } 1392 1393 // create channel for some events 1394 switch (event->type) { 1395 case CH_EVT_RCVD_SABM: 1396 case CH_EVT_RCVD_PN: 1397 case CH_EVT_RCVD_RPN_REQ: 1398 case CH_EVT_RCVD_RPN_CMD: 1399 // setup incoming channel 1400 channel = rfcomm_channel_create(multiplexer, service, dlci >> 1); 1401 if (!channel){ 1402 // discard request by sending disconnected mode 1403 multiplexer->send_dm_for_dlci = dlci; 1404 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1405 } 1406 break; 1407 default: 1408 break; 1409 } 1410 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 return; 1416 } 1417 1418 rfcomm_channel_state_machine_with_channel(channel, event); 1419 if (rfcomm_channel_ready_to_send(channel)){ 1420 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1421 } 1422 } 1423 1424 static void rfcomm_channel_packet_handler(rfcomm_multiplexer_t * multiplexer, uint8_t *packet, uint16_t size){ 1425 1426 UNUSED(size); // ok: fixed format messages 1427 1428 // rfcomm: (0) addr [76543 server channel] [2 direction: initiator uses 1] [1 C/R: CMD by initiator = 1] [0 EA=1] 1429 const uint8_t frame_dlci = packet[0] >> 2; 1430 uint8_t message_dlci; // used by commands in UIH(_PF) packets 1431 uint8_t message_len; // " 1432 1433 // rfcomm: (1) command/control 1434 // -- credits_offset = 1 if command == BT_RFCOMM_UIH_PF 1435 const uint8_t credit_offset = ((packet[1] & BT_RFCOMM_UIH_PF) == BT_RFCOMM_UIH_PF) ? 1 : 0; // credits for uih_pf frames 1436 // rfcomm: (2) length. if bit 0 is cleared, 2 byte length is used. (little endian) 1437 const uint8_t length_offset = (packet[2] & 1) ^ 1; // to be used for pos >= 3 1438 // rfcomm: (3+length_offset) credits if credits_offset == 1 1439 // rfcomm: (3+length_offest+credits_offset) 1440 const uint8_t payload_offset = 3 + length_offset + credit_offset; 1441 1442 rfcomm_channel_event_t event; 1443 rfcomm_channel_event_pn_t event_pn; 1444 rfcomm_channel_event_rpn_t event_rpn; 1445 rfcomm_channel_event_msc_t event_msc; 1446 1447 // switch by rfcomm message type 1448 switch(packet[1]) { 1449 1450 case BT_RFCOMM_SABM: 1451 event.type = CH_EVT_RCVD_SABM; 1452 log_info("Received SABM #%u", frame_dlci); 1453 rfcomm_channel_state_machine_with_dlci(multiplexer, frame_dlci, &event); 1454 break; 1455 1456 case BT_RFCOMM_UA: 1457 event.type = CH_EVT_RCVD_UA; 1458 log_info("Received UA #%u",frame_dlci); 1459 rfcomm_channel_state_machine_with_dlci(multiplexer, frame_dlci, &event); 1460 break; 1461 1462 case BT_RFCOMM_DISC: 1463 event.type = CH_EVT_RCVD_DISC; 1464 rfcomm_channel_state_machine_with_dlci(multiplexer, frame_dlci, &event); 1465 break; 1466 1467 case BT_RFCOMM_DM: 1468 case BT_RFCOMM_DM_PF: 1469 event.type = CH_EVT_RCVD_DM; 1470 rfcomm_channel_state_machine_with_dlci(multiplexer, frame_dlci, &event); 1471 break; 1472 1473 case BT_RFCOMM_UIH_PF: 1474 case BT_RFCOMM_UIH: 1475 1476 message_len = packet[payload_offset+1] >> 1; 1477 1478 switch (packet[payload_offset]) { 1479 case BT_RFCOMM_PN_CMD: 1480 message_dlci = packet[payload_offset+2]; 1481 event_pn.super.type = CH_EVT_RCVD_PN; 1482 event_pn.priority = packet[payload_offset+4]; 1483 event_pn.max_frame_size = little_endian_read_16(packet, payload_offset+6); 1484 event_pn.credits_outgoing = packet[payload_offset+9]; 1485 log_info("Received UIH Parameter Negotiation Command for #%u, credits %u", 1486 message_dlci, event_pn.credits_outgoing); 1487 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_pn); 1488 break; 1489 1490 case BT_RFCOMM_PN_RSP: 1491 message_dlci = packet[payload_offset+2]; 1492 event_pn.super.type = CH_EVT_RCVD_PN_RSP; 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 Response max frame %u, credits %u", 1497 event_pn.max_frame_size, 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_MSC_CMD: 1502 message_dlci = packet[payload_offset+2] >> 2; 1503 event_msc.super.type = CH_EVT_RCVD_MSC_CMD; 1504 event_msc.modem_status = packet[payload_offset+3]; 1505 log_info("Received MSC CMD for #%u, ", message_dlci); 1506 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_msc); 1507 break; 1508 1509 case BT_RFCOMM_MSC_RSP: 1510 message_dlci = packet[payload_offset+2] >> 2; 1511 event.type = CH_EVT_RCVD_MSC_RSP; 1512 log_info("Received MSC RSP for #%u", message_dlci); 1513 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, &event); 1514 break; 1515 1516 case BT_RFCOMM_RPN_CMD: 1517 message_dlci = packet[payload_offset+2] >> 2; 1518 switch (message_len){ 1519 case 1: 1520 log_info("Received Remote Port Negotiation Request for #%u", message_dlci); 1521 event.type = CH_EVT_RCVD_RPN_REQ; 1522 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, &event); 1523 break; 1524 case 8: 1525 log_info("Received Remote Port Negotiation Update for #%u", message_dlci); 1526 event_rpn.super.type = CH_EVT_RCVD_RPN_CMD; 1527 event_rpn.data = *(rfcomm_rpn_data_t*) &packet[payload_offset+3]; 1528 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_rpn); 1529 break; 1530 default: 1531 break; 1532 } 1533 break; 1534 1535 case BT_RFCOMM_RPN_RSP: 1536 log_info("Received RPN response"); 1537 break; 1538 1539 case BT_RFCOMM_RLS_CMD: { 1540 log_info("Received RLS command"); 1541 message_dlci = packet[payload_offset+2] >> 2; 1542 rfcomm_channel_event_rls_t event_rls; 1543 event_rls.super.type = CH_EVT_RCVD_RLS_CMD; 1544 event_rls.line_status = packet[payload_offset+3]; 1545 rfcomm_channel_state_machine_with_dlci(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_rls); 1546 break; 1547 } 1548 1549 case BT_RFCOMM_RLS_RSP: 1550 log_info("Received RLS response"); 1551 break; 1552 1553 // Following commands are handled by rfcomm_multiplexer_l2cap_packet_handler 1554 // case BT_RFCOMM_TEST_CMD: 1555 // case BT_RFCOMM_FCOFF_CMD: 1556 // case BT_RFCOMM_FCON_CMD: 1557 // everything else is an not supported command 1558 default: { 1559 log_error("Received unknown UIH command packet - 0x%02x", packet[payload_offset]); 1560 multiplexer->nsc_command = packet[payload_offset]; 1561 break; 1562 } 1563 } 1564 break; 1565 1566 default: 1567 log_error("Received unknown RFCOMM message type %x", packet[1]); 1568 break; 1569 } 1570 1571 // trigger next action - example W4_PN_RSP: transition to SEND_SABM which only depends on "can send" 1572 if (rfcomm_multiplexer_ready_to_send(multiplexer)){ 1573 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 1574 } 1575 } 1576 1577 static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1578 1579 if (packet_type == HCI_EVENT_PACKET){ 1580 rfcomm_hci_event_handler(packet, size); 1581 return; 1582 } 1583 1584 // we only handle l2cap packets for: 1585 if (packet_type != L2CAP_DATA_PACKET) return; 1586 1587 // - multiplexer itself 1588 int handled = rfcomm_multiplexer_l2cap_packet_handler(channel, packet, size); 1589 1590 if (handled) return; 1591 1592 // - channel over open mutliplexer 1593 rfcomm_multiplexer_t * multiplexer = rfcomm_multiplexer_for_l2cap_cid(channel); 1594 if (!multiplexer || multiplexer->state != RFCOMM_MULTIPLEXER_OPEN) return; 1595 1596 // channel data ? 1597 // rfcomm: (0) addr [76543 server channel] [2 direction: initiator uses 1] [1 C/R: CMD by initiator = 1] [0 EA=1] 1598 const uint8_t frame_dlci = packet[0] >> 2; 1599 1600 if (frame_dlci && (packet[1] == BT_RFCOMM_UIH || packet[1] == BT_RFCOMM_UIH_PF)) { 1601 rfcomm_channel_packet_handler_uih(multiplexer, packet, size); 1602 return; 1603 } 1604 1605 rfcomm_channel_packet_handler(multiplexer, packet, size); 1606 } 1607 1608 static int rfcomm_channel_ready_for_open(rfcomm_channel_t *channel){ 1609 // note: exchanging MSC isn't neccessary to consider channel open 1610 // note: having outgoing credits is also not necessary to consider channel open 1611 // 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); 1612 // if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP) == 0) return 0; 1613 // if (channel->credits_outgoing == 0) return 0; 1614 log_info("rfcomm_channel_ready_for_open state %u, flags needed %04x, current %04x, rf credits %u", 1615 channel->state, RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP, channel->state_var, channel->credits_outgoing); 1616 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP) == 0) return 0; 1617 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS) == 0) return 0; 1618 1619 return 1; 1620 } 1621 1622 static int rfcomm_channel_ready_for_incoming_dlc_setup(rfcomm_channel_t * channel){ 1623 log_info("rfcomm_channel_ready_for_incoming_dlc_setup state var %04x", channel->state_var); 1624 // Client accept and SABM/UA is required, PN RSP is needed if PN was received 1625 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED) == 0) return 0; 1626 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM ) == 0) return 0; 1627 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_UA ) != 0) return 0; 1628 if ((channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP ) != 0) return 0; 1629 return 1; 1630 } 1631 1632 inline static void rfcomm_channel_state_add(rfcomm_channel_t *channel, RFCOMM_CHANNEL_STATE_VAR event){ 1633 channel->state_var = (RFCOMM_CHANNEL_STATE_VAR) (channel->state_var | event); 1634 } 1635 inline static void rfcomm_channel_state_remove(rfcomm_channel_t *channel, RFCOMM_CHANNEL_STATE_VAR event){ 1636 channel->state_var = (RFCOMM_CHANNEL_STATE_VAR) (channel->state_var & ~event); 1637 } 1638 1639 static int rfcomm_channel_ready_to_send(rfcomm_channel_t * channel){ 1640 switch (channel->state){ 1641 case RFCOMM_CHANNEL_SEND_UIH_PN: 1642 log_debug("ch-ready: state %u", channel->state); 1643 return 1; 1644 case RFCOMM_CHANNEL_SEND_SABM_W4_UA: 1645 log_debug("ch-ready: state %u", channel->state); 1646 return 1; 1647 case RFCOMM_CHANNEL_SEND_UA_AFTER_DISC: 1648 log_debug("ch-ready: state %u", channel->state); 1649 return 1; 1650 case RFCOMM_CHANNEL_SEND_DISC: 1651 log_debug("ch-ready: state %u", channel->state); 1652 return 1; 1653 case RFCOMM_CHANNEL_SEND_DM: 1654 log_debug("ch-ready: state %u", channel->state); 1655 return 1; 1656 case RFCOMM_CHANNEL_OPEN: 1657 if (channel->new_credits_incoming) { 1658 log_debug("ch-ready: channel open & new_credits_incoming") ; 1659 return 1; 1660 } 1661 break; 1662 case RFCOMM_CHANNEL_DLC_SETUP: 1663 if (channel->state_var & ( 1664 RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD | 1665 RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS 1666 )) { 1667 log_debug("ch-ready: channel dlc setup & send msc cmd or send credits") ; 1668 return 1; 1669 } 1670 break; 1671 1672 default: 1673 break; 1674 } 1675 1676 if (channel->state_var & ( 1677 RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP | 1678 RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_INFO | 1679 RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP | 1680 RFCOMM_CHANNEL_STATE_VAR_SEND_UA | 1681 RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP 1682 )){ 1683 log_debug("ch-ready: state %x, state var %x", channel->state, channel->state_var); 1684 return 1; 1685 } 1686 1687 if (channel->rls_line_status != RFCOMM_RLS_STATUS_INVALID) { 1688 log_debug("ch-ready: rls_line_status"); 1689 return 1; 1690 } 1691 1692 return 0; 1693 } 1694 1695 1696 static void rfcomm_channel_state_machine_with_channel(rfcomm_channel_t *channel, const rfcomm_channel_event_t *event){ 1697 1698 // log_info("rfcomm_channel_state_machine_with_channel: state %u, state_var %04x, event %u", channel->state, channel->state_var ,event->type); 1699 1700 rfcomm_multiplexer_t *multiplexer = channel->multiplexer; 1701 1702 // TODO: integrate in common switch 1703 if (event->type == CH_EVT_RCVD_DISC){ 1704 rfcomm_emit_channel_closed(channel); 1705 channel->state = RFCOMM_CHANNEL_SEND_UA_AFTER_DISC; 1706 return; 1707 } 1708 1709 // TODO: integrate in common switch 1710 if (event->type == CH_EVT_RCVD_DM){ 1711 log_info("Received DM message for #%u", channel->dlci); 1712 log_info("-> Closing channel locally for #%u", channel->dlci); 1713 rfcomm_emit_channel_closed(channel); 1714 rfcomm_channel_finalize(channel); 1715 return; 1716 } 1717 1718 // remote port negotiation command - just accept everything for now 1719 // 1720 // "The RPN command can be used before a new DLC is opened and should be used whenever the port settings change." 1721 // "The RPN command is specified as optional in TS 07.10, but it is mandatory to recognize and respond to it in RFCOMM. 1722 // (Although the handling of individual settings are implementation-dependent.)" 1723 // 1724 1725 // TODO: integrate in common switch 1726 if (event->type == CH_EVT_RCVD_RPN_CMD){ 1727 // control port parameters 1728 rfcomm_channel_event_rpn_t *event_rpn = (rfcomm_channel_event_rpn_t*) event; 1729 rfcomm_rpn_data_update(&channel->rpn_data, &event_rpn->data); 1730 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP); 1731 // notify client about new settings 1732 rfcomm_emit_port_configuration(channel); 1733 return; 1734 } 1735 1736 // TODO: integrate in common switch 1737 if (event->type == CH_EVT_RCVD_RPN_REQ){ 1738 // no values got accepted (no values have beens sent) 1739 channel->rpn_data.parameter_mask_0 = 0x00; 1740 channel->rpn_data.parameter_mask_1 = 0x00; 1741 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP); 1742 return; 1743 } 1744 1745 if (event->type == CH_EVT_RCVD_RLS_CMD){ 1746 rfcomm_channel_event_rls_t * event_rls = (rfcomm_channel_event_rls_t*) event; 1747 channel->rls_line_status = event_rls->line_status & 0x0f; 1748 log_info("CH_EVT_RCVD_RLS_CMD setting line status to 0x%0x", channel->rls_line_status); 1749 rfcomm_emit_remote_line_status(channel, event_rls->line_status); 1750 return; 1751 } 1752 1753 // TODO: integrate in common switch 1754 if (event->type == CH_EVT_READY_TO_SEND){ 1755 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP){ 1756 log_info("Sending Remote Port Negotiation RSP for #%u", channel->dlci); 1757 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_RSP); 1758 rfcomm_send_uih_rpn_rsp(multiplexer, channel->dlci, &channel->rpn_data); 1759 return; 1760 } 1761 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP){ 1762 log_info("Sending MSC RSP for #%u", channel->dlci); 1763 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP); 1764 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_RSP); 1765 rfcomm_send_uih_msc_rsp(multiplexer, channel->dlci, 0x8d); // ea=1,fc=0,rtc=1,rtr=1,ic=0,dv=1 1766 return; 1767 } 1768 if (channel->rls_line_status != RFCOMM_RLS_STATUS_INVALID){ 1769 log_info("Sending RLS RSP 0x%0x", channel->rls_line_status); 1770 uint8_t line_status = channel->rls_line_status; 1771 channel->rls_line_status = RFCOMM_RLS_STATUS_INVALID; 1772 rfcomm_send_uih_rls_rsp(multiplexer, channel->dlci, line_status); 1773 return; 1774 } 1775 } 1776 1777 // emit MSC status to app 1778 if (event->type == CH_EVT_RCVD_MSC_CMD){ 1779 // notify client about new settings 1780 rfcomm_channel_event_msc_t *event_msc = (rfcomm_channel_event_msc_t*) event; 1781 uint8_t modem_status_event[2+1]; 1782 modem_status_event[0] = RFCOMM_EVENT_REMOTE_MODEM_STATUS; 1783 modem_status_event[1] = 1; 1784 modem_status_event[2] = event_msc->modem_status; 1785 (channel->packet_handler)(HCI_EVENT_PACKET, channel->rfcomm_cid, (uint8_t*)&modem_status_event, sizeof(modem_status_event)); 1786 // no return, MSC_CMD will be handled by state machine below 1787 } 1788 1789 rfcomm_channel_event_pn_t * event_pn = (rfcomm_channel_event_pn_t*) event; 1790 1791 switch (channel->state) { 1792 case RFCOMM_CHANNEL_CLOSED: 1793 switch (event->type){ 1794 case CH_EVT_RCVD_SABM: 1795 log_info("-> Inform app"); 1796 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM); 1797 channel->state = RFCOMM_CHANNEL_INCOMING_SETUP; 1798 rfcomm_emit_connection_request(channel); 1799 break; 1800 case CH_EVT_RCVD_PN: 1801 rfcomm_channel_accept_pn(channel, event_pn); 1802 log_info("-> Inform app"); 1803 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_PN); 1804 channel->state = RFCOMM_CHANNEL_INCOMING_SETUP; 1805 rfcomm_emit_connection_request(channel); 1806 break; 1807 default: 1808 break; 1809 } 1810 break; 1811 1812 case RFCOMM_CHANNEL_INCOMING_SETUP: 1813 switch (event->type){ 1814 case CH_EVT_RCVD_SABM: 1815 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM); 1816 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED) { 1817 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_UA); 1818 } 1819 break; 1820 case CH_EVT_RCVD_PN: 1821 rfcomm_channel_accept_pn(channel, event_pn); 1822 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_PN); 1823 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED) { 1824 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP); 1825 } 1826 break; 1827 case CH_EVT_READY_TO_SEND: 1828 // if / else if is used to check for state transition after sending 1829 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP){ 1830 log_info("Sending UIH Parameter Negotiation Respond for #%u", channel->dlci); 1831 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP); 1832 rfcomm_send_uih_pn_response(multiplexer, channel->dlci, channel->pn_priority, channel->max_frame_size); 1833 } else if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_UA){ 1834 log_info("Sending UA #%u", channel->dlci); 1835 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_UA); 1836 rfcomm_send_ua(multiplexer, channel->dlci); 1837 } 1838 if (rfcomm_channel_ready_for_incoming_dlc_setup(channel)){ 1839 log_info("Incomping setup done, requesting send MSC CMD and send Credits"); 1840 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD); 1841 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS); 1842 channel->state = RFCOMM_CHANNEL_DLC_SETUP; 1843 } 1844 break; 1845 default: 1846 break; 1847 } 1848 break; 1849 1850 case RFCOMM_CHANNEL_W4_MULTIPLEXER: 1851 switch (event->type) { 1852 case CH_EVT_MULTIPLEXER_READY: 1853 log_info("Muliplexer opened, sending UIH PN next"); 1854 channel->state = RFCOMM_CHANNEL_SEND_UIH_PN; 1855 break; 1856 default: 1857 break; 1858 } 1859 break; 1860 1861 case RFCOMM_CHANNEL_SEND_UIH_PN: 1862 switch (event->type) { 1863 case CH_EVT_READY_TO_SEND: 1864 log_info("Sending UIH Parameter Negotiation Command for #%u (channel 0x%p)", channel->dlci, channel ); 1865 channel->state = RFCOMM_CHANNEL_W4_PN_RSP; 1866 rfcomm_send_uih_pn_command(multiplexer, channel->dlci, channel->max_frame_size); 1867 break; 1868 default: 1869 break; 1870 } 1871 break; 1872 1873 case RFCOMM_CHANNEL_W4_PN_RSP: 1874 switch (event->type){ 1875 case CH_EVT_RCVD_PN_RSP: 1876 // update max frame size 1877 if (channel->max_frame_size > event_pn->max_frame_size) { 1878 channel->max_frame_size = event_pn->max_frame_size; 1879 } 1880 // new credits 1881 channel->credits_outgoing = event_pn->credits_outgoing; 1882 channel->state = RFCOMM_CHANNEL_SEND_SABM_W4_UA; 1883 break; 1884 default: 1885 break; 1886 } 1887 break; 1888 1889 case RFCOMM_CHANNEL_SEND_SABM_W4_UA: 1890 switch (event->type) { 1891 case CH_EVT_READY_TO_SEND: 1892 log_info("Sending SABM #%u", channel->dlci); 1893 channel->state = RFCOMM_CHANNEL_W4_UA; 1894 rfcomm_send_sabm(multiplexer, channel->dlci); 1895 break; 1896 default: 1897 break; 1898 } 1899 break; 1900 1901 case RFCOMM_CHANNEL_W4_UA: 1902 switch (event->type){ 1903 case CH_EVT_RCVD_UA: 1904 channel->state = RFCOMM_CHANNEL_DLC_SETUP; 1905 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD); 1906 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS); 1907 break; 1908 default: 1909 break; 1910 } 1911 break; 1912 1913 case RFCOMM_CHANNEL_DLC_SETUP: 1914 switch (event->type){ 1915 case CH_EVT_RCVD_MSC_CMD: 1916 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_CMD); 1917 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP); 1918 break; 1919 case CH_EVT_RCVD_MSC_RSP: 1920 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_RCVD_MSC_RSP); 1921 break; 1922 1923 case CH_EVT_READY_TO_SEND: 1924 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD){ 1925 log_info("Sending MSC CMD for #%u", channel->dlci); 1926 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_CMD); 1927 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SENT_MSC_CMD); 1928 rfcomm_send_uih_msc_cmd(multiplexer, channel->dlci , 0x8d); // ea=1,fc=0,rtc=1,rtr=1,ic=0,dv=1 1929 break; 1930 } 1931 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS){ 1932 log_info("Providing credits for #%u", channel->dlci); 1933 rfcomm_channel_state_remove(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_CREDITS); 1934 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SENT_CREDITS); 1935 1936 if (channel->new_credits_incoming) { 1937 uint8_t new_credits = channel->new_credits_incoming; 1938 channel->new_credits_incoming = 0; 1939 rfcomm_channel_send_credits(channel, new_credits); 1940 } 1941 break; 1942 1943 } 1944 break; 1945 default: 1946 break; 1947 } 1948 // finally done? 1949 if (rfcomm_channel_ready_for_open(channel)){ 1950 channel->state = RFCOMM_CHANNEL_OPEN; 1951 rfcomm_channel_opened(channel); 1952 } 1953 break; 1954 1955 case RFCOMM_CHANNEL_OPEN: 1956 switch (event->type){ 1957 case CH_EVT_RCVD_MSC_CMD: 1958 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_MSC_RSP); 1959 break; 1960 case CH_EVT_READY_TO_SEND: 1961 if (channel->new_credits_incoming) { 1962 uint8_t new_credits = channel->new_credits_incoming; 1963 channel->new_credits_incoming = 0; 1964 rfcomm_channel_send_credits(channel, new_credits); 1965 break; 1966 } 1967 break; 1968 case CH_EVT_RCVD_CREDITS: 1969 rfcomm_notify_channel_can_send(); 1970 break; 1971 default: 1972 break; 1973 } 1974 break; 1975 1976 case RFCOMM_CHANNEL_SEND_DM: 1977 switch (event->type) { 1978 case CH_EVT_READY_TO_SEND: 1979 log_info("Sending DM_PF for #%u", channel->dlci); 1980 // don't emit channel closed - channel was never open 1981 channel->state = RFCOMM_CHANNEL_CLOSED; 1982 rfcomm_send_dm_pf(multiplexer, channel->dlci); 1983 rfcomm_channel_finalize(channel); 1984 break; 1985 default: 1986 break; 1987 } 1988 break; 1989 1990 case RFCOMM_CHANNEL_SEND_DISC: 1991 switch (event->type) { 1992 case CH_EVT_READY_TO_SEND: 1993 channel->state = RFCOMM_CHANNEL_W4_UA_AFTER_DISC; 1994 rfcomm_send_disc(multiplexer, channel->dlci); 1995 break; 1996 default: 1997 break; 1998 } 1999 break; 2000 2001 case RFCOMM_CHANNEL_W4_UA_AFTER_DISC: 2002 switch (event->type){ 2003 case CH_EVT_RCVD_UA: 2004 channel->state = RFCOMM_CHANNEL_CLOSED; 2005 rfcomm_emit_channel_closed(channel); 2006 rfcomm_channel_finalize(channel); 2007 break; 2008 default: 2009 break; 2010 } 2011 break; 2012 2013 case RFCOMM_CHANNEL_SEND_UA_AFTER_DISC: 2014 switch (event->type) { 2015 case CH_EVT_READY_TO_SEND: 2016 log_info("Sending UA after DISC for #%u", channel->dlci); 2017 channel->state = RFCOMM_CHANNEL_CLOSED; 2018 rfcomm_send_ua(multiplexer, channel->dlci); 2019 rfcomm_channel_finalize(channel); 2020 break; 2021 default: 2022 break; 2023 } 2024 break; 2025 2026 default: 2027 break; 2028 } 2029 } 2030 2031 // MARK: RFCOMM BTstack API 2032 2033 void rfcomm_init(void){ 2034 rfcomm_client_cid_generator = 0; 2035 rfcomm_multiplexers = NULL; 2036 rfcomm_services = NULL; 2037 rfcomm_channels = NULL; 2038 rfcomm_security_level = LEVEL_2; 2039 } 2040 2041 void rfcomm_set_required_security_level(gap_security_level_t security_level){ 2042 rfcomm_security_level = security_level; 2043 } 2044 2045 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){ 2046 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2047 if (!channel){ 2048 log_error("rfcomm_send cid 0x%02x doesn't exist!", rfcomm_cid); 2049 return 0; 2050 } 2051 return rfcomm_channel_can_send(channel); 2052 } 2053 2054 void rfcomm_request_can_send_now_event(uint16_t rfcomm_cid){ 2055 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2056 if (!channel){ 2057 log_error("rfcomm_send cid 0x%02x doesn't exist!", rfcomm_cid); 2058 return; 2059 } 2060 channel->waiting_for_can_send_now = 1; 2061 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2062 } 2063 2064 static int rfcomm_assert_send_valid(rfcomm_channel_t * channel , uint16_t len){ 2065 if (len > channel->max_frame_size){ 2066 log_error("rfcomm_send cid 0x%02x, rfcomm data lenght exceeds MTU!", channel->rfcomm_cid); 2067 return RFCOMM_DATA_LEN_EXCEEDS_MTU; 2068 } 2069 2070 if (!channel->credits_outgoing){ 2071 log_info("rfcomm_send cid 0x%02x, no rfcomm outgoing credits!", channel->rfcomm_cid); 2072 return RFCOMM_NO_OUTGOING_CREDITS; 2073 } 2074 2075 if ((channel->multiplexer->fcon & 1) == 0){ 2076 log_info("rfcomm_send cid 0x%02x, aggregate flow off!", channel->rfcomm_cid); 2077 return RFCOMM_AGGREGATE_FLOW_OFF; 2078 } 2079 return 0; 2080 } 2081 2082 // pre: rfcomm_can_send_packet_now(rfcomm_cid) == true 2083 int rfcomm_reserve_packet_buffer(void){ 2084 return l2cap_reserve_packet_buffer(); 2085 } 2086 2087 void rfcomm_release_packet_buffer(void){ 2088 l2cap_release_packet_buffer(); 2089 } 2090 2091 uint8_t * rfcomm_get_outgoing_buffer(void){ 2092 uint8_t * rfcomm_out_buffer = l2cap_get_outgoing_buffer(); 2093 // address + control + length (16) + no credit field 2094 return &rfcomm_out_buffer[4]; 2095 } 2096 2097 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid){ 2098 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2099 if (!channel){ 2100 log_error("rfcomm_get_max_frame_size cid 0x%02x doesn't exist!", rfcomm_cid); 2101 return 0; 2102 } 2103 return channel->max_frame_size; 2104 } 2105 2106 int rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len){ 2107 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2108 if (!channel){ 2109 log_error("rfcomm_send_prepared cid 0x%02x doesn't exist!", rfcomm_cid); 2110 return 0; 2111 } 2112 2113 int err = rfcomm_assert_send_valid(channel, len); 2114 if (err) return err; 2115 if (!l2cap_can_send_prepared_packet_now(channel->multiplexer->l2cap_cid)){ 2116 log_error("rfcomm_send_prepared: l2cap cannot send now"); 2117 return BTSTACK_ACL_BUFFERS_FULL; 2118 } 2119 2120 // send might cause l2cap to emit new credits, update counters first 2121 if (len){ 2122 channel->credits_outgoing--; 2123 } else { 2124 log_info("sending empty RFCOMM packet for cid %02x", rfcomm_cid); 2125 } 2126 2127 int result = rfcomm_send_uih_prepared(channel->multiplexer, channel->dlci, len); 2128 2129 if (result != 0) { 2130 if (len) { 2131 channel->credits_outgoing++; 2132 } 2133 log_error("rfcomm_send_prepared: error %d", result); 2134 return result; 2135 } 2136 2137 return result; 2138 } 2139 2140 int rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){ 2141 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2142 if (!channel){ 2143 log_error("cid 0x%02x doesn't exist!", rfcomm_cid); 2144 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2145 } 2146 2147 int err = rfcomm_assert_send_valid(channel, len); 2148 if (err) return err; 2149 if (!l2cap_can_send_packet_now(channel->multiplexer->l2cap_cid)){ 2150 log_error("rfcomm_send_internal: l2cap cannot send now"); 2151 return BTSTACK_ACL_BUFFERS_FULL; 2152 } 2153 2154 rfcomm_reserve_packet_buffer(); 2155 uint8_t * rfcomm_payload = rfcomm_get_outgoing_buffer(); 2156 memcpy(rfcomm_payload, data, len); 2157 err = rfcomm_send_prepared(rfcomm_cid, len); 2158 if (err){ 2159 rfcomm_release_packet_buffer(); 2160 } 2161 return err; 2162 } 2163 2164 // Sends Local Lnie Status, see LINE_STATUS_.. 2165 int rfcomm_send_local_line_status(uint16_t rfcomm_cid, uint8_t line_status){ 2166 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2167 if (!channel){ 2168 log_error("rfcomm_send_local_line_status cid 0x%02x doesn't exist!", rfcomm_cid); 2169 return 0; 2170 } 2171 return rfcomm_send_uih_rls_cmd(channel->multiplexer, channel->dlci, line_status); 2172 } 2173 2174 // Sned local modem status. see MODEM_STAUS_.. 2175 int rfcomm_send_modem_status(uint16_t rfcomm_cid, uint8_t modem_status){ 2176 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2177 if (!channel){ 2178 log_error("rfcomm_send_modem_status cid 0x%02x doesn't exist!", rfcomm_cid); 2179 return 0; 2180 } 2181 return rfcomm_send_uih_msc_cmd(channel->multiplexer, channel->dlci, modem_status); 2182 } 2183 2184 // Configure remote port 2185 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){ 2186 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2187 if (!channel){ 2188 log_error("rfcomm_send_port_configuration cid 0x%02x doesn't exist!", rfcomm_cid); 2189 return 0; 2190 } 2191 rfcomm_rpn_data_t rpn_data; 2192 rpn_data.baud_rate = baud_rate; 2193 rpn_data.flags = data_bits | (stop_bits << 2) | (parity << 3); 2194 rpn_data.flow_control = flow_control; 2195 rpn_data.xon = 0; 2196 rpn_data.xoff = 0; 2197 rpn_data.parameter_mask_0 = 0x1f; // all but xon/xoff 2198 rpn_data.parameter_mask_1 = 0x3f; // all flow control options 2199 return rfcomm_send_uih_rpn_cmd(channel->multiplexer, channel->dlci, &rpn_data); 2200 } 2201 2202 // Query remote port 2203 int rfcomm_query_port_configuration(uint16_t rfcomm_cid){ 2204 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2205 if (!channel){ 2206 log_error("rfcomm_query_port_configuration cid 0x%02x doesn't exist!", rfcomm_cid); 2207 return 0; 2208 } 2209 return rfcomm_send_uih_rpn_req(channel->multiplexer, channel->dlci); 2210 } 2211 2212 2213 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){ 2214 log_info("RFCOMM_CREATE_CHANNEL addr %s channel #%u init credits %u", bd_addr_to_str(addr), server_channel, initial_credits); 2215 2216 // create new multiplexer if necessary 2217 uint8_t status = 0; 2218 uint8_t dlci = 0; 2219 int new_multiplexer = 0; 2220 rfcomm_channel_t * channel = NULL; 2221 rfcomm_multiplexer_t * multiplexer = rfcomm_multiplexer_for_addr(addr); 2222 if (!multiplexer) { 2223 multiplexer = rfcomm_multiplexer_create_for_addr(addr); 2224 if (!multiplexer){ 2225 status = BTSTACK_MEMORY_ALLOC_FAILED; 2226 goto fail; 2227 } 2228 multiplexer->outgoing = 1; 2229 multiplexer->state = RFCOMM_MULTIPLEXER_W4_CONNECT; 2230 new_multiplexer = 1; 2231 } 2232 2233 // check if channel for this remote service already exists 2234 dlci = (server_channel << 1) | (multiplexer->outgoing ^ 1); 2235 channel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, dlci); 2236 if (channel){ 2237 status = RFCOMM_CHANNEL_ALREADY_REGISTERED; 2238 goto fail; 2239 } 2240 2241 // prepare channel 2242 channel = rfcomm_channel_create(multiplexer, NULL, server_channel); 2243 if (!channel){ 2244 status = BTSTACK_MEMORY_ALLOC_FAILED; 2245 goto fail; 2246 } 2247 2248 // rfcomm_cid is already assigned by rfcomm_channel_create 2249 channel->incoming_flow_control = incoming_flow_control; 2250 channel->new_credits_incoming = initial_credits; 2251 channel->packet_handler = packet_handler; 2252 2253 // return rfcomm_cid 2254 if (out_rfcomm_cid){ 2255 *out_rfcomm_cid = channel->rfcomm_cid; 2256 } 2257 2258 // start multiplexer setup 2259 if (multiplexer->state != RFCOMM_MULTIPLEXER_OPEN) { 2260 channel->state = RFCOMM_CHANNEL_W4_MULTIPLEXER; 2261 uint16_t l2cap_cid = 0; 2262 status = l2cap_create_channel(rfcomm_packet_handler, addr, BLUETOOTH_PROTOCOL_RFCOMM, l2cap_max_mtu(), &l2cap_cid); 2263 if (status) goto fail; 2264 multiplexer->l2cap_cid = l2cap_cid; 2265 return 0; 2266 } 2267 2268 channel->state = RFCOMM_CHANNEL_SEND_UIH_PN; 2269 2270 // start connecting, if multiplexer is already up and running 2271 l2cap_request_can_send_now_event(multiplexer->l2cap_cid); 2272 return 0; 2273 2274 fail: 2275 if (new_multiplexer) btstack_memory_rfcomm_multiplexer_free(multiplexer); 2276 if (channel) btstack_memory_rfcomm_channel_free(channel); 2277 return status; 2278 } 2279 2280 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){ 2281 return rfcomm_channel_create_internal(packet_handler, addr, server_channel, 1, initial_credits, out_rfcomm_cid); 2282 } 2283 2284 uint8_t rfcomm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint16_t * out_rfcomm_cid){ 2285 return rfcomm_channel_create_internal(packet_handler, addr, server_channel, 0, RFCOMM_CREDITS, out_rfcomm_cid); 2286 } 2287 2288 void rfcomm_disconnect(uint16_t rfcomm_cid){ 2289 log_info("RFCOMM_DISCONNECT cid 0x%02x", rfcomm_cid); 2290 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2291 if (!channel) return; 2292 2293 channel->state = RFCOMM_CHANNEL_SEND_DISC; 2294 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2295 } 2296 2297 static uint8_t rfcomm_register_service_internal(btstack_packet_handler_t packet_handler, 2298 uint8_t channel, uint16_t max_frame_size, uint8_t incoming_flow_control, uint8_t initial_credits){ 2299 2300 log_info("RFCOMM_REGISTER_SERVICE channel #%u mtu %u flow_control %u credits %u", 2301 channel, max_frame_size, incoming_flow_control, initial_credits); 2302 2303 // check if already registered 2304 rfcomm_service_t * service = rfcomm_service_for_channel(channel); 2305 if (service){ 2306 return RFCOMM_CHANNEL_ALREADY_REGISTERED; 2307 } 2308 2309 // alloc structure 2310 service = btstack_memory_rfcomm_service_get(); 2311 if (!service) { 2312 return BTSTACK_MEMORY_ALLOC_FAILED; 2313 } 2314 2315 // register with l2cap if not registered before, max MTU 2316 if (btstack_linked_list_empty(&rfcomm_services)){ 2317 l2cap_register_service(rfcomm_packet_handler, BLUETOOTH_PROTOCOL_RFCOMM, 0xffff, rfcomm_security_level); 2318 } 2319 2320 // fill in 2321 service->packet_handler = packet_handler; 2322 service->server_channel = channel; 2323 service->max_frame_size = max_frame_size; 2324 service->incoming_flow_control = incoming_flow_control; 2325 service->incoming_initial_credits = initial_credits; 2326 2327 // add to services list 2328 btstack_linked_list_add(&rfcomm_services, (btstack_linked_item_t *) service); 2329 2330 return 0; 2331 } 2332 2333 uint8_t rfcomm_register_service_with_initial_credits(btstack_packet_handler_t packet_handler, 2334 uint8_t channel, uint16_t max_frame_size, uint8_t initial_credits){ 2335 2336 return rfcomm_register_service_internal(packet_handler, channel, max_frame_size, 1, initial_credits); 2337 } 2338 2339 uint8_t rfcomm_register_service(btstack_packet_handler_t packet_handler, uint8_t channel, 2340 uint16_t max_frame_size){ 2341 2342 return rfcomm_register_service_internal(packet_handler, channel, max_frame_size, 0,RFCOMM_CREDITS); 2343 } 2344 2345 void rfcomm_unregister_service(uint8_t service_channel){ 2346 log_info("RFCOMM_UNREGISTER_SERVICE #%u", service_channel); 2347 rfcomm_service_t *service = rfcomm_service_for_channel(service_channel); 2348 if (!service) return; 2349 btstack_linked_list_remove(&rfcomm_services, (btstack_linked_item_t *) service); 2350 btstack_memory_rfcomm_service_free(service); 2351 2352 // unregister if no services active 2353 if (btstack_linked_list_empty(&rfcomm_services)){ 2354 // bt_send_cmd(&l2cap_unregister_service, BLUETOOTH_PROTOCOL_RFCOMM); 2355 l2cap_unregister_service(BLUETOOTH_PROTOCOL_RFCOMM); 2356 } 2357 } 2358 2359 void rfcomm_accept_connection(uint16_t rfcomm_cid){ 2360 log_info("RFCOMM_ACCEPT_CONNECTION cid 0x%02x", rfcomm_cid); 2361 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2362 if (!channel) return; 2363 switch (channel->state) { 2364 case RFCOMM_CHANNEL_INCOMING_SETUP: 2365 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_CLIENT_ACCEPTED); 2366 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_RCVD_PN){ 2367 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_PN_RSP); 2368 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2369 } 2370 if (channel->state_var & RFCOMM_CHANNEL_STATE_VAR_RCVD_SABM){ 2371 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_UA); 2372 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2373 } 2374 // at least one of { PN RSP, UA } needs to be sent 2375 // state transistion incoming setup -> dlc setup happens in rfcomm_run after these have been sent 2376 break; 2377 default: 2378 break; 2379 } 2380 2381 } 2382 2383 void rfcomm_decline_connection(uint16_t rfcomm_cid){ 2384 log_info("RFCOMM_DECLINE_CONNECTION cid 0x%02x", rfcomm_cid); 2385 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2386 if (!channel) return; 2387 switch (channel->state) { 2388 case RFCOMM_CHANNEL_INCOMING_SETUP: 2389 channel->state = RFCOMM_CHANNEL_SEND_DM; 2390 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2391 break; 2392 default: 2393 break; 2394 } 2395 } 2396 2397 void rfcomm_grant_credits(uint16_t rfcomm_cid, uint8_t credits){ 2398 log_info("RFCOMM_GRANT_CREDITS cid 0x%02x credits %u", rfcomm_cid, credits); 2399 rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid); 2400 if (!channel) return; 2401 if (!channel->incoming_flow_control) return; 2402 channel->new_credits_incoming += credits; 2403 2404 // process 2405 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid); 2406 } 2407 2408 2409