87 lines
1.8 KiB
C++
87 lines
1.8 KiB
C++
|
#include "UsbEndpoint.h"
|
||
|
#include <errno.h>
|
||
|
#include <sys/select.h>
|
||
|
extern int errno;
|
||
|
|
||
|
UsbEndPoint::UsbEndPoint(EndpointType eptype):m_fd(-1)
|
||
|
{
|
||
|
m_endpointtype=eptype;
|
||
|
}
|
||
|
|
||
|
UsbEndPoint::~UsbEndPoint()
|
||
|
{
|
||
|
if(m_fd)
|
||
|
{
|
||
|
::close(m_fd);
|
||
|
LOG("try close %s \n",map_endpoints[m_endpointtype].c_str());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int UsbEndPoint::open()
|
||
|
{
|
||
|
int ret=(int)HGUSB_ErrorCode::HG_USBSuccess;
|
||
|
if(m_endpointtype!=EndpointType::EP_Undefine)
|
||
|
{
|
||
|
LOG("try open %s \n",map_endpoints[m_endpointtype].c_str());
|
||
|
m_fd = ::open(map_endpoints[m_endpointtype].c_str(),O_RDWR);
|
||
|
if(m_fd ==-1)
|
||
|
{
|
||
|
ret=(int)HGUSB_ErrorCode::HG_USBOpenFailed;
|
||
|
LOG("try open %s fialed errorcode %d \n",map_endpoints[m_endpointtype].c_str(),errno);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
LOG("try open %s success \n",map_endpoints[m_endpointtype].c_str());
|
||
|
}
|
||
|
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
void UsbEndPoint::close()
|
||
|
{
|
||
|
if(m_fd)
|
||
|
{
|
||
|
::close(m_fd);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int UsbEndPoint::read(void* pdata,int length)
|
||
|
{
|
||
|
int ret = (int)HGUSB_ErrorCode::HG_USBSuccess;
|
||
|
if(!pdata)
|
||
|
return (int)HGUSB_ErrorCode::HG_USBInvalidePtr;
|
||
|
|
||
|
if(m_fd!=-1)
|
||
|
{
|
||
|
//DEBUG("try reading USB %s endpoint data\n",map_endpoints[m_endpointtype].c_str());
|
||
|
return ::read(m_fd,pdata,length);
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
int UsbEndPoint::write(void* pdata,int length)
|
||
|
{
|
||
|
int ret = 0;
|
||
|
if(!pdata)
|
||
|
return (int)HGUSB_ErrorCode::HG_USBInvalidePtr;
|
||
|
|
||
|
if(m_fd!=-1)
|
||
|
{
|
||
|
//DEBUG("try writing USB %s endpoint data\n",map_endpoints[m_endpointtype].c_str());
|
||
|
return ::write(m_fd,pdata,length);
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
int UsbEndPoint::getEndpointType()
|
||
|
{
|
||
|
return (int)m_endpointtype;
|
||
|
}
|
||
|
|
||
|
bool UsbEndPoint::is_open()
|
||
|
{
|
||
|
return m_fd != -1;
|
||
|
}
|
||
|
|