libftdi1  1.0
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-2013 by Marek Vavruša and libftdi developers
6  email : opensource@intra2net.com and marek@vavrusa.com
7  ***************************************************************************/
8 /*
9 Copyright (C) 2008-2013 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 
97  /* I/O */
98  int read(unsigned char *buf, int size);
99  int write(unsigned char *buf, int size);
100  int set_read_chunk_size(unsigned int chunksize);
101  int set_write_chunk_size(unsigned int chunksize);
102  int read_chunk_size();
103  int write_chunk_size();
104 
105  /* Async IO
106  TODO: should wrap?
107  int writeAsync(unsigned char *buf, int size);
108  void asyncComplete(int wait_for_more);
109  */
110 
111  /* Flow control */
112  int set_event_char(unsigned char eventch, unsigned char enable);
113  int set_error_char(unsigned char errorch, unsigned char enable);
114  int set_flow_control(int flowctrl);
115  int set_modem_control(int mask = Dtr|Rts);
116  int set_latency(unsigned char latency);
117  int set_dtr(bool state);
118  int set_rts(bool state);
119 
120  unsigned short poll_modem_status();
121  unsigned latency();
122 
123  /* BitBang mode */
124  int set_bitmode(unsigned char bitmask, unsigned char mode);
125  int set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode);
126  int bitbang_disable();
127  int read_pins(unsigned char *pins);
128 
129  /* Misc */
130  char* error_string();
131 
132 protected:
133  int get_strings();
135 
136  /* Properties */
137  struct ftdi_context* context();
138  void set_context(struct ftdi_context* context);
139  void set_usb_device(struct libusb_device *dev);
140 
141 private:
142  class Private;
143  boost::shared_ptr<Private> d;
144 };
145 
148 class Eeprom
149 {
150 public:
151  Eeprom(Context* parent);
152  ~Eeprom();
153 
154  int init_defaults(char *manufacturer, char* product, char * serial);
155  int chip_id(unsigned int *chipid);
156  int build(unsigned char *output);
157 
158  int read(unsigned char *eeprom);
159  int write(unsigned char *eeprom);
160  int read_location(int eeprom_addr, unsigned short *eeprom_val);
161  int write_location(int eeprom_addr, unsigned short eeprom_val);
162  int erase();
163 
164 private:
165  class Private;
166  boost::shared_ptr<Private> d;
167 };
168 
171 class List
172 {
173 public:
174  List(struct ftdi_device_list* devlist = 0);
175  ~List();
176 
177  static List* find_all(Context &context, int vendor, int product);
178 
180  typedef std::list<Context> ListType;
182  typedef ListType::iterator iterator;
184  typedef ListType::const_iterator const_iterator;
186  typedef ListType::reverse_iterator reverse_iterator;
188  typedef ListType::const_reverse_iterator const_reverse_iterator;
189 
190  iterator begin();
191  iterator end();
192  const_iterator begin() const;
193  const_iterator end() const;
194 
199 
200  ListType::size_type size() const;
201  bool empty() const;
202  void clear();
203 
204  void push_back(const Context& element);
205  void push_front(const Context& element);
206 
207  iterator erase(iterator pos);
209 
210 private:
211  class Private;
212  boost::shared_ptr<Private> d;
213 };
214 
215 }
216 
217 #endif