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