BluetoothDispatcher refactoring
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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<void(esp_bd_addr_t bt_addr)> DeviceConnectedCb;
|
||||
typedef std::function<void(BTAddress device)> DeviceConnectedCb;
|
||||
typedef std::function<void(char *package)> 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;
|
||||
};
|
||||
Reference in New Issue
Block a user