From 084c3f54788309ff1650157406c795bf88349bc2 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Thu, 12 Dec 2013 01:01:50 +0100 Subject: [PATCH 01/11] Removed non-generic case --- BTHID.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/BTHID.cpp b/BTHID.cpp index f410bcc2..71e35743 100644 --- a/BTHID.cpp +++ b/BTHID.cpp @@ -204,13 +204,6 @@ void BTHID::ACLData(uint8_t* l2capinbuf) { pRptParser[MOUSE_PARSER_ID]->Parse(reinterpret_cast (this), 0, (uint8_t) length, &l2capinbuf[10]); // Use reinterpret_cast again to extract the instance } break; - - case 0x03: -#ifdef DEBUG_USB_HOST - Notify(PSTR("\r\nChange mode event: "), 0x80); - D_PrintHex (l2capinbuf[11], 0x80); -#endif - break; #ifdef DEBUG_USB_HOST default: Notify(PSTR("\r\nUnknown Report type: "), 0x80); From a7f6b2e4dc9626ee45ea9555cbeb47a5e3d1783f Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Fri, 13 Dec 2013 10:35:27 +0100 Subject: [PATCH 02/11] Removed unneeded case --- BTD.h | 9 +++------ SPP.cpp | 28 +++------------------------- 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/BTD.h b/BTD.h index becc3da3..cfbc2023 100755 --- a/BTD.h +++ b/BTD.h @@ -30,7 +30,7 @@ #define IOGEAR_GBU521_PID 0x21E8 /* Bluetooth dongle data taken from descriptors */ -#define BULK_MAXPKTSIZE 64 // max size for ACL data +#define BULK_MAXPKTSIZE 64 // Max size for ACL data // Used in control endpoint header for HCI Commands #define bmREQ_HCI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE @@ -117,17 +117,16 @@ /* Used for SDP channel */ #define L2CAP_SDP_WAIT 10 -#define L2CAP_SDP_REQUEST 11 #define L2CAP_SDP_SUCCESS 12 #define L2CAP_SDP_DONE 13 -#define L2CAP_DISCONNECT_RESPONSE 14 /* Used for RFCOMM channel */ #define L2CAP_RFCOMM_WAIT 15 -#define L2CAP_RFCOMM_REQUEST 16 #define L2CAP_RFCOMM_SUCCESS 17 #define L2CAP_RFCOMM_DONE 18 +#define L2CAP_DISCONNECT_RESPONSE 14 // Used for both SDP and RFCOMM channel + /* Bluetooth states used by some drivers */ #define TURN_ON_LED 19 #define PS3_ENABLE_SIXAXIS 20 @@ -149,13 +148,11 @@ /* L2CAP event flags for SDP channel */ #define L2CAP_FLAG_CONNECTION_SDP_REQUEST 0x00000100 -#define L2CAP_FLAG_CONFIG_SDP_REQUEST 0x00000200 #define L2CAP_FLAG_CONFIG_SDP_SUCCESS 0x00000400 #define L2CAP_FLAG_DISCONNECT_SDP_REQUEST 0x00000800 /* L2CAP event flags for RFCOMM channel */ #define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST 0x00001000 -#define L2CAP_FLAG_CONFIG_RFCOMM_REQUEST 0x00002000 #define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS 0x00004000 #define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST 0x00008000 diff --git a/SPP.cpp b/SPP.cpp index 7a13ca1d..f21b6770 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -151,12 +151,10 @@ void SPP::ACLData(uint8_t* l2capinbuf) { } else if (l2capinbuf[8] == L2CAP_CMD_CONFIG_REQUEST) { if (l2capinbuf[12] == sdp_dcid[0] && l2capinbuf[13] == sdp_dcid[1]) { //Notify(PSTR("\r\nSDP Configuration Request"), 0x80); - identifier = l2capinbuf[9]; - l2cap_set_flag(L2CAP_FLAG_CONFIG_SDP_REQUEST); + pBtd->l2cap_config_response(hci_handle, l2capinbuf[9], sdp_scid); } else if (l2capinbuf[12] == rfcomm_dcid[0] && l2capinbuf[13] == rfcomm_dcid[1]) { //Notify(PSTR("\r\nRFCOMM Configuration Request"), 0x80); - identifier = l2capinbuf[9]; - l2cap_set_flag(L2CAP_FLAG_CONFIG_RFCOMM_REQUEST); + pBtd->l2cap_config_response(hci_handle, l2capinbuf[9], rfcomm_scid); } } else if (l2capinbuf[8] == L2CAP_CMD_DISCONNECT_REQUEST) { if (l2capinbuf[12] == sdp_dcid[0] && l2capinbuf[13] == sdp_dcid[1]) { @@ -403,7 +401,7 @@ void SPP::ACLData(uint8_t* l2capinbuf) { connected = true; // The RFCOMM channel is now established sppIndex = 0; } -#ifdef DEBUG_USB_HOST +#ifdef EXTRADEBUG else if (rfcommChannelType != RFCOMM_DISC) { Notify(PSTR("\r\nUnsupported RFCOMM Data - ChannelType: "), 0x80); D_PrintHex (rfcommChannelType, 0x80); @@ -453,16 +451,6 @@ void SPP::SDP_task() { identifier++; delay(1); pBtd->l2cap_config_request(hci_handle, identifier, sdp_scid); - l2cap_sdp_state = L2CAP_SDP_REQUEST; - } - break; - case L2CAP_SDP_REQUEST: - if (l2cap_check_flag(L2CAP_FLAG_CONFIG_SDP_REQUEST)) { - l2cap_clear_flag(L2CAP_FLAG_CONFIG_SDP_REQUEST); // Clear flag -#ifdef DEBUG_USB_HOST - Notify(PSTR("\r\nSDP Configuration Request"), 0x80); -#endif - pBtd->l2cap_config_response(hci_handle, identifier, sdp_scid); l2cap_sdp_state = L2CAP_SDP_SUCCESS; } break; @@ -520,16 +508,6 @@ void SPP::RFCOMM_task() { identifier++; delay(1); pBtd->l2cap_config_request(hci_handle, identifier, rfcomm_scid); - l2cap_rfcomm_state = L2CAP_RFCOMM_REQUEST; - } - break; - case L2CAP_RFCOMM_REQUEST: - if (l2cap_check_flag(L2CAP_FLAG_CONFIG_RFCOMM_REQUEST)) { - l2cap_clear_flag(L2CAP_FLAG_CONFIG_RFCOMM_REQUEST); // Clear flag -#ifdef DEBUG_USB_HOST - Notify(PSTR("\r\nRFCOMM Configuration Request"), 0x80); -#endif - pBtd->l2cap_config_response(hci_handle, identifier, rfcomm_scid); l2cap_rfcomm_state = L2CAP_RFCOMM_SUCCESS; } break; From 413e65a44fb4e742c82665fa74d6838084c273c3 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Fri, 13 Dec 2013 10:36:15 +0100 Subject: [PATCH 03/11] Reorder values --- BTD.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/BTD.h b/BTD.h index cfbc2023..a6b7b479 100755 --- a/BTD.h +++ b/BTD.h @@ -117,22 +117,22 @@ /* Used for SDP channel */ #define L2CAP_SDP_WAIT 10 -#define L2CAP_SDP_SUCCESS 12 -#define L2CAP_SDP_DONE 13 +#define L2CAP_SDP_SUCCESS 11 +#define L2CAP_SDP_DONE 12 /* Used for RFCOMM channel */ -#define L2CAP_RFCOMM_WAIT 15 -#define L2CAP_RFCOMM_SUCCESS 17 -#define L2CAP_RFCOMM_DONE 18 +#define L2CAP_RFCOMM_WAIT 13 +#define L2CAP_RFCOMM_SUCCESS 14 +#define L2CAP_RFCOMM_DONE 15 -#define L2CAP_DISCONNECT_RESPONSE 14 // Used for both SDP and RFCOMM channel +#define L2CAP_DISCONNECT_RESPONSE 16 // Used for both SDP and RFCOMM channel /* Bluetooth states used by some drivers */ -#define TURN_ON_LED 19 -#define PS3_ENABLE_SIXAXIS 20 -#define WII_CHECK_MOTION_PLUS_STATE 21 -#define WII_CHECK_EXTENSION_STATE 22 -#define WII_INIT_MOTION_PLUS_STATE 23 +#define TURN_ON_LED 17 +#define PS3_ENABLE_SIXAXIS 18 +#define WII_CHECK_MOTION_PLUS_STATE 19 +#define WII_CHECK_EXTENSION_STATE 20 +#define WII_INIT_MOTION_PLUS_STATE 21 /* L2CAP event flags for HID Control channel */ #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x00000001 @@ -148,15 +148,15 @@ /* L2CAP event flags for SDP channel */ #define L2CAP_FLAG_CONNECTION_SDP_REQUEST 0x00000100 -#define L2CAP_FLAG_CONFIG_SDP_SUCCESS 0x00000400 -#define L2CAP_FLAG_DISCONNECT_SDP_REQUEST 0x00000800 +#define L2CAP_FLAG_CONFIG_SDP_SUCCESS 0x00000200 +#define L2CAP_FLAG_DISCONNECT_SDP_REQUEST 0x00000400 /* L2CAP event flags for RFCOMM channel */ -#define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST 0x00001000 -#define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS 0x00004000 -#define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST 0x00008000 +#define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST 0x00000800 +#define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS 0x00001000 +#define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST 0x00002000 -#define L2CAP_FLAG_DISCONNECT_RESPONSE 0x00010000 +#define L2CAP_FLAG_DISCONNECT_RESPONSE 0x00004000 /* Macros for L2CAP event flag tests */ #define l2cap_check_flag(flag) (l2cap_event_flag & flag) From 1ef78c3674f5ad6e4626502d3402b4c8eebaa6bd Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Fri, 13 Dec 2013 10:38:41 +0100 Subject: [PATCH 04/11] Do not hardcode identifier when disconnecting --- BTHID.cpp | 2 +- PS3BT.cpp | 6 +++--- SPP.cpp | 6 +++--- Wii.cpp | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/BTHID.cpp b/BTHID.cpp index 71e35743..5f4c2e57 100644 --- a/BTHID.cpp +++ b/BTHID.cpp @@ -53,7 +53,7 @@ void BTHID::Reset() { void BTHID::disconnect() { // Use this void to disconnect any of the controllers // First the HID interrupt channel has to be disconnected, then the HID control channel and finally the HCI connection - pBtd->l2cap_disconnection_request(hci_handle, 0x0A, interrupt_scid, interrupt_dcid); + pBtd->l2cap_disconnection_request(hci_handle, ++identifier, interrupt_scid, interrupt_dcid); Reset(); l2cap_state = L2CAP_INTERRUPT_DISCONNECT; } diff --git a/PS3BT.cpp b/PS3BT.cpp index 6edd4ca7..16beea60 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -218,14 +218,14 @@ void PS3BT::Reset() { l2cap_event_flag = 0; // Reset flags l2cap_state = L2CAP_WAIT; - // Needed for PS3 Dualshock Controller commands to work via bluetooth + // Needed for PS3 Dualshock Controller commands to work via Bluetooth for (uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++) HIDBuffer[i + 2] = pgm_read_byte(&PS3_REPORT_BUFFER[i]); // First two bytes reserved for report type and ID } void PS3BT::disconnect() { // Use this void to disconnect any of the controllers - //First the HID interrupt channel has to be disconencted, then the HID control channel and finally the HCI connection - pBtd->l2cap_disconnection_request(hci_handle, 0x0A, interrupt_scid, interrupt_dcid); + // First the HID interrupt channel has to be disconnected, then the HID control channel and finally the HCI connection + pBtd->l2cap_disconnection_request(hci_handle, ++identifier, interrupt_scid, interrupt_dcid); Reset(); l2cap_state = L2CAP_INTERRUPT_DISCONNECT; } diff --git a/SPP.cpp b/SPP.cpp index f21b6770..b36b2e4f 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -73,13 +73,13 @@ void SPP::Reset() { void SPP::disconnect() { connected = false; - // First the two L2CAP channels has to be disconencted and then the HCI connection + // First the two L2CAP channels has to be disconnected and then the HCI connection if (RFCOMMConnected) - pBtd->l2cap_disconnection_request(hci_handle, 0x0A, rfcomm_scid, rfcomm_dcid); + pBtd->l2cap_disconnection_request(hci_handle, ++identifier, rfcomm_scid, rfcomm_dcid); if (RFCOMMConnected && SDPConnected) delay(1); // Add delay between commands if (SDPConnected) - pBtd->l2cap_disconnection_request(hci_handle, 0x0B, sdp_scid, sdp_dcid); + pBtd->l2cap_disconnection_request(hci_handle, ++identifier, sdp_scid, sdp_dcid); l2cap_sdp_state = L2CAP_DISCONNECT_RESPONSE; } diff --git a/Wii.cpp b/Wii.cpp index 02cddecf..56cd70e5 100755 --- a/Wii.cpp +++ b/Wii.cpp @@ -126,7 +126,7 @@ void WII::disconnect() { // Use this void to disconnect any of the controllers } else timer = millis(); // Don't wait // First the HID interrupt channel has to be disconnected, then the HID control channel and finally the HCI connection - pBtd->l2cap_disconnection_request(hci_handle, 0x0A, interrupt_scid, interrupt_dcid); + pBtd->l2cap_disconnection_request(hci_handle, ++identifier, interrupt_scid, interrupt_dcid); Reset(); l2cap_state = L2CAP_INTERRUPT_DISCONNECT; } From 184770489f19b9874b1bcd75b2d249bd4d96846a Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Fri, 13 Dec 2013 10:41:12 +0100 Subject: [PATCH 05/11] Removed another unneeded case in the state machine --- BTD.h | 8 +++----- SPP.cpp | 56 ++++++++++++++++++++++---------------------------------- 2 files changed, 25 insertions(+), 39 deletions(-) diff --git a/BTD.h b/BTD.h index a6b7b479..ca1e903e 100755 --- a/BTD.h +++ b/BTD.h @@ -118,14 +118,12 @@ /* Used for SDP channel */ #define L2CAP_SDP_WAIT 10 #define L2CAP_SDP_SUCCESS 11 -#define L2CAP_SDP_DONE 12 /* Used for RFCOMM channel */ -#define L2CAP_RFCOMM_WAIT 13 -#define L2CAP_RFCOMM_SUCCESS 14 -#define L2CAP_RFCOMM_DONE 15 +#define L2CAP_RFCOMM_WAIT 12 +#define L2CAP_RFCOMM_SUCCESS 13 -#define L2CAP_DISCONNECT_RESPONSE 16 // Used for both SDP and RFCOMM channel +#define L2CAP_DISCONNECT_RESPONSE 14 // Used for both SDP and RFCOMM channel /* Bluetooth states used by some drivers */ #define TURN_ON_LED 17 diff --git a/SPP.cpp b/SPP.cpp index b36b2e4f..db79f2cb 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -452,6 +452,13 @@ void SPP::SDP_task() { delay(1); pBtd->l2cap_config_request(hci_handle, identifier, sdp_scid); l2cap_sdp_state = L2CAP_SDP_SUCCESS; + } else if (l2cap_check_flag(L2CAP_FLAG_DISCONNECT_SDP_REQUEST)) { + l2cap_clear_flag(L2CAP_FLAG_DISCONNECT_SDP_REQUEST); // Clear flag + SDPConnected = false; +#ifdef DEBUG_USB_HOST + Notify(PSTR("\r\nDisconnected SDP Channel"), 0x80); +#endif + pBtd->l2cap_disconnection_response(hci_handle, identifier, sdp_dcid, sdp_scid); } break; case L2CAP_SDP_SUCCESS: @@ -462,33 +469,18 @@ void SPP::SDP_task() { #endif firstMessage = true; // Reset bool SDPConnected = true; - l2cap_sdp_state = L2CAP_SDP_DONE; + l2cap_sdp_state = L2CAP_SDP_WAIT; } break; - case L2CAP_SDP_DONE: - if (l2cap_check_flag(L2CAP_FLAG_DISCONNECT_SDP_REQUEST)) { - l2cap_clear_flag(L2CAP_FLAG_DISCONNECT_SDP_REQUEST); // Clear flag - SDPConnected = false; -#ifdef DEBUG_USB_HOST - Notify(PSTR("\r\nDisconnected SDP Channel"), 0x80); -#endif - pBtd->l2cap_disconnection_response(hci_handle, identifier, sdp_dcid, sdp_scid); - l2cap_sdp_state = L2CAP_SDP_WAIT; - } else if (l2cap_check_flag(L2CAP_FLAG_CONNECTION_SDP_REQUEST)) - l2cap_rfcomm_state = L2CAP_SDP_WAIT; - break; + case L2CAP_DISCONNECT_RESPONSE: // This is for both disconnection response from the RFCOMM and SDP channel if they were connected if (l2cap_check_flag(L2CAP_FLAG_DISCONNECT_RESPONSE)) { #ifdef DEBUG_USB_HOST Notify(PSTR("\r\nDisconnected L2CAP Connection"), 0x80); #endif - RFCOMMConnected = false; - SDPConnected = false; pBtd->hci_disconnect(hci_handle); hci_handle = -1; // Reset handle - l2cap_event_flag = 0; // Reset flags - l2cap_sdp_state = L2CAP_SDP_WAIT; - l2cap_rfcomm_state = L2CAP_RFCOMM_WAIT; + Reset(); } break; } @@ -509,6 +501,14 @@ void SPP::RFCOMM_task() { delay(1); pBtd->l2cap_config_request(hci_handle, identifier, rfcomm_scid); l2cap_rfcomm_state = L2CAP_RFCOMM_SUCCESS; + } else if (l2cap_check_flag(L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST)) { + l2cap_clear_flag(L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST); // Clear flag + RFCOMMConnected = false; + connected = false; +#ifdef DEBUG_USB_HOST + Notify(PSTR("\r\nDisconnected RFCOMM Channel"), 0x80); +#endif + pBtd->l2cap_disconnection_response(hci_handle, identifier, rfcomm_dcid, rfcomm_scid); } break; case L2CAP_RFCOMM_SUCCESS: @@ -520,22 +520,9 @@ void SPP::RFCOMM_task() { rfcommAvailable = 0; // Reset number of bytes available bytesRead = 0; // Reset number of bytes received RFCOMMConnected = true; - l2cap_rfcomm_state = L2CAP_RFCOMM_DONE; + l2cap_rfcomm_state = L2CAP_RFCOMM_WAIT; } break; - case L2CAP_RFCOMM_DONE: - if (l2cap_check_flag(L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST)) { - l2cap_clear_flag(L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST); // Clear flag - RFCOMMConnected = false; - connected = false; -#ifdef DEBUG_USB_HOST - Notify(PSTR("\r\nDisconnected RFCOMM Channel"), 0x80); -#endif - pBtd->l2cap_disconnection_response(hci_handle, identifier, rfcomm_dcid, rfcomm_scid); - l2cap_rfcomm_state = L2CAP_RFCOMM_WAIT; - } else if (l2cap_check_flag(L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST)) - l2cap_rfcomm_state = L2CAP_RFCOMM_WAIT; - break; } } /************************************************************/ @@ -719,10 +706,11 @@ uint8_t SPP::crc(uint8_t *data) { /* Calculate FCS */ uint8_t SPP::calcFcs(uint8_t *data) { + uint8_t temp = crc(data); if ((data[1] & 0xEF) == RFCOMM_UIH) - return (0xFF - crc(data)); // FCS on 2 bytes + return (0xFF - temp); // FCS on 2 bytes else - return (0xFF - pgm_read_byte(&rfcomm_crc_table[crc(data) ^ data[2]])); // FCS on 3 bytes + return (0xFF - pgm_read_byte(&rfcomm_crc_table[temp ^ data[2]])); // FCS on 3 bytes } /* Check FCS */ From 471a8d9163590a7fdb1e6106b49b909e6c6d9223 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Fri, 13 Dec 2013 10:41:59 +0100 Subject: [PATCH 06/11] Set pin even if it should not pair to a device Needed by some Bluetooth mice that always want authentication --- BTHID.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/BTHID.cpp b/BTHID.cpp index 5f4c2e57..17b5ea63 100644 --- a/BTHID.cpp +++ b/BTHID.cpp @@ -31,9 +31,7 @@ protocolMode(HID_BOOT_PROTOCOL) pBtd->registerServiceClass(this); // Register it as a Bluetooth service pBtd->pairWithHIDDevice = pair; - - if (pair) - pBtd->btdPin= pin; + pBtd->btdPin= pin; /* Set device cid for the control and intterrupt channelse - LSB */ control_dcid[0] = 0x70; // 0x0070 From 6ea34f955e8704696072e4ce9eaaa7d357330d7e Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Fri, 13 Dec 2013 11:18:30 +0100 Subject: [PATCH 07/11] Fixed flag macros Needed if you want to clear multiple flags at once --- BTD.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/BTD.h b/BTD.h index ca1e903e..cc328deb 100755 --- a/BTD.h +++ b/BTD.h @@ -71,9 +71,9 @@ #define HCI_FLAG_CONNECT_EVENT 0x100 /* Macros for HCI event flag tests */ -#define hci_check_flag(flag) (hci_event_flag & flag) -#define hci_set_flag(flag) (hci_event_flag |= flag) -#define hci_clear_flag(flag) (hci_event_flag &= ~flag) +#define hci_check_flag(flag) (hci_event_flag & (flag)) +#define hci_set_flag(flag) (hci_event_flag |= (flag)) +#define hci_clear_flag(flag) (hci_event_flag &= ~(flag)) /* HCI Events managed */ #define EV_INQUIRY_COMPLETE 0x01 @@ -157,9 +157,9 @@ #define L2CAP_FLAG_DISCONNECT_RESPONSE 0x00004000 /* Macros for L2CAP event flag tests */ -#define l2cap_check_flag(flag) (l2cap_event_flag & flag) -#define l2cap_set_flag(flag) (l2cap_event_flag |= flag) -#define l2cap_clear_flag(flag) (l2cap_event_flag &= ~flag) +#define l2cap_check_flag(flag) (l2cap_event_flag & (flag)) +#define l2cap_set_flag(flag) (l2cap_event_flag |= (flag)) +#define l2cap_clear_flag(flag) (l2cap_event_flag &= ~(flag)) /* L2CAP signaling commands */ #define L2CAP_CMD_COMMAND_REJECT 0x01 From 0b2e444e7b9db182d18b3c7ea7100c59f088b31d Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Fri, 13 Dec 2013 11:27:05 +0100 Subject: [PATCH 08/11] Fixed macros in Wii.h as well --- Wii.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Wii.h b/Wii.h index 06a74ee5..8b46c2b2 100755 --- a/Wii.h +++ b/Wii.h @@ -30,9 +30,9 @@ #define WII_FLAG_MOTION_PLUS_CONNECTED 0x01 #define WII_FLAG_NUNCHUCK_CONNECTED 0x02 -#define wii_check_flag(flag) (wii_event_flag & flag) -#define wii_set_flag(flag) (wii_event_flag |= flag) -#define wii_clear_flag(flag) (wii_event_flag &= ~flag) +#define wii_check_flag(flag) (wii_event_flag & (flag)) +#define wii_set_flag(flag) (wii_event_flag |= (flag)) +#define wii_clear_flag(flag) (wii_event_flag &= ~(flag)) /** Enum used to read the joystick on the Nunchuck. */ enum Hat { From f283e90402a9abab6776aa2697c35320ae8037fc Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Sun, 15 Dec 2013 22:19:50 +0100 Subject: [PATCH 09/11] Renamed CLK, MOSI, MISO and SS variables, as they conflicted with defines in pins_arduino.h See: https://github.com/arduino/Arduino/commit/7fcba37acfd11313640b3f5d5c813d63d2f59999 --- usbhost.h | 96 +++++++++++++++++++++++++++---------------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/usbhost.h b/usbhost.h index 81949431..90b4da5a 100644 --- a/usbhost.h +++ b/usbhost.h @@ -28,7 +28,7 @@ e-mail : support@circuitsathome.com #endif /* SPI initialization */ -template< typename CLK, typename MOSI, typename MISO, typename SPI_SS > class SPi { +template< typename SPI_CLK, typename SPI_MOSI, typename SPI_MISO, typename SPI_SS > class SPi { #if USING_SPI4TEENSY3 public: @@ -46,9 +46,9 @@ public: static void init() { //uint8_t tmp; - CLK::SetDirWrite(); - MOSI::SetDirWrite(); - MISO::SetDirRead(); + SPI_CLK::SetDirWrite(); + SPI_MOSI::SetDirWrite(); + SPI_MISO::SetDirRead(); SPI_SS::SetDirWrite(); /* mode 00 (CPOL=0, CPHA=0) master, fclk/2. Mode 11 (CPOL=11, CPHA=11) is also supported by MAX3421E */ SPCR = 0x50; @@ -78,7 +78,7 @@ typedef enum { vbus_off = GPX_VBDET } VBUS_t; -template< typename SS, typename INTR > class MAX3421e /* : public spi */ { +template< typename SPI_SS, typename INTR > class MAX3421e /* : public spi */ { static uint8_t vbusState; public: @@ -106,12 +106,12 @@ public: uint8_t Task(); }; -template< typename SS, typename INTR > - uint8_t MAX3421e< SS, INTR >::vbusState = 0; +template< typename SPI_SS, typename INTR > + uint8_t MAX3421e< SPI_SS, INTR >::vbusState = 0; /* constructor */ -template< typename SS, typename INTR > -MAX3421e< SS, INTR >::MAX3421e() { +template< typename SPI_SS, typename INTR > +MAX3421e< SPI_SS, INTR >::MAX3421e() { // Leaving ADK hardware setup in here, for now. This really belongs with the other parts. #ifdef BOARD_MEGA_ADK // For Mega ADK, which has a Max3421e on-board, set MAX_RESET to output mode, and then set it to HIGH @@ -121,10 +121,10 @@ MAX3421e< SS, INTR >::MAX3421e() { }; /* write single byte into MAX3421 register */ -template< typename SS, typename INTR > -void MAX3421e< SS, INTR >::regWr(uint8_t reg, uint8_t data) { +template< typename SPI_SS, typename INTR > +void MAX3421e< SPI_SS, INTR >::regWr(uint8_t reg, uint8_t data) { XMEM_ACQUIRE_SPI(); - SS::Clear(); + SPI_SS::Clear(); #if USING_SPI4TEENSY3 uint8_t c[2]; c[0] = reg | 0x02; @@ -136,17 +136,17 @@ void MAX3421e< SS, INTR >::regWr(uint8_t reg, uint8_t data) { SPDR = data; while(!(SPSR & (1 << SPIF))); #endif - SS::Set(); + SPI_SS::Set(); XMEM_RELEASE_SPI(); return; }; /* multiple-byte write */ /* returns a pointer to memory position after last written */ -template< typename SS, typename INTR > -uint8_t* MAX3421e< SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* data_p) { +template< typename SPI_SS, typename INTR > +uint8_t* MAX3421e< SPI_SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* data_p) { XMEM_ACQUIRE_SPI(); - SS::Clear(); + SPI_SS::Clear(); #if USING_SPI4TEENSY3 spi4teensy3::send(reg | 0x02); spi4teensy3::send(data_p, nbytes); @@ -160,7 +160,7 @@ uint8_t* MAX3421e< SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* dat } while(!(SPSR & (1 << SPIF))); #endif - SS::Set(); + SPI_SS::Set(); XMEM_RELEASE_SPI(); return( data_p); } @@ -168,8 +168,8 @@ uint8_t* MAX3421e< SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* dat /*GPIO byte is split between 2 registers, so two writes are needed to write one byte */ /* GPOUT bits are in the low nibble. 0-3 in IOPINS1, 4-7 in IOPINS2 */ -template< typename SS, typename INTR > -void MAX3421e< SS, INTR >::gpioWr(uint8_t data) { +template< typename SPI_SS, typename INTR > +void MAX3421e< SPI_SS, INTR >::gpioWr(uint8_t data) { regWr(rIOPINS1, data); data >>= 4; regWr(rIOPINS2, data); @@ -177,20 +177,20 @@ void MAX3421e< SS, INTR >::gpioWr(uint8_t data) { } /* single host register read */ -template< typename SS, typename INTR > -uint8_t MAX3421e< SS, INTR >::regRd(uint8_t reg) { +template< typename SPI_SS, typename INTR > +uint8_t MAX3421e< SPI_SS, INTR >::regRd(uint8_t reg) { XMEM_ACQUIRE_SPI(); - SS::Clear(); + SPI_SS::Clear(); #if USING_SPI4TEENSY3 spi4teensy3::send(reg); uint8_t rv = spi4teensy3::receive(); - SS::Set(); + SPI_SS::Set(); #else SPDR = reg; while(!(SPSR & (1 << SPIF))); SPDR = 0; //send empty byte while(!(SPSR & (1 << SPIF))); - SS::Set(); + SPI_SS::Set(); uint8_t rv = SPDR; #endif XMEM_RELEASE_SPI(); @@ -199,10 +199,10 @@ uint8_t MAX3421e< SS, INTR >::regRd(uint8_t reg) { /* multiple-byte register read */ /* returns a pointer to a memory position after last read */ -template< typename SS, typename INTR > -uint8_t* MAX3421e< SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* data_p) { +template< typename SPI_SS, typename INTR > +uint8_t* MAX3421e< SPI_SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* data_p) { XMEM_ACQUIRE_SPI(); - SS::Clear(); + SPI_SS::Clear(); #if USING_SPI4TEENSY3 spi4teensy3::send(reg); spi4teensy3::receive(data_p, nbytes); @@ -227,15 +227,15 @@ uint8_t* MAX3421e< SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* dat } #endif #endif - SS::Set(); + SPI_SS::Set(); XMEM_RELEASE_SPI(); return( data_p); } /* GPIO read. See gpioWr for explanation */ /* GPIN pins are in high nibbles of IOPINS1, IOPINS2 */ -template< typename SS, typename INTR > -uint8_t MAX3421e< SS, INTR >::gpioRd() { +template< typename SPI_SS, typename INTR > +uint8_t MAX3421e< SPI_SS, INTR >::gpioRd() { uint8_t gpin = 0; gpin = regRd(rIOPINS2); //pins 4-7 gpin &= 0xf0; //clean lower nibble @@ -245,8 +245,8 @@ uint8_t MAX3421e< SS, INTR >::gpioRd() { /* reset MAX3421E. Returns number of cycles it took for PLL to stabilize after reset or zero if PLL haven't stabilized in 65535 cycles */ -template< typename SS, typename INTR > -uint16_t MAX3421e< SS, INTR >::reset() { +template< typename SPI_SS, typename INTR > +uint16_t MAX3421e< SPI_SS, INTR >::reset() { uint16_t i = 0; regWr(rUSBCTL, bmCHIPRES); regWr(rUSBCTL, 0x00); @@ -259,15 +259,15 @@ uint16_t MAX3421e< SS, INTR >::reset() { } /* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */ -template< typename SS, typename INTR > -int8_t MAX3421e< SS, INTR >::Init() { +template< typename SPI_SS, typename INTR > +int8_t MAX3421e< SPI_SS, INTR >::Init() { XMEM_ACQUIRE_SPI(); // Moved here. // you really should not init hardware in the constructor when it involves locks. // Also avoids the vbus flicker issue confusing some devices. /* pin and peripheral setup */ - SS::SetDirWrite(); - SS::Set(); + SPI_SS::SetDirWrite(); + SPI_SS::Set(); spi::init(); INTR::SetDirRead(); XMEM_RELEASE_SPI(); @@ -296,15 +296,15 @@ int8_t MAX3421e< SS, INTR >::Init() { } /* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */ -template< typename SS, typename INTR > -int8_t MAX3421e< SS, INTR >::Init(int mseconds) { +template< typename SPI_SS, typename INTR > +int8_t MAX3421e< SPI_SS, INTR >::Init(int mseconds) { XMEM_ACQUIRE_SPI(); // Moved here. // you really should not init hardware in the constructor when it involves locks. // Also avoids the vbus flicker issue confusing some devices. /* pin and peripheral setup */ - SS::SetDirWrite(); - SS::Set(); + SPI_SS::SetDirWrite(); + SPI_SS::Set(); spi::init(); INTR::SetDirRead(); XMEM_RELEASE_SPI(); @@ -340,8 +340,8 @@ int8_t MAX3421e< SS, INTR >::Init(int mseconds) { } /* probe bus to determine device presence and speed and switch host to this speed */ -template< typename SS, typename INTR > -void MAX3421e< SS, INTR >::busprobe() { +template< typename SPI_SS, typename INTR > +void MAX3421e< SPI_SS, INTR >::busprobe() { uint8_t bus_sample; bus_sample = regRd(rHRSL); //Get J,K status bus_sample &= (bmJSTATUS | bmKSTATUS); //zero the rest of the byte @@ -375,8 +375,8 @@ void MAX3421e< SS, INTR >::busprobe() { } /* MAX3421 state change task and interrupt handler */ -template< typename SS, typename INTR > -uint8_t MAX3421e< SS, INTR >::Task(void) { +template< typename SPI_SS, typename INTR > +uint8_t MAX3421e< SPI_SS, INTR >::Task(void) { uint8_t rcode = 0; uint8_t pinvalue; //USB_HOST_SERIAL.print("Vbus state: "); @@ -394,8 +394,8 @@ uint8_t MAX3421e< SS, INTR >::Task(void) { return( rcode); } -template< typename SS, typename INTR > -uint8_t MAX3421e< SS, INTR >::IntHandler() { +template< typename SPI_SS, typename INTR > +uint8_t MAX3421e< SPI_SS, INTR >::IntHandler() { uint8_t HIRQ; uint8_t HIRQ_sendback = 0x00; HIRQ = regRd(rHIRQ); //determine interrupt source @@ -410,8 +410,8 @@ uint8_t MAX3421e< SS, INTR >::IntHandler() { regWr(rHIRQ, HIRQ_sendback); return( HIRQ_sendback); } -//template< typename SS, typename INTR > -//uint8_t MAX3421e< SS, INTR >::GpxHandler() +//template< typename SPI_SS, typename INTR > +//uint8_t MAX3421e< SPI_SS, INTR >::GpxHandler() //{ // uint8_t GPINIRQ = regRd( rGPINIRQ ); //read GPIN IRQ register //// if( GPINIRQ & bmGPINIRQ7 ) { //vbus overload From 92c7a6430917b22222fecc0f0c934e080527d6c4 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Mon, 16 Dec 2013 09:16:12 +0100 Subject: [PATCH 10/11] Store all strings in getStatusString() in flash --- PS3BT.cpp | 60 +++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/PS3BT.cpp b/PS3BT.cpp index 16beea60..7745e38e 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -162,48 +162,48 @@ bool PS3BT::getStatus(Status c) { String PS3BT::getStatusString() { if (PS3Connected || PS3NavigationConnected) { - char statusOutput[100]; + char statusOutput[100]; // Max string length plus null character - strcpy(statusOutput, "ConnectionStatus: "); + strcpy_P(statusOutput, PSTR("ConnectionStatus: ")); - if (getStatus(Plugged)) strcat(statusOutput, "Plugged"); - else if (getStatus(Unplugged)) strcat(statusOutput, "Unplugged"); - else strcat(statusOutput, "Error"); + if (getStatus(Plugged)) strcat_P(statusOutput, PSTR("Plugged")); + else if (getStatus(Unplugged)) strcat_P(statusOutput, PSTR("Unplugged")); + else strcat_P(statusOutput, PSTR("Error")); - strcat(statusOutput, " - PowerRating: "); - if (getStatus(Charging)) strcat(statusOutput, "Charging"); - else if (getStatus(NotCharging)) strcat(statusOutput, "Not Charging"); - else if (getStatus(Shutdown)) strcat(statusOutput, "Shutdown"); - else if (getStatus(Dying)) strcat(statusOutput, "Dying"); - else if (getStatus(Low)) strcat(statusOutput, "Low"); - else if (getStatus(High)) strcat(statusOutput, "High"); - else if (getStatus(Full)) strcat(statusOutput, "Full"); - else strcat(statusOutput, "Error"); + strcat_P(statusOutput, PSTR(" - PowerRating: ")); + if (getStatus(Charging)) strcat_P(statusOutput, PSTR("Charging")); + else if (getStatus(NotCharging)) strcat_P(statusOutput, PSTR("Not Charging")); + else if (getStatus(Shutdown)) strcat_P(statusOutput, PSTR("Shutdown")); + else if (getStatus(Dying)) strcat_P(statusOutput, PSTR("Dying")); + else if (getStatus(Low)) strcat_P(statusOutput, PSTR("Low")); + else if (getStatus(High)) strcat_P(statusOutput, PSTR("High")); + else if (getStatus(Full)) strcat_P(statusOutput, PSTR("Full")); + else strcat_P(statusOutput, PSTR("Error")); - strcat(statusOutput, " - WirelessStatus: "); + strcat_P(statusOutput, PSTR(" - WirelessStatus: ")); - if (getStatus(CableRumble)) strcat(statusOutput, "Cable - Rumble is on"); - else if (getStatus(Cable)) strcat(statusOutput, "Cable - Rumble is off"); - else if (getStatus(BluetoothRumble)) strcat(statusOutput, "Bluetooth - Rumble is on"); - else if (getStatus(Bluetooth)) strcat(statusOutput, "Bluetooth - Rumble is off"); - else strcat(statusOutput, "Error"); + if (getStatus(CableRumble)) strcat_P(statusOutput, PSTR("Cable - Rumble is on")); + else if (getStatus(Cable)) strcat_P(statusOutput, PSTR("Cable - Rumble is off")); + else if (getStatus(BluetoothRumble)) strcat_P(statusOutput, PSTR("Bluetooth - Rumble is on")); + else if (getStatus(Bluetooth)) strcat_P(statusOutput, PSTR("Bluetooth - Rumble is off")); + else strcat_P(statusOutput, PSTR("Error")); return statusOutput; } else if (PS3MoveConnected) { - char statusOutput[50]; + char statusOutput[26]; // Max string length plus null character - strcpy(statusOutput, "PowerRating: "); + strcpy_P(statusOutput, PSTR("PowerRating: ")); - if (getStatus(MoveCharging)) strcat(statusOutput, "Charging"); - else if (getStatus(MoveNotCharging)) strcat(statusOutput, "Not Charging"); - else if (getStatus(MoveShutdown)) strcat(statusOutput, "Shutdown"); - else if (getStatus(MoveDying)) strcat(statusOutput, "Dying"); - else if (getStatus(MoveLow)) strcat(statusOutput, "Low"); - else if (getStatus(MoveHigh)) strcat(statusOutput, "High"); - else if (getStatus(MoveFull)) strcat(statusOutput, "Full"); - else strcat(statusOutput, "Error"); + if (getStatus(MoveCharging)) strcat_P(statusOutput, PSTR("Charging")); + else if (getStatus(MoveNotCharging)) strcat_P(statusOutput, PSTR("Not Charging")); + else if (getStatus(MoveShutdown)) strcat_P(statusOutput, PSTR("Shutdown")); + else if (getStatus(MoveDying)) strcat_P(statusOutput, PSTR("Dying")); + else if (getStatus(MoveLow)) strcat_P(statusOutput, PSTR("Low")); + else if (getStatus(MoveHigh)) strcat_P(statusOutput, PSTR("High")); + else if (getStatus(MoveFull)) strcat_P(statusOutput, PSTR("Full")); + else strcat_P(statusOutput, PSTR("Error")); return statusOutput; } else From fdd65eab68eaf509d2f768aa20540fec3eec7b95 Mon Sep 17 00:00:00 2001 From: Kristian Lauszus Date: Mon, 16 Dec 2013 09:18:01 +0100 Subject: [PATCH 11/11] Whitespace --- SPP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SPP.cpp b/SPP.cpp index db79f2cb..60973547 100644 --- a/SPP.cpp +++ b/SPP.cpp @@ -706,7 +706,7 @@ uint8_t SPP::crc(uint8_t *data) { /* Calculate FCS */ uint8_t SPP::calcFcs(uint8_t *data) { - uint8_t temp = crc(data); + uint8_t temp = crc(data); if ((data[1] & 0xEF) == RFCOMM_UIH) return (0xFF - temp); // FCS on 2 bytes else