Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry opening of wiimote channels on initial failure #4940

Merged
merged 1 commit into from
Jun 19, 2017

Conversation

sjnewbury
Copy link
Contributor

It's not uncommon on some Bluetooth controllers for a
peripheral device to be "present" before it's "ready".
There is a window where connection can fail because the
device communication didn't succeed initially, but
will always work on retry after a short delay.

I'm not the original author of this patch and I can't
remember where I found it. I can confirm that it
works perfectly for me though...

@JosJuice
Copy link
Member

Please run the change through clang-format (or fix the lint errors manually, as shown here: https://buildbot.dolphin-emu.org/builders/lint/builds/5017/steps/shell/logs/stdio)

@sjnewbury
Copy link
Contributor Author

@JosJuice Oops! Okay.

@BhaaLseN
Copy link
Member

Can you update that comment to include a brief summary of what you added to your commit message? We can see that you wait and try again from what is done; but it isn't obvious why this is done.

@sjnewbury
Copy link
Contributor Author

@BhaaLseN I'm not sure what I can add. There seems to be a race condition between a peripheral device connecting to the bluetooth controller and it being ready to use. It's very short and it depends upon the controller, some appear to connect synchronously and block until the device is ready, others report the device upon discovery but do not allow communication straight away. I don't know which is the correct behaviour, or whether it depends on the peripheral, controller or both. Anyway, Dolphin waits for a remote to appear and immediately attempts to open the communication channels, this can fail because the device isn't ready yet, delay, try again, and it works.

I'm willing to bet this actually makes wiimotes work with most bluetooth controllers which otherwise fail.

@BhaaLseN
Copy link
Member

When in doubt, just add that part of your commit message. Otherwise nobody knows why you do the sleep there, and might remove it thinking it has no purpose.

@sjnewbury
Copy link
Contributor Author

@BhaaLseN I've updated the commit message based on my reply above.

@Parlane
Copy link
Member

Parlane commented Feb 23, 2017

@dolphin-emu-bot rebuild

@PatrickFerry
Copy link
Member

There are other (unlikely) chances the device is busy at random moments after this initial race condition.
Could you loop around try to reconnect 2-3 times, it would be more robust for the chaotic and fragmented world of bluetooth adaptors/devices/driver stacks?

@sjnewbury
Copy link
Contributor Author

sjnewbury commented Feb 24, 2017 via email

@sjnewbury
Copy link
Contributor Author

Updated patch with loops instead of in-line retries.

@sjnewbury
Copy link
Contributor Author

@ZephyrSurfer I've put in multiple retries as you suggested.


// Output channel
addr.l2_psm = htobs(WM_OUTPUT_CHANNEL);
if ((m_cmd_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
connect(m_cmd_sock, (sockaddr*)&addr, sizeof(addr)) < 0)
retry = 0;

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@PatrickFerry
Copy link
Member

@sjnewbury It seems the original patch is from here

Could you bump the attempts up one more time?

@sjnewbury
Copy link
Contributor Author

@ZephyrSurfer I'll put that reference into the commit message.

@sjnewbury
Copy link
Contributor Author

@ZephyrSurfer As requested, one more attempt.

@leoetlino
Copy link
Member

Looks like this will need a rebase + some (quick and easy) lint fixes.

@sjnewbury
Copy link
Contributor Author

Sorry for the delay, I've just been distracted with other things. I'll take a look now.

m_cmd_sock = -1;
return false;
}
retry++;

This comment was marked as off-topic.

return false;
}
retry++;
sleep(1);

This comment was marked as off-topic.

This comment was marked as off-topic.

@sjnewbury
Copy link
Contributor Author

There's no point in retrying opening of the socket, it either works or it doesn't, so I've changed the code to just try opening the sockets, if it succeeds, then retry the connections.

Will push after testing...

connect(m_cmd_sock, (sockaddr*)&addr, sizeof(addr)) < 0)
if (m_cmd_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP))
{
retry = 0;

This comment was marked as off-topic.

This comment was marked as off-topic.

connect(m_int_sock, (sockaddr*)&addr, sizeof(addr)) < 0)
if (m_int_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP))
{
retry = 0;

This comment was marked as off-topic.

This comment was marked as off-topic.

@sjnewbury
Copy link
Contributor Author

Looks like I need to rebase again ...

There seems to be a race condition between a peripheral device
connecting to the bluetooth controller and it being ready to use.
It's very short and it depends upon the controller, some appear to
connect synchronously and block until the device is ready, others
report the device upon discovery but do not allow communication straight
away. I don't know which is the correct behaviour, or whether it depends
on the peripheral, controller or both. Anyway, Dolphin waits for a
remote to appear and immediately attempts to open the communication
channels, this can fail because the device isn't ready yet, delay, try
again, and it works.

There are other (unlikely) chances the device is busy at random
moments after this initial race condition so it loops around try to
reconnect.

This was inspired by an earlier patch, see here:
https://bugs.dolphin-emu.org/issues/5997#note-20

I can confirm that it works perfectly for me on a bluetooth
controller where otherwise it's impossible to connect (Dell 380
Bluetooth 4.0).
@sjnewbury
Copy link
Contributor Author

Rebased

@leoetlino leoetlino merged commit 1be10e7 into dolphin-emu:master Jun 19, 2017
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

7 participants