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