Skip to content

Commit

Permalink
AudioBLE cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 3, 2023
1 parent 4960467 commit a7a6ecf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
18 changes: 16 additions & 2 deletions src/Sandbox/BLE/AudioBLEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -152,6 +163,7 @@ class AudioBLEClient : public AudioBLEStream,
bool setupBLEClient() {
if (is_client_set_up)
return true;

TRACEI();

if (p_client == nullptr)
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 3 additions & 10 deletions src/Sandbox/BLE/AudioBLEServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Sandbox/BLE/AudioBLEStream.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit a7a6ecf

Please sign in to comment.