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
23*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka Ringwald * GMBH 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
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "gap_inquiry.c"
39ab2c6ae4SMatthias Ringwald
40bcf00d8fSMatthias Ringwald // *****************************************************************************
41ec8ae085SMilanka Ringwald /* EXAMPLE_START(gap_inquiry): GAP Classic Inquiry
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
601cfb383eSMatthias Ringwald #define MAX_DEVICES 20
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 uint8_t pageScanRepetitionMode;
651cfb383eSMatthias Ringwald uint16_t clockOffset;
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
getDeviceIndexForAddress(bd_addr_t addr)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
start_scan(void)89bcf00d8fSMatthias Ringwald static void start_scan(void){
90bcf00d8fSMatthias Ringwald printf("Starting inquiry scan..\n");
91f5875de5SMatthias Ringwald gap_inquiry_start(INQUIRY_INTERVAL);
92bcf00d8fSMatthias Ringwald }
93bcf00d8fSMatthias Ringwald
has_more_remote_name_requests(void)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
do_next_remote_name_request(void)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));
109b7f1ee76SMatthias Ringwald gap_remote_name_request( devices[i].address, devices[i].pageScanRepetitionMode, devices[i].clockOffset | 0x8000);
110bcf00d8fSMatthias Ringwald return;
111bcf00d8fSMatthias Ringwald }
112bcf00d8fSMatthias Ringwald }
113bcf00d8fSMatthias Ringwald }
114bcf00d8fSMatthias Ringwald
continue_remote_names(void)115bcf00d8fSMatthias Ringwald static void continue_remote_names(void){
116bcf00d8fSMatthias Ringwald if (has_more_remote_name_requests()){
117bcf00d8fSMatthias Ringwald do_next_remote_name_request();
118bcf00d8fSMatthias Ringwald return;
119bcf00d8fSMatthias Ringwald }
120bcf00d8fSMatthias Ringwald start_scan();
121bcf00d8fSMatthias Ringwald }
122bcf00d8fSMatthias Ringwald
123bcf00d8fSMatthias Ringwald /* @section Bluetooth Logic
124bcf00d8fSMatthias Ringwald *
125bcf00d8fSMatthias Ringwald * @text The Bluetooth logic is implemented as a state machine within the packet
126bcf00d8fSMatthias Ringwald * handler. In this example, the following states are passed sequentially:
127bcf00d8fSMatthias Ringwald * INIT, and ACTIVE.
128bcf00d8fSMatthias Ringwald */
129bcf00d8fSMatthias Ringwald
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)130bcf00d8fSMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1319ec2630cSMatthias Ringwald UNUSED(channel);
1329ec2630cSMatthias Ringwald UNUSED(size);
1339ec2630cSMatthias Ringwald
134bcf00d8fSMatthias Ringwald bd_addr_t addr;
135bcf00d8fSMatthias Ringwald int i;
136bcf00d8fSMatthias Ringwald int index;
137bcf00d8fSMatthias Ringwald
138bcf00d8fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return;
139bcf00d8fSMatthias Ringwald
1400e2df43fSMatthias Ringwald uint8_t event = hci_event_packet_get_type(packet);
141bcf00d8fSMatthias Ringwald
142bcf00d8fSMatthias Ringwald switch(state){
143bcf00d8fSMatthias Ringwald /* @text In INIT, an inquiry scan is started, and the application transits to
144bcf00d8fSMatthias Ringwald * ACTIVE state.
145bcf00d8fSMatthias Ringwald */
146bcf00d8fSMatthias Ringwald case INIT:
147a0ffb263SMatthias Ringwald switch(event){
148a0ffb263SMatthias Ringwald case BTSTACK_EVENT_STATE:
149fb42b6e5SMilanka Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
150bcf00d8fSMatthias Ringwald start_scan();
151bcf00d8fSMatthias Ringwald state = ACTIVE;
152bcf00d8fSMatthias Ringwald }
153bcf00d8fSMatthias Ringwald break;
154a0ffb263SMatthias Ringwald default:
155a0ffb263SMatthias Ringwald break;
156a0ffb263SMatthias Ringwald }
157a0ffb263SMatthias Ringwald break;
158bcf00d8fSMatthias Ringwald
159bcf00d8fSMatthias Ringwald /* @text In ACTIVE, the following events are processed:
1601cfb383eSMatthias Ringwald * - GAP Inquiry result event: BTstack provides a unified inquiry result that contain
1611cfb383eSMatthias Ringwald * Class of Device (CoD), page scan mode, clock offset. RSSI and name (from EIR) are optional.
162bcf00d8fSMatthias Ringwald * - Inquiry complete event: the remote name is requested for devices without a fetched
163bcf00d8fSMatthias Ringwald * name. The state of a remote name can be one of the following:
164bcf00d8fSMatthias Ringwald * REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, or REMOTE_NAME_FETCHED.
165bcf00d8fSMatthias Ringwald * - Remote name request complete event: the remote name is stored in the table and the
166bcf00d8fSMatthias Ringwald * state is updated to REMOTE_NAME_FETCHED. The query of remote names is continued.
167bcf00d8fSMatthias Ringwald */
168bcf00d8fSMatthias Ringwald case ACTIVE:
169bcf00d8fSMatthias Ringwald switch(event){
1701cfb383eSMatthias Ringwald
1711cfb383eSMatthias Ringwald case GAP_EVENT_INQUIRY_RESULT:
1721cfb383eSMatthias Ringwald if (deviceCount >= MAX_DEVICES) break; // already full
1731cfb383eSMatthias Ringwald gap_event_inquiry_result_get_bd_addr(packet, addr);
174bcf00d8fSMatthias Ringwald index = getDeviceIndexForAddress(addr);
1751cfb383eSMatthias Ringwald if (index >= 0) break; // already in our list
176bcf00d8fSMatthias Ringwald
177bcf00d8fSMatthias Ringwald memcpy(devices[deviceCount].address, addr, 6);
1781cfb383eSMatthias Ringwald devices[deviceCount].pageScanRepetitionMode = gap_event_inquiry_result_get_page_scan_repetition_mode(packet);
1791cfb383eSMatthias Ringwald devices[deviceCount].clockOffset = gap_event_inquiry_result_get_clock_offset(packet);
1801cfb383eSMatthias Ringwald // print info
1811cfb383eSMatthias Ringwald printf("Device found: %s ", bd_addr_to_str(addr));
1821cfb383eSMatthias Ringwald printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(packet));
1831cfb383eSMatthias Ringwald printf("pageScan %d, ", devices[deviceCount].pageScanRepetitionMode);
184ee8a36c8SMatthias Ringwald printf("clock offset 0x%04x",devices[deviceCount].clockOffset);
185eaac31e8SMatthias Ringwald if (gap_event_inquiry_result_get_rssi_available(packet)){
1861cfb383eSMatthias Ringwald printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(packet));
187bcf00d8fSMatthias Ringwald }
1881cfb383eSMatthias Ringwald if (gap_event_inquiry_result_get_name_available(packet)){
189b12ad867SMatthias Ringwald char name_buffer[240];
1901cfb383eSMatthias Ringwald int name_len = gap_event_inquiry_result_get_name_len(packet);
1911cfb383eSMatthias Ringwald memcpy(name_buffer, gap_event_inquiry_result_get_name(packet), name_len);
1921cfb383eSMatthias Ringwald name_buffer[name_len] = 0;
193b12ad867SMatthias Ringwald printf(", name '%s'", name_buffer);
1941cfb383eSMatthias Ringwald devices[deviceCount].state = REMOTE_NAME_FETCHED;;
1951cfb383eSMatthias Ringwald } else {
1961cfb383eSMatthias Ringwald devices[deviceCount].state = REMOTE_NAME_REQUEST;
197b12ad867SMatthias Ringwald }
198b12ad867SMatthias Ringwald printf("\n");
199bcf00d8fSMatthias Ringwald deviceCount++;
200bcf00d8fSMatthias Ringwald break;
201bcf00d8fSMatthias Ringwald
202f5875de5SMatthias Ringwald case GAP_EVENT_INQUIRY_COMPLETE:
203bcf00d8fSMatthias Ringwald for (i=0;i<deviceCount;i++) {
204bcf00d8fSMatthias Ringwald // retry remote name request
205bcf00d8fSMatthias Ringwald if (devices[i].state == REMOTE_NAME_INQUIRED)
206bcf00d8fSMatthias Ringwald devices[i].state = REMOTE_NAME_REQUEST;
207bcf00d8fSMatthias Ringwald }
208bcf00d8fSMatthias Ringwald continue_remote_names();
209bcf00d8fSMatthias Ringwald break;
210bcf00d8fSMatthias Ringwald
211bcf00d8fSMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
212bcf00d8fSMatthias Ringwald reverse_bd_addr(&packet[3], addr);
213bcf00d8fSMatthias Ringwald index = getDeviceIndexForAddress(addr);
214bcf00d8fSMatthias Ringwald if (index >= 0) {
215bcf00d8fSMatthias Ringwald if (packet[2] == 0) {
216bcf00d8fSMatthias Ringwald printf("Name: '%s'\n", &packet[9]);
217bcf00d8fSMatthias Ringwald devices[index].state = REMOTE_NAME_FETCHED;
218bcf00d8fSMatthias Ringwald } else {
219bcf00d8fSMatthias Ringwald printf("Failed to get name: page timeout\n");
220bcf00d8fSMatthias Ringwald }
221bcf00d8fSMatthias Ringwald }
222bcf00d8fSMatthias Ringwald continue_remote_names();
223bcf00d8fSMatthias Ringwald break;
224bcf00d8fSMatthias Ringwald
225bcf00d8fSMatthias Ringwald default:
226bcf00d8fSMatthias Ringwald break;
227bcf00d8fSMatthias Ringwald }
228bcf00d8fSMatthias Ringwald break;
229bcf00d8fSMatthias Ringwald
230bcf00d8fSMatthias Ringwald default:
231bcf00d8fSMatthias Ringwald break;
232bcf00d8fSMatthias Ringwald }
233bcf00d8fSMatthias Ringwald }
234bcf00d8fSMatthias Ringwald
235bcf00d8fSMatthias Ringwald /* @text For more details on discovering remote devices, please see
236bcf00d8fSMatthias Ringwald * Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices).
237bcf00d8fSMatthias Ringwald */
238bcf00d8fSMatthias Ringwald
239bcf00d8fSMatthias Ringwald
240bcf00d8fSMatthias Ringwald /* @section Main Application Setup
241bcf00d8fSMatthias Ringwald *
242bcf00d8fSMatthias Ringwald * @text Listing MainConfiguration shows main application code.
243bcf00d8fSMatthias Ringwald * It registers the HCI packet handler and starts the Bluetooth stack.
244bcf00d8fSMatthias Ringwald */
245bcf00d8fSMatthias Ringwald
246bcf00d8fSMatthias Ringwald /* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */
247bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])248bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]) {
2499ec2630cSMatthias Ringwald (void)argc;
2509ec2630cSMatthias Ringwald (void)argv;
251bcf00d8fSMatthias Ringwald
252b12ad867SMatthias Ringwald // enabled EIR
253b12ad867SMatthias Ringwald hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
254b12ad867SMatthias Ringwald
255a4fe6467SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler;
256a4fe6467SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration);
257a4fe6467SMatthias Ringwald
258bcf00d8fSMatthias Ringwald // turn on!
259bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON);
260bcf00d8fSMatthias Ringwald
261bcf00d8fSMatthias Ringwald return 0;
262bcf00d8fSMatthias Ringwald }
263bcf00d8fSMatthias Ringwald /* LISTING_END */
264bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
265