ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bluedroid_gattc_queue.c
Go to the documentation of this file.
1// Linker wraps that stop Bluedroid running a failed queued MTU or execute write request again until the BTU task
2// stack overflows (esphome#17954). The same fix as removing the command from p_cmd_list in bta_gattc_act.c before
3// the failure path pops the queue; drop once an ESP-IDF release has it.
4
5// Compiled only with Bluedroid's private headers (bluedroid_headers_cmake); static analysis skips it
6#ifdef USE_ESP32_BLE_BLUEDROID_HEADERS
7
8#include <stddef.h>
9
10#include "bta_gattc_int.h"
11#include "osi/list.h"
12
13// The command bta_gattc_cfg_mtu() or bta_gattc_execute() is running, NULL outside them
14static tBTA_GATTC_CLCB *running_clcb = NULL;
15static tBTA_GATTC_DATA *running_cmd = NULL;
16
17void __real_bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
18void __real_bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
19tGATT_STATUS __real_GATTC_ConfigureMTU(UINT16 conn_id);
20tGATT_STATUS __real_GATTC_ExecuteWrite(UINT16 conn_id, BOOLEAN is_execute);
21
22// Runs on the Bluetooth host task; the failure path pops the queue again, so this nests
23static void run_command(void (*real)(tBTA_GATTC_CLCB *, tBTA_GATTC_DATA *), tBTA_GATTC_CLCB *p_clcb,
24 tBTA_GATTC_DATA *p_data) {
25 tBTA_GATTC_CLCB *const outer_clcb = running_clcb;
26 tBTA_GATTC_DATA *const outer_cmd = running_cmd;
27 running_clcb = p_clcb;
28 running_cmd = p_data;
29 real(p_clcb, p_data);
30 running_clcb = outer_clcb;
31 running_cmd = outer_cmd;
32}
33
34// The send failed, so the caller clears p_q_cmd and pops the queue. A command popped from p_cmd_list is still its
35// head and would run again; remove (and free) it first. A command not from the queue is not in it: a no-op.
36static void drop_failed_command(void) {
37 if (running_clcb != NULL && running_clcb->p_cmd_list != NULL && running_clcb->p_q_cmd == running_cmd) {
38 list_remove(running_clcb->p_cmd_list, running_cmd);
39 }
40}
41
42void __wrap_bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) {
43 run_command(__real_bta_gattc_cfg_mtu, p_clcb, p_data);
44}
45
46void __wrap_bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) {
47 run_command(__real_bta_gattc_execute, p_clcb, p_data);
48}
49
50// The failure checks are the ones bta_gattc_cfg_mtu() and bta_gattc_execute() make
51tGATT_STATUS __wrap_GATTC_ConfigureMTU(UINT16 conn_id) {
52 const tGATT_STATUS status = __real_GATTC_ConfigureMTU(conn_id);
53 if (status != GATT_SUCCESS && status != GATT_CMD_STARTED) {
54 drop_failed_command();
55 }
56 return status;
57}
58
59tGATT_STATUS __wrap_GATTC_ExecuteWrite(UINT16 conn_id, BOOLEAN is_execute) {
60 const tGATT_STATUS status = __real_GATTC_ExecuteWrite(conn_id, is_execute);
61 if (status != BTA_GATT_OK) {
62 drop_failed_command();
63 }
64 return status;
65}
66
67#endif // USE_ESP32_BLE_BLUEDROID_HEADERS
uint8_t status
Definition bl0942.h:8
tGATT_STATUS __wrap_GATTC_ExecuteWrite(UINT16 conn_id, BOOLEAN is_execute)
tGATT_STATUS __real_GATTC_ConfigureMTU(UINT16 conn_id)
tGATT_STATUS __real_GATTC_ExecuteWrite(UINT16 conn_id, BOOLEAN is_execute)
void __wrap_bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
void __real_bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
void __real_bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
void __wrap_bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
tGATT_STATUS __wrap_GATTC_ConfigureMTU(UINT16 conn_id)