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