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