Skip to content

Commit a2df439

Browse files
committed
extmod/btstack: Fix MTU handling.
There was no event handler for central-initiated MTU exchange. Fix truncation of notify/indicate to match NimBLE. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent a652695 commit a2df439

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

extmod/btstack/modbluetooth_btstack.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,14 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan
172172
uint8_t status = att_event_handle_value_indication_complete_get_status(packet);
173173
mp_bluetooth_gatts_on_indicate_complete(conn_handle, value_handle, status);
174174
} else if (event_type == ATT_EVENT_MTU_EXCHANGE_COMPLETE) {
175+
DEBUG_printf(" --> att mtu exchange complete\n");
175176
// This is triggered in peripheral mode, when exchange initiated by us or remote.
176177
uint16_t conn_handle = att_event_mtu_exchange_complete_get_handle(packet);
177178
uint16_t mtu = att_event_mtu_exchange_complete_get_MTU(packet);
178179
mp_bluetooth_gatts_on_mtu_exchanged(conn_handle, mtu);
179180
} else if (event_type == HCI_EVENT_LE_META || event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
180181
// Ignore, duplicated by att_server.c.
182+
DEBUG_printf(" --> hci att server event type: le_meta/disconnection (0x%02x)\n", event_type);
181183
} else {
182184
DEBUG_printf(" --> hci att server event type: unknown (0x%02x)\n", event_type);
183185
}
@@ -330,7 +332,8 @@ STATIC void btstack_packet_handler_generic(uint8_t packet_type, uint16_t channel
330332
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
331333
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
332334
} else if (event_type == GATT_EVENT_MTU) {
333-
// This is triggered in client mode.
335+
// This is triggered in central mode.
336+
DEBUG_printf(" --> gatt event mtu\n");
334337
uint16_t conn_handle = gatt_event_mtu_get_handle(packet);
335338
uint16_t mtu = gatt_event_mtu_get_MTU(packet);
336339
mp_bluetooth_gatts_on_mtu_exchanged(conn_handle, mtu);
@@ -1108,6 +1111,14 @@ int mp_bluetooth_gatts_notify_indicate(uint16_t conn_handle, uint16_t value_hand
11081111
mp_bluetooth_gatts_db_read(MP_STATE_PORT(bluetooth_btstack_root_pointers)->gatts_db, value_handle, &value, &value_len);
11091112
}
11101113

1114+
// Even if a lower MTU is negotiated, btstack allows sending a larger
1115+
// notification/indication. Truncate at the MTU-3 (to match NimBLE).
1116+
uint16_t current_mtu = att_server_get_mtu(conn_handle);
1117+
if (current_mtu) {
1118+
current_mtu -= 3;
1119+
value_len = MIN(value_len, current_mtu);
1120+
}
1121+
11111122
int err = ERROR_CODE_UNKNOWN_HCI_COMMAND;
11121123

11131124
// Attempt to send immediately. If it succeeds, btstack will copy the buffer.
@@ -1443,9 +1454,9 @@ int mp_bluetooth_gattc_write(uint16_t conn_handle, uint16_t value_handle, const
14431454
}
14441455

14451456
int mp_bluetooth_gattc_exchange_mtu(uint16_t conn_handle) {
1446-
DEBUG_printf("mp_bluetooth_exchange_mtu: conn_handle=%d mtu=%d\n", conn_handle, l2cap_max_le_mtu());
1457+
DEBUG_printf("mp_bluetooth_gattc_exchange_mtu: conn_handle=%d mtu=%d\n", conn_handle, l2cap_max_le_mtu());
14471458

1448-
gatt_client_send_mtu_negotiation(&btstack_packet_handler_att_server, conn_handle);
1459+
gatt_client_send_mtu_negotiation(&btstack_packet_handler_generic, conn_handle);
14491460

14501461
return 0;
14511462
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy