ftdi.hpp
Go to the documentation of this file.
/***************************************************************************
ftdi.hpp - C++ wrapper for libftdi
-------------------
begin : Mon Oct 13 2008
copyright : (C) 2008-2020 by Marek Vavruša and libftdi developers
email : opensource@intra2net.com and marek@vavrusa.com
***************************************************************************/
/*
Copyright (C) 2008-2017 by Marek Vavruša and libftdi developers
The software in this package is distributed under the GNU General
Public License version 2 (with a special exception described below).
A copy of GNU General Public License (GPL) is included in this distribution,
in the file COPYING.GPL.
As a special exception, if other files instantiate templates or use macros
or inline functions from this file, or you compile this file and link it
with other works to produce a work based on this file, this file
does not by itself cause the resulting work to be covered
by the GNU General Public License.
However the source code for this file must still be made available
in accordance with section (3) of the GNU General Public License.
This exception does not invalidate any other reasons why a work based
on this file might be covered by the GNU General Public License.
*/
#ifndef __libftdi_hpp__
#define __libftdi_hpp__
#include <list>
#include <string>
#include <boost/shared_ptr.hpp>
#include <ftdi.h>
namespace Ftdi
{
/* Forward declarations*/
class List;
class Eeprom;
class Context
{
/* Friends */
friend class Eeprom;
friend class List;
public:
enum Direction
{
Input = 0x2,
Output = 0x1,
};
enum ModemCtl
{
Dtr = 0x2,
Rts = 0x1,
};
/* Constructor, Destructor */
Context();
~Context();
/* Properties */
Eeprom* eeprom();
const std::string& vendor();
const std::string& description();
const std::string& serial();
/* Device manipulators */
bool is_open();
int open(struct libusb_device *dev = 0);
int open(int vendor, int product);
int open(int vendor, int product, const std::string& description, const std::string& serial = std::string(), unsigned int index=0);
int open(const std::string& description);
int close();
int reset();
int DEPRECATED(flush)(int mask = Input|Output);
int tcflush(int mask = Input|Output);
int set_interface(enum ftdi_interface interface);
void set_usb_device(struct libusb_device_handle *dev);
/* Line manipulators */
int set_baud_rate(int baudrate);
int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, enum ftdi_break_type break_type);
int get_usb_read_timeout() const;
void set_usb_read_timeout(int usb_read_timeout);
int get_usb_write_timeout() const;
void set_usb_write_timeout(int usb_write_timeout);
/* I/O */
int read(unsigned char *buf, int size);
int write(const unsigned char *buf, int size);
int set_read_chunk_size(unsigned int chunksize);
int set_write_chunk_size(unsigned int chunksize);
int read_chunk_size();
int write_chunk_size();
/* Async IO
TODO: should wrap?
int writeAsync(const unsigned char *buf, int size);
void asyncComplete(int wait_for_more);
*/
/* Flow control */
int set_event_char(unsigned char eventch, unsigned char enable);
int set_error_char(unsigned char errorch, unsigned char enable);
int set_flow_control(int flowctrl);
int set_modem_control(int mask = Dtr|Rts);
int set_latency(unsigned char latency);
int set_dtr(bool state);
int set_rts(bool state);
unsigned short poll_modem_status();
unsigned latency();
/* BitBang mode */
int set_bitmode(unsigned char bitmask, unsigned char mode);
int set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode);
int bitbang_disable();
int read_pins(unsigned char *pins);
/* Misc */
const char* error_string();
protected:
int get_strings(bool vendor=true, bool description=true, bool serial=true);
int get_strings_and_reopen(bool vendor=true, bool description=true, bool serial=true);
/* Properties */
struct ftdi_context* context();
void set_context(struct ftdi_context* context);
void set_usb_device(struct libusb_device *dev);
private:
class Private;
boost::shared_ptr<Private> d;
};
class Eeprom
{
public:
Eeprom(Context* parent);
~Eeprom();
int init_defaults(char *manufacturer, char* product, char * serial);
int chip_id(unsigned int *chipid);
int build(unsigned char *output);
int read(unsigned char *eeprom);
int write(unsigned char *eeprom);
int read_location(int eeprom_addr, unsigned short *eeprom_val);
int write_location(int eeprom_addr, unsigned short eeprom_val);
int erase();
private:
class Private;
boost::shared_ptr<Private> d;
};
class List
{
public:
List(struct ftdi_device_list* devlist = 0);
~List();
static List* find_all(Context &context, int vendor, int product);
typedef std::list<Context> ListType;
typedef ListType::iterator iterator;
typedef ListType::const_iterator const_iterator;
typedef ListType::reverse_iterator reverse_iterator;
typedef ListType::const_reverse_iterator const_reverse_iterator;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator rbegin() const;
const_reverse_iterator rend() const;
ListType::size_type size() const;
bool empty() const;
void clear();
void push_back(const Context& element);
void push_front(const Context& element);
iterator erase(iterator pos);
iterator erase(iterator beg, iterator end);
private:
class Private;
boost::shared_ptr<Private> d;
};
}
#endif
Ftdi::Context::Private
Definition ftdi.cpp:39
Ftdi::Context
FTDI device context. Represents single FTDI device context.
Definition ftdi.hpp:48
Ftdi::Context::error_string
const char * error_string()
Definition ftdi.cpp:358
Ftdi::Context::Context
Context()
Constructor.
Definition ftdi.cpp:67
Ftdi::Context::set_flow_control
int set_flow_control(int flowctrl)
Definition ftdi.cpp:282
Ftdi::Context::set_baud_rate
int set_baud_rate(int baudrate)
Definition ftdi.cpp:209
Ftdi::Context::description
const std::string & description()
Device strings properties.
Definition ftdi.cpp:418
Ftdi::Context::read
int read(unsigned char *buf, int size)
Definition ftdi.cpp:244
Ftdi::Context::flush
int DEPRECATED() flush(int mask=Input|Output)
Definition ftdi.cpp:146
Ftdi::Context::is_open
bool is_open()
Definition ftdi.cpp:78
Ftdi::Context::reset
int reset()
Definition ftdi.cpp:141
Ftdi::Context::serial
const std::string & serial()
Device strings properties.
Definition ftdi.cpp:427
Ftdi::Context::set_usb_device
void set_usb_device(struct libusb_device_handle *dev)
Definition ftdi.cpp:203
Ftdi::Context::set_usb_write_timeout
void set_usb_write_timeout(int usb_write_timeout)
Definition ftdi.cpp:239
Ftdi::Context::get_usb_read_timeout
int get_usb_read_timeout() const
Definition ftdi.cpp:224
Ftdi::Context::read_chunk_size
int read_chunk_size()
Definition ftdi.cpp:254
Ftdi::Context::set_write_chunk_size
int set_write_chunk_size(unsigned int chunksize)
Definition ftdi.cpp:268
Ftdi::Context::set_error_char
int set_error_char(unsigned char errorch, unsigned char enable)
Definition ftdi.cpp:333
Ftdi::Context::set_dtr
int set_dtr(bool state)
Definition ftdi.cpp:299
Ftdi::Context::Direction
Direction
Direction flags for flush().
Definition ftdi.hpp:57
Ftdi::Context::Input
@ Input
Definition ftdi.hpp:58
Ftdi::Context::Output
@ Output
Definition ftdi.hpp:59
Ftdi::Context::set_latency
int set_latency(unsigned char latency)
Definition ftdi.cpp:309
Ftdi::Context::set_read_chunk_size
int set_read_chunk_size(unsigned int chunksize)
Definition ftdi.cpp:249
Ftdi::Context::tcflush
int tcflush(int mask=Input|Output)
Definition ftdi.cpp:172
Ftdi::Context::open
int open(struct libusb_device *dev=0)
Definition ftdi.cpp:123
Ftdi::Context::set_event_char
int set_event_char(unsigned char eventch, unsigned char enable)
Definition ftdi.cpp:328
Ftdi::Context::latency
unsigned latency()
Definition ftdi.cpp:314
Ftdi::Context::poll_modem_status
unsigned short poll_modem_status()
Definition ftdi.cpp:321
Ftdi::Context::set_interface
int set_interface(enum ftdi_interface interface)
Definition ftdi.cpp:198
Ftdi::Context::get_strings
int get_strings(bool vendor=true, bool description=true, bool serial=true)
Definition ftdi.cpp:363
Ftdi::Context::read_pins
int read_pins(unsigned char *pins)
Definition ftdi.cpp:353
Ftdi::Context::set_context
void set_context(struct ftdi_context *context)
Definition ftdi.cpp:434
Ftdi::Context::close
int close()
Definition ftdi.cpp:134
Ftdi::Context::vendor
const std::string & vendor()
Device strings properties.
Definition ftdi.cpp:409
Ftdi::Context::set_usb_read_timeout
void set_usb_read_timeout(int usb_read_timeout)
Definition ftdi.cpp:229
Ftdi::Context::write
int write(const unsigned char *buf, int size)
Definition ftdi.cpp:263
Ftdi::Context::ModemCtl
ModemCtl
Modem control flags.
Definition ftdi.hpp:65
Ftdi::Context::Dtr
@ Dtr
Definition ftdi.hpp:66
Ftdi::Context::Rts
@ Rts
Definition ftdi.hpp:67
Ftdi::Context::set_line_property
int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
Definition ftdi.cpp:214
Ftdi::Context::bitbang_disable
int bitbang_disable()
Definition ftdi.cpp:348
Ftdi::Context::~Context
~Context()
Destructor.
Definition ftdi.cpp:74
Ftdi::Context::set_modem_control
int set_modem_control(int mask=Dtr|Rts)
Definition ftdi.cpp:287
Ftdi::Context::write_chunk_size
int write_chunk_size()
Definition ftdi.cpp:273
Ftdi::Context::set_bitmode
int set_bitmode(unsigned char bitmask, unsigned char mode)
Definition ftdi.cpp:338
Ftdi::Context::eeprom
Eeprom * eeprom()
Ftdi::Context::set_rts
int set_rts(bool state)
Definition ftdi.cpp:304
Ftdi::Context::context
struct ftdi_context * context()
Definition ftdi.cpp:445
Ftdi::Context::get_strings_and_reopen
int get_strings_and_reopen(bool vendor=true, bool description=true, bool serial=true)
Definition ftdi.cpp:380
Ftdi::Context::get_usb_write_timeout
int get_usb_write_timeout() const
Definition ftdi.cpp:234
Ftdi::Eeprom::Private
Definition ftdi.cpp:451
Ftdi::Eeprom
Device EEPROM.
Definition ftdi.hpp:154
Ftdi::Eeprom::erase
int erase()
Definition ftdi.cpp:506
Ftdi::Eeprom::read
int read(unsigned char *eeprom)
Definition ftdi.cpp:486
Ftdi::Eeprom::init_defaults
int init_defaults(char *manufacturer, char *product, char *serial)
Definition ftdi.cpp:471
Ftdi::Eeprom::write_location
int write_location(int eeprom_addr, unsigned short eeprom_val)
Definition ftdi.cpp:501
Ftdi::Eeprom::read_location
int read_location(int eeprom_addr, unsigned short *eeprom_val)
Definition ftdi.cpp:496
Ftdi::Eeprom::build
int build(unsigned char *output)
Definition ftdi.cpp:481
Ftdi::Eeprom::write
int write(unsigned char *eeprom)
Definition ftdi.cpp:491
Ftdi::Eeprom::~Eeprom
~Eeprom()
Definition ftdi.cpp:467
Ftdi::Eeprom::chip_id
int chip_id(unsigned int *chipid)
Definition ftdi.cpp:476
Ftdi::List::Private
Definition ftdi.cpp:512
Ftdi::List
Device list.
Definition ftdi.hpp:177
Ftdi::List::~List
~List()
Definition ftdi.cpp:544
Ftdi::List::ListType
std::list< Context > ListType
List type storing "Context" objects.
Definition ftdi.hpp:185
Ftdi::List::push_back
void push_back(const Context &element)
Definition ftdi.cpp:660
Ftdi::List::clear
void clear()
Definition ftdi.cpp:644
Ftdi::List::const_reverse_iterator
ListType::const_reverse_iterator const_reverse_iterator
Const reverse iterator type for the container.
Definition ftdi.hpp:193
Ftdi::List::begin
iterator begin()
Definition ftdi.cpp:552
Ftdi::List::iterator
ListType::iterator iterator
Iterator type for the container.
Definition ftdi.hpp:187
Ftdi::List::reverse_iterator
ListType::reverse_iterator reverse_iterator
Reverse iterator type for the container.
Definition ftdi.hpp:191
Ftdi::List::erase
iterator erase(iterator pos)
Definition ftdi.cpp:679
Ftdi::List::rbegin
reverse_iterator rbegin()
Definition ftdi.cpp:588
Ftdi::List::const_iterator
ListType::const_iterator const_iterator
Const iterator type for the container.
Definition ftdi.hpp:189
Ftdi::List::empty
bool empty() const
Definition ftdi.cpp:634
Ftdi::List::end
iterator end()
Definition ftdi.cpp:561
Ftdi::List::rend
reverse_iterator rend()
Definition ftdi.cpp:597
Ftdi::List::find_all
static List * find_all(Context &context, int vendor, int product)
Definition ftdi.cpp:695
Ftdi::List::push_front
void push_front(const Context &element)
Definition ftdi.cpp:669
Ftdi::List::size
ListType::size_type size() const
Definition ftdi.cpp:625
ftdi.h
ftdi_mpsse_mode
ftdi_mpsse_mode
Definition ftdi.h:68
ftdi_stopbits_type
ftdi_stopbits_type
Definition ftdi.h:60
ftdi_bits_type
ftdi_bits_type
Definition ftdi.h:62
ftdi_interface
ftdi_interface
Definition ftdi.h:83
DEPRECATED
#define DEPRECATED(func)
Definition ftdi.h:252
ftdi_parity_type
ftdi_parity_type
Definition ftdi.h:58
ftdi_break_type
ftdi_break_type
Definition ftdi.h:64
Ftdi
Definition ftdi.cpp:36
ftdi_context
Main context structure for all libftdi functions.
Definition ftdi.h:272
ftdi_device_list
list of usb devices created by ftdi_usb_find_all()
Definition ftdi.h:395