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){ 132bcf00d8fSMatthias Ringwald bd_addr_t addr; 133bcf00d8fSMatthias Ringwald int i; 134bcf00d8fSMatthias Ringwald int numResponses; 135bcf00d8fSMatthias Ringwald int index; 136bcf00d8fSMatthias Ringwald 137bcf00d8fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 138bcf00d8fSMatthias Ringwald 1390e2df43fSMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet); 140bcf00d8fSMatthias Ringwald 141bcf00d8fSMatthias Ringwald switch(state){ 142bcf00d8fSMatthias Ringwald /* @text In INIT, an inquiry scan is started, and the application transits to 143bcf00d8fSMatthias Ringwald * ACTIVE state. 144bcf00d8fSMatthias Ringwald */ 145bcf00d8fSMatthias Ringwald case INIT: 146*a0ffb263SMatthias Ringwald switch(event){ 147*a0ffb263SMatthias Ringwald case BTSTACK_EVENT_STATE: 148bcf00d8fSMatthias Ringwald if (packet[2] == HCI_STATE_WORKING){ 149bcf00d8fSMatthias Ringwald start_scan(); 150bcf00d8fSMatthias Ringwald state = ACTIVE; 151bcf00d8fSMatthias Ringwald } 152bcf00d8fSMatthias Ringwald break; 153*a0ffb263SMatthias Ringwald default: 154*a0ffb263SMatthias Ringwald break; 155*a0ffb263SMatthias Ringwald } 156*a0ffb263SMatthias Ringwald break; 157bcf00d8fSMatthias Ringwald 158bcf00d8fSMatthias Ringwald /* @text In ACTIVE, the following events are processed: 159bcf00d8fSMatthias Ringwald * - Inquiry result event: the list of discovered devices is processed and the 160bcf00d8fSMatthias Ringwald * Class of Device (CoD), page scan mode, clock offset, and RSSI are stored in a table. 161bcf00d8fSMatthias Ringwald * - Inquiry complete event: the remote name is requested for devices without a fetched 162bcf00d8fSMatthias Ringwald * name. The state of a remote name can be one of the following: 163bcf00d8fSMatthias Ringwald * REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, or REMOTE_NAME_FETCHED. 164bcf00d8fSMatthias Ringwald * - Remote name cached event: prints cached remote names provided by BTstack, 165bcf00d8fSMatthias Ringwald * if persistent storage is provided. 166bcf00d8fSMatthias Ringwald * - Remote name request complete event: the remote name is stored in the table and the 167bcf00d8fSMatthias Ringwald * state is updated to REMOTE_NAME_FETCHED. The query of remote names is continued. 168bcf00d8fSMatthias Ringwald */ 169bcf00d8fSMatthias Ringwald case ACTIVE: 170bcf00d8fSMatthias Ringwald switch(event){ 171bcf00d8fSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 172bcf00d8fSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 173bcf00d8fSMatthias Ringwald numResponses = packet[2]; 174bcf00d8fSMatthias Ringwald for (i=0; i<numResponses && deviceCount < MAX_DEVICES;i++){ 175bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[3 + i * 6], addr); 176bcf00d8fSMatthias Ringwald index = getDeviceIndexForAddress(addr); 177bcf00d8fSMatthias Ringwald if (index >= 0) continue; // already in our list 178bcf00d8fSMatthias Ringwald 179bcf00d8fSMatthias Ringwald memcpy(devices[deviceCount].address, addr, 6); 180bcf00d8fSMatthias Ringwald devices[deviceCount].pageScanRepetitionMode = packet [3 + numResponses*(6) + i*1]; 181bcf00d8fSMatthias Ringwald if (event == HCI_EVENT_INQUIRY_RESULT){ 182bcf00d8fSMatthias Ringwald devices[deviceCount].classOfDevice = little_endian_read_24(packet, 3 + numResponses*(6+1+1+1) + i*3); 183bcf00d8fSMatthias Ringwald devices[deviceCount].clockOffset = little_endian_read_16(packet, 3 + numResponses*(6+1+1+1+3) + i*2) & 0x7fff; 184bcf00d8fSMatthias Ringwald devices[deviceCount].rssi = 0; 185bcf00d8fSMatthias Ringwald } else { 186bcf00d8fSMatthias Ringwald devices[deviceCount].classOfDevice = little_endian_read_24(packet, 3 + numResponses*(6+1+1) + i*3); 187bcf00d8fSMatthias Ringwald devices[deviceCount].clockOffset = little_endian_read_16(packet, 3 + numResponses*(6+1+1+3) + i*2) & 0x7fff; 188bcf00d8fSMatthias Ringwald devices[deviceCount].rssi = packet [3 + numResponses*(6+1+1+3+2) + i*1]; 189bcf00d8fSMatthias Ringwald } 190bcf00d8fSMatthias Ringwald devices[deviceCount].state = REMOTE_NAME_REQUEST; 191bcf00d8fSMatthias Ringwald printf("Device found: %s with COD: 0x%06x, pageScan %d, clock offset 0x%04x, rssi 0x%02x\n", bd_addr_to_str(addr), 192bcf00d8fSMatthias Ringwald devices[deviceCount].classOfDevice, devices[deviceCount].pageScanRepetitionMode, 193bcf00d8fSMatthias Ringwald devices[deviceCount].clockOffset, devices[deviceCount].rssi); 194bcf00d8fSMatthias Ringwald deviceCount++; 195bcf00d8fSMatthias Ringwald } 196bcf00d8fSMatthias Ringwald break; 197bcf00d8fSMatthias Ringwald 198bcf00d8fSMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 199bcf00d8fSMatthias Ringwald for (i=0;i<deviceCount;i++) { 200bcf00d8fSMatthias Ringwald // retry remote name request 201bcf00d8fSMatthias Ringwald if (devices[i].state == REMOTE_NAME_INQUIRED) 202bcf00d8fSMatthias Ringwald devices[i].state = REMOTE_NAME_REQUEST; 203bcf00d8fSMatthias Ringwald } 204bcf00d8fSMatthias Ringwald continue_remote_names(); 205bcf00d8fSMatthias Ringwald break; 206bcf00d8fSMatthias Ringwald 207bcf00d8fSMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 208bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[3], addr); 209bcf00d8fSMatthias Ringwald index = getDeviceIndexForAddress(addr); 210bcf00d8fSMatthias Ringwald if (index >= 0) { 211bcf00d8fSMatthias Ringwald if (packet[2] == 0) { 212bcf00d8fSMatthias Ringwald printf("Name: '%s'\n", &packet[9]); 213bcf00d8fSMatthias Ringwald devices[index].state = REMOTE_NAME_FETCHED; 214bcf00d8fSMatthias Ringwald } else { 215bcf00d8fSMatthias Ringwald printf("Failed to get name: page timeout\n"); 216bcf00d8fSMatthias Ringwald } 217bcf00d8fSMatthias Ringwald } 218bcf00d8fSMatthias Ringwald continue_remote_names(); 219bcf00d8fSMatthias Ringwald break; 220bcf00d8fSMatthias Ringwald 221bcf00d8fSMatthias Ringwald default: 222bcf00d8fSMatthias Ringwald break; 223bcf00d8fSMatthias Ringwald } 224bcf00d8fSMatthias Ringwald break; 225bcf00d8fSMatthias Ringwald 226bcf00d8fSMatthias Ringwald default: 227bcf00d8fSMatthias Ringwald break; 228bcf00d8fSMatthias Ringwald } 229bcf00d8fSMatthias Ringwald } 230bcf00d8fSMatthias Ringwald 231bcf00d8fSMatthias Ringwald /* @text For more details on discovering remote devices, please see 232bcf00d8fSMatthias Ringwald * Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices). 233bcf00d8fSMatthias Ringwald */ 234bcf00d8fSMatthias Ringwald 235bcf00d8fSMatthias Ringwald 236bcf00d8fSMatthias Ringwald /* @section Main Application Setup 237bcf00d8fSMatthias Ringwald * 238bcf00d8fSMatthias Ringwald * @text Listing MainConfiguration shows main application code. 239bcf00d8fSMatthias Ringwald * It registers the HCI packet handler and starts the Bluetooth stack. 240bcf00d8fSMatthias Ringwald */ 241bcf00d8fSMatthias Ringwald 242bcf00d8fSMatthias Ringwald /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */ 243bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 244bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]) { 245bcf00d8fSMatthias Ringwald 246bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 247bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 248bcf00d8fSMatthias Ringwald 249bcf00d8fSMatthias Ringwald // turn on! 250bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 251bcf00d8fSMatthias Ringwald 252bcf00d8fSMatthias Ringwald return 0; 253bcf00d8fSMatthias Ringwald } 254bcf00d8fSMatthias Ringwald /* LISTING_END */ 255bcf00d8fSMatthias Ringwald /* EXAMPLE_END */ 256