From 317120c749c5eb0ec5766ca9f8f43886322c04c4 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Fri, 10 Nov 2017 09:40:27 +0100 Subject: [PATCH] Simply try to connect again if the connection attempt fails This was an issue when connecting to the Wiimote Fixes #169 and #319 --- BTD.cpp | 17 ++++++++++++++--- BTD.h | 36 +++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/BTD.cpp b/BTD.cpp index 07f033395..1c7f101ad 100644 --- a/BTD.cpp +++ b/BTD.cpp @@ -423,6 +423,7 @@ void BTD::HCI_event_task() { Notify(PSTR("\r\nHCI Command Failed: "), 0x80); D_PrintHex (hcibuf[2], 0x80); #endif + hci_set_flag(HCI_FLAG_CMD_FAILED); } break; @@ -774,6 +775,15 @@ void BTD::HCI_task() { } break; + case HCI_RETRY_CONNECT_STATE: + hci_counter++; + if(hci_counter > 100) { // Wait until we have looped 100 times before trying to re-connect + hci_counter = 0; + hci_connect(); // Try to connect one more time + hci_state = HCI_CONNECTED_DEVICE_STATE; + } + break; + case HCI_CONNECTED_DEVICE_STATE: if(hci_check_flag(HCI_FLAG_CONNECT_EVENT)) { if(hci_check_flag(HCI_FLAG_CONNECT_COMPLETE)) { @@ -789,9 +799,10 @@ void BTD::HCI_task() { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nTrying to connect one more time..."), 0x80); #endif - hci_connect(); // Try to connect one more time + hci_state = HCI_RETRY_CONNECT_STATE; // Try to connect one more time } - } + } else if(hci_check_flag(HCI_FLAG_CMD_FAILED)) + hci_state = HCI_RETRY_CONNECT_STATE; // Try to connect one more time break; case HCI_SCANNING_STATE: @@ -946,7 +957,7 @@ void BTD::ACL_event_task() { /************************************************************/ void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) { - hci_clear_flag(HCI_FLAG_CMD_COMPLETE); + hci_clear_flag(HCI_FLAG_CMD_COMPLETE | HCI_FLAG_CMD_FAILED); pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL); } diff --git a/BTD.h b/BTD.h index 684835ab1..d22404451 100644 --- a/BTD.h +++ b/BTD.h @@ -45,28 +45,30 @@ #define HCI_SET_NAME_STATE 5 #define HCI_CHECK_DEVICE_SERVICE 6 -#define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a device +#define HCI_INQUIRY_STATE 7 // These four states are only used if it should pair and connect to a device #define HCI_CONNECT_DEVICE_STATE 8 -#define HCI_CONNECTED_DEVICE_STATE 9 +#define HCI_RETRY_CONNECT_STATE 9 +#define HCI_CONNECTED_DEVICE_STATE 10 -#define HCI_SCANNING_STATE 10 -#define HCI_CONNECT_IN_STATE 11 -#define HCI_REMOTE_NAME_STATE 12 -#define HCI_CONNECTED_STATE 13 -#define HCI_DISABLE_SCAN_STATE 14 -#define HCI_DONE_STATE 15 -#define HCI_DISCONNECT_STATE 16 +#define HCI_SCANNING_STATE 11 +#define HCI_CONNECT_IN_STATE 12 +#define HCI_REMOTE_NAME_STATE 13 +#define HCI_CONNECTED_STATE 14 +#define HCI_DISABLE_SCAN_STATE 15 +#define HCI_DONE_STATE 16 +#define HCI_DISCONNECT_STATE 17 /* HCI event flags*/ #define HCI_FLAG_CMD_COMPLETE (1UL << 0) -#define HCI_FLAG_CONNECT_COMPLETE (1UL << 1) -#define HCI_FLAG_DISCONNECT_COMPLETE (1UL << 2) -#define HCI_FLAG_REMOTE_NAME_COMPLETE (1UL << 3) -#define HCI_FLAG_INCOMING_REQUEST (1UL << 4) -#define HCI_FLAG_READ_BDADDR (1UL << 5) -#define HCI_FLAG_READ_VERSION (1UL << 6) -#define HCI_FLAG_DEVICE_FOUND (1UL << 7) -#define HCI_FLAG_CONNECT_EVENT (1UL << 8) +#define HCI_FLAG_CMD_FAILED (1UL << 1) +#define HCI_FLAG_CONNECT_COMPLETE (1UL << 2) +#define HCI_FLAG_DISCONNECT_COMPLETE (1UL << 3) +#define HCI_FLAG_REMOTE_NAME_COMPLETE (1UL << 4) +#define HCI_FLAG_INCOMING_REQUEST (1UL << 5) +#define HCI_FLAG_READ_BDADDR (1UL << 6) +#define HCI_FLAG_READ_VERSION (1UL << 7) +#define HCI_FLAG_DEVICE_FOUND (1UL << 8) +#define HCI_FLAG_CONNECT_EVENT (1UL << 9) /* Macros for HCI event flag tests */ #define hci_check_flag(flag) (hci_event_flag & (flag))