Skip to content

Commit f307851

Browse files
committed
further cleanup and bug fixing
2 parents 180f5c6 + 3d62f87 commit f307851

File tree

12 files changed

+118
-58
lines changed

12 files changed

+118
-58
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ size_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *sel
127127
// self->value is set by evt handler.
128128
return common_hal_bleio_gattc_read(self->handle, conn_handle, buf, len);
129129
} else {
130+
// conn_handle is ignored for non-system attributes.
130131
return common_hal_bleio_gatts_read(self->handle, conn_handle, buf, len);
131132
}
132133
}
@@ -152,6 +153,7 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
152153
(self->props & CHAR_PROP_WRITE_NO_RESPONSE));
153154
} else {
154155
// Always write the value locally even if no connections are active.
156+
// conn_handle is ignored for non-system attributes, so we use BLE_CONN_HANDLE_INVALID.
155157
common_hal_bleio_gatts_write(self->handle, BLE_CONN_HANDLE_INVALID, bufinfo);
156158
// Check to see if we need to notify or indicate any active connections.
157159
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ mp_float_t common_hal_bleio_connection_get_connection_interval(bleio_connection_
371371
return 1.25f * self->conn_params.min_conn_interval;
372372
}
373373

374+
// Return the current negotiated MTU length, minus overhead.
375+
mp_int_t common_hal_bleio_connection_get_max_packet_length(bleio_connection_internal_t *self) {
376+
return (self->mtu == 0 ? BLE_GATT_ATT_MTU_DEFAULT : self->mtu) - 3;
377+
}
378+
374379
void common_hal_bleio_connection_set_connection_interval(bleio_connection_internal_t *self, mp_float_t new_interval) {
375380
self->conn_params_updating = true;
376381
uint16_t interval = new_interval / 1.25f;
@@ -752,3 +757,16 @@ mp_obj_t bleio_connection_new_from_internal(bleio_connection_internal_t* interna
752757

753758
return MP_OBJ_FROM_PTR(connection);
754759
}
760+
761+
// Find the connection that uses the given conn_handle. Return NULL if not found.
762+
bleio_connection_internal_t *bleio_conn_handle_to_connection(uint16_t conn_handle) {
763+
bleio_connection_internal_t *connection;
764+
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
765+
connection = &bleio_connections[i];
766+
if (connection->conn_handle == conn_handle) {
767+
return connection;
768+
}
769+
}
770+
771+
return NULL;
772+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,6 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in);
8787

8888
uint16_t bleio_connection_get_conn_handle(bleio_connection_obj_t *self);
8989
mp_obj_t bleio_connection_new_from_internal(bleio_connection_internal_t* connection);
90+
bleio_connection_internal_t *bleio_conn_handle_to_connection(uint16_t conn_handle);
9091

9192
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CONNECTION_H

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

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,18 @@ STATIC uint32_t queue_next_write(bleio_packet_buffer_obj_t *self) {
107107
}
108108

109109
STATIC bool packet_buffer_on_ble_client_evt(ble_evt_t *ble_evt, void *param) {
110-
bleio_packet_buffer_obj_t *self = (bleio_packet_buffer_obj_t *) param;
110+
const uint16_t evt_id = ble_evt->header.evt_id;
111+
// Check if this is a GATTC event so we can make sure the conn_handle is valid.
112+
if (evt_id < BLE_GATTC_EVT_BASE || evt_id > BLE_GATTC_EVT_LAST) {
113+
return false;
114+
}
115+
111116
uint16_t conn_handle = ble_evt->evt.gattc_evt.conn_handle;
117+
bleio_packet_buffer_obj_t *self = (bleio_packet_buffer_obj_t *) param;
112118
if (conn_handle != self->conn_handle) {
113119
return false;
114120
}
115-
switch (ble_evt->header.evt_id) {
121+
switch (evt_id) {
116122
case BLE_GATTC_EVT_HVX: {
117123
// A remote service wrote to this characteristic.
118124
ble_gattc_evt_hvx_t* evt_hvx = &ble_evt->evt.gattc_evt.params.hvx;
@@ -142,9 +148,9 @@ STATIC bool packet_buffer_on_ble_client_evt(ble_evt_t *ble_evt, void *param) {
142148

143149
STATIC bool packet_buffer_on_ble_server_evt(ble_evt_t *ble_evt, void *param) {
144150
bleio_packet_buffer_obj_t *self = (bleio_packet_buffer_obj_t *) param;
145-
uint16_t conn_handle = ble_evt->evt.gatts_evt.conn_handle;
146151
switch (ble_evt->header.evt_id) {
147152
case BLE_GATTS_EVT_WRITE: {
153+
uint16_t conn_handle = ble_evt->evt.gatts_evt.conn_handle;
148154
// A client wrote to this server characteristic.
149155

150156
ble_gatts_evt_write_t *evt_write = &ble_evt->evt.gatts_evt.params.write;
@@ -168,7 +174,7 @@ STATIC bool packet_buffer_on_ble_server_evt(ble_evt_t *ble_evt, void *param) {
168174
break;
169175
}
170176
case BLE_GAP_EVT_DISCONNECTED: {
171-
if (self->conn_handle == conn_handle) {
177+
if (self->conn_handle == ble_evt->evt.gap_evt.conn_handle) {
172178
self->conn_handle = BLE_CONN_HANDLE_INVALID;
173179
}
174180
}
@@ -246,21 +252,20 @@ void common_hal_bleio_packet_buffer_construct(
246252
}
247253
}
248254

249-
int common_hal_bleio_packet_buffer_readinto(bleio_packet_buffer_obj_t *self, uint8_t *data, size_t len) {
255+
mp_int_t common_hal_bleio_packet_buffer_readinto(bleio_packet_buffer_obj_t *self, uint8_t *data, size_t len) {
250256
if (ringbuf_num_filled(&self->ringbuf) < 2) {
251257
return 0;
252258
}
253259

254-
uint16_t packet_length;
255-
int ret;
256-
257260
// Copy received data. Lock out write interrupt handler while copying.
258261
uint8_t is_nested_critical_region;
259262
sd_nvic_critical_region_enter(&is_nested_critical_region);
260263

261-
// Get packet length first.
264+
// Get packet length, which is in first two bytes of packet.
265+
uint16_t packet_length;
262266
ringbuf_get_n(&self->ringbuf, (uint8_t*) &packet_length, sizeof(uint16_t));
263267

268+
mp_int_t ret;
264269
if (packet_length > len) {
265270
// Packet is longer than requested. Return negative of overrun value.
266271
ret = len - packet_length;
@@ -311,26 +316,29 @@ void common_hal_bleio_packet_buffer_write(bleio_packet_buffer_obj_t *self, uint8
311316
}
312317
}
313318

314-
uint16_t common_hal_bleio_packet_buffer_get_packet_size(bleio_packet_buffer_obj_t *self) {
315-
// First, assume default MTU size.
316-
uint16_t mtu = BLE_GATT_ATT_MTU_DEFAULT;
317-
318-
// If there's a connection, get its actual MTU.
319-
if (self->conn_handle != BLE_CONN_HANDLE_INVALID) {
320-
bleio_connection_internal_t *connection;
321-
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
322-
connection = &bleio_connections[i];
323-
if (connection->conn_handle == self->conn_handle) {
324-
if (connection->mtu != 0) {
325-
mtu = connection->mtu;
326-
}
327-
break;
328-
}
319+
mp_int_t common_hal_bleio_packet_buffer_get_packet_size(bleio_packet_buffer_obj_t *self) {
320+
// If this PacketBuffer is being used for NOTIFY or INDICATE,
321+
// the maximum size is what can be sent in one
322+
// BLE packet. But we must be connected to know that value.
323+
//
324+
// Otherwise it can be as long as the characteristic
325+
// will permit, whether or not we're connected.
326+
327+
if (self->characteristic != NULL &&
328+
self->characteristic->service != NULL &&
329+
(common_hal_bleio_characteristic_get_properties(self->characteristic) &
330+
(CHAR_PROP_INDICATE | CHAR_PROP_NOTIFY)) &&
331+
self->conn_handle != BLE_CONN_HANDLE_INVALID) {
332+
bleio_connection_internal_t *connection = bleio_conn_handle_to_connection(self->conn_handle);
333+
if (connection) {
334+
return MIN(common_hal_bleio_connection_get_max_packet_length(connection),
335+
self->characteristic->max_length);
329336
}
337+
// There's no current connection, so we don't know the MTU, and
338+
// we can't tell what the largest incoming packet length would be.
339+
return -1;
330340
}
331-
332-
// 3 is bytes of ATT overhead.
333-
return MIN(mtu - 3, self->characteristic->max_length);
341+
return self->characteristic->max_length;
334342
}
335343

336344
bool common_hal_bleio_packet_buffer_deinited(bleio_packet_buffer_obj_t *self) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ typedef struct {
4141
// the other is waiting to be queued and can be extended.
4242
uint8_t* outgoing[2];
4343
uint16_t pending_size;
44+
// We remember the conn_handle so we can do a NOTIFY/INDICATE to a client.
45+
// We can find out the conn_handle on a Characteristic write or a CCCD write (but not a read).
4446
uint16_t conn_handle;
4547
uint8_t pending_index;
4648
uint8_t write_type;

ports/nrf/common-hal/_bleio/Service.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ typedef struct bleio_service_obj {
3939
bool is_remote;
4040
bool is_secondary;
4141
bleio_uuid_obj_t *uuid;
42+
// The connection object is set only when this is a remote service.
43+
// A local service doesn't know the connection.
4244
mp_obj_t connection;
4345
mp_obj_list_t *characteristic_list;
4446
// Range of attribute handles of this remote service.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ void common_hal_bleio_check_connected(uint16_t conn_handle) {
118118

119119
// GATTS read of a Characteristic or Descriptor.
120120
size_t common_hal_bleio_gatts_read(uint16_t handle, uint16_t conn_handle, uint8_t* buf, size_t len) {
121-
// conn_handle might be BLE_CONN_HANDLE_INVALID if we're not connected, but that's OK, because
122-
// we can still read and write the local value.
121+
// conn_handle is ignored unless this is a system attribute.
122+
// If we're not connected, but that's OK, because we can still read and write the local value.
123123

124124
ble_gatts_value_t gatts_value = {
125125
.p_value = buf,
@@ -132,8 +132,8 @@ size_t common_hal_bleio_gatts_read(uint16_t handle, uint16_t conn_handle, uint8_
132132
}
133133

134134
void common_hal_bleio_gatts_write(uint16_t handle, uint16_t conn_handle, mp_buffer_info_t *bufinfo) {
135-
// conn_handle might be BLE_CONN_HANDLE_INVALID if we're not connected, but that's OK, because
136-
// we can still read and write the local value.
135+
// conn_handle is ignored unless this is a system attribute.
136+
// If we're not connected, but that's OK, because we can still read and write the local value.
137137

138138
ble_gatts_value_t gatts_value = {
139139
.p_value = bufinfo->buf,

ports/nrf/mpconfigport.mk

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,8 @@ endif
5151
# frequencyio not yet implemented
5252
CIRCUITPY_FREQUENCYIO = 0
5353

54-
ifndef CIRCUITPY_PROTOMATTER
55-
CIRCUITPY_RGBMATRIX = 1
56-
CIRCUITPY_FRAMEBUFFERIO = 1
57-
endif
58-
59-
ifndef CIRCUITPY_ULAB
60-
CIRCUITPY_ULAB = 1
61-
endif
54+
CIRCUITPY_RGBMATRIX ?= 1
55+
CIRCUITPY_FRAMEBUFFERIO ?= 1
6256

6357
# nRF52840-specific
6458

shared-bindings/_bleio/Connection.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,25 @@ STATIC mp_obj_t bleio_connection_get_connection_interval(mp_obj_t self_in) {
214214
}
215215
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_connection_get_connection_interval_obj, bleio_connection_get_connection_interval);
216216

217+
//| .. attribute:: max_packet_length
218+
//|
219+
//| The maximum number of data bytes that can be sent in a single transmission,
220+
//| not including overhead bytes.
221+
//|
222+
//| This is the maximum number of bytes that can be sent in a notification,
223+
//| which must be sent in a single packet.
224+
//| But for a regular characteristic read or write, may be sent in multiple packets,
225+
//| so this limit does not apply.
226+
//|
227+
STATIC mp_obj_t bleio_connection_get_max_packet_length(mp_obj_t self_in) {
228+
bleio_connection_obj_t *self = MP_OBJ_TO_PTR(self_in);
229+
230+
bleio_connection_ensure_connected(self);
231+
return mp_obj_new_int(common_hal_bleio_connection_get_max_packet_length(self->connection));
232+
}
233+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_connection_get_max_packet_length_obj, bleio_connection_get_max_packet_length);
234+
235+
217236
STATIC mp_obj_t bleio_connection_set_connection_interval(mp_obj_t self_in, mp_obj_t interval_in) {
218237
bleio_connection_obj_t *self = MP_OBJ_TO_PTR(self_in);
219238

@@ -233,6 +252,13 @@ const mp_obj_property_t bleio_connection_connection_interval_obj = {
233252
(mp_obj_t)&mp_const_none_obj },
234253
};
235254

255+
const mp_obj_property_t bleio_connection_max_packet_length_obj = {
256+
.base.type = &mp_type_property,
257+
.proxy = { (mp_obj_t)&bleio_connection_get_max_packet_length_obj,
258+
(mp_obj_t)&mp_const_none_obj,
259+
(mp_obj_t)&mp_const_none_obj },
260+
};
261+
236262
STATIC const mp_rom_map_elem_t bleio_connection_locals_dict_table[] = {
237263
// Methods
238264
{ MP_ROM_QSTR(MP_QSTR_pair), MP_ROM_PTR(&bleio_connection_pair_obj) },
@@ -243,7 +269,7 @@ STATIC const mp_rom_map_elem_t bleio_connection_locals_dict_table[] = {
243269
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_connection_connected_obj) },
244270
{ MP_ROM_QSTR(MP_QSTR_paired), MP_ROM_PTR(&bleio_connection_paired_obj) },
245271
{ MP_ROM_QSTR(MP_QSTR_connection_interval), MP_ROM_PTR(&bleio_connection_connection_interval_obj) },
246-
272+
{ MP_ROM_QSTR(MP_QSTR_max_packet_length), MP_ROM_PTR(&bleio_connection_max_packet_length_obj) },
247273
};
248274

249275
STATIC MP_DEFINE_CONST_DICT(bleio_connection_locals_dict, bleio_connection_locals_dict_table);

shared-bindings/_bleio/Connection.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434

3535
extern const mp_obj_type_t bleio_connection_type;
3636

37-
extern void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bond);
38-
extern void common_hal_bleio_connection_disconnect(bleio_connection_internal_t *self);
39-
extern bool common_hal_bleio_connection_get_connected(bleio_connection_obj_t *self);
40-
extern bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self);
41-
extern mp_obj_tuple_t *common_hal_bleio_connection_discover_remote_services(bleio_connection_obj_t *self, mp_obj_t service_uuids_whitelist);
37+
void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bond);
38+
void common_hal_bleio_connection_disconnect(bleio_connection_internal_t *self);
39+
bool common_hal_bleio_connection_get_connected(bleio_connection_obj_t *self);
40+
mp_int_t common_hal_bleio_connection_get_max_packet_length(bleio_connection_internal_t *self);
41+
bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self);
42+
mp_obj_tuple_t *common_hal_bleio_connection_discover_remote_services(bleio_connection_obj_t *self, mp_obj_t service_uuids_whitelist);
4243

4344
mp_float_t common_hal_bleio_connection_get_connection_interval(bleio_connection_internal_t *self);
4445
void common_hal_bleio_connection_set_connection_interval(bleio_connection_internal_t *self, mp_float_t new_interval);

0 commit comments

Comments
 (0)