Fix a hang in linux with the wiimote new plugin caused by a critical section not being exited. Also removed the original wiiuse_find function and replaced it with my wiiuse_find_more function with a little tweak to speed up wiimote discovery.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6007 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Glenn Rice 2010-07-30 18:55:36 +00:00
parent 6ed3357066
commit 87043879f8
3 changed files with 138 additions and 226 deletions

View File

@ -22,17 +22,8 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* $Header$
*
*/ */
/**
* @file
* @brief Handles device I/O for *nix.
*/
#ifndef WIN32
#include <sys/time.h> #include <sys/time.h>
#include <errno.h> #include <errno.h>
@ -48,135 +39,70 @@
#include <bluetooth/hci_lib.h> #include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h> #include <bluetooth/l2cap.h>
#include "definitions.h"
#include "wiiuse_internal.h" #include "wiiuse_internal.h"
static int wiiuse_connect_single(struct wiimote_t* wm, char* address); static int wiiuse_connect_single(struct wiimote_t* wm, char* address);
/** // Find a wiimote or wiimotes.
* @brief Find a wiimote or wiimotes. // Does not replace already found wiimotes even if they are disconnected.
* // wm An array of wiimote_t structures.
* @param wm An array of wiimote_t structures. // max_wiimotes The number of wiimote structures in wm.
* @param max_wiimotes The number of wiimote structures in \a wm. // timeout The number of seconds before the search times out.
* @param timeout The number of seconds before the search times out. // Returns the total number of found wiimotes.
* // This function will only look for wiimote devices.
* @return The number of wiimotes found. // When a device is found the address in the structures will be set.
* // You can then call wiimote_connect() to connect to the found devices.
* @see wiimote_connect() int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout)
* {
* This function will only look for wiimote devices. \n
* When a device is found the address in the structures will be set. \n
* You can then call wiimote_connect() to connect to the found \n
* devices.
*/
int wiiuse_find(struct wiimote_t** wm, int max_wiimotes, int timeout) {
int device_id; int device_id;
int device_sock; int device_sock;
int found_devices; int found_devices;
int found_wiimotes; int found_wiimotes = 0;
int i;
/* reset all wiimote bluetooth device addresses */ // Count the number of already found wiimotes
for (found_wiimotes = 0; found_wiimotes < max_wiimotes; ++found_wiimotes) for (i = 0; i < max_wiimotes; ++i)
wm[found_wiimotes]->bdaddr = *BDADDR_ANY; {
found_wiimotes = 0; if (WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_DEV_FOUND))
found_wiimotes++;
}
/* get the id of the first bluetooth device. */ // get the id of the first bluetooth device.
device_id = hci_get_route(NULL); device_id = hci_get_route(NULL);
if (device_id < 0) { if (device_id < 0)
{
perror("hci_get_route"); perror("hci_get_route");
return 0; return 0;
} }
/* create a socket to the device */ // create a socket to the device
device_sock = hci_open_dev(device_id); device_sock = hci_open_dev(device_id);
if (device_sock < 0) { if (device_sock < 0)
{
perror("hci_open_dev"); perror("hci_open_dev");
return 0; return 0;
} }
int try_num = 0;
while ((try_num < timeout) && (found_wiimotes < max_wiimotes))
{
inquiry_info scan_info_arr[128]; inquiry_info scan_info_arr[128];
inquiry_info* scan_info = scan_info_arr; inquiry_info* scan_info = scan_info_arr;
memset(&scan_info_arr, 0, sizeof(scan_info_arr)); memset(&scan_info_arr, 0, sizeof(scan_info_arr));
/* scan for bluetooth devices for 'timeout' seconds */ // scan for bluetooth devices for ~one second
found_devices = hci_inquiry(device_id, timeout, 128, NULL, &scan_info, IREQ_CACHE_FLUSH); found_devices = hci_inquiry(device_id, 1, 128, NULL, &scan_info, IREQ_CACHE_FLUSH);
if (found_devices < 0) { if (found_devices < 0)
{
perror("hci_inquiry"); perror("hci_inquiry");
return 0; return 0;
} }
WIIUSE_INFO("Found %i bluetooth device(s).", found_devices); WIIUSE_INFO("Found %i bluetooth device(s).", found_devices);
int i = 0; // display discovered devices
for (i = 0; (i < found_devices) && (found_wiimotes < max_wiimotes); ++i)
/* display discovered devices */
for (; (i < found_devices) && (found_wiimotes < max_wiimotes); ++i) {
if ((scan_info[i].dev_class[0] == WM_DEV_CLASS_0) &&
(scan_info[i].dev_class[1] == WM_DEV_CLASS_1) &&
(scan_info[i].dev_class[2] == WM_DEV_CLASS_2))
{ {
/* found a device */
ba2str(&scan_info[i].bdaddr, wm[found_wiimotes]->bdaddr_str);
WIIUSE_INFO("Found wiimote (%s) [id %i].", wm[found_wiimotes]->bdaddr_str, wm[found_wiimotes]->unid);
wm[found_wiimotes]->bdaddr = scan_info[i].bdaddr;
WIIMOTE_ENABLE_STATE(wm[found_wiimotes], WIIMOTE_STATE_DEV_FOUND);
++found_wiimotes;
}
}
close(device_sock);
return found_wiimotes;
}
// Scan for more wiimotes and add them to the wm structure.
// Does not replace already found wiimotes even if they are disconnected.
// Returns the total number of found wiimotes.
// It would probably be safe to replace wiiuse_find with this function. The only thing
// it does that this does not is reset the bdaddr fields.
int wiiuse_find_more(struct wiimote_t** wm, int max_wiimotes, int timeout) {
int device_id;
int device_sock;
int found_devices;
int found_wiimotes = 0;
int i;
// Count the number of already found wiimotes
for (i = 0; i < max_wiimotes; ++i) {
if (WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_DEV_FOUND))
found_wiimotes++;
}
// get the id of the first bluetooth device.
device_id = hci_get_route(NULL);
if (device_id < 0) {
perror("hci_get_route");
return 0;
}
// create a socket to the device
device_sock = hci_open_dev(device_id);
if (device_sock < 0) {
perror("hci_open_dev");
return 0;
}
inquiry_info scan_info_arr[128];
inquiry_info* scan_info = scan_info_arr;
memset(&scan_info_arr, 0, sizeof(scan_info_arr));
// scan for bluetooth devices for timeout seconds
found_devices = hci_inquiry(device_id, timeout, 128, NULL, &scan_info, IREQ_CACHE_FLUSH);
if (found_devices < 0) {
perror("hci_inquiry");
return 0;
}
WIIUSE_INFO("Found %i bluetooth device(s).", found_devices);
// display discovered devices
for (i = 0; (i < found_devices) && (found_wiimotes < max_wiimotes); ++i) {
if ((scan_info[i].dev_class[0] == WM_DEV_CLASS_0) && if ((scan_info[i].dev_class[0] == WM_DEV_CLASS_0) &&
(scan_info[i].dev_class[1] == WM_DEV_CLASS_1) && (scan_info[i].dev_class[1] == WM_DEV_CLASS_1) &&
(scan_info[i].dev_class[2] == WM_DEV_CLASS_2)) (scan_info[i].dev_class[2] == WM_DEV_CLASS_2))
@ -196,42 +122,38 @@ for (i = 0; (i < found_devices) && (found_wiimotes < max_wiimotes); ++i) {
// found a new device // found a new device
ba2str(&scan_info[i].bdaddr, wm[found_wiimotes]->bdaddr_str); ba2str(&scan_info[i].bdaddr, wm[found_wiimotes]->bdaddr_str);
WIIUSE_INFO("Found wiimote (%s) [id %i].", wm[found_wiimotes]->bdaddr_str, wm[found_wiimotes]->unid); WIIUSE_INFO("Found wiimote (%s) [id %i].",
wm[found_wiimotes]->bdaddr_str, wm[found_wiimotes]->unid);
wm[found_wiimotes]->bdaddr = scan_info[i].bdaddr; wm[found_wiimotes]->bdaddr = scan_info[i].bdaddr;
WIIMOTE_ENABLE_STATE(wm[found_wiimotes], WIIMOTE_STATE_DEV_FOUND); WIIMOTE_ENABLE_STATE(wm[found_wiimotes], WIIMOTE_STATE_DEV_FOUND);
++found_wiimotes; ++found_wiimotes;
} }
} }
}
try_num++;
}
close(device_sock);
return found_wiimotes;
} }
close(device_sock); // Connect to a wiimote or wiimotes once an address is known.
return found_wiimotes; // wm An array of wiimote_t structures.
} // wiimotes The number of wiimote structures in wm.
// Return the number of wiimotes that successfully connected.
/** // Connect to a number of wiimotes when the address is already set
* @brief Connect to a wiimote or wiimotes once an address is known. // in the wiimote_t structures. These addresses are normally set
* // by the wiiuse_find() function, but can also be set manually.
* @param wm An array of wiimote_t structures. int wiiuse_connect(struct wiimote_t** wm, int wiimotes)
* @param wiimotes The number of wiimote structures in \a wm. {
*
* @return The number of wiimotes that successfully connected.
*
* @see wiiuse_find()
* @see wiiuse_connect_single()
* @see wiiuse_disconnect()
*
* Connect to a number of wiimotes when the address is already set
* in the wiimote_t structures. These addresses are normally set
* by the wiiuse_find() function, but can also be set manually.
*/
int wiiuse_connect(struct wiimote_t** wm, int wiimotes) {
int connected = 0; int connected = 0;
int i = 0; int i = 0;
for (; i < wiimotes; ++i) { for (; i < wiimotes; ++i)
{
if (!WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_DEV_FOUND)) if (!WIIMOTE_IS_SET(wm[i], WIIMOTE_STATE_DEV_FOUND))
/* if the device address is not set, skip it */ // if the device address is not set, skip it
continue; continue;
if (wiiuse_connect_single(wm[i], NULL)) if (wiiuse_connect_single(wm[i], NULL))
@ -241,17 +163,13 @@ int wiiuse_connect(struct wiimote_t** wm, int wiimotes) {
return connected; return connected;
} }
// Connect to a wiimote with a known address.
/** // wm Pointer to a wiimote_t structure.
* @brief Connect to a wiimote with a known address. // address The address of the device to connect to.
* // If NULL, use the address in the struct set by wiiuse_find().
* @param wm Pointer to a wiimote_t structure. // Return 1 on success, 0 on failure
* @param address The address of the device to connect to. static int wiiuse_connect_single(struct wiimote_t* wm, char* address)
* If NULL, use the address in the struct set by wiiuse_find(). {
*
* @return 1 on success, 0 on failure
*/
static int wiiuse_connect_single(struct wiimote_t* wm, char* address) {
struct sockaddr_l2 addr; struct sockaddr_l2 addr;
if (!wm || WIIMOTE_IS_CONNECTED(wm)) if (!wm || WIIMOTE_IS_CONNECTED(wm))
@ -260,20 +178,18 @@ static int wiiuse_connect_single(struct wiimote_t* wm, char* address) {
addr.l2_family = AF_BLUETOOTH; addr.l2_family = AF_BLUETOOTH;
bdaddr_t *bdaddr = &wm->bdaddr; bdaddr_t *bdaddr = &wm->bdaddr;
if (address) if (address)
/* use provided address */ // use provided address
str2ba(address, &addr.l2_bdaddr); str2ba(address, &addr.l2_bdaddr);
else else
{ {
if (bacmp(bdaddr, BDADDR_ANY) == 0) if (bacmp(bdaddr, BDADDR_ANY) == 0)
return 0; return 0;
/* use address of device discovered */ // use address of device discovered
addr.l2_bdaddr = *bdaddr; addr.l2_bdaddr = *bdaddr;
} }
/* // OUTPUT CHANNEL
* OUTPUT CHANNEL
*/
wm->out_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); wm->out_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
if (wm->out_sock == -1) if (wm->out_sock == -1)
return 0; return 0;
@ -281,17 +197,17 @@ static int wiiuse_connect_single(struct wiimote_t* wm, char* address) {
addr.l2_cid = 0; addr.l2_cid = 0;
addr.l2_psm = htobs(WM_OUTPUT_CHANNEL); addr.l2_psm = htobs(WM_OUTPUT_CHANNEL);
/* connect to wiimote */ // connect to wiimote
if (connect(wm->out_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { if (connect(wm->out_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
perror("connect() output sock"); perror("connect() output sock");
return 0; return 0;
} }
/* // INPUT CHANNEL
* INPUT CHANNEL
*/
wm->in_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); wm->in_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
if (wm->in_sock == -1) { if (wm->in_sock == -1)
{
close(wm->out_sock); close(wm->out_sock);
wm->out_sock = -1; wm->out_sock = -1;
return 0; return 0;
@ -299,8 +215,9 @@ static int wiiuse_connect_single(struct wiimote_t* wm, char* address) {
addr.l2_psm = htobs(WM_INPUT_CHANNEL); addr.l2_psm = htobs(WM_INPUT_CHANNEL);
/* connect to wiimote */ // connect to wiimote
if (connect(wm->in_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { if (connect(wm->in_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
perror("connect() interrupt sock"); perror("connect() interrupt sock");
close(wm->out_sock); close(wm->out_sock);
wm->out_sock = -1; wm->out_sock = -1;
@ -308,7 +225,7 @@ static int wiiuse_connect_single(struct wiimote_t* wm, char* address) {
} }
WIIUSE_INFO("Connected to wiimote [id %i].", wm->unid); WIIUSE_INFO("Connected to wiimote [id %i].", wm->unid);
/* do the handshake */ // do the handshake
WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_CONNECTED); WIIMOTE_ENABLE_STATE(wm, WIIMOTE_STATE_CONNECTED);
wiiuse_set_report_type(wm); wiiuse_set_report_type(wm);
@ -316,17 +233,11 @@ static int wiiuse_connect_single(struct wiimote_t* wm, char* address) {
return 1; return 1;
} }
// Disconnect a wiimote.
/** // wm Pointer to a wiimote_t structure.
* @brief Disconnect a wiimote. // Note that this will not free the wiimote structure.
* void wiiuse_disconnect(struct wiimote_t* wm)
* @param wm Pointer to a wiimote_t structure. {
*
* @see wiiuse_connect()
*
* Note that this will not free the wiimote structure.
*/
void wiiuse_disconnect(struct wiimote_t* wm) {
if (!wm || !WIIMOTE_IS_CONNECTED(wm)) if (!wm || !WIIMOTE_IS_CONNECTED(wm))
return; return;
@ -340,49 +251,55 @@ void wiiuse_disconnect(struct wiimote_t* wm) {
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_CONNECTED); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_CONNECTED);
WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE); WIIMOTE_DISABLE_STATE(wm, WIIMOTE_STATE_HANDSHAKE);
} }
int wiiuse_io_read(struct wiimote_t* wm) {
int wiiuse_io_read(struct wiimote_t* wm)
{
struct timeval tv; struct timeval tv;
fd_set fds; fd_set fds;
int r; int r;
if (!wm) if (!wm)
return 0; return 0;
/* block select() for 1/2000th of a second */ // block select() for 1/2000th of a second
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = wm->timeout * 1000; // timeout is in Milliseconds tv_usec is in Microseconds! tv.tv_usec = wm->timeout * 1000; // timeout is in Milliseconds tv_usec is in Microseconds!
FD_ZERO(&fds); FD_ZERO(&fds);
/* only poll it if it is connected */ // only poll it if it is connected
if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED)) { if (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED))
{
FD_SET(wm->in_sock, &fds); FD_SET(wm->in_sock, &fds);
//highest_fd = wm[i]->in_sock; //highest_fd = wm[i]->in_sock;
} }
else else
/* nothing to poll */ // nothing to poll
return 0; return 0;
if (select(wm->in_sock + 1, &fds, NULL, NULL, &tv) == -1) { if (select(wm->in_sock + 1, &fds, NULL, NULL, &tv) == -1)
{
WIIUSE_ERROR("Unable to select() the wiimote interrupt socket(s)."); WIIUSE_ERROR("Unable to select() the wiimote interrupt socket(s).");
perror("Error Details"); perror("Error Details");
return 0; return 0;
} }
/* if this wiimote is not connected, skip it */ // if this wiimote is not connected, skip it
if (!WIIMOTE_IS_CONNECTED(wm)) if (!WIIMOTE_IS_CONNECTED(wm))
return 0; return 0;
if (FD_ISSET(wm->in_sock, &fds)) if (FD_ISSET(wm->in_sock, &fds))
{ {
//memset(wm->event_buf, 0, sizeof(wm->event_buf)); //memset(wm->event_buf, 0, sizeof(wm->event_buf));
/* read the pending message into the buffer */ // read the pending message into the buffer
r = read(wm->in_sock, wm->event_buf, sizeof(wm->event_buf)); r = read(wm->in_sock, wm->event_buf, sizeof(wm->event_buf));
if (r == -1) { if (r == -1)
/* error reading data */ {
// error reading data
WIIUSE_ERROR("Receiving wiimote data (id %i).", wm->unid); WIIUSE_ERROR("Receiving wiimote data (id %i).", wm->unid);
perror("Error Details"); perror("Error Details");
if (errno == ENOTCONN) { if (errno == ENOTCONN)
/* this can happen if the bluetooth dongle is disconnected */ {
// this can happen if the bluetooth dongle is disconnected
WIIUSE_ERROR("Bluetooth appears to be disconnected. Wiimote unid %i will be disconnected.", wm->unid); WIIUSE_ERROR("Bluetooth appears to be disconnected. Wiimote unid %i will be disconnected.", wm->unid);
wiiuse_disconnect(wm); wiiuse_disconnect(wm);
wm->event = WIIUSE_UNEXPECTED_DISCONNECT; wm->event = WIIUSE_UNEXPECTED_DISCONNECT;
@ -390,8 +307,9 @@ int wiiuse_io_read(struct wiimote_t* wm) {
return 0; return 0;
} }
if (!r) { if (!r)
/* remote disconnect */ {
// remote disconnect
wiiuse_disconnected(wm); wiiuse_disconnected(wm);
return 0; return 0;
} }
@ -401,14 +319,9 @@ int wiiuse_io_read(struct wiimote_t* wm) {
return 0; return 0;
} }
int wiiuse_io_write(struct wiimote_t* wm, byte* buf, int len) int wiiuse_io_write(struct wiimote_t* wm, byte* buf, int len)
{ {
if(buf[0] == 0xa2) if(buf[0] == 0xa2)
buf[0] = 0x52; // May not be needed. Will be changing/correcting in the next few revisions buf[0] = 0x52; // May not be needed. Will be changing/correcting in the next few revisions
return write(wm->out_sock, buf, len); return write(wm->out_sock, buf, len);
} }
#endif /* ifndef WIN32 */

View File

@ -225,12 +225,7 @@ WIIUSE_EXPORT extern int wiiuse_check_system_notification(unsigned int nMsg, WPA
WIIUSE_EXPORT extern int wiiuse_register_system_notification(HWND hwnd); WIIUSE_EXPORT extern int wiiuse_register_system_notification(HWND hwnd);
#endif #endif
#ifdef __linux__
int wiiuse_find_more(struct wiimote_t** wm, int max_wiimotes, int timeout);
#endif
/* ir.c */ /* ir.c */
WIIUSE_EXPORT extern void wiiuse_set_ir_sensitivity(struct wiimote_t* wm, int level); WIIUSE_EXPORT extern void wiiuse_set_ir_sensitivity(struct wiimote_t* wm, int level);
/* io.c */ /* io.c */

View File

@ -389,6 +389,7 @@ void Refresh() // this gets called from the GUI thread
// make sure real wiimotes have been initialized // make sure real wiimotes have been initialized
if (!g_real_wiimotes_initialized) if (!g_real_wiimotes_initialized)
{ {
g_refresh_critsec.Leave();
Initialize(); Initialize();
return; return;
} }
@ -401,10 +402,13 @@ void Refresh() // this gets called from the GUI thread
// don't scan for wiimotes if we don't want any more // don't scan for wiimotes if we don't want any more
if (wanted_wiimotes <= g_wiimotes_found) if (wanted_wiimotes <= g_wiimotes_found)
{
g_refresh_critsec.Leave();
return; return;
}
// scan for wiimotes // scan for wiimotes
unsigned int num_wiimotes = wiiuse_find_more(g_wiimotes_from_wiiuse, wanted_wiimotes, 5); unsigned int num_wiimotes = wiiuse_find(g_wiimotes_from_wiiuse, wanted_wiimotes, 5);
DEBUG_LOG(WIIMOTE, "Found %i Real Wiimotes, %i wanted", num_wiimotes, wanted_wiimotes); DEBUG_LOG(WIIMOTE, "Found %i Real Wiimotes, %i wanted", num_wiimotes, wanted_wiimotes);