From 7f48cb04859b61835a3e92bdd7e445cc6e46552d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sat, 14 Mar 2026 20:36:26 +0100 Subject: [PATCH] Save file with proper formatting --- src/helpers/esp32/BLELogInterface.h | 54 ++++++++++++++--------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/helpers/esp32/BLELogInterface.h b/src/helpers/esp32/BLELogInterface.h index 9ccc8155..8209ba91 100644 --- a/src/helpers/esp32/BLELogInterface.h +++ b/src/helpers/esp32/BLELogInterface.h @@ -1,16 +1,16 @@ #pragma once #include +#include #include #include #include -#include // Nordic UART Service UUIDs (standard, recognised by nRF Connect and bleak) -#define NUS_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" -#define NUS_TX_UUID "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" +#define NUS_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" +#define NUS_TX_UUID "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" -#define BLE_LOG_ADVERT_RESTART_DELAY_MS 1000 +#define BLE_LOG_ADVERT_RESTART_DELAY_MS 1000 /** * Unsecured BLE UART (Nordic UART Service) logger for ESP32 platforms. @@ -22,12 +22,12 @@ * Call loop() from the Arduino loop() to handle advertising restart on disconnect. */ class BLELogInterface : public Print, BLEServerCallbacks { - BLEServer* _server; - BLECharacteristic* _tx_char; - bool _connected; - unsigned long _adv_restart_time; - char _line_buf[256]; - int _line_len; + BLEServer *_server; + BLECharacteristic *_tx_char; + bool _connected; + unsigned long _adv_restart_time; + char _line_buf[256]; + int _line_len; void flushLine() { if (_line_len == 0 || !_connected) { @@ -42,58 +42,55 @@ class BLELogInterface : public Print, BLEServerCallbacks { while (offset < _line_len) { int n = _line_len - offset; if (n > CHUNK) n = CHUNK; - _tx_char->setValue((uint8_t*)_line_buf + offset, n); + _tx_char->setValue((uint8_t *)_line_buf + offset, n); _tx_char->notify(); offset += n; } _line_len = 0; } - void onConnect(BLEServer* pServer) override { - _connected = true; - } + void onConnect(BLEServer *pServer) override { _connected = true; } - void onDisconnect(BLEServer* pServer) override { + void onDisconnect(BLEServer *pServer) override { _connected = false; - _line_len = 0; // discard partial line + _line_len = 0; // discard partial line _adv_restart_time = millis() + BLE_LOG_ADVERT_RESTART_DELAY_MS; } public: BLELogInterface() - : _server(nullptr), _tx_char(nullptr), - _connected(false), _adv_restart_time(0), _line_len(0) {} + : _server(nullptr), _tx_char(nullptr), _connected(false), _adv_restart_time(0), _line_len(0) {} - void begin(const char* device_name) { + void begin(const char *device_name) { BLEDevice::init(device_name); - BLEDevice::setMTU(256); // default 23-byte MTU caps notifications at 20 bytes; request more + BLEDevice::setMTU(256); // default 23-byte MTU caps notifications at 20 bytes; request more // Explicitly disable bonding so the ESP32 does not send security requests. // Without this the BLE stack initiates Just Works pairing by default, which // fails with AUTH FAILED when no security callbacks are registered. - BLESecurity* pSecurity = new BLESecurity(); + BLESecurity *pSecurity = new BLESecurity(); pSecurity->setAuthenticationMode(ESP_LE_AUTH_NO_BOND); pSecurity->setCapability(ESP_IO_CAP_NONE); _server = BLEDevice::createServer(); _server->setCallbacks(this); - BLEService* service = _server->createService(NUS_SERVICE_UUID); + BLEService *service = _server->createService(NUS_SERVICE_UUID); _tx_char = service->createCharacteristic(NUS_TX_UUID, BLECharacteristic::PROPERTY_NOTIFY); _tx_char->addDescriptor(new BLE2902()); // Characteristic Presentation Format (0x2904): declare value as UTF-8 string // Format: format(1) exponent(1) unit(2) namespace(1) description(2) static const uint8_t utf8_format[] = { 0x19, 0x00, 0x00, 0x27, 0x01, 0x00, 0x00 }; - BLEDescriptor* pFormat = new BLEDescriptor((uint16_t)0x2904); - pFormat->setValue(const_cast(utf8_format), sizeof(utf8_format)); + BLEDescriptor *pFormat = new BLEDescriptor((uint16_t)0x2904); + pFormat->setValue(const_cast(utf8_format), sizeof(utf8_format)); _tx_char->addDescriptor(pFormat); service->start(); - BLEAdvertising* adv = BLEDevice::getAdvertising(); + BLEAdvertising *adv = BLEDevice::getAdvertising(); adv->addServiceUUID(NUS_SERVICE_UUID); adv->setScanResponse(true); - adv->setMinPreferred(0x06); // helps iOS find and stay connected to the device + adv->setMinPreferred(0x06); // helps iOS find and stay connected to the device adv->setMinPreferred(0x12); BLEDevice::startAdvertising(); } @@ -116,8 +113,9 @@ public: return 1; } - size_t write(const uint8_t* buf, size_t size) override { - for (size_t i = 0; i < size; i++) write(buf[i]); + size_t write(const uint8_t *buf, size_t size) override { + for (size_t i = 0; i < size; i++) + write(buf[i]); return size; } };