BluetoothDispatcher refactoring

This commit is contained in:
2024-01-06 12:11:06 +07:00
parent b86af180ab
commit 3c2889b8f7
3 changed files with 64 additions and 37 deletions

View File

@@ -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));
}
}