1bcf00d8fSMatthias Ringwald /* 2bcf00d8fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3bcf00d8fSMatthias Ringwald * 4bcf00d8fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5bcf00d8fSMatthias Ringwald * modification, are permitted provided that the following conditions 6bcf00d8fSMatthias Ringwald * are met: 7bcf00d8fSMatthias Ringwald * 8bcf00d8fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10bcf00d8fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12bcf00d8fSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13bcf00d8fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14bcf00d8fSMatthias Ringwald * contributors may be used to endorse or promote products derived 15bcf00d8fSMatthias Ringwald * from this software without specific prior written permission. 16bcf00d8fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17bcf00d8fSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18bcf00d8fSMatthias Ringwald * monetary gain. 19bcf00d8fSMatthias Ringwald * 20bcf00d8fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21bcf00d8fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22bcf00d8fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23bcf00d8fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24bcf00d8fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25bcf00d8fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26bcf00d8fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27bcf00d8fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28bcf00d8fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29bcf00d8fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30bcf00d8fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31bcf00d8fSMatthias Ringwald * SUCH DAMAGE. 32bcf00d8fSMatthias Ringwald * 33bcf00d8fSMatthias Ringwald * Please inquire about commercial licensing options at 34bcf00d8fSMatthias Ringwald * [email protected] 35bcf00d8fSMatthias Ringwald * 36bcf00d8fSMatthias Ringwald */ 37bcf00d8fSMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "gatt_battery_query.c" 39ab2c6ae4SMatthias Ringwald 40bcf00d8fSMatthias Ringwald // ***************************************************************************** 41*ec8ae085SMilanka Ringwald /* EXAMPLE_START(gatt_battery_query): GATT Battery Client 42*ec8ae085SMilanka Ringwald * 43*ec8ae085SMilanka Ringwald */ 44bcf00d8fSMatthias Ringwald 45bcf00d8fSMatthias Ringwald #include <stdint.h> 46bcf00d8fSMatthias Ringwald #include <stdio.h> 47bcf00d8fSMatthias Ringwald #include <stdlib.h> 48bcf00d8fSMatthias Ringwald #include <string.h> 49bcf00d8fSMatthias Ringwald 50bcf00d8fSMatthias Ringwald #include "btstack.h" 51a63a688aSMatthias Ringwald 52a63a688aSMatthias Ringwald // gatt_battery_query.gatt contains the declaration of the provided GATT Services + Characteristics 53a63a688aSMatthias Ringwald // gatt_battery_query.h contains the binary representation of gatt_battery_query.gatt 54a63a688aSMatthias Ringwald // it is generated by the build system by calling: $BTSTACK_ROOT/tool/compile_gatt.py gatt_battery_query.gatt gatt_battery_query.h 55a63a688aSMatthias Ringwald // it needs to be regenerated when the GATT Database declared in gatt_battery_query.gatt file is modified 56add9769eSMatthias Ringwald #include "gatt_battery_query.h" 57bcf00d8fSMatthias Ringwald 58bcf00d8fSMatthias Ringwald typedef struct advertising_report { 59bcf00d8fSMatthias Ringwald uint8_t type; 60bcf00d8fSMatthias Ringwald uint8_t event_type; 61bcf00d8fSMatthias Ringwald uint8_t address_type; 62bcf00d8fSMatthias Ringwald bd_addr_t address; 63bcf00d8fSMatthias Ringwald uint8_t rssi; 64bcf00d8fSMatthias Ringwald uint8_t length; 653ee82ab1SMilanka Ringwald const uint8_t * data; 66bcf00d8fSMatthias Ringwald } advertising_report_t; 67bcf00d8fSMatthias Ringwald 68bcf00d8fSMatthias Ringwald 69bcf00d8fSMatthias Ringwald typedef enum { 70bcf00d8fSMatthias Ringwald TC_IDLE, 71bcf00d8fSMatthias Ringwald TC_W4_SCAN_RESULT, 72bcf00d8fSMatthias Ringwald TC_W4_CONNECT, 73bcf00d8fSMatthias Ringwald TC_W4_SERVICE_RESULT, 74bcf00d8fSMatthias Ringwald TC_W4_CHARACTERISTIC_RESULT, 75bcf00d8fSMatthias Ringwald TC_W4_BATTERY_DATA 76bcf00d8fSMatthias Ringwald } gc_state_t; 77bcf00d8fSMatthias Ringwald 780f09bd96SMilanka Ringwald static int blacklist_index = 0; 790f09bd96SMilanka Ringwald static bd_addr_t blacklist[20]; 800f09bd96SMilanka Ringwald static advertising_report_t report; 810f09bd96SMilanka Ringwald 82bcf00d8fSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 83bcf00d8fSMatthias Ringwald 84c30af2ffSMatthias Ringwald static bd_addr_t cmdline_addr; 85bcf00d8fSMatthias Ringwald static int cmdline_addr_found = 0; 86bcf00d8fSMatthias Ringwald 87a59bfbf7SMatthias Ringwald static hci_con_handle_t connection_handle; 88bcf00d8fSMatthias Ringwald static uint16_t battery_service_uuid = 0x180F; 89bcf00d8fSMatthias Ringwald static uint16_t battery_level_characteristic_uuid = 0x2a19; 90bcf00d8fSMatthias Ringwald static gatt_client_service_t battery_service; 9126687d14SMatthias Ringwald static gatt_client_characteristic_t battery_level_characteristic; 92bcf00d8fSMatthias Ringwald 93bcf00d8fSMatthias Ringwald static gc_state_t state = TC_IDLE; 94bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 95bcf00d8fSMatthias Ringwald 9626687d14SMatthias Ringwald static gatt_client_notification_t notification_listener; 9726687d14SMatthias Ringwald static int listener_registered; 9826687d14SMatthias Ringwald 99bcf00d8fSMatthias Ringwald static void printUUID(uint8_t * uuid128, uint16_t uuid16){ 100bcf00d8fSMatthias Ringwald if (uuid16){ 101bcf00d8fSMatthias Ringwald printf("%04x",uuid16); 102bcf00d8fSMatthias Ringwald } else { 103bcf00d8fSMatthias Ringwald printf("%s", uuid128_to_str(uuid128)); 104bcf00d8fSMatthias Ringwald } 105bcf00d8fSMatthias Ringwald } 106bcf00d8fSMatthias Ringwald 107287eecffSMilanka Ringwald static int blacklist_size(void){ 1080f09bd96SMilanka Ringwald return sizeof(blacklist) / sizeof(bd_addr_t); 1090f09bd96SMilanka Ringwald } 1100f09bd96SMilanka Ringwald 1110f09bd96SMilanka Ringwald static int blacklist_contains(bd_addr_t addr){ 1120f09bd96SMilanka Ringwald int i; 1130f09bd96SMilanka Ringwald for (i=0; i<blacklist_size(); i++){ 1140f09bd96SMilanka Ringwald if (bd_addr_cmp(addr, blacklist[i]) == 0) return 1; 1150f09bd96SMilanka Ringwald } 1160f09bd96SMilanka Ringwald return 0; 1170f09bd96SMilanka Ringwald } 1180f09bd96SMilanka Ringwald 1190f09bd96SMilanka Ringwald static void add_to_blacklist(bd_addr_t addr){ 1200f09bd96SMilanka Ringwald printf("%s added to blacklist (no battery service found\n", bd_addr_to_str(addr)); 1210f09bd96SMilanka Ringwald bd_addr_copy(blacklist[blacklist_index], addr); 1220f09bd96SMilanka Ringwald blacklist_index = (blacklist_index + 1) % blacklist_size(); 1230f09bd96SMilanka Ringwald } 1240f09bd96SMilanka Ringwald 125bcf00d8fSMatthias Ringwald static void dump_advertising_report(advertising_report_t * e){ 126bcf00d8fSMatthias Ringwald printf(" * adv. event: evt-type %u, addr-type %u, addr %s, rssi %u, length adv %u, data: ", e->event_type, 127bcf00d8fSMatthias Ringwald e->address_type, bd_addr_to_str(e->address), e->rssi, e->length); 128bcf00d8fSMatthias Ringwald printf_hexdump(e->data, e->length); 129bcf00d8fSMatthias Ringwald 130bcf00d8fSMatthias Ringwald } 131bcf00d8fSMatthias Ringwald 132bcf00d8fSMatthias Ringwald static void dump_characteristic_value(uint8_t * blob, uint16_t blob_length){ 133bcf00d8fSMatthias Ringwald printf(" * characteristic value of length %d *** ", blob_length ); 134bcf00d8fSMatthias Ringwald printf_hexdump(blob , blob_length); 135bcf00d8fSMatthias Ringwald printf("\n"); 136bcf00d8fSMatthias Ringwald } 137bcf00d8fSMatthias Ringwald 138bcf00d8fSMatthias Ringwald static void dump_characteristic(gatt_client_characteristic_t * characteristic){ 139bcf00d8fSMatthias Ringwald printf(" * characteristic: [0x%04x-0x%04x-0x%04x], properties 0x%02x, uuid ", 140bcf00d8fSMatthias Ringwald characteristic->start_handle, characteristic->value_handle, characteristic->end_handle, characteristic->properties); 141bcf00d8fSMatthias Ringwald printUUID(characteristic->uuid128, characteristic->uuid16); 142bcf00d8fSMatthias Ringwald printf("\n"); 143bcf00d8fSMatthias Ringwald } 144bcf00d8fSMatthias Ringwald 145bcf00d8fSMatthias Ringwald static void dump_service(gatt_client_service_t * service){ 146bcf00d8fSMatthias Ringwald printf(" * service: [0x%04x-0x%04x], uuid ", service->start_group_handle, service->end_group_handle); 147bcf00d8fSMatthias Ringwald printUUID(service->uuid128, service->uuid16); 148bcf00d8fSMatthias Ringwald printf("\n"); 149bcf00d8fSMatthias Ringwald } 150bcf00d8fSMatthias Ringwald 151bcf00d8fSMatthias Ringwald 152bcf00d8fSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1539ec2630cSMatthias Ringwald UNUSED(packet_type); 1549ec2630cSMatthias Ringwald UNUSED(channel); 1559ec2630cSMatthias Ringwald UNUSED(size); 1569ec2630cSMatthias Ringwald 1570f09bd96SMilanka Ringwald int status; 1580f09bd96SMilanka Ringwald uint8_t battery_level; 159bcf00d8fSMatthias Ringwald 160bcf00d8fSMatthias Ringwald switch(state){ 161bcf00d8fSMatthias Ringwald case TC_W4_SERVICE_RESULT: 1620e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 163bcf00d8fSMatthias Ringwald case GATT_EVENT_SERVICE_QUERY_RESULT: 164bcf00d8fSMatthias Ringwald gatt_event_service_query_result_get_service(packet, &battery_service); 165bcf00d8fSMatthias Ringwald dump_service(&battery_service); 166bcf00d8fSMatthias Ringwald break; 167bcf00d8fSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 1680f09bd96SMilanka Ringwald if (packet[4] != 0){ 1690f09bd96SMilanka Ringwald printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]); 1700f09bd96SMilanka Ringwald add_to_blacklist(report.address); 1710f09bd96SMilanka Ringwald gap_disconnect(connection_handle); 172bcf00d8fSMatthias Ringwald break; 173bcf00d8fSMatthias Ringwald } 174bcf00d8fSMatthias Ringwald state = TC_W4_CHARACTERISTIC_RESULT; 1750f09bd96SMilanka Ringwald printf("\nSearch for battery level characteristic.\n"); 176a59bfbf7SMatthias Ringwald gatt_client_discover_characteristics_for_service_by_uuid16(handle_gatt_client_event, connection_handle, &battery_service, battery_level_characteristic_uuid); 177bcf00d8fSMatthias Ringwald break; 178bcf00d8fSMatthias Ringwald default: 179bcf00d8fSMatthias Ringwald break; 180bcf00d8fSMatthias Ringwald } 181bcf00d8fSMatthias Ringwald break; 182bcf00d8fSMatthias Ringwald 183bcf00d8fSMatthias Ringwald case TC_W4_CHARACTERISTIC_RESULT: 1840e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 185bcf00d8fSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 18626687d14SMatthias Ringwald gatt_event_characteristic_query_result_get_characteristic(packet, &battery_level_characteristic); 18726687d14SMatthias Ringwald dump_characteristic(&battery_level_characteristic); 188bcf00d8fSMatthias Ringwald break; 189bcf00d8fSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 1900f09bd96SMilanka Ringwald if (packet[4] != 0){ 1910f09bd96SMilanka Ringwald printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]); 1920f09bd96SMilanka Ringwald add_to_blacklist(report.address); 1930f09bd96SMilanka Ringwald gap_disconnect(connection_handle); 1940f09bd96SMilanka Ringwald break; 1950f09bd96SMilanka Ringwald } 19626687d14SMatthias Ringwald 19726687d14SMatthias Ringwald // register handler for notifications 19826687d14SMatthias Ringwald listener_registered = 1; 19926687d14SMatthias Ringwald gatt_client_listen_for_characteristic_value_updates(¬ification_listener, handle_gatt_client_event, connection_handle, &battery_level_characteristic); 20026687d14SMatthias Ringwald 201bcf00d8fSMatthias Ringwald state = TC_W4_BATTERY_DATA; 2020f09bd96SMilanka Ringwald printf("\nConfigure battery level characteristic for notify.\n"); 20326687d14SMatthias Ringwald status = gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, &battery_level_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 2040f09bd96SMilanka Ringwald if (status != 0){ 2050f09bd96SMilanka Ringwald printf("\nNotification not supported. Query value of characteristic.\n"); 20626687d14SMatthias Ringwald gatt_client_read_value_of_characteristic(handle_gatt_client_event, connection_handle, &battery_level_characteristic); 2070f09bd96SMilanka Ringwald } 2080f09bd96SMilanka Ringwald 209bcf00d8fSMatthias Ringwald break; 210bcf00d8fSMatthias Ringwald default: 211bcf00d8fSMatthias Ringwald break; 212bcf00d8fSMatthias Ringwald } 213bcf00d8fSMatthias Ringwald break; 214bcf00d8fSMatthias Ringwald case TC_W4_BATTERY_DATA: 2150f09bd96SMilanka Ringwald switch(hci_event_packet_get_type(packet)){ 2160f09bd96SMilanka Ringwald case GATT_EVENT_NOTIFICATION: 217bcf00d8fSMatthias Ringwald printf("\nBattery Data:\n"); 218bcf00d8fSMatthias Ringwald dump_characteristic_value(&packet[8], little_endian_read_16(packet, 6)); 219bcf00d8fSMatthias Ringwald break; 2200f09bd96SMilanka Ringwald case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 2210f09bd96SMilanka Ringwald 2220f09bd96SMilanka Ringwald if (gatt_event_characteristic_value_query_result_get_value_length(packet) < 1) break; 2230f09bd96SMilanka Ringwald battery_level = gatt_event_characteristic_value_query_result_get_value(packet)[0]; 224a8d3af57SMatthias Ringwald printf("Battery level %d \n", battery_level); 2250f09bd96SMilanka Ringwald break; 2260f09bd96SMilanka Ringwald 2270f09bd96SMilanka Ringwald case GATT_EVENT_QUERY_COMPLETE: 2280f09bd96SMilanka Ringwald if (packet[4] != 0){ 2290f09bd96SMilanka Ringwald printf("CHARACTERISTIC_VALUE_QUERY_RESULT - Error status %x.\n", packet[4]); 2300f09bd96SMilanka Ringwald break; 2310f09bd96SMilanka Ringwald } 2320f09bd96SMilanka Ringwald break; 2330f09bd96SMilanka Ringwald default: 2340f09bd96SMilanka Ringwald printf("Unknown packet type %x\n", hci_event_packet_get_type(packet)); 2350f09bd96SMilanka Ringwald break; 2360f09bd96SMilanka Ringwald } 2370f09bd96SMilanka Ringwald break; 238bcf00d8fSMatthias Ringwald 239bcf00d8fSMatthias Ringwald default: 240bcf00d8fSMatthias Ringwald printf("error\n"); 241bcf00d8fSMatthias Ringwald break; 242bcf00d8fSMatthias Ringwald } 243bcf00d8fSMatthias Ringwald 244bcf00d8fSMatthias Ringwald } 245bcf00d8fSMatthias Ringwald 2460f09bd96SMilanka Ringwald static void fill_advertising_report_from_packet(advertising_report_t * adv_report, uint8_t *packet){ 2470f09bd96SMilanka Ringwald gap_event_advertising_report_get_address(packet, adv_report->address); 2480f09bd96SMilanka Ringwald adv_report->event_type = gap_event_advertising_report_get_advertising_event_type(packet); 2490f09bd96SMilanka Ringwald adv_report->address_type = gap_event_advertising_report_get_address_type(packet); 2500f09bd96SMilanka Ringwald adv_report->rssi = gap_event_advertising_report_get_rssi(packet); 2510f09bd96SMilanka Ringwald adv_report->length = gap_event_advertising_report_get_data_length(packet); 2520f09bd96SMilanka Ringwald adv_report->data = gap_event_advertising_report_get_data(packet); 253bcf00d8fSMatthias Ringwald } 254bcf00d8fSMatthias Ringwald 255bcf00d8fSMatthias Ringwald static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2569ec2630cSMatthias Ringwald UNUSED(channel); 2579ec2630cSMatthias Ringwald UNUSED(size); 2589ec2630cSMatthias Ringwald 259bcf00d8fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 2600f09bd96SMilanka Ringwald 2610e2df43fSMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet); 262bcf00d8fSMatthias Ringwald switch (event) { 263bcf00d8fSMatthias Ringwald case BTSTACK_EVENT_STATE: 264bcf00d8fSMatthias Ringwald // BTstack activated, get started 265cdc7d1abSMilanka Ringwald if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 266bcf00d8fSMatthias Ringwald if (cmdline_addr_found){ 2670f09bd96SMilanka Ringwald printf("Connect to %s\n", bd_addr_to_str(cmdline_addr)); 268bcf00d8fSMatthias Ringwald state = TC_W4_CONNECT; 269bcf00d8fSMatthias Ringwald gap_connect(cmdline_addr, 0); 270bcf00d8fSMatthias Ringwald break; 271bcf00d8fSMatthias Ringwald } 2720f09bd96SMilanka Ringwald printf("Start scanning!\n"); 273bcf00d8fSMatthias Ringwald state = TC_W4_SCAN_RESULT; 274bcf00d8fSMatthias Ringwald gap_set_scan_parameters(0,0x0030, 0x0030); 275bcf00d8fSMatthias Ringwald gap_start_scan(); 276bcf00d8fSMatthias Ringwald break; 277045013feSMatthias Ringwald case GAP_EVENT_ADVERTISING_REPORT: 278bcf00d8fSMatthias Ringwald if (state != TC_W4_SCAN_RESULT) return; 279bcf00d8fSMatthias Ringwald fill_advertising_report_from_packet(&report, packet); 2803ee82ab1SMilanka Ringwald 2810f09bd96SMilanka Ringwald if (blacklist_contains(report.address)) { 2820f09bd96SMilanka Ringwald break; 2830f09bd96SMilanka Ringwald } 2840f09bd96SMilanka Ringwald dump_advertising_report(&report); 285bcf00d8fSMatthias Ringwald // stop scanning, and connect to the device 286bcf00d8fSMatthias Ringwald state = TC_W4_CONNECT; 287bcf00d8fSMatthias Ringwald gap_stop_scan(); 2880f09bd96SMilanka Ringwald printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(report.address)); 289bcf00d8fSMatthias Ringwald gap_connect(report.address,report.address_type); 2900f09bd96SMilanka Ringwald 291bcf00d8fSMatthias Ringwald break; 292bcf00d8fSMatthias Ringwald case HCI_EVENT_LE_META: 293bcf00d8fSMatthias Ringwald // wait for connection complete 29410cad102SMilanka Ringwald if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 295bcf00d8fSMatthias Ringwald if (state != TC_W4_CONNECT) return; 296a59bfbf7SMatthias Ringwald connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 297bcf00d8fSMatthias Ringwald // initialize gatt client context with handle, and add it to the list of active clients 298bcf00d8fSMatthias Ringwald // query primary services 2998e14b3b1SMatthias Ringwald printf("\nSearch for battery service.\n"); 300bcf00d8fSMatthias Ringwald state = TC_W4_SERVICE_RESULT; 301a59bfbf7SMatthias Ringwald gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, battery_service_uuid); 302bcf00d8fSMatthias Ringwald break; 303bcf00d8fSMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 30426687d14SMatthias Ringwald // unregister listener 30526687d14SMatthias Ringwald connection_handle = HCI_CON_HANDLE_INVALID; 30626687d14SMatthias Ringwald if (listener_registered){ 30726687d14SMatthias Ringwald listener_registered = 0; 30826687d14SMatthias Ringwald gatt_client_stop_listening_for_characteristic_value_updates(¬ification_listener); 30926687d14SMatthias Ringwald } 3100f09bd96SMilanka Ringwald 3110f09bd96SMilanka Ringwald if (cmdline_addr_found){ 3120f09bd96SMilanka Ringwald printf("\nDisconnected %s\n", bd_addr_to_str(cmdline_addr)); 313eb8fc740SMatthias Ringwald return; 3140f09bd96SMilanka Ringwald } 3150f09bd96SMilanka Ringwald 3160f09bd96SMilanka Ringwald printf("\nDisconnected %s\n", bd_addr_to_str(report.address)); 3170f09bd96SMilanka Ringwald printf("Restart scan.\n"); 3180f09bd96SMilanka Ringwald state = TC_W4_SCAN_RESULT; 3190f09bd96SMilanka Ringwald gap_start_scan(); 320bcf00d8fSMatthias Ringwald break; 321bcf00d8fSMatthias Ringwald default: 322bcf00d8fSMatthias Ringwald break; 323bcf00d8fSMatthias Ringwald } 324bcf00d8fSMatthias Ringwald } 325bcf00d8fSMatthias Ringwald 3267ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 327bcf00d8fSMatthias Ringwald static void usage(const char *name){ 328bcf00d8fSMatthias Ringwald fprintf(stderr, "\nUsage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name); 329bcf00d8fSMatthias Ringwald 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"); 330bcf00d8fSMatthias Ringwald } 331eb8fc740SMatthias Ringwald #endif 332bcf00d8fSMatthias Ringwald 333bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 334bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 335bcf00d8fSMatthias Ringwald 3367ea7688aSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 337bcf00d8fSMatthias Ringwald int arg = 1; 338bcf00d8fSMatthias Ringwald cmdline_addr_found = 0; 339bcf00d8fSMatthias Ringwald 340bcf00d8fSMatthias Ringwald while (arg < argc) { 341bcf00d8fSMatthias Ringwald if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ 342bcf00d8fSMatthias Ringwald arg++; 343a6efb919SMatthias Ringwald cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr); 344bcf00d8fSMatthias Ringwald arg++; 3450f09bd96SMilanka Ringwald if (!cmdline_addr_found) exit(1); 346bcf00d8fSMatthias Ringwald continue; 347bcf00d8fSMatthias Ringwald } 348bcf00d8fSMatthias Ringwald usage(argv[0]); 349bcf00d8fSMatthias Ringwald return 0; 350bcf00d8fSMatthias Ringwald } 351eb8fc740SMatthias Ringwald #else 3526316c8edSMatthias Ringwald (void)argc; 3536316c8edSMatthias Ringwald (void)argv; 354eb8fc740SMatthias Ringwald #endif 355bcf00d8fSMatthias Ringwald l2cap_init(); 356bcf00d8fSMatthias Ringwald 357add9769eSMatthias Ringwald // setup ATT server - only needed if LE Peripheral does ATT queries on its own, e.g. Android phones 358add9769eSMatthias Ringwald att_server_init(profile_data, NULL, NULL); 359add9769eSMatthias Ringwald 360add9769eSMatthias Ringwald // GATT Client setup 361bcf00d8fSMatthias Ringwald gatt_client_init(); 362bcf00d8fSMatthias Ringwald 363bcf00d8fSMatthias Ringwald sm_init(); 364bcf00d8fSMatthias Ringwald sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 365bcf00d8fSMatthias Ringwald 366a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &hci_event_handler; 367a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 368a4fe6467SMatthias Ringwald 369a4fe6467SMatthias Ringwald 370bcf00d8fSMatthias Ringwald // turn on! 371bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 372bcf00d8fSMatthias Ringwald 373bcf00d8fSMatthias Ringwald return 0; 374bcf00d8fSMatthias Ringwald } 375bcf00d8fSMatthias Ringwald 376*ec8ae085SMilanka Ringwald /* EXAMPLE_END */ 377bcf00d8fSMatthias Ringwald 378bcf00d8fSMatthias Ringwald 379