libftdi  0.19
ftdi.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           ftdi.hpp  -  C++ wrapper for libftdi
00003                              -------------------
00004     begin                : Mon Oct 13 2008
00005     copyright            : (C) 2008 by Marek Vavruša
00006     email                : opensource@intra2net.com and marek@vavrusa.com
00007  ***************************************************************************/
00008 /*
00009 Copyright (C) 2008 by Marek Vavruša
00010 
00011 The software in this package is distributed under the GNU General
00012 Public License version 2 (with a special exception described below).
00013 
00014 A copy of GNU General Public License (GPL) is included in this distribution,
00015 in the file COPYING.GPL.
00016 
00017 As a special exception, if other files instantiate templates or use macros
00018 or inline functions from this file, or you compile this file and link it
00019 with other works to produce a work based on this file, this file
00020 does not by itself cause the resulting work to be covered
00021 by the GNU General Public License.
00022 
00023 However the source code for this file must still be made available
00024 in accordance with section (3) of the GNU General Public License.
00025 
00026 This exception does not invalidate any other reasons why a work based
00027 on this file might be covered by the GNU General Public License.
00028 */
00029 #ifndef __libftdi_hpp__
00030 #define __libftdi_hpp__
00031 
00032 #include <list>
00033 #include <string>
00034 #include <boost/shared_ptr.hpp>
00035 #include <ftdi.h>
00036 
00037 namespace Ftdi
00038 {
00039 
00040 /* Forward declarations*/
00041 class List;
00042 class Eeprom;
00043 
00047 class Context
00048 {
00049     /* Friends */
00050     friend class Eeprom;
00051     friend class List;
00052 
00053 public:
00056     enum Direction
00057     {
00058         Input,
00059         Output
00060     };
00061 
00064     enum ModemCtl
00065     {
00066         Dtr,
00067         Rts
00068     };
00069 
00070     /* Constructor, Destructor */
00071     Context();
00072     ~Context();
00073 
00074     /* Properties */
00075     Eeprom* eeprom();
00076     const std::string& vendor();
00077     const std::string& description();
00078     const std::string& serial();
00079 
00080     /* Device manipulators */
00081     bool is_open();
00082     int open(struct usb_device *dev = 0);
00083     int open(int vendor, int product);
00084     int open(int vendor, int product, const std::string& description, const std::string& serial = std::string(), unsigned int index=0);
00085     int open(const std::string& description);
00086     int close();
00087     int reset();
00088     int flush(int mask = Input|Output);
00089     int set_interface(enum ftdi_interface interface);
00090     void set_usb_device(struct usb_dev_handle *dev);
00091 
00092     /* Line manipulators */
00093     int set_baud_rate(int baudrate);
00094     int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00095     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);
00096 
00097     /* I/O */
00098     int read(unsigned char *buf, int size);
00099     int write(unsigned char *buf, int size);
00100     int set_read_chunk_size(unsigned int chunksize);
00101     int set_write_chunk_size(unsigned int chunksize);
00102     int read_chunk_size();
00103     int write_chunk_size();
00104 
00105     /* Async IO
00106     TODO: should wrap?
00107     int writeAsync(unsigned char *buf, int size);
00108     void asyncComplete(int wait_for_more);
00109     */
00110 
00111     /* Flow control */
00112     int set_event_char(unsigned char eventch, unsigned char enable);
00113     int set_error_char(unsigned char errorch, unsigned char enable);
00114     int set_flow_control(int flowctrl);
00115     int set_modem_control(int mask = Dtr|Rts);
00116     int set_latency(unsigned char latency);
00117     int set_dtr(bool state);
00118     int set_rts(bool state);
00119 
00120     unsigned short poll_modem_status();
00121     unsigned latency();
00122 
00123     /* BitBang mode */
00124     int set_bitmode(unsigned char bitmask, unsigned char mode);
00125     int set_bitmode(unsigned char bitmask, enum ftdi_mpsse_mode mode);
00126     int DEPRECATED(bitbang_enable(unsigned char bitmask));
00127     int bitbang_disable();
00128     int read_pins(unsigned char *pins);
00129 
00130     /* Misc */
00131     char* error_string();
00132 
00133 protected:
00134     int get_strings();
00135     int get_strings_and_reopen();
00136 
00137     /* Properties */
00138     struct ftdi_context* context();
00139     void set_context(struct ftdi_context* context);
00140     void set_usb_device(struct usb_device *dev);
00141 
00142 private:
00143     class Private;
00144     boost::shared_ptr<Private> d;
00145 };
00146 
00149 class Eeprom
00150 {
00151 public:
00152     Eeprom(Context* parent);
00153     ~Eeprom();
00154 
00155     void init_defaults();
00156     void set_size(int size);
00157     int size(unsigned char *eeprom, int maxsize);
00158     int chip_id(unsigned int *chipid);
00159     int build(unsigned char *output);
00160 
00161     int read(unsigned char *eeprom);
00162     int write(unsigned char *eeprom);
00163     int read_location(int eeprom_addr, unsigned short *eeprom_val);
00164     int write_location(int eeprom_addr, unsigned short eeprom_val);
00165     int erase();
00166 
00167 private:
00168     class Private;
00169     boost::shared_ptr<Private> d;
00170 };
00171 
00174 class List
00175 {
00176 public:
00177     List(struct ftdi_device_list* devlist = 0);
00178     ~List();
00179 
00180     static List* find_all(int vendor, int product);
00181 
00183     typedef std::list<Context> ListType;
00185     typedef ListType::iterator iterator;
00187     typedef ListType::const_iterator const_iterator;
00189     typedef ListType::reverse_iterator reverse_iterator;
00191     typedef ListType::const_reverse_iterator const_reverse_iterator;
00192 
00193     iterator begin();
00194     iterator end();
00195     const_iterator begin() const;
00196     const_iterator end() const;
00197 
00198     reverse_iterator rbegin();
00199     reverse_iterator rend();
00200     const_reverse_iterator rbegin() const;
00201     const_reverse_iterator rend() const;
00202 
00203     ListType::size_type size() const;
00204     bool empty() const;
00205     void clear();
00206 
00207     void push_back(const Context& element);
00208     void push_front(const Context& element);
00209 
00210     iterator erase(iterator pos);
00211     iterator erase(iterator beg, iterator end);
00212 
00213 private:
00214     class Private;
00215     boost::shared_ptr<Private> d;
00216 };
00217 
00218 }
00219 
00220 #endif