EHS/src/io/Usb_LNX.cpp

107 lines
1.9 KiB
C++
Raw Normal View History

2024-04-08 03:10:24 -07:00
#include "ehs/io/Usb_LNX.h"
#include <fcntl.h>
#include <unistd.h>
namespace ehs
{
Usb::~Usb()
{
}
Usb::Usb()
: hdl(-1)
{
}
Usb::Usb(const UInt_32 bus, const UInt_32 address)
: UsbBase(bus, address), hdl(-1)
{
Initialize();
}
Usb::Usb(Usb&& usb) noexcept
: UsbBase((UsbBase&&)usb), hdl(usb.hdl)
{
usb.hdl = -1;
}
Usb::Usb(const Usb& usb)
: UsbBase(usb), hdl(-1)
{
Initialize();
}
Usb& Usb::operator=(Usb&& usb) noexcept
{
if (this == &usb)
return *this;
UsbBase::operator=((UsbBase&&)usb);
hdl = usb.hdl;
usb.hdl = -1;
return *this;
}
Usb& Usb::operator=(const Usb& usb)
{
if (this == &usb)
return *this;
UsbBase::operator=(usb);
hdl = -1;
Initialize();
return *this;
}
void Usb::Initialize()
{
if (!IsValid())
{
EHS_LOG_INT("Error", 0, "Cannot initialize with an invalid object.");
return;
}
if (IsInitialized())
{
EHS_LOG_INT("Warning", 1, "Object is already initialized.");
return;
}
hdl = open("/dev/bus/usb/" + Str_8::FromNum(GetBus()) + "/" + Str_8::FromNum(GetAddress()), O_RDWR);
if (hdl == -1)
EHS_LOG_INT("Error", 2, "Failed to connect to USB device.");
}
void Usb::Release()
{
if (!IsValid())
{
EHS_LOG_INT("Error", 0, "Cannot release with an invalid object.");
return;
}
if (!IsInitialized())
{
EHS_LOG_INT("Warning", 1, "Object is already released.");
return;
}
close(hdl);
hdl = -1;
}
bool Usb::IsInitialized() const
{
return hdl != -1;
}
}