/* ptrnt - print labels on linux Copyright (C) 2023 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 . */ #pragma once #include #include #include "libusb.h" #include "libusbwrap/LibUsbTypes.hpp" #include "libusbwrap/interface/IUsbDevice.hpp" namespace libusbwrap { struct usbDevice_deleter { void operator()(libusb_device* dev_ptr) const { if (nullptr != dev_ptr) { libusb_unref_device(dev_ptr); } } }; using usbDevice_ptr = std::unique_ptr; class UsbDevice : public IUsbDevice { public: explicit UsbDevice(libusb_context* ctx, usbDevice_ptr dev); ~UsbDevice() override; // delete copy ctor and assignment UsbDevice(const UsbDevice&) = delete; UsbDevice& operator=(const UsbDevice&) = delete; UsbDevice(UsbDevice&&) = delete; UsbDevice& operator=(UsbDevice&&) = delete; bool open() override; void close() override; // libusb wrappers bool detachKernelDriver(int interfaceNo) override; bool claimInterface(int interfaceNo) override; bool releaseInterface(int interfaceNo) override; bool bulkTransfer(uint8_t endpoint, const std::vector& data, int* tx, unsigned int timeout) override; // getters const usbId getUsbId() override; const device::Speed getSpeed() override; const uint8_t getBusNumber() override; const uint8_t getPortNumber() override; // errors const Error getLastError() override; const std::string getLastErrorString() override; private: libusb_context* mLibusbCtx{nullptr}; usbDevice_ptr mLibusbDev{nullptr}; libusb_device_handle* mLibusbDevHandle{nullptr}; libusb_device_descriptor mLibusbDevDesc{0}; std::atomic mIsOpen = false; Error mLastError = Error::SUCCESS; }; } // namespace libusbwrap