Skip to content

Commit c90b4a4

Browse files
alwa-nordiccarlescufi
authored andcommitted
Bluetooth: Shell: Fix GATT exchange mtu assertion fail
This change fixes a bug where the second use of shell command `gatt exchange-mtu` would trigger an assertion failure in the stack. The BT API `bt_gatt_exchange_mtu` asserts `params->func != NULL`. Signed-off-by: Aleksander Wasaznik <aleksander.wasaznik@nordicsemi.no>
1 parent dafdef8 commit c90b4a4

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

subsys/bluetooth/shell/gatt.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,22 @@ static void reset_write_stats(void)
8585
memset(&write_stats, 0, sizeof(write_stats));
8686
}
8787

88+
/* This variable is write-locked when `(exchange_params.func != NULL)`.
89+
* Must be zero-initialized when unlocked.
90+
*/
91+
static struct bt_gatt_exchange_params exchange_params;
92+
8893
static void exchange_func(struct bt_conn *conn, uint8_t err,
8994
struct bt_gatt_exchange_params *params)
9095
{
9196
shell_print(ctx_shell, "Exchange %s", err == 0U ? "successful" :
9297
"failed");
9398

99+
/* Release global `exchange_params`. */
100+
__ASSERT_NO_MSG(params == &exchange_params);
94101
(void)memset(params, 0, sizeof(*params));
95102
}
96103

97-
static struct bt_gatt_exchange_params exchange_params = {
98-
.func = exchange_func,
99-
};
100-
101104
static int cmd_exchange_mtu(const struct shell *sh,
102105
size_t argc, char *argv[])
103106
{
@@ -108,7 +111,19 @@ static int cmd_exchange_mtu(const struct shell *sh,
108111
return -ENOEXEC;
109112
}
110113

114+
if (exchange_params.func) {
115+
shell_print(sh, "Shell command busy. A previous invocation is in progress.");
116+
return -EBUSY;
117+
}
118+
119+
exchange_params.func = exchange_func;
120+
111121
err = bt_gatt_exchange_mtu(default_conn, &exchange_params);
122+
if (err) {
123+
/* Release global `exchange_params`. */
124+
exchange_params.func = NULL;
125+
}
126+
112127
if (err == -EALREADY) {
113128
shell_print(sh, "Already exchanged");
114129
} else if (err) {

0 commit comments

Comments
 (0)