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 /* EXAMPLE_START(gatt_browser): GATT Client - Discovering primary services and their characteristics 40 * 41 * @text This example shows how to use the GATT Client 42 * API to discover primary services and their characteristics of the first found 43 * device that is advertising its services. 44 * 45 * The logic is divided between the HCI and GATT client packet handlers. 46 * The HCI packet handler is responsible for finding a remote device, 47 * connecting to it, and for starting the first GATT client query. 48 * Then, the GATT client packet handler receives all primary services and 49 * requests the characteristics of the last one to keep the example short. 50 * 51 */ 52 // ***************************************************************************** 53 54 #include <stdint.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 59 #include "btstack.h" 60 61 typedef struct advertising_report { 62 uint8_t type; 63 uint8_t event_type; 64 uint8_t address_type; 65 bd_addr_t address; 66 uint8_t rssi; 67 uint8_t length; 68 const uint8_t * data; 69 } advertising_report_t; 70 71 static bd_addr_t cmdline_addr = { }; 72 static int cmdline_addr_found = 0; 73 74 static hci_con_handle_t connection_handler; 75 static gatt_client_service_t services[40]; 76 static int service_count = 0; 77 static int service_index = 0; 78 79 static btstack_packet_callback_registration_t hci_event_callback_registration; 80 81 /* @section GATT client setup 82 * 83 * @text In the setup phase, a GATT client must register the HCI and GATT client 84 * packet handlers, as shown in Listing GATTClientSetup. 85 * Additionally, the security manager can be setup, if signed writes, or 86 * encrypted, or authenticated connection are required, to access the 87 * characteristics, as explained in Section on [SMP](../protocols/#sec:smpProtocols). 88 */ 89 90 /* LISTING_START(GATTClientSetup): Setting up GATT client */ 91 92 // Handles connect, disconnect, and advertising report events, 93 // starts the GATT client, and sends the first query. 94 static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 95 96 // Handles GATT client query results, sends queries and the 97 // GAP disconnect command when the querying is done. 98 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 99 100 static void gatt_client_setup(void){ 101 102 // register for HCI events 103 hci_event_callback_registration.callback = &handle_hci_event; 104 hci_add_event_handler(&hci_event_callback_registration); 105 106 // Initialize L2CAP and register HCI event handler 107 l2cap_init(); 108 109 // Initialize GATT client 110 gatt_client_init(); 111 112 // Optinoally, Setup security manager 113 sm_init(); 114 sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 115 } 116 /* LISTING_END */ 117 118 static void printUUID(uint8_t * uuid128, uint16_t uuid16){ 119 if (uuid16){ 120 printf("%04x",uuid16); 121 } else { 122 printf("%s", uuid128_to_str(uuid128)); 123 } 124 } 125 126 static void dump_advertising_report(advertising_report_t * e){ 127 printf(" * adv. event: evt-type %u, addr-type %u, addr %s, rssi %u, length adv %u, data: ", e->event_type, 128 e->address_type, bd_addr_to_str(e->address), e->rssi, e->length); 129 printf_hexdump(e->data, e->length); 130 131 } 132 133 static void dump_characteristic(gatt_client_characteristic_t * characteristic){ 134 printf(" * characteristic: [0x%04x-0x%04x-0x%04x], properties 0x%02x, uuid ", 135 characteristic->start_handle, characteristic->value_handle, characteristic->end_handle, characteristic->properties); 136 printUUID(characteristic->uuid128, characteristic->uuid16); 137 printf("\n"); 138 } 139 140 static void dump_service(gatt_client_service_t * service){ 141 printf(" * service: [0x%04x-0x%04x], uuid ", service->start_group_handle, service->end_group_handle); 142 printUUID(service->uuid128, service->uuid16); 143 printf("\n"); 144 } 145 146 static void fill_advertising_report_from_packet(advertising_report_t * report, uint8_t *packet){ 147 gap_event_advertising_report_get_address(packet, report->address); 148 report->event_type = gap_event_advertising_report_get_advertising_event_type(packet); 149 report->address_type = gap_event_advertising_report_get_address_type(packet); 150 report->rssi = gap_event_advertising_report_get_rssi(packet); 151 report->length = gap_event_advertising_report_get_data_length(packet); 152 report->data = gap_event_advertising_report_get_data(packet); 153 } 154 155 /* @section HCI packet handler 156 * 157 * @text The HCI packet handler has to start the scanning, 158 * to find the first advertising device, to stop scanning, to connect 159 * to and later to disconnect from it, to start the GATT client upon 160 * the connection is completed, and to send the first query - in this 161 * case the gatt_client_discover_primary_services() is called, see 162 * Listing GATTBrowserHCIPacketHandler. 163 */ 164 165 /* LISTING_START(GATTBrowserHCIPacketHandler): Connecting and disconnecting from the GATT client */ 166 static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 167 UNUSED(channel); 168 UNUSED(size); 169 170 if (packet_type != HCI_EVENT_PACKET) return; 171 advertising_report_t report; 172 173 uint8_t event = hci_event_packet_get_type(packet); 174 switch (event) { 175 case BTSTACK_EVENT_STATE: 176 // BTstack activated, get started 177 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 178 if (cmdline_addr_found){ 179 printf("Trying to connect to %s\n", bd_addr_to_str(cmdline_addr)); 180 gap_connect(cmdline_addr, 0); 181 break; 182 } 183 printf("BTstack activated, start scanning!\n"); 184 gap_set_scan_parameters(0,0x0030, 0x0030); 185 gap_start_scan(); 186 break; 187 case GAP_EVENT_ADVERTISING_REPORT: 188 fill_advertising_report_from_packet(&report, packet); 189 dump_advertising_report(&report); 190 191 // stop scanning, and connect to the device 192 gap_stop_scan(); 193 gap_connect(report.address,report.address_type); 194 break; 195 case HCI_EVENT_LE_META: 196 // wait for connection complete 197 if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 198 connection_handler = hci_subevent_le_connection_complete_get_connection_handle(packet); 199 // query primary services 200 gatt_client_discover_primary_services(handle_gatt_client_event, connection_handler); 201 break; 202 case HCI_EVENT_DISCONNECTION_COMPLETE: 203 printf("\nGATT browser - DISCONNECTED\n"); 204 break; 205 default: 206 break; 207 } 208 } 209 /* LISTING_END */ 210 211 /* @section GATT Client event handler 212 * 213 * @text Query results and further queries are handled by the GATT client packet 214 * handler, as shown in Listing GATTBrowserQueryHandler. Here, upon 215 * receiving the primary services, the 216 * gatt_client_discover_characteristics_for_service() query for the last 217 * received service is sent. After receiving the characteristics for the service, 218 * gap_disconnect is called to terminate the connection. Upon 219 * disconnect, the HCI packet handler receives the disconnect complete event. 220 */ 221 222 /* LISTING_START(GATTBrowserQueryHandler): Handling of the GATT client queries */ 223 static int search_services = 1; 224 225 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 226 UNUSED(packet_type); 227 UNUSED(channel); 228 UNUSED(size); 229 230 gatt_client_service_t service; 231 gatt_client_characteristic_t characteristic; 232 switch(hci_event_packet_get_type(packet)){ 233 case GATT_EVENT_SERVICE_QUERY_RESULT:\ 234 gatt_event_service_query_result_get_service(packet, &service); 235 dump_service(&service); 236 services[service_count++] = service; 237 break; 238 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 239 gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic); 240 dump_characteristic(&characteristic); 241 break; 242 case GATT_EVENT_QUERY_COMPLETE: 243 if (search_services){ 244 // GATT_EVENT_QUERY_COMPLETE of search services 245 service_index = 0; 246 printf("\nGATT browser - CHARACTERISTIC for SERVICE %s\n", uuid128_to_str(service.uuid128)); 247 search_services = 0; 248 gatt_client_discover_characteristics_for_service(handle_gatt_client_event, connection_handler, &services[service_index]); 249 } else { 250 // GATT_EVENT_QUERY_COMPLETE of search characteristics 251 if (service_index < service_count) { 252 service = services[service_index++]; 253 printf("\nGATT browser - CHARACTERISTIC for SERVICE %s, [0x%04x-0x%04x]\n", 254 uuid128_to_str(service.uuid128), service.start_group_handle, service.end_group_handle); 255 gatt_client_discover_characteristics_for_service(handle_gatt_client_event, connection_handler, &service); 256 break; 257 } 258 service_index = 0; 259 gap_disconnect(connection_handler); 260 } 261 break; 262 default: 263 break; 264 } 265 } 266 /* LISTING_END */ 267 268 #ifdef HAVE_POSIX_STDIN 269 static void usage(const char *name){ 270 fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name); 271 fprintf(stderr, "If no argument is provided, GATT browser will start scanning and connect to the first found device.\nTo connect to a specific device use argument [-a].\n\n"); 272 } 273 #endif 274 275 int btstack_main(int argc, const char * argv[]); 276 int btstack_main(int argc, const char * argv[]){ 277 278 #ifdef HAVE_POSIX_STDIN 279 int arg = 1; 280 cmdline_addr_found = 0; 281 282 while (arg < argc) { 283 if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ 284 arg++; 285 cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr); 286 arg++; 287 continue; 288 } 289 usage(argv[0]); 290 return 0; 291 } 292 #else 293 UNUSED(argc); 294 UNUSED(argv); 295 #endif 296 297 gatt_client_setup(); 298 299 // turn on! 300 hci_power_control(HCI_POWER_ON); 301 302 return 0; 303 } 304 305 /* EXAMPLE_END */ 306 307 308