From d848cc33d7fd4d197f9697590234099c3f6dd5ba Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 30 Oct 2025 17:54:35 -0500 Subject: [PATCH] dry --- esphome/components/esp32_ble/ble.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index 117f489777..69e317ff6d 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -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(&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(&ble_event->event_.gap.adv_complete); break; + case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT: param = reinterpret_cast(&ble_event->event_.gap.read_rssi_complete); break; + GAP_SECURITY_EVENTS: param = reinterpret_cast(&ble_event->event_.gap.security); break; + default: break; }