scanner.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Yujin Robot.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of Yujin Robot nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * RACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00039 /*****************************************************************************
00040  ** Includes
00041  *****************************************************************************/
00042 
00043 #include <vector>
00044 #include <map>
00045 #include <usb.h>
00046 #include <stdint.h>
00047 
00048 /*****************************************************************************
00049  ** Definition
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     //usb_set_debug(32); // disable it to see bebug outputs
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 };


kobuki_ftdi
Author(s): Younghun Ju
autogenerated on Mon Oct 6 2014 01:31:47