2015-02-06 11:25:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 BlueKitchen GmbH
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* 4. Any redistribution, use, or modification is done solely for
|
|
|
|
* personal benefit and not for any commercial purpose or for
|
|
|
|
* monetary gain.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
2021-11-02 08:31:00 +00:00
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
|
|
|
|
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
2015-02-06 11:25:18 +00:00
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* Please inquire about commercial licensing options at
|
|
|
|
* contact@bluekitchen-gmbh.com
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-05-11 17:31:55 +00:00
|
|
|
#define BTSTACK_FILE__ "gap_inquiry.c"
|
2017-03-24 22:39:20 +00:00
|
|
|
|
2014-10-16 09:15:23 +00:00
|
|
|
// *****************************************************************************
|
2020-10-23 07:54:54 +00:00
|
|
|
/* EXAMPLE_START(gap_inquiry): GAP Classic Inquiry
|
2015-04-17 12:21:27 +00:00
|
|
|
*
|
|
|
|
* @text The Generic Access Profile (GAP) defines how Bluetooth devices discover
|
|
|
|
* and establish a connection with each other. In this example, the application
|
|
|
|
* discovers surrounding Bluetooth devices and collects their Class of Device
|
|
|
|
* (CoD), page scan mode, clock offset, and RSSI. After that, the remote name of
|
|
|
|
* each device is requested. In the following section we outline the Bluetooth
|
|
|
|
* logic part, i.e., how the packet handler handles the asynchronous events and
|
|
|
|
* data packets.
|
|
|
|
*/
|
2014-10-16 09:15:23 +00:00
|
|
|
// *****************************************************************************
|
2014-01-22 22:25:56 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-02-19 13:52:36 +00:00
|
|
|
#include "btstack.h"
|
|
|
|
|
2017-05-17 21:07:55 +00:00
|
|
|
#define MAX_DEVICES 20
|
2014-10-02 12:28:55 +00:00
|
|
|
enum DEVICE_STATE { REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, REMOTE_NAME_FETCHED };
|
|
|
|
struct device {
|
2017-05-17 21:07:55 +00:00
|
|
|
bd_addr_t address;
|
|
|
|
uint8_t pageScanRepetitionMode;
|
|
|
|
uint16_t clockOffset;
|
2014-10-02 12:28:55 +00:00
|
|
|
enum DEVICE_STATE state;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define INQUIRY_INTERVAL 5
|
|
|
|
struct device devices[MAX_DEVICES];
|
|
|
|
int deviceCount = 0;
|
2014-01-22 22:25:56 +00:00
|
|
|
|
2014-10-02 12:28:55 +00:00
|
|
|
|
2015-04-17 14:00:29 +00:00
|
|
|
enum STATE {INIT, ACTIVE} ;
|
2014-01-22 22:25:56 +00:00
|
|
|
enum STATE state = INIT;
|
|
|
|
|
2016-02-03 13:09:26 +00:00
|
|
|
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
2014-01-22 22:25:56 +00:00
|
|
|
|
2015-11-06 18:43:35 +00:00
|
|
|
static int getDeviceIndexForAddress( bd_addr_t addr){
|
2014-10-02 12:28:55 +00:00
|
|
|
int j;
|
|
|
|
for (j=0; j< deviceCount; j++){
|
2016-02-10 16:09:24 +00:00
|
|
|
if (bd_addr_cmp(addr, devices[j].address) == 0){
|
2014-10-02 12:28:55 +00:00
|
|
|
return j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-11-06 18:43:35 +00:00
|
|
|
static void start_scan(void){
|
2014-10-02 12:28:55 +00:00
|
|
|
printf("Starting inquiry scan..\n");
|
2017-05-21 20:23:03 +00:00
|
|
|
gap_inquiry_start(INQUIRY_INTERVAL);
|
2014-10-02 12:28:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 18:43:35 +00:00
|
|
|
static int has_more_remote_name_requests(void){
|
2014-10-02 12:28:55 +00:00
|
|
|
int i;
|
|
|
|
for (i=0;i<deviceCount;i++) {
|
|
|
|
if (devices[i].state == REMOTE_NAME_REQUEST) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-06 18:43:35 +00:00
|
|
|
static void do_next_remote_name_request(void){
|
2014-10-02 12:28:55 +00:00
|
|
|
int i;
|
|
|
|
for (i=0;i<deviceCount;i++) {
|
|
|
|
// remote name request
|
|
|
|
if (devices[i].state == REMOTE_NAME_REQUEST){
|
|
|
|
devices[i].state = REMOTE_NAME_INQUIRED;
|
|
|
|
printf("Get remote name of %s...\n", bd_addr_to_str(devices[i].address));
|
2017-05-21 20:44:47 +00:00
|
|
|
gap_remote_name_request( devices[i].address, devices[i].pageScanRepetitionMode, devices[i].clockOffset | 0x8000);
|
2014-10-02 12:28:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 08:30:46 +00:00
|
|
|
static void continue_remote_names(void){
|
2014-10-02 12:28:55 +00:00
|
|
|
if (has_more_remote_name_requests()){
|
|
|
|
do_next_remote_name_request();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
start_scan();
|
|
|
|
}
|
2014-01-22 22:25:56 +00:00
|
|
|
|
2015-04-17 12:21:27 +00:00
|
|
|
/* @section Bluetooth Logic
|
|
|
|
*
|
|
|
|
* @text The Bluetooth logic is implemented as a state machine within the packet
|
|
|
|
* handler. In this example, the following states are passed sequentially:
|
2015-04-17 14:00:29 +00:00
|
|
|
* INIT, and ACTIVE.
|
2015-04-17 12:21:27 +00:00
|
|
|
*/
|
|
|
|
|
2016-02-04 16:45:02 +00:00
|
|
|
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
2016-12-22 21:16:16 +00:00
|
|
|
UNUSED(channel);
|
|
|
|
UNUSED(size);
|
|
|
|
|
2014-10-02 12:28:55 +00:00
|
|
|
bd_addr_t addr;
|
|
|
|
int i;
|
2014-10-16 10:12:03 +00:00
|
|
|
int index;
|
2014-10-02 12:28:55 +00:00
|
|
|
|
2014-01-22 22:25:56 +00:00
|
|
|
if (packet_type != HCI_EVENT_PACKET) return;
|
|
|
|
|
2016-02-19 13:52:36 +00:00
|
|
|
uint8_t event = hci_event_packet_get_type(packet);
|
2014-10-02 12:28:55 +00:00
|
|
|
|
2015-04-17 12:21:27 +00:00
|
|
|
switch(state){
|
2015-04-17 14:00:29 +00:00
|
|
|
/* @text In INIT, an inquiry scan is started, and the application transits to
|
|
|
|
* ACTIVE state.
|
2015-04-17 12:21:27 +00:00
|
|
|
*/
|
2016-03-03 11:06:41 +00:00
|
|
|
case INIT:
|
|
|
|
switch(event){
|
|
|
|
case BTSTACK_EVENT_STATE:
|
2016-04-01 10:07:08 +00:00
|
|
|
if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
|
2016-03-03 11:06:41 +00:00
|
|
|
start_scan();
|
|
|
|
state = ACTIVE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-01-22 22:25:56 +00:00
|
|
|
}
|
2015-04-24 20:44:13 +00:00
|
|
|
break;
|
|
|
|
|
2015-04-17 12:21:27 +00:00
|
|
|
/* @text In ACTIVE, the following events are processed:
|
2017-05-17 21:07:55 +00:00
|
|
|
* - GAP Inquiry result event: BTstack provides a unified inquiry result that contain
|
|
|
|
* Class of Device (CoD), page scan mode, clock offset. RSSI and name (from EIR) are optional.
|
2015-04-17 12:21:27 +00:00
|
|
|
* - Inquiry complete event: the remote name is requested for devices without a fetched
|
|
|
|
* name. The state of a remote name can be one of the following:
|
|
|
|
* REMOTE_NAME_REQUEST, REMOTE_NAME_INQUIRED, or REMOTE_NAME_FETCHED.
|
|
|
|
* - Remote name request complete event: the remote name is stored in the table and the
|
|
|
|
* state is updated to REMOTE_NAME_FETCHED. The query of remote names is continued.
|
|
|
|
*/
|
2014-10-02 12:28:55 +00:00
|
|
|
case ACTIVE:
|
|
|
|
switch(event){
|
2017-05-17 21:07:55 +00:00
|
|
|
|
|
|
|
case GAP_EVENT_INQUIRY_RESULT:
|
|
|
|
if (deviceCount >= MAX_DEVICES) break; // already full
|
|
|
|
gap_event_inquiry_result_get_bd_addr(packet, addr);
|
|
|
|
index = getDeviceIndexForAddress(addr);
|
|
|
|
if (index >= 0) break; // already in our list
|
|
|
|
|
|
|
|
memcpy(devices[deviceCount].address, addr, 6);
|
|
|
|
devices[deviceCount].pageScanRepetitionMode = gap_event_inquiry_result_get_page_scan_repetition_mode(packet);
|
|
|
|
devices[deviceCount].clockOffset = gap_event_inquiry_result_get_clock_offset(packet);
|
|
|
|
// print info
|
|
|
|
printf("Device found: %s ", bd_addr_to_str(addr));
|
|
|
|
printf("with COD: 0x%06x, ", (unsigned int) gap_event_inquiry_result_get_class_of_device(packet));
|
|
|
|
printf("pageScan %d, ", devices[deviceCount].pageScanRepetitionMode);
|
2017-07-05 15:12:43 +00:00
|
|
|
printf("clock offset 0x%04x",devices[deviceCount].clockOffset);
|
2017-06-07 13:14:44 +00:00
|
|
|
if (gap_event_inquiry_result_get_rssi_available(packet)){
|
2017-05-17 21:07:55 +00:00
|
|
|
printf(", rssi %d dBm", (int8_t) gap_event_inquiry_result_get_rssi(packet));
|
|
|
|
}
|
|
|
|
if (gap_event_inquiry_result_get_name_available(packet)){
|
2016-09-29 13:53:57 +00:00
|
|
|
char name_buffer[240];
|
2017-05-17 21:07:55 +00:00
|
|
|
int name_len = gap_event_inquiry_result_get_name_len(packet);
|
|
|
|
memcpy(name_buffer, gap_event_inquiry_result_get_name(packet), name_len);
|
|
|
|
name_buffer[name_len] = 0;
|
|
|
|
printf(", name '%s'", name_buffer);
|
|
|
|
devices[deviceCount].state = REMOTE_NAME_FETCHED;;
|
|
|
|
} else {
|
|
|
|
devices[deviceCount].state = REMOTE_NAME_REQUEST;
|
2014-10-02 12:28:55 +00:00
|
|
|
}
|
2017-05-17 21:07:55 +00:00
|
|
|
printf("\n");
|
|
|
|
deviceCount++;
|
2014-10-02 12:28:55 +00:00
|
|
|
break;
|
2017-05-17 21:07:55 +00:00
|
|
|
|
2017-05-21 20:23:03 +00:00
|
|
|
case GAP_EVENT_INQUIRY_COMPLETE:
|
2014-10-02 12:28:55 +00:00
|
|
|
for (i=0;i<deviceCount;i++) {
|
|
|
|
// retry remote name request
|
|
|
|
if (devices[i].state == REMOTE_NAME_INQUIRED)
|
|
|
|
devices[i].state = REMOTE_NAME_REQUEST;
|
|
|
|
}
|
|
|
|
continue_remote_names();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
|
2016-02-11 14:20:08 +00:00
|
|
|
reverse_bd_addr(&packet[3], addr);
|
2014-10-15 08:27:13 +00:00
|
|
|
index = getDeviceIndexForAddress(addr);
|
2014-10-02 12:28:55 +00:00
|
|
|
if (index >= 0) {
|
|
|
|
if (packet[2] == 0) {
|
|
|
|
printf("Name: '%s'\n", &packet[9]);
|
|
|
|
devices[index].state = REMOTE_NAME_FETCHED;
|
|
|
|
} else {
|
|
|
|
printf("Failed to get name: page timeout\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue_remote_names();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2014-01-22 22:25:56 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-10-02 12:28:55 +00:00
|
|
|
|
2014-01-22 22:25:56 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 20:44:13 +00:00
|
|
|
/* @text For more details on discovering remote devices, please see
|
2015-06-24 15:21:05 +00:00
|
|
|
* Section on [GAP](../profiles/#sec:GAPdiscoverRemoteDevices).
|
2015-04-17 12:21:27 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-24 20:44:13 +00:00
|
|
|
|
2015-04-26 22:02:07 +00:00
|
|
|
/* @section Main Application Setup
|
2015-04-24 20:44:13 +00:00
|
|
|
*
|
|
|
|
* @text Listing MainConfiguration shows main application code.
|
2015-04-26 22:02:07 +00:00
|
|
|
* It registers the HCI packet handler and starts the Bluetooth stack.
|
2015-04-24 20:44:13 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-26 22:02:07 +00:00
|
|
|
/* LISTING_START(MainConfiguration): Setup packet handler for GAP inquiry */
|
2014-11-14 20:42:29 +00:00
|
|
|
int btstack_main(int argc, const char * argv[]);
|
|
|
|
int btstack_main(int argc, const char * argv[]) {
|
2016-12-22 21:16:16 +00:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2016-02-03 13:09:26 +00:00
|
|
|
|
2016-09-29 13:53:57 +00:00
|
|
|
// enabled EIR
|
|
|
|
hci_set_inquiry_mode(INQUIRY_MODE_RSSI_AND_EIR);
|
|
|
|
|
2018-08-24 08:03:14 +00:00
|
|
|
hci_event_callback_registration.callback = &packet_handler;
|
|
|
|
hci_add_event_handler(&hci_event_callback_registration);
|
|
|
|
|
2014-01-22 22:25:56 +00:00
|
|
|
// turn on!
|
2015-07-01 13:40:29 +00:00
|
|
|
hci_power_control(HCI_POWER_ON);
|
|
|
|
|
2014-01-22 22:25:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-04-24 20:44:13 +00:00
|
|
|
/* LISTING_END */
|
2015-04-17 12:21:27 +00:00
|
|
|
/* EXAMPLE_END */
|