From a7a6ecf8f7aae88317ecd17d79043011ba83533b Mon Sep 17 00:00:00 2001 From: pschatzmann Date: Fri, 3 Nov 2023 19:02:43 +0100 Subject: [PATCH] AudioBLE cleanup --- src/Sandbox/BLE/AudioBLEClient.h | 18 ++++++++++++++++-- src/Sandbox/BLE/AudioBLEServer.h | 13 +++---------- src/Sandbox/BLE/AudioBLEStream.h | 3 ++- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Sandbox/BLE/AudioBLEClient.h b/src/Sandbox/BLE/AudioBLEClient.h index f114c2c2f7..009971c7d3 100644 --- a/src/Sandbox/BLE/AudioBLEClient.h +++ b/src/Sandbox/BLE/AudioBLEClient.h @@ -65,7 +65,7 @@ class AudioBLEClient : public AudioBLEStream, return str.length(); } - int available() override { return BLE_MTU; } + int available() override { return BLE_MTU - BLE_MTU_OVERHEAD; } size_t write(const uint8_t *data, size_t dataSize) override { TRACED(); @@ -80,7 +80,7 @@ class AudioBLEClient : public AudioBLEStream, return result; } - int availableForWrite() override { return BLE_MTU; } + int availableForWrite() override { return BLE_MTU - BLE_MTU_OVERHEAD; } bool connected() override { if (!setupBLEClient()) { @@ -125,6 +125,17 @@ class AudioBLEClient : public AudioBLEStream, info_char->writeValue((uint8_t *)&info, sizeof(AudioInfo)); } + bool readAudioInfoCharacteristic(){ + if (!info_char->canRead()) + return false; + auto str = info_char->readValue(); + if (str.length() > 0) { + setAudioInfo((const uint8_t*)str.c_str(), str.length()); + return true; + } + return false; + } + // Scanning Results void onResult(BLEAdvertisedDevice advertisedDevice) override { TRACEI(); @@ -152,6 +163,7 @@ class AudioBLEClient : public AudioBLEStream, bool setupBLEClient() { if (is_client_set_up) return true; + TRACEI(); if (p_client == nullptr) @@ -209,6 +221,8 @@ class AudioBLEClient : public AudioBLEStream, return false; } info_char->registerForNotify(notifyCallback); + readAudioInfoCharacteristic(); + } LOGI("Connected to server: %s", is_client_connected ? "true" : "false"); is_client_set_up = true; diff --git a/src/Sandbox/BLE/AudioBLEServer.h b/src/Sandbox/BLE/AudioBLEServer.h index b1e53bcf55..c3f4bd7d5b 100644 --- a/src/Sandbox/BLE/AudioBLEServer.h +++ b/src/Sandbox/BLE/AudioBLEServer.h @@ -26,20 +26,13 @@ class AudioBLEServer : public AudioBLEStream, TRACEI(); ble_server_name = name; BLEDevice::init(name); - //BLEDevice::setMTU(BLE_MTU); - // Increase connection interval to 30 milliseconds (30 * 1.25 ms) - // BLEDevice::setConnectionParams(30, 30, 0, 0); p_server = BLEDevice::createServer(); p_server->setCallbacks(this); setupBLEService(); - p_advertising = BLEDevice::getAdvertising(); p_advertising->addServiceUUID(BLE_AUDIO_SERVICE_UUID); - //p_advertising->setScanResponse(false); - //p_advertising->setMinPreferred(0x00); - // p_advertising->setMinPreferred(0x06); BLEDevice::startAdvertising(); return true; } @@ -121,8 +114,8 @@ class AudioBLEServer : public AudioBLEStream, TRACED(); if (max_transfer_size == 0) { int peer_max_transfer_size = - p_server->getPeerMTU(p_server->getConnId()) - 5; - max_transfer_size = std::min(BLE_MTU, peer_max_transfer_size); + p_server->getPeerMTU(p_server->getConnId()) - BLE_MTU_OVERHEAD; + max_transfer_size = std::min(BLE_MTU - BLE_MTU, peer_max_transfer_size); LOGI("max_transfer_size: %d", max_transfer_size); } @@ -201,7 +194,7 @@ class AudioBLEServer : public AudioBLEStream, auto uuid = pCharacteristic->getUUID().toString(); if (uuid == BLE_CH1_UUID || uuid == BLE_CH2_UUID) { setupTXBuffer(); - int len = min(getMTU(), (int)transmit_buffer.available()); + int len = std::min(getMTU() - BLE_MTU_OVERHEAD, (int)transmit_buffer.available()); if (is_framed) { len = transmit_buffer_sizes.read(); } diff --git a/src/Sandbox/BLE/AudioBLEStream.h b/src/Sandbox/BLE/AudioBLEStream.h index b50a904969..68bd21fcfd 100644 --- a/src/Sandbox/BLE/AudioBLEStream.h +++ b/src/Sandbox/BLE/AudioBLEStream.h @@ -1,11 +1,12 @@ #pragma once -#include "AudioBasic/Collections/Vector.h" #include "AudioConfig.h" +#include "AudioBasic/Collections/Vector.h" #include "AudioTools/Buffers.h" // must be greater than MTU, less than ESP_GATT_MAX_ATTR_LEN #define BLE_MTU 517 +#define BLE_MTU_OVERHEAD 5 #define RX_BUFFER_SIZE 4096 #define RX_COUNT 100 #define TX_BUFFER_SIZE 4096