Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00039
00040
00041
00042
00043 #include <vector>
00044 #include <map>
00045 #include <usb.h>
00046 #include <stdint.h>
00047
00048
00049
00050
00051
00053
00056 class FTDI_Scanner {
00057 public:
00064 std::vector<struct usb_device *> find_devices(uint16_t vendor, uint16_t product)
00065 {
00066 struct usb_bus *bus;
00067 struct usb_device *dev;
00068 struct usb_bus *busses;
00069 std::vector<struct usb_device *> ret_vec;
00070
00071 usb_init();
00072
00073 usb_find_busses();
00074 usb_find_devices();
00075 busses = usb_get_busses();
00076
00077 for (bus = busses; bus; bus = bus->next)
00078 for (dev = bus->devices; dev; dev = dev->next)
00079 if ((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
00080 ret_vec.push_back(dev);
00081
00082 return ret_vec;
00083 }
00084
00089 int scan()
00090 {
00091 devices = find_devices(0x0403,0x6001);
00092
00093 if( devices.empty() ) return -1;
00094 scanned = true;
00095 retrieved = false;
00096 return devices.size();
00097 }
00098
00106 int retrieve()
00107 {
00108 if( !scanned ) if ( scan() < 0 ) return -1;
00109 if( devices.empty() ) return -1;
00110
00111 char buff[128];
00112 descriptions.clear();
00113
00114 for (unsigned int i=0; i<devices.size(); i++) {
00115 struct usb_device *dev = devices[i];
00116 usb_dev_handle *h = usb_open(dev);
00117 if ( h < 0 ) continue;
00118
00119 std::map<std::string, std::string> M_desc;
00120 if ( usb_get_string_simple(h, dev->descriptor.iSerialNumber, buff, 128) < 0 ) continue;
00121 M_desc["serial_number"] = std::string(buff);
00122
00123 if ( usb_get_string_simple(h, dev->descriptor.iManufacturer, buff, 128) < 0 ) continue;
00124 M_desc["manufacturer"] = std::string(buff);
00125
00126 if ( usb_get_string_simple(h, dev->descriptor.iProduct, buff, 128) < 0 ) continue;
00127 M_desc["product"] = std::string(buff);
00128
00129 descriptions.push_back(M_desc);
00130 }
00131 retrieved = true;
00132 return descriptions.size();
00133 }
00134
00143 int get_serial_id(unsigned int index, std::string &serial_id) {
00144 if( ! retrieved ) if( retrieve() < 0 ) return -1;
00145 if (descriptions.size() <= index) return -1;
00146 serial_id = descriptions[index]["serial_number"];
00147 return 0;
00148 }
00149
00158 int get_manufacturer (unsigned int index, std::string &manufacturer) {
00159 if( ! retrieved ) if( retrieve() < 0 ) return -1;
00160 if (descriptions.size() <= index) return -1;
00161 manufacturer = descriptions[index]["manufacturer"];
00162 return 0;
00163 }
00164
00173 int get_product (unsigned int index, std::string &product) {
00174 if( ! retrieved ) if( retrieve() < 0 ) return -1;
00175 if (descriptions.size() <= index) return -1;
00176 product = descriptions[index]["product"];
00177 return 0;
00178 }
00179
00187 int get_serial_id(std::string &serial_id) { return get_serial_id(0, serial_id); }
00188
00196 int get_manufacturer (std::string &manufacturer) { return get_manufacturer(0, manufacturer); }
00197
00205 int get_product (std::string &product) { return get_product(0, product); }
00206
00211 int reset()
00212 {
00213 if( devices.empty() ) {
00214 if( scan() < 0 ) return -1;
00215 };
00216
00217 struct usb_device *dev = devices[0];
00218 usb_dev_handle *h = usb_open(dev);
00219 if( h < 0 ) {
00220 return -1;
00221 }
00222 int ret_val = usb_reset(h);
00223 return ret_val;
00224 }
00225
00226 private:
00227 bool scanned;
00228 bool retrieved;
00229 std::vector<struct usb_device *> devices;
00230 std::vector<std::map<std::string, std::string> > descriptions;
00231 };