mirror of
https://github.com/esphome/esphome.git
synced 2026-09-21 03:58:41 +00:00
Fix static storage duration capture warnings in lambdas
Use braced scope with local variable to avoid capturing static storage duration variables directly in lambda captures.
This commit is contained in:
@@ -134,16 +134,30 @@ class HandlerCounts:
|
||||
_handler_counts = HandlerCounts()
|
||||
|
||||
|
||||
def _add_callback(parent_var, method, handler_var, params, call_args):
|
||||
"""Generate a lambda callback that forwards to a handler method.
|
||||
|
||||
Uses a braced scope with a local variable to avoid capturing variables
|
||||
with static storage duration.
|
||||
"""
|
||||
cg.add(
|
||||
cg.RawStatement(
|
||||
f"{{ auto *h = {handler_var}; "
|
||||
f"{parent_var}->{method}("
|
||||
f"[h]({params}) {{ h->{call_args}; }}); }}"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def register_gap_event_handler(parent_var: cg.MockObj, handler_var: cg.MockObj) -> None:
|
||||
"""Register a GAP event handler and track the count."""
|
||||
_handler_counts.gap_event += 1
|
||||
cg.add(
|
||||
parent_var.add_gap_event_callback(
|
||||
cg.RawExpression(
|
||||
f"[{handler_var}](esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {{ "
|
||||
f"{handler_var}->gap_event_handler(event, param); }}"
|
||||
)
|
||||
)
|
||||
_add_callback(
|
||||
parent_var,
|
||||
"add_gap_event_callback",
|
||||
handler_var,
|
||||
"esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param",
|
||||
"gap_event_handler(event, param)",
|
||||
)
|
||||
|
||||
|
||||
@@ -152,13 +166,12 @@ def register_gap_scan_event_handler(
|
||||
) -> None:
|
||||
"""Register a GAP scan event handler and track the count."""
|
||||
_handler_counts.gap_scan_event += 1
|
||||
cg.add(
|
||||
parent_var.add_gap_scan_event_callback(
|
||||
cg.RawExpression(
|
||||
f"[{handler_var}](const esphome::esp32_ble::BLEScanResult &scan_result) {{ "
|
||||
f"{handler_var}->gap_scan_event_handler(scan_result); }}"
|
||||
)
|
||||
)
|
||||
_add_callback(
|
||||
parent_var,
|
||||
"add_gap_scan_event_callback",
|
||||
handler_var,
|
||||
"const esphome::esp32_ble::BLEScanResult &scan_result",
|
||||
"gap_scan_event_handler(scan_result)",
|
||||
)
|
||||
|
||||
|
||||
@@ -167,14 +180,12 @@ def register_gattc_event_handler(
|
||||
) -> None:
|
||||
"""Register a GATTc event handler and track the count."""
|
||||
_handler_counts.gattc_event += 1
|
||||
cg.add(
|
||||
parent_var.add_gattc_event_callback(
|
||||
cg.RawExpression(
|
||||
f"[{handler_var}](esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, "
|
||||
f"esp_ble_gattc_cb_param_t *param) {{ "
|
||||
f"{handler_var}->gattc_event_handler(event, gattc_if, param); }}"
|
||||
)
|
||||
)
|
||||
_add_callback(
|
||||
parent_var,
|
||||
"add_gattc_event_callback",
|
||||
handler_var,
|
||||
"esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param",
|
||||
"gattc_event_handler(event, gattc_if, param)",
|
||||
)
|
||||
|
||||
|
||||
@@ -183,14 +194,12 @@ def register_gatts_event_handler(
|
||||
) -> None:
|
||||
"""Register a GATTs event handler and track the count."""
|
||||
_handler_counts.gatts_event += 1
|
||||
cg.add(
|
||||
parent_var.add_gatts_event_callback(
|
||||
cg.RawExpression(
|
||||
f"[{handler_var}](esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, "
|
||||
f"esp_ble_gatts_cb_param_t *param) {{ "
|
||||
f"{handler_var}->gatts_event_handler(event, gatts_if, param); }}"
|
||||
)
|
||||
)
|
||||
_add_callback(
|
||||
parent_var,
|
||||
"add_gatts_event_callback",
|
||||
handler_var,
|
||||
"esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param",
|
||||
"gatts_event_handler(event, gatts_if, param)",
|
||||
)
|
||||
|
||||
|
||||
@@ -199,13 +208,12 @@ def register_ble_status_event_handler(
|
||||
) -> None:
|
||||
"""Register a BLE status event handler and track the count."""
|
||||
_handler_counts.ble_status_event += 1
|
||||
cg.add(
|
||||
parent_var.add_ble_status_event_callback(
|
||||
cg.RawExpression(
|
||||
f"[{handler_var}]() {{ "
|
||||
f"{handler_var}->ble_before_disabled_event_handler(); }}"
|
||||
)
|
||||
)
|
||||
_add_callback(
|
||||
parent_var,
|
||||
"add_ble_status_event_callback",
|
||||
handler_var,
|
||||
"",
|
||||
"ble_before_disabled_event_handler()",
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user