Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions platform/usb_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ 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 kUnpluggedMs = 6000; // no phone on the bus for this long: it was unplugged
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;

class Loop {
Expand All @@ -36,6 +37,7 @@ class Loop {
host_(host),
bridge_(nullptr),
bridgeStart_(0),
bridgeKnown_(false),
quietSince_(0),
quiet_(false),
emptySince_(0),
Expand All @@ -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<std::string, KnownPhone>::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) {
Expand Down Expand Up @@ -185,6 +199,13 @@ class Loop {
const bool gone = bridge_->phoneGone();
delete bridge_;
bridge_ = nullptr;
if (bridgeKnown_) {
std::map<std::string, KnownPhone>::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,
gone ? "USB device disappeared"
: left ? "USB transport ended"
Expand Down Expand Up @@ -248,13 +269,17 @@ class Loop {
}
bridgePort_ = phone.port;
bridgeStart_ = monoMs();
const std::map<std::string, KnownPhone>::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]);
return false;
}

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",
Expand Down Expand Up @@ -303,7 +328,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;
Expand Down Expand Up @@ -346,6 +378,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);
Expand Down Expand Up @@ -400,9 +433,12 @@ class Loop {
const UsbLoopSetup& setup_;
UsbHost& host_;
std::map<std::string, ProbeRetry> probeRetries_;
std::map<std::string, KnownPhone> known_;
UsbBridge* bridge_;
std::string bridgePort_;
uint32_t bridgeStart_;
bool bridgeKnown_;
std::set<std::string> 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;
Expand Down
75 changes: 75 additions & 0 deletions tests/test_usb_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,77 @@ void probeRetriesResetOnUnplug() {
run.end();
}

void endSpokenSession(Run& run, fake::Phone& phone) {
phone.say(std::vector<uint8_t>(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;
Expand Down Expand Up @@ -383,6 +454,10 @@ int main() {
probeRetriesAreBounded();
probeRetriesResetOnNewAddress();
probeRetriesResetOnUnplug();
knownPhoneStall(2, true);
knownPhoneStall(20, false);
differentPhoneStall();
longSessionRenewsKnownPhone();
onePortOnly();
heldInterface();
resetAddressDisappears();
Expand Down
5 changes: 5 additions & 0 deletions tests/usb_fakes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,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;
Expand Down