Simply try to connect again if the connection attempt fails

This was an issue when connecting to the Wiimote

Fixes #169 and #319
This commit is contained in:
Kristian Sloth Lauszus 2017-11-10 09:40:27 +01:00
parent e7b6c1ca22
commit 317120c749
2 changed files with 33 additions and 20 deletions

17
BTD.cpp
View file

@ -423,6 +423,7 @@ void BTD::HCI_event_task() {
Notify(PSTR("\r\nHCI Command Failed: "), 0x80); Notify(PSTR("\r\nHCI Command Failed: "), 0x80);
D_PrintHex<uint8_t > (hcibuf[2], 0x80); D_PrintHex<uint8_t > (hcibuf[2], 0x80);
#endif #endif
hci_set_flag(HCI_FLAG_CMD_FAILED);
} }
break; break;
@ -774,6 +775,15 @@ void BTD::HCI_task() {
} }
break; 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: case HCI_CONNECTED_DEVICE_STATE:
if(hci_check_flag(HCI_FLAG_CONNECT_EVENT)) { if(hci_check_flag(HCI_FLAG_CONNECT_EVENT)) {
if(hci_check_flag(HCI_FLAG_CONNECT_COMPLETE)) { if(hci_check_flag(HCI_FLAG_CONNECT_COMPLETE)) {
@ -789,9 +799,10 @@ void BTD::HCI_task() {
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nTrying to connect one more time..."), 0x80); Notify(PSTR("\r\nTrying to connect one more time..."), 0x80);
#endif #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; break;
case HCI_SCANNING_STATE: case HCI_SCANNING_STATE:
@ -946,7 +957,7 @@ void BTD::ACL_event_task() {
/************************************************************/ /************************************************************/
void BTD::HCI_Command(uint8_t* data, uint16_t nbytes) { 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); pUsb->ctrlReq(bAddress, epInfo[ BTD_CONTROL_PIPE ].epAddr, bmREQ_HCI_OUT, 0x00, 0x00, 0x00, 0x00, nbytes, nbytes, data, NULL);
} }

36
BTD.h
View file

@ -45,28 +45,30 @@
#define HCI_SET_NAME_STATE 5 #define HCI_SET_NAME_STATE 5
#define HCI_CHECK_DEVICE_SERVICE 6 #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_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_SCANNING_STATE 11
#define HCI_CONNECT_IN_STATE 11 #define HCI_CONNECT_IN_STATE 12
#define HCI_REMOTE_NAME_STATE 12 #define HCI_REMOTE_NAME_STATE 13
#define HCI_CONNECTED_STATE 13 #define HCI_CONNECTED_STATE 14
#define HCI_DISABLE_SCAN_STATE 14 #define HCI_DISABLE_SCAN_STATE 15
#define HCI_DONE_STATE 15 #define HCI_DONE_STATE 16
#define HCI_DISCONNECT_STATE 16 #define HCI_DISCONNECT_STATE 17
/* HCI event flags*/ /* HCI event flags*/
#define HCI_FLAG_CMD_COMPLETE (1UL << 0) #define HCI_FLAG_CMD_COMPLETE (1UL << 0)
#define HCI_FLAG_CONNECT_COMPLETE (1UL << 1) #define HCI_FLAG_CMD_FAILED (1UL << 1)
#define HCI_FLAG_DISCONNECT_COMPLETE (1UL << 2) #define HCI_FLAG_CONNECT_COMPLETE (1UL << 2)
#define HCI_FLAG_REMOTE_NAME_COMPLETE (1UL << 3) #define HCI_FLAG_DISCONNECT_COMPLETE (1UL << 3)
#define HCI_FLAG_INCOMING_REQUEST (1UL << 4) #define HCI_FLAG_REMOTE_NAME_COMPLETE (1UL << 4)
#define HCI_FLAG_READ_BDADDR (1UL << 5) #define HCI_FLAG_INCOMING_REQUEST (1UL << 5)
#define HCI_FLAG_READ_VERSION (1UL << 6) #define HCI_FLAG_READ_BDADDR (1UL << 6)
#define HCI_FLAG_DEVICE_FOUND (1UL << 7) #define HCI_FLAG_READ_VERSION (1UL << 7)
#define HCI_FLAG_CONNECT_EVENT (1UL << 8) #define HCI_FLAG_DEVICE_FOUND (1UL << 8)
#define HCI_FLAG_CONNECT_EVENT (1UL << 9)
/* Macros for HCI event flag tests */ /* Macros for HCI event flag tests */
#define hci_check_flag(flag) (hci_event_flag & (flag)) #define hci_check_flag(flag) (hci_event_flag & (flag))