libftdi1  1.0
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-2013 by Marek Vavruša and libftdi developers
00006     email                : opensource@intra2net.com and marek@vavrusa.com
00007  ***************************************************************************/
00008 /*
00009 Copyright (C) 2008-2013 by Marek Vavruša and libftdi developers
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 libusb_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 libusb_device_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 bitbang_disable();
00127     int read_pins(unsigned char *pins);
00128 
00129     /* Misc */
00130     char* error_string();
00131 
00132 protected:
00133     int get_strings();
00134     int get_strings_and_reopen();
00135 
00136     /* Properties */
00137     struct ftdi_context* context();
00138     void set_context(struct ftdi_context* context);
00139     void set_usb_device(struct libusb_device *dev);
00140 
00141 private:
00142     class Private;
00143     boost::shared_ptr<Private> d;
00144 };
00145 
00148 class Eeprom
00149 {
00150 public:
00151     Eeprom(Context* parent);
00152     ~Eeprom();
00153 
00154     int init_defaults(char *manufacturer, char* product, char * serial);
00155     int chip_id(unsigned int *chipid);
00156     int build(unsigned char *output);
00157 
00158     int read(unsigned char *eeprom);
00159     int write(unsigned char *eeprom);
00160     int read_location(int eeprom_addr, unsigned short *eeprom_val);
00161     int write_location(int eeprom_addr, unsigned short eeprom_val);
00162     int erase();
00163 
00164 private:
00165     class Private;
00166     boost::shared_ptr<Private> d;
00167 };
00168 
00171 class List
00172 {
00173 public:
00174     List(struct ftdi_device_list* devlist = 0);
00175     ~List();
00176 
00177     static List* find_all(Context &context, int vendor, int product);
00178 
00180     typedef std::list<Context> ListType;
00182     typedef ListType::iterator iterator;
00184     typedef ListType::const_iterator const_iterator;
00186     typedef ListType::reverse_iterator reverse_iterator;
00188     typedef ListType::const_reverse_iterator const_reverse_iterator;
00189 
00190     iterator begin();
00191     iterator end();
00192     const_iterator begin() const;
00193     const_iterator end() const;
00194 
00195     reverse_iterator rbegin();
00196     reverse_iterator rend();
00197     const_reverse_iterator rbegin() const;
00198     const_reverse_iterator rend() const;
00199 
00200     ListType::size_type size() const;
00201     bool empty() const;
00202     void clear();
00203 
00204     void push_back(const Context& element);
00205     void push_front(const Context& element);
00206 
00207     iterator erase(iterator pos);
00208     iterator erase(iterator beg, iterator end);
00209 
00210 private:
00211     class Private;
00212     boost::shared_ptr<Private> d;
00213 };
00214 
00215 }
00216 
00217 #endif