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