Skip to content

Bluetooth-classic: release BLE memory when BT classic only is requested #8051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 29, 2023
38 changes: 34 additions & 4 deletions cores/esp32/esp32-hal-bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,48 @@ bool btStarted(){
return (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
}

bool btStart(){
bool btStart() {
return btStartMode(BT_MODE);
}

bool btStartMode(bt_mode mode){
esp_bt_mode_t esp_bt_mode;
esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
#if CONFIG_IDF_TARGET_ESP32
switch(mode) {
case BT_MODE_BLE: esp_bt_mode=ESP_BT_MODE_BLE;
break;
case BT_MODE_CLASSIC_BT: esp_bt_mode=ESP_BT_MODE_CLASSIC_BT;
break;
case BT_MODE_BTDM: esp_bt_mode=ESP_BT_MODE_BTDM;
break;
default: esp_bt_mode=BT_MODE;
break;
}
// esp_bt_controller_enable(MODE) This mode must be equal as the mode in “cfg” of esp_bt_controller_init().
cfg.mode=esp_bt_mode;
if(cfg.mode == ESP_BT_MODE_CLASSIC_BT) {
esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
}
#else
// other esp variants dont support BT-classic / DM.
esp_bt_mode=BT_MODE;
#endif

if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED){
return true;
}
esp_err_t ret;
if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE){
esp_bt_controller_init(&cfg);
if((ret = esp_bt_controller_init(&cfg)) != ESP_OK) {
log_e("initialize controller failed: %s", esp_err_to_name(ret));
return false;
}
while(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE){}
}
if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED){
if (esp_bt_controller_enable(BT_MODE)) {
log_e("BT Enable failed");
if((ret = esp_bt_controller_enable(esp_bt_mode)) != ESP_OK) {
log_e("BT Enable mode=%d failed %s", BT_MODE, esp_err_to_name(ret));
return false;
}
}
Expand Down
3 changes: 3 additions & 0 deletions cores/esp32/esp32-hal-bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
extern "C" {
#endif

typedef enum {BT_MODE_DEFAULT, BT_MODE_BLE, BT_MODE_CLASSIC_BT, BT_MODE_BTDM } bt_mode;

bool btStarted();
bool btStart();
bool btStartMode(bt_mode mode);
bool btStop();

#ifdef __cplusplus
Expand Down
32 changes: 21 additions & 11 deletions libraries/BluetoothSerial/src/BluetoothSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
}
}

static bool _init_bt(const char *deviceName)
static bool _init_bt(const char *deviceName, bt_mode mode)
{
if(!_bt_event_group){
_bt_event_group = xEventGroupCreate();
Expand Down Expand Up @@ -678,7 +678,7 @@ static bool _init_bt(const char *deviceName)
}
}

if (!btStarted() && !btStart()){
if (!btStarted() && !btStartMode(mode)){
log_e("initialize controller failed");
return false;
}
Expand Down Expand Up @@ -815,11 +815,10 @@ static bool waitForSDPRecord(int timeout) {
return (xEventGroupWaitBits(_bt_event_group, BT_SDP_COMPLETED, pdFALSE, pdTRUE, xTicksToWait) & BT_SDP_COMPLETED) != 0;
}

/*
/**
* Serial Bluetooth Arduino
*
* */

*/
BluetoothSerial::BluetoothSerial()
{
local_name = "ESP32"; //default bluetooth name
Expand All @@ -831,15 +830,16 @@ BluetoothSerial::~BluetoothSerial(void)
}

/**
* @Param isMaster set to true if you want to connect to an other device
* @param isMaster set to true if you want to connect to an other device
* @param disableBLE if BLE is not used, its ram can be freed to get +10kB free ram
*/
bool BluetoothSerial::begin(String localName, bool isMaster)
bool BluetoothSerial::begin(String localName, bool isMaster, bool disableBLE)
{
_isMaster = isMaster;
if (localName.length()){
local_name = localName;
}
return _init_bt(local_name.c_str());
return _init_bt(local_name.c_str(), disableBLE ? BT_MODE_CLASSIC_BT : BT_MODE_BTDM);
}

int BluetoothSerial::available(void)
Expand Down Expand Up @@ -910,6 +910,14 @@ void BluetoothSerial::end()
_stop_bt();
}

/**
* free additional ~30kB ram, reset is required to enable BT again
*/
void BluetoothSerial::memrelease()
{
esp_bt_mem_release(ESP_BT_MODE_BTDM);
}

#ifdef CONFIG_BT_SSP_ENABLED
void BluetoothSerial::onConfirmRequest(ConfirmRequestCb cb)
{
Expand Down Expand Up @@ -1026,11 +1034,13 @@ bool BluetoothSerial::connect(String remoteName)
}

/**
* @Param channel: specify channel or 0 for auto-detect
* @Param sec_mask:
* Connect to an other bluetooth device
*
* @param channel specify channel or 0 for auto-detect
* @param sec_mask
* ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE
* ESP_SPP_SEC_NONE
* @Param role:
* @param role
* ESP_SPP_ROLE_MASTER master can handle up to 7 connections to slaves
* ESP_SPP_ROLE_SLAVE can only have one connection to a master
*/
Expand Down
3 changes: 2 additions & 1 deletion libraries/BluetoothSerial/src/BluetoothSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BluetoothSerial: public Stream
BluetoothSerial(void);
~BluetoothSerial(void);

bool begin(String localName=String(), bool isMaster=false);
bool begin(String localName=String(), bool isMaster=false, bool disableBLE=false);
bool begin(unsigned long baud){//compatibility
return begin();
}
Expand All @@ -54,6 +54,7 @@ class BluetoothSerial: public Stream
size_t write(const uint8_t *buffer, size_t size);
void flush();
void end(void);
void memrelease();
void setTimeout(int timeoutMS);
void onData(BluetoothSerialDataCb cb);
esp_err_t register_callback(esp_spp_cb_t * callback);
Expand Down