@@ -172,12 +172,14 @@ STATIC void btstack_packet_handler_att_server(uint8_t packet_type, uint16_t chan
172
172
uint8_t status = att_event_handle_value_indication_complete_get_status (packet );
173
173
mp_bluetooth_gatts_on_indicate_complete (conn_handle , value_handle , status );
174
174
} else if (event_type == ATT_EVENT_MTU_EXCHANGE_COMPLETE ) {
175
+ DEBUG_printf (" --> att mtu exchange complete\n" );
175
176
// This is triggered in peripheral mode, when exchange initiated by us or remote.
176
177
uint16_t conn_handle = att_event_mtu_exchange_complete_get_handle (packet );
177
178
uint16_t mtu = att_event_mtu_exchange_complete_get_MTU (packet );
178
179
mp_bluetooth_gatts_on_mtu_exchanged (conn_handle , mtu );
179
180
} else if (event_type == HCI_EVENT_LE_META || event_type == HCI_EVENT_DISCONNECTION_COMPLETE ) {
180
181
// Ignore, duplicated by att_server.c.
182
+ DEBUG_printf (" --> hci att server event type: le_meta/disconnection (0x%02x)\n" , event_type );
181
183
} else {
182
184
DEBUG_printf (" --> hci att server event type: unknown (0x%02x)\n" , event_type );
183
185
}
@@ -330,7 +332,8 @@ STATIC void btstack_packet_handler_generic(uint8_t packet_type, uint16_t channel
330
332
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
331
333
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
332
334
} 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" );
334
337
uint16_t conn_handle = gatt_event_mtu_get_handle (packet );
335
338
uint16_t mtu = gatt_event_mtu_get_MTU (packet );
336
339
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
1108
1111
mp_bluetooth_gatts_db_read (MP_STATE_PORT (bluetooth_btstack_root_pointers )-> gatts_db , value_handle , & value , & value_len );
1109
1112
}
1110
1113
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
+
1111
1122
int err = ERROR_CODE_UNKNOWN_HCI_COMMAND ;
1112
1123
1113
1124
// 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
1443
1454
}
1444
1455
1445
1456
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 ());
1447
1458
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 );
1449
1460
1450
1461
return 0 ;
1451
1462
}
0 commit comments