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__ "gatt_device_information_query.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(gatt_device_information_query): GATT Device Information Service Client 42 * 43 * @text This example demonstrates how to use the GATT Device Information Service client to 44 * receive device information such as various IDs and revisions. The example scans 45 * for remote devices and connects to the first found device. If the remote device provides a Device 46 * Information Service, the information is collected and printed in console output, otherwise, 47 * the device will be blacklisted and the scan restarted. 48 */ 49 // ***************************************************************************** 50 51 #include <stdint.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 56 #include "btstack.h" 57 58 // gatt_device_information_query.gatt contains the declaration of the provided GATT Services + Characteristics 59 // gatt_device_information_query.h contains the binary representation of gatt_device_information_query.gatt 60 // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py gatt_device_information_query.gatt gatt_device_information_query.h 61 // it needs to be regenerated when the GATT Database declared in gatt_device_information_query.gatt file is modified 62 #include "gatt_device_information_query.h" 63 64 typedef struct advertising_report { 65 uint8_t type; 66 uint8_t event_type; 67 uint8_t address_type; 68 bd_addr_t address; 69 uint8_t rssi; 70 uint8_t length; 71 const uint8_t * data; 72 } advertising_report_t; 73 74 static enum { 75 APP_STATE_IDLE, 76 APP_STATE_W4_SCAN_RESULT, 77 APP_STATE_W4_CONNECT, 78 APP_STATE_CONNECTED 79 } app_state; 80 81 static int blacklist_index = 0; 82 static bd_addr_t blacklist[20]; 83 static advertising_report_t report; 84 85 static hci_con_handle_t connection_handle; 86 87 static bd_addr_t cmdline_addr; 88 static int cmdline_addr_found = 0; 89 90 static btstack_packet_callback_registration_t hci_event_callback_registration; 91 92 /* @section Main Application Setup 93 * 94 * @text The Listing MainConfiguration shows how to setup Device Information Service client. 95 * Besides calling init() method for each service, you'll also need to register HCI packet handler 96 * to handle advertisements, as well as connect and disconect events. 97 * 98 * @text Handling of GATT Device Information Service events will be later delegated to a sepparate packet 99 * handler, i.e. gatt_client_event_handler. 100 * 101 * @note There are two additional files associated with this client to allow a remote device to query out GATT database: 102 * - gatt_device_information_query.gatt - contains the declaration of the provided GATT Services and Characteristics. 103 * - gatt_device_information_query.h - contains the binary representation of gatt_device_information_query.gatt. 104 * 105 * gatt_device_information_query.h is generated by the build system by calling: 106 * $BTSTACK_ROOT/tool/compile_gatt.py gatt_device_information_query.gatt gatt_device_information_query.h 107 * This file needs to be regenerated when the GATT Database declared in gatt_device_information_query.gatt file is modified. 108 */ 109 110 /* LISTING_START(MainConfiguration): Setup Device Information Service Client service */ 111 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 112 static void gatt_client_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 113 114 static void device_information_service_client_setup(void){ 115 // Init L2CAP 116 l2cap_init(); 117 118 // Setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android phones 119 att_server_init(profile_data, NULL, NULL); 120 121 // GATT Client setup 122 gatt_client_init(); 123 // Device Information Service Client setup 124 device_information_service_client_init(); 125 126 sm_init(); 127 sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 128 129 hci_event_callback_registration.callback = &hci_packet_handler; 130 hci_add_event_handler(&hci_event_callback_registration); 131 } 132 /* LISTING_END */ 133 134 static int blacklist_size(void){ 135 return sizeof(blacklist) / sizeof(bd_addr_t); 136 } 137 138 static int blacklist_contains(bd_addr_t addr){ 139 int i; 140 for (i=0; i<blacklist_size(); i++){ 141 if (bd_addr_cmp(addr, blacklist[i]) == 0) return 1; 142 } 143 return 0; 144 } 145 146 static void add_to_blacklist(bd_addr_t addr){ 147 printf("%s added to blacklist (no device information service found).\n", bd_addr_to_str(addr)); 148 bd_addr_copy(blacklist[blacklist_index], addr); 149 blacklist_index = (blacklist_index + 1) % blacklist_size(); 150 } 151 152 static void dump_advertising_report(uint8_t *packet){ 153 bd_addr_t address; 154 gap_event_advertising_report_get_address(packet, address); 155 156 printf(" * adv. event: evt-type %u, addr-type %u, addr %s, rssi %u, length adv %u, data: ", 157 gap_event_advertising_report_get_advertising_event_type(packet), 158 gap_event_advertising_report_get_address_type(packet), 159 bd_addr_to_str(address), 160 gap_event_advertising_report_get_rssi(packet), 161 gap_event_advertising_report_get_data_length(packet)); 162 printf_hexdump(gap_event_advertising_report_get_data(packet), gap_event_advertising_report_get_data_length(packet)); 163 164 } 165 166 /* LISTING_START(packetHandler): Packet Handler */ 167 static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 168 /* LISTING_PAUSE */ 169 UNUSED(channel); 170 UNUSED(size); 171 172 uint8_t status; 173 bd_addr_t address; 174 175 if (packet_type != HCI_EVENT_PACKET){ 176 return; 177 } 178 179 switch (hci_event_packet_get_type(packet)) { 180 case BTSTACK_EVENT_STATE: 181 // BTstack activated, get started 182 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 183 if (cmdline_addr_found){ 184 printf("Connect to %s\n", bd_addr_to_str(cmdline_addr)); 185 app_state = APP_STATE_W4_CONNECT; 186 gap_connect(cmdline_addr, 0); 187 break; 188 } 189 printf("Start scanning!\n"); 190 app_state = APP_STATE_W4_SCAN_RESULT; 191 gap_set_scan_parameters(0,0x0030, 0x0030); 192 gap_start_scan(); 193 break; 194 195 case GAP_EVENT_ADVERTISING_REPORT: 196 if (app_state != APP_STATE_W4_SCAN_RESULT) return; 197 198 gap_event_advertising_report_get_address(packet, address); 199 if (blacklist_contains(address)) { 200 break; 201 } 202 dump_advertising_report(packet); 203 204 // stop scanning, and connect to the device 205 app_state = APP_STATE_W4_CONNECT; 206 gap_stop_scan(); 207 printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(report.address)); 208 gap_connect(report.address,report.address_type); 209 break; 210 211 /* LISTING_RESUME */ 212 case HCI_EVENT_LE_META: 213 // wait for connection complete 214 if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 215 216 /* LISTING_PAUSE */ 217 if (app_state != APP_STATE_W4_CONNECT) return; 218 219 /* LISTING_RESUME */ 220 // get connection handle from event 221 connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 222 223 // Connect to remote Device Information Service. The client will query the remote service and emit events, 224 // that will be passed on to gatt_client_event_handler. 225 status = device_information_service_client_query(connection_handle, gatt_client_event_handler); 226 btstack_assert(status == ERROR_CODE_SUCCESS); 227 228 printf("Device Information connected.\n"); 229 230 app_state = APP_STATE_CONNECTED; 231 break; 232 /* LISTING_PAUSE */ 233 234 case HCI_EVENT_DISCONNECTION_COMPLETE: 235 connection_handle = HCI_CON_HANDLE_INVALID; 236 237 if (cmdline_addr_found){ 238 printf("Disconnected %s\n", bd_addr_to_str(cmdline_addr)); 239 return; 240 } 241 242 printf("Disconnected %s\n", bd_addr_to_str(report.address)); 243 printf("Restart scan.\n"); 244 app_state = APP_STATE_W4_SCAN_RESULT; 245 gap_start_scan(); 246 break; 247 default: 248 break; 249 } 250 } 251 /* LISTING_END */ 252 253 254 /* LISTING_START(gatt_client_event_handler): GATT Client Event Handler */ 255 // The gatt_client_event_handler receives following events from remote device: 256 // - GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED 257 // - GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL 258 // 259 // Event GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_DONE is received when all queries are done, 260 // of if service was not found. The status field of this event indicated ATT errors (see bluetooth.h). 261 262 static void gatt_client_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 263 /* LISTING_PAUSE */ 264 UNUSED(packet_type); 265 UNUSED(channel); 266 UNUSED(size); 267 268 /* LISTING_RESUME */ 269 uint8_t att_status = 0; 270 271 if (hci_event_packet_get_type(packet) != HCI_EVENT_GATTSERVICE_META){ 272 return; 273 } 274 275 switch (hci_event_gattservice_meta_get_subevent_code(packet)){ 276 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_MANUFACTURER_NAME: 277 att_status = gattservice_subevent_device_information_manufacturer_name_get_att_status(packet); 278 if (att_status != ATT_ERROR_SUCCESS){ 279 printf("Manufacturer Name read failed, ATT Error 0x%02x\n", att_status); 280 } else { 281 printf("Manufacturer Name: %s\n", gattservice_subevent_device_information_manufacturer_name_get_value(packet)); 282 } 283 break; 284 285 // ... 286 /* LISTING_PAUSE */ 287 288 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_MODEL_NUMBER: 289 att_status = gattservice_subevent_device_information_model_number_get_att_status(packet); 290 if (att_status != ATT_ERROR_SUCCESS){ 291 printf("Model Number read failed, ATT Error 0x%02x\n", att_status); 292 } else { 293 printf("Model Number: %s\n", gattservice_subevent_device_information_model_number_get_value(packet)); 294 } 295 break; 296 297 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_SERIAL_NUMBER: 298 att_status = gattservice_subevent_device_information_serial_number_get_att_status(packet); 299 if (att_status != ATT_ERROR_SUCCESS){ 300 printf("Serial Number read failed, ATT Error 0x%02x\n", att_status); 301 } else { 302 printf("Serial Number: %s\n", gattservice_subevent_device_information_serial_number_get_value(packet)); 303 } 304 break; 305 306 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_HARDWARE_REVISION: 307 att_status = gattservice_subevent_device_information_hardware_revision_get_att_status(packet); 308 if (att_status != ATT_ERROR_SUCCESS){ 309 printf("Hardware Revision read failed, ATT Error 0x%02x\n", att_status); 310 } else { 311 printf("Hardware Revision: %s\n", gattservice_subevent_device_information_hardware_revision_get_value(packet)); 312 } 313 break; 314 315 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_FIRMWARE_REVISION: 316 att_status = gattservice_subevent_device_information_firmware_revision_get_att_status(packet); 317 if (att_status != ATT_ERROR_SUCCESS){ 318 printf("Firmware Revision read failed, ATT Error 0x%02x\n", att_status); 319 } else { 320 printf("Firmware Revision: %s\n", gattservice_subevent_device_information_firmware_revision_get_value(packet)); 321 } 322 break; 323 324 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_SOFTWARE_REVISION: 325 att_status = gattservice_subevent_device_information_software_revision_get_att_status(packet); 326 if (att_status != ATT_ERROR_SUCCESS){ 327 printf("Software Revision read failed, ATT Error 0x%02x\n", att_status); 328 } else { 329 printf("Software Revision: %s\n", gattservice_subevent_device_information_software_revision_get_value(packet)); 330 } 331 break; 332 333 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_SYSTEM_ID: 334 att_status = gattservice_subevent_device_information_system_id_get_att_status(packet); 335 if (att_status != ATT_ERROR_SUCCESS){ 336 printf("System ID read failed, ATT Error 0x%02x\n", att_status); 337 } else { 338 uint32_t manufacturer_identifier_low = gattservice_subevent_device_information_system_id_get_manufacturer_id_low(packet); 339 uint8_t manufacturer_identifier_high = gattservice_subevent_device_information_system_id_get_manufacturer_id_high(packet); 340 341 printf("Manufacturer ID: 0x%02x%08x\n", manufacturer_identifier_high, manufacturer_identifier_low); 342 printf("Organizationally Unique ID: 0x%06x\n", gattservice_subevent_device_information_system_id_get_organizationally_unique_id(packet)); 343 } 344 break; 345 346 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_IEEE_REGULATORY_CERTIFICATION: 347 att_status = gattservice_subevent_device_information_ieee_regulatory_certification_get_att_status(packet); 348 if (att_status != ATT_ERROR_SUCCESS){ 349 printf("IEEE Regulatory Certification read failed, ATT Error 0x%02x\n", att_status); 350 } else { 351 printf("value_a: 0x%04x\n", gattservice_subevent_device_information_ieee_regulatory_certification_get_value_a(packet)); 352 printf("value_b: 0x%04x\n", gattservice_subevent_device_information_ieee_regulatory_certification_get_value_b(packet)); 353 } 354 break; 355 356 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_PNP_ID: 357 att_status = gattservice_subevent_device_information_pnp_id_get_att_status(packet); 358 if (att_status != ATT_ERROR_SUCCESS){ 359 printf("PNP ID read failed, ATT Error 0x%02x\n", att_status); 360 } else { 361 printf("Vendor Source ID: 0x%02x\n", gattservice_subevent_device_information_pnp_id_get_vendor_source_id(packet)); 362 printf("Vendor ID: 0x%04x\n", gattservice_subevent_device_information_pnp_id_get_vendor_id(packet)); 363 printf("Product ID: 0x%04x\n", gattservice_subevent_device_information_pnp_id_get_product_id(packet)); 364 printf("Product Version: 0x%04x\n", gattservice_subevent_device_information_pnp_id_get_product_version(packet)); 365 } 366 break; 367 368 /* LISTING_RESUME */ 369 case GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_DONE: 370 att_status = gattservice_subevent_device_information_serial_number_get_att_status(packet); 371 switch (att_status){ 372 case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE: 373 printf("Device Information service client not found.\n"); 374 add_to_blacklist(report.address); 375 gap_disconnect(connection_handle); 376 break; 377 case ATT_ERROR_SUCCESS: 378 printf("Query done\n"); 379 break; 380 default: 381 printf("Query failed, ATT Error 0x%02x\n", att_status); 382 break; 383 384 } 385 if (att_status != ATT_ERROR_SUCCESS){ 386 if (att_status == ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE) 387 printf("Query failed, ATT Error 0x%02x\n", att_status); 388 } else { 389 printf("Query done\n"); 390 } 391 break; 392 default: 393 break; 394 } 395 } 396 /* LISTING_END */ 397 398 int btstack_main(int argc, const char * argv[]); 399 int btstack_main(int argc, const char * argv[]){ 400 401 // parse address if command line arguments are provided 402 int arg = 1; 403 cmdline_addr_found = 0; 404 405 while (arg < argc) { 406 if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ 407 arg++; 408 cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr); 409 arg++; 410 if (!cmdline_addr_found) exit(1); 411 continue; 412 } 413 fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", argv[0]); 414 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"); 415 return 0; 416 } 417 (void)argv; 418 419 device_information_service_client_setup(); 420 421 app_state = APP_STATE_IDLE; 422 423 // turn on! 424 hci_power_control(HCI_POWER_ON); 425 426 return 0; 427 } 428 429 /* EXAMPLE_END */ 430 431 432