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

libuuu: implement a basic retry strategy for usb transfer #411

Merged
merged 1 commit into from
Feb 12, 2024
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
46 changes: 42 additions & 4 deletions libuuu/trans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,44 @@ TransBase::~TransBase()
{
}

int TransBase::write(void *buff, size_t size)
{
int ret;

for (int retry = 0; retry < m_retry; retry++) {
ret = write_simple(buff, size);
switch (ret) {
case 0:
return 0;
case LIBUSB_ERROR_TIMEOUT:
continue;
default:
return ret;
}
}

return LIBUSB_ERROR_TIMEOUT;
}

int TransBase::read(void *buff, size_t size, size_t *return_size)
{
int ret;

for (int retry = 0; retry < m_retry; retry++) {
ret = read_simple(buff, size, return_size);
switch (ret) {
case 0:
return 0;
case LIBUSB_ERROR_TIMEOUT:
continue;
default:
return ret;
}
}

return LIBUSB_ERROR_TIMEOUT;
}

int TransBase::read(vector<uint8_t> &buff)
{
size_t size;
Expand Down Expand Up @@ -117,7 +155,7 @@ int HIDTrans::open(void *p)
return 0;
}

int HIDTrans::write(void *buff, size_t size)
int HIDTrans::write_simple(void *buff, size_t size)
{
int ret;
uint8_t *p = (uint8_t *)buff;
Expand Down Expand Up @@ -160,7 +198,7 @@ int HIDTrans::write(void *buff, size_t size)
return ret;
}

int HIDTrans::read(void *buff, size_t size, size_t *rsize)
int HIDTrans::read_simple(void *buff, size_t size, size_t *rsize)
{
int ret;
int actual;
Expand Down Expand Up @@ -188,7 +226,7 @@ int HIDTrans::read(void *buff, size_t size, size_t *rsize)
return 0;
}

int BulkTrans::write(void *buff, size_t size)
int BulkTrans::write_simple(void *buff, size_t size)
{
int ret = 0;
int actual_length;
Expand Down Expand Up @@ -264,7 +302,7 @@ int BulkTrans::open(void *p)
}
return 0;
}
int BulkTrans::read(void *buff, size_t size, size_t *rsize)
int BulkTrans::read_simple(void *buff, size_t size, size_t *rsize)
{
int ret;
int actual_length;
Expand Down
27 changes: 18 additions & 9 deletions libuuu/trans.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@
class TransBase
{
public:
TransBase() = default;
TransBase(int retry = 1) : m_retry{retry} {}
TransBase(const TransBase&) = delete;
TransBase& operator=(const TransBase&) = delete;
virtual ~TransBase();

virtual int open(void *) { return 0; }
virtual int close() { return 0; }
virtual int write(void *buff, size_t size) = 0;
virtual int read(void *buff, size_t size, size_t *return_size) = 0;
int write(void *buff, size_t size);
int read(void *buff, size_t size, size_t *return_size);
int write(std::vector<uint8_t> & buff) { return write(buff.data(), buff.size()); }
int read(std::vector<uint8_t> &buff);

protected:
void * m_devhandle = nullptr;
int m_retry = 1;

virtual int write_simple(void *buff, size_t size) = 0;
virtual int read_simple(void *buff, size_t size, size_t *return_size) = 0;
};

class EPInfo
Expand All @@ -65,6 +69,7 @@ class EPInfo
class USBTrans : public TransBase
{
public:
USBTrans(int retry = 1) : TransBase(retry) {}
int open(void *p) override;
int close() override;

Expand All @@ -74,13 +79,15 @@ class USBTrans : public TransBase
class HIDTrans : public USBTrans
{
public:
HIDTrans(int timeout = 1000) : m_timeout{timeout} {}
HIDTrans(int timeout = 1000) : USBTrans(2), m_timeout{timeout} {}
~HIDTrans() override { if (m_devhandle) close(); m_devhandle = nullptr; }

int open(void *p) override;
void set_hid_out_ep(int ep) noexcept { m_outEP = ep; }
int write(void *buff, size_t size) override;
int read(void *buff, size_t size, size_t *return_size) override;

protected:
int write_simple(void *buff, size_t size) override;
int read_simple(void *buff, size_t size, size_t *return_size) override;

private:
int m_outEP = 0;
Expand All @@ -91,12 +98,14 @@ class HIDTrans : public USBTrans
class BulkTrans : public USBTrans
{
public:
BulkTrans(int timeout = 2000) : m_timeout{timeout} {}
BulkTrans(int timeout = 2000) : USBTrans(2), m_timeout{timeout} {}
~BulkTrans() override { if (m_devhandle) close(); m_devhandle = nullptr; }

int open(void *p) override;
int write(void *buff, size_t size) override;
int read(void *buff, size_t size, size_t *return_size) override;

protected:
int write_simple(void *buff, size_t size) override;
int read_simple(void *buff, size_t size, size_t *return_size) override;

private:
size_t m_MaxTransPreRequest = 0x100000;
Expand Down
Loading