Unverified Commit dbf0b18d authored by Christian Ferbar's avatar Christian Ferbar Committed by GitHub

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

* esp32-hal-bt.c free Bluetooth LE memory if CONFIG_BTDM_CONTROLLER_MODE_BR_EDR_ONLY is set

BLE memory can be released if bluetooth-classic - only is requested

* tStart( add error output

* ble mem_release only for esp32

* disable BLE with BT_MODE define

* BluetoothSerial add begin()+disableBLE; add memrelease

* btStart with BT_MODE parameter

* beautification

* Update BluetoothSerial.cpp fix wrong merges

---------
Co-authored-by: default avatarMe No Dev <me-no-dev@users.noreply.github.com>
parent 7d26b070
......@@ -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;
}
}
......
......@@ -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
......
......@@ -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();
......@@ -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;
}
......@@ -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
......@@ -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)
......@@ -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)
{
......@@ -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
*/
......
......@@ -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();
}
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment