scanner.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, SawYer Robot.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of SawYer Robot nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * RACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
39 /*****************************************************************************
40  ** Includes
41  *****************************************************************************/
42 
43 #include <vector>
44 #include <map>
45 #include <usb.h>
46 #include <stdint.h>
47 
48 /*****************************************************************************
49  ** Definition
50  *****************************************************************************/
51 
53 
56 class FTDI_Scanner {
57 public:
64  std::vector<struct usb_device *> find_devices(uint16_t vendor, uint16_t product)
65  {
66  struct usb_bus *bus;
67  struct usb_device *dev;
68  struct usb_bus *busses;
69  std::vector<struct usb_device *> ret_vec;
70 
71  usb_init();
72  //usb_set_debug(32); // disable it to see bebug outputs
73  usb_find_busses();
74  usb_find_devices();
75  busses = usb_get_busses();
76 
77  for (bus = busses; bus; bus = bus->next)
78  for (dev = bus->devices; dev; dev = dev->next)
79  if ((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
80  ret_vec.push_back(dev);
81 
82  return ret_vec;
83  }
84 
89  int scan()
90  {
91  devices = find_devices(0x10c4,0xea60);
92 
93  if( devices.empty() ) return -1;
94  scanned = true;
95  retrieved = false;
96  return devices.size();
97  }
98 
106  int retrieve()
107  {
108  if( !scanned ) if ( scan() < 0 ) return -1;
109  if( devices.empty() ) return -1;
110 
111  char buff[128];
112  descriptions.clear();
113 
114  for (unsigned int i=0; i<devices.size(); i++) {
115  struct usb_device *dev = devices[i];
116  usb_dev_handle *h = usb_open(dev);
117  if ( h < 0 ) continue;
118 
119  std::map<std::string, std::string> M_desc;
120  if ( usb_get_string_simple(h, dev->descriptor.iSerialNumber, buff, 128) < 0 ) continue;
121  M_desc["serial_number"] = std::string(buff);
122 
123  if ( usb_get_string_simple(h, dev->descriptor.iManufacturer, buff, 128) < 0 ) continue;
124  M_desc["manufacturer"] = std::string(buff);
125 
126  if ( usb_get_string_simple(h, dev->descriptor.iProduct, buff, 128) < 0 ) continue;
127  M_desc["product"] = std::string(buff);
128 
129  descriptions.push_back(M_desc);
130  }
131  retrieved = true;
132  return descriptions.size();
133  }
134 
143  int get_serial_id(unsigned int index, std::string &serial_id) {
144  if( ! retrieved ) if( retrieve() < 0 ) return -1;
145  if (descriptions.size() <= index) return -1;
146  serial_id = descriptions[index]["serial_number"];
147  return 0;
148  }
149 
158  int get_manufacturer (unsigned int index, std::string &manufacturer) {
159  if( ! retrieved ) if( retrieve() < 0 ) return -1;
160  if (descriptions.size() <= index) return -1;
161  manufacturer = descriptions[index]["manufacturer"];
162  return 0;
163  }
164 
173  int get_product (unsigned int index, std::string &product) {
174  if( ! retrieved ) if( retrieve() < 0 ) return -1;
175  if (descriptions.size() <= index) return -1;
176  product = descriptions[index]["product"];
177  return 0;
178  }
179 
187  int get_serial_id(std::string &serial_id) { return get_serial_id(0, serial_id); }
188 
196  int get_manufacturer (std::string &manufacturer) { return get_manufacturer(0, manufacturer); }
197 
205  int get_product (std::string &product) { return get_product(0, product); }
206 
211  int reset()
212  {
213  if( devices.empty() ) {
214  if( scan() < 0 ) return -1;
215  };
216 
217  struct usb_device *dev = devices[0];
218  usb_dev_handle *h = usb_open(dev);
219  if( h < 0 ) {
220  return -1;
221  }
222  int ret_val = usb_reset(h);
223  return ret_val;
224  }
225 
226 private:
227  bool scanned;
228  bool retrieved;
229  std::vector<struct usb_device *> devices;
230  std::vector<std::map<std::string, std::string> > descriptions;
231 };
int retrieve()
Definition: scanner.hpp:106
bool retrieved
Definition: scanner.hpp:228
int get_serial_id(unsigned int index, std::string &serial_id)
Definition: scanner.hpp:143
std::vector< struct usb_device * > find_devices(uint16_t vendor, uint16_t product)
Definition: scanner.hpp:64
std::vector< struct usb_device * > devices
Definition: scanner.hpp:229
int scan()
Definition: scanner.hpp:89
int get_product(unsigned int index, std::string &product)
Definition: scanner.hpp:173
int get_manufacturer(std::string &manufacturer)
Definition: scanner.hpp:196
int reset()
Definition: scanner.hpp:211
int get_manufacturer(unsigned int index, std::string &manufacturer)
Definition: scanner.hpp:158
std::vector< std::map< std::string, std::string > > descriptions
Definition: scanner.hpp:230
int get_product(std::string &product)
Definition: scanner.hpp:205
bool scanned
Definition: scanner.hpp:227
A Scanner class.
Definition: scanner.hpp:56
int get_serial_id(std::string &serial_id)
Definition: scanner.hpp:187


roch_ftdi
Author(s): Younghun Ju
autogenerated on Mon Jun 10 2019 14:41:09