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