1*bcf00d8fSMatthias Ringwald /* 2*bcf00d8fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*bcf00d8fSMatthias Ringwald * 4*bcf00d8fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*bcf00d8fSMatthias Ringwald * modification, are permitted provided that the following conditions 6*bcf00d8fSMatthias Ringwald * are met: 7*bcf00d8fSMatthias Ringwald * 8*bcf00d8fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*bcf00d8fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*bcf00d8fSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*bcf00d8fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*bcf00d8fSMatthias Ringwald * contributors may be used to endorse or promote products derived 15*bcf00d8fSMatthias Ringwald * from this software without specific prior written permission. 16*bcf00d8fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*bcf00d8fSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*bcf00d8fSMatthias Ringwald * monetary gain. 19*bcf00d8fSMatthias Ringwald * 20*bcf00d8fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*bcf00d8fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*bcf00d8fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*bcf00d8fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*bcf00d8fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*bcf00d8fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*bcf00d8fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*bcf00d8fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*bcf00d8fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*bcf00d8fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*bcf00d8fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*bcf00d8fSMatthias Ringwald * SUCH DAMAGE. 32*bcf00d8fSMatthias Ringwald * 33*bcf00d8fSMatthias Ringwald * Please inquire about commercial licensing options at 34*bcf00d8fSMatthias Ringwald * [email protected] 35*bcf00d8fSMatthias Ringwald * 36*bcf00d8fSMatthias Ringwald */ 37*bcf00d8fSMatthias Ringwald 38*bcf00d8fSMatthias Ringwald // ***************************************************************************** 39*bcf00d8fSMatthias Ringwald /* EXAMPLE_START(spp_flowcontrol): SPP Server - Flow Control 40*bcf00d8fSMatthias Ringwald * 41*bcf00d8fSMatthias Ringwald * @text This example adds explicit flow control for incoming RFCOMM data to the 42*bcf00d8fSMatthias Ringwald * SPP heartbeat counter example. We will highlight the changes compared to the 43*bcf00d8fSMatthias Ringwald * SPP counter example. 44*bcf00d8fSMatthias Ringwald */ 45*bcf00d8fSMatthias Ringwald // ***************************************************************************** 46*bcf00d8fSMatthias Ringwald 47*bcf00d8fSMatthias Ringwald #include <stdint.h> 48*bcf00d8fSMatthias Ringwald #include <stdio.h> 49*bcf00d8fSMatthias Ringwald #include <stdlib.h> 50*bcf00d8fSMatthias Ringwald #include <string.h> 51*bcf00d8fSMatthias Ringwald 52*bcf00d8fSMatthias Ringwald #include "hci_cmd.h" 53*bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h" 54*bcf00d8fSMatthias Ringwald #include "classic/sdp_util.h" 55*bcf00d8fSMatthias Ringwald 56*bcf00d8fSMatthias Ringwald #include "hci.h" 57*bcf00d8fSMatthias Ringwald #include "l2cap.h" 58*bcf00d8fSMatthias Ringwald #include "btstack_memory.h" 59*bcf00d8fSMatthias Ringwald #include "classic/rfcomm.h" 60*bcf00d8fSMatthias Ringwald #include "classic/sdp_server.h" 61*bcf00d8fSMatthias Ringwald #include "btstack_config.h" 62*bcf00d8fSMatthias Ringwald 63*bcf00d8fSMatthias Ringwald #define HEARTBEAT_PERIOD_MS 500 64*bcf00d8fSMatthias Ringwald 65*bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 66*bcf00d8fSMatthias Ringwald 67*bcf00d8fSMatthias Ringwald static uint8_t rfcomm_channel_nr = 1; 68*bcf00d8fSMatthias Ringwald static uint16_t rfcomm_channel_id; 69*bcf00d8fSMatthias Ringwald static uint8_t rfcomm_send_credit = 0; 70*bcf00d8fSMatthias Ringwald static uint8_t spp_service_buffer[150]; 71*bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 72*bcf00d8fSMatthias Ringwald 73*bcf00d8fSMatthias Ringwald /* @section SPP Service Setup 74*bcf00d8fSMatthias Ringwald * 75*bcf00d8fSMatthias Ringwald * @text Listing explicitFlowControl shows how to 76*bcf00d8fSMatthias Ringwald * provide one initial credit during RFCOMM service initialization. Please note 77*bcf00d8fSMatthias Ringwald * that providing a single credit effectively reduces the credit-based (sliding 78*bcf00d8fSMatthias Ringwald * window) flow control to a stop-and-wait flow control that limits the data 79*bcf00d8fSMatthias Ringwald * throughput substantially. 80*bcf00d8fSMatthias Ringwald */ 81*bcf00d8fSMatthias Ringwald 82*bcf00d8fSMatthias Ringwald /* LISTING_START(explicitFlowControl): Providing one initial credit during RFCOMM service initialization */ 83*bcf00d8fSMatthias Ringwald static void spp_service_setup(void){ 84*bcf00d8fSMatthias Ringwald 85*bcf00d8fSMatthias Ringwald // register for HCI events 86*bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 87*bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 88*bcf00d8fSMatthias Ringwald 89*bcf00d8fSMatthias Ringwald // init L2CAP 90*bcf00d8fSMatthias Ringwald l2cap_init(); 91*bcf00d8fSMatthias Ringwald 92*bcf00d8fSMatthias Ringwald // init RFCOMM 93*bcf00d8fSMatthias Ringwald rfcomm_init(); 94*bcf00d8fSMatthias Ringwald // reserved channel, mtu limited by l2cap, 1 credit 95*bcf00d8fSMatthias Ringwald rfcomm_register_service_with_initial_credits(rfcomm_channel_nr, 0xffff, 1); 96*bcf00d8fSMatthias Ringwald 97*bcf00d8fSMatthias Ringwald // init SDP, create record for SPP and register with SDP 98*bcf00d8fSMatthias Ringwald sdp_init(); 99*bcf00d8fSMatthias Ringwald memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 100*bcf00d8fSMatthias Ringwald sdp_create_spp_service(spp_service_buffer, 0x10001, 1, "SPP Counter"); 101*bcf00d8fSMatthias Ringwald sdp_register_service(spp_service_buffer); 102*bcf00d8fSMatthias Ringwald printf("SDP service buffer size: %u\n\r", (uint16_t) de_get_len(spp_service_buffer)); 103*bcf00d8fSMatthias Ringwald } 104*bcf00d8fSMatthias Ringwald /* LISTING_END */ 105*bcf00d8fSMatthias Ringwald 106*bcf00d8fSMatthias Ringwald /* @section Periodic Timer Setup 107*bcf00d8fSMatthias Ringwald * 108*bcf00d8fSMatthias Ringwald * @text Explicit credit management is 109*bcf00d8fSMatthias Ringwald * recommended when received RFCOMM data cannot be processed immediately. In this 110*bcf00d8fSMatthias Ringwald * example, delayed processing of received data is simulated with the help of a 111*bcf00d8fSMatthias Ringwald * periodic timer as follows. When the packet handler receives a data packet, it 112*bcf00d8fSMatthias Ringwald * does not provide a new credit, it sets a flag instead, see Listing phManual. 113*bcf00d8fSMatthias Ringwald * If the flag is set, a new 114*bcf00d8fSMatthias Ringwald * credit will be granted by the heartbeat handler, introducing a delay of up to 1 115*bcf00d8fSMatthias Ringwald * second. The heartbeat handler code is shown in Listing hbhManual. 116*bcf00d8fSMatthias Ringwald */ 117*bcf00d8fSMatthias Ringwald 118*bcf00d8fSMatthias Ringwald static btstack_timer_source_t heartbeat; 119*bcf00d8fSMatthias Ringwald 120*bcf00d8fSMatthias Ringwald /* LISTING_START(hbhManual): Heartbeat handler with manual credit management */ 121*bcf00d8fSMatthias Ringwald static void heartbeat_handler(struct btstack_timer_source *ts){ 122*bcf00d8fSMatthias Ringwald if (rfcomm_send_credit){ 123*bcf00d8fSMatthias Ringwald rfcomm_grant_credits(rfcomm_channel_id, 1); 124*bcf00d8fSMatthias Ringwald rfcomm_send_credit = 0; 125*bcf00d8fSMatthias Ringwald } 126*bcf00d8fSMatthias Ringwald btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); 127*bcf00d8fSMatthias Ringwald btstack_run_loop_add_timer(ts); 128*bcf00d8fSMatthias Ringwald } 129*bcf00d8fSMatthias Ringwald /* LISTING_END */ 130*bcf00d8fSMatthias Ringwald 131*bcf00d8fSMatthias Ringwald static void one_shot_timer_setup(void){ 132*bcf00d8fSMatthias Ringwald heartbeat.process = &heartbeat_handler; 133*bcf00d8fSMatthias Ringwald btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); 134*bcf00d8fSMatthias Ringwald btstack_run_loop_add_timer(&heartbeat); 135*bcf00d8fSMatthias Ringwald } 136*bcf00d8fSMatthias Ringwald 137*bcf00d8fSMatthias Ringwald /* LISTING_START(phManual): Packet handler with manual credit management */ 138*bcf00d8fSMatthias Ringwald // Bluetooth logic 139*bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 140*bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */ 141*bcf00d8fSMatthias Ringwald bd_addr_t event_addr; 142*bcf00d8fSMatthias Ringwald uint8_t rfcomm_channel_nr; 143*bcf00d8fSMatthias Ringwald uint16_t mtu; 144*bcf00d8fSMatthias Ringwald int i; 145*bcf00d8fSMatthias Ringwald 146*bcf00d8fSMatthias Ringwald switch (packet_type) { 147*bcf00d8fSMatthias Ringwald case HCI_EVENT_PACKET: 148*bcf00d8fSMatthias Ringwald switch (packet[0]) { 149*bcf00d8fSMatthias Ringwald 150*bcf00d8fSMatthias Ringwald case BTSTACK_EVENT_STATE: 151*bcf00d8fSMatthias Ringwald if (packet[2] == HCI_STATE_WORKING) { 152*bcf00d8fSMatthias Ringwald printf("BTstack is up and running\n"); 153*bcf00d8fSMatthias Ringwald } 154*bcf00d8fSMatthias Ringwald break; 155*bcf00d8fSMatthias Ringwald 156*bcf00d8fSMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 157*bcf00d8fSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)){ 158*bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[6], event_addr); 159*bcf00d8fSMatthias Ringwald printf("BD-ADDR: %s\n\r", bd_addr_to_str(event_addr)); 160*bcf00d8fSMatthias Ringwald break; 161*bcf00d8fSMatthias Ringwald } 162*bcf00d8fSMatthias Ringwald break; 163*bcf00d8fSMatthias Ringwald 164*bcf00d8fSMatthias Ringwald case HCI_EVENT_LINK_KEY_REQUEST: 165*bcf00d8fSMatthias Ringwald // deny link key request 166*bcf00d8fSMatthias Ringwald printf("Link key request\n\r"); 167*bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[2], event_addr); 168*bcf00d8fSMatthias Ringwald hci_send_cmd(&hci_link_key_request_negative_reply, &event_addr); 169*bcf00d8fSMatthias Ringwald break; 170*bcf00d8fSMatthias Ringwald 171*bcf00d8fSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 172*bcf00d8fSMatthias Ringwald // inform about pin code request 173*bcf00d8fSMatthias Ringwald printf("Pin code request - using '0000'\n\r"); 174*bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[2], event_addr); 175*bcf00d8fSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 176*bcf00d8fSMatthias Ringwald break; 177*bcf00d8fSMatthias Ringwald 178*bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_INCOMING_CONNECTION: 179*bcf00d8fSMatthias Ringwald // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 180*bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[2], event_addr); 181*bcf00d8fSMatthias Ringwald rfcomm_channel_nr = packet[8]; 182*bcf00d8fSMatthias Ringwald rfcomm_channel_id = little_endian_read_16(packet, 9); 183*bcf00d8fSMatthias Ringwald printf("RFCOMM channel %u requested for %s\n\r", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 184*bcf00d8fSMatthias Ringwald rfcomm_accept_connection(rfcomm_channel_id); 185*bcf00d8fSMatthias Ringwald break; 186*bcf00d8fSMatthias Ringwald 187*bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: 188*bcf00d8fSMatthias Ringwald // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 189*bcf00d8fSMatthias Ringwald if (packet[2]) { 190*bcf00d8fSMatthias Ringwald printf("RFCOMM channel open failed, status %u\n\r", packet[2]); 191*bcf00d8fSMatthias Ringwald } else { 192*bcf00d8fSMatthias Ringwald rfcomm_channel_id = little_endian_read_16(packet, 12); 193*bcf00d8fSMatthias Ringwald mtu = little_endian_read_16(packet, 14); 194*bcf00d8fSMatthias Ringwald printf("\n\rRFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n\r", rfcomm_channel_id, mtu); 195*bcf00d8fSMatthias Ringwald } 196*bcf00d8fSMatthias Ringwald break; 197*bcf00d8fSMatthias Ringwald 198*bcf00d8fSMatthias Ringwald case RFCOMM_EVENT_CHANNEL_CLOSED: 199*bcf00d8fSMatthias Ringwald rfcomm_channel_id = 0; 200*bcf00d8fSMatthias Ringwald break; 201*bcf00d8fSMatthias Ringwald 202*bcf00d8fSMatthias Ringwald default: 203*bcf00d8fSMatthias Ringwald break; 204*bcf00d8fSMatthias Ringwald } 205*bcf00d8fSMatthias Ringwald break; 206*bcf00d8fSMatthias Ringwald /* LISTING_RESUME */ 207*bcf00d8fSMatthias Ringwald case RFCOMM_DATA_PACKET: 208*bcf00d8fSMatthias Ringwald for (i=0;i<size;i++){ 209*bcf00d8fSMatthias Ringwald putchar(packet[i]); 210*bcf00d8fSMatthias Ringwald }; 211*bcf00d8fSMatthias Ringwald putchar('\n'); 212*bcf00d8fSMatthias Ringwald rfcomm_send_credit = 1; 213*bcf00d8fSMatthias Ringwald break; 214*bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */ 215*bcf00d8fSMatthias Ringwald default: 216*bcf00d8fSMatthias Ringwald break; 217*bcf00d8fSMatthias Ringwald } 218*bcf00d8fSMatthias Ringwald /* LISTING_RESUME */ 219*bcf00d8fSMatthias Ringwald } 220*bcf00d8fSMatthias Ringwald /* LISTING_END */ 221*bcf00d8fSMatthias Ringwald 222*bcf00d8fSMatthias Ringwald 223*bcf00d8fSMatthias Ringwald 224*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 225*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 226*bcf00d8fSMatthias Ringwald 227*bcf00d8fSMatthias Ringwald spp_service_setup(); 228*bcf00d8fSMatthias Ringwald one_shot_timer_setup(); 229*bcf00d8fSMatthias Ringwald 230*bcf00d8fSMatthias Ringwald puts("SPP FlowControl Demo: simulates processing on received data...\n\r"); 231*bcf00d8fSMatthias Ringwald gap_set_local_name("BTstack SPP Flow Control"); 232*bcf00d8fSMatthias Ringwald gap_discoverable_control(1); 233*bcf00d8fSMatthias Ringwald 234*bcf00d8fSMatthias Ringwald // turn on! 235*bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 236*bcf00d8fSMatthias Ringwald 237*bcf00d8fSMatthias Ringwald return 0; 238*bcf00d8fSMatthias Ringwald } 239*bcf00d8fSMatthias Ringwald /* EXAMPLE_END */ 240*bcf00d8fSMatthias Ringwald 241*bcf00d8fSMatthias Ringwald 242