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