successfully claim printer interace
This commit is contained in:
parent
76d6c7c069
commit
f71f257eb0
5 changed files with 60 additions and 28 deletions
|
@ -29,7 +29,7 @@ set(GUI_SOURCES
|
||||||
|
|
||||||
add_executable(kima2 ${GUI_SOURCES})
|
add_executable(kima2 ${GUI_SOURCES})
|
||||||
target_include_directories(kima2 PRIVATE ${PROJECT_BINARY_DIR})
|
target_include_directories(kima2 PRIVATE ${PROJECT_BINARY_DIR})
|
||||||
target_link_libraries(kima2 core Qt5::Widgets Qt5::PrintSupport stdc++fs)
|
target_link_libraries(kima2 core printer Qt5::Widgets Qt5::PrintSupport stdc++fs)
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
set_target_properties(kima2 PROPERTIES LINK_FLAGS "-mwindows")
|
set_target_properties(kima2 PROPERTIES LINK_FLAGS "-mwindows")
|
||||||
endif(WIN32)
|
endif(WIN32)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "settingsdialog.h"
|
#include "settingsdialog.h"
|
||||||
|
|
||||||
#include <database.h>
|
#include <database.h>
|
||||||
|
#include <posprinter.h>
|
||||||
|
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
@ -19,6 +20,11 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par
|
||||||
ui_.posPrinterDeviceEdit->setText(posPrinterDevice);
|
ui_.posPrinterDeviceEdit->setText(posPrinterDevice);
|
||||||
ui_.feePercentSpinBox->setValue(feeInPercent);
|
ui_.feePercentSpinBox->setValue(feeInPercent);
|
||||||
ui_.maxFeeSpinBox->setValue(maxFeeInEuro);
|
ui_.maxFeeSpinBox->setValue(maxFeeInEuro);
|
||||||
|
|
||||||
|
connect(ui_.testPosPrinterButton, &QPushButton::clicked, this, [](){
|
||||||
|
//PosPrinter::initialize({0, 0});
|
||||||
|
PosPrinter printer;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsDialog::accept()
|
void SettingsDialog::accept()
|
||||||
|
|
|
@ -5,5 +5,5 @@ set(PRINTER_SOURCES
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(printer STATIC ${PRINTER_SOURCES})
|
add_library(printer STATIC ${PRINTER_SOURCES})
|
||||||
target_link_libraries(printer ${LibUSB_LIBRARY})
|
target_link_libraries(printer ${LIBUSB_1_LIBRARIES})
|
||||||
target_include_directories(printer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
target_include_directories(printer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
|
@ -4,9 +4,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
PosPrinter::PosPrinter() : PosPrinter(std::pair<int, int>{0x0456, 0x0808}) {}
|
PosPrinter::PosPrinter()
|
||||||
|
|
||||||
PosPrinter::PosPrinter(std::pair<int, int> vendorModelId)
|
|
||||||
{
|
{
|
||||||
int retValue;
|
int retValue;
|
||||||
|
|
||||||
|
@ -15,17 +13,50 @@ PosPrinter::PosPrinter(std::pair<int, int> vendorModelId)
|
||||||
throw std::runtime_error("Init error");
|
throw std::runtime_error("Init error");
|
||||||
}
|
}
|
||||||
|
|
||||||
// libusb_set_debug(contextPtr_, 3); // set verbosity level to 3, as suggested in the
|
libusb_device** devList;
|
||||||
// documentation
|
int devCount = libusb_get_device_list(contextPtr_, &devList);
|
||||||
|
if (devCount <= 0) {
|
||||||
|
libusb_exit(contextPtr_);
|
||||||
|
throw std::runtime_error("Could not receive device list");
|
||||||
|
}
|
||||||
|
int numDevice = -1;
|
||||||
|
for (int i = 0; i < devCount; ++i) {
|
||||||
|
libusb_device_descriptor desc;
|
||||||
|
libusb_get_device_descriptor(devList[i], &desc);
|
||||||
|
for (const auto& supported : supportedPrinters_.models) {
|
||||||
|
if (desc.idVendor == supported.first && desc.idProduct == supported.second) {
|
||||||
|
numDevice = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
devicePtr_ =
|
if (numDevice < 0) {
|
||||||
libusb_open_device_with_vid_pid(contextPtr_, vendorModelId.first,
|
libusb_exit(contextPtr_);
|
||||||
vendorModelId.second); // these are vendorID and productID
|
return;
|
||||||
if (devicePtr_ == NULL) {
|
}
|
||||||
|
|
||||||
|
retValue = libusb_open(devList[numDevice], &devicePtr_); // these are vendorID and productID
|
||||||
|
if (retValue != 0) {
|
||||||
|
libusb_free_device_list(devList, 1);
|
||||||
|
libusb_exit(contextPtr_);
|
||||||
throw std::runtime_error("Cannot open printer device");
|
throw std::runtime_error("Cannot open printer device");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...
|
if (libusb_kernel_driver_active(devicePtr_, 0) == 1) { // find out if kernel driver is attached
|
||||||
|
std::cout << "Kernel Driver Active" << std::endl;
|
||||||
|
if (libusb_detach_kernel_driver(devicePtr_, 0) == 0) // detach it
|
||||||
|
std::cout << "Kernel Driver Detached!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
retValue = libusb_claim_interface(
|
||||||
|
devicePtr_, 0); // claim interface 0 (the first) of device (mine had jsut 1)
|
||||||
|
if (retValue < 0) {
|
||||||
|
std::cout << "Cannot Claim Interface" << std::endl;
|
||||||
|
throw std::runtime_error("Cannot claim interface");
|
||||||
|
}
|
||||||
|
|
||||||
|
libusb_free_device_list(devList, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
PosPrinter::~PosPrinter()
|
PosPrinter::~PosPrinter()
|
||||||
|
@ -40,15 +71,4 @@ PosPrinter::~PosPrinter()
|
||||||
|
|
||||||
libusb_close(devicePtr_); // close the device we opened
|
libusb_close(devicePtr_); // close the device we opened
|
||||||
libusb_exit(contextPtr_); // close the session
|
libusb_exit(contextPtr_); // close the session
|
||||||
|
|
||||||
delete instance_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PosPrinter::initialize(std::pair<int, int> vendorModelId)
|
|
||||||
{
|
|
||||||
if (!instance_) {
|
|
||||||
instance_ = new PosPrinter(vendorModelId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
PosPrinter* PosPrinter::getInstance() { return instance_; }
|
|
||||||
|
|
|
@ -5,18 +5,24 @@
|
||||||
|
|
||||||
#include <libusb-1.0/libusb.h>
|
#include <libusb-1.0/libusb.h>
|
||||||
|
|
||||||
|
struct SupportedPrinters {
|
||||||
|
std::array<std::pair<int, int>, 2> models{
|
||||||
|
// {Vendor ID, Model ID}
|
||||||
|
std::make_pair(0x0456, 0x0808),
|
||||||
|
std::make_pair(0x0416, 0x5011),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
class PosPrinter
|
class PosPrinter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void initialize(std::pair<int, int> vendorModelIds);
|
|
||||||
static PosPrinter* getInstance();
|
|
||||||
private:
|
|
||||||
PosPrinter();
|
PosPrinter();
|
||||||
PosPrinter(std::pair<int, int> vendorModelId);
|
|
||||||
~PosPrinter();
|
~PosPrinter();
|
||||||
static PosPrinter* instance_;
|
|
||||||
|
private:
|
||||||
libusb_context* contextPtr_{};
|
libusb_context* contextPtr_{};
|
||||||
libusb_device_handle* devicePtr_{};
|
libusb_device_handle* devicePtr_{};
|
||||||
|
SupportedPrinters supportedPrinters_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in a new issue