From d2976086b6c8d691e5c3090f73001083362f7f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Mon, 25 Jul 2016 14:48:15 +0200 Subject: [PATCH] WiimoteReal: Remove unsafe static_cast in IOLinux Since we now support different scanner sources, g_wiimotes is not guaranteed to only contain WiimoteLinux anymore. This replaces the previous "already connected" check with one that doesn't use g_wiimotes. --- Source/Core/Core/HW/WiimoteReal/IOLinux.cpp | 69 ++++++++++----------- Source/Core/Core/HW/WiimoteReal/IOLinux.h | 1 - 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/Source/Core/Core/HW/WiimoteReal/IOLinux.cpp b/Source/Core/Core/HW/WiimoteReal/IOLinux.cpp index 57ffeac585..287956137a 100644 --- a/Source/Core/Core/HW/WiimoteReal/IOLinux.cpp +++ b/Source/Core/Core/HW/WiimoteReal/IOLinux.cpp @@ -14,6 +14,14 @@ namespace WiimoteReal { +// This is used to store the Bluetooth address of connected Wiimotes, +// so we can ignore Wiimotes that are already connected. +static std::vector s_known_addrs; +static bool IsNewWiimote(const std::string& addr) +{ + return std::find(s_known_addrs.begin(), s_known_addrs.end(), addr) == s_known_addrs.end(); +} + WiimoteScannerLinux::WiimoteScannerLinux() : m_device_id(-1), m_device_sock(-1) { // Get the id of the first Bluetooth device. @@ -82,39 +90,27 @@ void WiimoteScannerLinux::FindWiimotes(std::vector& found_wiimotes, Wi } ERROR_LOG(WIIMOTE, "device name %s", name); - if (IsValidBluetoothName(name)) + if (!IsValidBluetoothName(name)) + continue; + + char bdaddr_str[18] = {}; + ba2str(&scan_infos[i].bdaddr, bdaddr_str); + + if (!IsNewWiimote(bdaddr_str)) + continue; + + // Found a new device + s_known_addrs.push_back(bdaddr_str); + Wiimote* wm = new WiimoteLinux(scan_infos[i].bdaddr); + if (IsBalanceBoardName(name)) { - bool new_wiimote = true; - - // Determine if this Wiimote has already been found. - for (int j = 0; j < MAX_BBMOTES && new_wiimote; ++j) - { - // compare this address with the stored addresses in our global array - // static_cast is OK here, since we're only ever going to have this subclass in g_wiimotes - // on Linux (and likewise, only WiimoteWindows on Windows, etc) - auto connected_wiimote = static_cast(g_wiimotes[j]); - if (connected_wiimote && bacmp(&scan_infos[i].bdaddr, &connected_wiimote->Address()) == 0) - new_wiimote = false; - } - - if (new_wiimote) - { - // Found a new device - char bdaddr_str[18] = {}; - ba2str(&scan_infos[i].bdaddr, bdaddr_str); - - Wiimote* wm = new WiimoteLinux(scan_infos[i].bdaddr); - if (IsBalanceBoardName(name)) - { - found_board = wm; - NOTICE_LOG(WIIMOTE, "Found balance board (%s).", bdaddr_str); - } - else - { - found_wiimotes.push_back(wm); - NOTICE_LOG(WIIMOTE, "Found Wiimote (%s).", bdaddr_str); - } - } + found_board = wm; + NOTICE_LOG(WIIMOTE, "Found balance board (%s).", bdaddr_str); + } + else + { + found_wiimotes.push_back(wm); + NOTICE_LOG(WIIMOTE, "Found Wiimote (%s).", bdaddr_str); } } } @@ -143,11 +139,6 @@ WiimoteLinux::~WiimoteLinux() close(m_wakeup_pipe_r); } -const bdaddr_t& WiimoteLinux::Address() const -{ - return m_bdaddr; -} - // Connect to a Wiimote with a known address. bool WiimoteLinux::ConnectInternal() { @@ -189,6 +180,10 @@ void WiimoteLinux::DisconnectInternal() m_cmd_sock = -1; m_int_sock = -1; + char bdaddr_str[18] = {}; + ba2str(&m_bdaddr, bdaddr_str); + s_known_addrs.erase(std::remove(s_known_addrs.begin(), s_known_addrs.end(), bdaddr_str), + s_known_addrs.end()); } bool WiimoteLinux::IsConnected() const diff --git a/Source/Core/Core/HW/WiimoteReal/IOLinux.h b/Source/Core/Core/HW/WiimoteReal/IOLinux.h index dde31f8e18..9f74e74a80 100644 --- a/Source/Core/Core/HW/WiimoteReal/IOLinux.h +++ b/Source/Core/Core/HW/WiimoteReal/IOLinux.h @@ -16,7 +16,6 @@ class WiimoteLinux final : public Wiimote public: WiimoteLinux(bdaddr_t bdaddr); ~WiimoteLinux() override; - const bdaddr_t& Address() const; protected: bool ConnectInternal() override;