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