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