Skip to content

Commit 097f93a

Browse files
committed
improve HCI packet error handling
1 parent 490380a commit 097f93a

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

devices/ble_hci/common-hal/_bleio/hci.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,27 +331,27 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
331331

332332
switch (rx_buffer[0]) {
333333
case H4_ACL:
334-
if (rx_idx > sizeof(h4_hci_acl_pkt_t)) {
334+
if (rx_idx >= sizeof(h4_hci_acl_pkt_t)) {
335335
const size_t total_len =
336336
sizeof(h4_hci_acl_pkt_t) + ((h4_hci_acl_pkt_t *) rx_buffer)->data_len;
337337
if (rx_idx == total_len) {
338338
packet_is_complete = true;
339339
}
340340
if (rx_idx > total_len) {
341-
mp_printf(&mp_plat_print, "acl: rx_idx > total_len\n");
341+
return HCI_PACKET_SIZE_ERROR;
342342
}
343343
}
344344
break;
345345

346346
case H4_EVT:
347-
if (rx_idx > sizeof(h4_hci_evt_pkt_t)) {
347+
if (rx_idx >= sizeof(h4_hci_evt_pkt_t)) {
348348
const size_t total_len =
349349
sizeof(h4_hci_evt_pkt_t) + ((h4_hci_evt_pkt_t *) rx_buffer)->param_len;
350350
if (rx_idx == total_len) {
351351
packet_is_complete = true;
352352
}
353353
if (rx_idx > total_len) {
354-
mp_printf(&mp_plat_print, "evt: rx_idx > total_len\n");
354+
return HCI_PACKET_SIZE_ERROR;
355355
}
356356
}
357357
break;
@@ -786,6 +786,10 @@ void hci_check_error(hci_result_t result) {
786786
mp_raise_bleio_BluetoothError(translate("Error writing to HCI adapter"));
787787
return;
788788

789+
case HCI_PACKET_SIZE_ERROR:
790+
mp_raise_RuntimeError(translate("HCI packet size mismatch"));
791+
return;
792+
789793
case HCI_ATT_ERROR:
790794
mp_raise_RuntimeError(translate("Error in ATT protocol code"));
791795
return;

devices/ble_hci/common-hal/_bleio/hci.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
typedef struct _bleio_adapter_obj_t bleio_adapter_obj_t;
3030

3131
// An hci_result_t is one of the HCI_x values below,
32-
// or is it > 0 and is an HCI command status value (see hci_include/hci_err.h)
32+
// or it is > 0 and is an HCI command status value (see hci_include/hci_err.h)
3333
typedef int hci_result_t;
34-
#define HCI_OK (0)
35-
#define HCI_RESPONSE_TIMEOUT (-1)
36-
#define HCI_WRITE_TIMEOUT (-2)
37-
#define HCI_READ_ERROR (-3)
38-
#define HCI_WRITE_ERROR (-4)
39-
#define HCI_ATT_ERROR (-5)
34+
#define HCI_OK (0)
35+
#define HCI_RESPONSE_TIMEOUT (-1)
36+
#define HCI_WRITE_TIMEOUT (-2)
37+
#define HCI_READ_ERROR (-3)
38+
#define HCI_WRITE_ERROR (-4)
39+
#define HCI_ATT_ERROR (-5)
40+
#define HCI_PACKET_SIZE_ERROR (-6)
4041

4142
extern void bleio_hci_reset(void);
4243

0 commit comments

Comments
 (0)