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() :
46 ftdi_context *newCtx =
new ftdi_context[1];
50 int ret = ftdi_init(newCtx);
51 if (ret)
THROW_EXCEPTION(
"There was a problem initializing ftdi_context.");
56 THROW_EXCEPTION(
"MRPT has been compiled without FTDI support. Please, reconfigure and recompile MRPT.")
59 #if MRPT_FTDI_VERSION>=0x120 83 #if MRPT_FTDI_VERSION>=0x120 128 if (it->ftdi_serial == serialNumber)
130 myDev = it->usb_device_struct;
141 int ret=ftdi_usb_open_dev( ctx,
142 #
if MRPT_FTDI_VERSION>=0x120
143 (
struct libusb_device*) myDev
145 (
struct usb_device*) myDev
167 #if MRPT_FTDI_VERSION>=0x120 171 libusb_device **list;
172 ssize_t nDevices = libusb_get_device_list(NULL,&list);
174 for (
unsigned int i = 0; i < nDevices; i++) {
175 libusb_device *device = list[i];
176 struct libusb_device_descriptor desc;
177 if (0!=libusb_get_device_descriptor(device,&desc))
179 if (!desc.idVendor)
continue;
182 newEntry.usb_device_struct = (
void*)device;
188 libusb_device_handle *handle;
189 if (0!=libusb_open(device,&handle))
195 ret=libusb_get_string_descriptor_ascii(handle,desc.iManufacturer,(
unsigned char*)buf,
sizeof(buf)-1);
201 ret=libusb_get_string_descriptor_ascii(handle,desc.iProduct,(
unsigned char*)buf,
sizeof(buf)-1);
207 ret=libusb_get_string_descriptor_ascii(handle,desc.iSerialNumber,(
unsigned char*)buf,
sizeof(buf)-1);
212 outList.push_back(newEntry);
219 struct usb_device *dev;
222 if (usb_find_busses() < 0)
224 if (usb_find_devices() < 0)
227 for (bus = usb_busses; bus; bus = bus->next)
228 for (dev = bus->devices; dev; dev = dev->next)
232 if (getenv(
"VERBOSE")!=NULL)
234 printf(
"[CInterfaceFTDI::ListAllDevices] List: \n");
236 printf(
"USB DEV: V=%04X P=%04X S=%s\n",i->usb_idVendor,i->usb_idProduct, i->ftdi_serial.c_str());
249 #if MRPT_FTDI_VERSION>=0x120 251 throw std::runtime_error(
"Should not have got to this function!");
254 struct usb_device *dev = (
struct usb_device *) usb_device_structure;
256 if (dev->descriptor.idProduct && dev->descriptor.idVendor)
261 newEntry.usb_device_struct = (
void*)dev;
266 usb_dev_handle * hUSB = usb_open(dev);
270 char manufacturer[3000];
271 if ((strLen=usb_get_string_simple(hUSB, dev->descriptor.iManufacturer, manufacturer,
sizeof(manufacturer))) <=0)
273 cerr <<
"Couldn't open " << (int)dev->descriptor.iManufacturer << endl;
278 manufacturer[strLen]=
'\0';
286 char description[3000];
287 if ((strLen=usb_get_string_simple(hUSB, dev->descriptor.iProduct, description,
sizeof(description))) <=0)
293 description[strLen]=
'\0';
301 if ((strLen=usb_get_string_simple(hUSB, dev->descriptor.iSerialNumber, serial,
sizeof(serial))) <=0)
314 outList.push_back( newEntry );
320 for (
unsigned char j=0;j<dev->num_children;j++)
338 int ret = ftdi_read_data(ctx,(
unsigned char*)lpvBuffer, dwBuffSize );
340 *lpdwBytesRead = ret;
343 if (!strcmp(
"usb bulk read failed",ctx->error_str))
366 int ret = ftdi_write_data(ctx,(
unsigned char*)lpvBuffer, dwBuffSize );
385 return ctx->usb_dev != NULL;
400 ftdi_usb_close( ctx );
421 if (ftdi_usb_reset(ctx)<0)
437 if (ftdi_usb_purge_buffers(ctx)<0)
453 if (ftdi_set_latency_timer(ctx, latency_ms)<0)
void SetTimeouts(unsigned long dwReadTimeout_ms, unsigned long dwWriteTimeout_ms)
Change read & write timeouts, in milliseconds.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::ostream HWDRIVERS_IMPEXP & operator<<(std::ostream &o, const TFTDIDevice &d)
Print out all the information of a FTDI device in textual form.
void ftdi_write(const void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytes)
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.
bool isOpen()
Checks whether the chip has been successfully open.
Contains classes for various device interfaces.
const Scalar * const_iterator
void Purge()
Purge the I/O buffers.
CInterfaceFTDI()
Constructor, which loads driver interface (the DLL under Windows).
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
std::string ftdi_description
CInterfaceFTDI & operator=(const CInterfaceFTDI &o)
This object cannot be copied.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
void ftdi_read(void *lpvBuffer, unsigned long dwBuffSize, unsigned long *lpdwBytesRead)
GLsizei const GLchar ** string
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string ftdi_manufacturer
void SetLatencyTimer(unsigned char latency_ms)
Change the latency timer (in milliseconds) implemented on the FTDI chip: for a few ms...
void recursive_fill_list_devices(void *usb_device_structure, TFTDIDeviceList &outList)
Process recursively a USB device and its children:
void ListAllDevices(TFTDIDeviceList &outList)
Generates a list with all FTDI devices connected right now.
std::deque< TFTDIDevice > TFTDIDeviceList
Used in CInterfaceFTDI::ListAllDevices.
A list of FTDI devices and their descriptors.
A definition of a CStream actually representing a USB connection to a FTDI chip.
void ResetDevice()
Reset the USB device.
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
mrpt::utils::circular_buffer< uint8_t > m_readBuffer
Used in Read.
virtual ~CInterfaceFTDI()
Destructor, which closes the connection with the chip and unloads the driver interface.
void Close()
Close the USB device.