10 #include <mrpt/config.h> 18 #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE) 22 #if MRPT_FTDI_VERSION >= 0x120 23 #include <libusb-1.0/libusb.h> 39 CInterfaceFTDI::CInterfaceFTDI() : m_readBuffer(4096)
45 ftdi_context* newCtx =
new ftdi_context[1];
49 int ret = ftdi_init(newCtx);
50 if (ret)
THROW_EXCEPTION(
"There was a problem initializing ftdi_context.");
56 "MRPT has been compiled without FTDI support. Please, reconfigure and " 60 #if MRPT_FTDI_VERSION >= 0x120 83 #if MRPT_FTDI_VERSION >= 0x120 122 void* myDev =
nullptr;
127 if (it->ftdi_serial == serialNumber)
129 myDev = it->usb_device_struct;
136 "USB device with serial number '%s' not found.",
137 serialNumber.c_str());
142 int ret = ftdi_usb_open_dev(
144 #
if MRPT_FTDI_VERSION >= 0x120
145 (
struct libusb_device*)myDev
147 (
struct usb_device*)myDev
169 #if MRPT_FTDI_VERSION >= 0x120 173 libusb_device** list;
174 ssize_t nDevices = libusb_get_device_list(
nullptr, &list);
176 for (
unsigned int i = 0; i < nDevices; i++)
178 libusb_device* device = list[i];
179 struct libusb_device_descriptor desc;
180 if (0 != libusb_get_device_descriptor(device, &desc))
continue;
181 if (!desc.idVendor)
continue;
184 newEntry.usb_device_struct = (
void*)device;
190 libusb_device_handle* handle;
191 if (0 != libusb_open(device, &handle))
continue;
196 ret = libusb_get_string_descriptor_ascii(
197 handle, desc.iManufacturer, (
unsigned char*)buf,
sizeof(buf) - 1);
198 if (ret < 0)
continue;
203 ret = libusb_get_string_descriptor_ascii(
204 handle, desc.iProduct, (
unsigned char*)buf,
sizeof(buf) - 1);
205 if (ret < 0)
continue;
210 ret = libusb_get_string_descriptor_ascii(
211 handle, desc.iSerialNumber, (
unsigned char*)buf,
sizeof(buf) - 1);
212 if (ret < 0)
continue;
216 outList.push_back(newEntry);
223 struct usb_device* dev;
226 if (usb_find_busses() < 0)
THROW_EXCEPTION(
"usb_find_busses() failed");
227 if (usb_find_devices() < 0)
THROW_EXCEPTION(
"usb_find_devices() failed");
229 for (bus = usb_busses; bus; bus = bus->next)
230 for (dev = bus->devices; dev; dev = dev->next)
235 if (getenv(
"VERBOSE") !=
nullptr)
237 printf(
"[CInterfaceFTDI::ListAllDevices] List: \n");
239 i != outList.end(); ++i)
241 "USB DEV: V=%04X P=%04X S=%s\n", i->usb_idVendor,
242 i->usb_idProduct, i->ftdi_serial.c_str());
255 #if MRPT_FTDI_VERSION >= 0x120 257 throw std::runtime_error(
"Should not have got to this function!");
260 struct usb_device* dev = (
struct usb_device*)usb_device_structure;
262 if (dev->descriptor.idProduct && dev->descriptor.idVendor)
267 newEntry.usb_device_struct = (
void*)dev;
272 usb_dev_handle* hUSB = usb_open(dev);
276 char manufacturer[3000];
277 if ((strLen = usb_get_string_simple(
278 hUSB, dev->descriptor.iManufacturer, manufacturer,
279 sizeof(manufacturer))) <= 0)
281 cerr <<
"Couldn't open " << (int)dev->descriptor.iManufacturer
287 manufacturer[strLen] =
'\0';
295 char description[3000];
296 if ((strLen = usb_get_string_simple(
297 hUSB, dev->descriptor.iProduct, description,
298 sizeof(description))) <= 0)
304 description[strLen] =
'\0';
312 if ((strLen = usb_get_string_simple(
313 hUSB, dev->descriptor.iSerialNumber, serial,
314 sizeof(serial))) <= 0)
320 serial[strLen] =
'\0';
327 outList.push_back(newEntry);
333 for (
unsigned char j = 0; j < dev->num_children; j++)
347 void* lpvBuffer,
unsigned long dwBuffSize,
unsigned long* lpdwBytesRead)
353 int ret = ftdi_read_data(ctx, (
unsigned char*)lpvBuffer, dwBuffSize);
355 *lpdwBytesRead = ret;
358 if (!strcmp(
"usb bulk read failed", ctx->error_str))
378 const void* lpvBuffer,
unsigned long dwBuffSize,
unsigned long* lpdwBytes)
384 int ret = ftdi_write_data(ctx, (
unsigned char*)lpvBuffer, dwBuffSize);
405 return ctx->usb_dev !=
nullptr;
421 ctx->usb_dev =
nullptr;
441 if (ftdi_usb_reset(ctx) < 0)
THROW_EXCEPTION(
"Error resetting device");
456 if (ftdi_usb_purge_buffers(ctx) < 0)
472 if (ftdi_set_latency_timer(ctx, latency_ms) < 0)
483 unsigned long dwReadTimeout_ms,
unsigned long dwWriteTimeout_ms)
506 <<
"USB ID (Vendor/Product) : " bool isOpen()
Checks whether the chip has been successfully open.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void ResetDevice()
Reset the USB device.
void clear()
Delete all the stored data, if any.
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
void OpenBySerialNumber(const std::string &serialNumber)
Open by device serial number.
const Scalar * const_iterator
void recursive_fill_list_devices(void *usb_device_structure, TFTDIDeviceList &outList)
Process recursively a USB device and its children:
void Close()
Close the USB device.
mrpt::utils::circular_buffer< uint8_t > m_readBuffer
Used in Read.
std::string ftdi_description
void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms)
Change read & write timeouts, in milliseconds.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void ListAllDevices(TFTDIDeviceList &outList)
Generates a list with all FTDI devices connected right now.
GLsizei const GLchar ** string
std::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CInterfaceFTDI & operator=(const CInterfaceFTDI &o)
This object cannot be copied.
std::ostream & operator<<(std::ostream &o, const TFTDIDevice &d)
Print out all the information of a FTDI device in textual form.
A definition of a CStream actually representing a USB connection to a FTDI chip.
CInterfaceFTDI()
Constructor, which loads driver interface (the DLL under Windows).
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
void Purge()
Purge the I/O buffers.
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
Serial and networking devices and utilities.
virtual ~CInterfaceFTDI()
Destructor, which closes the connection with the chip and unloads the driver interface.
void SetLatencyTimer(unsigned char latency_ms)
Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms...
std::string ftdi_manufacturer
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
A list of FTDI devices and their descriptors.