Update Copyright to 2017, switch download URLs to HTTPS
[libftdi] / ftdipp / ftdi.hpp
CommitLineData
cdf448f6
TJ
1/***************************************************************************
2 ftdi.hpp - C++ wrapper for libftdi
3 -------------------
4 begin : Mon Oct 13 2008
79646368 5 copyright : (C) 2008-2017 by Marek Vavruša and libftdi developers
cdf448f6
TJ
6 email : opensource@intra2net.com and marek@vavrusa.com
7 ***************************************************************************/
3ab8f642 8/*
79646368 9Copyright (C) 2008-2017 by Marek Vavruša and libftdi developers
cdf448f6 10
3ab8f642
TJ
11The software in this package is distributed under the GNU General
12Public License version 2 (with a special exception described below).
13
14A copy of GNU General Public License (GPL) is included in this distribution,
15in the file COPYING.GPL.
16
17As a special exception, if other files instantiate templates or use macros
18or inline functions from this file, or you compile this file and link it
19with other works to produce a work based on this file, this file
20does not by itself cause the resulting work to be covered
21by the GNU General Public License.
22
23However the source code for this file must still be made available
24in accordance with section (3) of the GNU General Public License.
cdf448f6 25
3ab8f642
TJ
26This exception does not invalidate any other reasons why a work based
27on 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
37namespace Ftdi
38{
39
40/* Forward declarations*/
41class List;
42class Eeprom;
43
44/*! \brief FTDI device context.
45 * Represents single FTDI device context.
46 */
47class Context
48{
cdf448f6
TJ
49 /* Friends */
50 friend class Eeprom;
51 friend class List;
52
53public:
54 /*! \brief Direction flags for flush().
55 */
56 enum Direction
57 {
58 Input,
59 Output
60 };
61
62 /*! \brief Modem control flags.
63 */
64 enum ModemCtl
65 {
66 Dtr,
67 Rts
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();
88 int flush(int mask = Input|Output);
89 int set_interface(enum ftdi_interface interface);
579b006f 90 void set_usb_device(struct libusb_device_handle *dev);
cdf448f6
TJ
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);
73a71502
JS
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);
cdf448f6
TJ
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);
58cce2d4 129 int set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode);
cdf448f6
TJ
130 int bitbang_disable();
131 int read_pins(unsigned char *pins);
132
133 /* Misc */
c45d2630 134 const char* error_string();
cdf448f6
TJ
135
136protected:
137 int get_strings();
58cce2d4 138 int get_strings_and_reopen();
cdf448f6
TJ
139
140 /* Properties */
141 struct ftdi_context* context();
142 void set_context(struct ftdi_context* context);
579b006f 143 void set_usb_device(struct libusb_device *dev);
cdf448f6
TJ
144
145private:
146 class Private;
cfceadbc 147 boost::shared_ptr<Private> d;
20b1459a
TJ
148};
149
150/*! \brief Device EEPROM.
151 */
152class Eeprom
153{
cdf448f6
TJ
154public:
155 Eeprom(Context* parent);
156 ~Eeprom();
157
f14f84d3 158 int init_defaults(char *manufacturer, char* product, char * serial);
cdf448f6
TJ
159 int chip_id(unsigned int *chipid);
160 int build(unsigned char *output);
449c87a9 161
cdf448f6
TJ
162 int read(unsigned char *eeprom);
163 int write(unsigned char *eeprom);
449c87a9
TJ
164 int read_location(int eeprom_addr, unsigned short *eeprom_val);
165 int write_location(int eeprom_addr, unsigned short eeprom_val);
cdf448f6
TJ
166 int erase();
167
168private:
169 class Private;
cfceadbc 170 boost::shared_ptr<Private> d;
20b1459a
TJ
171};
172
20b1459a
TJ
173/*! \brief Device list.
174 */
6b22a054 175class List
20b1459a 176{
cdf448f6
TJ
177public:
178 List(struct ftdi_device_list* devlist = 0);
179 ~List();
20b1459a 180
7c21beca 181 static List* find_all(Context &context, int vendor, int product);
20b1459a 182
a14193ac
TJ
183 /// List type storing "Context" objects
184 typedef std::list<Context> ListType;
185 /// Iterator type for the container
186 typedef ListType::iterator iterator;
187 /// Const iterator type for the container
188 typedef ListType::const_iterator const_iterator;
189 /// Reverse iterator type for the container
190 typedef ListType::reverse_iterator reverse_iterator;
191 /// Const reverse iterator type for the container
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;
6b22a054
MV
206 void clear();
207
a14193ac
TJ
208 void push_back(const Context& element);
209 void push_front(const Context& element);
6b22a054 210
a14193ac
TJ
211 iterator erase(iterator pos);
212 iterator erase(iterator beg, iterator end);
6b22a054 213
cdf448f6
TJ
214private:
215 class Private;
cfceadbc 216 boost::shared_ptr<Private> d;
20b1459a
TJ
217};
218
219}
220
221#endif