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 ftdi_context* 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 ftdi_context* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
80 #if MRPT_FTDI_VERSION >= 0x120 102 void* myDev =
nullptr;
107 if (it->ftdi_serial == serialNumber)
109 myDev = it->usb_device_struct;
116 "USB device with serial number '%s' not found.",
117 serialNumber.c_str());
120 ftdi_context* 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
149 #if MRPT_FTDI_VERSION >= 0x120 153 libusb_device** list;
154 ssize_t nDevices = libusb_get_device_list(
nullptr, &list);
156 for (
unsigned int i = 0; i < nDevices; i++)
158 libusb_device* device = list[i];
159 struct libusb_device_descriptor desc;
160 if (0 != libusb_get_device_descriptor(device, &desc))
continue;
161 if (!desc.idVendor)
continue;
164 newEntry.usb_device_struct = (
void*)device;
170 libusb_device_handle* handle;
171 if (0 != libusb_open(device, &handle))
continue;
176 ret = libusb_get_string_descriptor_ascii(
177 handle, desc.iManufacturer, (
unsigned char*)buf,
sizeof(buf) - 1);
178 if (ret < 0)
continue;
183 ret = libusb_get_string_descriptor_ascii(
184 handle, desc.iProduct, (
unsigned char*)buf,
sizeof(buf) - 1);
185 if (ret < 0)
continue;
190 ret = libusb_get_string_descriptor_ascii(
191 handle, desc.iSerialNumber, (
unsigned char*)buf,
sizeof(buf) - 1);
192 if (ret < 0)
continue;
196 outList.push_back(newEntry);
203 struct usb_device* dev;
206 if (usb_find_busses() < 0)
THROW_EXCEPTION(
"usb_find_busses() failed");
207 if (usb_find_devices() < 0)
THROW_EXCEPTION(
"usb_find_devices() failed");
209 for (bus = usb_busses; bus; bus = bus->next)
210 for (dev = bus->devices; dev; dev = dev->next)
211 recursive_fill_list_devices(
215 if (getenv(
"VERBOSE") !=
nullptr)
217 printf(
"[CInterfaceFTDI::ListAllDevices] List: \n");
219 i != outList.end(); ++i)
221 "USB DEV: V=%04X P=%04X S=%s\n", i->usb_idVendor,
222 i->usb_idProduct, i->ftdi_serial.c_str());
231 void CInterfaceFTDI::recursive_fill_list_devices(
235 #if MRPT_FTDI_VERSION >= 0x120 237 throw std::runtime_error(
"Should not have got to this function!");
240 struct usb_device* dev = (
struct usb_device*)usb_device_structure;
242 if (dev->descriptor.idProduct && dev->descriptor.idVendor)
247 newEntry.usb_device_struct = (
void*)dev;
252 usb_dev_handle* hUSB = usb_open(dev);
256 char manufacturer[3000];
257 if ((strLen = usb_get_string_simple(
258 hUSB, dev->descriptor.iManufacturer, manufacturer,
259 sizeof(manufacturer))) <= 0)
261 cerr <<
"Couldn't open " << (int)dev->descriptor.iManufacturer
267 manufacturer[strLen] =
'\0';
275 char description[3000];
276 if ((strLen = usb_get_string_simple(
277 hUSB, dev->descriptor.iProduct, description,
278 sizeof(description))) <= 0)
284 description[strLen] =
'\0';
292 if ((strLen = usb_get_string_simple(
293 hUSB, dev->descriptor.iSerialNumber, serial,
294 sizeof(serial))) <= 0)
300 serial[strLen] =
'\0';
307 outList.push_back(newEntry);
313 for (
unsigned char j = 0; j < dev->num_children; j++)
314 recursive_fill_list_devices((
void*)dev->children[j], outList);
327 void* lpvBuffer,
unsigned long dwBuffSize,
unsigned long* lpdwBytesRead)
331 ftdi_context* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
333 int ret = ftdi_read_data(ctx, (
unsigned char*)lpvBuffer, dwBuffSize);
335 *lpdwBytesRead = ret;
338 if (!strcmp(
"usb bulk read failed", ctx->error_str))
358 const void* lpvBuffer,
unsigned long dwBuffSize,
unsigned long* lpdwBytes)
362 ftdi_context* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
364 int ret = ftdi_write_data(ctx, (
unsigned char*)lpvBuffer, dwBuffSize);
384 ftdi_context* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
385 return ctx->usb_dev !=
nullptr;
397 ftdi_context* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
401 ctx->usb_dev =
nullptr;
418 ftdi_context* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
421 if (ftdi_usb_reset(ctx) < 0)
THROW_EXCEPTION(
"Error resetting device");
433 ftdi_context* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
436 if (ftdi_usb_purge_buffers(ctx) < 0)
449 ftdi_context* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
452 if (ftdi_set_latency_timer(ctx, latency_ms) < 0)
463 unsigned long dwReadTimeout_ms,
unsigned long dwWriteTimeout_ms)
466 ftdi_context* ctx =
static_cast<ftdi_context*
>(m_ftdi_context);
486 <<
"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::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
mrpt::containers::circular_buffer< uint8_t > m_readBuffer
Used in Read.
virtual ~CInterfaceFTDI()
Destructor, which closes the connection with the chip and unloads the driver interface.
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.
GLsizei const GLchar ** string
void ResetDevice()
Reset the USB device.
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Serial and networking devices and utilities.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
const Scalar * const_iterator
std::string ftdi_manufacturer
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.