Implemented printer status retrieval

This commit is contained in:
2023-08-27 09:29:37 +02:00
parent 5644c84c83
commit f467a39919
9 changed files with 202 additions and 9 deletions

View File

@@ -1,8 +1,29 @@
#include "P700Printer.hpp" #include "P700Printer.hpp"
#include <spdlog/spdlog.h>
#include <chrono>
#include <cstdint>
#include <iostream> #include <iostream>
#include <ratio>
#include <thread>
#include <vector>
#include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp"
#include "spdlog/fmt/bin_to_hex.h"
namespace ptprnt::printer { namespace ptprnt::printer {
P700Printer::P700Printer() {}
P700Printer::~P700Printer() {
detachUsbDevice();
if (mUsbHndl) {
mUsbHndl->close();
}
}
const std::string_view P700Printer::getDriverName() { const std::string_view P700Printer::getDriverName() {
return mInfo.driverName; return mInfo.driverName;
} }
@@ -19,6 +40,25 @@ const PrinterInfo P700Printer::getPrinterInfo() {
return mInfo; return mInfo;
} }
const PrinterStatus P700Printer::getPrinterStatus() {
using namespace std::chrono_literals;
std::vector<uint8_t> getStatusCmd({0x1b, 0x69, 0x53}); // status info request
send(getStatusCmd);
int tx = 0;
int tries = 0;
std::vector<uint8_t> recvBuf(32);
do {
std::this_thread::sleep_for(100ms);
mUsbHndl->bulkTransfer(0x81, recvBuf, &tx, 0);
if (tries++ > 10) {
break;
}
} while (tx == 0);
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
}
const uint16_t P700Printer::getVid() { const uint16_t P700Printer::getVid() {
return mInfo.vid; return mInfo.vid;
} }
@@ -28,10 +68,35 @@ const uint16_t P700Printer::getPid() {
} }
bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) { bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {
if (!usbHndl->open()) {
spdlog::error("Unable to open USB device: {}", usbHndl->getLastErrorString());
return false;
}
if (!usbHndl->detachKernelDriver(0)) {
spdlog::error("Device is already in use or couldn't be detached from kernel: {}",
usbHndl->getLastErrorString());
return false;
}
if (!usbHndl->claimInterface(0)) {
spdlog::error("Could not claim interface 0: {}", usbHndl->getLastErrorString());
return false;
}
mUsbHndl = std::move(usbHndl);
return true; return true;
} }
bool P700Printer::detachUsbDevice() { bool P700Printer::detachUsbDevice() {
if (!mUsbHndl) {
spdlog::warn("No device to detach...");
return true;
}
if (!mUsbHndl->releaseInterface(0)) {
spdlog::error("Could not release interface 0: {}", mUsbHndl->getLastErrorString());
return false;
}
return true; return true;
} }
@@ -42,4 +107,18 @@ bool P700Printer::printBitmap(const Bitmap& bitmap) {
bool P700Printer::printText(const std::string& text, uint16_t fontSize) { bool P700Printer::printText(const std::string& text, uint16_t fontSize) {
return true; return true;
} }
bool P700Printer::send(std::vector<uint8_t>& data) {
if (mUsbHndl == nullptr || data.size() > 128) {
spdlog::error("Invalid device handle or invalid data.");
return false;
}
if (!mUsbHndl->bulkTransfer(0x02, data, nullptr, 0)) {
spdlog::error("Error writing command to Printer: {}", mUsbHndl->getLastErrorString());
return false;
}
return true;
}
} // namespace ptprnt::printer } // namespace ptprnt::printer

View File

