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 // ***************************************************************************** 39 /* EXAMPLE_START(spp_flowcontrol): SPP Server - Flow Control 40 * 41 * @text This example adds explicit flow control for incoming RFCOMM data to the 42 * SPP heartbeat counter example. We will highlight the changes compared to the 43 * SPP counter example. 44 */ 45 // ***************************************************************************** 46 47 #include <stdint.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 52 #include "hci_cmd.h" 53 #include "btstack_run_loop.h" 54 #include "classic/sdp_util.h" 55 56 #include "hci.h" 57 #include "l2cap.h" 58 #include "btstack_memory.h" 59 #include "classic/rfcomm.h" 60 #include "classic/sdp_server.h" 61 #include "btstack_config.h" 62 63 #define HEARTBEAT_PERIOD_MS 500 64 65 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 66 67 static uint8_t rfcomm_channel_nr = 1; 68 static uint16_t rfcomm_channel_id; 69 static uint8_t rfcomm_send_credit = 0; 70 static uint8_t spp_service_buffer[150]; 71 static btstack_packet_callback_registration_t hci_event_callback_registration; 72 73 /* @section SPP Service Setup 74 * 75 * @text Listing explicitFlowControl shows how to 76 * provide one initial credit during RFCOMM service initialization. Please note 77 * that providing a single credit effectively reduces the credit-based (sliding 78 * window) flow control to a stop-and-wait flow control that limits the data 79 * throughput substantially. 80 */ 81 82 /* LISTING_START(explicitFlowControl): Providing one initial credit during RFCOMM service initialization */ 83 static void spp_service_setup(void){ 84 85 // register for HCI events 86 hci_event_callback_registration.callback = &packet_handler; 87 hci_add_event_handler(&hci_event_callback_registration); 88 89 // init L2CAP 90 l2cap_init(); 91 92 // init RFCOMM 93 rfcomm_init(); 94 // reserved channel, mtu limited by l2cap, 1 credit 95 rfcomm_register_service_with_initial_credits(rfcomm_channel_nr, 0xffff, 1); 96 97 // init SDP, create record for SPP and register with SDP 98 sdp_init(); 99 memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 100 sdp_create_spp_service(spp_service_buffer, 0x10001, 1, "SPP Counter"); 101 sdp_register_service(spp_service_buffer); 102 printf("SDP service buffer size: %u\n\r", (uint16_t) de_get_len(spp_service_buffer)); 103 } 104 /* LISTING_END */ 105 106 /* @section Periodic Timer Setup 107 * 108 * @text Explicit credit management is 109 * recommended when received RFCOMM data cannot be processed immediately. In this 110 * example, delayed processing of received data is simulated with the help of a 111 * periodic timer as follows. When the packet handler receives a data packet, it 112 * does not provide a new credit, it sets a flag instead, see Listing phManual. 113 * If the flag is set, a new 114 * credit will be granted by the heartbeat handler, introducing a delay of up to 1 115 * second. The heartbeat handler code is shown in Listing hbhManual. 116 */ 117 118 static btstack_timer_source_t heartbeat; 119 120 /* LISTING_START(hbhManual): Heartbeat handler with manual credit management */ 121 static void heartbeat_handler(struct btstack_timer_source *ts){ 122 if (rfcomm_send_credit){ 123 rfcomm_grant_credits(rfcomm_channel_id, 1); 124 rfcomm_send_credit = 0; 125 } 126 btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); 127 btstack_run_loop_add_timer(ts); 128 } 129 /* LISTING_END */ 130 131 static void one_shot_timer_setup(void){ 132 heartbeat.process = &heartbeat_handler; 133 btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); 134 btstack_run_loop_add_timer(&heartbeat); 135 } 136 137 /* LISTING_START(phManual): Packet handler with manual credit management */ 138 // Bluetooth logic 139 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 140 /* LISTING_PAUSE */ 141 bd_addr_t event_addr; 142 uint8_t rfcomm_channel_nr; 143 uint16_t mtu; 144 int i; 145 146 switch (packet_type) { 147 case HCI_EVENT_PACKET: 148 switch (hci_event_packet_get_type(packet)) { 149 150 case BTSTACK_EVENT_STATE: 151 if (packet[2] == HCI_STATE_WORKING) { 152 printf("BTstack is up and running\n"); 153 } 154 break; 155 156 case HCI_EVENT_COMMAND_COMPLETE: 157 if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)){ 158 reverse_bd_addr(&packet[6], event_addr); 159 printf("BD-ADDR: %s\n\r", bd_addr_to_str(event_addr)); 160 break; 161 } 162 break; 163 164 case HCI_EVENT_LINK_KEY_REQUEST: 165 // deny link key request 166 printf("Link key request\n\r"); 167 reverse_bd_addr(&packet[2], event_addr); 168 hci_send_cmd(&hci_link_key_request_negative_reply, &event_addr); 169 break; 170 171 case HCI_EVENT_PIN_CODE_REQUEST: 172 // inform about pin code request 173 printf("Pin code request - using '0000'\n\r"); 174 reverse_bd_addr(&packet[2], event_addr); 175 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 176 break; 177 178 case RFCOMM_EVENT_INCOMING_CONNECTION: 179 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 180 reverse_bd_addr(&packet[2], event_addr); 181 rfcomm_channel_nr = packet[8]; 182 rfcomm_channel_id = little_endian_read_16(packet, 9); 183 printf("RFCOMM channel %u requested for %s\n\r", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 184 rfcomm_accept_connection(rfcomm_channel_id); 185 break; 186 187 case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE: 188 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 189 if (packet[2]) { 190 printf("RFCOMM channel open failed, status %u\n\r", packet[2]); 191 } else { 192 rfcomm_channel_id = little_endian_read_16(packet, 12); 193 mtu = little_endian_read_16(packet, 14); 194 printf("\n\rRFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n\r", rfcomm_channel_id, mtu); 195 } 196 break; 197 198 case RFCOMM_EVENT_CHANNEL_CLOSED: 199 rfcomm_channel_id = 0; 200 break; 201 202 default: 203 break; 204 } 205 break; 206 /* LISTING_RESUME */ 207 case RFCOMM_DATA_PACKET: 208 for (i=0;i<size;i++){ 209 putchar(packet[i]); 210 }; 211 putchar('\n'); 212 rfcomm_send_credit = 1; 213 break; 214 /* LISTING_PAUSE */ 215 default: 216 break; 217 } 218 /* LISTING_RESUME */ 219 } 220 /* LISTING_END */ 221 222 223 224 int btstack_main(int argc, const char * argv[]); 225 int btstack_main(int argc, const char * argv[]){ 226 227 spp_service_setup(); 228 one_shot_timer_setup(); 229 230 puts("SPP FlowControl Demo: simulates processing on received data...\n\r"); 231 gap_set_local_name("BTstack SPP Flow Control"); 232 gap_discoverable_control(1); 233 234 // turn on! 235 hci_power_control(HCI_POWER_ON); 236 237 return 0; 238 } 239 /* EXAMPLE_END */ 240 241 242