From b30510d2c5a2ccd11f605f5e528508a97081006c Mon Sep 17 00:00:00 2001 From: mbalous Date: Sat, 28 Nov 2026 23:40:29 +0100 Subject: [PATCH 1/2] fix(usb): retry stalls on recently confirmed phones Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- platform/usb_loop.cpp | 38 ++++++++++++++++++++- tests/test_usb_loop.cpp | 75 +++++++++++++++++++++++++++++++++++++++++ tests/usb_fakes.hpp | 7 +++- 3 files changed, 118 insertions(+), 2 deletions(-) diff --git a/platform/usb_loop.cpp b/platform/usb_loop.cpp index 875fd9c..0e8cccd 100644 --- a/platform/usb_loop.cpp +++ b/platform/usb_loop.cpp @@ -26,6 +26,7 @@ const unsigned kMaxClaimTries = 10; // turns in a row its interface was held o const unsigned kMaxAskFailures = 3; // switch requests that failed half-way const unsigned kMaxUnanswered = 5; // per phone: switch requests taken, with no accessory back const unsigned kMaxProbeRetries = 3; // retries after a temporary failure of the initial AOA request +const uint32_t kKnownPhoneMs = 30000; const uint32_t kUnpluggedMs = 6000; // no phone on the bus for this long: it was unplugged const unsigned kReturnStepUs = 20000; @@ -36,6 +37,7 @@ class Loop { host_(host), bridge_(nullptr), bridgeStart_(0), + bridgeKnown_(false), quietSince_(0), quiet_(false), emptySince_(0), @@ -60,6 +62,18 @@ class Loop { ProbeRetry() : address(0), failures(0) {} }; + struct KnownPhone { + unsigned vendor, product; + uint32_t confirmedAt; + KnownPhone() : vendor(0), product(0), confirmedAt(0) {} + }; + + bool recentlyKnown(const UsbPhone& phone) const { + const std::map::const_iterator it = known_.find(phone.port); + return it != known_.end() && it->second.vendor == phone.vendor && it->second.product == phone.product && + monoMs() - it->second.confirmedAt < kKnownPhoneMs; + } + // Sleeps, but not past a stop request. void rest(unsigned ms) { for (unsigned waited = 0; waited < ms && !host_.stopRequested(); waited += 50) { @@ -145,6 +159,13 @@ class Loop { const bool left = bridge_->phoneLeft(); delete bridge_; bridge_ = nullptr; + if (bridgeKnown_) { + std::map::iterator known = known_.find(bridgePort_); + if (known != known_.end()) { + known->second.confirmedAt = monoMs(); + } + } + knownSwitch_.erase(bridgePort_); ELOG("UsbLoop", "USB session over after %u s (%s)", lasted / 1000, left ? "the phone left" : spoke ? "session ended" @@ -207,6 +228,9 @@ class Loop { } bridgePort_ = phone.port; bridgeStart_ = monoMs(); + const std::map::const_iterator known = known_.find(phone.port); + bridgeKnown_ = knownSwitch_.count(phone.port) && known != known_.end() && + monoMs() - known->second.confirmedAt < kKnownPhoneMs; ELOG("UsbLoop", "phone on %s (bus %u, address %u) in accessory mode: starting Android Auto", phone.port.c_str(), phone.bus, phone.address); host_.usbPhoneReady(fds[1]); @@ -214,6 +238,7 @@ class Loop { } bool resetPhone(const UsbPhone& phone) { + knownSwitch_.erase(phone.port); PortCounts& c = counts_[phone.port]; if (c.resets >= kMaxResets) { ELOG("UsbLoop", "phone on %s still not starting Android Auto after %u resets: unplug it and plug it again", @@ -252,7 +277,14 @@ class Loop { int probeResult = 0; const AccessorySwitch result = askAccessoryMode(*device, protocol, probeResult); delete device; - if (result == kSwitchProbeFailed) { + const bool knownStall = result == kSwitchNotAndroid && probeResult == -EPIPE && recentlyKnown(phone); + if (probeResult == 2 && protocol >= 1) { + KnownPhone& known = known_[phone.port]; + known.vendor = phone.vendor; + known.product = phone.product; + known.confirmedAt = monoMs(); + } + if (result == kSwitchProbeFailed || knownStall) { ProbeRetry& retry = probeRetries_[phone.port]; if (retry.address != phone.address) { retry.address = phone.address; @@ -291,6 +323,7 @@ class Loop { return false; } c.askFailures = 0; + knownSwitch_.insert(phone.port); ELOG("UsbLoop", "phone %04x:%04x on %s asked to start Android Auto (accessory protocol %u)", phone.vendor, phone.product, phone.port.c_str(), protocol); ours_.insert(phone.port); @@ -345,9 +378,11 @@ class Loop { const UsbLoopSetup& setup_; UsbHost& host_; std::map probeRetries_; + std::map known_; UsbBridge* bridge_; std::string bridgePort_; uint32_t bridgeStart_; + bool bridgeKnown_; // A phone's switch requests taken with no accessory back, by port: for the phone at that address. struct Unanswered { unsigned address, asks; @@ -360,6 +395,7 @@ class Loop { }; std::set ours_; // ports whose accessory this loop made, or reset + std::set knownSwitch_; // switches backed by a successful protocol response std::map leftAlone_; // port and address of the devices not to touch again std::map unanswered_; std::map counts_; diff --git a/tests/test_usb_loop.cpp b/tests/test_usb_loop.cpp index 56e0270..267d930 100644 --- a/tests/test_usb_loop.cpp +++ b/tests/test_usb_loop.cpp @@ -195,6 +195,77 @@ void probeRetriesResetOnUnplug() { run.end(); } +void endSpokenSession(Run& run, fake::Phone& phone) { + phone.say(std::vector(1, 0x42)); + uint8_t byte; + CHECK(recv(run.host.fd, &byte, 1, MSG_WAITALL) == 1); + run.host.endSession(); +} + +void knownPhoneStall(unsigned failures, bool succeeds) { + fake::Bus bus; + fake::Phone phone; + g_phones.assign(1, &phone); + phone.plug(bus, "1-1"); + Run run(bus); + CHECK(run.handed(1)); + phone.failProbes(failures, -EPIPE); + endSpokenSession(run, phone); + if (succeeds) { + CHECK(run.handed(2)); + CHECK(phone.count(&fake::Phone::protocolAsked) == failures + 2); + } else { + CHECK(fake::within(5000, [&]() { return phone.count(&fake::Phone::protocolAsked) == 5; })); + usleep(500000); + CHECK(phone.count(&fake::Phone::protocolAsked) == 5 && run.host.handed == 1); + } + run.end(); +} + +void differentPhoneStall() { + fake::Bus bus; + fake::Phone first, other; + other.address = 40; + other.vendor = 0x1234; + other.android = false; + g_phones.clear(); + g_phones.push_back(&first); + g_phones.push_back(&other); + first.plug(bus, "1-1"); + Run run(bus); + CHECK(run.handed(1)); + endSpokenSession(run, first); + other.plug(bus, "1-1"); + CHECK(fake::within(3000, [&]() { return other.count(&fake::Phone::protocolAsked) == 1; })); + usleep(500000); + CHECK(other.count(&fake::Phone::protocolAsked) == 1); + run.end(); +} + +void longSessionRenewsKnownPhone() { + fake::Bus bus; + fake::Phone phone; + g_phones.assign(1, &phone); + phone.plug(bus, "1-1"); + Run run(bus); + CHECK(run.handed(1)); + usleep(31000000); + phone.failProbes(1, -EPIPE); + endSpokenSession(run, phone); + CHECK(run.handed(2)); + CHECK(phone.count(&fake::Phone::protocolAsked) == 3); + endSpokenSession(run, phone); + bus.remove("1-1"); + usleep(32000000); + phone.failProbes(2, -EPIPE); + phone.address = 40; + phone.plug(bus, "1-1"); + CHECK(fake::within(3000, [&]() { return phone.count(&fake::Phone::protocolAsked) == 4; })); + usleep(500000); + CHECK(phone.count(&fake::Phone::protocolAsked) == 4 && run.host.handed == 2); + run.end(); +} + void onePortOnly() { fake::Bus bus; fake::Phone phone; @@ -332,6 +403,10 @@ int main() { probeRetriesAreBounded(); probeRetriesResetOnNewAddress(); probeRetriesResetOnUnplug(); + knownPhoneStall(2, true); + knownPhoneStall(20, false); + differentPhoneStall(); + longSessionRenewsKnownPhone(); onePortOnly(); heldInterface(); neverSwitches(); diff --git a/tests/usb_fakes.hpp b/tests/usb_fakes.hpp index 600ab02..59f45c6 100644 --- a/tests/usb_fakes.hpp +++ b/tests/usb_fakes.hpp @@ -28,7 +28,7 @@ #define CHECK(x) \ do { \ if (!(x)) { \ - printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); \ + fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); \ exit(1); \ } \ } while (0) @@ -194,6 +194,11 @@ struct Phone { this->*flag = value; notify(); } + void failProbes(unsigned count, int result) { + Lock lock(mu); + probeFailures = count; + probeFailureResult = result; + } unsigned count(unsigned Phone::*counter) { Lock lock(mu); return this->*counter; From 2cf2849f8e59f80ff5639ef87fd325f24c7600b6 Mon Sep 17 00:00:00 2001 From: mbalous Date: Sat, 28 Nov 2026 23:45:51 +0100 Subject: [PATCH 2/2] style(usb): format retry changes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- platform/usb_loop.cpp | 4 ++-- tests/usb_fakes.hpp | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/platform/usb_loop.cpp b/platform/usb_loop.cpp index 0e8cccd..1325669 100644 --- a/platform/usb_loop.cpp +++ b/platform/usb_loop.cpp @@ -27,7 +27,7 @@ const unsigned kMaxAskFailures = 3; // switch requests that failed half-way const unsigned kMaxUnanswered = 5; // per phone: switch requests taken, with no accessory back const unsigned kMaxProbeRetries = 3; // retries after a temporary failure of the initial AOA request const uint32_t kKnownPhoneMs = 30000; -const uint32_t kUnpluggedMs = 6000; // no phone on the bus for this long: it was unplugged +const uint32_t kUnpluggedMs = 6000; // no phone on the bus for this long: it was unplugged const unsigned kReturnStepUs = 20000; class Loop { @@ -383,6 +383,7 @@ class Loop { std::string bridgePort_; uint32_t bridgeStart_; bool bridgeKnown_; + std::set knownSwitch_; // switches backed by a successful protocol response // A phone's switch requests taken with no accessory back, by port: for the phone at that address. struct Unanswered { unsigned address, asks; @@ -395,7 +396,6 @@ class Loop { }; std::set ours_; // ports whose accessory this loop made, or reset - std::set knownSwitch_; // switches backed by a successful protocol response std::map leftAlone_; // port and address of the devices not to touch again std::map unanswered_; std::map counts_; diff --git a/tests/usb_fakes.hpp b/tests/usb_fakes.hpp index 59f45c6..a5ebc11 100644 --- a/tests/usb_fakes.hpp +++ b/tests/usb_fakes.hpp @@ -25,12 +25,12 @@ #include "usb_device.hpp" -#define CHECK(x) \ - do { \ - if (!(x)) { \ +#define CHECK(x) \ + do { \ + if (!(x)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); \ - exit(1); \ - } \ + exit(1); \ + } \ } while (0) namespace fake {