10 #include <mrpt/config.h> 15 #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE) 19 #if MRPT_FTDI_VERSION >= 0x120 20 #include <libusb-1.0/libusb.h> 42 auto* newCtx =
new ftdi_context[1];
46 int ret = ftdi_init(newCtx);
47 if (ret)
THROW_EXCEPTION(
"There was a problem initializing ftdi_context.");
50 m_ftdi_context =
static_cast<void*
>(newCtx);
53 "MRPT has been compiled without FTDI support. Please, reconfigure and " 57 #if MRPT_FTDI_VERSION >= 0x120 71 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
80 #if MRPT_FTDI_VERSION >= 0x120 86 [maybe_unused]]
const std::string& serialNumber)
103 void* myDev =
nullptr;
105 for (
auto& lstDev : lstDevs)
107 if (lstDev.ftdi_serial == serialNumber)
109 myDev = lstDev.usb_device_struct;
116 "USB device with serial number '%s' not found.",
117 serialNumber.c_str());
120 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
122 int ret = ftdi_usb_open_dev(
124 #
if MRPT_FTDI_VERSION >= 0x120
125 (
struct libusb_device*)myDev
127 (
struct usb_device*)myDev
147 #if MRPT_FTDI_VERSION >= 0x120 151 libusb_device** list;
152 ssize_t nDevices = libusb_get_device_list(
nullptr, &list);
154 for (
unsigned int i = 0; i < nDevices; i++)
156 libusb_device* device = list[i];
157 struct libusb_device_descriptor desc;
158 if (0 != libusb_get_device_descriptor(device, &desc))
continue;
159 if (!desc.idVendor)
continue;
162 newEntry.usb_device_struct = (
void*)device;
168 libusb_device_handle* handle;
169 if (0 != libusb_open(device, &handle))
continue;
174 ret = libusb_get_string_descriptor_ascii(
175 handle, desc.iManufacturer, (
unsigned char*)buf,
sizeof(buf) - 1);
176 if (ret < 0)
continue;
181 ret = libusb_get_string_descriptor_ascii(
182 handle, desc.iProduct, (
unsigned char*)buf,
sizeof(buf) - 1);
183 if (ret < 0)
continue;
188 ret = libusb_get_string_descriptor_ascii(
189 handle, desc.iSerialNumber, (
unsigned char*)buf,
sizeof(buf) - 1);
190 if (ret < 0)
continue;
194 outList.push_back(newEntry);
201 struct usb_device* dev;
204 if (usb_find_busses() < 0)
THROW_EXCEPTION(
"usb_find_busses() failed");
205 if (usb_find_devices() < 0)
THROW_EXCEPTION(
"usb_find_devices() failed");
207 for (bus = usb_busses; bus; bus = bus->next)
208 for (dev = bus->devices; dev; dev = dev->next)
209 recursive_fill_list_devices(
213 if (getenv(
"VERBOSE") !=
nullptr)
215 printf(
"[CInterfaceFTDI::ListAllDevices] List: \n");
216 for (
auto i = outList.begin(); i != outList.end(); ++i)
218 "USB DEV: V=%04X P=%04X S=%s\n", i->usb_idVendor,
219 i->usb_idProduct, i->ftdi_serial.c_str());
226 void CInterfaceFTDI::recursive_fill_list_devices(
227 [[maybe_unused]]
void* usb_device_structure,
231 #if MRPT_FTDI_VERSION >= 0x120 233 throw std::runtime_error(
"Should not have got to this function!");
236 struct usb_device* dev = (
struct usb_device*)usb_device_structure;
238 if (dev->descriptor.idProduct && dev->descriptor.idVendor)
243 newEntry.usb_device_struct = (
void*)dev;
248 usb_dev_handle* hUSB = usb_open(dev);
252 char manufacturer[3000];
253 if ((strLen = usb_get_string_simple(
254 hUSB, dev->descriptor.iManufacturer, manufacturer,
255 sizeof(manufacturer))) <= 0)
257 cerr <<
"Couldn't open " << (int)dev->descriptor.iManufacturer
263 manufacturer[strLen] =
'\0';
271 char description[3000];
272 if ((strLen = usb_get_string_simple(
273 hUSB, dev->descriptor.iProduct, description,
274 sizeof(description))) <= 0)
280 description[strLen] =
'\0';
288 if ((strLen = usb_get_string_simple(
289 hUSB, dev->descriptor.iSerialNumber, serial,
290 sizeof(serial))) <= 0)
296 serial[strLen] =
'\0';
303 outList.push_back(newEntry);
309 for (
unsigned char j = 0; j < dev->num_children; j++)
310 recursive_fill_list_devices((
void*)dev->children[j], outList);
320 [[maybe_unused]]
void* lpvBuffer, [[maybe_unused]]
unsigned long dwBuffSize,
321 [[maybe_unused]]
unsigned long* lpdwBytesRead)
325 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
327 int ret = ftdi_read_data(ctx, (
unsigned char*)lpvBuffer, dwBuffSize);
329 *lpdwBytesRead = ret;
332 if (!strcmp(
"usb bulk read failed", ctx->error_str))
348 [[maybe_unused]]
const void* lpvBuffer,
349 [[maybe_unused]]
unsigned long dwBuffSize,
350 [[maybe_unused]]
unsigned long* lpdwBytes)
354 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
356 int ret = ftdi_write_data(ctx, (
unsigned char*)lpvBuffer, dwBuffSize);
372 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
373 return ctx->usb_dev !=
nullptr;
385 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
389 ctx->usb_dev =
nullptr;
406 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
409 if (ftdi_usb_reset(ctx) < 0)
THROW_EXCEPTION(
"Error resetting device");
421 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
424 if (ftdi_usb_purge_buffers(ctx) < 0)
437 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
440 if (ftdi_set_latency_timer(ctx, latency_ms) < 0)
449 [[maybe_unused]]
unsigned long dwReadTimeout_ms,
450 [[maybe_unused]]
unsigned long dwWriteTimeout_ms)
453 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
470 <<
"USB ID (Vendor/Product) : " std::ostream & operator<<(std::ostream &o, const TFTDIDevice &d)
Print out all the information of a FTDI device in textual form.
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
bool isOpen()
Checks whether the chip has been successfully open.
#define MRPT_TRY_END
The end of a standard MRPT "try...catch()" block that allows tracing throw the call stack after an ex...
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
#define THROW_EXCEPTION(msg)
std::string std::string format(std::string_view fmt, ARGS &&... args)
std::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
mrpt::containers::circular_buffer< uint8_t > m_readBuffer
Used in Read.
CInterfaceFTDI()
Constructor, which loads driver interface (the DLL under Windows).
#define MRPT_TRY_START
The start of a standard MRPT "try...catch()" block that allows tracing throw the call stack after an ...
void ListAllDevices(TFTDIDeviceList &outList)
Generates a list with all FTDI devices connected right now.
#define ASSERT_(f)
Defines an assertion mechanism.
std::string ftdi_description
void Close()
Close the USB device.
void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms)
Change read & write timeouts, in milliseconds.
void OpenBySerialNumber(const std::string &serialNumber)
Open by device serial number.
void ResetDevice()
Reset the USB device.
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
Serial and networking devices and utilities.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
std::string ftdi_manufacturer
~CInterfaceFTDI() override
Destructor, which closes the connection with the chip and unloads the driver interface.
A list of FTDI devices and their descriptors.
void clear()
Delete all the stored data, if any.
void SetLatencyTimer(unsigned char latency_ms)
Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms...
void Purge()
Purge the I/O buffers.