Skip to content

Commit

Permalink
Audio BLE Server with ArduinoBLE libary
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 13, 2023
1 parent bee406c commit 7b79b0c
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 2,167 deletions.
1 change: 1 addition & 0 deletions src/Sandbox/BLE/AudioBLEClient.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "AudioBLEStream.h"
#include "ConstantsArduino.h"
#include <ArduinoBLE.h>

namespace audio_tools {
Expand Down
1 change: 1 addition & 0 deletions src/Sandbox/BLE/AudioBLEClientESP32.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "AudioBLEStream.h"
#include "ConstantsESP32.h"
//#include <BLE2902.h>
#include <BLEDevice.h>
#include <BLEServer.h>
Expand Down
38 changes: 19 additions & 19 deletions src/Sandbox/BLE/AudioBLEServer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "AudioBLEStream.h"
#include "ConstantsArduino.h"
#include <ArduinoBLE.h>

namespace audio_tools {
Expand All @@ -13,14 +14,16 @@ class AudioBLEServer *selfAudioBLEServer = nullptr;
* can be used to send and recevie audio. In BLE terminologiy this is a
* Peripheral.
* This implementation uses the ArduinoBLE library!
* This is working only correctly if the client sets the max MTU to a value >= 256.
* Otherwise some of the transmitted information gets silently dropped
* @ingroup communications
* @author Phil Schatzmann
* @copyright GPLv3
*/

class AudioBLEServer : public AudioBLEStream {
public:
AudioBLEServer(int mtu = BLE_MTU) : AudioBLEStream(mtu) {
AudioBLEServer(int mtu = 0) : AudioBLEStream(mtu) {
selfAudioBLEServer = this;
}

Expand Down Expand Up @@ -93,16 +96,14 @@ class AudioBLEServer : public AudioBLEStream {
return result;
}


bool connected() override { return checkCentralConnected(); }

protected:
// server
BLEDevice central;
BLEService service{BLE_AUDIO_SERVICE_UUID}; // create service
BLECharacteristic ch01_char{BLE_CH1_UUID, BLERead,
BLE_MTU - BLE_MTU_OVERHEAD};
BLECharacteristic ch02_char{BLE_CH2_UUID, BLEWrite,
BLE_MTU - BLE_MTU_OVERHEAD};
BLECharacteristic ch01_char{BLE_CH1_UUID, BLERead, getMTU()};
BLECharacteristic ch02_char{BLE_CH2_UUID, BLEWrite, getMTU()};
BLECharacteristic info_char{BLE_INFO_UUID, BLERead | BLEWrite | BLENotify,
80};
BLEDescriptor ch01_desc{"2901", "channel 1"};
Expand Down Expand Up @@ -159,27 +160,26 @@ class AudioBLEServer : public AudioBLEStream {
auto uuid = Str(characteristic.uuid());
if (uuid == BLE_CH1_UUID || uuid == BLE_CH2_UUID) {
TRACEI();
setupTXBuffer();
int len = std::min(getMTU() - BLE_MTU_OVERHEAD,
(int)transmit_buffer.available());
int len = std::min(getMTU(), (int)transmit_buffer.available());
if (is_framed) {
len = transmit_buffer_sizes.read();
}
LOGI("%s: len: %d, buffer: %d", uuid.c_str(), len,
transmit_buffer.size());
if (len>0){
assert(len==512);
if (len > 0) {
uint8_t tmp[len];
transmit_buffer.readArray(tmp, len);
characteristic.writeValue(tmp, len);
transmit_buffer.peekArray(tmp, len);
if (characteristic.writeValue(tmp, len)) {
transmit_buffer.readArray(tmp, len);
} else {
LOGW("writeValue failed")
}
}
}
}


bool checkCentralConnected() {
BLEDevice central = BLE.central();

central = BLE.central();
// if a central is connected to the peripheral:
if (central)
return central.connected();
Expand Down Expand Up @@ -211,6 +211,7 @@ class AudioBLEServer : public AudioBLEStream {
// p_server->getPeerMTU(p_server->getConnId()) - BLE_MTU_OVERHEAD;
// max_transfer_size = std::min(BLE_MTU - BLE_MTU,
// peer_max_transfer_size);
// max_transfer_size = central.mtu() - BLE_MTU_OVERHEAD;
max_transfer_size = BLE_MTU - BLE_MTU_OVERHEAD;

LOGI("max_transfer_size: %d", max_transfer_size);
Expand Down Expand Up @@ -248,14 +249,13 @@ class AudioBLEServer : public AudioBLEStream {
}

// Read callback works only when we provide some initial data
uint8_t tmp[512]={0xFF};
uint8_t tmp[512] = {0xFF};
ch01_char.writeValue(tmp, 512, false);
}

void setupTXBuffer() {
if (transmit_buffer.size() == 0) {
LOGI("Setting transmit_buffer to %d for mtu %d", RX_BUFFER_SIZE,
getMTU());
LOGI("Setting transmit_buffer to %d", RX_BUFFER_SIZE);
transmit_buffer.resize(TX_BUFFER_SIZE);
if (is_framed) {
transmit_buffer_sizes.resize(TX_COUNT);
Expand Down
1 change: 1 addition & 0 deletions src/Sandbox/BLE/AudioBLEServerESP32.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "AudioBLEStream.h"
#include "ConstantsESP32.h"
//#include <BLE2902.h>
#include <BLEDevice.h>
#include <BLEServer.h>
Expand Down
9 changes: 1 addition & 8 deletions src/Sandbox/BLE/AudioBLEStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@
#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
#define TX_COUNT 100

namespace audio_tools {

Expand Down Expand Up @@ -83,7 +76,7 @@ class AudioBLEStream : public AudioStream {
const char *BLE_CH2_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"; // TX
const char *BLE_INFO_UUID = "6e400004-b5a3-f393-e0a9-e50e24dcca9e";

virtual int getMTU() { return BLE_MTU; }
virtual int getMTU() = 0;

// override to implement your own extended logic
virtual void setAudioInfo(const uint8_t *data, size_t size) {
Expand Down
Loading

0 comments on commit 7b79b0c

Please sign in to comment.