1c8f97be6SMatthias Ringwald /* 2c8f97be6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3c8f97be6SMatthias Ringwald * 4c8f97be6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5c8f97be6SMatthias Ringwald * modification, are permitted provided that the following conditions 6c8f97be6SMatthias Ringwald * are met: 7c8f97be6SMatthias Ringwald * 8c8f97be6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9c8f97be6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10c8f97be6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11c8f97be6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12c8f97be6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13c8f97be6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14c8f97be6SMatthias Ringwald * contributors may be used to endorse or promote products derived 15c8f97be6SMatthias Ringwald * from this software without specific prior written permission. 16c8f97be6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17c8f97be6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18c8f97be6SMatthias Ringwald * monetary gain. 19c8f97be6SMatthias Ringwald * 20c8f97be6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21c8f97be6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22c8f97be6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23c8f97be6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24c8f97be6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25c8f97be6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26c8f97be6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27c8f97be6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28c8f97be6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29c8f97be6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30c8f97be6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31c8f97be6SMatthias Ringwald * SUCH DAMAGE. 32c8f97be6SMatthias Ringwald * 33c8f97be6SMatthias Ringwald * Please inquire about commercial licensing options at 34c8f97be6SMatthias Ringwald * [email protected] 35c8f97be6SMatthias Ringwald * 36c8f97be6SMatthias Ringwald */ 37c8f97be6SMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "le_streamer_client.c" 39ab2c6ae4SMatthias Ringwald 40c8f97be6SMatthias Ringwald // ***************************************************************************** 41c8f97be6SMatthias Ringwald // 42c8f97be6SMatthias Ringwald // LE Streamer Client - connects to 'LE Streamer' and subscribes to test characteristic 43c8f97be6SMatthias Ringwald // 44c8f97be6SMatthias Ringwald // ***************************************************************************** 45c8f97be6SMatthias Ringwald 46c8f97be6SMatthias Ringwald #include <stdint.h> 47c8f97be6SMatthias Ringwald #include <stdio.h> 48c8f97be6SMatthias Ringwald #include <stdlib.h> 49c8f97be6SMatthias Ringwald #include <string.h> 50c8f97be6SMatthias Ringwald 51c8f97be6SMatthias Ringwald #include "btstack.h" 52c8f97be6SMatthias Ringwald 53c8f97be6SMatthias Ringwald typedef enum { 54c8f97be6SMatthias Ringwald TC_IDLE, 55c8f97be6SMatthias Ringwald TC_W4_SCAN_RESULT, 56c8f97be6SMatthias Ringwald TC_W4_CONNECT, 57c8f97be6SMatthias Ringwald TC_W4_SERVICE_RESULT, 58c8f97be6SMatthias Ringwald TC_W4_CHARACTERISTIC_RESULT, 59c8f97be6SMatthias Ringwald TC_W4_TEST_DATA 60c8f97be6SMatthias Ringwald } gc_state_t; 61c8f97be6SMatthias Ringwald 62c8f97be6SMatthias Ringwald static bd_addr_t cmdline_addr = { }; 63c8f97be6SMatthias Ringwald static int cmdline_addr_found = 0; 64c8f97be6SMatthias Ringwald 65c8f97be6SMatthias Ringwald // addr and type of device with correct name 66c8f97be6SMatthias Ringwald static bd_addr_t le_streamer_addr; 67c8f97be6SMatthias Ringwald static bd_addr_type_t le_streamer_addr_type; 68c8f97be6SMatthias Ringwald 69c8f97be6SMatthias Ringwald static hci_con_handle_t connection_handle; 70c8f97be6SMatthias Ringwald static uint8_t le_streamer_service_uuid[16] = { 0x00, 0x00, 0xFF, 0x10, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; 71c8f97be6SMatthias Ringwald static uint8_t le_streamer_characteristic_uuid[16] = { 0x00, 0x00, 0xFF, 0x11, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; 72c8f97be6SMatthias Ringwald 73c8f97be6SMatthias Ringwald static gatt_client_service_t le_streamer_service; 74c8f97be6SMatthias Ringwald static gatt_client_characteristic_t le_streamer_characteristic; 75c8f97be6SMatthias Ringwald 76*44a1ebc0SMatthias Ringwald static gatt_client_notification_t notification_listener; 77*44a1ebc0SMatthias Ringwald static int listener_registered; 78c8f97be6SMatthias Ringwald 79c8f97be6SMatthias Ringwald static gc_state_t state = TC_IDLE; 80c8f97be6SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 81c8f97be6SMatthias Ringwald 82c8f97be6SMatthias Ringwald // returns 1 if name is found in advertisement 83c8f97be6SMatthias Ringwald static int advertisement_report_contains_name(const char * name, uint8_t * advertisement_report){ 84c8f97be6SMatthias Ringwald // get advertisement from report event 85c8f97be6SMatthias Ringwald const uint8_t * adv_data = gap_event_advertising_report_get_data(advertisement_report); 86c8f97be6SMatthias Ringwald uint16_t adv_len = gap_event_advertising_report_get_data_length(advertisement_report); 87c8f97be6SMatthias Ringwald int name_len = strlen(name); 88c8f97be6SMatthias Ringwald 89c8f97be6SMatthias Ringwald // iterate over advertisement data 90c8f97be6SMatthias Ringwald ad_context_t context; 91c8f97be6SMatthias Ringwald for (ad_iterator_init(&context, adv_len, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 92c8f97be6SMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 93c8f97be6SMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 94c8f97be6SMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 95c8f97be6SMatthias Ringwald int i; 96c8f97be6SMatthias Ringwald switch (data_type){ 97c8f97be6SMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 98c8f97be6SMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 99c8f97be6SMatthias Ringwald // compare common prefix 100c8f97be6SMatthias Ringwald for (i=0; i<data_size && i<name_len;i++){ 101c8f97be6SMatthias Ringwald if (data[i] != name[i]) break; 102c8f97be6SMatthias Ringwald } 103c8f97be6SMatthias Ringwald // prefix match 104c8f97be6SMatthias Ringwald return 1; 105c8f97be6SMatthias Ringwald default: 106c8f97be6SMatthias Ringwald break; 107c8f97be6SMatthias Ringwald } 108c8f97be6SMatthias Ringwald } 109c8f97be6SMatthias Ringwald return 0; 110c8f97be6SMatthias Ringwald } 111c8f97be6SMatthias Ringwald 112c8f97be6SMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 113c8f97be6SMatthias Ringwald UNUSED(packet_type); 114c8f97be6SMatthias Ringwald UNUSED(channel); 115c8f97be6SMatthias Ringwald UNUSED(size); 116c8f97be6SMatthias Ringwald 117c8f97be6SMatthias Ringwald switch(state){ 118c8f97be6SMatthias Ringwald case TC_W4_SERVICE_RESULT: 119c8f97be6SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 120c8f97be6SMatthias Ringwald case GATT_EVENT_SERVICE_QUERY_RESULT: 121c8f97be6SMatthias Ringwald // store service (we expect only one) 122c8f97be6SMatthias Ringwald gatt_event_service_query_result_get_service(packet, &le_streamer_service); 123c8f97be6SMatthias Ringwald break; 124c8f97be6SMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 125c8f97be6SMatthias Ringwald if (packet[4] != 0){ 126c8f97be6SMatthias Ringwald printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]); 127c8f97be6SMatthias Ringwald gap_disconnect(connection_handle); 128c8f97be6SMatthias Ringwald break; 129c8f97be6SMatthias Ringwald } 130c8f97be6SMatthias Ringwald // service query complete, look for characteristic 131c8f97be6SMatthias Ringwald state = TC_W4_CHARACTERISTIC_RESULT; 132c8f97be6SMatthias Ringwald printf("Search for LE Streamer test characteristic.\n"); 133c8f97be6SMatthias Ringwald gatt_client_discover_characteristics_for_service_by_uuid128(handle_gatt_client_event, connection_handle, &le_streamer_service, le_streamer_characteristic_uuid); 134c8f97be6SMatthias Ringwald break; 135c8f97be6SMatthias Ringwald default: 136c8f97be6SMatthias Ringwald break; 137c8f97be6SMatthias Ringwald } 138c8f97be6SMatthias Ringwald break; 139c8f97be6SMatthias Ringwald 140c8f97be6SMatthias Ringwald case TC_W4_CHARACTERISTIC_RESULT: 141c8f97be6SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 142c8f97be6SMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 143c8f97be6SMatthias Ringwald gatt_event_characteristic_query_result_get_characteristic(packet, &le_streamer_characteristic); 144c8f97be6SMatthias Ringwald break; 145c8f97be6SMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 146c8f97be6SMatthias Ringwald if (packet[4] != 0){ 147c8f97be6SMatthias Ringwald printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]); 148c8f97be6SMatthias Ringwald gap_disconnect(connection_handle); 149c8f97be6SMatthias Ringwald break; 150c8f97be6SMatthias Ringwald } 151c8f97be6SMatthias Ringwald // register handler for notifications 152*44a1ebc0SMatthias Ringwald listener_registered = 1; 153*44a1ebc0SMatthias Ringwald gatt_client_listen_for_characteristic_value_updates(¬ification_listener, handle_gatt_client_event, connection_handle, &le_streamer_characteristic); 154c8f97be6SMatthias Ringwald // enable notifications 155c8f97be6SMatthias Ringwald state = TC_W4_TEST_DATA; 156c8f97be6SMatthias Ringwald printf("Start streaming - enable notify on test characteristic.\n"); 157c8f97be6SMatthias Ringwald gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &le_streamer_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 158c8f97be6SMatthias Ringwald break; 159c8f97be6SMatthias Ringwald default: 160c8f97be6SMatthias Ringwald break; 161c8f97be6SMatthias Ringwald } 162c8f97be6SMatthias Ringwald break; 163c8f97be6SMatthias Ringwald 164c8f97be6SMatthias Ringwald case TC_W4_TEST_DATA: 165c8f97be6SMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 166c8f97be6SMatthias Ringwald case GATT_EVENT_NOTIFICATION: 167c8f97be6SMatthias Ringwald printf("Data: "); 168c8f97be6SMatthias Ringwald printf_hexdump( gatt_event_notification_get_value(packet), gatt_event_notification_get_value_length(packet)); 169c8f97be6SMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 170c8f97be6SMatthias Ringwald break; 171c8f97be6SMatthias Ringwald default: 172c8f97be6SMatthias Ringwald printf("Unknown packet type %x\n", hci_event_packet_get_type(packet)); 173c8f97be6SMatthias Ringwald break; 174c8f97be6SMatthias Ringwald } 175c8f97be6SMatthias Ringwald break; 176c8f97be6SMatthias Ringwald 177c8f97be6SMatthias Ringwald default: 178c8f97be6SMatthias Ringwald printf("error\n"); 179c8f97be6SMatthias Ringwald break; 180c8f97be6SMatthias Ringwald } 181c8f97be6SMatthias Ringwald 182c8f97be6SMatthias Ringwald } 183c8f97be6SMatthias Ringwald 184*44a1ebc0SMatthias Ringwald // Either connect to remote specified on command line or start scan for device with "LE Streamer" in advertisement 185*44a1ebc0SMatthias Ringwald static void le_streamer_client_start(void){ 186*44a1ebc0SMatthias Ringwald if (cmdline_addr_found){ 187*44a1ebc0SMatthias Ringwald printf("Connect to %s\n", bd_addr_to_str(cmdline_addr)); 188*44a1ebc0SMatthias Ringwald state = TC_W4_CONNECT; 189*44a1ebc0SMatthias Ringwald gap_connect(cmdline_addr, 0); 190*44a1ebc0SMatthias Ringwald } else { 191*44a1ebc0SMatthias Ringwald printf("Start scanning!\n"); 192*44a1ebc0SMatthias Ringwald state = TC_W4_SCAN_RESULT; 193*44a1ebc0SMatthias Ringwald gap_set_scan_parameters(0,0x0030, 0x0030); 194*44a1ebc0SMatthias Ringwald gap_start_scan(); 195*44a1ebc0SMatthias Ringwald } 196*44a1ebc0SMatthias Ringwald } 197*44a1ebc0SMatthias Ringwald 198c8f97be6SMatthias Ringwald static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 199c8f97be6SMatthias Ringwald UNUSED(channel); 200c8f97be6SMatthias Ringwald UNUSED(size); 201c8f97be6SMatthias Ringwald 202c8f97be6SMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 203c8f97be6SMatthias Ringwald 2048e46c847SMatthias Ringwald uint16_t conn_interval; 205c8f97be6SMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet); 206c8f97be6SMatthias Ringwald switch (event) { 207c8f97be6SMatthias Ringwald case BTSTACK_EVENT_STATE: 208c8f97be6SMatthias Ringwald // BTstack activated, get started 209c8f97be6SMatthias Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 210*44a1ebc0SMatthias Ringwald le_streamer_client_start(); 211c8f97be6SMatthias Ringwald break; 212c8f97be6SMatthias Ringwald case GAP_EVENT_ADVERTISING_REPORT: 213c8f97be6SMatthias Ringwald if (state != TC_W4_SCAN_RESULT) return; 214c8f97be6SMatthias Ringwald // check name in advertisement 215c8f97be6SMatthias Ringwald if (!advertisement_report_contains_name("LE Streamer", packet)) return; 216c8f97be6SMatthias Ringwald // store address and type 217c8f97be6SMatthias Ringwald gap_event_advertising_report_get_address(packet, le_streamer_addr); 218c8f97be6SMatthias Ringwald le_streamer_addr_type = gap_event_advertising_report_get_address_type(packet); 219c8f97be6SMatthias Ringwald // stop scanning, and connect to the device 220c8f97be6SMatthias Ringwald state = TC_W4_CONNECT; 221c8f97be6SMatthias Ringwald gap_stop_scan(); 222c8f97be6SMatthias Ringwald printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(le_streamer_addr)); 223c8f97be6SMatthias Ringwald gap_connect(le_streamer_addr,le_streamer_addr_type); 224c8f97be6SMatthias Ringwald break; 225c8f97be6SMatthias Ringwald case HCI_EVENT_LE_META: 226c8f97be6SMatthias Ringwald // wait for connection complete 227c8f97be6SMatthias Ringwald if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 228c8f97be6SMatthias Ringwald if (state != TC_W4_CONNECT) return; 229c8f97be6SMatthias Ringwald connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 2308e46c847SMatthias Ringwald // print connection parameters (without using float operations) 2318e46c847SMatthias Ringwald conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 2328e46c847SMatthias Ringwald printf("Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3)); 2339163ed0fSMatthias Ringwald printf("Connection Latency: %u\n", hci_subevent_le_connection_complete_get_conn_latency(packet)); 234c8f97be6SMatthias Ringwald // initialize gatt client context with handle, and add it to the list of active clients 235c8f97be6SMatthias Ringwald // query primary services 236c8f97be6SMatthias Ringwald printf("Search for LE Streamer service.\n"); 237c8f97be6SMatthias Ringwald state = TC_W4_SERVICE_RESULT; 238c8f97be6SMatthias Ringwald gatt_client_discover_primary_services_by_uuid128(handle_gatt_client_event, connection_handle, le_streamer_service_uuid); 239c8f97be6SMatthias Ringwald break; 240c8f97be6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 241*44a1ebc0SMatthias Ringwald // unregister listener 242*44a1ebc0SMatthias Ringwald if (listener_registered){ 243*44a1ebc0SMatthias Ringwald listener_registered = 0; 244*44a1ebc0SMatthias Ringwald gatt_client_stop_listening_for_characteristic_value_updates(¬ification_listener); 245*44a1ebc0SMatthias Ringwald } 246c8f97be6SMatthias Ringwald if (cmdline_addr_found){ 247c8f97be6SMatthias Ringwald printf("Disconnected %s\n", bd_addr_to_str(cmdline_addr)); 248c8f97be6SMatthias Ringwald return; 249c8f97be6SMatthias Ringwald } 250c8f97be6SMatthias Ringwald printf("Disconnected %s\n", bd_addr_to_str(le_streamer_addr)); 251*44a1ebc0SMatthias Ringwald le_streamer_client_start(); 252c8f97be6SMatthias Ringwald break; 253c8f97be6SMatthias Ringwald default: 254c8f97be6SMatthias Ringwald break; 255c8f97be6SMatthias Ringwald } 256c8f97be6SMatthias Ringwald } 257c8f97be6SMatthias Ringwald 2587ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 259c8f97be6SMatthias Ringwald static void usage(const char *name){ 260c8f97be6SMatthias Ringwald fprintf(stderr, "Usage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name); 261c8f97be6SMatthias Ringwald fprintf(stderr, "If no argument is provided, LE Streamer Client will start scanning and connect to the first device named 'LE Streamer'.\n"); 262c8f97be6SMatthias Ringwald fprintf(stderr, "To connect to a specific device use argument [-a].\n\n"); 263c8f97be6SMatthias Ringwald } 264c8f97be6SMatthias Ringwald #endif 265c8f97be6SMatthias Ringwald 266c8f97be6SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 267c8f97be6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 268c8f97be6SMatthias Ringwald 2697ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 270c8f97be6SMatthias Ringwald int arg = 1; 271c8f97be6SMatthias Ringwald cmdline_addr_found = 0; 272c8f97be6SMatthias Ringwald 273c8f97be6SMatthias Ringwald while (arg < argc) { 274c8f97be6SMatthias Ringwald if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ 275c8f97be6SMatthias Ringwald arg++; 276c8f97be6SMatthias Ringwald cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr); 277c8f97be6SMatthias Ringwald arg++; 278c8f97be6SMatthias Ringwald if (!cmdline_addr_found) exit(1); 279c8f97be6SMatthias Ringwald continue; 280c8f97be6SMatthias Ringwald } 281c8f97be6SMatthias Ringwald usage(argv[0]); 282c8f97be6SMatthias Ringwald return 0; 283c8f97be6SMatthias Ringwald } 284c8f97be6SMatthias Ringwald #else 2856316c8edSMatthias Ringwald (void)argc; 2866316c8edSMatthias Ringwald (void)argv; 287c8f97be6SMatthias Ringwald #endif 288c8f97be6SMatthias Ringwald 289c8f97be6SMatthias Ringwald hci_event_callback_registration.callback = &hci_event_handler; 290c8f97be6SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 291c8f97be6SMatthias Ringwald 292c8f97be6SMatthias Ringwald l2cap_init(); 293c8f97be6SMatthias Ringwald 294c8f97be6SMatthias Ringwald gatt_client_init(); 295c8f97be6SMatthias Ringwald 296c8f97be6SMatthias Ringwald sm_init(); 297c8f97be6SMatthias Ringwald sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 298c8f97be6SMatthias Ringwald 299c8f97be6SMatthias Ringwald // turn on! 300c8f97be6SMatthias Ringwald hci_power_control(HCI_POWER_ON); 301c8f97be6SMatthias Ringwald 302c8f97be6SMatthias Ringwald return 0; 303c8f97be6SMatthias Ringwald } 304