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, const std::string& description = std::string(), const std::string& serial = std::string());
00084     int close();
00085     int reset();
00086     int flush(int mask = Input|Output);
00087     int set_interface(enum ftdi_interface interface);
00088     void set_usb_device(struct usb_dev_handle *dev);
00089 
00090     /* Line manipulators */
00091     int set_baud_rate(int baudrate);
00092     int set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity);
00093     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);
00094 
00095     /* I/O */
00096     int read(unsigned char *buf, int size);
00097     int write(unsigned char *buf, int size);
00098     int set_read_chunk_size(unsigned int chunksize);
00099     int set_write_chunk_size(unsigned int chunksize);
00100     int read_chunk_size();
00101     int write_chunk_size();
00102 
00103     /* Async IO
00104     TODO: should wrap?
00105     int writeAsync(unsigned char *buf, int size);
00106     void asyncComplete(int wait_for_more);
00107     */
00108 
00109     /* Flow control */
00110     int set_event_char(unsigned char eventch, unsigned char enable);
00111     int set_error_char(unsigned char errorch, unsigned char enable);
00112     int set_flow_control(int flowctrl);
00113     int set_modem_control(int mask = Dtr|Rts);
00114     int set_latency(unsigned char latency);
00115     int set_dtr(bool state);
00116     int set_rts(bool state);
00117 
00118     unsigned short poll_modem_status();
00119     unsigned latency();
00120 
00121     /* BitBang mode */
00122     int set_bitmode(unsigned char bitmask, unsigned char mode);
00123     int bitbang_enable(unsigned char bitmask);
00124     int bitbang_disable();
00125     int read_pins(unsigned char *pins);
00126 
00127     /* Misc */
00128     char* error_string();
00129 
00130 protected:
00131     int get_strings();
00132 
00133     /* Properties */
00134     struct ftdi_context* context();
00135     void set_context(struct ftdi_context* context);
00136     void set_usb_device(struct usb_device *dev);
00137 
00138 private:
00139     class Private;
00140     boost::shared_ptr<Private> d;
00141 };
00142 
00145 class Eeprom
00146 {
00147 public:
00148     Eeprom(Context* parent);
00149     ~Eeprom();
00150 
00151     void init_defaults();
00152     void set_size(int size);
00153     int size(unsigned char *eeprom, int maxsize);
00154     int chip_id(unsigned int *chipid);
00155     int build(unsigned char *output);
00156     int read(unsigned char *eeprom);
00157     int write(unsigned char *eeprom);
00158     int erase();
00159 
00160 private:
00161     class Private;
00162     boost::shared_ptr<Private> d;
00163 };
00164 
00167 class List
00168 {
00169 public:
00170     List(struct ftdi_device_list* devlist = 0);
00171     ~List();
00172 
00173     static List* find_all(int vendor, int product);
00174 
00176     typedef std::list<Context> ListType;
00178     typedef ListType::iterator iterator;
00180     typedef ListType::const_iterator const_iterator;
00182     typedef ListType::reverse_iterator reverse_iterator;
00184     typedef ListType::const_reverse_iterator const_reverse_iterator;
00185 
00186     iterator begin();
00187     iterator end();
00188     const_iterator begin() const;
00189     const_iterator end() const;
00190 
00191     reverse_iterator rbegin();
00192     reverse_iterator rend();
00193     const_reverse_iterator rbegin() const;
00194     const_reverse_iterator rend() const;
00195 
00196     ListType::size_type size() const;
00197     bool empty() const;
00198     void clear();
00199 
00200     void push_back(const Context& element);
00201     void push_front(const Context& element);
00202 
00203     iterator erase(iterator pos);
00204     iterator erase(iterator beg, iterator end);
00205 
00206 private:
00207     class Private;
00208     boost::shared_ptr<Private> d;
00209 };
00210 
00211 }
00212 
00213 #endif

Generated on Tue Jun 12 16:40:47 2012 for libftdi by  doxygen 1.4.6