rfcomm.c (b7a6ae66575782378456cc3d53125339922601c7) rfcomm.c (99b223ff1aad61200a10313f310ddb1d7f3af75c)
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

--- 2403 unchanged lines hidden (view full) ---

2412 // trigger send
2413 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_QUERY | RFCOMM_CHANNEL_STATE_VAR_EMIT_RPN_RESPONSE);
2414 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid);
2415 return ERROR_CODE_SUCCESS;
2416}
2417
2418
2419static 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){
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

--- 2403 unchanged lines hidden (view full) ---

2412 // trigger send
2413 rfcomm_channel_state_add(channel, RFCOMM_CHANNEL_STATE_VAR_SEND_RPN_QUERY | RFCOMM_CHANNEL_STATE_VAR_EMIT_RPN_RESPONSE);
2414 l2cap_request_can_send_now_event(channel->multiplexer->l2cap_cid);
2415 return ERROR_CODE_SUCCESS;
2416}
2417
2418
2419static 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){
2420 log_info("create addr %s channel #%u init credits %u", bd_addr_to_str(addr), server_channel, initial_credits);
2420 log_info("create for addr %s channel #%u init credits %u", bd_addr_to_str(addr), server_channel, initial_credits);
2421
2421
2422 // create new multiplexer if necessary
2422 // create new multiplexer if necessary (initial state is closed)
2423 uint8_t status = 0;
2423 uint8_t status = 0;
2424 uint8_t dlci = 0;
2425 int new_multiplexer = 0;
2426 rfcomm_channel_t * channel = NULL;
2427 rfcomm_multiplexer_t * multiplexer = rfcomm_multiplexer_for_addr(addr);
2424 rfcomm_channel_t * channel = NULL;
2425 rfcomm_multiplexer_t * multiplexer = rfcomm_multiplexer_for_addr(addr);
2428 if (!multiplexer) {
2426 if (multiplexer == NULL) {
2429 multiplexer = rfcomm_multiplexer_create_for_addr(addr);
2427 multiplexer = rfcomm_multiplexer_create_for_addr(addr);
2430 if (!multiplexer) return BTSTACK_MEMORY_ALLOC_FAILED;
2431
2428 if (multiplexer == NULL) {
2429 return BTSTACK_MEMORY_ALLOC_FAILED;
2430 }
2432 multiplexer->outgoing = 1;
2431 multiplexer->outgoing = 1;
2433 multiplexer->state = RFCOMM_MULTIPLEXER_W4_CONNECT;
2434 new_multiplexer = 1;
2435 }
2436
2432 }
2433
2437 // check if channel for this remote service already exists
2438 dlci = (server_channel << 1) | (multiplexer->outgoing ^ 1);
2434 // abort if channel for this remote service already exists
2435 uint8_t dlci = (server_channel << 1) | (multiplexer->outgoing ^ 1);
2439 channel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, dlci);
2436 channel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, dlci);
2440 if (channel){
2441 if (new_multiplexer) btstack_memory_rfcomm_multiplexer_free(multiplexer);
2437 if (channel != NULL){
2442 return RFCOMM_CHANNEL_ALREADY_REGISTERED;
2443 }
2444
2445 // prepare channel
2446 channel = rfcomm_channel_create(multiplexer, NULL, server_channel);
2438 return RFCOMM_CHANNEL_ALREADY_REGISTERED;
2439 }
2440
2441 // prepare channel
2442 channel = rfcomm_channel_create(multiplexer, NULL, server_channel);
2447 if (!channel){
2448 if (new_multiplexer) btstack_memory_rfcomm_multiplexer_free(multiplexer);
2443 if (channel == NULL){
2444 if (multiplexer->state == RFCOMM_MULTIPLEXER_CLOSED) {
2445 // free newly created multiplexer
2446 btstack_memory_rfcomm_multiplexer_free(multiplexer);
2447 }
2449 return BTSTACK_MEMORY_ALLOC_FAILED;
2450 }
2451
2448 return BTSTACK_MEMORY_ALLOC_FAILED;
2449 }
2450
2452 // rfcomm_cid is already assigned by rfcomm_channel_create
2451 // rfcomm_cid was assigned by rfcomm_channel_create
2453 channel->incoming_flow_control = incoming_flow_control;
2454 channel->new_credits_incoming = initial_credits;
2455 channel->packet_handler = packet_handler;
2456
2457 // return rfcomm_cid
2458 if (out_rfcomm_cid){
2459 *out_rfcomm_cid = channel->rfcomm_cid;
2460 }
2461
2452 channel->incoming_flow_control = incoming_flow_control;
2453 channel->new_credits_incoming = initial_credits;
2454 channel->packet_handler = packet_handler;
2455
2456 // return rfcomm_cid
2457 if (out_rfcomm_cid){
2458 *out_rfcomm_cid = channel->rfcomm_cid;
2459 }
2460
2462 // start multiplexer setup
2463 if (multiplexer->state != RFCOMM_MULTIPLEXER_OPEN) {
2464 channel->state = RFCOMM_CHANNEL_W4_MULTIPLEXER;
2465 uint16_t l2cap_cid = 0;
2461 // check multiplexer
2462 log_info("multiplexer state %u", multiplexer->state);
2463 switch (multiplexer->state){
2464 case RFCOMM_MULTIPLEXER_CLOSED:
2465 // multiplexer just created -> start new multiplexer
2466 channel->state = RFCOMM_CHANNEL_W4_MULTIPLEXER;
2467 multiplexer->state = RFCOMM_MULTIPLEXER_W4_CONNECT;
2466#ifdef RFCOMM_USE_ERTM
2468#ifdef RFCOMM_USE_ERTM
2467 // request
2468 rfcomm_ertm_request_t request;
2469 memset(&request, 0, sizeof(rfcomm_ertm_request_t));
2470 (void)memcpy(request.addr, addr, 6);
2471 request.ertm_id = rfcomm_next_ertm_id();
2472 if (rfcomm_ertm_request_callback){
2473 (*rfcomm_ertm_request_callback)(&request);
2474 }
2475 if (request.ertm_config && request.ertm_buffer && request.ertm_buffer_size){
2476 multiplexer->ertm_id = request.ertm_id;
2477 status = l2cap_ertm_create_channel(rfcomm_packet_handler, addr, BLUETOOTH_PROTOCOL_RFCOMM,
2478 request.ertm_config, request.ertm_buffer, request.ertm_buffer_size, &l2cap_cid);
2479 }
2480 else
2469 // request
2470 rfcomm_ertm_request_t request;
2471 memset(&request, 0, sizeof(rfcomm_ertm_request_t));
2472 (void)memcpy(request.addr, addr, 6);
2473 request.ertm_id = rfcomm_next_ertm_id();
2474 if (rfcomm_ertm_request_callback){
2475 (*rfcomm_ertm_request_callback)(&request);
2476 }
2477 if (request.ertm_config && request.ertm_buffer && request.ertm_buffer_size){
2478 multiplexer->ertm_id = request.ertm_id;
2479 status = l2cap_ertm_create_channel(rfcomm_packet_handler, addr, BLUETOOTH_PROTOCOL_RFCOMM,
2480 request.ertm_config, request.ertm_buffer, request.ertm_buffer_size, &multiplexer->l2cap_cid);
2481 }
2482 else
2481#endif
2483#endif
2482 {
2483 status = l2cap_create_channel(rfcomm_packet_handler, addr, BLUETOOTH_PROTOCOL_RFCOMM, l2cap_max_mtu(), &l2cap_cid);
2484 }
2485 if (status) {
2486 if (new_multiplexer) btstack_memory_rfcomm_multiplexer_free(multiplexer);
2487 btstack_memory_rfcomm_channel_free(channel);
2488 return status;
2489 }
2490 multiplexer->l2cap_cid = l2cap_cid;
2491 return ERROR_CODE_SUCCESS;
2484 {
2485 status = l2cap_create_channel(rfcomm_packet_handler, addr, BLUETOOTH_PROTOCOL_RFCOMM, l2cap_max_mtu(), &multiplexer->l2cap_cid);
2486 }
2487 if (status) {
2488 btstack_memory_rfcomm_multiplexer_free(multiplexer);
2489 btstack_memory_rfcomm_channel_free(channel);
2490 return status;
2491 }
2492 break;
2493 case RFCOMM_MULTIPLEXER_W4_CONNECT:
2494 case RFCOMM_MULTIPLEXER_SEND_SABM_0:
2495 case RFCOMM_MULTIPLEXER_W4_UA_0:
2496 case RFCOMM_MULTIPLEXER_W4_SABM_0:
2497 case RFCOMM_MULTIPLEXER_SEND_UA_0:
2498 // multiplexer setup has been triggered before
2499 channel->state = RFCOMM_CHANNEL_W4_MULTIPLEXER;
2500 break;
2501 case RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC:
2502 // was about to shut multiplexer down, but it's required again. set back to open and start connecting
2503 multiplexer->state = RFCOMM_MULTIPLEXER_OPEN;
2504
2505 /* fall through */
2506
2507 case RFCOMM_MULTIPLEXER_OPEN:
2508 // multiplexer up and running -> start connecting
2509 channel->state = RFCOMM_CHANNEL_SEND_UIH_PN;
2510 l2cap_request_can_send_now_event(multiplexer->l2cap_cid);
2511 break;
2512 default:
2513 btstack_unreachable();
2514 break;
2492 }
2515 }
2493
2494 channel->state = RFCOMM_CHANNEL_SEND_UIH_PN;
2495
2496 // start connecting, if multiplexer is already up and running
2497 l2cap_request_can_send_now_event(multiplexer->l2cap_cid);
2498 return ERROR_CODE_SUCCESS;
2499}
2500
2501uint8_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){
2502 return rfcomm_channel_create_internal(packet_handler, addr, server_channel, 1, initial_credits, out_rfcomm_cid);
2503}
2504
2505uint8_t rfcomm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint16_t * out_rfcomm_cid){

--- 137 unchanged lines hidden ---
2516 return ERROR_CODE_SUCCESS;
2517}
2518
2519uint8_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){
2520 return rfcomm_channel_create_internal(packet_handler, addr, server_channel, 1, initial_credits, out_rfcomm_cid);
2521}
2522
2523uint8_t rfcomm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t addr, uint8_t server_channel, uint16_t * out_rfcomm_cid){

--- 137 unchanged lines hidden ---