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 102 void* myDev =
nullptr;
104 for (
auto& lstDev : lstDevs)
106 if (lstDev.ftdi_serial == serialNumber)
108 myDev = lstDev.usb_device_struct;
115 "USB device with serial number '%s' not found.",
116 serialNumber.c_str());
119 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
121 int ret = ftdi_usb_open_dev(
123 #
if MRPT_FTDI_VERSION >= 0x120
124 (
struct libusb_device*)myDev
126 (
struct usb_device*)myDev
148 #if MRPT_FTDI_VERSION >= 0x120 152 libusb_device** list;
153 ssize_t nDevices = libusb_get_device_list(
nullptr, &list);
155 for (
unsigned int i = 0; i < nDevices; i++)
157 libusb_device* device = list[i];
158 struct libusb_device_descriptor desc;
159 if (0 != libusb_get_device_descriptor(device, &desc))
continue;
160 if (!desc.idVendor)
continue;
163 newEntry.usb_device_struct = (
void*)device;
169 libusb_device_handle* handle;
170 if (0 != libusb_open(device, &handle))
continue;
175 ret = libusb_get_string_descriptor_ascii(
176 handle, desc.iManufacturer, (
unsigned char*)buf,
sizeof(buf) - 1);
177 if (ret < 0)
continue;
182 ret = libusb_get_string_descriptor_ascii(
183 handle, desc.iProduct, (
unsigned char*)buf,
sizeof(buf) - 1);
184 if (ret < 0)
continue;
189 ret = libusb_get_string_descriptor_ascii(
190 handle, desc.iSerialNumber, (
unsigned char*)buf,
sizeof(buf) - 1);
191 if (ret < 0)
continue;
195 outList.push_back(newEntry);
202 struct usb_device* dev;
205 if (usb_find_busses() < 0)
THROW_EXCEPTION(
"usb_find_busses() failed");
206 if (usb_find_devices() < 0)
THROW_EXCEPTION(
"usb_find_devices() failed");
208 for (bus = usb_busses; bus; bus = bus->next)
209 for (dev = bus->devices; dev; dev = dev->next)
210 recursive_fill_list_devices(
214 if (getenv(
"VERBOSE") !=
nullptr)
216 printf(
"[CInterfaceFTDI::ListAllDevices] List: \n");
217 for (
auto i = outList.begin(); i != outList.end(); ++i)
219 "USB DEV: V=%04X P=%04X S=%s\n", i->usb_idVendor,
220 i->usb_idProduct, i->ftdi_serial.c_str());
229 void CInterfaceFTDI::recursive_fill_list_devices(
233 #if MRPT_FTDI_VERSION >= 0x120 235 throw std::runtime_error(
"Should not have got to this function!");
238 struct usb_device* dev = (
struct usb_device*)usb_device_structure;
240 if (dev->descriptor.idProduct && dev->descriptor.idVendor)
245 newEntry.usb_device_struct = (
void*)dev;
250 usb_dev_handle* hUSB = usb_open(dev);
254 char manufacturer[3000];
255 if ((strLen = usb_get_string_simple(
256 hUSB, dev->descriptor.iManufacturer, manufacturer,
257 sizeof(manufacturer))) <= 0)
259 cerr <<
"Couldn't open " << (int)dev->descriptor.iManufacturer
265 manufacturer[strLen] =
'\0';
273 char description[3000];
274 if ((strLen = usb_get_string_simple(
275 hUSB, dev->descriptor.iProduct, description,
276 sizeof(description))) <= 0)
282 description[strLen] =
'\0';
290 if ((strLen = usb_get_string_simple(
291 hUSB, dev->descriptor.iSerialNumber, serial,
292 sizeof(serial))) <= 0)
298 serial[strLen] =
'\0';
305 outList.push_back(newEntry);
311 for (
unsigned char j = 0; j < dev->num_children; j++)
312 recursive_fill_list_devices((
void*)dev->children[j], outList);
325 void* lpvBuffer,
unsigned long dwBuffSize,
unsigned long* lpdwBytesRead)
329 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
331 int ret = ftdi_read_data(ctx, (
unsigned char*)lpvBuffer, dwBuffSize);
333 *lpdwBytesRead = ret;
336 if (!strcmp(
"usb bulk read failed", ctx->error_str))
356 const void* lpvBuffer,
unsigned long dwBuffSize,
unsigned long* lpdwBytes)
360 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
362 int ret = ftdi_write_data(ctx, (
unsigned char*)lpvBuffer, dwBuffSize);
382 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
383 return ctx->usb_dev !=
nullptr;
395 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
399 ctx->usb_dev =
nullptr;
416 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
419 if (ftdi_usb_reset(ctx) < 0)
THROW_EXCEPTION(
"Error resetting device");
431 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
434 if (ftdi_usb_purge_buffers(ctx) < 0)
447 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
450 if (ftdi_set_latency_timer(ctx, latency_ms) < 0)
461 unsigned long dwReadTimeout_ms,
unsigned long dwWriteTimeout_ms)
464 auto* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
484 <<
"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...
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
void Purge()
Purge the I/O buffers.