Skip to content

Commit 72055ff

Browse files
authored
Merge pull request #4578 from tannewt/packetbuffer_lengths
Improve PacketBuffer lengths and remove packet_size
2 parents 19235d9 + 5ec195b commit 72055ff

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
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

shared-bindings/_bleio/PacketBuffer.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ STATIC void check_for_deinit(bleio_packet_buffer_obj_t *self) {
9595

9696
//| def readinto(self, buf: WriteableBuffer) -> int:
9797
//| """Reads a single BLE packet into the ``buf``. Raises an exception if the next packet is longer
98-
//| than the given buffer. Use `packet_size` to read the maximum length of a single packet.
98+
//| than the given buffer. Use `incoming_packet_length` to read the maximum length of a single packet.
9999
//|
100100
//| :return: number of bytes read and stored into ``buf``
101101
//| :rtype: int"""
@@ -179,11 +179,6 @@ STATIC mp_obj_t bleio_packet_buffer_deinit(mp_obj_t self_in) {
179179
}
180180
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_packet_buffer_deinit_obj, bleio_packet_buffer_deinit);
181181

182-
//| packet_size: int
183-
//| """`packet_size` is the same as `incoming_packet_length`.
184-
//| The name `packet_size` is deprecated and
185-
//| will be removed in CircuitPython 6.0.0."""
186-
//|
187182
//| incoming_packet_length: int
188183
//| """Maximum length in bytes of a packet we are reading."""
189184
//|
@@ -233,9 +228,6 @@ STATIC const mp_rom_map_elem_t bleio_packet_buffer_locals_dict_table[] = {
233228
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&bleio_packet_buffer_readinto_obj) },
234229
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&bleio_packet_buffer_write_obj) },
235230

236-
// .packet_size is now an alias for .incoming_packet_length
237-
// TODO: Remove in 6.0.0.
238-
{ MP_OBJ_NEW_QSTR(MP_QSTR_packet_size), MP_ROM_PTR(&bleio_packet_buffer_incoming_packet_length_obj) },
239231
{ MP_OBJ_NEW_QSTR(MP_QSTR_incoming_packet_length), MP_ROM_PTR(&bleio_packet_buffer_incoming_packet_length_obj) },
240232
{ MP_OBJ_NEW_QSTR(MP_QSTR_outgoing_packet_length), MP_ROM_PTR(&bleio_packet_buffer_outgoing_packet_length_obj) },
241233
};

0 commit comments

Comments
 (0)