Skip to content

Commit 519ef78

Browse files
committed
use bt only when enabled
1 parent 3b971f2 commit 519ef78

File tree

2 files changed

+73
-88
lines changed

2 files changed

+73
-88
lines changed

cores/esp32/esp32-hal-bt.c

Lines changed: 71 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
6+
//
77
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
@@ -12,112 +12,95 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "esp32-hal-bt.h"
16-
17-
#if SOC_BT_SUPPORTED
18-
#ifdef CONFIG_BT_ENABLED
15+
#include "sdkconfig.h"
16+
#if defined(CONFIG_BT_ENABLED) && SOC_BT_SUPPORTED
1917

2018
#include "esp_bt.h"
19+
#include "esp_bt_main.h"
20+
#include "esp_bt_device.h"
21+
#include "esp32-hal-bt.h"
22+
#include "esp_log.h"
2123

22-
#ifdef CONFIG_BTDM_CONTROLLER_MODE_BTDM
23-
#define BT_MODE ESP_BT_MODE_BTDM
24-
#elif defined(CONFIG_BTDM_CONTROLLER_MODE_BR_EDR_ONLY)
25-
#define BT_MODE ESP_BT_MODE_CLASSIC_BT
26-
#else
27-
#define BT_MODE ESP_BT_MODE_BLE
28-
#endif
24+
static const char* TAG = "arduino-bt";
25+
static bool _bt_initialized = false;
26+
static bool _bt_enabled = false;
2927

30-
bool btStarted() {
31-
return (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
28+
// Diese Implementierung überschreibt die schwache (weak) Version in esp32-hal-misc.c
29+
bool btInUse() {
30+
return _bt_initialized && _bt_enabled;
3231
}
3332

3433
bool btStart() {
35-
return btStartMode(BT_MODE);
36-
}
34+
esp_err_t err;
3735

38-
bool btStartMode(bt_mode mode) {
39-
esp_bt_mode_t esp_bt_mode;
40-
esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
41-
#if CONFIG_IDF_TARGET_ESP32
42-
switch (mode) {
43-
case BT_MODE_BLE: esp_bt_mode = ESP_BT_MODE_BLE; break;
44-
case BT_MODE_CLASSIC_BT: esp_bt_mode = ESP_BT_MODE_CLASSIC_BT; break;
45-
case BT_MODE_BTDM: esp_bt_mode = ESP_BT_MODE_BTDM; break;
46-
default: esp_bt_mode = BT_MODE; break;
47-
}
48-
// esp_bt_controller_enable(MODE) This mode must be equal as the mode in “cfg” of esp_bt_controller_init().
49-
cfg.mode = esp_bt_mode;
50-
if (cfg.mode == ESP_BT_MODE_CLASSIC_BT) {
51-
esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
52-
}
53-
#else
54-
// other esp variants dont support BT-classic / DM.
55-
esp_bt_mode = BT_MODE;
56-
#endif
36+
if(_bt_enabled) {
37+
return true;
38+
}
5739

58-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
59-
return true;
60-
}
61-
esp_err_t ret;
62-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
63-
if ((ret = esp_bt_controller_init(&cfg)) != ESP_OK) {
64-
log_e("initialize controller failed: %s", esp_err_to_name(ret));
65-
return false;
40+
if(!_bt_initialized) {
41+
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
42+
43+
err = esp_bt_controller_init(&bt_cfg);
44+
if(err != ESP_OK) {
45+
ESP_LOGE(TAG, "BT controller initialize failed: %s", esp_err_to_name(err));
46+
return false;
47+
}
48+
49+
_bt_initialized = true;
50+
}
51+
52+
err = esp_bt_controller_enable(ESP_BT_MODE_BTDM);
53+
if(err != ESP_OK) {
54+
ESP_LOGE(TAG, "BT controller enable failed: %s", esp_err_to_name(err));
55+
return false;
56+
}
57+
58+
err = esp_bluedroid_init();
59+
if(err != ESP_OK) {
60+
ESP_LOGE(TAG, "Bluedroid initialize failed: %s", esp_err_to_name(err));
61+
esp_bt_controller_disable();
62+
return false;
6663
}
67-
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {}
68-
}
69-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
70-
if ((ret = esp_bt_controller_enable(esp_bt_mode)) != ESP_OK) {
71-
log_e("BT Enable mode=%d failed %s", BT_MODE, esp_err_to_name(ret));
72-
return false;
64+
65+
err = esp_bluedroid_enable();
66+
if(err != ESP_OK) {
67+
ESP_LOGE(TAG, "Bluedroid enable failed: %s", esp_err_to_name(err));
68+
esp_bluedroid_deinit();
69+
esp_bt_controller_disable();
70+
return false;
7371
}
74-
}
75-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
72+
73+
_bt_enabled = true;
7674
return true;
77-
}
78-
log_e("BT Start failed");
79-
return false;
8075
}
8176

8277
bool btStop() {
83-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
84-
return true;
85-
}
86-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) {
87-
if (esp_bt_controller_disable()) {
88-
log_e("BT Disable failed");
89-
return false;
90-
}
91-
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
92-
}
93-
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
94-
if (esp_bt_controller_deinit()) {
95-
log_e("BT deint failed");
96-
return false;
78+
esp_err_t err;
79+
80+
if(!_bt_enabled) {
81+
return true;
9782
}
98-
vTaskDelay(1);
99-
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_IDLE) {
100-
return false;
83+
84+
err = esp_bluedroid_disable();
85+
if(err != ESP_OK) {
86+
ESP_LOGE(TAG, "Bluedroid disable failed: %s", esp_err_to_name(err));
87+
return false;
10188
}
102-
return true;
103-
}
104-
log_e("BT Stop failed");
105-
return false;
106-
}
10789

108-
#else // CONFIG_BT_ENABLED
109-
bool btStarted() {
110-
return false;
111-
}
90+
err = esp_bluedroid_deinit();
91+
if(err != ESP_OK) {
92+
ESP_LOGE(TAG, "Bluedroid deinitialize failed: %s", esp_err_to_name(err));
93+
return false;
94+
}
11295

113-
bool btStart() {
114-
return false;
115-
}
96+
err = esp_bt_controller_disable();
97+
if(err != ESP_OK) {
98+
ESP_LOGE(TAG, "BT controller disable failed: %s", esp_err_to_name(err));
99+
return false;
100+
}
116101

117-
bool btStop() {
118-
return false;
102+
_bt_enabled = false;
103+
return true;
119104
}
120105

121-
#endif /* CONFIG_BT_ENABLED */
122-
123-
#endif /* SOC_BT_SUPPORTED */
106+
#endif

cores/esp32/esp32-hal-misc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
#if defined(CONFIG_BT_ENABLED) && SOC_BT_SUPPORTED
2929
#include "esp_bt.h"
3030
#if CONFIG_IDF_TARGET_ESP32
31+
bool btInUse() __attribute__((weak));
3132
bool btInUse() {
3233
return true;
3334
}
3435
#else
36+
bool btInUse() __attribute__((weak));
3537
bool btInUse() {
3638
return false;
3739
}

0 commit comments

Comments
 (0)