Skip to content

Commit

Permalink
Fixed input packets bleeding into each other to fix #78
Browse files Browse the repository at this point in the history
  • Loading branch information
cathery committed Nov 25, 2019
1 parent c2e12bc commit 7356a74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
2 changes: 2 additions & 0 deletions SwitchUSB/include/SwitchUSBEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class SwitchUSBEndpoint : public IUSBEndpoint
UsbHsClientIfSession *m_ifSession;
usb_endpoint_descriptor *m_descriptor;

void *m_buffer = nullptr;

public:
//Pass the necessary information to be able to open the endpoint
SwitchUSBEndpoint(UsbHsClientIfSession &if_session, usb_endpoint_descriptor &desc);
Expand Down
33 changes: 19 additions & 14 deletions SwitchUSB/source/SwitchUSBEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,65 @@ SwitchUSBEndpoint::~SwitchUSBEndpoint()

Result SwitchUSBEndpoint::Open(int maxPacketSize)
{
Result rc = usbHsIfOpenUsbEp(m_ifSession, &m_epSession, 1, (maxPacketSize != 0 ? maxPacketSize : m_descriptor->wMaxPacketSize), m_descriptor);
maxPacketSize = maxPacketSize != 0 ? maxPacketSize : m_descriptor->wMaxPacketSize;

Result rc = usbHsIfOpenUsbEp(m_ifSession, &m_epSession, 1, maxPacketSize, m_descriptor);
if (R_FAILED(rc))
return 73011;

m_buffer = memalign(0x1000, maxPacketSize);
if (m_buffer == nullptr)
return -1;
return rc;
}

void SwitchUSBEndpoint::Close()
{
if (m_buffer != nullptr)
{
free(m_buffer);
m_buffer = nullptr;
}

usbHsEpClose(&m_epSession);
}

Result SwitchUSBEndpoint::Write(const void *inBuffer, size_t bufferSize)
{
void *temp_buffer = memalign(0x1000, bufferSize);
if (temp_buffer == nullptr)
if (m_buffer == nullptr)
return -1;

u32 transferredSize = 0;

for (size_t byte = 0; byte != bufferSize; ++byte)
{
static_cast<uint8_t *>(temp_buffer)[byte] = static_cast<const uint8_t *>(inBuffer)[byte];
static_cast<uint8_t *>(m_buffer)[byte] = static_cast<const uint8_t *>(inBuffer)[byte];
}

Result rc = usbHsEpPostBuffer(&m_epSession, temp_buffer, bufferSize, &transferredSize);
Result rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize);

if (R_SUCCEEDED(rc))
{
svcSleepThread(m_descriptor->bInterval * 1e+6L);
}

free(temp_buffer);
return rc;
}

Result SwitchUSBEndpoint::Read(void *outBuffer, size_t bufferSize)
{
void *temp_buffer = memalign(0x1000, bufferSize);
if (temp_buffer == nullptr)
if (m_buffer == nullptr)
return -1;

u32 transferredSize;

Result rc = usbHsEpPostBuffer(&m_epSession, temp_buffer, bufferSize, &transferredSize);
Result rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize);

if (R_SUCCEEDED(rc))
{
for (u32 byte = 0; byte != transferredSize; ++byte)
{
static_cast<uint8_t *>(outBuffer)[byte] = static_cast<uint8_t *>(temp_buffer)[byte];
static_cast<uint8_t *>(outBuffer)[byte] = static_cast<uint8_t *>(m_buffer)[byte];
}
}
free(temp_buffer);

return rc;
}

Expand Down

0 comments on commit 7356a74

Please sign in to comment.