1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-15 09:42:19 +01:00
This commit is contained in:
J. Nick Koston
2025-06-11 10:20:18 -05:00
parent 78315fd388
commit 0e9f14f969
2 changed files with 24 additions and 22 deletions

View File

@@ -322,23 +322,19 @@ void ESP32BLE::loop() {
} else if (gap_event == ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT || } else if (gap_event == ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT ||
gap_event == ESP_GAP_BLE_SCAN_START_COMPLETE_EVT || gap_event == ESP_GAP_BLE_SCAN_START_COMPLETE_EVT ||
gap_event == ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT) { gap_event == ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT) {
// Create temporary param for scan complete events // All three scan complete events have the same structure with just status
esp_ble_gap_cb_param_t param; // We can create a minimal structure that matches their layout
memset(&param, 0, sizeof(param)); struct {
esp_bt_status_t status;
} scan_complete_param;
// Set the appropriate status field based on event type scan_complete_param.status = ble_event->event_.gap.scan_complete.status;
if (gap_event == ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT) {
param.scan_param_cmpl.status = ble_event->event_.gap.scan_complete.status;
} else if (gap_event == ESP_GAP_BLE_SCAN_START_COMPLETE_EVT) {
param.scan_start_cmpl.status = ble_event->event_.gap.scan_complete.status;
} else if (gap_event == ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT) {
param.scan_stop_cmpl.status = ble_event->event_.gap.scan_complete.status;
}
this->real_gap_event_handler_(gap_event, &param); // Cast is safe because all three event structures start with status
this->real_gap_event_handler_(gap_event, (esp_ble_gap_cb_param_t *) &scan_complete_param);
} else { } else {
// Fallback for unexpected events (uses full param copy) // Unexpected GAP event - log and drop
this->real_gap_event_handler_(gap_event, &ble_event->event_.gap.gap_param); ESP_LOGW(TAG, "Unexpected GAP event type: %d", gap_event);
} }
break; break;
} }
@@ -357,6 +353,13 @@ void ESP32BLE::loop() {
void ESP32BLE::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { void ESP32BLE::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
static constexpr size_t MAX_BLE_QUEUE_SIZE = SCAN_RESULT_BUFFER_SIZE * 2; static constexpr size_t MAX_BLE_QUEUE_SIZE = SCAN_RESULT_BUFFER_SIZE * 2;
// Only queue the 4 GAP events we actually handle
if (event != ESP_GAP_BLE_SCAN_RESULT_EVT && event != ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT &&
event != ESP_GAP_BLE_SCAN_START_COMPLETE_EVT && event != ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT) {
ESP_LOGW(TAG, "Ignoring unexpected GAP event type: %d", event);
return;
}
if (global_ble->ble_events_.size() >= MAX_BLE_QUEUE_SIZE) { if (global_ble->ble_events_.size() >= MAX_BLE_QUEUE_SIZE) {
ESP_LOGW(TAG, "BLE event queue full (%d), dropping GAP event %d", MAX_BLE_QUEUE_SIZE, event); ESP_LOGW(TAG, "BLE event queue full (%d), dropping GAP event %d", MAX_BLE_QUEUE_SIZE, event);
return; return;

View File

@@ -48,9 +48,8 @@ class BLEEvent {
break; break;
default: default:
// For any other GAP events, copy the full param // We only handle 4 GAP event types, others are dropped
// This is a safety fallback but shouldn't happen in normal operation // This should never happen in normal operation
memcpy(&this->event_.gap.gap_param, p, sizeof(esp_ble_gap_cb_param_t));
break; break;
} }
}; };
@@ -106,10 +105,10 @@ class BLEEvent {
esp_bt_status_t status; esp_bt_status_t status;
} scan_complete; // 1 byte } scan_complete; // 1 byte
// Fallback for unexpected events (shouldn't be used) // We only handle 4 GAP event types, no need for full fallback
esp_ble_gap_cb_param_t gap_param; // If we ever get an unexpected event, we'll just drop it in ble.cpp
}; };
} gap; // ~80 bytes instead of 400+ } gap; // ~73 bytes (size of BLEScanResult)
// NOLINTNEXTLINE(readability-identifier-naming) // NOLINTNEXTLINE(readability-identifier-naming)
struct gattc_event { struct gattc_event {
@@ -124,7 +123,7 @@ class BLEEvent {
esp_gatt_if_t gatts_if; esp_gatt_if_t gatts_if;
esp_ble_gatts_cb_param_t gatts_param; esp_ble_gatts_cb_param_t gatts_param;
} gatts; // ~68 bytes } gatts; // ~68 bytes
} event_; // Union size is now ~80 bytes (largest member) } event_; // Union size is now ~73 bytes (BLEScanResult is largest)
std::vector<uint8_t> data{}; // For GATTC/GATTS data std::vector<uint8_t> data{}; // For GATTC/GATTS data
@@ -140,7 +139,7 @@ class BLEEvent {
const BLEScanResult &scan_result() const { return event_.gap.scan_result; } const BLEScanResult &scan_result() const { return event_.gap.scan_result; }
esp_bt_status_t scan_complete_status() const { return event_.gap.scan_complete.status; } esp_bt_status_t scan_complete_status() const { return event_.gap.scan_complete.status; }
}; };
// Total size: ~110 bytes instead of 440 bytes! // Total size: ~100 bytes instead of 440 bytes!
} // namespace esp32_ble } // namespace esp32_ble
} // namespace esphome } // namespace esphome