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