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 // ***************************************************************************** 39 // 40 // LE Streamer Client - connects to 'LE Streamer' and subscribes to test characteristic 41 // 42 // ***************************************************************************** 43 44 #include <stdint.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 49 #include "btstack.h" 50 51 typedef enum { 52 TC_IDLE, 53 TC_W4_SCAN_RESULT, 54 TC_W4_CONNECT, 55 TC_W4_SERVICE_RESULT, 56 TC_W4_CHARACTERISTIC_RESULT, 57 TC_W4_TEST_DATA 58 } gc_state_t; 59 60 static bd_addr_t cmdline_addr = { }; 61 static int cmdline_addr_found = 0; 62 63 // addr and type of device with correct name 64 static bd_addr_t le_streamer_addr; 65 static bd_addr_type_t le_streamer_addr_type; 66 67 static hci_con_handle_t connection_handle; 68 static uint8_t le_streamer_service_uuid[16] = { 0x00, 0x00, 0xFF, 0x10, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; 69 static uint8_t le_streamer_characteristic_uuid[16] = { 0x00, 0x00, 0xFF, 0x11, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB}; 70 71 static gatt_client_service_t le_streamer_service; 72 static gatt_client_characteristic_t le_streamer_characteristic; 73 74 static gatt_client_notification_t notification_registration; 75 76 static gc_state_t state = TC_IDLE; 77 static btstack_packet_callback_registration_t hci_event_callback_registration; 78 79 // returns 1 if name is found in advertisement 80 static int advertisement_report_contains_name(const char * name, uint8_t * advertisement_report){ 81 // get advertisement from report event 82 const uint8_t * adv_data = gap_event_advertising_report_get_data(advertisement_report); 83 uint16_t adv_len = gap_event_advertising_report_get_data_length(advertisement_report); 84 int name_len = strlen(name); 85 86 // iterate over advertisement data 87 ad_context_t context; 88 for (ad_iterator_init(&context, adv_len, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 89 uint8_t data_type = ad_iterator_get_data_type(&context); 90 uint8_t data_size = ad_iterator_get_data_len(&context); 91 const uint8_t * data = ad_iterator_get_data(&context); 92 int i; 93 switch (data_type){ 94 case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 95 case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 96 // compare common prefix 97 for (i=0; i<data_size && i<name_len;i++){ 98 if (data[i] != name[i]) break; 99 } 100 // prefix match 101 return 1; 102 default: 103 break; 104 } 105 } 106 return 0; 107 } 108 109 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 110 UNUSED(packet_type); 111 UNUSED(channel); 112 UNUSED(size); 113 114 switch(state){ 115 case TC_W4_SERVICE_RESULT: 116 switch(hci_event_packet_get_type(packet)){ 117 case GATT_EVENT_SERVICE_QUERY_RESULT: 118 // store service (we expect only one) 119 gatt_event_service_query_result_get_service(packet, &le_streamer_service); 120 break; 121 case GATT_EVENT_QUERY_COMPLETE: 122 if (packet[4] != 0){ 123 printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]); 124 gap_disconnect(connection_handle); 125 break; 126 } 127 // service query complete, look for characteristic 128 state = TC_W4_CHARACTERISTIC_RESULT; 129 printf("Search for LE Streamer test characteristic.\n"); 130 gatt_client_discover_characteristics_for_service_by_uuid128(handle_gatt_client_event, connection_handle, &le_streamer_service, le_streamer_characteristic_uuid); 131 break; 132 default: 133 break; 134 } 135 break; 136 137 case TC_W4_CHARACTERISTIC_RESULT: 138 switch(hci_event_packet_get_type(packet)){ 139 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 140 gatt_event_characteristic_query_result_get_characteristic(packet, &le_streamer_characteristic); 141 break; 142 case GATT_EVENT_QUERY_COMPLETE: 143 if (packet[4] != 0){ 144 printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]); 145 gap_disconnect(connection_handle); 146 break; 147 } 148 // register handler for notifications 149 gatt_client_listen_for_characteristic_value_updates(¬ification_registration, handle_gatt_client_event, connection_handle, &le_streamer_characteristic); 150 // enable notifications 151 state = TC_W4_TEST_DATA; 152 printf("Start streaming - enable notify on test characteristic.\n"); 153 gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &le_streamer_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 154 break; 155 default: 156 break; 157 } 158 break; 159 160 case TC_W4_TEST_DATA: 161 switch(hci_event_packet_get_type(packet)){ 162 case GATT_EVENT_NOTIFICATION: 163 printf("Data: "); 164 printf_hexdump( gatt_event_notification_get_value(packet), gatt_event_notification_get_value_length(packet)); 165 case GATT_EVENT_QUERY_COMPLETE: 166 break; 167 default: 168 printf("Unknown packet type %x\n", hci_event_packet_get_type(packet)); 169 break; 170 } 171 break; 172 173 default: 174 printf("error\n"); 175 break; 176 } 177 178 } 179 180 static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 181 UNUSED(channel); 182 UNUSED(size); 183 184 if (packet_type != HCI_EVENT_PACKET) return; 185 186 uint8_t event = hci_event_packet_get_type(packet); 187 switch (event) { 188 case BTSTACK_EVENT_STATE: 189 // BTstack activated, get started 190 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 191 if (cmdline_addr_found){ 192 printf("Connect to %s\n", bd_addr_to_str(cmdline_addr)); 193 state = TC_W4_CONNECT; 194 gap_connect(cmdline_addr, 0); 195 break; 196 } 197 printf("Start scanning!\n"); 198 state = TC_W4_SCAN_RESULT; 199 gap_set_scan_parameters(0,0x0030, 0x0030); 200 gap_start_scan(); 201 break; 202 case GAP_EVENT_ADVERTISING_REPORT: 203 if (state != TC_W4_SCAN_RESULT) return; 204 // check name in advertisement 205 if (!advertisement_report_contains_name("LE Streamer", packet)) return; 206 // store address and type 207 gap_event_advertising_report_get_address(packet, le_streamer_addr); 208 le_streamer_addr_type = gap_event_advertising_report_get_address_type(packet); 209 // stop scanning, and connect to the device 210 state = TC_W4_CONNECT; 211 gap_stop_scan(); 212 printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(le_streamer_addr)); 213 gap_connect(le_streamer_addr,le_streamer_addr_type); 214 break; 215 case HCI_EVENT_LE_META: 216 // wait for connection complete 217 if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 218 if (state != TC_W4_CONNECT) return; 219 connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 220 // initialize gatt client context with handle, and add it to the list of active clients 221 // query primary services 222 printf("Search for LE Streamer service.\n"); 223 state = TC_W4_SERVICE_RESULT; 224 gatt_client_discover_primary_services_by_uuid128(handle_gatt_client_event, connection_handle, le_streamer_service_uuid); 225 break; 226 case HCI_EVENT_DISCONNECTION_COMPLETE: 227 if (cmdline_addr_found){ 228 printf("Disconnected %s\n", bd_addr_to_str(cmdline_addr)); 229 return; 230 } 231 printf("Disconnected %s\n", bd_addr_to_str(le_streamer_addr)); 232 break; 233 default: 234 break; 235 } 236 } 237 238 #ifdef HAVE_POSIX_STDIN 239 static void usage(const char *name){ 240 fprintf(stderr, "Usage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name); 241 fprintf(stderr, "If no argument is provided, LE Streamer Client will start scanning and connect to the first device named 'LE Streamer'.\n"); 242 fprintf(stderr, "To connect to a specific device use argument [-a].\n\n"); 243 } 244 #endif 245 246 int btstack_main(int argc, const char * argv[]); 247 int btstack_main(int argc, const char * argv[]){ 248 249 #ifdef HAVE_POSIX_STDIN 250 int arg = 1; 251 cmdline_addr_found = 0; 252 253 while (arg < argc) { 254 if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ 255 arg++; 256 cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr); 257 arg++; 258 if (!cmdline_addr_found) exit(1); 259 continue; 260 } 261 usage(argv[0]); 262 return 0; 263 } 264 #else 265 UNUSED(argc); 266 UNUSED(argv); 267 #endif 268 269 hci_event_callback_registration.callback = &hci_event_handler; 270 hci_add_event_handler(&hci_event_callback_registration); 271 272 l2cap_init(); 273 274 gatt_client_init(); 275 276 sm_init(); 277 sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 278 279 // turn on! 280 hci_power_control(HCI_POWER_ON); 281 282 return 0; 283 } 284