Files
ptprnt/src/libusbwrap/UsbDevice.cpp
Moritz Martinius 2d37f6fcfb
All checks were successful
Build ptprnt / build (push) Successful in 3m50s
cli-parser-cleanup (#15)
Reviewed-on: moritz/ptouch-prnt#15
2025-10-13 19:23:27 +00:00

129 lines
4.1 KiB
C++

/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "libusbwrap/UsbDevice.hpp"
#include <cstdint>
#include <stdexcept>
#include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
UsbDevice::UsbDevice(libusb_context* ctx, usbDevice_ptr dev) : mLibusbCtx(ctx), mLibusbDev(std::move(dev)) {
if (mLibusbCtx == nullptr || mLibusbDev == nullptr) {
throw std::invalid_argument("ctx or device are nullptr");
}
libusb_get_device_descriptor(mLibusbDev.get(), &mLibusbDevDesc);
}
UsbDevice::~UsbDevice() {
// only free the device, not the context, which is shared between every device
// the UsbDeviceFactory will take care of that
if (mIsOpen) {
close();
}
}
bool UsbDevice::open() {
int openStatus = libusb_open(mLibusbDev.get(), &mLibusbDevHandle);
if (openStatus != 0) {
mLastError = static_cast<Error>(openStatus);
return false;
}
return true;
}
void UsbDevice::close() {
libusb_close(mLibusbDevHandle);
}
bool UsbDevice::detachKernelDriver(int interfaceNo) {
// TODO: cover the other status codes that can be returned
int kernelDriverStatus = libusb_kernel_driver_active(mLibusbDevHandle, interfaceNo);
if (kernelDriverStatus == 1) { // kernel driver is active, we have to detach to continue...
int detachStatus = libusb_detach_kernel_driver(mLibusbDevHandle, interfaceNo);
if (detachStatus != 0) {
mLastError = static_cast<Error>(detachStatus);
return false;
}
}
return true;
}
bool UsbDevice::claimInterface(int interfaceNo) {
// TODO: cover the other status codes that can be returned
int claimInterfaceStatus = libusb_claim_interface(mLibusbDevHandle, interfaceNo);
if (claimInterfaceStatus != 0) {
mLastError = static_cast<Error>(claimInterfaceStatus);
return false;
}
return true;
}
bool UsbDevice::releaseInterface(int interfaceNo) {
int releaseInterfaceStatus = libusb_release_interface(mLibusbDevHandle, interfaceNo);
if (releaseInterfaceStatus != 0) {
mLastError = static_cast<Error>(releaseInterfaceStatus);
return false;
}
return true;
}
bool UsbDevice::bulkTransfer(uint8_t endpoint, const std::vector<uint8_t>& data, int* tx, unsigned int timeout) {
// TODO: implement error handling for incomplete transactions (tx length != data length)
int bulkTransferStatus = 0;
bulkTransferStatus = libusb_bulk_transfer(mLibusbDevHandle, endpoint, const_cast<unsigned char*>(data.data()),
data.size(), tx, timeout);
if (bulkTransferStatus != 0) {
mLastError = static_cast<Error>(bulkTransferStatus);
return false;
}
return true;
}
const usbId UsbDevice::getUsbId() {
return {mLibusbDevDesc.idVendor, mLibusbDevDesc.idProduct};
}
const device::Speed UsbDevice::getSpeed() {
return static_cast<device::Speed>(libusb_get_device_speed(mLibusbDev.get()));
}
const uint8_t UsbDevice::getBusNumber() {
return libusb_get_bus_number(mLibusbDev.get());
}
const uint8_t UsbDevice::getPortNumber() {
return libusb_get_port_number(mLibusbDev.get());
}
const Error UsbDevice::getLastError() {
return mLastError;
}
const std::string UsbDevice::getLastErrorString() {
return std::string(libusb_error_name(static_cast<int>(mLastError)));
}
} // namespace libusbwrap