This commit is contained in:
J. Nick Koston
2025-10-30 17:54:35 -05:00
parent 1925cd0379
commit d848cc33d7

View File

@@ -444,27 +444,30 @@ void ESP32BLE::loop() {
ESP_LOGV(TAG, "gap_event_handler - %d", gap_event);
#ifdef ESPHOME_ESP32_BLE_GAP_EVENT_HANDLER_COUNT
{
// Determine which union member to use based on event type.
// All event structures are properly laid out in memory per ESP-IDF.
// The reinterpret_cast operations are safe because:
// 1. Structure sizes match ESP-IDF expectations (verified by static_assert in ble_event.h)
// 2. Status fields are at offset 0 (verified by static_assert in ble_event.h)
// 3. The struct already contains our copy of the data (copied in BLEEvent constructor)
esp_ble_gap_cb_param_t *param;
// clang-format off
switch (gap_event) {
// All three scan complete events have the same structure with just status
// The scan_complete struct matches ESP-IDF's layout exactly, so this reinterpret_cast is safe
// This is verified at compile-time by static_assert checks in ble_event.h
// The struct already contains our copy of the status (copied in BLEEvent constructor)
GAP_SCAN_COMPLETE_EVENTS:
param = reinterpret_cast<esp_ble_gap_cb_param_t *>(&ble_event->event_.gap.scan_complete);
break;
// All advertising complete events have the same structure with just status
GAP_ADV_COMPLETE_EVENTS:
param = reinterpret_cast<esp_ble_gap_cb_param_t *>(&ble_event->event_.gap.adv_complete);
break;
case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT:
param = reinterpret_cast<esp_ble_gap_cb_param_t *>(&ble_event->event_.gap.read_rssi_complete);
break;
GAP_SECURITY_EVENTS:
param = reinterpret_cast<esp_ble_gap_cb_param_t *>(&ble_event->event_.gap.security);
break;
default:
break;
}