kopia lustrzana https://github.com/espressif/esp-idf
Merge branch 'bugfix/backport_ble_fixes_v5.1' into 'release/v5.1'
Bugfix/backport ble fixes v5.1 See merge request espressif/esp-idf!29291pull/13572/head
commit
67c4eac799
|
@ -3,33 +3,35 @@ if(CONFIG_BT_ENABLED)
|
|||
set(srcs "")
|
||||
set(include_dirs "")
|
||||
set(ldfragments "linker.lf")
|
||||
if(CONFIG_BT_CONTROLLER_ENABLED)
|
||||
if(CONFIG_IDF_TARGET_ESP32)
|
||||
list(APPEND srcs "controller/esp32/bt.c"
|
||||
"controller/esp32/hli_api.c"
|
||||
"controller/esp32/hli_vectors.S")
|
||||
list(APPEND include_dirs include/esp32/include)
|
||||
|
||||
if(CONFIG_IDF_TARGET_ESP32)
|
||||
list(APPEND srcs "controller/esp32/bt.c"
|
||||
"controller/esp32/hli_api.c"
|
||||
"controller/esp32/hli_vectors.S")
|
||||
list(APPEND include_dirs include/esp32/include)
|
||||
elseif(CONFIG_IDF_TARGET_ESP32C3)
|
||||
list(APPEND srcs "controller/esp32c3/bt.c")
|
||||
list(APPEND include_dirs include/esp32c3/include)
|
||||
|
||||
elseif(CONFIG_IDF_TARGET_ESP32C3)
|
||||
list(APPEND srcs "controller/esp32c3/bt.c")
|
||||
list(APPEND include_dirs include/esp32c3/include)
|
||||
elseif(CONFIG_IDF_TARGET_ESP32S3)
|
||||
list(APPEND srcs "controller/esp32c3/bt.c")
|
||||
list(APPEND include_dirs include/esp32c3/include)
|
||||
|
||||
elseif(CONFIG_IDF_TARGET_ESP32S3)
|
||||
list(APPEND srcs "controller/esp32c3/bt.c")
|
||||
list(APPEND include_dirs include/esp32c3/include)
|
||||
elseif(CONFIG_IDF_TARGET_ESP32C2)
|
||||
set(ldfragments "linker.lf.esp32c2")
|
||||
list(APPEND srcs "controller/esp32c2/bt.c")
|
||||
list(APPEND include_dirs include/esp32c2/include)
|
||||
|
||||
elseif(CONFIG_IDF_TARGET_ESP32C2)
|
||||
set(ldfragments "linker.lf.esp32c2")
|
||||
list(APPEND srcs "controller/esp32c2/bt.c")
|
||||
list(APPEND include_dirs include/esp32c2/include)
|
||||
elseif(CONFIG_IDF_TARGET_ESP32C6)
|
||||
list(APPEND srcs "controller/esp32c6/bt.c")
|
||||
list(APPEND include_dirs include/esp32c6/include)
|
||||
|
||||
elseif(CONFIG_IDF_TARGET_ESP32C6)
|
||||
list(APPEND srcs "controller/esp32c6/bt.c")
|
||||
list(APPEND include_dirs include/esp32c6/include)
|
||||
elseif(CONFIG_IDF_TARGET_ESP32H2)
|
||||
list(APPEND srcs "controller/esp32h2/bt.c")
|
||||
list(APPEND include_dirs include/esp32h2/include)
|
||||
endif()
|
||||
|
||||
elseif(CONFIG_IDF_TARGET_ESP32H2)
|
||||
list(APPEND srcs "controller/esp32h2/bt.c")
|
||||
list(APPEND include_dirs include/esp32h2/include)
|
||||
endif()
|
||||
|
||||
|
||||
|
@ -483,6 +485,11 @@ if(CONFIG_BT_ENABLED)
|
|||
"porting/nimble/src/os_msys_init.c"
|
||||
)
|
||||
|
||||
if(CONFIG_BT_CONTROLLER_DISABLED)
|
||||
list(APPEND srcs
|
||||
"host/nimble/nimble/porting/nimble/src/hal_uart.c"
|
||||
)
|
||||
endif()
|
||||
list(APPEND include_dirs
|
||||
porting/include
|
||||
porting/nimble/include
|
||||
|
@ -524,6 +531,7 @@ if(CONFIG_BT_ENABLED)
|
|||
list(APPEND include_dirs
|
||||
|
||||
host/nimble/nimble/nimble/host/include
|
||||
host/nimble/nimble/nimble/include
|
||||
host/nimble/nimble/nimble/host/services/ans/include
|
||||
host/nimble/nimble/nimble/host/services/bas/include
|
||||
host/nimble/nimble/nimble/host/services/dis/include
|
||||
|
@ -613,6 +621,13 @@ if(CONFIG_BT_ENABLED)
|
|||
"host/nimble/nimble/nimble/host/src/ble_gattc_cache_conn.c"
|
||||
)
|
||||
|
||||
if(CONFIG_BT_CONTROLLER_DISABLED AND CONFIG_BT_NIMBLE_TRANSPORT_UART)
|
||||
list(APPEND srcs
|
||||
"host/nimble/nimble/nimble/transport/uart_ll/src/hci_uart.c"
|
||||
"host/nimble/nimble/nimble/transport/common/hci_h4/src/hci_h4.c"
|
||||
)
|
||||
endif()
|
||||
|
||||
list(APPEND srcs
|
||||
"host/nimble/nimble/porting/nimble/src/nimble_port.c"
|
||||
"host/nimble/nimble/porting/npl/freertos/src/nimble_port_freertos.c"
|
||||
|
@ -625,6 +640,12 @@ if(CONFIG_BT_ENABLED)
|
|||
host/nimble/nimble/nimble/transport/include
|
||||
)
|
||||
|
||||
if(CONFIG_BT_CONTROLLER_DISABLED)
|
||||
list(APPEND include_dirs
|
||||
host/nimble/nimble/nimble/transport/common/hci_h4/include
|
||||
)
|
||||
endif()
|
||||
|
||||
if(NOT CONFIG_BT_LE_CONTROLLER_NPL_OS_PORTING_SUPPORT)
|
||||
list(APPEND srcs
|
||||
"host/nimble/nimble/porting/nimble/src/endian.c"
|
||||
|
@ -634,6 +655,13 @@ if(CONFIG_BT_ENABLED)
|
|||
"host/nimble/nimble/porting/nimble/src/os_msys_init.c"
|
||||
"host/nimble/nimble/porting/npl/freertos/src/npl_os_freertos.c"
|
||||
)
|
||||
|
||||
if(CONFIG_BT_CONTROLLER_DISABLED AND CONFIG_BT_NIMBLE_TRANSPORT_UART)
|
||||
list(APPEND srcs
|
||||
"host/nimble/nimble/porting/nimble/src/hal_uart.c"
|
||||
)
|
||||
endif()
|
||||
|
||||
list(APPEND include_dirs
|
||||
host/nimble/nimble/porting/npl/freertos/include
|
||||
host/nimble/nimble/porting/nimble/include
|
||||
|
@ -641,7 +669,7 @@ if(CONFIG_BT_ENABLED)
|
|||
)
|
||||
endif()
|
||||
|
||||
if(CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE)
|
||||
if(CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE AND CONFIG_BT_CONTROLLER_ENABLED)
|
||||
list(APPEND srcs
|
||||
"host/nimble/esp-hci/src/esp_nimble_hci.c"
|
||||
)
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
menu "Bluetooth"
|
||||
visible if SOC_BT_SUPPORTED
|
||||
|
||||
config BT_ENABLED
|
||||
bool "Bluetooth"
|
||||
depends on SOC_BT_SUPPORTED && !APP_NO_BLOBS
|
||||
depends on !APP_NO_BLOBS
|
||||
help
|
||||
Select this option to enable Bluetooth and show the submenu with Bluetooth configuration choices.
|
||||
|
||||
|
@ -15,6 +14,7 @@ menu "Bluetooth"
|
|||
This helps to choose Bluetooth host stack
|
||||
|
||||
config BT_BLUEDROID_ENABLED
|
||||
depends on BT_CONTROLLER_ENABLED
|
||||
bool "Bluedroid - Dual-mode"
|
||||
help
|
||||
This option is recommended for classic Bluetooth or for dual-mode
|
||||
|
@ -26,6 +26,7 @@ menu "Bluetooth"
|
|||
This option is recommended for BLE only usecases to save on memory
|
||||
|
||||
config BT_CONTROLLER_ONLY
|
||||
depends on SOC_BT_SUPPORTED
|
||||
bool "Disabled"
|
||||
help
|
||||
This option is recommended when you want to communicate directly with the
|
||||
|
@ -42,6 +43,7 @@ menu "Bluetooth"
|
|||
This helps to choose Bluetooth controller stack
|
||||
|
||||
config BT_CONTROLLER_ENABLED
|
||||
depends on SOC_BT_SUPPORTED
|
||||
bool "Enabled"
|
||||
help
|
||||
This option is recommended for Bluetooth controller usecases
|
||||
|
|
|
@ -201,6 +201,17 @@ config BT_NIMBLE_LL_CFG_FEAT_LE_ENCRYPTION
|
|||
help
|
||||
Enable encryption connection
|
||||
|
||||
config BT_NIMBLE_SM_SC_LVL
|
||||
int "Security level"
|
||||
depends on BT_NIMBLE_SECURITY_ENABLE
|
||||
default 0
|
||||
help
|
||||
LE Security Mode 1 Levels:
|
||||
1. No Security
|
||||
2. Unauthenticated pairing with encryption
|
||||
3. Authenticated pairing with encryption
|
||||
4. Authenticated LE Secure Connections pairing with encryption using a 128-bit strength encryption key.
|
||||
|
||||
config BT_NIMBLE_DEBUG
|
||||
bool "Enable extra runtime asserts and host debugging"
|
||||
default n
|
||||
|
@ -756,3 +767,91 @@ config BT_NIMBLE_HOST_QUEUE_CONG_CHECK
|
|||
When scanning and scan duplicate is not enabled, if there are a lot of adv packets around
|
||||
or application layer handling adv packets is slow, it will cause the controller memory
|
||||
to run out. if enabled, adv packets will be lost when host queue is congested.
|
||||
|
||||
menu "Host-controller Transport"
|
||||
config BT_NIMBLE_TRANSPORT_UART
|
||||
bool "Enable Uart Transport"
|
||||
default y
|
||||
depends on BT_CONTROLLER_DISABLED
|
||||
help
|
||||
Use UART transport
|
||||
|
||||
config BT_NIMBLE_TRANSPORT_UART_PORT
|
||||
int "Uart port"
|
||||
depends on BT_CONTROLLER_DISABLED && BT_NIMBLE_TRANSPORT_UART
|
||||
default 1
|
||||
help
|
||||
Uart port
|
||||
|
||||
choice BT_NIMBLE_USE_HCI_UART_PARITY
|
||||
prompt "Uart PARITY"
|
||||
default UART_PARITY_NONE
|
||||
help
|
||||
Uart Parity
|
||||
|
||||
config UART_PARITY_NONE
|
||||
bool "None"
|
||||
config UART_PARITY_ODD
|
||||
bool "Odd"
|
||||
config UART_PARITY_EVEN
|
||||
bool "Even"
|
||||
endchoice
|
||||
|
||||
config BT_NIMBLE_TRANSPORT_UART_PARITY_NONE
|
||||
int
|
||||
default 0 if !UART_PARITY_NONE
|
||||
default 1 if UART_PARITY_NONE
|
||||
|
||||
config BT_NIMBLE_TRANSPORT_UART_PARITY_ODD
|
||||
int
|
||||
default 0 if !UART_PARITY_ODD
|
||||
default 1 if UART_PARITY_ODD
|
||||
|
||||
config BT_NIMBLE_TRANSPORT_UART_PARITY_EVEN
|
||||
int
|
||||
default 0 if !UART_PARITY_EVEN
|
||||
default 1 if UART_PARITY_EVEN
|
||||
|
||||
config BT_NIMBLE_UART_RX_PIN
|
||||
int "UART Rx pin"
|
||||
depends on BT_CONTROLLER_DISABLED && BT_NIMBLE_TRANSPORT_UART
|
||||
default 5
|
||||
help
|
||||
Rx pin for Nimble Transport
|
||||
|
||||
config BT_NIMBLE_UART_TX_PIN
|
||||
int "UART Tx pin"
|
||||
depends on BT_CONTROLLER_DISABLED && BT_NIMBLE_TRANSPORT_UART
|
||||
default 4
|
||||
help
|
||||
Tx pin for Nimble Transport
|
||||
|
||||
choice BT_NIMBLE_USE_HCI_UART_FLOW_CTRL
|
||||
prompt "Uart Flow Control"
|
||||
default UART_HW_FLOWCTRL_DISABLE
|
||||
help
|
||||
Uart Flow Control
|
||||
|
||||
config UART_HW_FLOWCTRL_DISABLE
|
||||
bool "Disable"
|
||||
config UART_HW_FLOWCTRL_CTS_RTS
|
||||
bool "Enable hardware flow control"
|
||||
endchoice
|
||||
|
||||
config BT_NIMBLE_HCI_UART_FLOW_CTRL
|
||||
int
|
||||
default 0 if UART_HW_FLOWCTRL_DISABLE
|
||||
default 1 if UART_HW_FLOWCTRL_CTS_RTS
|
||||
|
||||
config BT_NIMBLE_HCI_UART_RTS_PIN
|
||||
int "UART Rts Pin"
|
||||
default 19
|
||||
help
|
||||
UART HCI RTS pin
|
||||
|
||||
config BT_NIMBLE_HCI_UART_CTS_PIN
|
||||
int "UART Cts Pin"
|
||||
default 23
|
||||
help
|
||||
UART HCI CTS pin
|
||||
endmenu
|
||||
|
|
|
@ -15,7 +15,9 @@
|
|||
#include "esp_nimble_hci.h"
|
||||
#include "esp_nimble_mem.h"
|
||||
#include "bt_osi_mem.h"
|
||||
#if CONFIG_BT_CONTROLLER_ENABLED
|
||||
#include "esp_bt.h"
|
||||
#endif
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_compiler.h"
|
||||
#include "soc/soc_caps.h"
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 31335a5a4fd0e063edc086d3c268399404db3bfd
|
||||
Subproject commit c89851043b079a691d7eec788de36c1bba780956
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -844,7 +844,7 @@
|
|||
|
||||
|
||||
#ifndef MYNEWT_VAL_BLE_SM_SC_LVL
|
||||
#define MYNEWT_VAL_BLE_SM_SC_LVL (0)
|
||||
#define MYNEWT_VAL_BLE_SM_SC_LVL CONFIG_BT_NIMBLE_SM_SC_LVL
|
||||
#endif
|
||||
|
||||
#ifndef MYNEWT_VAL_BLE_SM_SC_ONLY
|
||||
|
@ -1773,4 +1773,38 @@
|
|||
#endif
|
||||
#endif
|
||||
|
||||
#if CONFIG_BT_CONTROLLER_DISABLED
|
||||
#ifndef MYNEWT_VAL_BLE_TRANSPORT_UART_PORT
|
||||
#define MYNEWT_VAL_BLE_TRANSPORT_UART_PORT CONFIG_BT_NIMBLE_TRANSPORT_UART_PORT
|
||||
#endif
|
||||
|
||||
#ifndef MYNEWT_VAL_BLE_TRANSPORT_UART_PARITY__none
|
||||
#define MYNEWT_VAL_BLE_TRANSPORT_UART_PARITY__none CONFIG_BT_NIMBLE_TRANSPORT_UART_PARITY_NONE
|
||||
#endif
|
||||
|
||||
#ifndef MYNEWT_VAL_BLE_TRANSPORT_UART_PARITY__odd
|
||||
#define MYNEWT_VAL_BLE_TRANSPORT_UART_PARITY__odd CONFIG_BT_NIMBLE_TRANSPORT_UART_PARITY_ODD
|
||||
#endif
|
||||
|
||||
#ifndef MYNEWT_VAL_BLE_TRANSPORT_UART_PARITY__even
|
||||
#define MYNEWT_VAL_BLE_TRANSPORT_UART_PARITY__even CONFIG_BT_NIMBLE_TRANSPORT_UART_PARITY_EVEN
|
||||
#endif
|
||||
|
||||
#ifndef MYNEWT_VAL_BLE_TRANSPORT_UART_FLOW_CONTROL__rtscts
|
||||
#define MYNEWT_VAL_BLE_TRANSPORT_UART_FLOW_CONTROL__rtscts CONFIG_BT_NIMBLE_HCI_UART_FLOW_CTRL
|
||||
#endif
|
||||
|
||||
#ifndef MYNEWT_VAL_BLE_TRANSPORT_UART_BAUDRATE
|
||||
#define MYNEWT_VAL_BLE_TRANSPORT_UART_BAUDRATE (921600)
|
||||
#endif
|
||||
|
||||
#ifndef MYNEWT_VAL_BLE_TRANSPORT_UART_DATA_BITS
|
||||
#define MYNEWT_VAL_BLE_TRANSPORT_UART_DATA_BITS (3)
|
||||
#endif
|
||||
|
||||
#ifndef MYNEWT_VAL_BLE_TRANSPORT_UART_STOP_BITS
|
||||
#define MYNEWT_VAL_BLE_TRANSPORT_UART_STOP_BITS (1)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
|
||||
|
||||
struct os_mempool;
|
||||
struct os_mbuf_pool;
|
||||
|
|
|
@ -63,7 +63,7 @@ extern "C" {
|
|||
typedef int ble_hci_trans_rx_cmd_fn(uint8_t *cmd, void *arg);
|
||||
typedef int ble_hci_trans_rx_acl_fn(struct os_mbuf *om, void *arg);
|
||||
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
|
||||
#define ble_transport_alloc_cmd() ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD)
|
||||
#define ble_transport_alloc_event(X) ble_hci_trans_buf_alloc(X ? BLE_HCI_TRANS_BUF_EVT_LO : BLE_HCI_TRANS_BUF_EVT_HI)
|
||||
#define ble_transport_free ble_hci_trans_buf_free
|
||||
|
|
Plik diff jest za duży
Load Diff
|
@ -92,6 +92,10 @@ extern "C" {
|
|||
#define NIMBLE_BLE_ATT_CLT_WRITE \
|
||||
(MYNEWT_VAL(BLE_GATT_WRITE))
|
||||
|
||||
#undef NIMBLE_BLE_ATT_CLT_SIGNED_WRITE
|
||||
#define NIMBLE_BLE_ATT_CLT_SIGNED_WRITE \
|
||||
(MYNEWT_VAL(BLE_GATT_SIGNED_WRITE))
|
||||
|
||||
#undef NIMBLE_BLE_ATT_CLT_WRITE_NO_RSP
|
||||
#define NIMBLE_BLE_ATT_CLT_WRITE_NO_RSP \
|
||||
(MYNEWT_VAL(BLE_GATT_WRITE_NO_RSP))
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
|
||||
#define NIMBLE_HS_STACK_SIZE CONFIG_BT_NIMBLE_HOST_TASK_STACK_SIZE
|
||||
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
|
||||
#define NIMBLE_LL_STACK_SIZE CONFIG_BT_LE_CONTROLLER_TASK_STACK_SIZE
|
||||
#endif
|
||||
|
||||
|
|
|
@ -213,7 +213,7 @@ extern "C" {
|
|||
|
||||
#endif
|
||||
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
|
||||
void r_put_le16(void *buf, uint16_t x);
|
||||
#define put_le16 r_put_le16
|
||||
|
||||
|
|
|
@ -244,7 +244,7 @@ _os_mbuf_trailingspace(struct os_mbuf *om)
|
|||
#define OS_MBUF_TRAILINGSPACE(__om) _os_mbuf_trailingspace(__om)
|
||||
|
||||
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
|
||||
/**
|
||||
* Initializes an mqueue. An mqueue is a queue of mbufs that ties to a
|
||||
* particular task's event queue. Mqueues form a helper API around a common
|
||||
|
|
|
@ -163,7 +163,7 @@ typedef __uint128_t os_membuf_t;
|
|||
#define OS_MEMPOOL_BYTES(n,blksize) \
|
||||
(sizeof (os_membuf_t) * OS_MEMPOOL_SIZE((n), (blksize)))
|
||||
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER
|
||||
#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED
|
||||
/**
|
||||
* Initialize a memory pool.
|
||||
*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -8,7 +8,9 @@
|
|||
#include <freertos/FreeRTOS.h>
|
||||
#include <esp_system.h>
|
||||
#include <esp_log.h>
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
#include "esp_bt.h"
|
||||
#endif
|
||||
#include <esp_gap_ble_api.h>
|
||||
#include <esp_gatts_api.h>
|
||||
#include <esp_bt_main.h>
|
||||
|
@ -213,6 +215,7 @@ esp_err_t simple_ble_start(simple_ble_cfg_t *cfg)
|
|||
ESP_LOGD(TAG, "Free mem at start of simple_ble_init %" PRIu32, esp_get_free_heap_size());
|
||||
esp_err_t ret;
|
||||
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
ret = esp_bt_controller_init(&bt_cfg);
|
||||
if (ret) {
|
||||
|
@ -232,6 +235,7 @@ esp_err_t simple_ble_start(simple_ble_cfg_t *cfg)
|
|||
ESP_LOGE(TAG, "%s enable controller failed %d", __func__, ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = esp_bluedroid_init();
|
||||
if (ret) {
|
||||
|
@ -306,6 +310,7 @@ esp_err_t simple_ble_stop(void)
|
|||
return err;
|
||||
}
|
||||
ESP_LOGD(TAG, "esp_bluedroid_deinit called successfully");
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
err = esp_bt_controller_disable();
|
||||
if (err != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
|
@ -320,7 +325,7 @@ esp_err_t simple_ble_stop(void)
|
|||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGD(TAG, "esp_bt_controller_deinit called successfully");
|
||||
|
||||
#endif
|
||||
ESP_LOGD(TAG, "Free mem at end of simple_ble_stop %" PRIu32, esp_get_free_heap_size());
|
||||
return ESP_OK;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
@ -7,7 +7,9 @@
|
|||
#include <string.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_err.h>
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
#include "esp_bt.h"
|
||||
#endif
|
||||
|
||||
#include <protocomm.h>
|
||||
#include <protocomm_ble.h>
|
||||
|
@ -197,9 +199,12 @@ static esp_err_t set_config_endpoint(void *config, const char *endpoint_name, ui
|
|||
/* Used when both BT and BLE are not needed by application */
|
||||
void wifi_prov_scheme_ble_event_cb_free_btdm(void *user_data, wifi_prov_cb_event_t event, void *event_data)
|
||||
{
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
esp_err_t err;
|
||||
#endif
|
||||
switch (event) {
|
||||
case WIFI_PROV_INIT:
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
/* Release BT memory, as we need only BLE */
|
||||
err = esp_bt_mem_release(ESP_BT_MODE_CLASSIC_BT);
|
||||
if (err != ESP_OK) {
|
||||
|
@ -207,10 +212,12 @@ void wifi_prov_scheme_ble_event_cb_free_btdm(void *user_data, wifi_prov_cb_event
|
|||
} else {
|
||||
ESP_LOGI(TAG, "BT memory released");
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
case WIFI_PROV_DEINIT:
|
||||
#ifndef CONFIG_WIFI_PROV_KEEP_BLE_ON_AFTER_PROV
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
/* Release memory used by BLE and Bluedroid host stack */
|
||||
err = esp_bt_mem_release(ESP_BT_MODE_BTDM);
|
||||
if (err != ESP_OK) {
|
||||
|
@ -218,6 +225,7 @@ void wifi_prov_scheme_ble_event_cb_free_btdm(void *user_data, wifi_prov_cb_event
|
|||
} else {
|
||||
ESP_LOGI(TAG, "BTDM memory released");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
break;
|
||||
|
||||
|
@ -229,9 +237,12 @@ void wifi_prov_scheme_ble_event_cb_free_btdm(void *user_data, wifi_prov_cb_event
|
|||
/* Used when BT is not needed by application */
|
||||
void wifi_prov_scheme_ble_event_cb_free_bt(void *user_data, wifi_prov_cb_event_t event, void *event_data)
|
||||
{
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
esp_err_t err;
|
||||
#endif
|
||||
switch (event) {
|
||||
case WIFI_PROV_INIT:
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
/* Release BT memory, as we need only BLE */
|
||||
err = esp_bt_mem_release(ESP_BT_MODE_CLASSIC_BT);
|
||||
if (err != ESP_OK) {
|
||||
|
@ -239,6 +250,7 @@ void wifi_prov_scheme_ble_event_cb_free_bt(void *user_data, wifi_prov_cb_event_t
|
|||
} else {
|
||||
ESP_LOGI(TAG, "BT memory released");
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -249,10 +261,13 @@ void wifi_prov_scheme_ble_event_cb_free_bt(void *user_data, wifi_prov_cb_event_t
|
|||
/* Used when BLE is not needed by application */
|
||||
void wifi_prov_scheme_ble_event_cb_free_ble(void *user_data, wifi_prov_cb_event_t event, void *event_data)
|
||||
{
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
esp_err_t err;
|
||||
#endif
|
||||
switch (event) {
|
||||
case WIFI_PROV_DEINIT:
|
||||
#ifndef CONFIG_WIFI_PROV_KEEP_BLE_ON_AFTER_PROV
|
||||
#ifdef CONFIG_BT_CONTROLLER_ENABLED
|
||||
/* Release memory used by BLE stack */
|
||||
err = esp_bt_mem_release(ESP_BT_MODE_BLE);
|
||||
if (err != ESP_OK) {
|
||||
|
@ -260,6 +275,7 @@ void wifi_prov_scheme_ble_event_cb_free_ble(void *user_data, wifi_prov_cb_event_
|
|||
} else {
|
||||
ESP_LOGI(TAG, "BLE memory released");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
break;
|
||||
|
||||
|
|
|
@ -287,6 +287,16 @@ examples/bluetooth/nimble/bleprph:
|
|||
temporary: true
|
||||
reason: The runner doesn't support yet
|
||||
|
||||
examples/bluetooth/nimble/bleprph_host_only:
|
||||
enable:
|
||||
- if: IDF_TARGET in ["esp32", "esp32c2", "esp32c3", "esp32c6" , "esp32s3", "esp32h2" ]
|
||||
temporary: true
|
||||
reason: the other targets are not tested yet
|
||||
disable_test:
|
||||
- if: IDF_TARGET in ["esp32c2", "esp32c3", "esp32h2", "esp32s3", "esp32c6"]
|
||||
temporary: true
|
||||
reason: The runner doesn't support yet
|
||||
|
||||
examples/bluetooth/nimble/bleprph_wifi_coex:
|
||||
enable:
|
||||
- if: IDF_TARGET in ["esp32", "esp32c2", "esp32c3", "esp32s3"]
|
||||
|
|
|
@ -24,7 +24,9 @@
|
|||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
|
||||
#include "esp_bt.h"
|
||||
#endif
|
||||
|
||||
#include "esp_blufi_api.h"
|
||||
#include "blufi_example.h"
|
||||
|
@ -463,11 +465,13 @@ void app_main(void)
|
|||
|
||||
initialise_wifi();
|
||||
|
||||
#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
|
||||
ret = esp_blufi_controller_init();
|
||||
if (ret) {
|
||||
BLUFI_ERROR("%s BLUFI controller init failed: %s\n", __func__, esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = esp_blufi_host_and_cb_init(&example_callbacks);
|
||||
if (ret) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
|
|
|
@ -16,7 +16,9 @@
|
|||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_random.h"
|
||||
#if CONFIG_BT_CONTROLLER_ENABLED || !CONFIG_BT_NIMBLE_ENABLED
|
||||
#include "esp_bt.h"
|
||||
#endif
|
||||
|
||||
#include "esp_blufi_api.h"
|
||||
#include "blufi_example.h"
|
||||
|
|
|
@ -393,6 +393,16 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
|
|||
}
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_AUTHORIZE:
|
||||
MODLOG_DFLT(INFO, "authorize event: conn_handle=%d attr_handle=%d is_read=%d",
|
||||
event->authorize.conn_handle,
|
||||
event->authorize.attr_handle,
|
||||
event->authorize.is_read);
|
||||
|
||||
/* The default behaviour for the event is to reject authorize request */
|
||||
event->authorize.out_response = BLE_GAP_AUTHORIZE_REJECT;
|
||||
return 0;
|
||||
|
||||
#if MYNEWT_VAL(BLE_POWER_CONTROL)
|
||||
case BLE_GAP_EVENT_TRANSMIT_POWER:
|
||||
MODLOG_DFLT(INFO, "Transmit power event : status=%d conn_handle=%d reason=%d "
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(bleprph_host_only)
|
|
@ -0,0 +1,114 @@
|
|||
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S3 |
|
||||
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- |
|
||||
|
||||
# BLE Peripheral Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example uses the UART transport written by application itself. Refer the file [main/uart_driver.c](main/uart_driver.c).
|
||||
|
||||
To write the custom transport in the application, the controller should be disabled and the default uart-transport should be disabled(When the controller is disabled, by default the uart-transport is selected). In order to compile the custom transport in the application, the default uart-transport should be disabled. Refer to the sdkconfig.defaults.
|
||||
|
||||
For more information about the application refer to the bleprph [README file](../bleprph/README.md)
|
||||
To test this demo, any BLE scanner app can be used.
|
||||
|
||||
Note :
|
||||
|
||||
* To install the dependency packages needed, please refer to the top level [README file](../../../README.md#running-test-python-script-pytest).
|
||||
* Currently this Python utility is only supported on Linux (BLE communication is via BLuez + DBus).
|
||||
|
||||
## How to Use Example
|
||||
|
||||
Before project configuration and build, be sure to set the correct chip target using:
|
||||
|
||||
```bash
|
||||
idf.py set-target <chip_name>
|
||||
```
|
||||
|
||||
### Configure the project
|
||||
|
||||
Open the project configuration menu:
|
||||
|
||||
```bash
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
In the `Component config-> Bluetooth` menu:
|
||||
|
||||
* Select `controller` to Disabled.
|
||||
* Disable `Nimble Options-> Host-controller Transport -> Enable Uart Transport`.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the [Getting Started Guide](https://idf.espressif.com/) for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
There is this console output when bleprph_host_only running on esp32 and using controller of esp32c6:
|
||||
|
||||
```
|
||||
I (31) boot: ESP-IDF v5.3-dev-1570-gbc703f3508-dirty 2nd stage bootloader
|
||||
I (31) boot: compile time Jan 18 2024 17:45:38
|
||||
I (33) boot: Multicore bootloader
|
||||
I (37) boot: chip revision: v3.0
|
||||
I (41) boot.esp32: SPI Speed : 40MHz
|
||||
I (46) boot.esp32: SPI Mode : DIO
|
||||
I (50) boot.esp32: SPI Flash Size : 2MB
|
||||
I (55) boot: Enabling RNG early entropy source...
|
||||
I (60) boot: Partition Table:
|
||||
I (64) boot: ## Label Usage Type ST Offset Length
|
||||
I (71) boot: 0 nvs WiFi data 01 02 00009000 00006000
|
||||
I (79) boot: 1 phy_init RF data 01 01 0000f000 00001000
|
||||
I (86) boot: 2 factory factory app 00 00 00010000 00100000
|
||||
I (94) boot: End of partition table
|
||||
I (98) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=19554h (103764) map
|
||||
I (142) esp_image: segment 1: paddr=0002957c vaddr=3ffb0000 size=0278ch ( 10124) load
|
||||
I (146) esp_image: segment 2: paddr=0002bd10 vaddr=40080000 size=04308h ( 17160) load
|
||||
I (155) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=42800h (272384) map
|
||||
I (250) esp_image: segment 4: paddr=00072828 vaddr=40084308 size=0a6ech ( 42732) load
|
||||
I (274) boot: Loaded app from partition at offset 0x10000
|
||||
I (274) boot: Disabling RNG early entropy source...
|
||||
I (285) cpu_start: Multicore app
|
||||
I (294) cpu_start: Pro cpu start user code
|
||||
I (294) cpu_start: cpu freq: 160000000 Hz
|
||||
I (294) cpu_start: Application information:
|
||||
I (297) cpu_start: Project name: bleprph
|
||||
I (302) cpu_start: App version: v5.3-dev-1570-gbc703f3508-dirty
|
||||
I (309) cpu_start: Compile time: Jan 18 2024 17:45:29
|
||||
I (315) cpu_start: ELF file SHA256: bd62addc6...
|
||||
I (321) cpu_start: ESP-IDF: v5.3-dev-1570-gbc703f3508-dirty
|
||||
I (328) cpu_start: Min chip rev: v0.0
|
||||
I (332) cpu_start: Max chip rev: v3.99
|
||||
I (337) cpu_start: Chip rev: v3.0
|
||||
I (342) heap_init: Initializing. RAM available for dynamic allocation:
|
||||
I (349) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
|
||||
I (355) heap_init: At 3FFB4AA8 len 0002B558 (173 KiB): DRAM
|
||||
I (362) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
|
||||
I (368) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
|
||||
I (374) heap_init: At 4008E9F4 len 0001160C (69 KiB): IRAM
|
||||
I (382) spi_flash: detected chip: generic
|
||||
I (385) spi_flash: flash io: dio
|
||||
W (389) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
|
||||
I (403) coexist: coex firmware version: c02915e
|
||||
I (408) main_task: Started on CPU0
|
||||
I (418) main_task: Calling app_main()
|
||||
I (428) NimBLE_BLE_PRPH: BLE Host Task Started
|
||||
I (438) uart: queue free spaces: 8
|
||||
I (438) main_task: Returned from app_main()
|
||||
I (478) NimBLE: Device Address:
|
||||
I (478) NimBLE: 60:55:f9:f6:07:e2
|
||||
I (478) NimBLE:
|
||||
|
||||
I (488) NimBLE: GAP procedure initiated: advertise;
|
||||
I (488) NimBLE: disc_mode=2
|
||||
I (488) NimBLE: adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=0 adv_itvl_max=0
|
||||
I (498) NimBLE:
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
|
@ -0,0 +1,6 @@
|
|||
set(srcs "main.c"
|
||||
"gatt_svr.c"
|
||||
"uart_driver.c")
|
||||
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS ".")
|
|
@ -0,0 +1,140 @@
|
|||
menu "Example Configuration"
|
||||
|
||||
menu "Uart Configuration"
|
||||
|
||||
config EXAMPLE_HCI_UART_BAUDRATE
|
||||
int "UART Baudrate for HCI"
|
||||
range 115200 921600
|
||||
default 921600
|
||||
help
|
||||
UART Baudrate for HCI. Please use standard baudrate.
|
||||
|
||||
config EXAMPLE_HCI_UART_TX_PIN
|
||||
int "UART Tx Pin"
|
||||
default 4
|
||||
help
|
||||
UART HCI Tx pin
|
||||
|
||||
config EXAMPLE_HCI_UART_RX_PIN
|
||||
int "UART Rx Pin"
|
||||
default 5
|
||||
help
|
||||
UART HCI Rx pin
|
||||
|
||||
choice EXAMPLE_USE_HCI_UART_FLOW_CTRL
|
||||
prompt "Uart Flow Control"
|
||||
default EXAMPLE_UART_HW_FLOWCTRL_DISABLE
|
||||
help
|
||||
Uart Flow Control
|
||||
|
||||
config EXAMPLE_UART_HW_FLOWCTRL_DISABLE
|
||||
bool "Disable"
|
||||
config EXAMPLE_UART_HW_FLOWCTRL_RTS
|
||||
bool "Enable Rx hardware flow control"
|
||||
config EXAMPLE_UART_HW_FLOWCTRL_CTS
|
||||
bool "Enable Tx hardware flow control"
|
||||
config EXAMPLE_UART_HW_FLOWCTRL_CTS_RTS
|
||||
bool "Enable hardware flow control"
|
||||
config EXAMPLE_UART_HW_FLOWCTRL_MAX
|
||||
bool "Hardware Flow control max"
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_HCI_UART_FLOW_CTRL
|
||||
int
|
||||
default 0 if EXAMPLE_UART_HW_FLOWCTRL_DISABLE
|
||||
default 1 if EXAMPLE_UART_HW_FLOWCTRL_RTS
|
||||
default 2 if EXAMPLE_UART_HW_FLOWCTRL_CTS
|
||||
default 3 if EXAMPLE_UART_HW_FLOWCTRL_CTS_RTS
|
||||
default 4 if EXAMPLE_UART_HW_FLOWCTRL_MAX
|
||||
|
||||
config EXAMPLE_HCI_UART_RTS_PIN
|
||||
int "UART Rts Pin"
|
||||
default 19
|
||||
help
|
||||
UART HCI RTS pin
|
||||
|
||||
config EXAMPLE_HCI_UART_CTS_PIN
|
||||
int "UART Cts Pin"
|
||||
default 23
|
||||
help
|
||||
UART HCI CTS pin
|
||||
endmenu
|
||||
|
||||
choice EXAMPLE_USE_IO_TYPE
|
||||
prompt "I/O Capability"
|
||||
default BLE_SM_IO_CAP_NO_IO
|
||||
help
|
||||
I/O capability of device.
|
||||
|
||||
config BLE_SM_IO_CAP_DISP_ONLY
|
||||
bool "DISPLAY ONLY"
|
||||
config BLE_SM_IO_CAP_DISP_YES_NO
|
||||
bool "DISPLAY YESNO"
|
||||
config BLE_SM_IO_CAP_KEYBOARD_ONLY
|
||||
bool "KEYBOARD ONLY"
|
||||
config BLE_SM_IO_CAP_NO_IO
|
||||
bool "Just works"
|
||||
config BLE_SM_IO_CAP_KEYBOARD_DISP
|
||||
bool "Both KEYBOARD & DISPLAY"
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_IO_TYPE
|
||||
int
|
||||
default 0 if BLE_SM_IO_CAP_DISP_ONLY
|
||||
default 1 if BLE_SM_IO_CAP_DISP_YES_NO
|
||||
default 2 if BLE_SM_IO_CAP_KEYBOARD_ONLY
|
||||
default 3 if BLE_SM_IO_CAP_NO_IO
|
||||
default 4 if BLE_SM_IO_CAP_KEYBOARD_DISP
|
||||
|
||||
config EXAMPLE_BONDING
|
||||
bool
|
||||
default n
|
||||
prompt "Use Bonding"
|
||||
help
|
||||
Use this option to enable/disable bonding.
|
||||
|
||||
config EXAMPLE_MITM
|
||||
bool
|
||||
default n
|
||||
prompt "MITM security"
|
||||
help
|
||||
Use this option to enable/disable MITM security.
|
||||
|
||||
config EXAMPLE_USE_SC
|
||||
bool
|
||||
depends on BT_NIMBLE_SM_SC
|
||||
default n
|
||||
prompt "Use Secure Connection feature"
|
||||
help
|
||||
Use this option to enable/disable Security Manager Secure Connection 4.2 feature.
|
||||
|
||||
config EXAMPLE_EXTENDED_ADV
|
||||
bool
|
||||
depends on SOC_BLE_50_SUPPORTED
|
||||
default y if SOC_ESP_NIMBLE_CONTROLLER
|
||||
select BT_NIMBLE_EXT_ADV
|
||||
prompt "Enable Extended Adv"
|
||||
help
|
||||
Use this option to enable extended advertising in the example.
|
||||
If this option is disabled, ensure config BT_NIMBLE_EXT_ADV is
|
||||
also disabled from Nimble stack menuconfig
|
||||
|
||||
config EXAMPLE_RANDOM_ADDR
|
||||
bool
|
||||
prompt "Advertise RANDOM Address"
|
||||
help
|
||||
Use this option to advertise a random address instead of public address
|
||||
|
||||
config EXAMPLE_ENCRYPTION
|
||||
bool
|
||||
prompt "Enable Link Encryption"
|
||||
help
|
||||
This adds Encrypted Read and Write permissions in the custom GATT server.
|
||||
|
||||
config EXAMPLE_RESOLVE_PEER_ADDR
|
||||
bool
|
||||
prompt "Enable resolving peer address"
|
||||
help
|
||||
Use this option to enable resolving peer's address.
|
||||
|
||||
endmenu
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#ifndef H_BLEPRPH_
|
||||
#define H_BLEPRPH_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "nimble/ble.h"
|
||||
#include "modlog/modlog.h"
|
||||
#include "esp_peripheral.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ble_hs_cfg;
|
||||
struct ble_gatt_register_ctxt;
|
||||
|
||||
/** GATT server. */
|
||||
#define GATT_SVR_SVC_ALERT_UUID 0x1811
|
||||
#define GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID 0x2A47
|
||||
#define GATT_SVR_CHR_NEW_ALERT 0x2A46
|
||||
#define GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID 0x2A48
|
||||
#define GATT_SVR_CHR_UNR_ALERT_STAT_UUID 0x2A45
|
||||
#define GATT_SVR_CHR_ALERT_NOT_CTRL_PT 0x2A44
|
||||
|
||||
void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg);
|
||||
int gatt_svr_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,247 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/ble_uuid.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "services/gatt/ble_svc_gatt.h"
|
||||
#include "bleprph.h"
|
||||
#include "services/ans/ble_svc_ans.h"
|
||||
|
||||
/*** Maximum number of characteristics with the notify flag ***/
|
||||
#define MAX_NOTIFY 5
|
||||
|
||||
static const ble_uuid128_t gatt_svr_svc_uuid =
|
||||
BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
|
||||
0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
|
||||
|
||||
/* A characteristic that can be subscribed to */
|
||||
static uint8_t gatt_svr_chr_val;
|
||||
static uint16_t gatt_svr_chr_val_handle;
|
||||
static const ble_uuid128_t gatt_svr_chr_uuid =
|
||||
BLE_UUID128_INIT(0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,
|
||||
0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33);
|
||||
|
||||
/* A custom descriptor */
|
||||
static uint8_t gatt_svr_dsc_val;
|
||||
static const ble_uuid128_t gatt_svr_dsc_uuid =
|
||||
BLE_UUID128_INIT(0x01, 0x01, 0x01, 0x01, 0x12, 0x12, 0x12, 0x12,
|
||||
0x23, 0x23, 0x23, 0x23, 0x34, 0x34, 0x34, 0x34);
|
||||
|
||||
static int
|
||||
gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt,
|
||||
void *arg);
|
||||
|
||||
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
|
||||
{
|
||||
/*** Service ***/
|
||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||
.uuid = &gatt_svr_svc_uuid.u,
|
||||
.characteristics = (struct ble_gatt_chr_def[])
|
||||
{ {
|
||||
/*** This characteristic can be subscribed to by writing 0x00 and 0x01 to the CCCD ***/
|
||||
.uuid = &gatt_svr_chr_uuid.u,
|
||||
.access_cb = gatt_svc_access,
|
||||
#if CONFIG_EXAMPLE_ENCRYPTION
|
||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE |
|
||||
BLE_GATT_CHR_F_READ_ENC | BLE_GATT_CHR_F_WRITE_ENC |
|
||||
BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE,
|
||||
#else
|
||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE,
|
||||
#endif
|
||||
.val_handle = &gatt_svr_chr_val_handle,
|
||||
.descriptors = (struct ble_gatt_dsc_def[])
|
||||
{ {
|
||||
.uuid = &gatt_svr_dsc_uuid.u,
|
||||
#if CONFIG_EXAMPLE_ENCRYPTION
|
||||
.att_flags = BLE_ATT_F_READ | BLE_ATT_F_READ_ENC,
|
||||
#else
|
||||
.att_flags = BLE_ATT_F_READ,
|
||||
#endif
|
||||
.access_cb = gatt_svc_access,
|
||||
}, {
|
||||
0, /* No more descriptors in this characteristic */
|
||||
}
|
||||
},
|
||||
}, {
|
||||
0, /* No more characteristics in this service. */
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
0, /* No more services. */
|
||||
},
|
||||
};
|
||||
|
||||
static int
|
||||
gatt_svr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
|
||||
void *dst, uint16_t *len)
|
||||
{
|
||||
uint16_t om_len;
|
||||
int rc;
|
||||
|
||||
om_len = OS_MBUF_PKTLEN(om);
|
||||
if (om_len < min_len || om_len > max_len) {
|
||||
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
||||
}
|
||||
|
||||
rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
|
||||
if (rc != 0) {
|
||||
return BLE_ATT_ERR_UNLIKELY;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access callback whenever a characteristic/descriptor is read or written to.
|
||||
* Here reads and writes need to be handled.
|
||||
* ctxt->op tells weather the operation is read or write and
|
||||
* weather it is on a characteristic or descriptor,
|
||||
* ctxt->dsc->uuid tells which characteristic/descriptor is accessed.
|
||||
* attr_handle give the value handle of the attribute being accessed.
|
||||
* Accordingly do:
|
||||
* Append the value to ctxt->om if the operation is READ
|
||||
* Write ctxt->om to the value if the operation is WRITE
|
||||
**/
|
||||
static int
|
||||
gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg)
|
||||
{
|
||||
const ble_uuid_t *uuid;
|
||||
int rc;
|
||||
|
||||
switch (ctxt->op) {
|
||||
case BLE_GATT_ACCESS_OP_READ_CHR:
|
||||
if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
|
||||
MODLOG_DFLT(INFO, "Characteristic read; conn_handle=%d attr_handle=%d\n",
|
||||
conn_handle, attr_handle);
|
||||
} else {
|
||||
MODLOG_DFLT(INFO, "Characteristic read by NimBLE stack; attr_handle=%d\n",
|
||||
attr_handle);
|
||||
}
|
||||
uuid = ctxt->chr->uuid;
|
||||
if (attr_handle == gatt_svr_chr_val_handle) {
|
||||
rc = os_mbuf_append(ctxt->om,
|
||||
&gatt_svr_chr_val,
|
||||
sizeof(gatt_svr_chr_val));
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
goto unknown;
|
||||
|
||||
case BLE_GATT_ACCESS_OP_WRITE_CHR:
|
||||
if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
|
||||
MODLOG_DFLT(INFO, "Characteristic write; conn_handle=%d attr_handle=%d",
|
||||
conn_handle, attr_handle);
|
||||
} else {
|
||||
MODLOG_DFLT(INFO, "Characteristic write by NimBLE stack; attr_handle=%d",
|
||||
attr_handle);
|
||||
}
|
||||
uuid = ctxt->chr->uuid;
|
||||
if (attr_handle == gatt_svr_chr_val_handle) {
|
||||
rc = gatt_svr_write(ctxt->om,
|
||||
sizeof(gatt_svr_chr_val),
|
||||
sizeof(gatt_svr_chr_val),
|
||||
&gatt_svr_chr_val, NULL);
|
||||
ble_gatts_chr_updated(attr_handle);
|
||||
MODLOG_DFLT(INFO, "Notification/Indication scheduled for "
|
||||
"all subscribed peers.\n");
|
||||
return rc;
|
||||
}
|
||||
goto unknown;
|
||||
|
||||
case BLE_GATT_ACCESS_OP_READ_DSC:
|
||||
if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
|
||||
MODLOG_DFLT(INFO, "Descriptor read; conn_handle=%d attr_handle=%d\n",
|
||||
conn_handle, attr_handle);
|
||||
} else {
|
||||
MODLOG_DFLT(INFO, "Descriptor read by NimBLE stack; attr_handle=%d\n",
|
||||
attr_handle);
|
||||
}
|
||||
uuid = ctxt->dsc->uuid;
|
||||
if (ble_uuid_cmp(uuid, &gatt_svr_dsc_uuid.u) == 0) {
|
||||
rc = os_mbuf_append(ctxt->om,
|
||||
&gatt_svr_dsc_val,
|
||||
sizeof(gatt_svr_chr_val));
|
||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
||||
}
|
||||
goto unknown;
|
||||
|
||||
case BLE_GATT_ACCESS_OP_WRITE_DSC:
|
||||
goto unknown;
|
||||
|
||||
default:
|
||||
goto unknown;
|
||||
}
|
||||
|
||||
unknown:
|
||||
/* Unknown characteristic/descriptor;
|
||||
* The NimBLE host should not have called this function;
|
||||
*/
|
||||
assert(0);
|
||||
return BLE_ATT_ERR_UNLIKELY;
|
||||
}
|
||||
|
||||
void
|
||||
gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
|
||||
{
|
||||
char buf[BLE_UUID_STR_LEN];
|
||||
|
||||
switch (ctxt->op) {
|
||||
case BLE_GATT_REGISTER_OP_SVC:
|
||||
MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
|
||||
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
|
||||
ctxt->svc.handle);
|
||||
break;
|
||||
|
||||
case BLE_GATT_REGISTER_OP_CHR:
|
||||
MODLOG_DFLT(DEBUG, "registering characteristic %s with "
|
||||
"def_handle=%d val_handle=%d\n",
|
||||
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
|
||||
ctxt->chr.def_handle,
|
||||
ctxt->chr.val_handle);
|
||||
break;
|
||||
|
||||
case BLE_GATT_REGISTER_OP_DSC:
|
||||
MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
|
||||
ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
|
||||
ctxt->dsc.handle);
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
gatt_svr_init(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
ble_svc_gap_init();
|
||||
ble_svc_gatt_init();
|
||||
ble_svc_ans_init();
|
||||
|
||||
rc = ble_gatts_count_cfg(gatt_svr_svcs);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = ble_gatts_add_svcs(gatt_svr_svcs);
|
||||
if (rc != 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Setting a value for the read-only descriptor */
|
||||
gatt_svr_dsc_val = 0x99;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
dependencies:
|
||||
nimble_peripheral_utils:
|
||||
path: ${IDF_PATH}/examples/bluetooth/nimble/common/nimble_peripheral_utils
|
|
@ -0,0 +1,548 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
/* BLE */
|
||||
#include "nimble/nimble_port.h"
|
||||
#include "nimble/nimble_port_freertos.h"
|
||||
#include "host/ble_hs.h"
|
||||
#include "host/util/util.h"
|
||||
#include "console/console.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "bleprph.h"
|
||||
#include "uart_driver.h"
|
||||
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
static uint8_t ext_adv_pattern_1[] = {
|
||||
0x02, 0x01, 0x06,
|
||||
0x03, 0x03, 0xab, 0xcd,
|
||||
0x03, 0x03, 0x18, 0x11,
|
||||
0x11, 0X09, 'n', 'i', 'm', 'b', 'l', 'e', '-', 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'e',
|
||||
};
|
||||
#endif
|
||||
|
||||
static const char *tag = "NimBLE_BLE_PRPH";
|
||||
static int bleprph_gap_event(struct ble_gap_event *event, void *arg);
|
||||
#if CONFIG_EXAMPLE_RANDOM_ADDR
|
||||
static uint8_t own_addr_type = BLE_OWN_ADDR_RANDOM;
|
||||
#else
|
||||
static uint8_t own_addr_type;
|
||||
#endif
|
||||
|
||||
void ble_store_config_init(void);
|
||||
|
||||
/**
|
||||
* Logs information about a connection to the console.
|
||||
*/
|
||||
static void
|
||||
bleprph_print_conn_desc(struct ble_gap_conn_desc *desc)
|
||||
{
|
||||
MODLOG_DFLT(INFO, "handle=%d our_ota_addr_type=%d our_ota_addr=",
|
||||
desc->conn_handle, desc->our_ota_addr.type);
|
||||
print_addr(desc->our_ota_addr.val);
|
||||
MODLOG_DFLT(INFO, " our_id_addr_type=%d our_id_addr=",
|
||||
desc->our_id_addr.type);
|
||||
print_addr(desc->our_id_addr.val);
|
||||
MODLOG_DFLT(INFO, " peer_ota_addr_type=%d peer_ota_addr=",
|
||||
desc->peer_ota_addr.type);
|
||||
print_addr(desc->peer_ota_addr.val);
|
||||
MODLOG_DFLT(INFO, " peer_id_addr_type=%d peer_id_addr=",
|
||||
desc->peer_id_addr.type);
|
||||
print_addr(desc->peer_id_addr.val);
|
||||
MODLOG_DFLT(INFO, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
|
||||
"encrypted=%d authenticated=%d bonded=%d\n",
|
||||
desc->conn_itvl, desc->conn_latency,
|
||||
desc->supervision_timeout,
|
||||
desc->sec_state.encrypted,
|
||||
desc->sec_state.authenticated,
|
||||
desc->sec_state.bonded);
|
||||
}
|
||||
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
/**
|
||||
* Enables advertising with the following parameters:
|
||||
* o General discoverable mode.
|
||||
* o Undirected connectable mode.
|
||||
*/
|
||||
static void
|
||||
ext_bleprph_advertise(void)
|
||||
{
|
||||
struct ble_gap_ext_adv_params params;
|
||||
struct os_mbuf *data;
|
||||
uint8_t instance = 0;
|
||||
int rc;
|
||||
|
||||
/* First check if any instance is already active */
|
||||
if(ble_gap_ext_adv_active(instance)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* use defaults for non-set params */
|
||||
memset (¶ms, 0, sizeof(params));
|
||||
|
||||
/* enable connectable advertising */
|
||||
params.connectable = 1;
|
||||
|
||||
/* advertise using random addr */
|
||||
params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
|
||||
|
||||
params.primary_phy = BLE_HCI_LE_PHY_1M;
|
||||
params.secondary_phy = BLE_HCI_LE_PHY_2M;
|
||||
//params.tx_power = 127;
|
||||
params.sid = 1;
|
||||
|
||||
params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
|
||||
params.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
|
||||
|
||||
/* configure instance 0 */
|
||||
rc = ble_gap_ext_adv_configure(instance, ¶ms, NULL,
|
||||
bleprph_gap_event, NULL);
|
||||
assert (rc == 0);
|
||||
|
||||
/* in this case only scan response is allowed */
|
||||
|
||||
/* get mbuf for scan rsp data */
|
||||
data = os_msys_get_pkthdr(sizeof(ext_adv_pattern_1), 0);
|
||||
assert(data);
|
||||
|
||||
/* fill mbuf with scan rsp data */
|
||||
rc = os_mbuf_append(data, ext_adv_pattern_1, sizeof(ext_adv_pattern_1));
|
||||
assert(rc == 0);
|
||||
|
||||
rc = ble_gap_ext_adv_set_data(instance, data);
|
||||
assert (rc == 0);
|
||||
|
||||
/* start advertising */
|
||||
rc = ble_gap_ext_adv_start(instance, 0, 0);
|
||||
assert (rc == 0);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* Enables advertising with the following parameters:
|
||||
* o General discoverable mode.
|
||||
* o Undirected connectable mode.
|
||||
*/
|
||||
static void
|
||||
bleprph_advertise(void)
|
||||
{
|
||||
struct ble_gap_adv_params adv_params;
|
||||
struct ble_hs_adv_fields fields;
|
||||
const char *name;
|
||||
int rc;
|
||||
|
||||
/**
|
||||
* Set the advertisement data included in our advertisements:
|
||||
* o Flags (indicates advertisement type and other general info).
|
||||
* o Advertising tx power.
|
||||
* o Device name.
|
||||
* o 16-bit service UUIDs (alert notifications).
|
||||
*/
|
||||
|
||||
memset(&fields, 0, sizeof fields);
|
||||
|
||||
/* Advertise two flags:
|
||||
* o Discoverability in forthcoming advertisement (general)
|
||||
* o BLE-only (BR/EDR unsupported).
|
||||
*/
|
||||
fields.flags = BLE_HS_ADV_F_DISC_GEN |
|
||||
BLE_HS_ADV_F_BREDR_UNSUP;
|
||||
|
||||
/* Indicate that the TX power level field should be included; have the
|
||||
* stack fill this value automatically. This is done by assigning the
|
||||
* special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
|
||||
*/
|
||||
fields.tx_pwr_lvl_is_present = 1;
|
||||
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
|
||||
|
||||
name = ble_svc_gap_device_name();
|
||||
fields.name = (uint8_t *)name;
|
||||
fields.name_len = strlen(name);
|
||||
fields.name_is_complete = 1;
|
||||
|
||||
fields.uuids16 = (ble_uuid16_t[]) {
|
||||
BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID)
|
||||
};
|
||||
fields.num_uuids16 = 1;
|
||||
fields.uuids16_is_complete = 1;
|
||||
|
||||
rc = ble_gap_adv_set_fields(&fields);
|
||||
if (rc != 0) {
|
||||
MODLOG_DFLT(ERROR, "error setting advertisement data; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Begin advertising. */
|
||||
memset(&adv_params, 0, sizeof adv_params);
|
||||
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
|
||||
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
||||
rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER,
|
||||
&adv_params, bleprph_gap_event, NULL);
|
||||
if (rc != 0) {
|
||||
MODLOG_DFLT(ERROR, "error enabling advertisement; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MYNEWT_VAL(BLE_POWER_CONTROL)
|
||||
static void bleprph_power_control(uint16_t conn_handle)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = ble_gap_read_remote_transmit_power_level(conn_handle, 0x01 ); // Attempting on LE 1M phy
|
||||
assert (rc == 0);
|
||||
|
||||
rc = ble_gap_set_transmit_power_reporting_enable(conn_handle, 0x1, 0x1);
|
||||
assert (rc == 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* The nimble host executes this callback when a GAP event occurs. The
|
||||
* application associates a GAP event callback with each connection that forms.
|
||||
* bleprph uses the same callback for all connections.
|
||||
*
|
||||
* @param event The type of event being signalled.
|
||||
* @param ctxt Various information pertaining to the event.
|
||||
* @param arg Application-specified argument; unused by
|
||||
* bleprph.
|
||||
*
|
||||
* @return 0 if the application successfully handled the
|
||||
* event; nonzero on failure. The semantics
|
||||
* of the return code is specific to the
|
||||
* particular GAP event being signalled.
|
||||
*/
|
||||
static int
|
||||
bleprph_gap_event(struct ble_gap_event *event, void *arg)
|
||||
{
|
||||
struct ble_gap_conn_desc desc;
|
||||
int rc;
|
||||
|
||||
switch (event->type) {
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
/* A new connection was established or a connection attempt failed. */
|
||||
MODLOG_DFLT(INFO, "connection %s; status=%d ",
|
||||
event->connect.status == 0 ? "established" : "failed",
|
||||
event->connect.status);
|
||||
if (event->connect.status == 0) {
|
||||
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
bleprph_print_conn_desc(&desc);
|
||||
}
|
||||
MODLOG_DFLT(INFO, "\n");
|
||||
|
||||
if (event->connect.status != 0) {
|
||||
/* Connection failed; resume advertising. */
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_bleprph_advertise();
|
||||
#else
|
||||
bleprph_advertise();
|
||||
#endif
|
||||
}
|
||||
|
||||
#if MYNEWT_VAL(BLE_POWER_CONTROL)
|
||||
bleprph_power_control(event->connect.conn_handle);
|
||||
#endif
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
MODLOG_DFLT(INFO, "disconnect; reason=%d ", event->disconnect.reason);
|
||||
bleprph_print_conn_desc(&event->disconnect.conn);
|
||||
MODLOG_DFLT(INFO, "\n");
|
||||
|
||||
/* Connection terminated; resume advertising. */
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_bleprph_advertise();
|
||||
#else
|
||||
bleprph_advertise();
|
||||
#endif
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_CONN_UPDATE:
|
||||
/* The central has updated the connection parameters. */
|
||||
MODLOG_DFLT(INFO, "connection updated; status=%d ",
|
||||
event->conn_update.status);
|
||||
rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
bleprph_print_conn_desc(&desc);
|
||||
MODLOG_DFLT(INFO, "\n");
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_ADV_COMPLETE:
|
||||
MODLOG_DFLT(INFO, "advertise complete; reason=%d",
|
||||
event->adv_complete.reason);
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_bleprph_advertise();
|
||||
#else
|
||||
bleprph_advertise();
|
||||
#endif
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_ENC_CHANGE:
|
||||
/* Encryption has been enabled or disabled for this connection. */
|
||||
MODLOG_DFLT(INFO, "encryption change event; status=%d ",
|
||||
event->enc_change.status);
|
||||
rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
bleprph_print_conn_desc(&desc);
|
||||
MODLOG_DFLT(INFO, "\n");
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_NOTIFY_TX:
|
||||
MODLOG_DFLT(INFO, "notify_tx event; conn_handle=%d attr_handle=%d "
|
||||
"status=%d is_indication=%d",
|
||||
event->notify_tx.conn_handle,
|
||||
event->notify_tx.attr_handle,
|
||||
event->notify_tx.status,
|
||||
event->notify_tx.indication);
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_SUBSCRIBE:
|
||||
MODLOG_DFLT(INFO, "subscribe event; conn_handle=%d attr_handle=%d "
|
||||
"reason=%d prevn=%d curn=%d previ=%d curi=%d\n",
|
||||
event->subscribe.conn_handle,
|
||||
event->subscribe.attr_handle,
|
||||
event->subscribe.reason,
|
||||
event->subscribe.prev_notify,
|
||||
event->subscribe.cur_notify,
|
||||
event->subscribe.prev_indicate,
|
||||
event->subscribe.cur_indicate);
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_MTU:
|
||||
MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d cid=%d mtu=%d\n",
|
||||
event->mtu.conn_handle,
|
||||
event->mtu.channel_id,
|
||||
event->mtu.value);
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_REPEAT_PAIRING:
|
||||
/* We already have a bond with the peer, but it is attempting to
|
||||
* establish a new secure link. This app sacrifices security for
|
||||
* convenience: just throw away the old bond and accept the new link.
|
||||
*/
|
||||
|
||||
/* Delete the old bond. */
|
||||
rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
ble_store_util_delete_peer(&desc.peer_id_addr);
|
||||
|
||||
/* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should
|
||||
* continue with the pairing operation.
|
||||
*/
|
||||
return BLE_GAP_REPEAT_PAIRING_RETRY;
|
||||
|
||||
case BLE_GAP_EVENT_PASSKEY_ACTION:
|
||||
ESP_LOGI(tag, "PASSKEY_ACTION_EVENT started");
|
||||
struct ble_sm_io pkey = {0};
|
||||
int key = 0;
|
||||
|
||||
if (event->passkey.params.action == BLE_SM_IOACT_DISP) {
|
||||
pkey.action = event->passkey.params.action;
|
||||
pkey.passkey = 123456; // This is the passkey to be entered on peer
|
||||
ESP_LOGI(tag, "Enter passkey %" PRIu32 "on the peer side", pkey.passkey);
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
ESP_LOGI(tag, "ble_sm_inject_io result: %d", rc);
|
||||
} else if (event->passkey.params.action == BLE_SM_IOACT_NUMCMP) {
|
||||
ESP_LOGI(tag, "Passkey on device's display: %" PRIu32 , event->passkey.params.numcmp);
|
||||
ESP_LOGI(tag, "Accept or reject the passkey through console in this format -> key Y or key N");
|
||||
pkey.action = event->passkey.params.action;
|
||||
if (scli_receive_key(&key)) {
|
||||
pkey.numcmp_accept = key;
|
||||
} else {
|
||||
pkey.numcmp_accept = 0;
|
||||
ESP_LOGE(tag, "Timeout! Rejecting the key");
|
||||
}
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
ESP_LOGI(tag, "ble_sm_inject_io result: %d", rc);
|
||||
} else if (event->passkey.params.action == BLE_SM_IOACT_OOB) {
|
||||
static uint8_t tem_oob[16] = {0};
|
||||
pkey.action = event->passkey.params.action;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
pkey.oob[i] = tem_oob[i];
|
||||
}
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
ESP_LOGI(tag, "ble_sm_inject_io result: %d", rc);
|
||||
} else if (event->passkey.params.action == BLE_SM_IOACT_INPUT) {
|
||||
ESP_LOGI(tag, "Enter the passkey through console in this format-> key 123456");
|
||||
pkey.action = event->passkey.params.action;
|
||||
if (scli_receive_key(&key)) {
|
||||
pkey.passkey = key;
|
||||
} else {
|
||||
pkey.passkey = 0;
|
||||
ESP_LOGE(tag, "Timeout! Passing 0 as the key");
|
||||
}
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
ESP_LOGI(tag, "ble_sm_inject_io result: %d", rc);
|
||||
}
|
||||
return 0;
|
||||
|
||||
#if MYNEWT_VAL(BLE_POWER_CONTROL)
|
||||
case BLE_GAP_EVENT_TRANSMIT_POWER:
|
||||
MODLOG_DFLT(INFO, "Transmit power event : status=%d conn_handle=%d reason=%d "
|
||||
"phy=%d power_level=%x power_level_flag=%d delta=%d",
|
||||
event->transmit_power.status,
|
||||
event->transmit_power.conn_handle,
|
||||
event->transmit_power.reason,
|
||||
event->transmit_power.phy,
|
||||
event->transmit_power.transmit_power_level,
|
||||
event->transmit_power.transmit_power_level_flag,
|
||||
event->transmit_power.delta);
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_PATHLOSS_THRESHOLD:
|
||||
MODLOG_DFLT(INFO, "Pathloss threshold event : conn_handle=%d current path loss=%d "
|
||||
"zone_entered =%d",
|
||||
event->pathloss_threshold.conn_handle,
|
||||
event->pathloss_threshold.current_path_loss,
|
||||
event->pathloss_threshold.zone_entered);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
bleprph_on_reset(int reason)
|
||||
{
|
||||
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
|
||||
}
|
||||
|
||||
#if CONFIG_EXAMPLE_RANDOM_ADDR
|
||||
static void
|
||||
ble_app_set_addr(void)
|
||||
{
|
||||
ble_addr_t addr;
|
||||
int rc;
|
||||
|
||||
/* generate new non-resolvable private address */
|
||||
rc = ble_hs_id_gen_rnd(0, &addr);
|
||||
assert(rc == 0);
|
||||
|
||||
/* set generated address */
|
||||
rc = ble_hs_id_set_rnd(addr.val);
|
||||
|
||||
assert(rc == 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
bleprph_on_sync(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
#if CONFIG_EXAMPLE_RANDOM_ADDR
|
||||
/* Generate a non-resolvable private address. */
|
||||
ble_app_set_addr();
|
||||
#endif
|
||||
|
||||
/* Make sure we have proper identity address set (public preferred) */
|
||||
#if CONFIG_EXAMPLE_RANDOM_ADDR
|
||||
rc = ble_hs_util_ensure_addr(1);
|
||||
#else
|
||||
rc = ble_hs_util_ensure_addr(0);
|
||||
#endif
|
||||
assert(rc == 0);
|
||||
|
||||
/* Figure out address to use while advertising (no privacy for now) */
|
||||
rc = ble_hs_id_infer_auto(0, &own_addr_type);
|
||||
if (rc != 0) {
|
||||
MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Printing ADDR */
|
||||
uint8_t addr_val[6] = {0};
|
||||
rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
|
||||
|
||||
MODLOG_DFLT(INFO, "Device Address: ");
|
||||
print_addr(addr_val);
|
||||
MODLOG_DFLT(INFO, "\n");
|
||||
/* Begin advertising. */
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_bleprph_advertise();
|
||||
#else
|
||||
bleprph_advertise();
|
||||
#endif
|
||||
}
|
||||
|
||||
void bleprph_host_task(void *param)
|
||||
{
|
||||
ESP_LOGI(tag, "BLE Host Task Started");
|
||||
/* This function will return only when nimble_port_stop() is executed */
|
||||
nimble_port_run();
|
||||
|
||||
nimble_port_freertos_deinit();
|
||||
}
|
||||
|
||||
void
|
||||
app_main(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
/* Initialize NVS — it is used to store PHY calibration data */
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
hci_uart_open();
|
||||
ret = nimble_port_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
|
||||
return;
|
||||
}
|
||||
/* Initialize the NimBLE host configuration. */
|
||||
ble_hs_cfg.reset_cb = bleprph_on_reset;
|
||||
ble_hs_cfg.sync_cb = bleprph_on_sync;
|
||||
ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb;
|
||||
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
|
||||
|
||||
ble_hs_cfg.sm_io_cap = CONFIG_EXAMPLE_IO_TYPE;
|
||||
#ifdef CONFIG_EXAMPLE_BONDING
|
||||
ble_hs_cfg.sm_bonding = 1;
|
||||
/* Enable the appropriate bit masks to make sure the keys
|
||||
* that are needed are exchanged
|
||||
*/
|
||||
ble_hs_cfg.sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC;
|
||||
ble_hs_cfg.sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ENC;
|
||||
#endif
|
||||
#ifdef CONFIG_EXAMPLE_MITM
|
||||
ble_hs_cfg.sm_mitm = 1;
|
||||
#endif
|
||||
#ifdef CONFIG_EXAMPLE_USE_SC
|
||||
ble_hs_cfg.sm_sc = 1;
|
||||
#else
|
||||
ble_hs_cfg.sm_sc = 0;
|
||||
#endif
|
||||
#ifdef CONFIG_EXAMPLE_RESOLVE_PEER_ADDR
|
||||
/* Stores the IRK */
|
||||
ble_hs_cfg.sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ID;
|
||||
ble_hs_cfg.sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ID;
|
||||
#endif
|
||||
|
||||
rc = gatt_svr_init();
|
||||
assert(rc == 0);
|
||||
|
||||
/* Set the default device name. */
|
||||
rc = ble_svc_gap_device_name_set("nimble-bleprph");
|
||||
assert(rc == 0);
|
||||
|
||||
/* XXX Need to have template for store */
|
||||
ble_store_config_init();
|
||||
|
||||
nimble_port_freertos_init(bleprph_host_task);
|
||||
|
||||
/* Initialize command line interface to accept input from user */
|
||||
rc = scli_init();
|
||||
if (rc != ESP_OK) {
|
||||
ESP_LOGE(tag, "scli_init() failed");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,231 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "driver/uart.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_attr.h"
|
||||
#include "uart_driver.h"
|
||||
#include "nimble/hci_common.h"
|
||||
#include "host/ble_hs_mbuf.h"
|
||||
|
||||
#define TAG "UART_HCI"
|
||||
|
||||
#define UART_NO (1)
|
||||
#define UART_BUF_SZ (1024)
|
||||
|
||||
#define UART_TX_PIN (CONFIG_EXAMPLE_HCI_UART_TX_PIN)
|
||||
#define UART_RX_PIN (CONFIG_EXAMPLE_HCI_UART_RX_PIN)
|
||||
#define UART_RTS_PIN (CONFIG_EXAMPLE_HCI_UART_RTS_PIN)
|
||||
#define UART_CTS_PIN (CONFIG_EXAMPLE_HCI_UART_CTS_PIN)
|
||||
#define HCI_H4_ACL (0x02)
|
||||
#define HCI_H4_CMD (0x01)
|
||||
#define HCI_H4_EVT (0x04)
|
||||
#define BLE_HCI_EVENT_HDR_LEN (2)
|
||||
#define BLE_HCI_CMD_HDR_LEN (3)
|
||||
|
||||
enum {
|
||||
UART_RX_TYPE = 0,
|
||||
UART_RX_LEN,
|
||||
UART_RX_DATA,
|
||||
};
|
||||
|
||||
enum {
|
||||
DATA_TYPE_COMMAND = 1,
|
||||
DATA_TYPE_ACL = 2,
|
||||
DATA_TYPE_EVENT = 4
|
||||
};
|
||||
|
||||
TaskHandle_t s_rx_task_hdl;
|
||||
|
||||
static void IRAM_ATTR hci_uart_rx_task(void *arg)
|
||||
{
|
||||
uint8_t buf[1026];
|
||||
int len_now_read = -1;
|
||||
uint32_t len_to_read = 1;
|
||||
uint32_t len_total_read = 0;
|
||||
uint8_t rx_st = UART_RX_TYPE;
|
||||
|
||||
while (1) {
|
||||
len_now_read = uart_read_bytes(UART_NO, &buf[len_total_read], len_to_read, portMAX_DELAY);
|
||||
assert(len_now_read == len_to_read);
|
||||
len_total_read += len_now_read;
|
||||
|
||||
switch (rx_st) {
|
||||
case UART_RX_TYPE: {
|
||||
assert(buf[0] >= DATA_TYPE_ACL && buf[0] <= DATA_TYPE_EVENT);
|
||||
if (buf[0] == DATA_TYPE_ACL) {
|
||||
len_to_read = 4;
|
||||
} else if (buf[0] == DATA_TYPE_EVENT) {
|
||||
len_to_read = 2;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
rx_st = UART_RX_LEN;
|
||||
}
|
||||
break;
|
||||
|
||||
case UART_RX_LEN: {
|
||||
if (buf[0] == DATA_TYPE_ACL) {
|
||||
len_to_read = buf[3] | (buf[4] << 8);
|
||||
} else if (buf[0] == DATA_TYPE_EVENT) {
|
||||
len_to_read = buf[2];
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
rx_st = UART_RX_DATA;
|
||||
}
|
||||
break;
|
||||
|
||||
case UART_RX_DATA: {
|
||||
uint8_t *data = buf;
|
||||
int rc;
|
||||
|
||||
if (data[0] == HCI_H4_EVT) {
|
||||
uint8_t *evbuf;
|
||||
int totlen;
|
||||
|
||||
totlen = BLE_HCI_EVENT_HDR_LEN + data[2];
|
||||
assert(totlen <= UINT8_MAX + BLE_HCI_EVENT_HDR_LEN);
|
||||
|
||||
if (totlen > MYNEWT_VAL(BLE_TRANSPORT_EVT_SIZE)) {
|
||||
ESP_LOGE(TAG, "Received HCI data length at host (%d)"
|
||||
"exceeds maximum configured HCI event buffer size (%d).",
|
||||
totlen, MYNEWT_VAL(BLE_TRANSPORT_EVT_SIZE));
|
||||
break;
|
||||
}
|
||||
|
||||
if (data[1] == BLE_HCI_EVCODE_HW_ERROR) {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
/* Allocate LE Advertising Report Event from lo pool only */
|
||||
if ((data[1] == BLE_HCI_EVCODE_LE_META) &&
|
||||
(data[3] == BLE_HCI_LE_SUBEV_ADV_RPT || data[3] == BLE_HCI_LE_SUBEV_EXT_ADV_RPT)) {
|
||||
evbuf = ble_transport_alloc_evt(1);
|
||||
/* Skip advertising report if we're out of memory */
|
||||
if (!evbuf) {
|
||||
ESP_LOGE(TAG, "No buffers");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
evbuf = ble_transport_alloc_evt(0);
|
||||
assert(evbuf != NULL);
|
||||
}
|
||||
|
||||
memset(evbuf, 0, sizeof * evbuf);
|
||||
memcpy(evbuf, &data[1], totlen);
|
||||
|
||||
rc = ble_transport_to_hs_evt(evbuf);
|
||||
assert(rc == 0);
|
||||
} else if (data[0] == HCI_H4_ACL) {
|
||||
struct os_mbuf *m = NULL;
|
||||
m = ble_transport_alloc_acl_from_ll();
|
||||
if (!m) {
|
||||
ESP_LOGE(TAG, "No buffers");
|
||||
}
|
||||
|
||||
if ((rc = os_mbuf_append(m, &data[1], len_total_read - 1)) != 0) {
|
||||
ESP_LOGE(TAG, "%s failed to os_mbuf_append; rc = %d", __func__, rc);
|
||||
os_mbuf_free_chain(m);
|
||||
return;
|
||||
}
|
||||
|
||||
ble_transport_to_hs_acl(m);
|
||||
}
|
||||
|
||||
rx_st = UART_RX_TYPE;
|
||||
len_to_read = 1;
|
||||
len_total_read = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
default: {
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void hci_uart_send(uint8_t *buf, uint16_t len)
|
||||
{
|
||||
uint8_t *p = buf;
|
||||
int len_write = 0;
|
||||
|
||||
while (len) {
|
||||
len_write = uart_write_bytes(UART_NO, p, len);
|
||||
assert(len_write > 0);
|
||||
len -= len_write;
|
||||
p += len_write;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
ble_transport_to_ll_acl_impl(struct os_mbuf *om)
|
||||
{
|
||||
uint8_t buf[OS_MBUF_PKTLEN(om) + 1];
|
||||
int rc;
|
||||
buf[0] = HCI_H4_ACL;
|
||||
rc = ble_hs_mbuf_to_flat(om, buf + 1, OS_MBUF_PKTLEN(om), NULL);
|
||||
if(rc) {
|
||||
ESP_LOGE(TAG, "Error copying data %d", rc);
|
||||
os_mbuf_free_chain(om);
|
||||
return rc;
|
||||
}
|
||||
hci_uart_send(buf, OS_MBUF_PKTLEN(om) + 1);
|
||||
os_mbuf_free_chain(om);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
ble_transport_to_ll_cmd_impl(void *buf)
|
||||
{
|
||||
int len = 3 + ((uint8_t *)buf)[2] + 1;
|
||||
uint8_t data[258];
|
||||
data[0] = HCI_H4_CMD;
|
||||
memcpy(data + 1, buf, len - 1);
|
||||
hci_uart_send(data, len);
|
||||
ble_transport_free(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hci_uart_open(void)
|
||||
{
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = CONFIG_EXAMPLE_HCI_UART_BAUDRATE,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = CONFIG_EXAMPLE_HCI_UART_FLOW_CTRL,
|
||||
.source_clk = UART_SCLK_DEFAULT,
|
||||
};
|
||||
|
||||
int intr_alloc_flags = 0;
|
||||
#if CONFIG_UART_ISR_IN_IRAM
|
||||
intr_alloc_flags = ESP_INTR_FLAG_IRAM;
|
||||
#endif
|
||||
|
||||
ESP_ERROR_CHECK(uart_driver_install(UART_NO, UART_BUF_SZ * 2, UART_BUF_SZ * 2, 0, NULL, intr_alloc_flags));
|
||||
ESP_ERROR_CHECK(uart_param_config(UART_NO, &uart_config));
|
||||
ESP_ERROR_CHECK(uart_set_pin(UART_NO, UART_TX_PIN, UART_RX_PIN, -1, -1));
|
||||
|
||||
xTaskCreate(hci_uart_rx_task, "hci_uart_rx_task", 2048, NULL, 12, &s_rx_task_hdl);
|
||||
}
|
||||
|
||||
void hci_uart_close(void)
|
||||
{
|
||||
if (s_rx_task_hdl) {
|
||||
vTaskDelete(s_rx_task_hdl);
|
||||
}
|
||||
uart_driver_delete(UART_NO);
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
|
||||
#ifndef __UART_DRIVER_H__
|
||||
#define __UART_DRIVER_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @brief open HCI transport of uart
|
||||
*/
|
||||
void hci_uart_open(void);
|
||||
|
||||
/**
|
||||
* @brief close HCI transport of uart
|
||||
*/
|
||||
void hci_uart_close(void);
|
||||
|
||||
/**
|
||||
* @brief send data from host to HCI transport
|
||||
*
|
||||
* @param[in] data pointer to data buffer
|
||||
* @param[in] len length of data
|
||||
*/
|
||||
void hci_uart_send(uint8_t *data, uint16_t len);
|
||||
|
||||
#endif /* __UART_DRIVER_H__ */
|
|
@ -0,0 +1,11 @@
|
|||
# Override some defaults so BT stack is enabled
|
||||
# in this example
|
||||
|
||||
#
|
||||
# BT config
|
||||
#
|
||||
CONFIG_BT_ENABLED=y
|
||||
CONFIG_BT_CONTROLLER_DISABLED=y
|
||||
CONFIG_BT_BLUEDROID_ENABLED=n
|
||||
CONFIG_BT_NIMBLE_ENABLED=y
|
||||
CONFIG_BT_NIMBLE_TRANSPORT_UART=n
|
|
@ -0,0 +1,56 @@
|
|||
# BLE Peripheral Example Walkthrough
|
||||
|
||||
## Introduction
|
||||
|
||||
The tutorial is focused on how to implement the custom transport in the application and register it with nimble-host. This example has the exact same working as bleprph except the transport used is custom UART transport written in `"main/uart_driver.c"` file. The controller is disabled for this example, the nimble-host tries to communicate with the external connected controller using UART transport.
|
||||
|
||||
Note: This walkthrough only covers the transport used by the application, for detailed explanation see the [bleprph_walkthrough](../../bleprph/tutorial/bleprph_walkthrough.md)
|
||||
## Hardware Setup
|
||||
|
||||
This example uses esp32 chip to run the example `bleprph_host_only` that runs the nimble-host and sends and receives the commands and events using uart. The another chip used is esp32c6 that runs the esp-hosted `esp-hosted/esp_hosted_fg/esp/esp_driver/network_adapter` example that runs the controller and receives commands and sends events using UART. Below is the setup of esp32 and esp32c6.
|
||||
|
||||
![Hardware Setup](hardware_setup.png)
|
||||
|
||||
Note that the esp32s gpio pins tx=4 and rx=5 are defined in [uart_driver.c](../main/uart_driver.c) file. The tx pin of one chip connectes to the rx pin of the other and vice versa.
|
||||
|
||||
```c
|
||||
#define UART_TX_PIN (4)
|
||||
#define UART_RX_PIN (5)
|
||||
#define UART_RTS_PIN (19)
|
||||
#define UART_CTS_PIN (23)
|
||||
```
|
||||
|
||||
## Main Entry Point
|
||||
|
||||
The program’s entry point is the app_main() function:
|
||||
|
||||
```c
|
||||
void
|
||||
app_main(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
/* Initialize NVS — it is used to store PHY calibration data */
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
hci_uart_open();
|
||||
ret = nimble_port_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(tag, "Failed to init nimble %d ", ret);
|
||||
return;
|
||||
}
|
||||
....
|
||||
....
|
||||
}
|
||||
```
|
||||
In the main function `hci_uart_open()` configures the uart with necessary parameters like baud_rate, parity, etc. The file [uart_driver.c](../main/uart_driver.c) has all the uart transport code along with the necessary apis mandated by nimble-host. `ble_transport_to_ll_acl_impl` is the api used to send the data to the controller. and `ble_transport_to_ll_cmd_impl` is used to send the command to the controller. These two apis are implemented as they are necessary. Along with these two apis, any data that is received in the rx direction is redirected to the host using `ble_transport_to_hs_evt` and `ble_transport_to_hs_acl` apis.
|
||||
|
||||
## Conclusion
|
||||
1. This example covered how to write the custom transport layer and use it with nimble-host.
|
||||
2. The example used the esp-hosted network-adapter example flashed on esp32c6 connected through UART to test this demo.
|
||||
3. In the similar fashion the external controller could be connected.
|
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 587 KiB |
|
@ -169,6 +169,43 @@ print_adv_fields(const struct ble_hs_adv_fields *fields)
|
|||
MODLOG_DFLT(DEBUG, "\n");
|
||||
}
|
||||
|
||||
if (fields->sm_tk_value_is_present) {
|
||||
MODLOG_DFLT(DEBUG, " sm_tk_value=");
|
||||
print_bytes(fields->sm_tk_value, 16);
|
||||
MODLOG_DFLT(DEBUG, "\n");
|
||||
}
|
||||
|
||||
if (fields->sm_oob_flag_is_present) {
|
||||
MODLOG_DFLT(DEBUG, " sm_oob_flag=%d\n", fields->sm_oob_flag);
|
||||
}
|
||||
|
||||
if (fields->sol_uuids16 != NULL) {
|
||||
MODLOG_DFLT(DEBUG, " sol_uuids16=");
|
||||
for (i = 0; i < fields->sol_num_uuids16; i++) {
|
||||
print_uuid(&fields->sol_uuids16[i].u);
|
||||
MODLOG_DFLT(DEBUG, " ");
|
||||
}
|
||||
MODLOG_DFLT(DEBUG, "\n");
|
||||
}
|
||||
|
||||
if (fields->sol_uuids32 != NULL) {
|
||||
MODLOG_DFLT(DEBUG, " sol_uuids32=");
|
||||
for (i = 0; i < fields->sol_num_uuids32; i++) {
|
||||
print_uuid(&fields->sol_uuids32[i].u);
|
||||
MODLOG_DFLT(DEBUG, "\n");
|
||||
}
|
||||
MODLOG_DFLT(DEBUG, "\n");
|
||||
}
|
||||
|
||||
if (fields->sol_uuids128 != NULL) {
|
||||
MODLOG_DFLT(DEBUG, " sol_uuids128=");
|
||||
for (i = 0; i < fields->sol_num_uuids128; i++) {
|
||||
print_uuid(&fields->sol_uuids128[i].u);
|
||||
MODLOG_DFLT(DEBUG, " ");
|
||||
}
|
||||
MODLOG_DFLT(DEBUG, "\n");
|
||||
}
|
||||
|
||||
if (fields->svc_data_uuid16 != NULL) {
|
||||
MODLOG_DFLT(DEBUG, " svc_data_uuid16=");
|
||||
print_bytes(fields->svc_data_uuid16, fields->svc_data_uuid16_len);
|
||||
|
@ -185,6 +222,16 @@ print_adv_fields(const struct ble_hs_adv_fields *fields)
|
|||
MODLOG_DFLT(DEBUG, "\n");
|
||||
}
|
||||
|
||||
if (fields->random_tgt_addr != NULL) {
|
||||
MODLOG_DFLT(DEBUG, " random_tgt_addr=");
|
||||
u8p = fields->random_tgt_addr;
|
||||
for (i = 0; i < fields->num_random_tgt_addrs; i++) {
|
||||
MODLOG_DFLT(DEBUG, "random_tgt_addr=%s ", addr_str(u8p));
|
||||
u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN;
|
||||
}
|
||||
MODLOG_DFLT(DEBUG, "\n");
|
||||
}
|
||||
|
||||
if (fields->appearance_is_present) {
|
||||
MODLOG_DFLT(DEBUG, " appearance=0x%04x\n", fields->appearance);
|
||||
}
|
||||
|
@ -193,6 +240,19 @@ print_adv_fields(const struct ble_hs_adv_fields *fields)
|
|||
MODLOG_DFLT(DEBUG, " adv_itvl=0x%04x\n", fields->adv_itvl);
|
||||
}
|
||||
|
||||
if (fields->device_addr_is_present) {
|
||||
MODLOG_DFLT(DEBUG, " device_addr=");
|
||||
u8p = fields->device_addr;
|
||||
MODLOG_DFLT(DEBUG, "%s ", addr_str(u8p));
|
||||
|
||||
u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN;
|
||||
MODLOG_DFLT(DEBUG, "addr_type %d ", *u8p);
|
||||
}
|
||||
|
||||
if (fields->le_role_is_present) {
|
||||
MODLOG_DFLT(DEBUG, " le_role=%d\n", fields->le_role);
|
||||
}
|
||||
|
||||
if (fields->svc_data_uuid32 != NULL) {
|
||||
MODLOG_DFLT(DEBUG, " svc_data_uuid32=");
|
||||
print_bytes(fields->svc_data_uuid32, fields->svc_data_uuid32_len);
|
||||
|
|
|
@ -272,7 +272,7 @@ void app_main(void)
|
|||
#endif // CONFIG_BT_ENABLED
|
||||
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
|
||||
|
||||
#if CONFIG_BT_BLE_ENABLED || CONFIG_BT_NIMBLE_ENABLED
|
||||
#if CONFIG_BT_CONTROLLER_ENABLED && (CONFIG_BT_BLE_ENABLED || CONFIG_BT_NIMBLE_ENABLED)
|
||||
ESP_ERROR_CHECK(esp_ble_helper_init());
|
||||
#endif
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#if CONFIG_BT_BLE_ENABLED || CONFIG_BT_NIMBLE_ENABLED
|
||||
#if CONFIG_BT_CONTROLLER_ENABLED && (CONFIG_BT_BLE_ENABLED || CONFIG_BT_NIMBLE_ENABLED)
|
||||
|
||||
#include "ble_api.h"
|
||||
#include "esp_log.h"
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if CONFIG_BT_BLE_ENABLED || CONFIG_BT_NIMBLE_ENABLED
|
||||
#if CONFIG_BT_CONTROLLER_ENABLED && (CONFIG_BT_BLE_ENABLED || CONFIG_BT_NIMBLE_ENABLED)
|
||||
esp_err_t esp_ble_helper_init(void);
|
||||
#endif
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue