16104991bSMatthias Ringwald /* 26104991bSMatthias Ringwald * Copyright (C) 2018 BlueKitchen GmbH 36104991bSMatthias Ringwald * 46104991bSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 56104991bSMatthias Ringwald * modification, are permitted provided that the following conditions 66104991bSMatthias Ringwald * are met: 76104991bSMatthias Ringwald * 86104991bSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 96104991bSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 106104991bSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 116104991bSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 126104991bSMatthias Ringwald * documentation and/or other materials provided with the distribution. 136104991bSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 146104991bSMatthias Ringwald * contributors may be used to endorse or promote products derived 156104991bSMatthias Ringwald * from this software without specific prior written permission. 166104991bSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 176104991bSMatthias Ringwald * personal benefit and not for any commercial purpose or for 186104991bSMatthias Ringwald * monetary gain. 196104991bSMatthias Ringwald * 206104991bSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 216104991bSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 226104991bSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 236104991bSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 246104991bSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 256104991bSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 266104991bSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 276104991bSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 286104991bSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 296104991bSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 306104991bSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 316104991bSMatthias Ringwald * SUCH DAMAGE. 326104991bSMatthias Ringwald * 336104991bSMatthias Ringwald * Please inquire about commercial licensing options at 346104991bSMatthias Ringwald * [email protected] 356104991bSMatthias Ringwald * 366104991bSMatthias Ringwald */ 376104991bSMatthias Ringwald 386104991bSMatthias Ringwald #define __BTSTACK_FILE__ "gatt_heart_rate_client.c" 396104991bSMatthias Ringwald 406104991bSMatthias Ringwald // ***************************************************************************** 416104991bSMatthias Ringwald /* EXAMPLE_START(gatt_heart_rate_client): Connects for Heart Rate Sensor and reports measurements */ 426104991bSMatthias Ringwald // ***************************************************************************** 436104991bSMatthias Ringwald 446104991bSMatthias Ringwald #include <inttypes.h> 456104991bSMatthias Ringwald #include <stdint.h> 466104991bSMatthias Ringwald #include <stdio.h> 476104991bSMatthias Ringwald #include <stdlib.h> 486104991bSMatthias Ringwald #include <string.h> 496104991bSMatthias Ringwald 506104991bSMatthias Ringwald #include "btstack.h" 516104991bSMatthias Ringwald 526104991bSMatthias Ringwald // prototypes 536104991bSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 546104991bSMatthias Ringwald 556104991bSMatthias Ringwald typedef enum { 566104991bSMatthias Ringwald TC_OFF, 576104991bSMatthias Ringwald TC_IDLE, 586104991bSMatthias Ringwald TC_W4_SCAN_RESULT, 596104991bSMatthias Ringwald TC_W4_CONNECT, 606104991bSMatthias Ringwald TC_W4_SERVICE_RESULT, 616104991bSMatthias Ringwald TC_W4_HEART_RATE_MEASUREMENT_CHARACTERISTIC, 626104991bSMatthias Ringwald TC_W4_ENABLE_NOTIFICATIONS_COMPLETE, 636104991bSMatthias Ringwald TC_W4_SENSOR_LOCATION_CHARACTERISTIC, 646104991bSMatthias Ringwald TC_W4_SENSOR_LOCATION, 656104991bSMatthias Ringwald TC_CONNECTED 666104991bSMatthias Ringwald } gc_state_t; 676104991bSMatthias Ringwald 686104991bSMatthias Ringwald static const char * sensor_contact_string[] = { 696104991bSMatthias Ringwald "not supported", 706104991bSMatthias Ringwald "not supported", 716104991bSMatthias Ringwald "no/poor contact", 726104991bSMatthias Ringwald "good contact" 736104991bSMatthias Ringwald }; 746104991bSMatthias Ringwald 75c30af2ffSMatthias Ringwald static bd_addr_t cmdline_addr; 766104991bSMatthias Ringwald static int cmdline_addr_found = 0; 776104991bSMatthias Ringwald 786104991bSMatthias Ringwald // addr and type of device with correct name 796104991bSMatthias Ringwald static bd_addr_t le_streamer_addr; 806104991bSMatthias Ringwald static bd_addr_type_t le_streamer_addr_type; 816104991bSMatthias Ringwald 826104991bSMatthias Ringwald static hci_con_handle_t connection_handle; 836104991bSMatthias Ringwald 846104991bSMatthias Ringwald static gatt_client_service_t heart_rate_service; 856104991bSMatthias Ringwald static gatt_client_characteristic_t body_sensor_location_characteristic; 866104991bSMatthias Ringwald static gatt_client_characteristic_t heart_rate_measurement_characteristic; 876104991bSMatthias Ringwald 886104991bSMatthias Ringwald static uint8_t body_sensor_location; 896104991bSMatthias Ringwald 906104991bSMatthias Ringwald static gatt_client_notification_t notification_listener; 916104991bSMatthias Ringwald static int listener_registered; 926104991bSMatthias Ringwald 936104991bSMatthias Ringwald static gc_state_t state = TC_OFF; 946104991bSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 956104991bSMatthias Ringwald 966104991bSMatthias Ringwald 976104991bSMatthias Ringwald // returns 1 if name is found in advertisement 986104991bSMatthias Ringwald static int advertisement_report_contains_uuid16(uint16_t uuid16, uint8_t * advertisement_report){ 996104991bSMatthias Ringwald // get advertisement from report event 1006104991bSMatthias Ringwald const uint8_t * adv_data = gap_event_advertising_report_get_data(advertisement_report); 1016104991bSMatthias Ringwald uint16_t adv_len = gap_event_advertising_report_get_data_length(advertisement_report); 1026104991bSMatthias Ringwald 1036104991bSMatthias Ringwald // iterate over advertisement data 1046104991bSMatthias Ringwald ad_context_t context; 1056104991bSMatthias Ringwald int found = 0; 1066104991bSMatthias Ringwald for (ad_iterator_init(&context, adv_len, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 1076104991bSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 1086104991bSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 1096104991bSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 1106104991bSMatthias Ringwald int i; 1116104991bSMatthias Ringwald switch (data_type){ 1126104991bSMatthias Ringwald case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS: 1136104991bSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS: 1146104991bSMatthias Ringwald // compare common prefix 1156104991bSMatthias Ringwald for (i=0; i<data_size;i+=2){ 1166104991bSMatthias Ringwald if (little_endian_read_16(data, i) == uuid16) { 1176104991bSMatthias Ringwald found = 1; 1186104991bSMatthias Ringwald break; 1196104991bSMatthias Ringwald } 1206104991bSMatthias Ringwald } 1216104991bSMatthias Ringwald break; 1226104991bSMatthias Ringwald default: 1236104991bSMatthias Ringwald break; 1246104991bSMatthias Ringwald } 1256104991bSMatthias Ringwald } 1266104991bSMatthias Ringwald return found; 1276104991bSMatthias Ringwald } 1286104991bSMatthias Ringwald 1296104991bSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1306104991bSMatthias Ringwald UNUSED(packet_type); 1316104991bSMatthias Ringwald UNUSED(channel); 1326104991bSMatthias Ringwald UNUSED(size); 1336104991bSMatthias Ringwald 1346104991bSMatthias Ringwald uint16_t heart_rate; 1356104991bSMatthias Ringwald uint8_t sensor_contact; 1366104991bSMatthias Ringwald 1376104991bSMatthias Ringwald switch(state){ 1386104991bSMatthias Ringwald case TC_W4_SERVICE_RESULT: 1396104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1406104991bSMatthias Ringwald case GATT_EVENT_SERVICE_QUERY_RESULT: 1416104991bSMatthias Ringwald // store service (we expect only one) 1426104991bSMatthias Ringwald gatt_event_service_query_result_get_service(packet, &heart_rate_service); 1436104991bSMatthias Ringwald break; 1446104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 1456104991bSMatthias Ringwald if (packet[4] != 0){ 1466104991bSMatthias Ringwald printf("SERVICE_QUERY_RESULT - Error status %x.\n", packet[4]); 1476104991bSMatthias Ringwald gap_disconnect(connection_handle); 1486104991bSMatthias Ringwald break; 1496104991bSMatthias Ringwald } 1506104991bSMatthias Ringwald state = TC_W4_HEART_RATE_MEASUREMENT_CHARACTERISTIC; 1516104991bSMatthias Ringwald printf("Search for Heart Rate Measurement characteristic.\n"); 1526104991bSMatthias Ringwald gatt_client_discover_characteristics_for_service_by_uuid16(handle_gatt_client_event, connection_handle, &heart_rate_service, ORG_BLUETOOTH_CHARACTERISTIC_HEART_RATE_MEASUREMENT); 1536104991bSMatthias Ringwald break; 1546104991bSMatthias Ringwald default: 1556104991bSMatthias Ringwald break; 1566104991bSMatthias Ringwald } 1576104991bSMatthias Ringwald break; 1586104991bSMatthias Ringwald 1596104991bSMatthias Ringwald case TC_W4_HEART_RATE_MEASUREMENT_CHARACTERISTIC: 1606104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1616104991bSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 1626104991bSMatthias Ringwald gatt_event_characteristic_query_result_get_characteristic(packet, &heart_rate_measurement_characteristic); 1636104991bSMatthias Ringwald break; 1646104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 1656104991bSMatthias Ringwald if (packet[4] != 0){ 1666104991bSMatthias Ringwald printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]); 1676104991bSMatthias Ringwald gap_disconnect(connection_handle); 1686104991bSMatthias Ringwald break; 1696104991bSMatthias Ringwald } 1706104991bSMatthias Ringwald // register handler for notifications 1716104991bSMatthias Ringwald listener_registered = 1; 1726104991bSMatthias Ringwald gatt_client_listen_for_characteristic_value_updates(¬ification_listener, handle_gatt_client_event, connection_handle, &heart_rate_measurement_characteristic); 1736104991bSMatthias Ringwald // enable notifications 1746104991bSMatthias Ringwald printf("Enable Notify on Heart Rate Measurements characteristic.\n"); 1756104991bSMatthias Ringwald state = TC_W4_ENABLE_NOTIFICATIONS_COMPLETE; 1766104991bSMatthias Ringwald gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, 1776104991bSMatthias Ringwald &heart_rate_measurement_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 1786104991bSMatthias Ringwald break; 1796104991bSMatthias Ringwald default: 1806104991bSMatthias Ringwald break; 1816104991bSMatthias Ringwald } 1826104991bSMatthias Ringwald break; 1836104991bSMatthias Ringwald 1846104991bSMatthias Ringwald case TC_W4_ENABLE_NOTIFICATIONS_COMPLETE: 1856104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1866104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 1876104991bSMatthias Ringwald printf("Notifications enabled, status %02x\n", gatt_event_query_complete_get_status(packet)); 1886104991bSMatthias Ringwald 1896104991bSMatthias Ringwald state = TC_W4_SENSOR_LOCATION_CHARACTERISTIC; 1906104991bSMatthias Ringwald printf("Search for Sensor Location characteristic.\n"); 1916104991bSMatthias Ringwald gatt_client_discover_characteristics_for_service_by_uuid16(handle_gatt_client_event, connection_handle, &heart_rate_service, ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION); 1926104991bSMatthias Ringwald break; 1936104991bSMatthias Ringwald default: 1946104991bSMatthias Ringwald break; 1956104991bSMatthias Ringwald } 1966104991bSMatthias Ringwald break; 1976104991bSMatthias Ringwald 1986104991bSMatthias Ringwald 1996104991bSMatthias Ringwald case TC_W4_SENSOR_LOCATION_CHARACTERISTIC: 2006104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2016104991bSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 2026104991bSMatthias Ringwald gatt_event_characteristic_query_result_get_characteristic(packet, &body_sensor_location_characteristic); 2036104991bSMatthias Ringwald break; 2046104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 2056104991bSMatthias Ringwald if (packet[4] != 0){ 2066104991bSMatthias Ringwald printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]); 2076104991bSMatthias Ringwald state = TC_CONNECTED; 2086104991bSMatthias Ringwald break; 2096104991bSMatthias Ringwald } 2106104991bSMatthias Ringwald state = TC_W4_HEART_RATE_MEASUREMENT_CHARACTERISTIC; 2116104991bSMatthias Ringwald printf("Read Body Sensor Location.\n"); 2126104991bSMatthias Ringwald state = TC_W4_SENSOR_LOCATION; 2136104991bSMatthias Ringwald gatt_client_read_value_of_characteristic(handle_gatt_client_event, connection_handle, &body_sensor_location_characteristic); 2146104991bSMatthias Ringwald break; 2156104991bSMatthias Ringwald default: 2166104991bSMatthias Ringwald break; 2176104991bSMatthias Ringwald } 2186104991bSMatthias Ringwald break; 2196104991bSMatthias Ringwald 2206104991bSMatthias Ringwald 2216104991bSMatthias Ringwald case TC_W4_SENSOR_LOCATION: 2226104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2236104991bSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 2246104991bSMatthias Ringwald body_sensor_location = gatt_event_characteristic_value_query_result_get_value(packet)[0]; 2256104991bSMatthias Ringwald printf("Sensor Location: %u\n", body_sensor_location); 2266104991bSMatthias Ringwald state = TC_CONNECTED; 2276104991bSMatthias Ringwald break; 2286104991bSMatthias Ringwald default: 2296104991bSMatthias Ringwald break; 2306104991bSMatthias Ringwald } 2316104991bSMatthias Ringwald break; 2326104991bSMatthias Ringwald 2336104991bSMatthias Ringwald case TC_CONNECTED: 2346104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2356104991bSMatthias Ringwald case GATT_EVENT_NOTIFICATION: 2366104991bSMatthias Ringwald if (gatt_event_notification_get_value(packet)[0] & 1){ 2376104991bSMatthias Ringwald heart_rate = little_endian_read_16(gatt_event_notification_get_value(packet), 1); 2386104991bSMatthias Ringwald } else { 2396104991bSMatthias Ringwald heart_rate = gatt_event_notification_get_value(packet)[1]; 2406104991bSMatthias Ringwald } 2416104991bSMatthias Ringwald sensor_contact = (gatt_event_notification_get_value(packet)[0] >> 1) & 3; 2426104991bSMatthias Ringwald printf("Heart Rate: %3u, Sensor Contact: %s\n", heart_rate, sensor_contact_string[sensor_contact]); 2436104991bSMatthias Ringwald break; 2446104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 2456104991bSMatthias Ringwald break; 2466104991bSMatthias Ringwald case GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE: 2476104991bSMatthias Ringwald break; 2486104991bSMatthias Ringwald default: 2496104991bSMatthias Ringwald break; 2506104991bSMatthias Ringwald } 2516104991bSMatthias Ringwald break; 2526104991bSMatthias Ringwald 2536104991bSMatthias Ringwald default: 2546104991bSMatthias Ringwald break; 2556104991bSMatthias Ringwald } 2566104991bSMatthias Ringwald } 2576104991bSMatthias Ringwald 2586104991bSMatthias Ringwald // Either connect to remote specified on command line or start scan for device with Hear Rate Service in advertisement 2596104991bSMatthias Ringwald static void gatt_heart_rate_client_start(void){ 2606104991bSMatthias Ringwald if (cmdline_addr_found){ 2616104991bSMatthias Ringwald printf("Connect to %s\n", bd_addr_to_str(cmdline_addr)); 2626104991bSMatthias Ringwald state = TC_W4_CONNECT; 2636104991bSMatthias Ringwald gap_connect(cmdline_addr, 0); 2646104991bSMatthias Ringwald } else { 2656104991bSMatthias Ringwald printf("Start scanning!\n"); 2666104991bSMatthias Ringwald state = TC_W4_SCAN_RESULT; 2676104991bSMatthias Ringwald gap_set_scan_parameters(0,0x0030, 0x0030); 2686104991bSMatthias Ringwald gap_start_scan(); 2696104991bSMatthias Ringwald } 2706104991bSMatthias Ringwald } 2716104991bSMatthias Ringwald 2726104991bSMatthias Ringwald static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2736104991bSMatthias Ringwald UNUSED(channel); 2746104991bSMatthias Ringwald UNUSED(size); 2756104991bSMatthias Ringwald 2766104991bSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 2776104991bSMatthias Ringwald 2786104991bSMatthias Ringwald uint16_t conn_interval; 2796104991bSMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet); 2806104991bSMatthias Ringwald switch (event) { 2816104991bSMatthias Ringwald case BTSTACK_EVENT_STATE: 2826104991bSMatthias Ringwald // BTstack activated, get started 2836104991bSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) { 2846104991bSMatthias Ringwald gatt_heart_rate_client_start(); 2856104991bSMatthias Ringwald } else { 2866104991bSMatthias Ringwald state = TC_OFF; 2876104991bSMatthias Ringwald } 2886104991bSMatthias Ringwald break; 2896104991bSMatthias Ringwald case GAP_EVENT_ADVERTISING_REPORT: 2906104991bSMatthias Ringwald if (state != TC_W4_SCAN_RESULT) return; 2916104991bSMatthias Ringwald // check name in advertisement 2926104991bSMatthias Ringwald if (!advertisement_report_contains_uuid16(ORG_BLUETOOTH_SERVICE_HEART_RATE, packet)) return; 2936104991bSMatthias Ringwald // store address and type 2946104991bSMatthias Ringwald gap_event_advertising_report_get_address(packet, le_streamer_addr); 2956104991bSMatthias Ringwald le_streamer_addr_type = gap_event_advertising_report_get_address_type(packet); 2966104991bSMatthias Ringwald // stop scanning, and connect to the device 2976104991bSMatthias Ringwald state = TC_W4_CONNECT; 2986104991bSMatthias Ringwald gap_stop_scan(); 2996104991bSMatthias Ringwald printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(le_streamer_addr)); 3006104991bSMatthias Ringwald gap_connect(le_streamer_addr,le_streamer_addr_type); 3016104991bSMatthias Ringwald break; 3026104991bSMatthias Ringwald case HCI_EVENT_LE_META: 3036104991bSMatthias Ringwald // wait for connection complete 3046104991bSMatthias Ringwald if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 3056104991bSMatthias Ringwald if (state != TC_W4_CONNECT) return; 3066104991bSMatthias Ringwald connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 3076104991bSMatthias Ringwald // print connection parameters (without using float operations) 3086104991bSMatthias Ringwald conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 3096104991bSMatthias Ringwald printf("Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3)); 3106104991bSMatthias Ringwald printf("Connection Latency: %u\n", hci_subevent_le_connection_complete_get_conn_latency(packet)); 3116104991bSMatthias Ringwald // initialize gatt client context with handle, and add it to the list of active clients 3126104991bSMatthias Ringwald // query primary services 3136104991bSMatthias Ringwald printf("Search for Heart Rate service.\n"); 3146104991bSMatthias Ringwald state = TC_W4_SERVICE_RESULT; 3156104991bSMatthias Ringwald gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, ORG_BLUETOOTH_SERVICE_HEART_RATE); 3166104991bSMatthias Ringwald break; 3176104991bSMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 3186104991bSMatthias Ringwald // unregister listener 3196104991bSMatthias Ringwald connection_handle = HCI_CON_HANDLE_INVALID; 3206104991bSMatthias Ringwald if (listener_registered){ 3216104991bSMatthias Ringwald listener_registered = 0; 3226104991bSMatthias Ringwald gatt_client_stop_listening_for_characteristic_value_updates(¬ification_listener); 3236104991bSMatthias Ringwald } 3246104991bSMatthias Ringwald if (cmdline_addr_found){ 3256104991bSMatthias Ringwald printf("Disconnected %s\n", bd_addr_to_str(cmdline_addr)); 3266104991bSMatthias Ringwald return; 3276104991bSMatthias Ringwald } 3286104991bSMatthias Ringwald printf("Disconnected %s\n", bd_addr_to_str(le_streamer_addr)); 3296104991bSMatthias Ringwald if (state == TC_OFF) break; 3306104991bSMatthias Ringwald gatt_heart_rate_client_start(); 3316104991bSMatthias Ringwald break; 3326104991bSMatthias Ringwald default: 3336104991bSMatthias Ringwald break; 3346104991bSMatthias Ringwald } 3356104991bSMatthias Ringwald } 3366104991bSMatthias Ringwald 3376104991bSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 3386104991bSMatthias Ringwald static void usage(const char *name){ 3396104991bSMatthias Ringwald fprintf(stderr, "Usage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name); 3406104991bSMatthias Ringwald fprintf(stderr, "If no argument is provided, GATT Heart Rate Client will start scanning and connect to the first device named 'LE Streamer'.\n"); 3416104991bSMatthias Ringwald fprintf(stderr, "To connect to a specific device use argument [-a].\n\n"); 3426104991bSMatthias Ringwald } 3436104991bSMatthias Ringwald #endif 3446104991bSMatthias Ringwald 3456104991bSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 3466104991bSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 3476104991bSMatthias Ringwald 3486104991bSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 3496104991bSMatthias Ringwald int arg = 1; 3506104991bSMatthias Ringwald cmdline_addr_found = 0; 3516104991bSMatthias Ringwald 3526104991bSMatthias Ringwald while (arg < argc) { 3536104991bSMatthias Ringwald if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ 3546104991bSMatthias Ringwald arg++; 3556104991bSMatthias Ringwald cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr); 3566104991bSMatthias Ringwald arg++; 3576104991bSMatthias Ringwald if (!cmdline_addr_found) exit(1); 3586104991bSMatthias Ringwald continue; 3596104991bSMatthias Ringwald } 3606104991bSMatthias Ringwald usage(argv[0]); 3616104991bSMatthias Ringwald return 0; 3626104991bSMatthias Ringwald } 3636104991bSMatthias Ringwald #else 3646104991bSMatthias Ringwald (void)argc; 3656104991bSMatthias Ringwald (void)argv; 3666104991bSMatthias Ringwald #endif 3676104991bSMatthias Ringwald l2cap_init(); 3686104991bSMatthias Ringwald 3696104991bSMatthias Ringwald gatt_client_init(); 3706104991bSMatthias Ringwald 3716104991bSMatthias Ringwald sm_init(); 3726104991bSMatthias Ringwald sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 3736104991bSMatthias Ringwald 374*a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &hci_event_handler; 375*a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 376*a4fe6467SMatthias Ringwald 3776104991bSMatthias Ringwald // turn on! 3786104991bSMatthias Ringwald hci_power_control(HCI_POWER_ON); 3796104991bSMatthias Ringwald 3806104991bSMatthias Ringwald return 0; 3816104991bSMatthias Ringwald } 3826104991bSMatthias Ringwald /* EXAMPLE_END */ 383