libftdi 0.20
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 by Marek Vavruša
6 email : opensource@intra2net.com and marek@vavrusa.com
7 ***************************************************************************/
8/*
9Copyright (C) 2008 by Marek Vavruša
10
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.
25
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*/
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
37namespace Ftdi
38{
39
40/* Forward declarations*/
41class List;
42class Eeprom;
43
48{
49 /* Friends */
50 friend class Eeprom;
51 friend class List;
52
53public:
57 {
59 Output
60 };
61
65 {
67 Rts
68 };
69
70 /* Constructor, Destructor */
71 Context();
72 ~Context();
73
74 /* Properties */
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 usb_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 usb_dev_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 DEPRECATED(bitbang_enable(unsigned char bitmask));
127 int bitbang_disable();
128 int read_pins(unsigned char *pins);
129
130 /* Misc */
131 char* error_string();
132
133protected:
134 int get_strings();
136
137 /* Properties */
138 struct ftdi_context* context();
139 void set_context(struct ftdi_context* context);
140 void set_usb_device(struct usb_device *dev);
141
142private:
143 class Private;
144 boost::shared_ptr<Private> d;
145};
146
150{
151public:
152 Eeprom(Context* parent);
153 ~Eeprom();
154
155 void init_defaults();
156 void set_size(int size);
157 int size(unsigned char *eeprom, int maxsize);
158 int chip_id(unsigned int *chipid);
159 int build(unsigned char *output);
160
161 int read(unsigned char *eeprom);
162 int write(unsigned char *eeprom);
163 int read_location(int eeprom_addr, unsigned short *eeprom_val);
164 int write_location(int eeprom_addr, unsigned short eeprom_val);
165 int erase();
166
167private:
168 class Private;
169 boost::shared_ptr<Private> d;
170};
171
174class List
175{
176public:
177 List(struct ftdi_device_list* devlist = 0);
178 ~List();
179
180 static List* find_all(int vendor, int product);
181
183 typedef std::list<Context> ListType;
185 typedef ListType::iterator iterator;
187 typedef ListType::const_iterator const_iterator;
189 typedef ListType::reverse_iterator reverse_iterator;
191 typedef ListType::const_reverse_iterator const_reverse_iterator;
192
193 iterator begin();
194 iterator end();
195 const_iterator begin() const;
196 const_iterator end() const;
197
202
203 ListType::size_type size() const;
204 bool empty() const;
205 void clear();
206
207 void push_back(const Context& element);
208 void push_front(const Context& element);
209
212
213private:
214 class Private;
215 boost::shared_ptr<Private> d;
216};
217
218}
219
220#endif
FTDI device context. Represents single FTDI device context.
Definition ftdi.hpp:48
Context()
Constructor.
Definition ftdi.cpp:64
int set_flow_control(int flowctrl)
Definition ftdi.cpp:218
int set_baud_rate(int baudrate)
Definition ftdi.cpp:165
const std::string & description()
Device strings properties.
Definition ftdi.cpp:347
int read(unsigned char *buf, int size)
Definition ftdi.cpp:180
int open(struct usb_device *dev=0)
Definition ftdi.cpp:120
int flush(int mask=Input|Output)
Definition ftdi.cpp:142
bool is_open()
Definition ftdi.cpp:75
int reset()
Definition ftdi.cpp:137
const std::string & serial()
Device strings properties.
Definition ftdi.cpp:354
void set_usb_device(struct usb_dev_handle *dev)
Definition ftdi.cpp:159
int read_chunk_size()
Definition ftdi.cpp:190
int set_write_chunk_size(unsigned int chunksize)
Definition ftdi.cpp:204
int set_error_char(unsigned char errorch, unsigned char enable)
Definition ftdi.cpp:269
int write(unsigned char *buf, int size)
Definition ftdi.cpp:199
int set_dtr(bool state)
Definition ftdi.cpp:235
Direction
Direction flags for flush().
Definition ftdi.hpp:57
int set_latency(unsigned char latency)
Definition ftdi.cpp:245
int set_read_chunk_size(unsigned int chunksize)
Definition ftdi.cpp:185
int get_strings_and_reopen()
Definition ftdi.cpp:321
int set_event_char(unsigned char eventch, unsigned char enable)
Definition ftdi.cpp:264
unsigned latency()
Definition ftdi.cpp:250
unsigned short poll_modem_status()
Definition ftdi.cpp:257
int set_interface(enum ftdi_interface interface)
Definition ftdi.cpp:154
int read_pins(unsigned char *pins)
Definition ftdi.cpp:294
void set_context(struct ftdi_context *context)
Definition ftdi.cpp:359
int get_strings()
Definition ftdi.cpp:304
int close()
Definition ftdi.cpp:131
const std::string & vendor()
Device strings properties.
Definition ftdi.cpp:340
char * error_string()
Definition ftdi.cpp:299
ModemCtl
Modem control flags.
Definition ftdi.hpp:65
int DEPRECATED(bitbang_enable(unsigned char bitmask))
int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
Definition ftdi.cpp:170
int bitbang_disable()
Definition ftdi.cpp:279
~Context()
Destructor.
Definition ftdi.cpp:71
int set_modem_control(int mask=Dtr|Rts)
Definition ftdi.cpp:223
int write_chunk_size()
Definition ftdi.cpp:209
int set_bitmode(unsigned char bitmask, unsigned char mode)
Definition ftdi.cpp:284
Eeprom * eeprom()
int set_rts(bool state)
Definition ftdi.cpp:240
struct ftdi_context * context()
Definition ftdi.cpp:370
Device EEPROM.
Definition ftdi.hpp:150
int erase()
Definition ftdi.cpp:441
void set_size(int size)
Definition ftdi.cpp:401
int read(unsigned char *eeprom)
Definition ftdi.cpp:421
int write_location(int eeprom_addr, unsigned short eeprom_val)
Definition ftdi.cpp:436
int read_location(int eeprom_addr, unsigned short *eeprom_val)
Definition ftdi.cpp:431
void init_defaults()
Definition ftdi.cpp:396
int build(unsigned char *output)
Definition ftdi.cpp:416
int write(unsigned char *eeprom)
Definition ftdi.cpp:426
int size(unsigned char *eeprom, int maxsize)
Definition ftdi.cpp:406
int chip_id(unsigned int *chipid)
Definition ftdi.cpp:411
Device list.
Definition ftdi.hpp:175
std::list< Context > ListType
List type storing "Context" objects.
Definition ftdi.hpp:183
void push_back(const Context &element)
Definition ftdi.cpp:595
void clear()
Definition ftdi.cpp:579
ListType::const_reverse_iterator const_reverse_iterator
Const reverse iterator type for the container.
Definition ftdi.hpp:191
iterator begin()
Definition ftdi.cpp:487
ListType::iterator iterator
Iterator type for the container.
Definition ftdi.hpp:185
ListType::reverse_iterator reverse_iterator
Reverse iterator type for the container.
Definition ftdi.hpp:189
iterator erase(iterator pos)
Definition ftdi.cpp:614
reverse_iterator rbegin()
Definition ftdi.cpp:523
ListType::const_iterator const_iterator
Const iterator type for the container.
Definition ftdi.hpp:187
bool empty() const
Definition ftdi.cpp:569
iterator end()
Definition ftdi.cpp:496
reverse_iterator rend()
Definition ftdi.cpp:532
static List * find_all(int vendor, int product)
Definition ftdi.cpp:630
void push_front(const Context &element)
Definition ftdi.cpp:604
ListType::size_type size() const
Definition ftdi.cpp:560
ftdi_mpsse_mode
Definition ftdi.h:37
ftdi_stopbits_type
Definition ftdi.h:29
ftdi_bits_type
Definition ftdi.h:31
ftdi_interface
Definition ftdi.h:51
ftdi_parity_type
Definition ftdi.h:27
ftdi_break_type
Definition ftdi.h:33
Definition ftdi.cpp:33
Main context structure for all libftdi functions.
Definition ftdi.h:189
list of usb devices created by ftdi_usb_find_all()
Definition ftdi.h:250