@@ -107,12 +107,18 @@ STATIC uint32_t queue_next_write(bleio_packet_buffer_obj_t *self) {
107107}
108108
109109STATIC 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
143149STATIC 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
336344bool common_hal_bleio_packet_buffer_deinited (bleio_packet_buffer_obj_t * self ) {
0 commit comments