2018-01-15 06:47:23 -05:00
|
|
|
/*
|
2022-12-08 23:23:36 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-12-06 01:59:55 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
|
|
|
*/
|
2017-11-24 04:28:43 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is for Classic Bluetooth device and service discovery Demo.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2022-12-08 23:23:36 -05:00
|
|
|
#include <inttypes.h>
|
2017-11-24 04:28:43 -05:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "nvs.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_log.h"
|
2017-12-22 02:13:45 -05:00
|
|
|
#include "esp_bt.h"
|
2017-11-24 04:28:43 -05:00
|
|
|
#include "esp_bt_main.h"
|
|
|
|
#include "esp_bt_device.h"
|
|
|
|
#include "esp_gap_bt_api.h"
|
|
|
|
|
|
|
|
#define GAP_TAG "GAP"
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
APP_GAP_STATE_IDLE = 0,
|
|
|
|
APP_GAP_STATE_DEVICE_DISCOVERING,
|
|
|
|
APP_GAP_STATE_DEVICE_DISCOVER_COMPLETE,
|
|
|
|
APP_GAP_STATE_SERVICE_DISCOVERING,
|
|
|
|
APP_GAP_STATE_SERVICE_DISCOVER_COMPLETE,
|
|
|
|
} app_gap_state_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
bool dev_found;
|
|
|
|
uint8_t bdname_len;
|
|
|
|
uint8_t eir_len;
|
|
|
|
uint8_t rssi;
|
|
|
|
uint32_t cod;
|
|
|
|
uint8_t eir[ESP_BT_GAP_EIR_DATA_LEN];
|
|
|
|
uint8_t bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
|
|
|
|
esp_bd_addr_t bda;
|
|
|
|
app_gap_state_t state;
|
|
|
|
} app_gap_cb_t;
|
|
|
|
|
|
|
|
static app_gap_cb_t m_dev_info;
|
|
|
|
|
|
|
|
static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
|
|
|
|
{
|
|
|
|
if (bda == NULL || str == NULL || size < 18) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *p = bda;
|
|
|
|
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
p[0], p[1], p[2], p[3], p[4], p[5]);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *uuid2str(esp_bt_uuid_t *uuid, char *str, size_t size)
|
|
|
|
{
|
|
|
|
if (uuid == NULL || str == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uuid->len == 2 && size >= 5) {
|
|
|
|
sprintf(str, "%04x", uuid->uuid.uuid16);
|
|
|
|
} else if (uuid->len == 4 && size >= 9) {
|
2022-12-08 23:23:36 -05:00
|
|
|
sprintf(str, "%08"PRIx32, uuid->uuid.uuid32);
|
2017-11-24 04:28:43 -05:00
|
|
|
} else if (uuid->len == 16 && size >= 37) {
|
|
|
|
uint8_t *p = uuid->uuid.uuid128;
|
|
|
|
sprintf(str, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
|
|
p[15], p[14], p[13], p[12], p[11], p[10], p[9], p[8],
|
|
|
|
p[7], p[6], p[5], p[4], p[3], p[2], p[1], p[0]);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_name_from_eir(uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len)
|
|
|
|
{
|
|
|
|
uint8_t *rmt_bdname = NULL;
|
|
|
|
uint8_t rmt_bdname_len = 0;
|
|
|
|
|
|
|
|
if (!eir) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_CMPL_LOCAL_NAME, &rmt_bdname_len);
|
|
|
|
if (!rmt_bdname) {
|
|
|
|
rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_SHORT_LOCAL_NAME, &rmt_bdname_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rmt_bdname) {
|
|
|
|
if (rmt_bdname_len > ESP_BT_GAP_MAX_BDNAME_LEN) {
|
|
|
|
rmt_bdname_len = ESP_BT_GAP_MAX_BDNAME_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bdname) {
|
|
|
|
memcpy(bdname, rmt_bdname, rmt_bdname_len);
|
|
|
|
bdname[rmt_bdname_len] = '\0';
|
|
|
|
}
|
|
|
|
if (bdname_len) {
|
|
|
|
*bdname_len = rmt_bdname_len;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_device_info(esp_bt_gap_cb_param_t *param)
|
|
|
|
{
|
|
|
|
char bda_str[18];
|
|
|
|
uint32_t cod = 0;
|
|
|
|
int32_t rssi = -129; /* invalid value */
|
2021-12-03 01:51:41 -05:00
|
|
|
uint8_t *bdname = NULL;
|
|
|
|
uint8_t bdname_len = 0;
|
|
|
|
uint8_t *eir = NULL;
|
|
|
|
uint8_t eir_len = 0;
|
2017-11-24 04:28:43 -05:00
|
|
|
esp_bt_gap_dev_prop_t *p;
|
|
|
|
|
|
|
|
ESP_LOGI(GAP_TAG, "Device found: %s", bda2str(param->disc_res.bda, bda_str, 18));
|
|
|
|
for (int i = 0; i < param->disc_res.num_prop; i++) {
|
|
|
|
p = param->disc_res.prop + i;
|
|
|
|
switch (p->type) {
|
|
|
|
case ESP_BT_GAP_DEV_PROP_COD:
|
|
|
|
cod = *(uint32_t *)(p->val);
|
2022-12-08 23:23:36 -05:00
|
|
|
ESP_LOGI(GAP_TAG, "--Class of Device: 0x%"PRIx32, cod);
|
2017-11-24 04:28:43 -05:00
|
|
|
break;
|
|
|
|
case ESP_BT_GAP_DEV_PROP_RSSI:
|
|
|
|
rssi = *(int8_t *)(p->val);
|
2022-12-08 23:23:36 -05:00
|
|
|
ESP_LOGI(GAP_TAG, "--RSSI: %"PRId32, rssi);
|
2017-11-24 04:28:43 -05:00
|
|
|
break;
|
|
|
|
case ESP_BT_GAP_DEV_PROP_BDNAME:
|
2021-12-03 01:51:41 -05:00
|
|
|
bdname_len = (p->len > ESP_BT_GAP_MAX_BDNAME_LEN) ? ESP_BT_GAP_MAX_BDNAME_LEN :
|
|
|
|
(uint8_t)p->len;
|
|
|
|
bdname = (uint8_t *)(p->val);
|
|
|
|
break;
|
|
|
|
case ESP_BT_GAP_DEV_PROP_EIR: {
|
|
|
|
eir_len = p->len;
|
|
|
|
eir = (uint8_t *)(p->val);
|
|
|
|
break;
|
|
|
|
}
|
2017-11-24 04:28:43 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 07:07:42 -05:00
|
|
|
/* search for device with Major device type "PHONE" or "Audio/Video" in COD */
|
2017-11-24 04:28:43 -05:00
|
|
|
app_gap_cb_t *p_dev = &m_dev_info;
|
2021-12-03 01:51:41 -05:00
|
|
|
if (p_dev->dev_found) {
|
2017-11-24 04:28:43 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!esp_bt_gap_is_valid_cod(cod) ||
|
2021-02-16 08:38:22 -05:00
|
|
|
(!(esp_bt_gap_get_cod_major_dev(cod) == ESP_BT_COD_MAJOR_DEV_PHONE) &&
|
|
|
|
!(esp_bt_gap_get_cod_major_dev(cod) == ESP_BT_COD_MAJOR_DEV_AV))) {
|
2017-11-24 04:28:43 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(p_dev->bda, param->disc_res.bda, ESP_BD_ADDR_LEN);
|
|
|
|
p_dev->dev_found = true;
|
2021-12-03 01:51:41 -05:00
|
|
|
|
|
|
|
p_dev->cod = cod;
|
|
|
|
p_dev->rssi = rssi;
|
|
|
|
if (bdname_len > 0) {
|
|
|
|
memcpy(p_dev->bdname, bdname, bdname_len);
|
|
|
|
p_dev->bdname[bdname_len] = '\0';
|
|
|
|
p_dev->bdname_len = bdname_len;
|
|
|
|
}
|
|
|
|
if (eir_len > 0) {
|
|
|
|
memcpy(p_dev->eir, eir, eir_len);
|
|
|
|
p_dev->eir_len = eir_len;
|
2017-11-24 04:28:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p_dev->eir && p_dev->bdname_len == 0) {
|
|
|
|
get_name_from_eir(p_dev->eir, p_dev->bdname, &p_dev->bdname_len);
|
|
|
|
}
|
2021-12-03 01:51:41 -05:00
|
|
|
|
|
|
|
ESP_LOGI(GAP_TAG, "Found a target device, address %s, name %s", bda_str, p_dev->bdname);
|
|
|
|
p_dev->state = APP_GAP_STATE_DEVICE_DISCOVER_COMPLETE;
|
|
|
|
ESP_LOGI(GAP_TAG, "Cancel device discovery ...");
|
|
|
|
esp_bt_gap_cancel_discovery();
|
2017-11-24 04:28:43 -05:00
|
|
|
}
|
|
|
|
|
2020-09-15 02:44:03 -04:00
|
|
|
static void bt_app_gap_init(void)
|
2017-11-24 04:28:43 -05:00
|
|
|
{
|
|
|
|
app_gap_cb_t *p_dev = &m_dev_info;
|
|
|
|
memset(p_dev, 0, sizeof(app_gap_cb_t));
|
2020-09-15 02:44:03 -04:00
|
|
|
|
2021-12-03 01:51:41 -05:00
|
|
|
p_dev->state = APP_GAP_STATE_IDLE;
|
2017-11-24 04:28:43 -05:00
|
|
|
}
|
|
|
|
|
2020-09-15 02:44:03 -04:00
|
|
|
static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
|
2017-11-24 04:28:43 -05:00
|
|
|
{
|
|
|
|
app_gap_cb_t *p_dev = &m_dev_info;
|
|
|
|
char bda_str[18];
|
|
|
|
char uuid_str[37];
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case ESP_BT_GAP_DISC_RES_EVT: {
|
|
|
|
update_device_info(param);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_BT_GAP_DISC_STATE_CHANGED_EVT: {
|
|
|
|
if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STOPPED) {
|
|
|
|
ESP_LOGI(GAP_TAG, "Device discovery stopped.");
|
|
|
|
if ( (p_dev->state == APP_GAP_STATE_DEVICE_DISCOVER_COMPLETE ||
|
|
|
|
p_dev->state == APP_GAP_STATE_DEVICE_DISCOVERING)
|
|
|
|
&& p_dev->dev_found) {
|
|
|
|
p_dev->state = APP_GAP_STATE_SERVICE_DISCOVERING;
|
|
|
|
ESP_LOGI(GAP_TAG, "Discover services ...");
|
|
|
|
esp_bt_gap_get_remote_services(p_dev->bda);
|
|
|
|
}
|
|
|
|
} else if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STARTED) {
|
|
|
|
ESP_LOGI(GAP_TAG, "Discovery started.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_BT_GAP_RMT_SRVCS_EVT: {
|
|
|
|
if (memcmp(param->rmt_srvcs.bda, p_dev->bda, ESP_BD_ADDR_LEN) == 0 &&
|
|
|
|
p_dev->state == APP_GAP_STATE_SERVICE_DISCOVERING) {
|
|
|
|
p_dev->state = APP_GAP_STATE_SERVICE_DISCOVER_COMPLETE;
|
|
|
|
if (param->rmt_srvcs.stat == ESP_BT_STATUS_SUCCESS) {
|
|
|
|
ESP_LOGI(GAP_TAG, "Services for device %s found", bda2str(p_dev->bda, bda_str, 18));
|
|
|
|
for (int i = 0; i < param->rmt_srvcs.num_uuids; i++) {
|
|
|
|
esp_bt_uuid_t *u = param->rmt_srvcs.uuid_list + i;
|
|
|
|
ESP_LOGI(GAP_TAG, "--%s", uuid2str(u, uuid_str, 37));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(GAP_TAG, "Services for device %s not found", bda2str(p_dev->bda, bda_str, 18));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_BT_GAP_RMT_SRVC_REC_EVT:
|
|
|
|
default: {
|
|
|
|
ESP_LOGI(GAP_TAG, "event: %d", event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-15 02:44:03 -04:00
|
|
|
static void bt_app_gap_start_up(void)
|
2017-11-24 04:28:43 -05:00
|
|
|
{
|
|
|
|
/* register GAP callback function */
|
|
|
|
esp_bt_gap_register_callback(bt_app_gap_cb);
|
|
|
|
|
2021-12-03 07:07:42 -05:00
|
|
|
char *dev_name = "ESP_GAP_INQRUIY";
|
|
|
|
esp_bt_dev_set_device_name(dev_name);
|
|
|
|
|
2020-09-15 02:44:03 -04:00
|
|
|
/* set discoverable and connectable mode, wait to be connected */
|
|
|
|
esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
|
|
|
|
|
2017-11-24 04:28:43 -05:00
|
|
|
/* inititialize device information and status */
|
2020-09-15 02:44:03 -04:00
|
|
|
bt_app_gap_init();
|
2017-11-24 04:28:43 -05:00
|
|
|
|
2021-12-03 01:51:41 -05:00
|
|
|
/* start to discover nearby Bluetooth devices */
|
|
|
|
app_gap_cb_t *p_dev = &m_dev_info;
|
|
|
|
p_dev->state = APP_GAP_STATE_DEVICE_DISCOVERING;
|
2017-11-24 04:28:43 -05:00
|
|
|
esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-11-24 04:28:43 -05:00
|
|
|
{
|
2021-12-03 07:07:42 -05:00
|
|
|
/* Initialize NVS — it is used to store PHY calibration data and save key-value pairs in flash memory*/
|
2017-11-24 04:28:43 -05:00
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 11:11:09 -04:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2017-11-24 04:28:43 -05:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
2018-09-10 23:20:30 -04:00
|
|
|
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
|
|
|
|
|
2017-11-24 04:28:43 -05:00
|
|
|
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
2018-02-22 08:48:53 -05:00
|
|
|
if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
|
|
|
|
ESP_LOGE(GAP_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
|
2017-11-24 04:28:43 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-10 23:18:13 -04:00
|
|
|
if ((ret = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) {
|
2018-02-22 08:48:53 -05:00
|
|
|
ESP_LOGE(GAP_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
|
2017-11-24 04:28:43 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 08:48:53 -05:00
|
|
|
if ((ret = esp_bluedroid_init()) != ESP_OK) {
|
|
|
|
ESP_LOGE(GAP_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
|
2017-11-24 04:28:43 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 08:48:53 -05:00
|
|
|
if ((ret = esp_bluedroid_enable()) != ESP_OK) {
|
|
|
|
ESP_LOGE(GAP_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
|
2017-11-24 04:28:43 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_app_gap_start_up();
|
|
|
|
}
|