1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 // ***************************************************************************** 39 /* EXAMPLE_START(gap_inquiry): GAP Inquiry Example 40 * 41 * @text The Generic Access Profile (GAP) defines how Bluetooth devices discover 42 * and establish a connection with each other. In this example, the application 43 * discovers surrounding Bluetooth devices and collects their Class of Device 44 * (CoD), page scan mode, clock offset, and RSSI. After that, the remote name of 45 * each device is requested. In the following section we outline the Bluetooth 46 * logic part, i.e., how the packet handler handles the asynchronous events and 47 * data packets. 48 */ 49 // ***************************************************************************** 50 51 #include <stdint.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 56 #include "btstack_config.h" 57 58 #include "btstack_run_loop.h" 59 60 #include "btstack_debug.h" 61 #include "btstack_memory.h" 62 #include "hci.h" 63 #include "hci_dump.h" 64 65 #include "classic/sdp_util.h" 66 67 #define MAX_DEVICES 10 68 enum DEVICE_STATE { REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED }; 69 struct device { 70 bd_addr_t address; 71 uint16_t clockOffset; 72 uint32_t classOfDevice; 73 uint8_t pageScanRepetitionMode; 74 uint8_t rssi; 75 enum DEVICE_STATE state; 76 }; 77 78 #define INQUIRY_INTERVAL 5 79 struct device devices[MAX_DEVICES]; 80 int deviceCount = 0; 81 82 83 enum STATE {INIT, ACTIVE} ; 84 enum STATE state = INIT; 85 86 static btstack_packet_callback_registration_t hci_event_callback_registration; 87 88 static int getDeviceIndexForAddress( bd_addr_t addr){ 89 int j; 90 for (j=0; j< deviceCount; j++){ 91 if (bd_addr_cmp(addr, devices[j].address) == 0){ 92 return j; 93 } 94 } 95 return -1; 96 } 97 98 static void start_scan(void){ 99 printf("Starting inquiry scan..\n"); 100 hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0); 101 } 102 103 static int has_more_remote_name_requests(void){ 104 int i; 105 for (i=0;i<deviceCount;i++) { 106 if (devices[i].state == REMOTE_NAME_REQUEST) return 1; 107 } 108 return 0; 109 } 110 111 static void do_next_remote_name_request(void){ 112 int i; 113 for (i=0;i<deviceCount;i++) { 114 // remote name request 115 if (devices[i].state == REMOTE_NAME_REQUEST){ 116 devices[i].state = REMOTE_NAME_INQUIRED; 117 printf("Get remote name of %s...\n", bd_addr_to_str(devices[i].address)); 118 hci_send_cmd(&hci_remote_name_request, devices[i].address, 119 devices[i].pageScanRepetitionMode, 0, devices[i].clockOffset | 0x8000); 120 return; 121 } 122 } 123 } 124 125 static void continue_remote_names(void){ 126 if (has_more_remote_name_requests()){ 127 do_next_remote_name_request(); 128 return; 129 } 130 start_scan(); 131 } 132 133 /* @section Bluetooth Logic 134 * 135 * @text The Bluetooth logic is implemented as a state machine within the packet 136 * handler. In this example, the following states are passed sequentially: 137 * INIT, and ACTIVE. 138 */ 139 140 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 141 bd_addr_t addr; 142 int i; 143 int numResponses; 144 int index; 145 146 // printf("packet_handler: pt: 0x%02x, packet[0]: 0x%02x\n", packet_type, packet[0]); 147 if (packet_type != HCI_EVENT_PACKET) return; 148 149 uint8_t event = packet[0]; 150 151 switch(state){ 152 /* @text In INIT, an inquiry scan is started, and the application transits to 153 * ACTIVE state. 154 */ 155 case INIT: 156 if (packet[2] == HCI_STATE_WORKING) { 157 start_scan(); 158 state = ACTIVE; 159 } 160 break; 161 162 /* @text In ACTIVE, the following events are processed: 163 * - Inquiry result event: the list of discovered devices is processed and the 164 * Class of Device (CoD), page scan mode, clock offset, and RSSI are stored in a table. 165 * - Inquiry complete event: the remote name is requested for devices without a fetched 166 * name. The state of a remote name can be one of the following: 167 * REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, or REMOTE_NAME_FETCHED. 168 * - Remote name cached event: prints cached remote names provided by BTstack, 169 * if persistent storage is provided. 170 * - Remote name request complete event: the remote name is stored in the table and the 171 * state is updated to REMOTE_NAME_FETCHED. The query of remote names is continued. 172 */ 173 case ACTIVE: 174 switch(event){ 175 case HCI_EVENT_INQUIRY_RESULT: 176 case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 177 numResponses = packet[2]; 178 for (i=0; i<numResponses && deviceCount < MAX_DEVICES;i++){ 179 reverse_bd_addr(&packet[3 + i * 6], addr); 180 index = getDeviceIndexForAddress(addr); 181 if (index >= 0) continue; // already in our list 182 183 memcpy(devices[deviceCount].address, addr, 6); 184 devices[deviceCount].pageScanRepetitionMode = packet [3 + numResponses*(6) + i*1]; 185 if (event == HCI_EVENT_INQUIRY_RESULT){ 186 devices[deviceCount].classOfDevice = little_endian_read_24(packet, 3 + numResponses*(6+1+1+1) + i*3); 187 devices[deviceCount].clockOffset = little_endian_read_16(packet, 3 + numResponses*(6+1+1+1+3) + i*2) & 0x7fff; 188 devices[deviceCount].rssi = 0; 189 } else { 190 devices[deviceCount].classOfDevice = little_endian_read_24(packet, 3 + numResponses*(6+1+1) + i*3); 191 devices[deviceCount].clockOffset = little_endian_read_16(packet, 3 + numResponses*(6+1+1+3) + i*2) & 0x7fff; 192 devices[deviceCount].rssi = packet [3 + numResponses*(6+1+1+3+2) + i*1]; 193 } 194 devices[deviceCount].state = REMOTE_NAME_REQUEST; 195 printf("Device found: %s with COD: 0x%06x, pageScan %d, clock offset 0x%04x, rssi 0x%02x\n", bd_addr_to_str(addr), 196 devices[deviceCount].classOfDevice, devices[deviceCount].pageScanRepetitionMode, 197 devices[deviceCount].clockOffset, devices[deviceCount].rssi); 198 deviceCount++; 199 } 200 break; 201 202 case HCI_EVENT_INQUIRY_COMPLETE: 203 for (i=0;i<deviceCount;i++) { 204 // retry remote name request 205 if (devices[i].state == REMOTE_NAME_INQUIRED) 206 devices[i].state = REMOTE_NAME_REQUEST; 207 } 208 continue_remote_names(); 209 break; 210 211 case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 212 reverse_bd_addr(&packet[3], addr); 213 index = getDeviceIndexForAddress(addr); 214 if (index >= 0) { 215 if (packet[2] == 0) { 216 printf("Name: '%s'\n", &packet[9]); 217 devices[index].state = REMOTE_NAME_FETCHED; 218 } else { 219 printf("Failed to get name: page timeout\n"); 220 } 221 } 222 continue_remote_names(); 223 break; 224 225 default: 226 break; 227 } 228 break; 229 230 default: 231 break; 232 } 233 } 234 235 /* @text For more details on discovering remote devices, please see 236 * Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices). 237 */ 238 239 240 /* @section Main Application Setup 241 * 242 * @text Listing MainConfiguration shows main application code. 243 * It registers the HCI packet handler and starts the Bluetooth stack. 244 */ 245 246 /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */ 247 int btstack_main(int argc, const char * argv[]); 248 int btstack_main(int argc, const char * argv[]) { 249 250 hci_event_callback_registration.callback = &packet_handler; 251 hci_add_event_handler(&hci_event_callback_registration); 252 253 // turn on! 254 hci_power_control(HCI_POWER_ON); 255 256 return 0; 257 } 258 /* LISTING_END */ 259 /* EXAMPLE_END */ 260