Skip to content
Open
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
48 changes: 48 additions & 0 deletions tests/test_usb_accessory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
// Android phones on a fake USB bus: which devices are asked, the accessory's endpoints, the switch requests, the
// setting file.
#include <atomic>
#include <thread>

#include "usb_accessory.hpp"
#include "usb_fakes.hpp"

Expand Down Expand Up @@ -68,6 +71,49 @@ void endpoints() {
CHECK(!evo::readAccessoryEndpoints(bus.root.c_str(), "1-1", found));
}

void concurrentBusChanges() {
fake::Bus bus;
std::atomic<bool> start(false);
std::thread switching([&]() {
while (!start.load()) {
std::this_thread::yield();
}
for (unsigned i = 0; i < 200; ++i) {
bus.accessory("1-1", 1, 3);
}
});
std::thread unplugging([&]() {
while (!start.load()) {
std::this_thread::yield();
}
for (unsigned i = 0; i < 200; ++i) {
bus.remove("1-1");
bus.device("1-1", 1, 2, 0x04e8, 0x6860, 0, 6);
}
});
start = true;
switching.join();
unplugging.join();
bus.accessory("1-1", 1, 3);
evo::AccessoryEndpoints found;
CHECK(evo::readAccessoryEndpoints(bus.root.c_str(), "1-1", found));
}

void oldDeviceCannotResetReplacement() {
fake::Bus bus;
fake::Phone oldPhone, replacement;
oldPhone.plug(bus, "1-1", true);
fake::Device oldDevice(oldPhone);
replacement.address = 40;
replacement.plug(bus, "1-1", true);
uint8_t protocol[2];
CHECK(oldDevice.control(0xc0, 51, 0, 0, protocol, sizeof(protocol), 0) == -ENODEV);
CHECK(!oldDevice.claimInterface(0) && errno == ENODEV);
CHECK(!oldDevice.reset() && errno == ENODEV);
evo::UsbPhone found;
CHECK(evo::readUsbDevice(bus.root.c_str(), "1-1", found) && found.address == 40 && found.accessory);
}