@@ -1,3 +1,5 @@
#include <sys/types.h>
#include <memory> #include <memory>
#include "interface/IPrinterDriver.hpp" #include "interface/IPrinterDriver.hpp"
@@ -10,8 +12,8 @@ namespace ptprnt::printer {
class P700Printer : public ::ptprnt::IPrinterDriver { class P700Printer : public ::ptprnt::IPrinterDriver {
public: public:
P700Printer() = default; P700Printer();
~P700Printer() = default; ~P700Printer();
// delete copy ctor and assignment // delete copy ctor and assignment
P700Printer(const P700Printer&) = delete; P700Printer(const P700Printer&) = delete;
@@ -24,12 +26,15 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
const uint16_t getVid() override; const uint16_t getVid() override;
const std::string_view getVersion() override; const std::string_view getVersion() override;
const PrinterInfo getPrinterInfo() override; const PrinterInfo getPrinterInfo() override;
const PrinterStatus getPrinterStatus() override;
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override; bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
bool detachUsbDevice() override; bool detachUsbDevice() override;
bool printBitmap(const Bitmap& bitmap) override; bool printBitmap(const Bitmap& bitmap) override;
bool printText(const std::string& text, uint16_t fontSize) override; bool printText(const std::string& text, uint16_t fontSize) override;
private: private:
bool send(std::vector<uint8_t>& data);
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr}; std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
PrinterInfo mInfo{.driverName = "P700", PrinterInfo mInfo{.driverName = "P700",

View File

@@ -15,10 +15,26 @@ void PtouchPrint::init() {
} }
void PtouchPrint::run() { void PtouchPrint::run() {
if (getCompatiblePrinters() == 0) { auto numFoundPrinters = getCompatiblePrinters();
if (numFoundPrinters == 0) {
spdlog::error( spdlog::error(
"No compatible printers found, please make sure if they are turned on and connected"); "No compatible printers found, please make sure if they are turned on and connected");
return;
} else if (numFoundPrinters > 1) {
spdlog::warn("Found more than one compatible printer. Currently not supported.");
return;
} }
auto printer = mCompatiblePrinters[0];
auto devices = mUsbDeviceFactory.findDevices(printer->getVid(), printer->getPid());
if (devices.size() != 1) {
spdlog::warn(
"Found more than one device of the same printer on bus. Currently not supported");
return;
}
printer->attachUsbDevice(devices[0]);
auto status = printer->getPrinterStatus();
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
} }
unsigned int PtouchPrint::getCompatiblePrinters() { unsigned int PtouchPrint::getCompatiblePrinters() {

View File

@@ -17,6 +17,7 @@ class IPrinterDriver {
virtual const uint16_t getVid() = 0; virtual const uint16_t getVid() = 0;
virtual const uint16_t getPid() = 0; virtual const uint16_t getPid() = 0;
virtual const PrinterInfo getPrinterInfo() = 0; virtual const PrinterInfo getPrinterInfo() = 0;
virtual const PrinterStatus getPrinterStatus() = 0;
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0; virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
virtual bool detachUsbDevice() = 0; virtual bool detachUsbDevice() = 0;
virtual bool printBitmap(const Bitmap& bitmap) = 0; virtual bool printBitmap(const Bitmap& bitmap) = 0;

View File

@@ -14,4 +14,9 @@ struct PrinterInfo {
uint16_t pid = 0x00; uint16_t pid = 0x00;
}; };
struct PrinterStatus {
unsigned int tapeWidthPixel = 0;
unsigned int tapeWidthMm = 0.0;
};
} // namespace ptprnt } // namespace ptprnt

View File

@@ -1,8 +1,10 @@
#include "libusbwrap/UsbDevice.hpp" #include "libusbwrap/UsbDevice.hpp"
#include <cstdint>
#include <stdexcept> #include <stdexcept>
#include "libusb.h" #include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp" #include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap { namespace libusbwrap {
@@ -22,14 +24,67 @@ UsbDevice::~UsbDevice() {
} }
} }
Error UsbDevice::open() { bool UsbDevice::open() {
return static_cast<Error>(libusb_open(mLibusbDev, &mLibusbDevHandle)); int openStatus = libusb_open(mLibusbDev, &mLibusbDevHandle);
if (openStatus != 0) {
mLastError = static_cast<Error>(openStatus);
return false;
}
return true;
} }
void UsbDevice::close() { void UsbDevice::close() {
libusb_close(mLibusbDevHandle); 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, 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, data.data(), data.size(), tx, timeout);
if (bulkTransferStatus != 0) {
mLastError = static_cast<Error>(bulkTransferStatus);
return false;
}
return true;
}
const uint16_t UsbDevice::getVid() { const uint16_t UsbDevice::getVid() {
return mLibusbDevDesc.idVendor; return mLibusbDevDesc.idVendor;
} }
@@ -49,4 +104,12 @@ const uint8_t UsbDevice::getBusNumber() {
const uint8_t UsbDevice::getPortNumber() { const uint8_t UsbDevice::getPortNumber() {
return libusb_get_port_number(mLibusbDev); return libusb_get_port_number(mLibusbDev);
} }
const Error UsbDevice::getLastError() {
return mLastError;
}
const std::string UsbDevice::getLastErrorString() {
return std::string(libusb_error_name(static_cast<int>(mLastError)));
}
} // namespace libusbwrap } // namespace libusbwrap

View File

@@ -4,6 +4,7 @@
#include <cstddef> #include <cstddef>
#include "libusb.h" #include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp" #include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap { namespace libusbwrap {
@@ -16,9 +17,16 @@ class UsbDevice : public IUsbDevice {
UsbDevice(const UsbDevice&) = delete; UsbDevice(const UsbDevice&) = delete;
UsbDevice& operator=(UsbDevice&) = delete; UsbDevice& operator=(UsbDevice&) = delete;
Error open() override; bool open() override;
void close() 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, std::vector<uint8_t>& data, int* tx,
unsigned int timeout) override;
// getters // getters
const uint16_t getVid() override; const uint16_t getVid() override;
const uint16_t getPid() override; const uint16_t getPid() override;
@@ -26,11 +34,16 @@ class UsbDevice : public IUsbDevice {
const uint8_t getBusNumber() override; const uint8_t getBusNumber() override;
const uint8_t getPortNumber() override; const uint8_t getPortNumber() override;
// errors
const Error getLastError() override;
const std::string getLastErrorString() override;
private: private:
libusb_context* mLibusbCtx{nullptr}; libusb_context* mLibusbCtx{nullptr};
libusb_device* mLibusbDev{nullptr}; libusb_device* mLibusbDev{nullptr};
libusb_device_handle* mLibusbDevHandle{nullptr}; libusb_device_handle* mLibusbDevHandle{nullptr};
libusb_device_descriptor mLibusbDevDesc; libusb_device_descriptor mLibusbDevDesc;
std::atomic<bool> mIsOpen = false; std::atomic<bool> mIsOpen = false;
Error mLastError = Error::SUCCESS;
}; };
} // namespace libusbwrap } // namespace libusbwrap

