libftdi: (tomj) initial submission of C++ wrapper
[libftdi] / ftdipp / ftdi.hpp
CommitLineData
20b1459a
TJ
1#ifndef FTDICPP_H
2#define FTDICPP_H
3
4#include <list>
5#include <string>
6#include "ftdi.h"
7
8namespace Ftdi
9{
10
11/* Forward declarations*/
12class List;
13class Eeprom;
14
15/*! \brief FTDI device context.
16 * Represents single FTDI device context.
17 */
18class Context
19{
20 /* Friends */
21 friend class Eeprom;
22 friend class List;
23
24 public:
25
26 /*! \brief Direction flags for flush().
27 */
28 enum Direction
29 {
30 Input,
31 Output
32 };
33
34 /*! \brief Modem control flags.
35 */
36 enum ModemCtl
37 {
38 Dtr,
39 Rts
40 };
41
42 /* Constructor, Destructor */
43 Context();
44 ~Context();
45
46 /* Properties */
47 Eeprom* eeprom();
48 const std::string& vendor();
49 const std::string& description();
50 const std::string& serial();
51
52 /* Device manipulators */
53 bool is_open();
54 int open(struct usb_device *dev = 0);
55 int open(int vendor, int product, const std::string& description = std::string(), const std::string& serial = std::string());
56 int close();
57 int reset();
58 int flush(int mask = Input|Output);
59 int set_interface(enum ftdi_interface interface);
60 void set_usb_device(struct usb_dev_handle *dev);
61
62 /* Line manipulators */
63 int set_baud_rate(int baudrate);
64 int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
65 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);
66
67 /* I/O */
68 int read(unsigned char *buf, int size);
69 int write(unsigned char *buf, int size);
70 int set_read_chunk_size(unsigned int chunksize);
71 int set_write_chunk_size(unsigned int chunksize);
72 int read_chunk_size();
73 int write_chunk_size();
74
75 /* Async IO
76 TODO: should wrap?
77 int writeAsync(unsigned char *buf, int size);
78 void asyncComplete(int wait_for_more);
79 */
80
81 /* Flow control */
82 int set_event_char(unsigned char eventch, unsigned char enable);
83 int set_error_char(unsigned char errorch, unsigned char enable);
84 int set_flow_control(int flowctrl);
85 int set_modem_control(int mask = Dtr|Rts);
86 int set_latency(unsigned char latency);
87 int set_dtr(bool state);
88 int set_rts(bool state);
89
90 unsigned short poll_modem_status();
91 unsigned latency();
92
93 /* BitBang mode */
94 int set_bitmode(unsigned char bitmask, unsigned char mode);
95 int bitbang_enable(unsigned char bitmask);
96 int bitbang_disable();
97 int read_pins(unsigned char *pins);
98
99 /* Misc */
100 char* error_string();
101
102 protected:
103 int get_strings();
104
105 /* Properties */
106 struct ftdi_context* context();
107 void set_context(struct ftdi_context* context);
108 void set_usb_device(struct usb_device *dev);
109
110 private:
111 class Private;
112 Private *d;
113
114 /* Disable copy constructor */
115 Context(const Context &) {}
116 Context& operator=(const Context &) {}
117};
118
119/*! \brief Device EEPROM.
120 */
121class Eeprom
122{
123 public:
124 Eeprom(Context* parent);
125 ~Eeprom();
126
127 void init_defaults();
128 void set_size(int size);
129 int size(unsigned char *eeprom, int maxsize);
130 int chip_id(unsigned int *chipid);
131 int build(unsigned char *output);
132 int read(unsigned char *eeprom);
133 int write(unsigned char *eeprom);
134 int erase();
135
136 private:
137 class Private;
138 Private *d;
139};
140
141typedef std::list<Context*> ListBase;
142
143/*! \brief Device list.
144 */
145class List : public ListBase
146{
147 public:
148 List(struct ftdi_device_list* devlist = 0);
149 ~List();
150
151 static List* find_all(int vendor, int product);
152
153 private:
154 class Private;
155 Private *d;
156};
157
158}
159
160#endif