From 3c2889b8f77ed63504c6eda828900ea7db0ad1ed Mon Sep 17 00:00:00 2001 From: gogacoder Date: Sat, 6 Jan 2024 12:11:06 +0700 Subject: [PATCH] BluetoothDispatcher refactoring --- RF/BluetoothDispatcher.cpp | 73 ++++++++++++++++++++++++-------------- RF/BluetoothDispatcher.hpp | 26 ++++++++------ main.cpp | 2 ++ 3 files changed, 64 insertions(+), 37 deletions(-) diff --git a/RF/BluetoothDispatcher.cpp b/RF/BluetoothDispatcher.cpp index 0291a74..9ee3263 100644 --- a/RF/BluetoothDispatcher.cpp +++ b/RF/BluetoothDispatcher.cpp @@ -3,41 +3,53 @@ static DeviceConnectedCb deviceConnectedCallback = nullptr; static void deviceConnectedStaticCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param); -BluetoothDispatcher::BluetoothDispatcher(BluetoothSerial *controller, const char *device_name, HardwareSerial *serial) { +BluetoothDispatcher::BluetoothDispatcher(BluetoothSerial *controller, const char *device_name) { + assert(controller != nullptr); + assert(device_name != nullptr); + _device_name = device_name; _controller = controller; - _serial = serial; } -bool BluetoothDispatcher::initialize() { +bool BluetoothDispatcher::initialize(bool loop_on_fail, int readTimeoutMS) { _controller->enableSSP(); _controller->onConfirmRequest([this](uint16_t pin) { - _confirmRequestCallback(pin); + _onConfirmRequest(pin); }); _controller->onAuthComplete([this](boolean success) { - _authCompleteCallback(success); + _onAuthComplete(success); }); _controller->register_callback(deviceConnectedStaticCallback); - deviceConnectedCallback = [this](esp_bd_addr_t bt_addr) { - _deviceConnectedCallback(bt_addr); + deviceConnectedCallback = [this](BTAddress device) { + _onDeviceConnected(device); }; _controller->setTimeout(2); - return _controller->begin(_device_name); + bool success = _controller->begin(_device_name, false); + if (!success) { + ESP_LOGI(_tag, "Failed to initialize Bluetooth controller!"); + while (loop_on_fail) + ; + return false; + } else { + ESP_LOGI(_tag, "Bluetooth host initialized"); + return true; + } } -void BluetoothDispatcher::tick() { - while (_controller->available() and _buffer.length() <= _buffer_size) { - _buffer += (char)_controller->read(); - } +void BluetoothDispatcher::tick(NewPackageCallback newPackageReceivedCb) { + /* Call the callback, if new package received */ + while (_controller->available() and _buffer.length() <= _buffer_size) { _buffer += (char)_controller->read(); } if (_buffer.endsWith("\r")) { char buffer[_buffer_size]; _buffer.substring(0, _buffer.lastIndexOf('\r'), buffer); - _serial->println(buffer); // print the payload + ESP_LOGD(_tag, "Received new buffer %s", buffer); + if (newPackageReceivedCb) { + newPackageReceivedCb(buffer); + } _buffer.clear(); _controller->write((uint8_t *)"Hello, world!", strlen("Hello, world!")); - // TODO: add callback, that receive new state } - if (_buffer.length() > _buffer_size) { + if (_buffer.length() > _available_buffer_size) { _buffer.clear(); } } @@ -47,30 +59,37 @@ BluetoothDispatcher::~BluetoothDispatcher() { delete _controller; } -void BluetoothDispatcher::_confirmRequestCallback(uint16_t pin) { - _confirmRequestDone = true; - _serial->print("The PIN is: "); - _serial->println(pin); +void BluetoothDispatcher::_onConfirmRequest(uint16_t pin) { + ESP_LOGI(_tag, "The Bluetooth PIN is: %06lu", (long unsigned int)pin); _controller->confirmReply(true); } -void BluetoothDispatcher::_authCompleteCallback(boolean success) { +void BluetoothDispatcher::_onAuthComplete(boolean success) { if (success) { - _confirmRequestDone = true; - _serial->println("Pairing success!"); + ESP_LOGI(_tag, "Pairing success!"); } else { - _serial->println("Pairing failed, rejected by user!"); + ESP_LOGI(_tag, "Pairing failed, rejected by user!"); } } -void BluetoothDispatcher::_deviceConnectedCallback(esp_bd_addr_t bt_addr) { - _serial->print("New connection opened: "); - _serial->println(BTAddress(bt_addr).toString(true)); +void BluetoothDispatcher::_onDeviceConnected(BTAddress device) { + ESP_LOGI(_tag, "New device connected: %s", device.toString(true).c_str()); + if (_deviceConnectedCallback) { + _deviceConnectedCallback(device); + } } +void BluetoothDispatcher::onNewDeviceConnected(DeviceConnectedCb deviceConnectedCb) { + /* Callback called if device connected successfully. */ + _deviceConnectedCallback = deviceConnectedCb; +} + +void BluetoothDispatcher::sendPackage(const char *package, size_t size) { + _controller->write((uint8_t *)package, size); +} static void deviceConnectedStaticCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) { if (event == ESP_SPP_SRV_OPEN_EVT and param->srv_open.status == ESP_SPP_SUCCESS and deviceConnectedCallback) { - deviceConnectedCallback(param->srv_open.rem_bda); + deviceConnectedCallback(BTAddress(param->srv_open.rem_bda)); } } \ No newline at end of file diff --git a/RF/BluetoothDispatcher.hpp b/RF/BluetoothDispatcher.hpp index 3043db3..a99d740 100644 --- a/RF/BluetoothDispatcher.hpp +++ b/RF/BluetoothDispatcher.hpp @@ -1,5 +1,6 @@ #include "BluetoothSerial.h" #include "HardwareSerial.h" +#include "esp_log.h" #include "mString.h" /* Check the ESP configuration */ @@ -18,24 +19,29 @@ #endif /* ************************* */ -typedef std::function DeviceConnectedCb; +typedef std::function DeviceConnectedCb; +typedef std::function NewPackageCallback; class BluetoothDispatcher { public: - BluetoothDispatcher(BluetoothSerial *controller, const char *device_name = "Helicopter", HardwareSerial *serial = &Serial); - bool initialize(); - void tick(); + BluetoothDispatcher(BluetoothSerial *controller, const char *device_name = "Helicopter"); + bool initialize(bool loop_on_fail = true, int readTimeoutMS = 2); + void tick(NewPackageCallback newPackageReceivedCb = nullptr); + void onNewDeviceConnected(DeviceConnectedCb deviceConnectedCb); + void sendPackage(const char *package, size_t size); ~BluetoothDispatcher(); private: - void _confirmRequestCallback(uint16_t pin); - void _authCompleteCallback(boolean success); - void _deviceConnectedCallback(esp_bd_addr_t bt_addr); + void _onConfirmRequest(uint16_t pin); + void _onAuthComplete(boolean success); + void _onDeviceConnected(BTAddress device); const char *_device_name = nullptr; - bool _confirmRequestDone = false; BluetoothSerial *_controller = nullptr; - HardwareSerial *_serial = nullptr; - static constexpr uint16_t _buffer_size = 256; + DeviceConnectedCb _deviceConnectedCallback = nullptr; + constexpr static const char *_tag = "BluetoothDispatcher"; + + static constexpr uint16_t _buffer_size = 512; + static constexpr uint16_t _available_buffer_size = 500; mString<_buffer_size> _buffer; }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index afbd4f3..f12e0cb 100644 --- a/main.cpp +++ b/main.cpp @@ -10,6 +10,7 @@ #include "board_pins.h" #include + TwoWire i2c = TwoWire(0); Barometer barometer(new GyverBME280(i2c)); MPU mpu(new MPU6050(MPU6050_DEFAULT_ADDRESS, &i2c)); @@ -19,6 +20,7 @@ BluetoothDispatcher bluetoothDispatcher(new BluetoothSerial()); void setup() { Serial.begin(115200); + Serial.setDebugOutput(true); Serial.print("Ininitialize I2C..."); Serial.println(i2c.begin(I2C_SDA_PIN, I2C_SCL_PIN, 400000));