From b273f59c4e2b0d6a56e54b1dd0c524e6d0d6c1c5 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 13 Apr 2021 15:37:47 -0700 Subject: [PATCH] Assume max characteristic size when the client --- ports/nrf/common-hal/_bleio/PacketBuffer.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ports/nrf/common-hal/_bleio/PacketBuffer.c b/ports/nrf/common-hal/_bleio/PacketBuffer.c index 42fc3475d6..0e90da1df6 100644 --- a/ports/nrf/common-hal/_bleio/PacketBuffer.c +++ b/ports/nrf/common-hal/_bleio/PacketBuffer.c @@ -196,18 +196,23 @@ void common_hal_bleio_packet_buffer_construct( bleio_characteristic_properties_t incoming = self->characteristic->props & (CHAR_PROP_WRITE_NO_RESPONSE | CHAR_PROP_WRITE); bleio_characteristic_properties_t outgoing = self->characteristic->props & (CHAR_PROP_NOTIFY | CHAR_PROP_INDICATE); + uint16_t max_packet_size; if (self->client) { // Swap if we're the client. bleio_characteristic_properties_t temp = incoming; incoming = outgoing; outgoing = temp; self->conn_handle = bleio_connection_get_conn_handle(MP_OBJ_TO_PTR(self->characteristic->service->connection)); + // TODO: We may want to make this variable because our BLE connection may not be able to + // negotiate the higher MTU. + max_packet_size = BLE_GATTS_VAR_ATTR_LEN_MAX - 3; // 3 for ATT overhead } else { self->conn_handle = BLE_CONN_HANDLE_INVALID; + max_packet_size = characteristic->max_length; } if (incoming) { - if (!ringbuf_alloc(&self->ringbuf, buffer_size * (sizeof(uint16_t) + characteristic->max_length), false)) { + if (!ringbuf_alloc(&self->ringbuf, buffer_size * (sizeof(uint16_t) + max_packet_size), false)) { mp_raise_ValueError(translate("Buffer too large and unable to allocate")); } }