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 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH 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 38ec8ae085SMilanka Ringwald #define BTSTACK_FILE__ "gatt_streamer_server.c" 39bdc352b1SMatthias Ringwald 40bdc352b1SMatthias Ringwald // ***************************************************************************** 41ec8ae085SMilanka Ringwald /* EXAMPLE_START(gatt_streamer_server): Performance - Stream Data over GATT (Server) 42bdc352b1SMatthias Ringwald * 43bdc352b1SMatthias Ringwald * @text All newer operating systems provide GATT Client functionality. 44bdc352b1SMatthias Ringwald * This example shows how to get a maximal throughput via BLE: 45bdc352b1SMatthias Ringwald * - send whenever possible, 46bdc352b1SMatthias Ringwald * - use the max ATT MTU. 47bdc352b1SMatthias Ringwald * 48bdc352b1SMatthias Ringwald * @text In theory, we should also update the connection parameters, but we already get 49bdc352b1SMatthias Ringwald * a connection interval of 30 ms and there's no public way to use a shorter 50bdc352b1SMatthias Ringwald * interval with iOS (if we're not implementing an HID device). 51bdc352b1SMatthias Ringwald * 52bdc352b1SMatthias Ringwald * @text Note: To start the streaming, run the example. 53bdc352b1SMatthias Ringwald * On remote device use some GATT Explorer, e.g. LightBlue, BLExplr to enable notifications. 54bdc352b1SMatthias Ringwald */ 55bdc352b1SMatthias Ringwald // ***************************************************************************** 56bdc352b1SMatthias Ringwald 57bdc352b1SMatthias Ringwald #include <inttypes.h> 58bdc352b1SMatthias Ringwald #include <stdint.h> 59bdc352b1SMatthias Ringwald #include <stdio.h> 60bdc352b1SMatthias Ringwald #include <stdlib.h> 61bdc352b1SMatthias Ringwald #include <string.h> 62bdc352b1SMatthias Ringwald 63bdc352b1SMatthias Ringwald #include "btstack.h" 64bdc352b1SMatthias Ringwald 65bdc352b1SMatthias Ringwald // le_streamer.gatt contains the declaration of the provided GATT Services + Characteristics 66bdc352b1SMatthias Ringwald // le_streamer.h contains the binary representation of le_streamer.gatt 67bdc352b1SMatthias Ringwald // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py le_streamer.gatt le_streamer.h 68bdc352b1SMatthias Ringwald // it needs to be regenerated when the GATT Database declared in le_streamer.gatt file is modified 69bdc352b1SMatthias Ringwald #include "gatt_streamer_server.h" 70bdc352b1SMatthias Ringwald 71bdc352b1SMatthias Ringwald #define REPORT_INTERVAL_MS 3000 72bdc352b1SMatthias Ringwald #define MAX_NR_CONNECTIONS 3 73bdc352b1SMatthias Ringwald 74bdc352b1SMatthias Ringwald 75bdc352b1SMatthias Ringwald static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 76bdc352b1SMatthias Ringwald static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 77bdc352b1SMatthias 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); 78bdc352b1SMatthias Ringwald static void streamer(void); 79bdc352b1SMatthias Ringwald 80e06798b6SMatthias Ringwald // Flags general discoverable, BR/EDR supported (== not supported flag not set) when ENABLE_GATT_OVER_CLASSIC is enabled 81e06798b6SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 82edf70fbfSMatthias Ringwald #define APP_AD_FLAGS 0x02 83e06798b6SMatthias Ringwald #else 84edf70fbfSMatthias Ringwald #define APP_AD_FLAGS 0x06 85e06798b6SMatthias Ringwald #endif 86e06798b6SMatthias Ringwald 87bdc352b1SMatthias Ringwald const uint8_t adv_data[] = { 88e06798b6SMatthias Ringwald // Flags general discoverable 89edf70fbfSMatthias Ringwald 0x02, BLUETOOTH_DATA_TYPE_FLAGS, APP_AD_FLAGS, 90bdc352b1SMatthias Ringwald // Name 91bdc352b1SMatthias Ringwald 0x0c, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 'L', 'E', ' ', 'S', 't', 'r', 'e', 'a', 'm', 'e', 'r', 92bdc352b1SMatthias Ringwald // Incomplete List of 16-bit Service Class UUIDs -- FF10 - only valid for testing! 93bdc352b1SMatthias Ringwald 0x03, BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 0x10, 0xff, 94bdc352b1SMatthias Ringwald }; 95bdc352b1SMatthias Ringwald const uint8_t adv_data_len = sizeof(adv_data); 96bdc352b1SMatthias Ringwald 97bdc352b1SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 98bdc352b1SMatthias Ringwald 99bdc352b1SMatthias Ringwald // support for multiple clients 100bdc352b1SMatthias Ringwald typedef struct { 101bdc352b1SMatthias Ringwald char name; 102bdc352b1SMatthias Ringwald int le_notification_enabled; 103bdc352b1SMatthias Ringwald uint16_t value_handle; 104bdc352b1SMatthias Ringwald hci_con_handle_t connection_handle; 105bdc352b1SMatthias Ringwald int counter; 106bdc352b1SMatthias Ringwald char test_data[200]; 107bdc352b1SMatthias Ringwald int test_data_len; 108bdc352b1SMatthias Ringwald uint32_t test_data_sent; 109bdc352b1SMatthias Ringwald uint32_t test_data_start; 110bdc352b1SMatthias Ringwald } le_streamer_connection_t; 111bdc352b1SMatthias Ringwald static le_streamer_connection_t le_streamer_connections[MAX_NR_CONNECTIONS]; 112bdc352b1SMatthias Ringwald 113bdc352b1SMatthias Ringwald // round robin sending 114bdc352b1SMatthias Ringwald static int connection_index; 115bdc352b1SMatthias Ringwald 116bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 117bdc352b1SMatthias Ringwald static uint8_t gatt_service_buffer[70]; 118bdc352b1SMatthias Ringwald #endif 119bdc352b1SMatthias Ringwald 120bdc352b1SMatthias Ringwald static void init_connections(void){ 121bdc352b1SMatthias Ringwald // track connections 122bdc352b1SMatthias Ringwald int i; 123bdc352b1SMatthias Ringwald for (i=0;i<MAX_NR_CONNECTIONS;i++){ 124bdc352b1SMatthias Ringwald le_streamer_connections[i].connection_handle = HCI_CON_HANDLE_INVALID; 125bdc352b1SMatthias Ringwald le_streamer_connections[i].name = 'A' + i; 126bdc352b1SMatthias Ringwald } 127bdc352b1SMatthias Ringwald } 128bdc352b1SMatthias Ringwald 129bdc352b1SMatthias Ringwald static le_streamer_connection_t * connection_for_conn_handle(hci_con_handle_t conn_handle){ 130bdc352b1SMatthias Ringwald int i; 131bdc352b1SMatthias Ringwald for (i=0;i<MAX_NR_CONNECTIONS;i++){ 132bdc352b1SMatthias Ringwald if (le_streamer_connections[i].connection_handle == conn_handle) return &le_streamer_connections[i]; 133bdc352b1SMatthias Ringwald } 134bdc352b1SMatthias Ringwald return NULL; 135bdc352b1SMatthias Ringwald } 136bdc352b1SMatthias Ringwald 137bdc352b1SMatthias Ringwald static void next_connection_index(void){ 138bdc352b1SMatthias Ringwald connection_index++; 139bdc352b1SMatthias Ringwald if (connection_index == MAX_NR_CONNECTIONS){ 140bdc352b1SMatthias Ringwald connection_index = 0; 141bdc352b1SMatthias Ringwald } 142bdc352b1SMatthias Ringwald } 143bdc352b1SMatthias Ringwald 144bdc352b1SMatthias Ringwald /* @section Main Application Setup 145bdc352b1SMatthias Ringwald * 146bdc352b1SMatthias Ringwald * @text Listing MainConfiguration shows main application code. 147bdc352b1SMatthias Ringwald * It initializes L2CAP, the Security Manager, and configures the ATT Server with the pre-compiled 148bdc352b1SMatthias Ringwald * ATT Database generated from $le_streamer.gatt$. Finally, it configures the advertisements 149bdc352b1SMatthias Ringwald * and boots the Bluetooth stack. 150bdc352b1SMatthias Ringwald */ 151bdc352b1SMatthias Ringwald 152bdc352b1SMatthias Ringwald /* LISTING_START(MainConfiguration): Init L2CAP, SM, ATT Server, and enable advertisements */ 153bdc352b1SMatthias Ringwald 154bdc352b1SMatthias Ringwald static void le_streamer_setup(void){ 155bdc352b1SMatthias Ringwald 156bdc352b1SMatthias Ringwald l2cap_init(); 157bdc352b1SMatthias Ringwald 158bdc352b1SMatthias Ringwald // setup SM: Display only 159bdc352b1SMatthias Ringwald sm_init(); 160bdc352b1SMatthias Ringwald 161bdc352b1SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 162bdc352b1SMatthias Ringwald // init SDP, create record for GATT and register with SDP 163bdc352b1SMatthias Ringwald sdp_init(); 164bdc352b1SMatthias Ringwald memset(gatt_service_buffer, 0, sizeof(gatt_service_buffer)); 165bdc352b1SMatthias Ringwald gatt_create_sdp_record(gatt_service_buffer, 0x10001, ATT_SERVICE_GATT_SERVICE_START_HANDLE, ATT_SERVICE_GATT_SERVICE_END_HANDLE); 166bdc352b1SMatthias Ringwald sdp_register_service(gatt_service_buffer); 167bdc352b1SMatthias Ringwald printf("SDP service record size: %u\n", de_get_len(gatt_service_buffer)); 168bdc352b1SMatthias Ringwald 169bdc352b1SMatthias Ringwald // configure Classic GAP 170bdc352b1SMatthias Ringwald gap_set_local_name("GATT Streamer BR/EDR 00:00:00:00:00:00"); 171bdc352b1SMatthias Ringwald gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 172bdc352b1SMatthias Ringwald gap_discoverable_control(1); 173bdc352b1SMatthias Ringwald #endif 174bdc352b1SMatthias Ringwald 175bdc352b1SMatthias Ringwald // setup ATT server 176bdc352b1SMatthias Ringwald att_server_init(profile_data, NULL, att_write_callback); 177bdc352b1SMatthias Ringwald 178bdc352b1SMatthias Ringwald // register for HCI events 179bdc352b1SMatthias Ringwald hci_event_callback_registration.callback = &hci_packet_handler; 180bdc352b1SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 181bdc352b1SMatthias Ringwald 182bdc352b1SMatthias Ringwald // register for ATT events 183bdc352b1SMatthias Ringwald att_server_register_packet_handler(att_packet_handler); 184bdc352b1SMatthias Ringwald 185bdc352b1SMatthias Ringwald // setup advertisements 186bdc352b1SMatthias Ringwald uint16_t adv_int_min = 0x0030; 187bdc352b1SMatthias Ringwald uint16_t adv_int_max = 0x0030; 188bdc352b1SMatthias Ringwald uint8_t adv_type = 0; 189bdc352b1SMatthias Ringwald bd_addr_t null_addr; 190bdc352b1SMatthias Ringwald memset(null_addr, 0, 6); 191bdc352b1SMatthias Ringwald gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 192bdc352b1SMatthias Ringwald gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); 193bdc352b1SMatthias Ringwald gap_advertisements_enable(1); 194bdc352b1SMatthias Ringwald 195bdc352b1SMatthias Ringwald // init client state 196bdc352b1SMatthias Ringwald init_connections(); 197bdc352b1SMatthias Ringwald } 198bdc352b1SMatthias Ringwald /* LISTING_END */ 199bdc352b1SMatthias Ringwald 200bdc352b1SMatthias Ringwald /* 201bdc352b1SMatthias Ringwald * @section Track throughput 202bdc352b1SMatthias Ringwald * @text We calculate the throughput by setting a start time and measuring the amount of 203bdc352b1SMatthias Ringwald * data sent. After a configurable REPORT_INTERVAL_MS, we print the throughput in kB/s 204bdc352b1SMatthias Ringwald * and reset the counter and start time. 205bdc352b1SMatthias Ringwald */ 206bdc352b1SMatthias Ringwald 207bdc352b1SMatthias Ringwald /* LISTING_START(tracking): Tracking throughput */ 208bdc352b1SMatthias Ringwald 209bdc352b1SMatthias Ringwald static void test_reset(le_streamer_connection_t * context){ 210bdc352b1SMatthias Ringwald context->test_data_start = btstack_run_loop_get_time_ms(); 211bdc352b1SMatthias Ringwald context->test_data_sent = 0; 212bdc352b1SMatthias Ringwald } 213bdc352b1SMatthias Ringwald 214bdc352b1SMatthias Ringwald static void test_track_sent(le_streamer_connection_t * context, int bytes_sent){ 215bdc352b1SMatthias Ringwald context->test_data_sent += bytes_sent; 216bdc352b1SMatthias Ringwald // evaluate 217bdc352b1SMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 218bdc352b1SMatthias Ringwald uint32_t time_passed = now - context->test_data_start; 219bdc352b1SMatthias Ringwald if (time_passed < REPORT_INTERVAL_MS) return; 220bdc352b1SMatthias Ringwald // print speed 221bdc352b1SMatthias Ringwald int bytes_per_second = context->test_data_sent * 1000 / time_passed; 222bdc352b1SMatthias Ringwald printf("%c: %"PRIu32" bytes sent-> %u.%03u kB/s\n", context->name, context->test_data_sent, bytes_per_second / 1000, bytes_per_second % 1000); 223bdc352b1SMatthias Ringwald 224bdc352b1SMatthias Ringwald // restart 225bdc352b1SMatthias Ringwald context->test_data_start = now; 226bdc352b1SMatthias Ringwald context->test_data_sent = 0; 227bdc352b1SMatthias Ringwald } 228bdc352b1SMatthias Ringwald /* LISTING_END(tracking): Tracking throughput */ 229bdc352b1SMatthias Ringwald 230bdc352b1SMatthias Ringwald /* 231bdc352b1SMatthias Ringwald * @section HCI Packet Handler 232bdc352b1SMatthias Ringwald * 233bdc352b1SMatthias Ringwald * @text The packet handler is used track incoming connections and to stop notifications on disconnect 234bdc352b1SMatthias Ringwald * It is also a good place to request the connection parameter update as indicated 235bdc352b1SMatthias Ringwald * in the commented code block. 236bdc352b1SMatthias Ringwald */ 237bdc352b1SMatthias Ringwald 238bdc352b1SMatthias Ringwald /* LISTING_START(hciPacketHandler): Packet Handler */ 239bdc352b1SMatthias Ringwald static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 240bdc352b1SMatthias Ringwald UNUSED(channel); 241bdc352b1SMatthias Ringwald UNUSED(size); 242bdc352b1SMatthias Ringwald 243bdc352b1SMatthias Ringwald uint16_t conn_interval; 244bdc352b1SMatthias Ringwald hci_con_handle_t con_handle; 2457bbeb3adSMilanka Ringwald 2467bbeb3adSMilanka Ringwald if (packet_type != HCI_EVENT_PACKET) return; 2477bbeb3adSMilanka Ringwald 248bdc352b1SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 249bdc352b1SMatthias Ringwald case BTSTACK_EVENT_STATE: 250bdc352b1SMatthias Ringwald // BTstack activated, get started 251bdc352b1SMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) { 252bdc352b1SMatthias Ringwald printf("To start the streaming, please run the le_streamer_client example on other device, or use some GATT Explorer, e.g. LightBlue, BLExplr.\n"); 253bdc352b1SMatthias Ringwald } 254bdc352b1SMatthias Ringwald break; 255bdc352b1SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 256bdc352b1SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 257*fcd55a0bSMilanka Ringwald printf("- LE Connection 0x%04x: disconnect, reason %02x\n", con_handle, hci_event_disconnection_complete_get_reason(packet)); 258bdc352b1SMatthias Ringwald break; 259bdc352b1SMatthias Ringwald case HCI_EVENT_LE_META: 260bdc352b1SMatthias Ringwald switch (hci_event_le_meta_get_subevent_code(packet)) { 261bdc352b1SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 262bdc352b1SMatthias Ringwald // print connection parameters (without using float operations) 263bdc352b1SMatthias Ringwald con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 264bdc352b1SMatthias Ringwald conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 265*fcd55a0bSMilanka Ringwald printf("- LE Connection 0x%04x: connected - connection interval %u.%02u ms, latency %u\n", con_handle, conn_interval * 125 / 100, 266bdc352b1SMatthias Ringwald 25 * (conn_interval & 3), hci_subevent_le_connection_complete_get_conn_latency(packet)); 267bdc352b1SMatthias Ringwald 268bdc352b1SMatthias Ringwald // request min con interval 15 ms for iOS 11+ 269*fcd55a0bSMilanka Ringwald printf("- LE Connection 0x%04x: request 15 ms connection interval\n", con_handle); 270bdc352b1SMatthias Ringwald gap_request_connection_parameter_update(con_handle, 12, 12, 0, 0x0048); 271bdc352b1SMatthias Ringwald break; 272bdc352b1SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 273bdc352b1SMatthias Ringwald // print connection parameters (without using float operations) 274bdc352b1SMatthias Ringwald con_handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 275bdc352b1SMatthias Ringwald conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 276*fcd55a0bSMilanka Ringwald printf("- LE Connection 0x%04x: connection update - connection interval %u.%02u ms, latency %u\n", con_handle, conn_interval * 125 / 100, 277bdc352b1SMatthias Ringwald 25 * (conn_interval & 3), hci_subevent_le_connection_update_complete_get_conn_latency(packet)); 278bdc352b1SMatthias Ringwald break; 279bdc352b1SMatthias Ringwald default: 280bdc352b1SMatthias Ringwald break; 281bdc352b1SMatthias Ringwald } 282bdc352b1SMatthias Ringwald break; 2837bbeb3adSMilanka Ringwald 284bdc352b1SMatthias Ringwald default: 285bdc352b1SMatthias Ringwald break; 286bdc352b1SMatthias Ringwald } 287bdc352b1SMatthias Ringwald } 288bdc352b1SMatthias Ringwald 289bdc352b1SMatthias Ringwald /* LISTING_END */ 290bdc352b1SMatthias Ringwald 291bdc352b1SMatthias Ringwald /* 292bdc352b1SMatthias Ringwald * @section ATT Packet Handler 293bdc352b1SMatthias Ringwald * 294bdc352b1SMatthias Ringwald * @text The packet handler is used to track the ATT MTU Exchange and trigger ATT send 295bdc352b1SMatthias Ringwald */ 296bdc352b1SMatthias Ringwald 297bdc352b1SMatthias Ringwald /* LISTING_START(attPacketHandler): Packet Handler */ 298bdc352b1SMatthias Ringwald static void att_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 299bdc352b1SMatthias Ringwald UNUSED(channel); 300bdc352b1SMatthias Ringwald UNUSED(size); 301bdc352b1SMatthias Ringwald 302bdc352b1SMatthias Ringwald int mtu; 303bdc352b1SMatthias Ringwald le_streamer_connection_t * context; 304bdc352b1SMatthias Ringwald switch (packet_type) { 305bdc352b1SMatthias Ringwald case HCI_EVENT_PACKET: 306bdc352b1SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 307bdc352b1SMatthias Ringwald case ATT_EVENT_CONNECTED: 308bdc352b1SMatthias Ringwald // setup new 309bdc352b1SMatthias Ringwald context = connection_for_conn_handle(HCI_CON_HANDLE_INVALID); 310bdc352b1SMatthias Ringwald if (!context) break; 311bdc352b1SMatthias Ringwald context->counter = 'A'; 312bdc352b1SMatthias Ringwald context->connection_handle = att_event_connected_get_handle(packet); 313bdc352b1SMatthias Ringwald context->test_data_len = btstack_min(att_server_get_mtu(context->connection_handle) - 3, sizeof(context->test_data)); 314*fcd55a0bSMilanka Ringwald printf("%c: ATT connected, handle 0x%04x, test data len %u\n", context->name, context->connection_handle, context->test_data_len); 315bdc352b1SMatthias Ringwald break; 316bdc352b1SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 317bdc352b1SMatthias Ringwald mtu = att_event_mtu_exchange_complete_get_MTU(packet) - 3; 318bdc352b1SMatthias Ringwald context = connection_for_conn_handle(att_event_mtu_exchange_complete_get_handle(packet)); 319bdc352b1SMatthias Ringwald if (!context) break; 320bdc352b1SMatthias Ringwald context->test_data_len = btstack_min(mtu - 3, sizeof(context->test_data)); 321bdc352b1SMatthias Ringwald printf("%c: ATT MTU = %u => use test data of len %u\n", context->name, mtu, context->test_data_len); 322bdc352b1SMatthias Ringwald break; 323bdc352b1SMatthias Ringwald case ATT_EVENT_CAN_SEND_NOW: 324bdc352b1SMatthias Ringwald streamer(); 325bdc352b1SMatthias Ringwald break; 326bdc352b1SMatthias Ringwald case ATT_EVENT_DISCONNECTED: 327bdc352b1SMatthias Ringwald context = connection_for_conn_handle(att_event_disconnected_get_handle(packet)); 328bdc352b1SMatthias Ringwald if (!context) break; 329bdc352b1SMatthias Ringwald // free connection 330*fcd55a0bSMilanka Ringwald printf("%c: ATT disconnected, handle 0x%04x\n", context->name, context->connection_handle); 331bdc352b1SMatthias Ringwald context->le_notification_enabled = 0; 332bdc352b1SMatthias Ringwald context->connection_handle = HCI_CON_HANDLE_INVALID; 333bdc352b1SMatthias Ringwald break; 334bdc352b1SMatthias Ringwald default: 335bdc352b1SMatthias Ringwald break; 336bdc352b1SMatthias Ringwald } 337bdc352b1SMatthias Ringwald break; 338bdc352b1SMatthias Ringwald default: 339bdc352b1SMatthias Ringwald break; 340bdc352b1SMatthias Ringwald } 341bdc352b1SMatthias Ringwald } 342bdc352b1SMatthias Ringwald /* LISTING_END */ 343bdc352b1SMatthias Ringwald 344bdc352b1SMatthias Ringwald /* 345bdc352b1SMatthias Ringwald * @section Streamer 346bdc352b1SMatthias Ringwald * 347bdc352b1SMatthias Ringwald * @text The streamer function checks if notifications are enabled and if a notification can be sent now. 348bdc352b1SMatthias Ringwald * It creates some test data - a single letter that gets increased every time - and tracks the data sent. 349bdc352b1SMatthias Ringwald */ 350bdc352b1SMatthias Ringwald 351bdc352b1SMatthias Ringwald /* LISTING_START(streamer): Streaming code */ 352bdc352b1SMatthias Ringwald static void streamer(void){ 353bdc352b1SMatthias Ringwald 354bdc352b1SMatthias Ringwald // find next active streaming connection 355bdc352b1SMatthias Ringwald int old_connection_index = connection_index; 356bdc352b1SMatthias Ringwald while (1){ 357bdc352b1SMatthias Ringwald // active found? 358bdc352b1SMatthias Ringwald if ((le_streamer_connections[connection_index].connection_handle != HCI_CON_HANDLE_INVALID) && 359bdc352b1SMatthias Ringwald (le_streamer_connections[connection_index].le_notification_enabled)) break; 360bdc352b1SMatthias Ringwald 361bdc352b1SMatthias Ringwald // check next 362bdc352b1SMatthias Ringwald next_connection_index(); 363bdc352b1SMatthias Ringwald 364bdc352b1SMatthias Ringwald // none found 365bdc352b1SMatthias Ringwald if (connection_index == old_connection_index) return; 366bdc352b1SMatthias Ringwald } 367bdc352b1SMatthias Ringwald 368bdc352b1SMatthias Ringwald le_streamer_connection_t * context = &le_streamer_connections[connection_index]; 369bdc352b1SMatthias Ringwald 370bdc352b1SMatthias Ringwald // create test data 371bdc352b1SMatthias Ringwald context->counter++; 372bdc352b1SMatthias Ringwald if (context->counter > 'Z') context->counter = 'A'; 373bdc352b1SMatthias Ringwald memset(context->test_data, context->counter, context->test_data_len); 374bdc352b1SMatthias Ringwald 375bdc352b1SMatthias Ringwald // send 376bdc352b1SMatthias Ringwald att_server_notify(context->connection_handle, context->value_handle, (uint8_t*) context->test_data, context->test_data_len); 377bdc352b1SMatthias Ringwald 378bdc352b1SMatthias Ringwald // track 379bdc352b1SMatthias Ringwald test_track_sent(context, context->test_data_len); 380bdc352b1SMatthias Ringwald 381bdc352b1SMatthias Ringwald // request next send event 382bdc352b1SMatthias Ringwald att_server_request_can_send_now_event(context->connection_handle); 383bdc352b1SMatthias Ringwald 384bdc352b1SMatthias Ringwald // check next 385bdc352b1SMatthias Ringwald next_connection_index(); 386bdc352b1SMatthias Ringwald } 387bdc352b1SMatthias Ringwald /* LISTING_END */ 388bdc352b1SMatthias Ringwald 389bdc352b1SMatthias Ringwald /* 390bdc352b1SMatthias Ringwald * @section ATT Write 391bdc352b1SMatthias Ringwald * 392bdc352b1SMatthias Ringwald * @text The only valid ATT write in this example is to the Client Characteristic Configuration, which configures notification 393bdc352b1SMatthias Ringwald * and indication. If the ATT handle matches the client configuration handle, the new configuration value is stored. 394bdc352b1SMatthias Ringwald * If notifications get enabled, an ATT_EVENT_CAN_SEND_NOW is requested. See Listing attWrite. 395bdc352b1SMatthias Ringwald */ 396bdc352b1SMatthias Ringwald 397bdc352b1SMatthias Ringwald /* LISTING_START(attWrite): ATT Write */ 398bdc352b1SMatthias 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){ 399bdc352b1SMatthias Ringwald UNUSED(offset); 400bdc352b1SMatthias Ringwald 401*fcd55a0bSMilanka Ringwald // printf("att_write_callback att_handle 0x%04x, transaction mode %u\n", att_handle, transaction_mode); 402bdc352b1SMatthias Ringwald if (transaction_mode != ATT_TRANSACTION_MODE_NONE) return 0; 403bdc352b1SMatthias Ringwald le_streamer_connection_t * context = connection_for_conn_handle(con_handle); 404bdc352b1SMatthias Ringwald switch(att_handle){ 405bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE: 406bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE: 407bdc352b1SMatthias Ringwald context->le_notification_enabled = little_endian_read_16(buffer, 0) == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION; 408bdc352b1SMatthias Ringwald printf("%c: Notifications enabled %u\n", context->name, context->le_notification_enabled); 409bdc352b1SMatthias Ringwald if (context->le_notification_enabled){ 410bdc352b1SMatthias Ringwald switch (att_handle){ 411bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE: 412bdc352b1SMatthias Ringwald context->value_handle = ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE; 413bdc352b1SMatthias Ringwald break; 414bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_CLIENT_CONFIGURATION_HANDLE: 415bdc352b1SMatthias Ringwald context->value_handle = ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE; 416bdc352b1SMatthias Ringwald break; 417bdc352b1SMatthias Ringwald default: 418bdc352b1SMatthias Ringwald break; 419bdc352b1SMatthias Ringwald } 420bdc352b1SMatthias Ringwald att_server_request_can_send_now_event(context->connection_handle); 421bdc352b1SMatthias Ringwald } 422bdc352b1SMatthias Ringwald test_reset(context); 423bdc352b1SMatthias Ringwald break; 424bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF11_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE: 425bdc352b1SMatthias Ringwald case ATT_CHARACTERISTIC_0000FF12_0000_1000_8000_00805F9B34FB_01_VALUE_HANDLE: 426bdc352b1SMatthias Ringwald test_track_sent(context, buffer_size); 427bdc352b1SMatthias Ringwald break; 428bdc352b1SMatthias Ringwald default: 429bdc352b1SMatthias Ringwald printf("Write to 0x%04x, len %u\n", att_handle, buffer_size); 4306058cb0dSMatthias Ringwald break; 431bdc352b1SMatthias Ringwald } 432bdc352b1SMatthias Ringwald return 0; 433bdc352b1SMatthias Ringwald } 434bdc352b1SMatthias Ringwald /* LISTING_END */ 435bdc352b1SMatthias Ringwald 436bdc352b1SMatthias Ringwald int btstack_main(void); 437bdc352b1SMatthias Ringwald int btstack_main(void) 438bdc352b1SMatthias Ringwald { 439bdc352b1SMatthias Ringwald le_streamer_setup(); 440bdc352b1SMatthias Ringwald 441bdc352b1SMatthias Ringwald // turn on! 442bdc352b1SMatthias Ringwald hci_power_control(HCI_POWER_ON); 443bdc352b1SMatthias Ringwald 444bdc352b1SMatthias Ringwald return 0; 445bdc352b1SMatthias Ringwald } 446bdc352b1SMatthias Ringwald /* EXAMPLE_END */ 447