Fix BT not starting correctly and SPP Coex not working

Fixes: https://github.com/espressif/arduino-esp32/issues/4912
This commit is contained in:
me-no-dev 2021-03-09 01:56:47 +02:00
parent 7dc769d81c
commit 4d95e3a7ea
2 changed files with 38 additions and 22 deletions

View File

@ -20,8 +20,10 @@ bool btInUse(){ return true; }
#include "esp_bt.h" #include "esp_bt.h"
#ifdef CONFIG_BT_CLASSIC_ENABLED #ifdef CONFIG_BTDM_CONTROLLER_MODE_BTDM
#define BT_MODE ESP_BT_MODE_BTDM #define BT_MODE ESP_BT_MODE_BTDM
#elif defined(CONFIG_BTDM_CONTROLLER_MODE_BR_EDR_ONLY)
#define BT_MODE ESP_BT_MODE_CLASSIC_BT
#else #else
#define BT_MODE ESP_BT_MODE_BLE #define BT_MODE ESP_BT_MODE_BLE
#endif #endif

View File

@ -235,26 +235,34 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
break; break;
case ESP_SPP_SRV_OPEN_EVT://Server connection open case ESP_SPP_SRV_OPEN_EVT://Server connection open
log_i("ESP_SPP_SRV_OPEN_EVT"); if (param->srv_open.status == ESP_SPP_SUCCESS) {
if (!_spp_client){ log_i("ESP_SPP_SRV_OPEN_EVT");
_spp_client = param->open.handle; if (!_spp_client){
_spp_client = param->srv_open.handle;
} else {
secondConnectionAttempt = true;
esp_spp_disconnect(param->srv_open.handle);
}
xEventGroupClearBits(_spp_event_group, SPP_DISCONNECTED);
xEventGroupSetBits(_spp_event_group, SPP_CONNECTED);
} else { } else {
secondConnectionAttempt = true; log_e("ESP_SPP_SRV_OPEN_EVT Failed!, status:%d", param->srv_open.status);
esp_spp_disconnect(param->open.handle);
} }
xEventGroupClearBits(_spp_event_group, SPP_DISCONNECTED);
xEventGroupSetBits(_spp_event_group, SPP_CONNECTED);
break; break;
case ESP_SPP_CLOSE_EVT://Client connection closed case ESP_SPP_CLOSE_EVT://Client connection closed
log_i("ESP_SPP_CLOSE_EVT"); if ((param->close.async == false && param->close.status == ESP_SPP_SUCCESS) || param->close.async) {
if(secondConnectionAttempt) { log_i("ESP_SPP_CLOSE_EVT");
secondConnectionAttempt = false; if(secondConnectionAttempt) {
secondConnectionAttempt = false;
} else {
_spp_client = 0;
xEventGroupSetBits(_spp_event_group, SPP_DISCONNECTED);
}
xEventGroupClearBits(_spp_event_group, SPP_CONNECTED);
} else { } else {
_spp_client = 0; log_e("ESP_SPP_CLOSE_EVT failed!, status:%d", param->close.status);
xEventGroupSetBits(_spp_event_group, SPP_DISCONNECTED);
} }
xEventGroupClearBits(_spp_event_group, SPP_CONNECTED);
break; break;
case ESP_SPP_CONG_EVT://connection congestion status changed case ESP_SPP_CONG_EVT://connection congestion status changed
@ -267,11 +275,15 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
break; break;
case ESP_SPP_WRITE_EVT://write operation completed case ESP_SPP_WRITE_EVT://write operation completed
if(param->write.cong){ if (param->write.status == ESP_SPP_SUCCESS) {
xEventGroupClearBits(_spp_event_group, SPP_CONGESTED); if(param->write.cong){
xEventGroupClearBits(_spp_event_group, SPP_CONGESTED);
}
xSemaphoreGive(_spp_tx_done);//we can try to send another packet
log_v("ESP_SPP_WRITE_EVT: %u %s", param->write.len, param->write.cong?"CONGESTED":"");
} else {
log_e("ESP_SPP_WRITE_EVT failed!, status:%d", param->write.status);
} }
xSemaphoreGive(_spp_tx_done);//we can try to send another packet
log_v("ESP_SPP_WRITE_EVT: %u %s", param->write.len, param->write.cong?"CONGESTED":"FREE");
break; break;
case ESP_SPP_DATA_IND_EVT://connection received data case ESP_SPP_DATA_IND_EVT://connection received data
@ -296,6 +308,8 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
if (param->disc_comp.status == ESP_SPP_SUCCESS) { if (param->disc_comp.status == ESP_SPP_SUCCESS) {
log_i("ESP_SPP_DISCOVERY_COMP_EVT: spp connect to remote"); log_i("ESP_SPP_DISCOVERY_COMP_EVT: spp connect to remote");
esp_spp_connect(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, param->disc_comp.scn[0], _peer_bd_addr); esp_spp_connect(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, param->disc_comp.scn[0], _peer_bd_addr);
} else {
log_e("ESP_SPP_DISCOVERY_COMP_EVT failed!, status:%d", param->disc_comp.status);
} }
break; break;
@ -532,9 +546,9 @@ static bool _init_bt(const char *deviceName)
return false; return false;
} }
if (esp_bt_sleep_disable() != ESP_OK){ // if (esp_bt_sleep_disable() != ESP_OK){
log_e("esp_bt_sleep_disable failed"); // log_e("esp_bt_sleep_disable failed");
} // }
log_i("device name set"); log_i("device name set");
esp_bt_dev_set_device_name(deviceName); esp_bt_dev_set_device_name(deviceName);