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