|
|
@@ -0,0 +1,55 @@
|
|
|
+#include "libusbdevice.h"
|
|
|
+
|
|
|
+#include <libusb-1.0/libusb.h>
|
|
|
+#include <iostream>
|
|
|
+
|
|
|
+namespace noolitelib
|
|
|
+{
|
|
|
+
|
|
|
+LibUsbDevice::LibUsbDevice()
|
|
|
+{
|
|
|
+ auto status = libusb_init(&m_context);
|
|
|
+ if (status) {
|
|
|
+ std::cout << "Error initializing context: " << libusb_strerror(status) << std::endl;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+LibUsbDevice::~LibUsbDevice()
|
|
|
+{
|
|
|
+ if (m_context) {
|
|
|
+ libusb_exit(m_context);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void LibUsbDevice::openDevice(uint16_t vendorId, uint16_t productId)
|
|
|
+{
|
|
|
+ m_device = libusb_open_device_with_vid_pid(m_context, vendorId, productId);
|
|
|
+}
|
|
|
+
|
|
|
+void LibUsbDevice::close()
|
|
|
+{
|
|
|
+ if (m_device) {
|
|
|
+ libusb_close(m_device);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+bool LibUsbDevice::sendDataToDevice(unsigned char *data, uint16_t length, std::chrono::milliseconds timeout)
|
|
|
+{
|
|
|
+ if (!m_device) {
|
|
|
+ std::cout << "Device not opened" << std::endl;
|
|
|
+ }
|
|
|
+
|
|
|
+ auto requestType = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE;
|
|
|
+ auto request = LIBUSB_REQUEST_SET_CONFIGURATION;
|
|
|
+
|
|
|
+ auto status = libusb_control_transfer(m_device, requestType, request, 0, 0, data, length, timeout.count());
|
|
|
+
|
|
|
+ if (status) {
|
|
|
+ std::cout << "Sending data error: " << libusb_strerror(status) << std::endl;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+}
|