3#include <zephyr/kernel.h>
4#include <bluetooth/services/nus.h>
10#include <zephyr/sys/ring_buffer.h>
17#ifdef ESPHOME_BLE_NUS_RX_RING_BUFFER_SIZE
22static const char *
const TAG =
"ble_nus";
25 if (atomic_get(&this->
tx_status_) == TX_DISABLED) {
28 auto sent = ring_buf_put(&global_ble_tx_ring_buf, data,
len);
30 ESP_LOGE(TAG,
"TX dropping %u bytes",
len - sent);
33#ifdef USE_UART_DEBUGGER
34 for (
size_t i = 0; i <
len; i++) {
41#ifdef ESPHOME_BLE_NUS_RX_RING_BUFFER_SIZE
60#ifdef ESPHOME_BLE_NUS_RX_RING_BUFFER_SIZE
70#ifdef USE_UART_DEBUGGER
81 if (ring_buf_get(&global_ble_rx_ring_buf, data,
len) !=
len) {
82 ESP_LOGE(TAG,
"UART BLE unexpected size");
85#ifdef USE_UART_DEBUGGER
86 for (
size_t i = 0; i <
len; i++) {
97#ifdef ESPHOME_BLE_NUS_RX_RING_BUFFER_SIZE
98 uint32_t size = ring_buf_size_get(&global_ble_rx_ring_buf);
99 ESP_LOGVV(TAG,
"UART BLE available %u",
size);
107 constexpr uint32_t timeout_500ms = 500;
109 while (atomic_get(&this->
tx_status_) != TX_DISABLED && !ring_buf_is_empty(&global_ble_tx_ring_buf)) {
110 if (
millis() - start > timeout_500ms) {
111 ESP_LOGW(TAG,
"Flush timeout");
137 ESP_LOGVV(TAG,
"Sent operation completed");
142 case BT_NUS_SEND_STATUS_ENABLED:
149 ESP_LOGD(TAG,
"NUS notification has been enabled");
151 case BT_NUS_SEND_STATUS_DISABLED:
153 ESP_LOGD(TAG,
"NUS notification has been disabled");
158 ESP_LOGV(TAG,
"Received %d bytes.",
len);
159#ifdef ESPHOME_BLE_NUS_RX_RING_BUFFER_SIZE
160 auto recv_len = ring_buf_put(&global_ble_rx_ring_buf, data,
len);
161 if (recv_len <
len) {
162 ESP_LOGE(TAG,
"RX dropping %u bytes",
len - recv_len);
167#ifdef ESPHOME_BLE_NUS_RX_RING_BUFFER_SIZE
170 bt_nus_cb callbacks = {
176 bt_nus_init(&callbacks);
178 static bt_conn_cb conn_callbacks = {
183 bt_conn_cb_register(&conn_callbacks);
189 this, [](
void *self, uint8_t level,
const char *
tag,
const char *
message,
size_t message_len) {
202 this->
write_array(
reinterpret_cast<const uint8_t *
>(&c), 1);
208 bt_conn *conn = this->
conn_.load();
210 mtu = bt_nus_get_mtu(conn);
221 if (ring_buf_is_empty(&global_ble_tx_ring_buf)) {
225 if (!atomic_cas(&this->
tx_status_, TX_ENABLED, TX_BUSY)) {
226 if (atomic_get(&this->
tx_status_) == TX_DISABLED) {
227 ring_buf_reset(&global_ble_tx_ring_buf);
232 bt_conn *conn = this->
conn_.load();
234 conn = bt_conn_ref(conn);
237 if (
nullptr == conn) {
238 atomic_cas(&this->
tx_status_, TX_BUSY, TX_ENABLED);
242 uint32_t req_len = bt_nus_get_mtu(conn);
245 uint32_t size = ring_buf_get_claim(&global_ble_tx_ring_buf, &buf, req_len);
249 err = bt_nus_send(conn, buf,
size);
250 err2 = ring_buf_get_finish(&global_ble_tx_ring_buf,
size);
253 ESP_LOGE(TAG,
"Size %u exceeds valid bytes in the ring buffer (%d error)",
size, err2);
256 ESP_LOGVV(TAG,
"Sent %d bytes",
size);
258 ESP_LOGE(TAG,
"Failed to send %d bytes (%d error)",
size, err);
259 atomic_cas(&this->
tx_status_, TX_BUSY, TX_ENABLED);
void schedule_dump_config()
static void disconnected(bt_conn *conn, uint8_t reason)
static void rx_callback(bt_conn *conn, const uint8_t *data, uint16_t len)
size_t available() override
static void connected(bt_conn *conn, uint8_t err)
uart::UARTFlushResult flush() override
bool read_array(uint8_t *data, size_t len) override
std::atomic< bt_conn * > conn_
static void tx_callback(bt_conn *conn)
void write_array(const uint8_t *data, size_t len) override
bool peek_byte(uint8_t *data) override
void on_log(uint8_t level, const char *tag, const char *message, size_t message_len)
void dump_config() override
std::atomic< bool > connected_
static void send_enabled_callback(bt_nus_send_status status)
void add_log_callback(void *instance, void(*fn)(void *, uint8_t, const char *, const char *, size_t))
Register a log callback to receive log messages.
bool read_byte(uint8_t *data)
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
RING_BUF_DECLARE(global_ble_tx_ring_buf, ESPHOME_BLE_NUS_TX_RING_BUFFER_SIZE)
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_SUCCESS
Confirmed: all bytes left the TX FIFO.
@ UART_FLUSH_RESULT_TIMEOUT
Confirmed: timed out before TX completed.
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.