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 HCI_EVENT_COMMAND_COMPLETE: 142 if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)){ 143 reverse_bd_addr(&packet[6], event_addr); 144 printf("BD-ADDR: %s\n\r", bd_addr_to_str(event_addr)); 145 break; 146 } 147 break; 148 149 case HCI_EVENT_LINK_KEY_REQUEST: 150 // deny link key request 151 printf("Link key request\n\r"); 152 hci_event_link_key_request_get_bd_addr(packet, event_addr); 153 hci_send_cmd(&hci_link_key_request_negative_reply, &event_addr); 154 break; 155 156 case HCI_EVENT_PIN_CODE_REQUEST: 157 // inform about pin code request 158 printf("Pin code request - using '0000'\n\r"); 159 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 160 hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 161 break; 162 163 case RFCOMM_EVENT_INCOMING_CONNECTION: 164 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 165 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 166 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 167 rfcomm_channel_id = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 168 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 169 rfcomm_accept_connection(rfcomm_channel_id); 170 break; 171 172 case RFCOMM_EVENT_CHANNEL_OPENED: 173 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 174 if (rfcomm_event_channel_opened_get_status(packet)) { 175 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 176 } else { 177 rfcomm_channel_id = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 178 mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 179 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_channel_id, mtu); 180 } 181 break; 182 183 case RFCOMM_EVENT_CHANNEL_CLOSED: 184 rfcomm_channel_id = 0; 185 break; 186 187 default: 188 break; 189 } 190 break; 191 /* LISTING_RESUME */ 192 case RFCOMM_DATA_PACKET: 193 for (i=0;i<size;i++){ 194 putchar(packet[i]); 195 }; 196 putchar('\n'); 197 rfcomm_send_credit = 1; 198 break; 199 /* LISTING_PAUSE */ 200 default: 201 break; 202 } 203 /* LISTING_RESUME */ 204 } 205 /* LISTING_END */ 206 207 208 209 int btstack_main(int argc, const char * argv[]); 210 int btstack_main(int argc, const char * argv[]){ 211 212 spp_service_setup(); 213 one_shot_timer_setup(); 214 215 puts("SPP FlowControl Demo: simulates processing on received data...\n\r"); 216 gap_set_local_name("BTstack SPP Flow Control"); 217 gap_discoverable_control(1); 218 219 // turn on! 220 hci_power_control(HCI_POWER_ON); 221 222 return 0; 223 } 224 /* EXAMPLE_END */ 225 226 227