327242f809de17917a176e0a9cb4e4dd3920a18c
[libftdi] / ftdipp / ftdi.hpp
1 /***************************************************************************
2                           ftdi.hpp  -  C++ wrapper for libftdi
3                              -------------------
4     begin                : Mon Oct 13 2008
5     copyright            : (C) 2008-2026 by Marek Vavruša and libftdi developers
6     email                : opensource@intra2net.com and marek@vavrusa.com
7  ***************************************************************************/
8 /*
9 Copyright (C) 2008-2017 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 <memory>
35 #include <ftdi.h>
36
37 namespace Ftdi
38 {
39
40 /* Forward declarations*/
41 class List;
42 class Eeprom;
43
44 /*! \brief FTDI device context.
45  * Represents single FTDI device context.
46  */
47 class Context
48 {
49     /* Friends */
50     friend class Eeprom;
51     friend class List;
52
53 public:
54     /*! \brief Direction flags for flush().
55      */
56     enum Direction
57     {
58         Input = 0x2,
59         Output = 0x1,
60     };
61
62     /*! \brief Modem control flags.
63      */
64     enum ModemCtl
65     {
66         Dtr = 0x2,
67         Rts = 0x1,
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 DEPRECATED(flush)(int mask = Input|Output);
89     int tcflush(int mask = Input|Output);
90     int set_interface(enum ftdi_interface interface);
91     int set_module_detach_mode(enum ftdi_module_detach_mode mode);
92     void set_usb_device(struct libusb_device_handle *dev);
93
94     /* Line manipulators */
95     int set_baud_rate(int baudrate);
96     int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
97     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);
98     int get_usb_read_timeout() const;
99     void set_usb_read_timeout(int usb_read_timeout);
100     int get_usb_write_timeout() const;
101     void set_usb_write_timeout(int usb_write_timeout);
102
103     /* I/O */
104     int read(unsigned char *buf, int size);
105     int write(const unsigned char *buf, int size);
106     int set_read_chunk_size(unsigned int chunksize);
107     int set_write_chunk_size(unsigned int chunksize);
108     int read_chunk_size();
109     int write_chunk_size();
110
111     /* Async IO
112     TODO: should wrap?
113     int writeAsync(const unsigned char *buf, int size);
114     void asyncComplete(int wait_for_more);
115     */
116
117     /* Flow control */
118     int set_event_char(unsigned char eventch, unsigned char enable);
119     int set_error_char(unsigned char errorch, unsigned char enable);
120     int set_flow_control(int flowctrl);
121     int set_modem_control(int mask = Dtr|Rts);
122     int set_latency(unsigned char latency);
123     int set_dtr(bool state);
124     int set_rts(bool state);
125
126     unsigned short poll_modem_status();
127     unsigned latency();
128
129     /* BitBang mode */
130     int set_bitmode(unsigned char bitmask, unsigned char mode);
131     int set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode);
132     int bitbang_disable();
133     int read_pins(unsigned char *pins);
134
135     /* Misc */
136     const char* error_string();
137
138 protected:
139     int get_strings(bool vendor=true, bool description=true, bool serial=true);
140     int get_strings_and_reopen(bool vendor=true, bool description=true, bool serial=true);
141
142     /* Properties */
143     struct ftdi_context* context();
144     void set_context(struct ftdi_context* context);
145     void set_usb_device(struct libusb_device *dev);
146
147 private:
148     class Private;
149     std::shared_ptr<Private> d;
150 };
151
152 /*! \brief Device EEPROM.
153  */
154 class Eeprom
155 {
156 public:
157     Eeprom(Context* parent);
158     ~Eeprom();
159
160     int init_defaults(char *manufacturer, char* product, char * serial);
161     int chip_id(unsigned int *chipid);
162     int build(unsigned char *output);
163
164     int read(unsigned char *eeprom);
165     int write(unsigned char *eeprom);
166     int read_location(int eeprom_addr, unsigned short *eeprom_val);
167     int write_location(int eeprom_addr, unsigned short eeprom_val);
168     int erase();
169
170 private:
171     class Private;
172     std::shared_ptr<Private> d;
173 };
174
175 /*! \brief Device list.
176  */
177 class List
178 {
179 public:
180     List(struct ftdi_device_list* devlist = 0);
181     ~List();
182
183     static List* find_all(Context &context, int vendor, int product);
184
185     /// List type storing "Context" objects
186     typedef std::list<Context> ListType;
187     /// Iterator type for the container
188     typedef ListType::iterator iterator;
189     /// Const iterator type for the container
190     typedef ListType::const_iterator const_iterator;
191     /// Reverse iterator type for the container
192     typedef ListType::reverse_iterator reverse_iterator;
193     /// Const reverse iterator type for the container
194     typedef ListType::const_reverse_iterator const_reverse_iterator;
195
196     iterator begin();
197     iterator end();
198     const_iterator begin() const;
199     const_iterator end() const;
200
201     reverse_iterator rbegin();
202     reverse_iterator rend();
203     const_reverse_iterator rbegin() const;
204     const_reverse_iterator rend() const;
205
206     ListType::size_type size() const;
207     bool empty() const;
208     void clear();
209
210     void push_back(const Context& element);
211     void push_front(const Context& element);
212
213     iterator erase(iterator pos);
214     iterator erase(iterator beg, iterator end);
215
216 private:
217     class Private;
218     std::shared_ptr<Private> d;
219 };
220
221 }
222
223 #endif