improve HCI packet error handling

This commit is contained in:
Dan Halbert 2020-08-21 10:17:59 -04:00
parent 490380a504
commit 097f93a630
2 changed files with 16 additions and 11 deletions

View File

@ -331,27 +331,27 @@ hci_result_t hci_poll_for_incoming_pkt(void) {
switch (rx_buffer[0]) { switch (rx_buffer[0]) {
case H4_ACL: 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 = const size_t total_len =
sizeof(h4_hci_acl_pkt_t) + ((h4_hci_acl_pkt_t *) rx_buffer)->data_len; sizeof(h4_hci_acl_pkt_t) + ((h4_hci_acl_pkt_t *) rx_buffer)->data_len;
if (rx_idx == total_len) { if (rx_idx == total_len) {
packet_is_complete = true; packet_is_complete = true;
} }
if (rx_idx > total_len) { if (rx_idx > total_len) {
mp_printf(&mp_plat_print, "acl: rx_idx > total_len\n"); return HCI_PACKET_SIZE_ERROR;
} }
} }
break; break;
case H4_EVT: 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 = const size_t total_len =
sizeof(h4_hci_evt_pkt_t) + ((h4_hci_evt_pkt_t *) rx_buffer)->param_len; sizeof(h4_hci_evt_pkt_t) + ((h4_hci_evt_pkt_t *) rx_buffer)->param_len;
if (rx_idx == total_len) { if (rx_idx == total_len) {
packet_is_complete = true; packet_is_complete = true;
} }
if (rx_idx > total_len) { if (rx_idx > total_len) {
mp_printf(&mp_plat_print, "evt: rx_idx > total_len\n"); return HCI_PACKET_SIZE_ERROR;
} }
} }
break; break;
@ -786,6 +786,10 @@ void hci_check_error(hci_result_t result) {
mp_raise_bleio_BluetoothError(translate("Error writing to HCI adapter")); mp_raise_bleio_BluetoothError(translate("Error writing to HCI adapter"));
return; return;
case HCI_PACKET_SIZE_ERROR:
mp_raise_RuntimeError(translate("HCI packet size mismatch"));
return;
case HCI_ATT_ERROR: case HCI_ATT_ERROR:
mp_raise_RuntimeError(translate("Error in ATT protocol code")); mp_raise_RuntimeError(translate("Error in ATT protocol code"));
return; return;

View File

@ -29,7 +29,7 @@
typedef struct _bleio_adapter_obj_t bleio_adapter_obj_t; typedef struct _bleio_adapter_obj_t bleio_adapter_obj_t;
// An hci_result_t is one of the HCI_x values below, // 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; typedef int hci_result_t;
#define HCI_OK (0) #define HCI_OK (0)
#define HCI_RESPONSE_TIMEOUT (-1) #define HCI_RESPONSE_TIMEOUT (-1)
@ -37,6 +37,7 @@ typedef int hci_result_t;
#define HCI_READ_ERROR (-3) #define HCI_READ_ERROR (-3)
#define HCI_WRITE_ERROR (-4) #define HCI_WRITE_ERROR (-4)
#define HCI_ATT_ERROR (-5) #define HCI_ATT_ERROR (-5)
#define HCI_PACKET_SIZE_ERROR (-6)
extern void bleio_hci_reset(void); extern void bleio_hci_reset(void);