Skip to content

Commit 8a5d3cd

Browse files
committed
Add exception on small buffer and fix Connecion WRITE handling
1 parent 3551b76 commit 8a5d3cd

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
162162
self->do_bond_cccds = true;
163163
self->do_bond_cccds_request_time = supervisor_ticks_ms64();
164164
}
165-
break;
165+
// Return false so other handlers get this event as well.
166+
return false;
166167

167168
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
168169
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ STATIC bool packet_buffer_on_ble_server_evt(ble_evt_t *ble_evt, void *param) {
148148
// A client wrote to this server characteristic.
149149

150150
ble_gatts_evt_write_t *evt_write = &ble_evt->evt.gatts_evt.params.write;
151+
151152
// Event handle must match the handle for my characteristic.
152153
if (evt_write->handle == self->characteristic->handle) {
153154
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
261262
sd_nvic_critical_region_enter(&is_nested_critical_region);
262263

263264
if (packet_length > len) {
264-
// TODO: raise an exception.
265-
packet_length = len;
265+
return len - packet_length;
266266
}
267267

268268
ringbuf_get_n(&self->ringbuf, data, packet_length);

shared-bindings/_bleio/PacketBuffer.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ STATIC mp_obj_t bleio_packet_buffer_readinto(mp_obj_t self_in, mp_obj_t buffer_o
109109
mp_buffer_info_t bufinfo;
110110
mp_get_buffer_raise(buffer_obj, &bufinfo, MP_BUFFER_WRITE);
111111

112-
return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_packet_buffer_readinto(self, bufinfo.buf, bufinfo.len));
112+
int size = common_hal_bleio_packet_buffer_readinto(self, bufinfo.buf, bufinfo.len);
113+
if (size < 0) {
114+
mp_raise_ValueError_varg(translate("Buffer too short by %d bytes"), size * -1);
115+
}
116+
117+
return MP_OBJ_NEW_SMALL_INT(size);
113118
}
114119
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_packet_buffer_readinto_obj, bleio_packet_buffer_readinto);
115120

0 commit comments

Comments
 (0)