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 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "gatt_heart_rate_client.c" 396104991bSMatthias Ringwald 406104991bSMatthias Ringwald // ***************************************************************************** 41ec8ae085SMilanka Ringwald /* EXAMPLE_START(gatt_heart_rate_client): GATT Heart Rate Sensor Client 42ec8ae085SMilanka Ringwald * 43ec8ae085SMilanka Ringwald * @text Connects for Heart Rate Sensor and reports measurements. 44ec8ae085SMilanka Ringwald */ 456104991bSMatthias Ringwald // ***************************************************************************** 466104991bSMatthias Ringwald 476104991bSMatthias Ringwald #include <inttypes.h> 486104991bSMatthias Ringwald #include <stdint.h> 496104991bSMatthias Ringwald #include <stdio.h> 506104991bSMatthias Ringwald #include <stdlib.h> 516104991bSMatthias Ringwald #include <string.h> 526104991bSMatthias Ringwald 536104991bSMatthias Ringwald #include "btstack.h" 546104991bSMatthias Ringwald 556104991bSMatthias Ringwald // prototypes 566104991bSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 576104991bSMatthias Ringwald 586104991bSMatthias Ringwald typedef enum { 596104991bSMatthias Ringwald TC_OFF, 606104991bSMatthias Ringwald TC_IDLE, 616104991bSMatthias Ringwald TC_W4_SCAN_RESULT, 626104991bSMatthias Ringwald TC_W4_CONNECT, 636104991bSMatthias Ringwald TC_W4_SERVICE_RESULT, 646104991bSMatthias Ringwald TC_W4_HEART_RATE_MEASUREMENT_CHARACTERISTIC, 656104991bSMatthias Ringwald TC_W4_ENABLE_NOTIFICATIONS_COMPLETE, 666104991bSMatthias Ringwald TC_W4_SENSOR_LOCATION_CHARACTERISTIC, 676104991bSMatthias Ringwald TC_W4_SENSOR_LOCATION, 686104991bSMatthias Ringwald TC_CONNECTED 696104991bSMatthias Ringwald } gc_state_t; 706104991bSMatthias Ringwald 716104991bSMatthias Ringwald static const char * sensor_contact_string[] = { 726104991bSMatthias Ringwald "not supported", 736104991bSMatthias Ringwald "not supported", 746104991bSMatthias Ringwald "no/poor contact", 756104991bSMatthias Ringwald "good contact" 766104991bSMatthias Ringwald }; 776104991bSMatthias Ringwald 78c30af2ffSMatthias Ringwald static bd_addr_t cmdline_addr; 796104991bSMatthias Ringwald static int cmdline_addr_found = 0; 806104991bSMatthias Ringwald 816104991bSMatthias Ringwald // addr and type of device with correct name 826104991bSMatthias Ringwald static bd_addr_t le_streamer_addr; 836104991bSMatthias Ringwald static bd_addr_type_t le_streamer_addr_type; 846104991bSMatthias Ringwald 856104991bSMatthias Ringwald static hci_con_handle_t connection_handle; 866104991bSMatthias Ringwald 876104991bSMatthias Ringwald static gatt_client_service_t heart_rate_service; 886104991bSMatthias Ringwald static gatt_client_characteristic_t body_sensor_location_characteristic; 896104991bSMatthias Ringwald static gatt_client_characteristic_t heart_rate_measurement_characteristic; 906104991bSMatthias Ringwald 916104991bSMatthias Ringwald static uint8_t body_sensor_location; 926104991bSMatthias Ringwald 936104991bSMatthias Ringwald static gatt_client_notification_t notification_listener; 946104991bSMatthias Ringwald static int listener_registered; 956104991bSMatthias Ringwald 966104991bSMatthias Ringwald static gc_state_t state = TC_OFF; 976104991bSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 986104991bSMatthias Ringwald 996104991bSMatthias Ringwald 1006104991bSMatthias Ringwald // returns 1 if name is found in advertisement 1016104991bSMatthias Ringwald static int advertisement_report_contains_uuid16(uint16_t uuid16, uint8_t * advertisement_report){ 1026104991bSMatthias Ringwald // get advertisement from report event 1036104991bSMatthias Ringwald const uint8_t * adv_data = gap_event_advertising_report_get_data(advertisement_report); 1046104991bSMatthias Ringwald uint16_t adv_len = gap_event_advertising_report_get_data_length(advertisement_report); 1056104991bSMatthias Ringwald 1066104991bSMatthias Ringwald // iterate over advertisement data 1076104991bSMatthias Ringwald ad_context_t context; 1086104991bSMatthias Ringwald int found = 0; 1096104991bSMatthias Ringwald for (ad_iterator_init(&context, adv_len, adv_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 1106104991bSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 1116104991bSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 1126104991bSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 1136104991bSMatthias Ringwald int i; 1146104991bSMatthias Ringwald switch (data_type){ 1156104991bSMatthias Ringwald case BLUETOOTH_DATA_TYPE_INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS: 1166104991bSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS: 1176104991bSMatthias Ringwald // compare common prefix 1186104991bSMatthias Ringwald for (i=0; i<data_size;i+=2){ 1196104991bSMatthias Ringwald if (little_endian_read_16(data, i) == uuid16) { 1206104991bSMatthias Ringwald found = 1; 1216104991bSMatthias Ringwald break; 1226104991bSMatthias Ringwald } 1236104991bSMatthias Ringwald } 1246104991bSMatthias Ringwald break; 1256104991bSMatthias Ringwald default: 1266104991bSMatthias Ringwald break; 1276104991bSMatthias Ringwald } 1286104991bSMatthias Ringwald } 1296104991bSMatthias Ringwald return found; 1306104991bSMatthias Ringwald } 1316104991bSMatthias Ringwald 1326104991bSMatthias Ringwald static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1336104991bSMatthias Ringwald UNUSED(packet_type); 1346104991bSMatthias Ringwald UNUSED(channel); 1356104991bSMatthias Ringwald UNUSED(size); 1366104991bSMatthias Ringwald 1376104991bSMatthias Ringwald uint16_t heart_rate; 1386104991bSMatthias Ringwald uint8_t sensor_contact; 139*6058cb0dSMatthias Ringwald uint8_t att_status; 1406104991bSMatthias Ringwald 1416104991bSMatthias Ringwald switch(state){ 1426104991bSMatthias Ringwald case TC_W4_SERVICE_RESULT: 1436104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1446104991bSMatthias Ringwald case GATT_EVENT_SERVICE_QUERY_RESULT: 1456104991bSMatthias Ringwald // store service (we expect only one) 1466104991bSMatthias Ringwald gatt_event_service_query_result_get_service(packet, &heart_rate_service); 1476104991bSMatthias Ringwald break; 1486104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 149*6058cb0dSMatthias Ringwald att_status = gatt_event_query_complete_get_att_status(packet); 150*6058cb0dSMatthias Ringwald if (att_status != ATT_ERROR_SUCCESS){ 151*6058cb0dSMatthias Ringwald printf("SERVICE_QUERY_RESULT - Error status %x.\n", att_status); 1526104991bSMatthias Ringwald gap_disconnect(connection_handle); 1536104991bSMatthias Ringwald break; 1546104991bSMatthias Ringwald } 1556104991bSMatthias Ringwald state = TC_W4_HEART_RATE_MEASUREMENT_CHARACTERISTIC; 1566104991bSMatthias Ringwald printf("Search for Heart Rate Measurement characteristic.\n"); 1576104991bSMatthias Ringwald gatt_client_discover_characteristics_for_service_by_uuid16(handle_gatt_client_event, connection_handle, &heart_rate_service, ORG_BLUETOOTH_CHARACTERISTIC_HEART_RATE_MEASUREMENT); 1586104991bSMatthias Ringwald break; 1596104991bSMatthias Ringwald default: 1606104991bSMatthias Ringwald break; 1616104991bSMatthias Ringwald } 1626104991bSMatthias Ringwald break; 1636104991bSMatthias Ringwald 1646104991bSMatthias Ringwald case TC_W4_HEART_RATE_MEASUREMENT_CHARACTERISTIC: 1656104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1666104991bSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 1676104991bSMatthias Ringwald gatt_event_characteristic_query_result_get_characteristic(packet, &heart_rate_measurement_characteristic); 1686104991bSMatthias Ringwald break; 1696104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 170*6058cb0dSMatthias Ringwald att_status = gatt_event_query_complete_get_att_status(packet); 171*6058cb0dSMatthias Ringwald if (att_status != ATT_ERROR_SUCCESS){ 172*6058cb0dSMatthias Ringwald printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", att_status); 1736104991bSMatthias Ringwald gap_disconnect(connection_handle); 1746104991bSMatthias Ringwald break; 1756104991bSMatthias Ringwald } 1766104991bSMatthias Ringwald // register handler for notifications 1776104991bSMatthias Ringwald listener_registered = 1; 1786104991bSMatthias Ringwald gatt_client_listen_for_characteristic_value_updates(¬ification_listener, handle_gatt_client_event, connection_handle, &heart_rate_measurement_characteristic); 1796104991bSMatthias Ringwald // enable notifications 1806104991bSMatthias Ringwald printf("Enable Notify on Heart Rate Measurements characteristic.\n"); 1816104991bSMatthias Ringwald state = TC_W4_ENABLE_NOTIFICATIONS_COMPLETE; 1826104991bSMatthias Ringwald gatt_client_write_client_characteristic_configuration(handle_gatt_client_event, connection_handle, 1836104991bSMatthias Ringwald &heart_rate_measurement_characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 1846104991bSMatthias Ringwald break; 1856104991bSMatthias Ringwald default: 1866104991bSMatthias Ringwald break; 1876104991bSMatthias Ringwald } 1886104991bSMatthias Ringwald break; 1896104991bSMatthias Ringwald 1906104991bSMatthias Ringwald case TC_W4_ENABLE_NOTIFICATIONS_COMPLETE: 1916104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1926104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 1939cb80b17SMilanka Ringwald printf("Notifications enabled, ATT status %02x\n", gatt_event_query_complete_get_att_status(packet)); 1946104991bSMatthias Ringwald 1956104991bSMatthias Ringwald state = TC_W4_SENSOR_LOCATION_CHARACTERISTIC; 1966104991bSMatthias Ringwald printf("Search for Sensor Location characteristic.\n"); 1976104991bSMatthias Ringwald gatt_client_discover_characteristics_for_service_by_uuid16(handle_gatt_client_event, connection_handle, &heart_rate_service, ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION); 1986104991bSMatthias Ringwald break; 1996104991bSMatthias Ringwald default: 2006104991bSMatthias Ringwald break; 2016104991bSMatthias Ringwald } 2026104991bSMatthias Ringwald break; 2036104991bSMatthias Ringwald 2046104991bSMatthias Ringwald 2056104991bSMatthias Ringwald case TC_W4_SENSOR_LOCATION_CHARACTERISTIC: 2066104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2076104991bSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 2086104991bSMatthias Ringwald gatt_event_characteristic_query_result_get_characteristic(packet, &body_sensor_location_characteristic); 2096104991bSMatthias Ringwald break; 2106104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 211*6058cb0dSMatthias Ringwald if (gatt_event_query_complete_get_att_status(packet) != ATT_ERROR_SUCCESS){ 2126104991bSMatthias Ringwald printf("CHARACTERISTIC_QUERY_RESULT - Error status %x.\n", packet[4]); 2136104991bSMatthias Ringwald state = TC_CONNECTED; 2146104991bSMatthias Ringwald break; 2156104991bSMatthias Ringwald } 216ddd48cb8SMatthias Ringwald if (body_sensor_location_characteristic.value_handle == 0){ 217ddd48cb8SMatthias Ringwald printf("Sensor Location characteristic not available.\n"); 218ddd48cb8SMatthias Ringwald state = TC_CONNECTED; 219ddd48cb8SMatthias Ringwald break; 220ddd48cb8SMatthias Ringwald } 2216104991bSMatthias Ringwald state = TC_W4_HEART_RATE_MEASUREMENT_CHARACTERISTIC; 2226104991bSMatthias Ringwald printf("Read Body Sensor Location.\n"); 2236104991bSMatthias Ringwald state = TC_W4_SENSOR_LOCATION; 2246104991bSMatthias Ringwald gatt_client_read_value_of_characteristic(handle_gatt_client_event, connection_handle, &body_sensor_location_characteristic); 2256104991bSMatthias Ringwald break; 2266104991bSMatthias Ringwald default: 2276104991bSMatthias Ringwald break; 2286104991bSMatthias Ringwald } 2296104991bSMatthias Ringwald break; 2306104991bSMatthias Ringwald 2316104991bSMatthias Ringwald 2326104991bSMatthias Ringwald case TC_W4_SENSOR_LOCATION: 2336104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2346104991bSMatthias Ringwald case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 2356104991bSMatthias Ringwald body_sensor_location = gatt_event_characteristic_value_query_result_get_value(packet)[0]; 2366104991bSMatthias Ringwald printf("Sensor Location: %u\n", body_sensor_location); 237ddd48cb8SMatthias Ringwald break; 238ddd48cb8SMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 2396104991bSMatthias Ringwald state = TC_CONNECTED; 2406104991bSMatthias Ringwald break; 2416104991bSMatthias Ringwald default: 2426104991bSMatthias Ringwald break; 2436104991bSMatthias Ringwald } 2446104991bSMatthias Ringwald break; 2456104991bSMatthias Ringwald 2466104991bSMatthias Ringwald case TC_CONNECTED: 2476104991bSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 2486104991bSMatthias Ringwald case GATT_EVENT_NOTIFICATION: 2496104991bSMatthias Ringwald if (gatt_event_notification_get_value(packet)[0] & 1){ 2506104991bSMatthias Ringwald heart_rate = little_endian_read_16(gatt_event_notification_get_value(packet), 1); 2516104991bSMatthias Ringwald } else { 2526104991bSMatthias Ringwald heart_rate = gatt_event_notification_get_value(packet)[1]; 2536104991bSMatthias Ringwald } 2546104991bSMatthias Ringwald sensor_contact = (gatt_event_notification_get_value(packet)[0] >> 1) & 3; 2556104991bSMatthias Ringwald printf("Heart Rate: %3u, Sensor Contact: %s\n", heart_rate, sensor_contact_string[sensor_contact]); 2566104991bSMatthias Ringwald break; 2576104991bSMatthias Ringwald case GATT_EVENT_QUERY_COMPLETE: 2586104991bSMatthias Ringwald break; 2596104991bSMatthias Ringwald case GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE: 2606104991bSMatthias Ringwald break; 2616104991bSMatthias Ringwald default: 2626104991bSMatthias Ringwald break; 2636104991bSMatthias Ringwald } 2646104991bSMatthias Ringwald break; 2656104991bSMatthias Ringwald 2666104991bSMatthias Ringwald default: 2676104991bSMatthias Ringwald break; 2686104991bSMatthias Ringwald } 2696104991bSMatthias Ringwald } 2706104991bSMatthias Ringwald 2716104991bSMatthias Ringwald // Either connect to remote specified on command line or start scan for device with Hear Rate Service in advertisement 2726104991bSMatthias Ringwald static void gatt_heart_rate_client_start(void){ 2736104991bSMatthias Ringwald if (cmdline_addr_found){ 2746104991bSMatthias Ringwald printf("Connect to %s\n", bd_addr_to_str(cmdline_addr)); 2756104991bSMatthias Ringwald state = TC_W4_CONNECT; 2766104991bSMatthias Ringwald gap_connect(cmdline_addr, 0); 2776104991bSMatthias Ringwald } else { 2786104991bSMatthias Ringwald printf("Start scanning!\n"); 2796104991bSMatthias Ringwald state = TC_W4_SCAN_RESULT; 2806104991bSMatthias Ringwald gap_set_scan_parameters(0,0x0030, 0x0030); 2816104991bSMatthias Ringwald gap_start_scan(); 2826104991bSMatthias Ringwald } 2836104991bSMatthias Ringwald } 2846104991bSMatthias Ringwald 2856104991bSMatthias Ringwald static void hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2866104991bSMatthias Ringwald UNUSED(channel); 2876104991bSMatthias Ringwald UNUSED(size); 2886104991bSMatthias Ringwald 2896104991bSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 2906104991bSMatthias Ringwald 2916104991bSMatthias Ringwald uint16_t conn_interval; 2926104991bSMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet); 2936104991bSMatthias Ringwald switch (event) { 2946104991bSMatthias Ringwald case BTSTACK_EVENT_STATE: 2956104991bSMatthias Ringwald // BTstack activated, get started 2966104991bSMatthias Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING) { 2976104991bSMatthias Ringwald gatt_heart_rate_client_start(); 2986104991bSMatthias Ringwald } else { 2996104991bSMatthias Ringwald state = TC_OFF; 3006104991bSMatthias Ringwald } 3016104991bSMatthias Ringwald break; 3026104991bSMatthias Ringwald case GAP_EVENT_ADVERTISING_REPORT: 3036104991bSMatthias Ringwald if (state != TC_W4_SCAN_RESULT) return; 3046104991bSMatthias Ringwald // check name in advertisement 3056104991bSMatthias Ringwald if (!advertisement_report_contains_uuid16(ORG_BLUETOOTH_SERVICE_HEART_RATE, packet)) return; 3066104991bSMatthias Ringwald // store address and type 3076104991bSMatthias Ringwald gap_event_advertising_report_get_address(packet, le_streamer_addr); 3086104991bSMatthias Ringwald le_streamer_addr_type = gap_event_advertising_report_get_address_type(packet); 3096104991bSMatthias Ringwald // stop scanning, and connect to the device 3106104991bSMatthias Ringwald state = TC_W4_CONNECT; 3116104991bSMatthias Ringwald gap_stop_scan(); 3126104991bSMatthias Ringwald printf("Stop scan. Connect to device with addr %s.\n", bd_addr_to_str(le_streamer_addr)); 3136104991bSMatthias Ringwald gap_connect(le_streamer_addr,le_streamer_addr_type); 3146104991bSMatthias Ringwald break; 3156104991bSMatthias Ringwald case HCI_EVENT_LE_META: 3166104991bSMatthias Ringwald // wait for connection complete 3176104991bSMatthias Ringwald if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 3186104991bSMatthias Ringwald if (state != TC_W4_CONNECT) return; 3196104991bSMatthias Ringwald connection_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 3206104991bSMatthias Ringwald // print connection parameters (without using float operations) 3216104991bSMatthias Ringwald conn_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 3226104991bSMatthias Ringwald printf("Connection Interval: %u.%02u ms\n", conn_interval * 125 / 100, 25 * (conn_interval & 3)); 3236104991bSMatthias Ringwald printf("Connection Latency: %u\n", hci_subevent_le_connection_complete_get_conn_latency(packet)); 3246104991bSMatthias Ringwald // initialize gatt client context with handle, and add it to the list of active clients 3256104991bSMatthias Ringwald // query primary services 3266104991bSMatthias Ringwald printf("Search for Heart Rate service.\n"); 3276104991bSMatthias Ringwald state = TC_W4_SERVICE_RESULT; 3286104991bSMatthias Ringwald gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, connection_handle, ORG_BLUETOOTH_SERVICE_HEART_RATE); 3296104991bSMatthias Ringwald break; 3306104991bSMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 3316104991bSMatthias Ringwald // unregister listener 3326104991bSMatthias Ringwald connection_handle = HCI_CON_HANDLE_INVALID; 3336104991bSMatthias Ringwald if (listener_registered){ 3346104991bSMatthias Ringwald listener_registered = 0; 3356104991bSMatthias Ringwald gatt_client_stop_listening_for_characteristic_value_updates(¬ification_listener); 3366104991bSMatthias Ringwald } 3376104991bSMatthias Ringwald if (cmdline_addr_found){ 3386104991bSMatthias Ringwald printf("Disconnected %s\n", bd_addr_to_str(cmdline_addr)); 3396104991bSMatthias Ringwald return; 3406104991bSMatthias Ringwald } 3416104991bSMatthias Ringwald printf("Disconnected %s\n", bd_addr_to_str(le_streamer_addr)); 3426104991bSMatthias Ringwald if (state == TC_OFF) break; 3436104991bSMatthias Ringwald gatt_heart_rate_client_start(); 3446104991bSMatthias Ringwald break; 3456104991bSMatthias Ringwald default: 3466104991bSMatthias Ringwald break; 3476104991bSMatthias Ringwald } 3486104991bSMatthias Ringwald } 3496104991bSMatthias Ringwald 3506104991bSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 3516104991bSMatthias Ringwald static void usage(const char *name){ 3526104991bSMatthias Ringwald fprintf(stderr, "Usage: %s [-a|--address aa:bb:cc:dd:ee:ff]\n", name); 3536104991bSMatthias 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"); 3546104991bSMatthias Ringwald fprintf(stderr, "To connect to a specific device use argument [-a].\n\n"); 3556104991bSMatthias Ringwald } 3566104991bSMatthias Ringwald #endif 3576104991bSMatthias Ringwald 3586104991bSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 3596104991bSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 3606104991bSMatthias Ringwald 3616104991bSMatthias Ringwald #ifdef HAVE_BTSTACK_STDIN 3626104991bSMatthias Ringwald int arg = 1; 3636104991bSMatthias Ringwald cmdline_addr_found = 0; 3646104991bSMatthias Ringwald 3656104991bSMatthias Ringwald while (arg < argc) { 3666104991bSMatthias Ringwald if(!strcmp(argv[arg], "-a") || !strcmp(argv[arg], "--address")){ 3676104991bSMatthias Ringwald arg++; 3686104991bSMatthias Ringwald cmdline_addr_found = sscanf_bd_addr(argv[arg], cmdline_addr); 3696104991bSMatthias Ringwald arg++; 3706104991bSMatthias Ringwald if (!cmdline_addr_found) exit(1); 3716104991bSMatthias Ringwald continue; 3726104991bSMatthias Ringwald } 3736104991bSMatthias Ringwald usage(argv[0]); 3746104991bSMatthias Ringwald return 0; 3756104991bSMatthias Ringwald } 3766104991bSMatthias Ringwald #else 3776104991bSMatthias Ringwald (void)argc; 3786104991bSMatthias Ringwald (void)argv; 3796104991bSMatthias Ringwald #endif 3806104991bSMatthias Ringwald l2cap_init(); 3816104991bSMatthias Ringwald 3826104991bSMatthias Ringwald gatt_client_init(); 3836104991bSMatthias Ringwald 3846104991bSMatthias Ringwald sm_init(); 3856104991bSMatthias Ringwald sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT); 3866104991bSMatthias Ringwald 387a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &hci_event_handler; 388a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 389a4fe6467SMatthias Ringwald 3906104991bSMatthias Ringwald // turn on! 3916104991bSMatthias Ringwald hci_power_control(HCI_POWER_ON); 3926104991bSMatthias Ringwald 3936104991bSMatthias Ringwald return 0; 3946104991bSMatthias Ringwald } 3956104991bSMatthias Ringwald /* EXAMPLE_END */ 396