From 38d155f9932c9666ac08b7fea1aabb72a7222361 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 9 Jun 2018 15:37:05 -0400 Subject: [PATCH 1/4] WiimoteDevice: Make ConnectionState enum an enum class Avoids polluting class scope and makes it strongly typed. --- .../Core/IOS/USB/Bluetooth/WiimoteDevice.cpp | 21 ++++++++----------- .../Core/IOS/USB/Bluetooth/WiimoteDevice.h | 17 ++++++++------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp index 3ea20a85b3..bf5f600372 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp @@ -56,7 +56,7 @@ WiimoteDevice::WiimoteDevice(Device::BluetoothEmu* host, int number, bdaddr_t bd { INFO_LOG(IOS_WIIMOTE, "Wiimote: #%i Constructed", number); - m_ConnectionState = (ready) ? CONN_READY : CONN_INACTIVE; + m_ConnectionState = ready ? ConnectionState::Ready : ConnectionState::Inactive; m_ConnectionHandle = 0x100 + number; memset(m_LinkKey, 0xA0 + number, HCI_KEY_SIZE); @@ -130,7 +130,7 @@ void WiimoteDevice::DoState(PointerWrap& p) bool WiimoteDevice::LinkChannel() { - if (m_ConnectionState != CONN_LINKING) + if (m_ConnectionState != ConnectionState::Linking) return false; // try to connect L2CAP_PSM_HID_CNTL @@ -178,7 +178,7 @@ bool WiimoteDevice::LinkChannel() } DEBUG_LOG(IOS_WIIMOTE, "ConnectionState CONN_LINKING -> CONN_COMPLETE"); - m_ConnectionState = CONN_COMPLETE; + m_ConnectionState = ConnectionState::Complete; return false; } @@ -195,9 +195,9 @@ bool WiimoteDevice::LinkChannel() // void WiimoteDevice::Activate(bool ready) { - if (ready && (m_ConnectionState == CONN_INACTIVE)) + if (ready && (m_ConnectionState == ConnectionState::Inactive)) { - m_ConnectionState = CONN_READY; + m_ConnectionState = ConnectionState::Ready; } else if (!ready) { @@ -209,7 +209,7 @@ void WiimoteDevice::Activate(bool ready) void WiimoteDevice::EventConnectionAccepted() { DEBUG_LOG(IOS_WIIMOTE, "ConnectionState %x -> CONN_LINKING", m_ConnectionState); - m_ConnectionState = CONN_LINKING; + m_ConnectionState = ConnectionState::Linking; } void WiimoteDevice::EventDisconnect() @@ -217,17 +217,14 @@ void WiimoteDevice::EventDisconnect() // Send disconnect message to plugin Wiimote::ControlChannel(m_ConnectionHandle & 0xFF, 99, nullptr, 0); - m_ConnectionState = CONN_INACTIVE; + m_ConnectionState = ConnectionState::Inactive; // Clear channel flags ResetChannels(); } -bool WiimoteDevice::EventPagingChanged(u8 _pageMode) +bool WiimoteDevice::EventPagingChanged(u8 page_mode) { - if ((m_ConnectionState == CONN_READY) && (_pageMode & HCI_PAGE_SCAN_ENABLE)) - return true; - - return false; + return (m_ConnectionState == ConnectionState::Ready) && (page_mode & HCI_PAGE_SCAN_ENABLE); } void WiimoteDevice::ResetChannels() diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h index 439b01dae6..6614fb3281 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h @@ -30,8 +30,8 @@ public: // ugly Host handling.... // we really have to clean all this code - bool IsConnected() const { return m_ConnectionState == CONN_COMPLETE; } - bool IsInactive() const { return m_ConnectionState == CONN_INACTIVE; } + bool IsConnected() const { return m_ConnectionState == ConnectionState::Complete; } + bool IsInactive() const { return m_ConnectionState == ConnectionState::Inactive; } bool LinkChannel(); void ResetChannels(); void Activate(bool ready); @@ -40,7 +40,7 @@ public: void EventConnectionAccepted(); void EventDisconnect(); - bool EventPagingChanged(u8 _pageMode); + bool EventPagingChanged(u8 page_mode); const bdaddr_t& GetBD() const { return m_BD; } const uint8_t* GetClass() const { return uclass; } @@ -53,13 +53,14 @@ public: const u8* GetLinkKey() const { return m_LinkKey; } private: - enum ConnectionState + enum class ConnectionState { - CONN_INACTIVE = -1, - CONN_READY, - CONN_LINKING, - CONN_COMPLETE + Inactive = -1, + Ready, + Linking, + Complete }; + ConnectionState m_ConnectionState; bool m_HIDControlChannel_Connected = false; From f906785e88d330453620f9731a1abea3b6104a9b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 9 Jun 2018 15:50:20 -0400 Subject: [PATCH 2/4] WiimoteDevice: Make EventPagingChanged() a const member function This only queries internal state, it doesn't modify it. With minor adjustments to BTEmu, this also allows us to make its usage instance a constant reference. --- Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp | 4 ++-- Source/Core/Core/IOS/USB/Bluetooth/BTEmu.h | 2 +- Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp | 2 +- Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp b/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp index df33bd99b0..7fe2ea235c 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.cpp @@ -339,7 +339,7 @@ void BluetoothEmu::Update() // Create ACL connection if (m_HCIEndpoint && (m_ScanEnable & HCI_PAGE_SCAN_ENABLE)) { - for (auto& wiimote : m_WiiMotes) + for (const auto& wiimote : m_WiiMotes) { if (wiimote.EventPagingChanged(m_ScanEnable)) SendEventRequestConnection(wiimote); @@ -524,7 +524,7 @@ bool BluetoothEmu::SendEventConnectionComplete(const bdaddr_t& _bd) } // This is called from Update() after ScanEnable has been enabled. -bool BluetoothEmu::SendEventRequestConnection(WiimoteDevice& _rWiiMote) +bool BluetoothEmu::SendEventRequestConnection(const WiimoteDevice& _rWiiMote) { SQueuedEvent Event(sizeof(SHCIEventRequestConnection), 0); diff --git a/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.h b/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.h index 19463e72c0..f695cfb4a1 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.h +++ b/Source/Core/Core/IOS/USB/Bluetooth/BTEmu.h @@ -112,7 +112,7 @@ private: bool SendEventInquiryResponse(); bool SendEventInquiryComplete(); bool SendEventRemoteNameReq(const bdaddr_t& _bd); - bool SendEventRequestConnection(WiimoteDevice& _rWiiMote); + bool SendEventRequestConnection(const WiimoteDevice& _rWiiMote); bool SendEventConnectionComplete(const bdaddr_t& _bd); bool SendEventReadClockOffsetComplete(u16 _connectionHandle); bool SendEventConPacketTypeChange(u16 _connectionHandle, u16 _packetType); diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp index bf5f600372..9d785dd886 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp @@ -222,7 +222,7 @@ void WiimoteDevice::EventDisconnect() ResetChannels(); } -bool WiimoteDevice::EventPagingChanged(u8 page_mode) +bool WiimoteDevice::EventPagingChanged(u8 page_mode) const { return (m_ConnectionState == ConnectionState::Ready) && (page_mode & HCI_PAGE_SCAN_ENABLE); } diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h index 6614fb3281..c1eef851cc 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h @@ -40,7 +40,7 @@ public: void EventConnectionAccepted(); void EventDisconnect(); - bool EventPagingChanged(u8 page_mode); + bool EventPagingChanged(u8 page_mode) const; const bdaddr_t& GetBD() const { return m_BD; } const uint8_t* GetClass() const { return uclass; } From 94fd8505d612e27bb919f145a42934bc80d40c1f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 9 Jun 2018 15:52:31 -0400 Subject: [PATCH 3/4] WiimoteDevice: Make DoesChannelExist() a const member function This only queries internal class state, it doesn't modify it. --- Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h index c1eef851cc..61c334a4d9 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h @@ -96,7 +96,7 @@ private: typedef std::map CChannelMap; CChannelMap m_Channel; - bool DoesChannelExist(u16 _SCID) { return m_Channel.find(_SCID) != m_Channel.end(); } + bool DoesChannelExist(u16 scid) const { return m_Channel.find(scid) != m_Channel.end(); } void SendCommandToACL(u8 _Ident, u8 _Code, u8 _CommandLength, u8* _pCommandData); void SignalChannel(u8* _pData, u32 _Size); From 647da59679f6cac4176afe283efc9dee496f64a5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 9 Jun 2018 16:01:39 -0400 Subject: [PATCH 4/4] WiimoteDevice: Move channel state booleans into a struct These were essentially duplicated for both channels, when they could be implemented in terms of a struct, which allows for simplifying the reset case. --- .../Core/IOS/USB/Bluetooth/WiimoteDevice.cpp | 62 +++++++++---------- .../Core/IOS/USB/Bluetooth/WiimoteDevice.h | 18 +++--- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp index 9d785dd886..7225656fc5 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.cpp @@ -96,14 +96,14 @@ void WiimoteDevice::DoState(PointerWrap& p) p.Do(m_ConnectionState); - p.Do(m_HIDControlChannel_Connected); - p.Do(m_HIDControlChannel_ConnectedWait); - p.Do(m_HIDControlChannel_Config); - p.Do(m_HIDControlChannel_ConfigWait); - p.Do(m_HIDInterruptChannel_Connected); - p.Do(m_HIDInterruptChannel_ConnectedWait); - p.Do(m_HIDInterruptChannel_Config); - p.Do(m_HIDInterruptChannel_ConfigWait); + p.Do(m_hid_control_channel.connected); + p.Do(m_hid_control_channel.connected_wait); + p.Do(m_hid_control_channel.config); + p.Do(m_hid_control_channel.config_wait); + p.Do(m_hid_interrupt_channel.connected); + p.Do(m_hid_interrupt_channel.connected_wait); + p.Do(m_hid_interrupt_channel.config); + p.Do(m_hid_interrupt_channel.config_wait); p.Do(m_BD); p.Do(m_ConnectionHandle); @@ -134,45 +134,45 @@ bool WiimoteDevice::LinkChannel() return false; // try to connect L2CAP_PSM_HID_CNTL - if (!m_HIDControlChannel_Connected) + if (!m_hid_control_channel.connected) { - if (m_HIDControlChannel_ConnectedWait) + if (m_hid_control_channel.connected_wait) return false; - m_HIDControlChannel_ConnectedWait = true; + m_hid_control_channel.connected_wait = true; SendConnectionRequest(0x0040, L2CAP_PSM_HID_CNTL); return true; } // try to config L2CAP_PSM_HID_CNTL - if (!m_HIDControlChannel_Config) + if (!m_hid_control_channel.config) { - if (m_HIDControlChannel_ConfigWait) + if (m_hid_control_channel.config_wait) return false; - m_HIDControlChannel_ConfigWait = true; + m_hid_control_channel.config_wait = true; SendConfigurationRequest(0x0040); return true; } // try to connect L2CAP_PSM_HID_INTR - if (!m_HIDInterruptChannel_Connected) + if (!m_hid_interrupt_channel.connected) { - if (m_HIDInterruptChannel_ConnectedWait) + if (m_hid_interrupt_channel.connected_wait) return false; - m_HIDInterruptChannel_ConnectedWait = true; + m_hid_interrupt_channel.connected_wait = true; SendConnectionRequest(0x0041, L2CAP_PSM_HID_INTR); return true; } // try to config L2CAP_PSM_HID_INTR - if (!m_HIDInterruptChannel_Config) + if (!m_hid_interrupt_channel.config) { - if (m_HIDInterruptChannel_ConfigWait) + if (m_hid_interrupt_channel.config_wait) return false; - m_HIDInterruptChannel_ConfigWait = true; + m_hid_interrupt_channel.config_wait = true; SendConfigurationRequest(0x0041); return true; } @@ -230,14 +230,8 @@ bool WiimoteDevice::EventPagingChanged(u8 page_mode) const void WiimoteDevice::ResetChannels() { // reset connection process - m_HIDControlChannel_Connected = false; - m_HIDControlChannel_Config = false; - m_HIDInterruptChannel_Connected = false; - m_HIDInterruptChannel_Config = false; - m_HIDControlChannel_ConnectedWait = false; - m_HIDControlChannel_ConfigWait = false; - m_HIDInterruptChannel_ConnectedWait = false; - m_HIDInterruptChannel_ConfigWait = false; + m_hid_control_channel = {}; + m_hid_interrupt_channel = {}; } // @@ -422,9 +416,9 @@ void WiimoteDevice::ReceiveConnectionResponse(u8 _Ident, u8* _pData, u32 _Size) // update state machine if (rChannel.PSM == L2CAP_PSM_HID_CNTL) - m_HIDControlChannel_Connected = true; + m_hid_control_channel.connected = true; else if (rChannel.PSM == L2CAP_PSM_HID_INTR) - m_HIDInterruptChannel_Connected = true; + m_hid_interrupt_channel.connected = true; } void WiimoteDevice::ReceiveConfigurationReq(u8 _Ident, u8* _pData, u32 _Size) @@ -498,9 +492,9 @@ void WiimoteDevice::ReceiveConfigurationReq(u8 _Ident, u8* _pData, u32 _Size) // update state machine if (rChannel.PSM == L2CAP_PSM_HID_CNTL) - m_HIDControlChannel_Connected = true; + m_hid_control_channel.connected = true; else if (rChannel.PSM == L2CAP_PSM_HID_INTR) - m_HIDInterruptChannel_Connected = true; + m_hid_interrupt_channel.connected = true; } void WiimoteDevice::ReceiveConfigurationResponse(u8 _Ident, u8* _pData, u32 _Size) @@ -518,9 +512,9 @@ void WiimoteDevice::ReceiveConfigurationResponse(u8 _Ident, u8* _pData, u32 _Siz SChannel& rChannel = m_Channel[rsp->scid]; if (rChannel.PSM == L2CAP_PSM_HID_CNTL) - m_HIDControlChannel_Config = true; + m_hid_control_channel.config = true; else if (rChannel.PSM == L2CAP_PSM_HID_INTR) - m_HIDInterruptChannel_Config = true; + m_hid_interrupt_channel.config = true; } void WiimoteDevice::ReceiveDisconnectionReq(u8 _Ident, u8* _pData, u32 _Size) diff --git a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h index 61c334a4d9..43b2acaacb 100644 --- a/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h +++ b/Source/Core/Core/IOS/USB/Bluetooth/WiimoteDevice.h @@ -61,16 +61,18 @@ private: Complete }; + struct HIDChannelState + { + bool connected = false; + bool connected_wait = false; + bool config = false; + bool config_wait = false; + }; + ConnectionState m_ConnectionState; - bool m_HIDControlChannel_Connected = false; - bool m_HIDControlChannel_ConnectedWait = false; - bool m_HIDControlChannel_Config = false; - bool m_HIDControlChannel_ConfigWait = false; - bool m_HIDInterruptChannel_Connected = false; - bool m_HIDInterruptChannel_ConnectedWait = false; - bool m_HIDInterruptChannel_Config = false; - bool m_HIDInterruptChannel_ConfigWait = false; + HIDChannelState m_hid_control_channel; + HIDChannelState m_hid_interrupt_channel; // STATE_TO_SAVE bdaddr_t m_BD;