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