[ble_nus] Atomic log-line framing (no partial ring-buffer writes) (#17105)

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: tomaszduda23 <tomaszduda23@gmail.com>
This commit is contained in:
Anunay Kulshrestha
2026-06-22 21:04:11 +00:00
committed by GitHub
co-authored by Claude Opus 4.8 tomaszduda23
parent 6c1724874b
commit 3a4831bd7e
+10 -3
View File
@@ -25,11 +25,14 @@ void BLENUS::write_array(const uint8_t *data, size_t len) {
if (atomic_get(&this->tx_status_) == TX_DISABLED) {
return;
}
auto sent = ring_buf_put(&global_ble_tx_ring_buf, data, len);
if (sent < len) {
ESP_LOGE(TAG, "TX dropping %u bytes", len - sent);
// ring_buf_put() performs a partial write when the buffer is nearly full, which would commit a
// truncated fragment and corrupt the stream. Only write when the whole payload fits, so the byte
// stream never contains a partial message.
if (ring_buf_space_get(&global_ble_tx_ring_buf) < len) {
ESP_LOGE(TAG, "TX dropping %u bytes", len);
return;
}
ring_buf_put(&global_ble_tx_ring_buf, data, len);
#ifdef USE_UART_DEBUGGER
for (size_t i = 0; i < len; i++) {
this->debug_callback_.call(uart::UART_DIRECTION_TX, data[i]);
@@ -197,6 +200,10 @@ void BLENUS::setup() {
void BLENUS::on_log(uint8_t level, const char *tag, const char *message, size_t message_len) {
(void) level;
(void) tag;
// make sure there is space for '\n' or entire message is dropped
if (ring_buf_space_get(&global_ble_tx_ring_buf) < message_len + 1) {
return;
}
this->write_array(reinterpret_cast<const uint8_t *>(message), message_len);
const char c = '\n';
this->write_array(reinterpret_cast<const uint8_t *>(&c), 1);