From e1c8bea2f6edc53644fd95d0f4a3f99526439577 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Thu, 10 Jan 2013 15:23:42 -0600 Subject: [PATCH 1/4] fix crash, but now 100% cpu usage on disconnect --- Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp index b01caf06c8..1fb61f7715 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp @@ -136,10 +136,10 @@ int FindWiimotes(Wiimote** wm, int max_wiimotes) // Connect to a wiimote with a known address. bool Wiimote::Connect() { - struct sockaddr_l2 addr; - - if (IsConnected()) return false; + if (IsConnected()) + return false; + sockaddr_l2 addr; addr.l2_family = AF_BLUETOOTH; addr.l2_bdaddr = bdaddr; addr.l2_cid = 0; @@ -206,17 +206,15 @@ void Wiimote::RealDisconnect() int Wiimote::IORead(unsigned char *buf) { - struct timeval tv; - fd_set fds; - int r; - if (!IsConnected()) return 0; // Block select for 1/2000th of a second + timeval tv; tv.tv_sec = 0; tv.tv_usec = WIIMOTE_DEFAULT_TIMEOUT * 1000; + fd_set fds; FD_ZERO(&fds); FD_SET(in_sock, &fds); @@ -230,7 +228,7 @@ int Wiimote::IORead(unsigned char *buf) return 0; // Read the pending message into the buffer - r = read(in_sock, buf, MAX_PAYLOAD); + int r = read(in_sock, buf, MAX_PAYLOAD); if (r == -1) { // Error reading data @@ -241,17 +239,17 @@ int Wiimote::IORead(unsigned char *buf) // This can happen if the bluetooth dongle is disconnected ERROR_LOG(WIIMOTE, "Bluetooth appears to be disconnected. " "Wiimote %i will be disconnected.", index + 1); - RealDisconnect(); + //RealDisconnect(); } return 0; } - if (!r) + else if (!r) { // Disconnect - RealDisconnect(); - return 0; + //RealDisconnect(); } + return r; } From 1c10d3a1bb9bde6a96c9e1789d7c758a12527f67 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Thu, 10 Jan 2013 16:16:49 -0600 Subject: [PATCH 2/4] better fix for linux wiimote disconnect crash --- Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp | 30 ++++++++++++------- Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp | 5 ++++ .../Core/Core/Src/HW/WiimoteReal/IOdarwin.mm | 5 ++++ .../Core/Src/HW/WiimoteReal/WiimoteReal.cpp | 2 +- .../Core/Src/HW/WiimoteReal/WiimoteReal.h | 4 +++ 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp index 1fb61f7715..f56c11e248 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp @@ -195,20 +195,30 @@ void Wiimote::RealDisconnect() if (m_wiimote_thread.joinable()) m_wiimote_thread.join(); - Host_ConnectWiimote(index, false); + Close(); +} - close(out_sock); - close(in_sock); +void Wiimote::Close() +{ + if (IsOpen()) + { + Host_ConnectWiimote(index, false); - out_sock = -1; - in_sock = -1; + close(out_sock); + close(in_sock); + + out_sock = -1; + in_sock = -1; + } +} + +bool Wiimote::IsOpen() const +{ + return out_sock != -1 && in_sock != -1; } int Wiimote::IORead(unsigned char *buf) { - if (!IsConnected()) - return 0; - // Block select for 1/2000th of a second timeval tv; tv.tv_sec = 0; @@ -239,7 +249,7 @@ int Wiimote::IORead(unsigned char *buf) // This can happen if the bluetooth dongle is disconnected ERROR_LOG(WIIMOTE, "Bluetooth appears to be disconnected. " "Wiimote %i will be disconnected.", index + 1); - //RealDisconnect(); + Close(); } return 0; @@ -247,7 +257,7 @@ int Wiimote::IORead(unsigned char *buf) else if (!r) { // Disconnect - //RealDisconnect(); + Close(); } return r; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp index c22e1972dc..cc05bc10e1 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IOWin.cpp @@ -294,6 +294,11 @@ void Wiimote::RealDisconnect() ResetEvent(&hid_overlap); } +bool Wiimote::IsOpen() const +{ + return IsConnected(); +} + int Wiimote::IORead(unsigned char* buf) { DWORD b, r; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm b/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm index 9ee39e5b92..64de880180 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm +++ b/Source/Core/Core/Src/HW/WiimoteReal/IOdarwin.mm @@ -227,6 +227,11 @@ void Wiimote::RealDisconnect() ichan = NULL; } +bool Wiimote::IsOpen() const +{ + return IsConnected(); +} + int Wiimote::IORead(unsigned char *buf) { int bytes; diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp index 204a1fc698..5ead30bdf8 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp @@ -312,7 +312,7 @@ void Wiimote::ThreadFunc() Rumble(); // main loop - while (IsConnected()) + while (IsOpen()) { #ifdef __APPLE__ while (Write()) {} diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h index cd1080903f..815c23d945 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h @@ -54,6 +54,7 @@ public: bool Write(); bool Connect(); bool IsConnected(); + bool IsOpen() const; void Disconnect(); void DisableDataReporting(); void Rumble(); @@ -72,6 +73,9 @@ public: bdaddr_t bdaddr; // Bluetooth address int out_sock; // Output socket int in_sock; // Input socket + + void Close(); + #elif defined(_WIN32) char devicepath[255]; // Unique wiimote reference //ULONGLONG btaddr; // Bluetooth address From cc0226254dabcbec36936a7c71d61d280168aecf Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Thu, 10 Jan 2013 16:23:47 -0600 Subject: [PATCH 3/4] Windows/OS X buildfix --- Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp | 2 +- Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp index 5ead30bdf8..1f9285f37e 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.cpp @@ -240,7 +240,7 @@ void Wiimote::Disconnect() DisableDataReporting(); } -bool Wiimote::IsConnected() +bool Wiimote::IsConnected() const { return m_connected; } diff --git a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h index 815c23d945..15c8044acd 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h +++ b/Source/Core/Core/Src/HW/WiimoteReal/WiimoteReal.h @@ -53,7 +53,7 @@ public: bool Read(); bool Write(); bool Connect(); - bool IsConnected(); + bool IsConnected() const; bool IsOpen() const; void Disconnect(); void DisableDataReporting(); From a693b847f0789e0bcf802ad2d5a61e42be23bfce Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Thu, 10 Jan 2013 16:29:16 -0600 Subject: [PATCH 4/4] buildfix! --- Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp b/Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp index 0b3d16454b..49afee8ad1 100644 --- a/Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp +++ b/Source/Core/Core/Src/HW/WiimoteReal/IODummy.cpp @@ -36,6 +36,11 @@ void Wiimote::RealDisconnect() return; } +bool Wiimote::IsOpen() const +{ + return IsConnected(); +} + int Wiimote::IORead(unsigned char* buf) { return 0;