1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-10-02 20:52:02 +00:00
sys-con/SwitchUSB/source/SwitchUSBInterface.cpp

72 lines
1.7 KiB
C++
Raw Normal View History

2019-10-31 18:00:42 +00:00
#include "SwitchUSBInterface.h"
#include "SwitchUSBEndpoint.h"
SwitchUSBInterface::SwitchUSBInterface(UsbHsInterface &interface)
: m_interface(interface)
{
}
SwitchUSBInterface::~SwitchUSBInterface()
{
Close();
}
Result SwitchUSBInterface::Open()
{
UsbHsClientIfSession temp;
Result rc = usbHsAcquireUsbIf(&temp, &m_interface);
if (R_FAILED(rc))
return 11037;
m_session = temp;
if (R_SUCCEEDED(rc))
{
m_inEndpoints.clear();
m_outEndpoints.clear();
m_inEndpoints.reserve(15);
m_outEndpoints.reserve(15);
//For some reason output_endpoint_descs contains IN endpoints and input_endpoint_descs contains OUT endpoints
//we're adjusting appropriately
for (int i = 0; i != 15; ++i)
{
2019-11-02 09:03:54 +00:00
m_inEndpoints.push_back(std::make_unique<SwitchUSBEndpoint>(m_session, m_session.inf.inf.input_endpoint_descs[i]));
2019-10-31 18:00:42 +00:00
}
for (int i = 0; i != 15; ++i)
{
2019-11-02 09:03:54 +00:00
m_outEndpoints.push_back(std::make_unique<SwitchUSBEndpoint>(m_session, m_session.inf.inf.output_endpoint_descs[i]));
2019-10-31 18:00:42 +00:00
}
}
return rc;
}
void SwitchUSBInterface::Close()
{
for (auto &&endpoint : m_inEndpoints)
{
endpoint->Close();
}
for (auto &&endpoint : m_outEndpoints)
{
endpoint->Close();
}
usbHsIfClose(&m_session);
}
IUSBEndpoint *SwitchUSBInterface::GetEndpoint(IUSBEndpoint::Direction direction, uint8_t index)
{
//TODO: replace this with a control transfer to get endpoint descriptor
if (direction == IUSBEndpoint::USB_ENDPOINT_IN)
return m_inEndpoints[index].get();
else
return m_outEndpoints[index].get();
}
Result SwitchUSBInterface::Reset()
{
usbHsIfResetDevice(&m_session);
return 0;
}