Skip to content

Commit e3b3f97

Browse files
committed
Improve PacketBuffer packet lengths for remote service
1 parent 58835e5 commit e3b3f97

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

ports/nrf/common-hal/_bleio/PacketBuffer.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ void common_hal_bleio_packet_buffer_construct(
252252
}
253253

254254
mp_int_t common_hal_bleio_packet_buffer_readinto(bleio_packet_buffer_obj_t *self, uint8_t *data, size_t len) {
255+
if (self->conn_handle == BLE_CONN_HANDLE_INVALID) {
256+
mp_raise_ConnectionError(translate("Not connected"));
257+
}
255258
if (ringbuf_num_filled(&self->ringbuf) < 2) {
256259
return 0;
257260
}
@@ -357,8 +360,7 @@ mp_int_t common_hal_bleio_packet_buffer_get_incoming_packet_length(bleio_packet_
357360
if (self->conn_handle != BLE_CONN_HANDLE_INVALID) {
358361
bleio_connection_internal_t *connection = bleio_conn_handle_to_connection(self->conn_handle);
359362
if (connection) {
360-
return MIN(common_hal_bleio_connection_get_max_packet_length(connection),
361-
self->characteristic->max_length);
363+
return common_hal_bleio_connection_get_max_packet_length(connection);
362364
}
363365
}
364366
// There's no current connection, so we don't know the MTU, and
@@ -396,6 +398,18 @@ mp_int_t common_hal_bleio_packet_buffer_get_outgoing_packet_length(bleio_packet_
396398
// we can't tell what the largest outgoing packet length would be.
397399
return -1;
398400
}
401+
// If we are talking to a remote service, we'll be bound by the MTU. (We don't actually
402+
// know the max size of the remote characteristic.)
403+
if (self->characteristic->service != NULL &&
404+
self->characteristic->service->is_remote) {
405+
// We are talking to a remote service so we're writing.
406+
if (self->conn_handle != BLE_CONN_HANDLE_INVALID) {
407+
bleio_connection_internal_t *connection = bleio_conn_handle_to_connection(self->conn_handle);
408+
if (connection) {
409+
return common_hal_bleio_connection_get_max_packet_length(connection);
410+
}
411+
}
412+
}
399413
return self->characteristic->max_length;
400414
}
401415

0 commit comments

Comments
 (0)