diff --git a/devices/ble_hci/common-hal/_bleio/hci.c b/devices/ble_hci/common-hal/_bleio/hci.c index ed9320330e..e261a98475 100644 --- a/devices/ble_hci/common-hal/_bleio/hci.c +++ b/devices/ble_hci/common-hal/_bleio/hci.c @@ -331,27 +331,27 @@ hci_result_t hci_poll_for_incoming_pkt(void) { switch (rx_buffer[0]) { case H4_ACL: - if (rx_idx > sizeof(h4_hci_acl_pkt_t)) { + if (rx_idx >= sizeof(h4_hci_acl_pkt_t)) { const size_t total_len = sizeof(h4_hci_acl_pkt_t) + ((h4_hci_acl_pkt_t *) rx_buffer)->data_len; if (rx_idx == total_len) { packet_is_complete = true; } if (rx_idx > total_len) { - mp_printf(&mp_plat_print, "acl: rx_idx > total_len\n"); + return HCI_PACKET_SIZE_ERROR; } } break; case H4_EVT: - if (rx_idx > sizeof(h4_hci_evt_pkt_t)) { + if (rx_idx >= sizeof(h4_hci_evt_pkt_t)) { const size_t total_len = sizeof(h4_hci_evt_pkt_t) + ((h4_hci_evt_pkt_t *) rx_buffer)->param_len; if (rx_idx == total_len) { packet_is_complete = true; } if (rx_idx > total_len) { - mp_printf(&mp_plat_print, "evt: rx_idx > total_len\n"); + return HCI_PACKET_SIZE_ERROR; } } break; @@ -786,6 +786,10 @@ void hci_check_error(hci_result_t result) { mp_raise_bleio_BluetoothError(translate("Error writing to HCI adapter")); return; + case HCI_PACKET_SIZE_ERROR: + mp_raise_RuntimeError(translate("HCI packet size mismatch")); + return; + case HCI_ATT_ERROR: mp_raise_RuntimeError(translate("Error in ATT protocol code")); return; diff --git a/devices/ble_hci/common-hal/_bleio/hci.h b/devices/ble_hci/common-hal/_bleio/hci.h index 3d082c49cb..c9fd2393af 100644 --- a/devices/ble_hci/common-hal/_bleio/hci.h +++ b/devices/ble_hci/common-hal/_bleio/hci.h @@ -29,14 +29,15 @@ typedef struct _bleio_adapter_obj_t bleio_adapter_obj_t; // An hci_result_t is one of the HCI_x values below, -// or is it > 0 and is an HCI command status value (see hci_include/hci_err.h) +// or it is > 0 and is an HCI command status value (see hci_include/hci_err.h) typedef int hci_result_t; -#define HCI_OK (0) -#define HCI_RESPONSE_TIMEOUT (-1) -#define HCI_WRITE_TIMEOUT (-2) -#define HCI_READ_ERROR (-3) -#define HCI_WRITE_ERROR (-4) -#define HCI_ATT_ERROR (-5) +#define HCI_OK (0) +#define HCI_RESPONSE_TIMEOUT (-1) +#define HCI_WRITE_TIMEOUT (-2) +#define HCI_READ_ERROR (-3) +#define HCI_WRITE_ERROR (-4) +#define HCI_ATT_ERROR (-5) +#define HCI_PACKET_SIZE_ERROR (-6) extern void bleio_hci_reset(void);