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_and_gatt_streamer.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(spp_and_le_streamer): Dual mode example 42 * 43 * @text The SPP and LE Streamer example combines the Bluetooth Classic SPP Streamer 44 * and the Bluetooth LE Streamer into a single application. 45 * 46 * @text In this Section, we only point out the differences to the individual examples 47 * and how how the stack is configured. 48 * 49 * @text Note: To test, please run the example, and then: 50 * - for SPP pair from a remote device, and open the Virtual Serial Port, 51 * - for LE use some GATT Explorer, e.g. LightBlue, BLExplr, to enable notifications. 52 * 53 */ 54 // ***************************************************************************** 55 56 #include <stdint.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <inttypes.h> 61 62 #include "btstack.h" 63 #include "spp_and_gatt_streamer.h" 64 65 int btstack_main(int argc, const char * argv[]); 66 67 #define RFCOMM_SERVER_CHANNEL 1 68 #define HEARTBEAT_PERIOD_MS 1000 69 70 #define TEST_COD 0x1234 71 #define NUM_ROWS 25 72 #define NUM_COLS 40 73 #define DATA_VOLUME (10 * 1000 * 1000) 74 75 /* 76 * @section Advertisements 77 * 78 * @text The Flags attribute in the Advertisement Data indicates if a device is in dual-mode or not. 79 * Flag 0x06 indicates LE General Discoverable, BR/EDR not supported although we're actually using BR/EDR. 80 * In the past, there have been problems with Anrdoid devices when the flag was not set. 81 * Setting it should prevent the remote implementation to try to use GATT over LE/EDR, which is not 82 * implemented by BTstack. So, setting the flag seems like the safer choice (while it's technically incorrect). 83 */ 84 /* LISTING_START(advertisements): Advertisement data: Flag 0x06 indicates LE-only device */ 85 const uint8_t adv_data[] = { 86 // Flags general discoverable, BR/EDR not supported 87 0x02, 0x01, 0x06, 88 // Name 89 0x0c, 0x09, 'L', 'E', ' ', 'S', 't', 'r', 'e', 'a', 'm', 'e', 'r', 90 }; 91 92 static btstack_packet_callback_registration_t hci_event_callback_registration; 93 94 uint8_t adv_data_len = sizeof(adv_data); 95 96 static uint8_t test_data[NUM_ROWS * NUM_COLS]; 97 98 // SPP 99 static uint8_t spp_service_buffer[150]; 100 101 static uint16_t spp_test_data_len; 102 static uint16_t rfcomm_mtu; 103 static uint16_t rfcomm_cid = 0; 104 // static uint32_t data_to_send = DATA_VOLUME; 105 106 // LE 107 static uint16_t att_mtu; 108 static int counter = 'A'; 109 static int le_notification_enabled; 110 static uint16_t le_test_data_len; 111 static hci_con_handle_t le_connection_handle; 112 113 #ifdef ENABLE_GATT_OVER_CLASSIC 114 static uint8_t gatt_service_buffer[70]; 115 #endif 116 117 /* 118 * @section Track throughput 119 * @text We calculate the throughput by setting a start time and measuring the amount of 120 * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 121 * and reset the counter and start time. 122 */ 123 124 /* LISTING_START(tracking): Tracking throughput */ 125 #define REPORT_INTERVAL_MS 3000 126 static uint32_t test_data_transferred; 127 static uint32_t test_data_start; 128 129 static void test_reset(void){ 130 test_data_start = btstack_run_loop_get_time_ms(); 131 test_data_transferred = 0; 132 } 133 134 static void test_track_transferred(int bytes_sent){ 135 test_data_transferred += bytes_sent; 136 // evaluate 137 uint32_t now = btstack_run_loop_get_time_ms(); 138 uint32_t time_passed = now - test_data_start; 139 if (time_passed < REPORT_INTERVAL_MS) return; 140 // print speed 141 int bytes_per_second = test_data_transferred * 1000 / time_passed; 142 printf("%u bytes -> %u.%03u kB/s\n", (int) test_data_transferred, (int) bytes_per_second / 1000, bytes_per_second % 1000); 143 144 // restart 145 test_data_start = now; 146 test_data_transferred = 0; 147 } 148 /* LISTING_END(tracking): Tracking throughput */ 149 150 151 static void spp_create_test_data(void){ 152 int x,y; 153 for (y=0;y<NUM_ROWS;y++){ 154 for (x=0;x<NUM_COLS-2;x++){ 155 test_data[y*NUM_COLS+x] = '0' + (x % 10); 156 } 157 test_data[y*NUM_COLS+NUM_COLS-2] = '\n'; 158 test_data[y*NUM_COLS+NUM_COLS-1] = '\r'; 159 } 160 } 161 162 static void spp_send_packet(void){ 163 rfcomm_send(rfcomm_cid, (uint8_t*) test_data, spp_test_data_len); 164 165 test_track_transferred(spp_test_data_len); 166 #if 0 167 if (data_to_send <= spp_test_data_len){ 168 printf("SPP Streamer: enough data send, closing channel\n"); 169 rfcomm_disconnect(rfcomm_cid); 170 rfcomm_cid = 0; 171 return; 172 } 173 data_to_send -= spp_test_data_len; 174 #endif 175 rfcomm_request_can_send_now_event(rfcomm_cid); 176 } 177 178 static void le_streamer(void){ 179 // check if we can send 180 if (!le_notification_enabled) return; 181 182 // create test data 183 counter++; 184 if (counter > 'Z') counter = 'A'; 185 memset(test_data, counter, sizeof(test_data)); 186 187 // send 188 att_server_notify(le_connection_handle, ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE, (uint8_t*) test_data, le_test_data_len); 189 190 // track 191 test_track_transferred(le_test_data_len); 192 193 // request next send event 194 att_server_request_can_send_now_event(le_connection_handle); 195 } 196 197 /* 198 * @section HCI Packet Handler 199 * 200 * @text The packet handler of the combined example is just the combination of the individual packet handlers. 201 */ 202 203 static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 204 UNUSED(channel); 205 UNUSED(size); 206 207 bd_addr_t event_addr; 208 uint16_t conn_interval; 209 hci_con_handle_t con_handle; 210 211 switch (packet_type) { 212 case HCI_EVENT_PACKET: 213 switch (hci_event_packet_get_type(packet)) { 214 215 case HCI_EVENT_PIN_CODE_REQUEST: 216 // inform about pin code request 217 printf("Pin code request - using '0000'\n"); 218 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 219 gap_pin_code_response(event_addr, "0000"); 220 break; 221 222 case HCI_EVENT_USER_CONFIRMATION_REQUEST: 223 // inform about user confirmation request 224 printf("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", little_endian_read_32(packet, 8)); 225 printf("SSP User Confirmation Auto accept\n"); 226 break; 227 228 case HCI_EVENT_LE_META: 229 switch (hci_event_le_meta_get_subevent_code(packet)) { 230 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 231 // print connection parameters (without using float operations) 232 con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 233 conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 234 printf("LE Connection - Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3)); 235 printf("LE Connection - Connection Latency: %u\n", hci_subevent_le_connection_complete_get_conn_latency(packet)); 236 237 // request min con interval 15 ms for iOS 11+ 238 printf("LE Connection - Request 15 ms connection interval\n"); 239 gap_request_connection_parameter_update(con_handle, 12, 12, 0, 0x0048); 240 break; 241 242 case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 243 // print connection parameters (without using float operations) 244 con_handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 245 conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 246 printf("LE Connection - Connection Param update - connection interval %u.%02u ms, latency %u\n", conn_interval * 125 / 100, 247 25 * (conn_interval & 3), hci_subevent_le_connection_update_complete_get_conn_latency(packet)); 248 break; 249 250 default: 251 break; 252 } 253 break; 254 255 case HCI_EVENT_DISCONNECTION_COMPLETE: 256 // re-enable page/inquiry scan again 257 gap_discoverable_control(1); 258 gap_connectable_control(1); 259 // re-enable advertisements 260 gap_advertisements_enable(1); 261 le_notification_enabled = 0; 262 break; 263 264 default: 265 break; 266 } 267 break; 268 269 default: 270 break; 271 } 272 } 273 274 /* 275 * @section RFCOMM Packet Handler 276 * 277 * @text The RFCOMM packet handler accepts incoming connection and triggers sending of RFCOMM data on RFCOMM_EVENT_CAN_SEND_NOW 278 */ 279 280 static void rfcomm_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 281 UNUSED(channel); 282 283 bd_addr_t event_addr; 284 uint8_t rfcomm_channel_nr; 285 286 switch (packet_type) { 287 case HCI_EVENT_PACKET: 288 switch (hci_event_packet_get_type(packet)) { 289 290 case RFCOMM_EVENT_INCOMING_CONNECTION: 291 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 292 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 293 rfcomm_channel_nr = rfcomm_event_incoming_connection_get_server_channel(packet); 294 rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 295 printf("RFCOMM channel %u requested for %s\n", rfcomm_channel_nr, bd_addr_to_str(event_addr)); 296 rfcomm_accept_connection(rfcomm_cid); 297 break; 298 299 case RFCOMM_EVENT_CHANNEL_OPENED: 300 // data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16) 301 if (rfcomm_event_channel_opened_get_status(packet)) { 302 printf("RFCOMM channel open failed, status %u\n", rfcomm_event_channel_opened_get_status(packet)); 303 } else { 304 rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 305 rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 306 printf("RFCOMM channel open succeeded. New RFCOMM Channel ID %u, max frame size %u\n", rfcomm_cid, rfcomm_mtu); 307 308 spp_test_data_len = rfcomm_mtu; 309 if (spp_test_data_len > sizeof(test_data)){ 310 spp_test_data_len = sizeof(test_data); 311 } 312 313 // disable page/inquiry scan to get max performance 314 gap_discoverable_control(0); 315 gap_connectable_control(0); 316 // disable advertisements 317 gap_advertisements_enable(0); 318 319 test_reset(); 320 rfcomm_request_can_send_now_event(rfcomm_cid); 321 } 322 break; 323 324 case RFCOMM_EVENT_CAN_SEND_NOW: 325 spp_send_packet(); 326 break; 327 328 case RFCOMM_EVENT_CHANNEL_CLOSED: 329 printf("RFCOMM channel closed\n"); 330 rfcomm_cid = 0; 331 break; 332 333 default: 334 break; 335 } 336 break; 337 338 case RFCOMM_DATA_PACKET: 339 test_track_transferred(size); 340 #if 0 341 printf("RCV: '"); 342 for (i=0;i<size;i++){ 343 putchar(packet[i]); 344 } 345 printf("'\n"); 346 #endif 347 break; 348 349 default: 350 break; 351 } 352 } 353 354 /* 355 * @section ATT Packet Handler 356 * 357 * @text The packet handler is used to track the ATT MTU Exchange and trigger ATT send 358 */ 359 360 /* LISTING_START(attPacketHandler): Packet Handler */ 361 static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 362 UNUSED(channel); 363 UNUSED(size); 364 365 if (packet_type != HCI_EVENT_PACKET) return; 366 367 switch (hci_event_packet_get_type(packet)) { 368 case ATT_EVENT_CONNECTED: 369 le_connection_handle = att_event_connected_get_handle(packet); 370 att_mtu = att_server_get_mtu(le_connection_handle); 371 le_test_data_len = btstack_min(att_server_get_mtu(le_connection_handle) - 3, sizeof(test_data)); 372 printf("ATT MTU = %u\n", att_mtu); 373 break; 374 375 case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 376 att_mtu = att_event_mtu_exchange_complete_get_MTU(packet); 377 le_test_data_len = btstack_min(att_mtu - 3, sizeof(test_data)); 378 printf("ATT MTU = %u\n", att_mtu); 379 break; 380 381 case ATT_EVENT_CAN_SEND_NOW: 382 le_streamer(); 383 break; 384 385 case ATT_EVENT_DISCONNECTED: 386 le_notification_enabled = 0; 387 le_connection_handle = HCI_CON_HANDLE_INVALID; 388 break; 389 390 default: 391 break; 392 } 393 } 394 395 // ATT Client Read Callback for Dynamic Data 396 // - if buffer == NULL, don't copy data, just return size of value 397 // - if buffer != NULL, copy data and return number bytes copied 398 // @param offset defines start of attribute value 399 static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 400 UNUSED(con_handle); 401 UNUSED(att_handle); 402 UNUSED(offset); 403 UNUSED(buffer); 404 UNUSED(buffer_size); 405 return 0; 406 } 407 408 // write requests 409 static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){ 410 UNUSED(con_handle); 411 UNUSED(offset); 412 UNUSED(buffer_size); 413 414 // printf("att_write_callback att_handle %04x, transaction mode %u\n", att_handle, transaction_mode); 415 if (transaction_mode != ATT_TRANSACTION_MODE_NONE) return 0; 416 switch(att_handle){ 417 case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE: 418 le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION; 419 printf("Notifications enabled %u\n", le_notification_enabled); 420 if (le_notification_enabled){ 421 att_server_request_can_send_now_event(le_connection_handle); 422 } 423 424 // disable page/inquiry scan to get max performance 425 gap_discoverable_control(0); 426 gap_connectable_control(0); 427 428 test_reset(); 429 break; 430 default: 431 break; 432 } 433 return 0; 434 } 435 436 /* 437 * @section Main Application Setup 438 * 439 * @text As with the packet and the heartbeat handlers, the combined app setup contains the code from the individual example setups. 440 */ 441 442 443 /* LISTING_START(MainConfiguration): Init L2CAP RFCOMM SDO SM ATT Server and start heartbeat timer */ 444 int btstack_main(int argc, const char * argv[]) 445 { 446 UNUSED(argc); 447 (void)argv; 448 449 l2cap_init(); 450 451 rfcomm_init(); 452 rfcomm_register_service(rfcomm_packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff); 453 454 // init SDP, create record for SPP and register with SDP 455 sdp_init(); 456 memset(spp_service_buffer, 0, sizeof(spp_service_buffer)); 457 spp_create_sdp_record(spp_service_buffer, 0x10001, RFCOMM_SERVER_CHANNEL, "SPP Streamer"); 458 sdp_register_service(spp_service_buffer); 459 460 #ifdef ENABLE_GATT_OVER_CLASSIC 461 // init SDP, create record for GATT and register with SDP 462 memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer)); 463 gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE); 464 sdp_register_service(gatt_service_buffer); 465 #endif 466 467 gap_set_local_name("SPP and LE Streamer 00:00:00:00:00:00"); 468 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 469 470 // short-cut to find other SPP Streamer 471 gap_set_class_of_device(TEST_COD); 472 473 gap_discoverable_control(1); 474 475 // setup le device db 476 le_device_db_init(); 477 478 // setup SM: Display only 479 sm_init(); 480 481 // setup ATT server 482 att_server_init(profile_data, att_read_callback, att_write_callback); 483 484 // register for HCI events 485 hci_event_callback_registration.callback = &hci_packet_handler; 486 hci_add_event_handler(&hci_event_callback_registration); 487 488 // register for ATT events 489 att_server_register_packet_handler(att_packet_handler); 490 491 // setup advertisements 492 uint16_t adv_int_min = 0x0030; 493 uint16_t adv_int_max = 0x0030; 494 uint8_t adv_type = 0; 495 bd_addr_t null_addr; 496 memset(null_addr, 0, 6); 497 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 498 gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 499 gap_advertisements_enable(1); 500 501 spp_create_test_data(); 502 503 // turn on! 504 hci_power_control(HCI_POWER_ON); 505 506 return 0; 507 } 508 /* LISTING_END */ 509 /* EXAMPLE_END */ 510