X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=blobdiff_plain;f=ftdipp%2Fftdi.cpp;h=afb84fb9367ef8e26eb39b8ef1247522ce80a0c7;hp=92b6e1fa48b073b2829ab8b0351073df1075edd1;hb=928bc100257e5d2bc18a35ede7597da1e9b92ee1;hpb=c45d26308211c383903cb06f15ba7575d807fe06 diff --git a/ftdipp/ftdi.cpp b/ftdipp/ftdi.cpp index 92b6e1f..afb84fb 100644 --- a/ftdipp/ftdi.cpp +++ b/ftdipp/ftdi.cpp @@ -1,12 +1,12 @@ /*************************************************************************** - ftdi.cpp - C++ wraper for libftdi + ftdi.cpp - C++ wrapper for libftdi ------------------- begin : Mon Oct 13 2008 - copyright : (C) 2008-2014 by Marek Vavruša / libftdi developers + copyright : (C) 2008-2020 by Marek Vavruša / libftdi developers email : opensource@intra2net.com and marek@vavrusa.com ***************************************************************************/ /* -Copyright (C) 2008-2014 by Marek Vavruša / libftdi developers +Copyright (C) 2008-2017 by Marek Vavruša / libftdi developers The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). @@ -27,6 +27,7 @@ This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ #include +#define _FTDI_DISABLE_DEPRECATED #include "ftdi.hpp" #include "ftdi_i.h" #include "ftdi.h" @@ -87,7 +88,7 @@ int Context::open(int vendor, int product) if (ret < 0) return ret; - return get_strings_and_reopen(); + return get_strings_and_reopen(false,false,false); } int Context::open(int vendor, int product, const std::string& description, const std::string& serial, unsigned int index) @@ -106,7 +107,7 @@ int Context::open(int vendor, int product, const std::string& description, const if (ret < 0) return ret; - return get_strings_and_reopen(); + return get_strings_and_reopen(false,!description.empty(),!serial.empty()); } int Context::open(const std::string& description) @@ -116,7 +117,7 @@ int Context::open(const std::string& description) if (ret < 0) return ret; - return get_strings_and_reopen(); + return get_strings_and_reopen(false,true,false); } int Context::open(struct libusb_device *dev) @@ -144,12 +145,52 @@ int Context::reset() int Context::flush(int mask) { - int ret = 1; + int ret; + + switch (mask & (Input | Output)) { + case Input: + ret = ftdi_usb_purge_rx_buffer(d->ftdi); + break; - if (mask & Input) - ret &= ftdi_usb_purge_rx_buffer(d->ftdi); - if (mask & Output) - ret &= ftdi_usb_purge_tx_buffer(d->ftdi); + case Output: + ret = ftdi_usb_purge_tx_buffer(d->ftdi); + break; + + case Input | Output: + ret = ftdi_usb_purge_buffers(d->ftdi); + break; + + default: + // Emulate behavior of previous version. + ret = 1; + break; + } + + return ret; +} + +int Context::tcflush(int mask) +{ + int ret; + + switch (mask & (Input | Output)) { + case Input: + ret = ftdi_tciflush(d->ftdi); + break; + + case Output: + ret = ftdi_tcoflush(d->ftdi); + break; + + case Input | Output: + ret = ftdi_tcioflush(d->ftdi); + break; + + default: + // Emulate behavior of previous version. + ret = 1; + break; + } return ret; } @@ -219,7 +260,7 @@ int Context::read_chunk_size() return chunk; } -int Context::write(unsigned char *buf, int size) +int Context::write(const unsigned char *buf, int size) { return ftdi_write_data(d->ftdi, buf, size); } @@ -319,41 +360,46 @@ const char* Context::error_string() return ftdi_get_error_string(d->ftdi); } -int Context::get_strings() +int Context::get_strings(bool vendor, bool description, bool serial) { // Prepare buffers - char vendor[512], desc[512], serial[512]; + char ivendor[512], idesc[512], iserial[512]; - int ret = ftdi_usb_get_strings(d->ftdi, d->dev, vendor, 512, desc, 512, serial, 512); + int ret = ftdi_usb_get_strings(d->ftdi, d->dev, vendor?ivendor:NULL, 512, description?idesc:NULL, 512, serial?iserial:NULL, 512); if (ret < 0) return -1; - d->vendor = vendor; - d->description = desc; - d->serial = serial; + d->vendor = ivendor; + d->description = idesc; + d->serial = iserial; return 1; } -int Context::get_strings_and_reopen() +int Context::get_strings_and_reopen(bool vendor, bool description, bool serial) { - if ( d->dev == 0 ) - { - d->dev = libusb_get_device(d->ftdi->usb_dev); - } + int ret = 0; - // Get device strings (closes device) - int ret=get_strings(); - if (ret < 0) + if(vendor || description || serial) { - d->open = 0; - return ret; - } + if (d->dev == 0) + { + d->dev = libusb_get_device(d->ftdi->usb_dev); + } + + // Get device strings (closes device) + ret=get_strings(vendor, description, serial); + if (ret < 0) + { + d->open = 0; + return ret; + } - // Reattach device - ret = ftdi_usb_open_dev(d->ftdi, d->dev); - d->open = (ret >= 0); + // Reattach device + ret = ftdi_usb_open_dev(d->ftdi, d->dev); + d->open = (ret >= 0); + } return ret; } @@ -362,6 +408,8 @@ int Context::get_strings_and_reopen() */ const std::string& Context::vendor() { + if(d->vendor.empty()) + get_strings_and_reopen(true,false,false); return d->vendor; } @@ -369,6 +417,8 @@ const std::string& Context::vendor() */ const std::string& Context::description() { + if(d->description.empty()) + get_strings_and_reopen(false,true,false); return d->description; } @@ -376,6 +426,8 @@ const std::string& Context::description() */ const std::string& Context::serial() { + if(d->serial.empty()) + get_strings_and_reopen(false,false,true); return d->serial; }