| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #include "libusbdevice.h"
- #include <libusb-1.0/libusb.h>
- #include <iostream>
- namespace noolitelib
- {
- constexpr auto DefaultTimeout = 1000;
- LibUsbDevice::LibUsbDevice()
- {
- auto status = libusb_init(&m_context);
- if (status) {
- std::cerr << "Error initializing context: " << libusb_strerror(status) << " [" << 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(const Data &data)
- {
- if (!m_device) {
- std::cerr << "Device not opened" << std::endl;
- return false;
- }
- auto requestType = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE;
- auto request = LIBUSB_REQUEST_SET_CONFIGURATION;
- unsigned char package[data.size()];
- std::copy(data.begin(), data.end(), package);
- auto status = libusb_control_transfer(m_device, requestType, request, 0, 0, package, data.size(), DefaultTimeout);
- if (status) {
- std::cerr << "Sending data error: " << libusb_strerror(status) << " [" << status << "]" << std::endl;
- return false;
- }
- return true;
- }
- }
|