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_battery_query.c" 39 40 // ***************************************************************************** 41 // 42 // BLE Client 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 // gatt_battery_query.gatt contains the declaration of the provided GATT Services + Characteristics 54 // gatt_battery_query.h contains the binary representation of gatt_battery_query.gatt 55 // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py gatt_battery_query.gatt gatt_battery_query.h 56 // it needs to be regenerated when the GATT Database declared in gatt_battery_query.gatt file is modified 57 #include "gatt_battery_query.h" 58 59 typedef struct advertising_report { 60 uint8_t type; 61 uint8_t event_type; 62 uint8_t address_type; 63 bd_addr_t address; 64 uint8_t rssi; 65 uint8_t length; 66 const uint8_t * data; 67 } advertising_report_t; 68 69 70 typedef enum { 71 TC_IDLE, 72 TC_W4_SCAN_RESULT, 73 TC_W4_CONNECT, 74 TC_W4_SERVICE_RESULT, 75 TC_W4_CHARACTERISTIC_RESULT, 76 TC_W4_BATTERY_DATA 77 } gc_state_t; 78 79 static int blacklist_index = 0; 80 static bd_addr_t blacklist[20]; 81 static advertising_report_t report; 82 83 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 84 85 static bd_addr_t cmdline_addr; 86 static int cmdline_addr_found = 0; 87 88 static hci_con_handle_t connection_handle; 89 static uint16_t battery_service_uuid = 0x180F; 90 static uint16_t battery_level_characteristic_uuid = 0x2a19; 91 static gatt_client_service_t battery_service; 92 static gatt_client_characteristic_t battery_level_characteristic; 93 94 static gc_state_t state = TC_IDLE; 95 static btstack_packet_callback_registration_t hci_event_callback_registration; 96 97 static gatt_client_notification_t notification_listener; 98 static int listener_registered; 99 100 static void printUUID(uint8_t * uuid128, uint16_t uuid16){ 101 if (uuid16){ 102 printf("%04x",uuid16); 103 } else { 104 printf("%s", uuid128_to_str(uuid128)); 105 } 106 } 107 108 static int blacklist_size(void){ 109 return sizeof(blacklist) / sizeof(bd_addr_t); 110 } 111 112 static int blacklist_contains(bd_addr_t addr){ 113 int i; 114 for (i=0; i<blacklist_size(); i++){ 115 if (bd_addr_cmp(addr, blacklist[i]) == 0) return 1; 116 } 117 return 0; 118 } 119 120 static void add_to_blacklist(bd_addr_t addr){ 121 printf("%s added to blacklist (no battery service found\n", bd_addr_to_str(addr)); 122 bd_addr_copy(blacklist[blacklist_index], addr); 123 blacklist_index = (blacklist_index + 1) % blacklist_size(); 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_value(uint8_t * blob, uint16_t blob_length){ 134 printf(" * characteristic value of length %d *** ", blob_length ); 135 printf_hexdump(blob , blob_length); 136 printf("\n"); 137 } 138 139 static void dump_characteristic(gatt_client_characteristic_t * characteristic){ 140 printf(" * characteristic: [0x%04x-0x%04x-0x%04x], properties 0x%02x, uuid ", 141 characteristic->start_handle, characteristic->value_handle, characteristic->end_handle, characteristic->properties); 142 printUUID(characteristic->uuid128, characteristic->uuid16); 143 printf("\n"); 144 } 145 146 static void dump_service(gatt_client_service_t * service){ 147 printf(" * service: [0x%04x-0x%04x], uuid ", service->start_group_handle, service->end_group_handle); 148 printUUID(service->uuid128, service->uuid16); 149 printf("\n"); 150 } 151 152 153 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 154 UNUSED(packet_type); 155 UNUSED(channel); 156 UNUSED(size); 157 158 int status; 159 uint8_t battery_level; 160 161 switch(state){ 162 case TC_W4_SERVICE_RESULT: 163 switch(hci_event_packet_get_type(packet)){ 164 case GATT_EVENT_SERVICE_QUERY_RESULT: 165 gatt_event_service_query_result_get_service(packet, &battery_service); 166 dump_service(&battery_service); 167 break; 168 case GATT_EVENT_QUERY_COMPLETE: 169 if (packet[4] != 0){ 170 printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]); 171 add_to_blacklist(report.address); 172 gap_disconnect(connection_handle); 173 break; 174 } 175 state = TC_W4_CHARACTERISTIC_RESULT; 176 printf("\nSearch for battery level characteristic.\n"); 177 gatt_client_discover_characteristics_for_service_by_uuid16(handle_gatt_client_event, connection_handle, &battery_service, battery_level_characteristic_uuid); 178 break; 179 default: 180 break; 181 } 182 break; 183 184 case TC_W4_CHARACTERISTIC_RESULT: 185 switch(hci_event_packet_get_type(packet)){ 186 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 187 gatt_event_characteristic_query_result_get_characteristic(packet, &battery_level_characteristic); 188 dump_characteristic(&battery_level_characteristic); 189 break; 190 case GATT_EVENT_QUERY_COMPLETE: 191 if (packet[4] != 0){ 192 printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]); 193 add_to_blacklist(report.address); 194 gap_disconnect(connection_handle); 195 break; 196 } 197 198 // register handler for notifications 199 listener_registered = 1; 200 gatt_client_listen_for_characteristic_value_updates(¬ification_listener, handle_gatt_client_event, connection_handle, &battery_level_characteristic); 201 202 state = TC_W4_BATTERY_DATA; 203 printf("\nConfigure battery level characteristic for notify.\n"); 204 status = gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &battery_level_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 205 if (status != 0){ 206 printf("\nNotification not supported. Query value of characteristic.\n"); 207 gatt_client_read_value_of_characteristic(handle_gatt_client_event, connection_handle, &battery_level_characteristic); 208 } 209 210 break; 211 default: 212 break; 213 } 214 break; 215 case TC_W4_BATTERY_DATA: 216 switch(hci_event_packet_get_type(packet)){ 217 case GATT_EVENT_NOTIFICATION: 218 printf("\nBattery Data:\n"); 219 dump_characteristic_value(&packet[8], little_endian_read_16(packet, 6)); 220 break; 221 case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 222 223 if (gatt_event_characteristic_value_query_result_get_value_length(packet) < 1) break; 224 battery_level = gatt_event_characteristic_value_query_result_get_value(packet)[0]; 225 printf("Battery level %d \n", battery_level); 226 break; 227 228 case GATT_EVENT_QUERY_COMPLETE: 229 if (packet[4] != 0){ 230 printf("CHARACTERISTIC_VALUE_QUERY_RESULT - Error status %x.\n", packet[4]); 231 break; 232 } 233 break; 234 default: 235 printf("Unknown packet type %x\n", hci_event_packet_get_type(packet)); 236 break; 237 } 238 break; 239 240 default: 241 printf("error\n"); 242 break; 243 } 244 245 } 246 247 static void fill_advertising_report_from_packet(advertising_report_t * adv_report, uint8_t *packet){ 248 gap_event_advertising_report_get_address(packet, adv_report->address); 249 adv_report->event_type = gap_event_advertising_report_get_advertising_event_type(packet); 250 adv_report->address_type = gap_event_advertising_report_get_address_type(packet); 251 adv_report->rssi = gap_event_advertising_report_get_rssi(packet); 252 adv_report->length = gap_event_advertising_report_get_data_length(packet); 253 adv_report->data = gap_event_advertising_report_get_data(packet); 254 } 255 256 static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 257 UNUSED(channel); 258 UNUSED(size); 259 260 if (packet_type != HCI_EVENT_PACKET) return; 261 262 uint8_t event = hci_event_packet_get_type(packet); 263 switch (event) { 264 case BTSTACK_EVENT_STATE: 265 // BTstack activated, get started 266 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 267 if (cmdline_addr_found){ 268 printf("Connect to %s\n", bd_addr_to_str(cmdline_addr)); 269 state = TC_W4_CONNECT; 270 gap_connect(cmdline_addr, 0); 271 break; 272 } 273 printf("Start scanning!\n"); 274 state = TC_W4_SCAN_RESULT; 275 gap_set_scan_parameters(0,0x0030, 0x0030); 276 gap_start_scan(); 277 break; 278 case GAP_EVENT_ADVERTISING_REPORT: 279 if (state != TC_W4_SCAN_RESULT) return; 280 fill_advertising_report_from_packet(&report, packet); 281 282 if (blacklist_contains(report.address)) { 283 break; 284 } 285 dump_advertising_report(&report); 286 // stop scanning, and connect to the device 287 state = TC_W4_CONNECT; 288 gap_stop_scan(); 289 printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(report.address)); 290 gap_connect(report.address,report.address_type); 291 292 break; 293 case HCI_EVENT_LE_META: 294 // wait for connection complete 295 if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 296 if (state != TC_W4_CONNECT) return; 297 connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 298 // initialize gatt client context with handle, and add it to the list of active clients 299 // query primary services 300 printf("\nSearch for battery service.\n"); 301 state = TC_W4_SERVICE_RESULT; 302 gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, battery_service_uuid); 303 break; 304 case HCI_EVENT_DISCONNECTION_COMPLETE: 305 // unregister listener 306 connection_handle = HCI_CON_HANDLE_INVALID; 307 if (listener_registered){ 308 listener_registered = 0; 309 gatt_client_stop_listening_for_characteristic_value_updates(¬ification_listener); 310 } 311 312 if (cmdline_addr_found){ 313 printf("\nDisconnected %s\n", bd_addr_to_str(cmdline_addr)); 314 return; 315 } 316 317 printf("\nDisconnected %s\n", bd_addr_to_str(report.address)); 318 printf("Restart scan.\n"); 319 state = TC_W4_SCAN_RESULT; 320 gap_start_scan(); 321 break; 322 default: 323 break; 324 } 325 } 326 327 #ifdef HAVE_BTSTACK_STDIN 328 static void usage(const char *name){ 329 fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name); 330 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"); 331 } 332 #endif 333 334 int btstack_main(int argc, const char * argv[]); 335 int btstack_main(int argc, const char * argv[]){ 336 337 #ifdef HAVE_BTSTACK_STDIN 338 int arg = 1; 339 cmdline_addr_found = 0; 340 341 while (arg < argc) { 342 if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ 343 arg++; 344 cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr); 345 arg++; 346 if (!cmdline_addr_found) exit(1); 347 continue; 348 } 349 usage(argv[0]); 350 return 0; 351 } 352 #else 353 (void)argc; 354 (void)argv; 355 #endif 356 l2cap_init(); 357 358 // setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android phones 359 att_server_init(profile_data, NULL, NULL); 360 361 // GATT Client setup 362 gatt_client_init(); 363 364 sm_init(); 365 sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 366 367 hci_event_callback_registration.callback = &hci_event_handler; 368 hci_add_event_handler(&hci_event_callback_registration); 369 370 371 // turn on! 372 hci_power_control(HCI_POWER_ON); 373 374 return 0; 375 } 376 377 378 379 380