From 8a5d3cd6c4a5c68606aafa7373a7b5fc2079a936 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 25 Mar 2020 17:41:47 -0700 Subject: [PATCH] Add exception on small buffer and fix Connecion WRITE handling --- ports/nrf/common-hal/_bleio/Connection.c | 3 ++- ports/nrf/common-hal/_bleio/PacketBuffer.c | 4 ++-- shared-bindings/_bleio/PacketBuffer.c | 7 ++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ports/nrf/common-hal/_bleio/Connection.c b/ports/nrf/common-hal/_bleio/Connection.c index 96e8b8fbe9..d4c7308fd1 100644 --- a/ports/nrf/common-hal/_bleio/Connection.c +++ b/ports/nrf/common-hal/_bleio/Connection.c @@ -162,7 +162,8 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) { self->do_bond_cccds = true; self->do_bond_cccds_request_time = supervisor_ticks_ms64(); } - break; + // Return false so other handlers get this event as well. + return false; case BLE_GATTS_EVT_SYS_ATTR_MISSING: sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0); diff --git a/ports/nrf/common-hal/_bleio/PacketBuffer.c b/ports/nrf/common-hal/_bleio/PacketBuffer.c index 8382a09709..6ed6d14514 100644 --- a/ports/nrf/common-hal/_bleio/PacketBuffer.c +++ b/ports/nrf/common-hal/_bleio/PacketBuffer.c @@ -148,6 +148,7 @@ STATIC bool packet_buffer_on_ble_server_evt(ble_evt_t *ble_evt, void *param) { // A client wrote to this server characteristic. ble_gatts_evt_write_t *evt_write = &ble_evt->evt.gatts_evt.params.write; + // Event handle must match the handle for my characteristic. if (evt_write->handle == self->characteristic->handle) { if (self->conn_handle == BLE_CONN_HANDLE_INVALID) { @@ -261,8 +262,7 @@ int common_hal_bleio_packet_buffer_readinto(bleio_packet_buffer_obj_t *self, uin sd_nvic_critical_region_enter(&is_nested_critical_region); if (packet_length > len) { - // TODO: raise an exception. - packet_length = len; + return len - packet_length; } ringbuf_get_n(&self->ringbuf, data, packet_length); diff --git a/shared-bindings/_bleio/PacketBuffer.c b/shared-bindings/_bleio/PacketBuffer.c index 3ed295f017..9e3666044a 100644 --- a/shared-bindings/_bleio/PacketBuffer.c +++ b/shared-bindings/_bleio/PacketBuffer.c @@ -109,7 +109,12 @@ STATIC mp_obj_t bleio_packet_buffer_readinto(mp_obj_t self_in, mp_obj_t buffer_o mp_buffer_info_t bufinfo; mp_get_buffer_raise(buffer_obj, &bufinfo, MP_BUFFER_WRITE); - return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_packet_buffer_readinto(self, bufinfo.buf, bufinfo.len)); + int size = common_hal_bleio_packet_buffer_readinto(self, bufinfo.buf, bufinfo.len); + if (size < 0) { + mp_raise_ValueError_varg(translate("Buffer too short by %d bytes"), size * -1); + } + + return MP_OBJ_NEW_SMALL_INT(size); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_packet_buffer_readinto_obj, bleio_packet_buffer_readinto);