void switchRequests() {
fake::Phone phone;
{
Expand Down Expand Up @@ -145,6 +191,8 @@ int main() {
portNames();
whoIsAsked();
endpoints();
concurrentBusChanges();
oldDeviceCannotResetReplacement();
switchRequests();
settingFile();
puts("PASS USB accessory: port names, devices asked and left alone, endpoints, switch requests, setting file");
Expand Down
13 changes: 9 additions & 4 deletions tests/test_usb_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ evo::UsbDevice* openFake(unsigned bus, unsigned address) {
struct Host : evo::UsbHost {
std::atomic<bool> stop, busy;
std::atomic<int> fd;
std::atomic<unsigned> handed;
Host() : stop(false), busy(false), fd(-1), handed(0) {}
std::atomic<unsigned> handed, turns;
Host() : stop(false), busy(false), fd(-1), handed(0), turns(0) {}
bool stopRequested() override { return stop; }
bool projectionBusy() override { return busy; }
bool projectionBusy() override {
++turns;
return busy;
}
void usbPhoneReady(int socket) override {
fd = socket;
busy = true;
Expand Down Expand Up @@ -393,7 +396,9 @@ void quickReplugAfterResetLimit() {
held.plug(bus, "1-1");
Run run(bus);
CHECK(fake::within(10000, [&]() { return held.count(&fake::Phone::claims) == 40; }));
usleep(300000); // the last held interface reaches the reset limit
// The next turn records the reset limit; the turn after that confirms it completed.
const unsigned turns = run.host.turns;
CHECK(fake::within(4000, [&]() { return run.host.turns >= turns + 2; }));
CHECK(held.count(&fake::Phone::resets) == 3 && run.host.handed == 0);
const unsigned opens = held.count(&fake::Phone::opens);
usleep(300000);
Expand Down
49 changes: 49 additions & 0 deletions tests/usb_fakes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <cerrno>
#include <deque>
#include <mutex>
#include <set>
#include <string>
#include <vector>
Expand Down Expand Up @@ -48,6 +49,8 @@ inline uint32_t nowMs() {
// The kernel's list of USB devices, in a temporary folder.
struct Bus {
std::string root;
// Re-enumerations may nest operations, but must not delete another writer's directories.
std::recursive_mutex mu;

Bus() {
char path[] = "/tmp/entry-aad-usb-XXXXXX";
Expand All @@ -57,6 +60,7 @@ struct Bus {
~Bus() { nftw(root.c_str(), removeEntry, 8, FTW_DEPTH | FTW_PHYS); }

void put(const std::string& folder, const char* name, const std::string& text) {
std::lock_guard<std::recursive_mutex> lock(mu);
const std::string path = root + "/" + folder;
mkdir(path.c_str(), 0700);
FILE* f = fopen((path + "/" + name).c_str(), "w");
Expand All @@ -69,8 +73,25 @@ struct Bus {
snprintf(text, sizeof(text), format, value);
return text;
}
bool hasDevice(const std::string& port, unsigned bus, unsigned address) {
std::lock_guard<std::recursive_mutex> lock(mu);
const std::string path = root + "/" + port;
FILE* busFile = fopen((path + "/busnum").c_str(), "r");
FILE* addressFile = fopen((path + "/devnum").c_str(), "r");
unsigned foundBus = 0, foundAddress = 0;
const bool found = busFile && addressFile && fscanf(busFile, "%u", &foundBus) == 1 &&
fscanf(addressFile, "%u", &foundAddress) == 1;
if (busFile) {
fclose(busFile);
}
if (addressFile) {
fclose(addressFile);
}
return found && foundBus == bus && foundAddress == address;
}
// Unplugged: the device and its interfaces go.
void remove(const std::string& port) {
std::lock_guard<std::recursive_mutex> lock(mu);
std::vector<std::string> gone;
DIR* dir = opendir(root.c_str());
CHECK(dir != nullptr);
Expand All @@ -88,6 +109,7 @@ struct Bus {
// A device with one interface of that class.
void device(const std::string& port, unsigned bus, unsigned address, unsigned vendor, unsigned product,
unsigned deviceClass, unsigned interfaceClass) {
std::lock_guard<std::recursive_mutex> lock(mu);
remove(port);
put(port, "busnum", number("%u", bus));
put(port, "devnum", number("%u", address));
Expand All @@ -99,6 +121,7 @@ struct Bus {
}
// A phone in accessory mode: interface 0 with its two bulk endpoints, as the kernel shows them.
void accessory(const std::string& port, unsigned bus, unsigned address) {
std::lock_guard<std::recursive_mutex> lock(mu);
device(port, bus, address, 0x18d1, 0x2d01, 0, 0xff);
const std::string interface = port + ":1.0";
put(interface, "bInterfaceSubClass", "ff");
Expand Down Expand Up @@ -224,11 +247,16 @@ class Device : public evo::UsbDevice {
public:
explicit Device(Phone& phone) : phone_(phone) {
Lock lock(phone_.mu);
address_ = phone_.address;
++phone_.opens;
}
int control(uint8_t type, uint8_t request, uint16_t, uint16_t index, uint8_t* data, uint16_t size,
unsigned) override {
Lock lock(phone_.mu);
std::unique_lock<std::recursive_mutex> busLock = lockBus();
if (!present()) {
return -ENODEV;
}
if (request == 51) {
++phone_.protocolAsked;
phone_.strings.clear(); // a new identification starts
Expand Down Expand Up @@ -263,6 +291,11 @@ class Device : public evo::UsbDevice {
}
bool claimInterface(unsigned number) override {
Lock lock(phone_.mu);
std::unique_lock<std::recursive_mutex> busLock = lockBus();
if (!present()) {
errno = ENODEV;
return false;
}
CHECK(number == 0);
++phone_.claims;
if (phone_.claimErrno != 0) {
Expand All @@ -282,6 +315,11 @@ class Device : public evo::UsbDevice {
}
bool reset() override {
Lock lock(phone_.mu);
std::unique_lock<std::recursive_mutex> busLock = lockBus();
if (!present()) {
errno = ENODEV;
return false;
}
++phone_.resets;
phone_.strings.clear();
if (phone_.resets == phone_.resetGoneAt) {
Expand Down Expand Up @@ -364,7 +402,18 @@ class Device : public evo::UsbDevice {
short waitEvents() const override { return POLLIN; }

private:
// Keep the presence check and operation atomic with fake unplug and re-enumeration.
std::unique_lock<std::recursive_mutex> lockBus() {
return phone_.bus ? std::unique_lock<std::recursive_mutex>(phone_.bus->mu)
: std::unique_lock<std::recursive_mutex>();
}
bool present() const {
return phone_.address == address_ &&
(!phone_.bus || phone_.bus->hasDevice(phone_.port, phone_.busNumber, address_));
}

Phone& phone_;
unsigned address_;
std::vector<evo::UsbTransfer*> pending_;
std::set<evo::UsbTransfer*> cancelled_;
};
Expand Down
Loading