libftdi1  1.3
ftdi.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  ftdi.hpp - C++ wrapper for libftdi
3  -------------------
4  begin : Mon Oct 13 2008
5  copyright : (C) 2008-2014 by Marek Vavruša and libftdi developers
6  email : opensource@intra2net.com and marek@vavrusa.com
7  ***************************************************************************/
8 /*
9 Copyright (C) 2008-2014 by Marek Vavruša and libftdi developers
10 
11 The software in this package is distributed under the GNU General
12 Public License version 2 (with a special exception described below).
13 
14 A copy of GNU General Public License (GPL) is included in this distribution,
15 in the file COPYING.GPL.
16 
17 As a special exception, if other files instantiate templates or use macros
18 or inline functions from this file, or you compile this file and link it
19 with other works to produce a work based on this file, this file
20 does not by itself cause the resulting work to be covered
21 by the GNU General Public License.
22 
23 However the source code for this file must still be made available
24 in accordance with section (3) of the GNU General Public License.
25 
26 This exception does not invalidate any other reasons why a work based
27 on this file might be covered by the GNU General Public License.
28 */
29 #ifndef __libftdi_hpp__
30 #define __libftdi_hpp__
31 
32 #include <list>
33 #include <string>
34 #include <boost/shared_ptr.hpp>
35 #include <ftdi.h>
36 
37 namespace Ftdi
38 {
39 
40 /* Forward declarations*/
41 class List;
42 class Eeprom;
43 
47 class Context
48 {
49  /* Friends */
50  friend class Eeprom;
51  friend class List;
52 
53 public:
56  enum Direction
57  {
60  };
61 
64  enum ModemCtl
65  {
66  Dtr,
68  };
69 
70  /* Constructor, Destructor */
71  Context();
72  ~Context();
73 
74  /* Properties */
75  Eeprom* eeprom();
76  const std::string& vendor();
77  const std::string& description();
78  const std::string& serial();
79 
80  /* Device manipulators */
81  bool is_open();
82  int open(struct libusb_device *dev = 0);
83  int open(int vendor, int product);
84  int open(int vendor, int product, const std::string& description, const std::string& serial = std::string(), unsigned int index=0);
85  int open(const std::string& description);
86  int close();
87  int reset();
88  int flush(int mask = Input|Output);
89  int set_interface(enum ftdi_interface interface);
90  void set_usb_device(struct libusb_device_handle *dev);
91 
92  /* Line manipulators */
93  int set_baud_rate(int baudrate);
94  int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
95  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);
96  int get_usb_read_timeout() const;
97  void set_usb_read_timeout(int usb_read_timeout);
98  int get_usb_write_timeout() const;
99  void set_usb_write_timeout(int usb_write_timeout);
100 
101  /* I/O */
102  int read(unsigned char *buf, int size);
103  int write(unsigned char *buf, int size);
104  int set_read_chunk_size(unsigned int chunksize);
105  int set_write_chunk_size(unsigned int chunksize);
106  int read_chunk_size();
107  int write_chunk_size();
108 
109  /* Async IO
110  TODO: should wrap?
111  int writeAsync(unsigned char *buf, int size);
112  void asyncComplete(int wait_for_more);
113  */
114 
115  /* Flow control */
116  int set_event_char(unsigned char eventch, unsigned char enable);
117  int set_error_char(unsigned char errorch, unsigned char enable);
118  int set_flow_control(int flowctrl);
119  int set_modem_control(int mask = Dtr|Rts);
120  int set_latency(unsigned char latency);
121  int set_dtr(bool state);
122  int set_rts(bool state);
123 
124  unsigned short poll_modem_status();
125  unsigned latency();
126 
127  /* BitBang mode */
128  int set_bitmode(unsigned char bitmask, unsigned char mode);
129  int set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode);
130  int bitbang_disable();
131  int read_pins(unsigned char *pins);
132 
133  /* Misc */
134  char* error_string();
135 
136 protected:
137  int get_strings();
139 
140  /* Properties */
141  struct ftdi_context* context();
142  void set_context(struct ftdi_context* context);
143  void set_usb_device(struct libusb_device *dev);
144 
145 private:
146  class Private;
147  boost::shared_ptr<Private> d;
148 };
149 
152 class Eeprom
153 {
154 public:
155  Eeprom(Context* parent);
156  ~Eeprom();
157 
158  int init_defaults(char *manufacturer, char* product, char * serial);
159  int chip_id(unsigned int *chipid);
160  int build(unsigned char *output);
161 
162  int read(unsigned char *eeprom);
163  int write(unsigned char *eeprom);
164  int read_location(int eeprom_addr, unsigned short *eeprom_val);
165  int write_location(int eeprom_addr, unsigned short eeprom_val);
166  int erase();
167 
168 private:
169  class Private;
170  boost::shared_ptr<Private> d;
171 };
172 
175 class List
176 {
177 public:
178  List(struct ftdi_device_list* devlist = 0);
179  ~List();
180 
181  static List* find_all(Context &context, int vendor, int product);
182 
184  typedef std::list<Context> ListType;
186  typedef ListType::iterator iterator;
188  typedef ListType::const_iterator const_iterator;
190  typedef ListType::reverse_iterator reverse_iterator;
192  typedef ListType::const_reverse_iterator const_reverse_iterator;
193 
194  iterator begin();
195  iterator end();
196  const_iterator begin() const;
197  const_iterator end() const;
198 
199  reverse_iterator rbegin();
200  reverse_iterator rend();
201  const_reverse_iterator rbegin() const;
202  const_reverse_iterator rend() const;
203 
204  ListType::size_type size() const;
205  bool empty() const;
206  void clear();
207 
208  void push_back(const Context& element);
209  void push_front(const Context& element);
210 
211  iterator erase(iterator pos);
212  iterator erase(iterator beg, iterator end);
213 
214 private:
215  class Private;
216  boost::shared_ptr<Private> d;
217 };
218 
219 }
220 
221 #endif
bool empty() const
Definition: ftdi.cpp:582
int set_modem_control(int mask=Dtr|Rts)
Definition: ftdi.cpp:246
list of usb devices created by ftdi_usb_find_all()
Definition: ftdi.h:342
iterator end()
Definition: ftdi.cpp:509
char * error_string()
Definition: ftdi.cpp:317
int write(unsigned char *buf, int size)
Definition: ftdi.cpp:222
FTDI device context. Represents single FTDI device context.
Definition: ftdi.hpp:47
int read_chunk_size()
Definition: ftdi.cpp:213
const std::string & description()
Device strings properties.
Definition: ftdi.cpp:370
reverse_iterator rbegin()
Definition: ftdi.cpp:536
ftdi_interface
Definition: ftdi.h:72
void push_front(const Context &element)
Definition: ftdi.cpp:617
int read(unsigned char *buf, int size)
Definition: ftdi.cpp:203
Direction
Direction flags for flush().
Definition: ftdi.hpp:56
ListType::iterator iterator
Iterator type for the container.
Definition: ftdi.hpp:186
int reset()
Definition: ftdi.cpp:140
const std::string & vendor()
Device strings properties.
Definition: ftdi.cpp:363
Main context structure for all libftdi functions.
Definition: ftdi.h:219
int read_location(int eeprom_addr, unsigned short *eeprom_val)
Definition: ftdi.cpp:444
std::list< Context > ListType
List type storing "Context" objects.
Definition: ftdi.hpp:184
int get_usb_write_timeout() const
Definition: ftdi.cpp:193
~Context()
Destructor.
Definition: ftdi.cpp:73
int bitbang_disable()
Definition: ftdi.cpp:307
Eeprom * eeprom()
int flush(int mask=Input|Output)
Definition: ftdi.cpp:145
iterator begin()
Definition: ftdi.cpp:500
int write_chunk_size()
Definition: ftdi.cpp:232
int open(struct libusb_device *dev=0)
Definition: ftdi.cpp:122
int set_latency(unsigned char latency)
Definition: ftdi.cpp:268
const std::string & serial()
Device strings properties.
Definition: ftdi.cpp:377
ModemCtl
Modem control flags.
Definition: ftdi.hpp:64
int erase()
Definition: ftdi.cpp:454
ftdi_bits_type
Definition: ftdi.h:52
int set_interface(enum ftdi_interface interface)
Definition: ftdi.cpp:157
int set_read_chunk_size(unsigned int chunksize)
Definition: ftdi.cpp:208
int get_usb_read_timeout() const
Definition: ftdi.cpp:183
Device EEPROM.
Definition: ftdi.hpp:152
int set_rts(bool state)
Definition: ftdi.cpp:263
unsigned latency()
Definition: ftdi.cpp:273
int close()
Definition: ftdi.cpp:133
unsigned short poll_modem_status()
Definition: ftdi.cpp:280
void set_usb_write_timeout(int usb_write_timeout)
Definition: ftdi.cpp:198
int set_write_chunk_size(unsigned int chunksize)
Definition: ftdi.cpp:227
Definition: ftdi.cpp:34
ListType::reverse_iterator reverse_iterator
Reverse iterator type for the container.
Definition: ftdi.hpp:190
void set_usb_read_timeout(int usb_read_timeout)
Definition: ftdi.cpp:188
reverse_iterator rend()
Definition: ftdi.cpp:545
void push_back(const Context &element)
Definition: ftdi.cpp:608
void set_usb_device(struct libusb_device_handle *dev)
Definition: ftdi.cpp:162
List(struct ftdi_device_list *devlist=0)
Definition: ftdi.cpp:476
int set_flow_control(int flowctrl)
Definition: ftdi.cpp:241
iterator erase(iterator pos)
Definition: ftdi.cpp:627
int build(unsigned char *output)
Definition: ftdi.cpp:429
ftdi_break_type
Definition: ftdi.h:54
int chip_id(unsigned int *chipid)
Definition: ftdi.cpp:424
int set_bitmode(unsigned char bitmask, unsigned char mode)
Definition: ftdi.cpp:297
int set_error_char(unsigned char errorch, unsigned char enable)
Definition: ftdi.cpp:292
ftdi_parity_type
Definition: ftdi.h:48
int get_strings()
Definition: ftdi.cpp:322
Context()
Constructor.
Definition: ftdi.cpp:66
void clear()
Definition: ftdi.cpp:592
int init_defaults(char *manufacturer, char *product, char *serial)
Definition: ftdi.cpp:419
ftdi_stopbits_type
Definition: ftdi.h:50
int set_dtr(bool state)
Definition: ftdi.cpp:258
Device list.
Definition: ftdi.hpp:175
int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
Definition: ftdi.cpp:173
int get_strings_and_reopen()
Definition: ftdi.cpp:339
ListType::const_iterator const_iterator
Const iterator type for the container.
Definition: ftdi.hpp:188
struct ftdi_context * context()
Definition: ftdi.cpp:393
Eeprom(Context *parent)
Definition: ftdi.cpp:409
int read(unsigned char *eeprom)
Definition: ftdi.cpp:434
int set_baud_rate(int baudrate)
Definition: ftdi.cpp:168
ListType::size_type size() const
Definition: ftdi.cpp:573
int write(unsigned char *eeprom)
Definition: ftdi.cpp:439
ftdi_mpsse_mode
Definition: ftdi.h:57
ListType::const_reverse_iterator const_reverse_iterator
Const reverse iterator type for the container.
Definition: ftdi.hpp:192
static List * find_all(Context &context, int vendor, int product)
Definition: ftdi.cpp:643
int set_event_char(unsigned char eventch, unsigned char enable)
Definition: ftdi.cpp:287
bool is_open()
Definition: ftdi.cpp:77
int read_pins(unsigned char *pins)
Definition: ftdi.cpp:312
void set_context(struct ftdi_context *context)
Definition: ftdi.cpp:382
int write_location(int eeprom_addr, unsigned short eeprom_val)
Definition: ftdi.cpp:449