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 #define __BTSTACK_FILE__ "spp_streamer_client.c" 39 40 /* 41 * spp_streamer_client.c 42 */ 43 44 // ***************************************************************************** 45 /* EXAMPLE_START(spp_streamer_client): Client for SPP Streamer 46 * 47 * @text Note: The SPP Streamer Client scans for and connects to SPP Streamer, 48 * and measures the throughput. 49 */ 50 // ***************************************************************************** 51 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <inttypes.h> 57 58 #include "btstack.h" 59 60 #define RFCOMM_SERVER_CHANNEL 1 61 62 #define TEST_COD 0x1234 63 64 typedef enum { 65 // SPP 66 W4_PEER_COD, 67 W4_SCAN_COMPLETE, 68 W4_SDP_RESULT, 69 W4_SDP_COMPLETE, 70 W4_RFCOMM_CHANNEL, 71 SENDING, 72 DONE 73 } state_t; 74 75 static btstack_packet_callback_registration_t hci_event_callback_registration; 76 77 static bd_addr_t peer_addr; 78 static state_t state; 79 80 // SPP 81 static uint16_t rfcomm_mtu; 82 static uint16_t rfcomm_cid = 0; 83 // static uint32_t data_to_send = DATA_VOLUME; 84 85 /** 86 * RFCOMM can make use for ERTM. Due to the need to re-transmit packets, 87 * a large buffer is needed to still get high throughput 88 */ 89 #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE_FOR_RFCOMM 90 static uint8_t ertm_buffer[20000]; 91 static l2cap_ertm_config_t ertm_config = { 92 0, // ertm mandatory 93 8, // max transmit 94 2000, 95 12000, 96 1000, // l2cap ertm mtu 97 8, 98 8, 99 0, // No FCS 100 }; 101 static int ertm_buffer_in_use; 102 static void rfcomm_ertm_request_handler(rfcomm_ertm_request_t * ertm_request){ 103 printf("ERTM Buffer requested, buffer in use %u\n", ertm_buffer_in_use); 104 if (ertm_buffer_in_use) return; 105 ertm_buffer_in_use = 1; 106 ertm_request->ertm_config = &ertm_config; 107 ertm_request->ertm_buffer = ertm_buffer; 108 ertm_request->ertm_buffer_size = sizeof(ertm_buffer); 109 } 110 static void rfcomm_ertm_released_handler(uint16_t ertm_id){ 111 printf("ERTM Buffer released, buffer in use %u, ertm_id %x\n", ertm_buffer_in_use, ertm_id); 112 ertm_buffer_in_use = 0; 113 } 114 #endif 115 116 /** 117 * Find remote peer by COD 118 */ 119 #define INQUIRY_INTERVAL 5 120 static void start_scan(void){ 121 printf("Starting inquiry scan..\n"); 122 state = W4_PEER_COD; 123 gap_inquiry_start(INQUIRY_INTERVAL); 124 } 125 static void stop_scan(void){ 126 printf("Stopping inquiry scan..\n"); 127 state = W4_SCAN_COMPLETE; 128 gap_inquiry_stop(); 129 } 130 /* 131 * @section Track throughput 132 * @text We calculate the throughput by setting a start time and measuring the amount of 133 * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 134 * and reset the counter and start time. 135 */ 136 137 /* LISTING_START(tracking): Tracking throughput */ 138 #define REPORT_INTERVAL_MS 3000 139 static uint32_t test_data_transferred; 140 static uint32_t test_data_start; 141 142 static void test_reset(void){ 143 test_data_start = btstack_run_loop_get_time_ms(); 144 test_data_transferred = 0; 145 } 146 147 static void test_track_transferred(int bytes_sent){ 148 test_data_transferred += bytes_sent; 149 // evaluate 150 uint32_t now = btstack_run_loop_get_time_ms(); 151 uint32_t time_passed = now - test_data_start; 152 if (time_passed < REPORT_INTERVAL_MS) return; 153 // print speed 154 int bytes_per_second = test_data_transferred * 1000 / time_passed; 155 printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 156 157 // restart 158 test_data_start = now; 159 test_data_transferred = 0; 160 } 161 /* LISTING_END(tracking): Tracking throughput */ 162 163 /* 164 * @section Packet Handler 165 * 166 * @text The packet handler of the combined example is just the combination of the individual packet handlers. 167 */ 168 169 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 170 UNUSED(channel); 171 172 bd_addr_t event_addr; 173 uint8_t rfcomm_channel_nr; 174 uint32_t class_of_device; 175 176 switch (packet_type) { 177 case HCI_EVENT_PACKET: 178 switch (hci_event_packet_get_type(packet)) { 179 180 case HCI_EVENT_PIN_CODE_REQUEST: 181 // inform about pin code request 182 printf("Pin code request - using '0000'\n"); 183 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 184 gap_pin_code_response(event_addr, "0000"); 185 break; 186 187 case HCI_EVENT_USER_CONFIRMATION_REQUEST: 188 // inform about user confirmation request 189 printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 190 printf("SSP User Confirmation Auto accept\n"); 191 break; 192 193 case RFCOMM_EVENT_INCOMING_CONNECTION: 194 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 195 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 196 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 197 rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 198 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 199 rfcomm_accept_connection(rfcomm_cid); 200 break; 201 202 case RFCOMM_EVENT_CHANNEL_OPENED: 203 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 204 if (rfcomm_event_channel_opened_get_status(packet)) { 205 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 206 } else { 207 rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 208 rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 209 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 210 test_reset(); 211 212 // disable page/inquiry scan to get max performance 213 gap_discoverable_control(0); 214 gap_connectable_control(0); 215 } 216 break; 217 218 case RFCOMM_EVENT_CHANNEL_CLOSED: 219 printf("RFCOMM channel closed\n"); 220 rfcomm_cid = 0; 221 222 // re-enable page/inquiry scan again 223 gap_discoverable_control(1); 224 gap_connectable_control(1); 225 break; 226 227 case BTSTACK_EVENT_STATE: 228 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; 229 start_scan(); 230 break; 231 232 case GAP_EVENT_INQUIRY_RESULT: 233 if (state != W4_PEER_COD) break; 234 class_of_device = gap_event_inquiry_result_get_class_of_device(packet); 235 gap_event_inquiry_result_get_bd_addr(packet, event_addr); 236 if (class_of_device == TEST_COD){ 237 memcpy(peer_addr, event_addr, 6); 238 printf("Peer found: %s\n", bd_addr_to_str(peer_addr)); 239 stop_scan(); 240 } else { 241 printf("Device found: %s with COD: 0x%06x\n", bd_addr_to_str(event_addr), (int) class_of_device); 242 } 243 break; 244 245 case GAP_EVENT_INQUIRY_COMPLETE: 246 switch (state){ 247 case W4_PEER_COD: 248 printf("Inquiry complete\n"); 249 printf("Peer not found, starting scan again\n"); 250 start_scan(); 251 break; 252 case W4_SCAN_COMPLETE: 253 printf("Start to connect\n"); 254 state = W4_RFCOMM_CHANNEL; 255 rfcomm_create_channel(packet_handler, peer_addr, RFCOMM_SERVER_CHANNEL, NULL); 256 break; 257 default: 258 break; 259 } 260 if (state == W4_PEER_COD){ 261 } 262 break; 263 264 default: 265 break; 266 } 267 break; 268 269 case RFCOMM_DATA_PACKET: 270 test_track_transferred(size); 271 272 #if 0 273 printf("RCV: '"); 274 for (i=0;i<size;i++){ 275 putchar(packet[i]); 276 } 277 printf("'\n"); 278 #endif 279 break; 280 281 default: 282 break; 283 } 284 } 285 286 /* 287 * @section Main Application Setup 288 * 289 * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 290 */ 291 292 293 /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */ 294 int btstack_main(int argc, const char * argv[]); 295 int btstack_main(int argc, const char * argv[]){ 296 UNUSED(argc); 297 (void)argv; 298 299 l2cap_init(); 300 301 rfcomm_init(); 302 rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 303 304 #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE_FOR_RFCOMM 305 // setup ERTM management 306 rfcomm_enable_l2cap_ertm(&rfcomm_ertm_request_handler, &rfcomm_ertm_released_handler); 307 #endif 308 309 // register for HCI events 310 hci_event_callback_registration.callback = &packet_handler; 311 hci_add_event_handler(&hci_event_callback_registration); 312 313 // init SDP 314 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 315 316 // turn on! 317 hci_power_control(HCI_POWER_ON); 318 319 return 0; 320 } 321 /* LISTING_END */ 322 /* EXAMPLE_END */ 323