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