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 38bcf00d8fSMatthias Ringwald // ***************************************************************************** 39bcf00d8fSMatthias Ringwald /* EXAMPLE_START(gap_inquiry): GAP Inquiry Example 40bcf00d8fSMatthias Ringwald * 41bcf00d8fSMatthias Ringwald * @text The Generic Access Profile (GAP) defines how Bluetooth devices discover 42bcf00d8fSMatthias Ringwald * and establish a connection with each other. In this example, the application 43bcf00d8fSMatthias Ringwald * discovers surrounding Bluetooth devices and collects their Class of Device 44bcf00d8fSMatthias Ringwald * (CoD), page scan mode, clock offset, and RSSI. After that, the remote name of 45bcf00d8fSMatthias Ringwald * each device is requested. In the following section we outline the Bluetooth 46bcf00d8fSMatthias Ringwald * logic part, i.e., how the packet handler handles the asynchronous events and 47bcf00d8fSMatthias Ringwald * data packets. 48bcf00d8fSMatthias Ringwald */ 49bcf00d8fSMatthias Ringwald // ***************************************************************************** 50bcf00d8fSMatthias Ringwald 51bcf00d8fSMatthias Ringwald #include <stdint.h> 52bcf00d8fSMatthias Ringwald #include <stdio.h> 53bcf00d8fSMatthias Ringwald #include <stdlib.h> 54bcf00d8fSMatthias Ringwald #include <string.h> 55bcf00d8fSMatthias Ringwald 560e2df43fSMatthias Ringwald #include "btstack.h" 57bcf00d8fSMatthias Ringwald 58bcf00d8fSMatthias Ringwald #define MAX_DEVICES 10 59bcf00d8fSMatthias Ringwald enum DEVICE_STATE { REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED }; 60bcf00d8fSMatthias Ringwald struct device { 61bcf00d8fSMatthias Ringwald bd_addr_t address; 62bcf00d8fSMatthias Ringwald uint16_t clockOffset; 63bcf00d8fSMatthias Ringwald uint32_t classOfDevice; 64bcf00d8fSMatthias Ringwald uint8_t pageScanRepetitionMode; 65bcf00d8fSMatthias Ringwald uint8_t rssi; 66bcf00d8fSMatthias Ringwald enum DEVICE_STATE state; 67bcf00d8fSMatthias Ringwald }; 68bcf00d8fSMatthias Ringwald 69bcf00d8fSMatthias Ringwald #define INQUIRY_INTERVAL 5 70bcf00d8fSMatthias Ringwald struct device devices[MAX_DEVICES]; 71bcf00d8fSMatthias Ringwald int deviceCount = 0; 72bcf00d8fSMatthias Ringwald 73bcf00d8fSMatthias Ringwald 74bcf00d8fSMatthias Ringwald enum STATE {INIT, ACTIVE} ; 75bcf00d8fSMatthias Ringwald enum STATE state = INIT; 76bcf00d8fSMatthias Ringwald 77bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 78bcf00d8fSMatthias Ringwald 79bcf00d8fSMatthias Ringwald static int getDeviceIndexForAddress( bd_addr_t addr){ 80bcf00d8fSMatthias Ringwald int j; 81bcf00d8fSMatthias Ringwald for (j=0; j< deviceCount; j++){ 82bcf00d8fSMatthias Ringwald if (bd_addr_cmp(addr, devices[j].address) == 0){ 83bcf00d8fSMatthias Ringwald return j; 84bcf00d8fSMatthias Ringwald } 85bcf00d8fSMatthias Ringwald } 86bcf00d8fSMatthias Ringwald return -1; 87bcf00d8fSMatthias Ringwald } 88bcf00d8fSMatthias Ringwald 89bcf00d8fSMatthias Ringwald static void start_scan(void){ 90bcf00d8fSMatthias Ringwald printf("Starting inquiry scan..\n"); 91bcf00d8fSMatthias Ringwald hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0); 92bcf00d8fSMatthias Ringwald } 93bcf00d8fSMatthias Ringwald 94bcf00d8fSMatthias Ringwald static int has_more_remote_name_requests(void){ 95bcf00d8fSMatthias Ringwald int i; 96bcf00d8fSMatthias Ringwald for (i=0;i<deviceCount;i++) { 97bcf00d8fSMatthias Ringwald if (devices[i].state == REMOTE_NAME_REQUEST) return 1; 98bcf00d8fSMatthias Ringwald } 99bcf00d8fSMatthias Ringwald return 0; 100bcf00d8fSMatthias Ringwald } 101bcf00d8fSMatthias Ringwald 102bcf00d8fSMatthias Ringwald static void do_next_remote_name_request(void){ 103bcf00d8fSMatthias Ringwald int i; 104bcf00d8fSMatthias Ringwald for (i=0;i<deviceCount;i++) { 105bcf00d8fSMatthias Ringwald // remote name request 106bcf00d8fSMatthias Ringwald if (devices[i].state == REMOTE_NAME_REQUEST){ 107bcf00d8fSMatthias Ringwald devices[i].state = REMOTE_NAME_INQUIRED; 108bcf00d8fSMatthias Ringwald printf("Get remote name of %s...\n", bd_addr_to_str(devices[i].address)); 109bcf00d8fSMatthias Ringwald hci_send_cmd(&hci_remote_name_request, devices[i].address, 110bcf00d8fSMatthias Ringwald devices[i].pageScanRepetitionMode, 0, devices[i].clockOffset | 0x8000); 111bcf00d8fSMatthias Ringwald return; 112bcf00d8fSMatthias Ringwald } 113bcf00d8fSMatthias Ringwald } 114bcf00d8fSMatthias Ringwald } 115bcf00d8fSMatthias Ringwald 116bcf00d8fSMatthias Ringwald static void continue_remote_names(void){ 117bcf00d8fSMatthias Ringwald if (has_more_remote_name_requests()){ 118bcf00d8fSMatthias Ringwald do_next_remote_name_request(); 119bcf00d8fSMatthias Ringwald return; 120bcf00d8fSMatthias Ringwald } 121bcf00d8fSMatthias Ringwald start_scan(); 122bcf00d8fSMatthias Ringwald } 123bcf00d8fSMatthias Ringwald 124bcf00d8fSMatthias Ringwald /* @section Bluetooth Logic 125bcf00d8fSMatthias Ringwald * 126bcf00d8fSMatthias Ringwald * @text The Bluetooth logic is implemented as a state machine within the packet 127bcf00d8fSMatthias Ringwald * handler. In this example, the following states are passed sequentially: 128bcf00d8fSMatthias Ringwald * INIT, and ACTIVE. 129bcf00d8fSMatthias Ringwald */ 130bcf00d8fSMatthias Ringwald 131bcf00d8fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 132*9ec2630cSMatthias Ringwald UNUSED(channel); 133*9ec2630cSMatthias Ringwald UNUSED(size); 134*9ec2630cSMatthias Ringwald 135bcf00d8fSMatthias Ringwald bd_addr_t addr; 136bcf00d8fSMatthias Ringwald int i; 137bcf00d8fSMatthias Ringwald int numResponses; 138bcf00d8fSMatthias Ringwald int index; 139bcf00d8fSMatthias Ringwald 140bcf00d8fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 141bcf00d8fSMatthias Ringwald 1420e2df43fSMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet); 143bcf00d8fSMatthias Ringwald 144bcf00d8fSMatthias Ringwald switch(state){ 145bcf00d8fSMatthias Ringwald /* @text In INIT, an inquiry scan is started, and the application transits to 146bcf00d8fSMatthias Ringwald * ACTIVE state. 147bcf00d8fSMatthias Ringwald */ 148bcf00d8fSMatthias Ringwald case INIT: 149a0ffb263SMatthias Ringwald switch(event){ 150a0ffb263SMatthias Ringwald case BTSTACK_EVENT_STATE: 151fb42b6e5SMilanka Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 152bcf00d8fSMatthias Ringwald start_scan(); 153bcf00d8fSMatthias Ringwald state = ACTIVE; 154bcf00d8fSMatthias Ringwald } 155bcf00d8fSMatthias Ringwald break; 156a0ffb263SMatthias Ringwald default: 157a0ffb263SMatthias Ringwald break; 158a0ffb263SMatthias Ringwald } 159a0ffb263SMatthias Ringwald break; 160bcf00d8fSMatthias Ringwald 161bcf00d8fSMatthias Ringwald /* @text In ACTIVE, the following events are processed: 162bcf00d8fSMatthias Ringwald * - Inquiry result event: the list of discovered devices is processed and the 163bcf00d8fSMatthias Ringwald * Class of Device (CoD), page scan mode, clock offset, and RSSI are stored in a table. 164bcf00d8fSMatthias Ringwald * - Inquiry complete event: the remote name is requested for devices without a fetched 165bcf00d8fSMatthias Ringwald * name. The state of a remote name can be one of the following: 166bcf00d8fSMatthias Ringwald * REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, or REMOTE_NAME_FETCHED. 167bcf00d8fSMatthias Ringwald * - Remote name cached event: prints cached remote names provided by BTstack, 168bcf00d8fSMatthias Ringwald * if persistent storage is provided. 169bcf00d8fSMatthias Ringwald * - Remote name request complete event: the remote name is stored in the table and the 170bcf00d8fSMatthias Ringwald * state is updated to REMOTE_NAME_FETCHED. The query of remote names is continued. 171bcf00d8fSMatthias Ringwald */ 172bcf00d8fSMatthias Ringwald case ACTIVE: 173bcf00d8fSMatthias Ringwald switch(event){ 174bcf00d8fSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 175bcf00d8fSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 176b12ad867SMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 177f8744deaSMilanka Ringwald numResponses = hci_event_inquiry_result_get_num_responses(packet); 178bcf00d8fSMatthias Ringwald for (i=0; i<numResponses && deviceCount < MAX_DEVICES;i++){ 179bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[3 + i * 6], addr); 180bcf00d8fSMatthias Ringwald index = getDeviceIndexForAddress(addr); 181bcf00d8fSMatthias Ringwald if (index >= 0) continue; // already in our list 182bcf00d8fSMatthias Ringwald 183bcf00d8fSMatthias Ringwald memcpy(devices[deviceCount].address, addr, 6); 184bcf00d8fSMatthias Ringwald devices[deviceCount].pageScanRepetitionMode = packet [3 + numResponses*(6) + i*1]; 185bcf00d8fSMatthias Ringwald if (event == HCI_EVENT_INQUIRY_RESULT){ 186bcf00d8fSMatthias Ringwald devices[deviceCount].classOfDevice = little_endian_read_24(packet, 3 + numResponses*(6+1+1+1) + i*3); 187bcf00d8fSMatthias Ringwald devices[deviceCount].clockOffset = little_endian_read_16(packet, 3 + numResponses*(6+1+1+1+3) + i*2) & 0x7fff; 188bcf00d8fSMatthias Ringwald devices[deviceCount].rssi = 0; 189bcf00d8fSMatthias Ringwald } else { 190bcf00d8fSMatthias Ringwald devices[deviceCount].classOfDevice = little_endian_read_24(packet, 3 + numResponses*(6+1+1) + i*3); 191bcf00d8fSMatthias Ringwald devices[deviceCount].clockOffset = little_endian_read_16(packet, 3 + numResponses*(6+1+1+3) + i*2) & 0x7fff; 192bcf00d8fSMatthias Ringwald devices[deviceCount].rssi = packet [3 + numResponses*(6+1+1+3+2) + i*1]; 193bcf00d8fSMatthias Ringwald } 194bcf00d8fSMatthias Ringwald devices[deviceCount].state = REMOTE_NAME_REQUEST; 195b12ad867SMatthias Ringwald char name_buffer[240]; 196b12ad867SMatthias Ringwald if (event == HCI_EVENT_EXTENDED_INQUIRY_RESPONSE){ 197b12ad867SMatthias Ringwald uint8_t * eir_data = &packet[3 + (6+1+1+3+2) + 1]; 198b12ad867SMatthias Ringwald ad_context_t context; 199b12ad867SMatthias Ringwald for (ad_iterator_init(&context, 240, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 200b12ad867SMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 201b12ad867SMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 202b12ad867SMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 203b12ad867SMatthias Ringwald // Prefer Complete Local Name over Shortend Local Name 204b12ad867SMatthias Ringwald switch (data_type){ 2051d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 206b12ad867SMatthias Ringwald if (devices[deviceCount].state == REMOTE_NAME_FETCHED) break; 2071d0cde9dSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 208b12ad867SMatthias Ringwald devices[deviceCount].state = REMOTE_NAME_FETCHED; 209b12ad867SMatthias Ringwald memcpy(name_buffer, data, data_size); 210b12ad867SMatthias Ringwald name_buffer[data_size] = 0; 211b12ad867SMatthias Ringwald break; 212b12ad867SMatthias Ringwald default: 213b12ad867SMatthias Ringwald break; 214b12ad867SMatthias Ringwald } 215b12ad867SMatthias Ringwald } 216b12ad867SMatthias Ringwald } 217b12ad867SMatthias Ringwald printf("Device found: %s with COD: 0x%06x, pageScan %d, clock offset 0x%04x", bd_addr_to_str(addr), 218a6efb919SMatthias Ringwald (unsigned int) devices[deviceCount].classOfDevice, devices[deviceCount].pageScanRepetitionMode, 219b12ad867SMatthias Ringwald devices[deviceCount].clockOffset); 220b12ad867SMatthias Ringwald if (event >= HCI_EVENT_INQUIRY_RESULT_WITH_RSSI){ 2211d0cde9dSMatthias Ringwald printf(", rssi 0x%02x", devices[deviceCount].rssi); 222b12ad867SMatthias Ringwald } 223b12ad867SMatthias Ringwald if (devices[deviceCount].state == REMOTE_NAME_FETCHED){ 224b12ad867SMatthias Ringwald printf(", name '%s'", name_buffer); 225b12ad867SMatthias Ringwald } 226b12ad867SMatthias Ringwald printf("\n"); 227bcf00d8fSMatthias Ringwald deviceCount++; 228bcf00d8fSMatthias Ringwald } 229bcf00d8fSMatthias Ringwald break; 230bcf00d8fSMatthias Ringwald 231bcf00d8fSMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 232bcf00d8fSMatthias Ringwald for (i=0;i<deviceCount;i++) { 233bcf00d8fSMatthias Ringwald // retry remote name request 234bcf00d8fSMatthias Ringwald if (devices[i].state == REMOTE_NAME_INQUIRED) 235bcf00d8fSMatthias Ringwald devices[i].state = REMOTE_NAME_REQUEST; 236bcf00d8fSMatthias Ringwald } 237bcf00d8fSMatthias Ringwald continue_remote_names(); 238bcf00d8fSMatthias Ringwald break; 239bcf00d8fSMatthias Ringwald 240bcf00d8fSMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 241bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[3], addr); 242bcf00d8fSMatthias Ringwald index = getDeviceIndexForAddress(addr); 243bcf00d8fSMatthias Ringwald if (index >= 0) { 244bcf00d8fSMatthias Ringwald if (packet[2] == 0) { 245bcf00d8fSMatthias Ringwald printf("Name: '%s'\n", &packet[9]); 246bcf00d8fSMatthias Ringwald devices[index].state = REMOTE_NAME_FETCHED; 247bcf00d8fSMatthias Ringwald } else { 248bcf00d8fSMatthias Ringwald printf("Failed to get name: page timeout\n"); 249bcf00d8fSMatthias Ringwald } 250bcf00d8fSMatthias Ringwald } 251bcf00d8fSMatthias Ringwald continue_remote_names(); 252bcf00d8fSMatthias Ringwald break; 253bcf00d8fSMatthias Ringwald 254bcf00d8fSMatthias Ringwald default: 255bcf00d8fSMatthias Ringwald break; 256bcf00d8fSMatthias Ringwald } 257bcf00d8fSMatthias Ringwald break; 258bcf00d8fSMatthias Ringwald 259bcf00d8fSMatthias Ringwald default: 260bcf00d8fSMatthias Ringwald break; 261bcf00d8fSMatthias Ringwald } 262bcf00d8fSMatthias Ringwald } 263bcf00d8fSMatthias Ringwald 264bcf00d8fSMatthias Ringwald /* @text For more details on discovering remote devices, please see 265bcf00d8fSMatthias Ringwald * Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices). 266bcf00d8fSMatthias Ringwald */ 267bcf00d8fSMatthias Ringwald 268bcf00d8fSMatthias Ringwald 269bcf00d8fSMatthias Ringwald /* @section Main Application Setup 270bcf00d8fSMatthias Ringwald * 271bcf00d8fSMatthias Ringwald * @text Listing MainConfiguration shows main application code. 272bcf00d8fSMatthias Ringwald * It registers the HCI packet handler and starts the Bluetooth stack. 273bcf00d8fSMatthias Ringwald */ 274bcf00d8fSMatthias Ringwald 275bcf00d8fSMatthias Ringwald /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */ 276bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 277bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]) { 278*9ec2630cSMatthias Ringwald (void)argc; 279*9ec2630cSMatthias Ringwald (void)argv; 280bcf00d8fSMatthias Ringwald 281bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 282bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 283bcf00d8fSMatthias Ringwald 284b12ad867SMatthias Ringwald // enabled EIR 285b12ad867SMatthias Ringwald hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR); 286b12ad867SMatthias Ringwald 287bcf00d8fSMatthias Ringwald // turn on! 288bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 289bcf00d8fSMatthias Ringwald 290bcf00d8fSMatthias Ringwald return 0; 291bcf00d8fSMatthias Ringwald } 292bcf00d8fSMatthias Ringwald /* LISTING_END */ 293bcf00d8fSMatthias Ringwald /* EXAMPLE_END */ 294