summaryrefslogtreecommitdiff
path: root/devices
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2020-08-21 10:17:59 -0400
committerDan Halbert <halbert@halwitz.org>2020-08-21 10:17:59 -0400
commit097f93a630199ae81732981ce3778632e7744f6c (patch)
tree8c4a561b9d4a76f242aaef72f2791ccb0b75a11a /devices
parent490380a504b539adbb9e2b9902c404733ff0a03f (diff)
improve HCI packet error handling
Diffstat (limited to 'devices')
-rw-r--r--devices/ble_hci/common-hal/_bleio/hci.c12
-rw-r--r--devices/ble_hci/common-hal/_bleio/hci.h15
2 files changed, 16 insertions, 11 deletions
diff --git a/devices/ble_hci/common-hal/_bleio/hci.c b/devices/ble_hci/common-hal/_bleio/hci.c
index ed9320330..e261a9847 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 3d082c49c..c9fd2393a 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);