Skip to content

Commit

Permalink
Simply try to connect again if the connection attempt fails
Browse files Browse the repository at this point in the history
This was an issue when connecting to the Wiimote

Fixes #169 and #319
  • Loading branch information
Lauszus committed Nov 10, 2017
1 parent e7b6c1c commit 317120c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
17 changes: 14 additions & 3 deletions BTD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ void BTD::HCI_event_task() {
Notify(PSTR("\r\nHCI Command Failed: "), 0x80);
D_PrintHex<uint8_t > (hcibuf[2], 0x80);
#endif
hci_set_flag(HCI_FLAG_CMD_FAILED);
}
break;

Expand Down Expand Up @@ -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)) {
Expand All @@ -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:
Expand Down Expand Up @@ -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);
}

Expand Down
36 changes: 19 additions & 17 deletions BTD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 317120c

Please sign in to comment.