View File

@@ -33,8 +33,6 @@ int UsbDeviceFactory::refreshDeviceList() {
spdlog::error("Error enumarating USB devices"); spdlog::error("Error enumarating USB devices");
} else if (ret == 0) { } else if (ret == 0) {
spdlog::warn("No USB devices found"); spdlog::warn("No USB devices found");
} else {
spdlog::debug("Found {} USB devices", ret);
} }
return ret; return ret;

View File

@@ -4,6 +4,8 @@
#include <cstdint> #include <cstdint>
#include <optional> #include <optional>
#include <string>
#include <vector>
#include "libusb.h" #include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp" #include "libusbwrap/LibUsbTypes.hpp"
@@ -22,14 +24,25 @@ enum class Speed {
class IUsbDevice { class IUsbDevice {
public: public:
virtual Error open() = 0; virtual bool open() = 0;
virtual void close() = 0; virtual void close() = 0;
// libusb wrappers
virtual bool detachKernelDriver(int interfaceNo) = 0;
virtual bool claimInterface(int interfaceNo) = 0;
virtual bool releaseInterface(int interfaceNo) = 0;
virtual bool bulkTransfer(uint8_t endpoint, std::vector<uint8_t>& data, int* tx,
unsigned int timeout) = 0;
// getters // getters
virtual const uint16_t getVid() = 0; virtual const uint16_t getVid() = 0;
virtual const uint16_t getPid() = 0; virtual const uint16_t getPid() = 0;
virtual const device::Speed getSpeed() = 0; virtual const device::Speed getSpeed() = 0;
virtual const uint8_t getBusNumber() = 0; virtual const uint8_t getBusNumber() = 0;
virtual const uint8_t getPortNumber() = 0; virtual const uint8_t getPortNumber() = 0;
// errors
virtual const Error getLastError() = 0;
virtual const std::string getLastErrorString() = 0;
}; };
} // namespace libusbwrap } // namespace libusbwrap