writer.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  */
37 /*****************************************************************************
38  ** Includes
39  *****************************************************************************/
40 
41 #include <cstring>
42 #include <ftdi.h>
43 
44 /*****************************************************************************
45  ** Definition
46  *****************************************************************************/
47 
49 
52 class FTDI_Writer {
53 public:
58  log_level(0),
59  serial_only(false),
60  use_first_device(false),
61  vendor_id(0x10c4),
62  product_id(0xea60)
63  {;}
64 
69  void reset_flags() {
70  serial_only = false;
71  use_first_device = false;
72  return;
73  }
74 
81  int write(const std::string &new_id_) {
82  serial_only = true;
83  use_first_device = true;
84  int ret_val = write("", new_id_, "", "");
85  reset_flags();
86 
87  return ret_val;
88  }
89 
97  int write(const std::string &old_id_, const std::string &new_id_)
98  {
99  serial_only = true;
100  int ret_val = write(old_id_, new_id_, "", "");
101  reset_flags();
102 
103  return ret_val;
104  }
105 
115  int write(const std::string &old_id_, const std::string &new_id_, const std::string &manufacturer_, const std::string &product_)
116  {
117 // int ret;
118 // int i, no_devices;
119  struct ftdi_context ftdic;
120 // struct ftdi_device_list *devlist, *curdev;
121 /*
122  char manufacturer[128], description[128];
123  char serial[128];
124 */
125 
126  /* Initialization */
127  if (ftdi_init(&ftdic) < 0)
128  {
129  std::cerr << "ftdi_init failed" << std::endl;
130  return -1;
131  }
132 
133 // if ((no_devices = ftdi_usb_find_all(&ftdic, &devlist, vendor_id, product_id)) < 0)
134 // {
135 // std::cerr << "ftdi_usb_find_all failed: " << ftdi_get_error_string(&ftdic) << std::endl;
136 // return -1;
137 // }
138 // std::cout << "Number of FTDI devices found: " << no_devices << std::endl;
139 // std::cout << std::endl;
140 //
141 //
142 // i = 0;
143 // for (curdev = devlist; curdev != NULL; i++)
144 // {
145 // if( i > no_devices ) break; // Exceptional cases
146 //
147 // std::cout << "Target Device #" << i << std::endl;
148 #if 0
149  if ((ret = ftdi_usb_get_strings(&ftdic, curdev->dev, manufacturer, 128, description, 128, serial, 128)) < 0)
150  {
151  std::cerr << "ftdi_usb_get_strings failed: " << ftdi_get_error_string(&ftdic) << "." << std::endl;
152  return -1;
153  /*
154  if( std::string(ftdi_get_error_string(&ftdic)).find("Operation not permitted") == -1 ) {
155  continue;
156  }
157  else
158  return EXIT_FAILURE;
159  */
160  }
161  std::cout << " Manufacturer: " << manufacturer << std::endl;
162  std::cout << " Description : " << description << std::endl;
163  std::cout << " Serial Id : " << serial << std::endl;
164  std::cout << std::endl;
165 
166  if ((!use_first_device) && (serial != old_id_)) {
167  std::cout << "It is not intedend target." << std::endl;
168  std::cout << "target: " << old_id_ << ", current: " << serial << std::endl;
169  curdev = curdev->next;
170  continue;
171  }
172 #endif
173  // Open the ftdi device
174  if (use_first_device) {
175  if (ftdi_usb_open(&ftdic, vendor_id, product_id) < 0)
176  {
177  std::cerr << "Couldn't find/open a ftdi device." << std::endl;
178  return -1;
179  }
180  } else {
181  if (ftdi_usb_open_desc(&ftdic, vendor_id, product_id, NULL, old_id_.c_str()) < 0)
182  {
183  std::cerr << "Couldn't open the device with serial id string " << old_id_ << std::endl;
184  return -1;
185  }
186  }
187 
188  // Open an Eeprom
189  //std::cout << "Eeprom Binary" << std::endl;
190  ftdi_eeprom eeprom;
191  unsigned char eeprom_binary[512];
192  int result = ftdi_read_eeprom(&ftdic, eeprom_binary);
193  int size = FTDI_DEFAULT_EEPROM_SIZE;
194  // this never works for me
195  // int size = ftdi_read_eeprom_getsize(&ftdi, eeprom_binary, 512);
196  if (size < 0)
197  {
198  std::cerr << "Error: Could not read the eeprom from the requested device." << std::endl;
199  return -1;
200  }
201  std::cout << "Read eeprom binary [" << size << " bytes]." << std::endl;
202 
203  std::cout << "Decoding into eeprom structure." << std::endl;
204  // put the binary into an eeprom structure
205  if ( ftdi_eeprom_decode(&eeprom, eeprom_binary, size) != 0 ) {
206  std::cerr << "Error: Could not write raw binary eeprom into the eeprom structure." << std::endl;
207  return -1;//EXIT_FAILURE;
208  }
209 
210  std::string new_manufacturer = manufacturer_;
211  std::string new_product = product_;
212  std::string new_id = new_id_;
213 
214  //Preparnig eeprom data to write
215  eeprom.chip_type = TYPE_R;
216  eeprom.size = size;
217  free(eeprom.serial);
218  eeprom.serial = (char*)malloc(new_id.size() + 1);
219  std::strcpy(eeprom.serial, new_id.c_str());
220  if (!serial_only) {
221  free(eeprom.manufacturer);
222  eeprom.manufacturer = (char*)malloc(new_manufacturer.size() + 1);
223  std::strcpy(eeprom.manufacturer, new_manufacturer.c_str());
224  free(eeprom.product);
225  eeprom.product = (char*)malloc(new_product.size() + 1);
226  std::strcpy(eeprom.product, new_product.c_str());
227  }
228 
229  /* Build new eeprom in binary */
230  std::cout << "Building new eeprom binary." << std::endl;
231  int eeprom_binary_length = ftdi_eeprom_build(&eeprom, eeprom_binary);
232  if (eeprom_binary_length == -1)
233  {
234  std::cerr << "Eeprom binary exceeded 128 bytes, reduce the size of your strings." << std::endl;
235  return -1;
236  //continue;
237  //return EXIT_FAILURE;
238  }
239  else if (eeprom_binary_length == -2)
240  {
241  std::cerr << "Eeprom structure not valid." << std::endl;
242  return -1; //continue;
243  //return EXIT_FAILURE;
244  }
245 
246  /* Flashing eeprom on ftdi chip */
247  std::cout << "Flashing eeprom binary." << std::endl;
248  result = ftdi_write_eeprom(&ftdic, eeprom_binary);
249  if (result < 0)
250  {
251  std::cerr << " Could not rewrite the eeprom." << std::endl;
252  return -1; //continue;
253  //return EXIT_FAILURE;
254  }
255  std::cout << " Flashed " << ftdic.eeprom_size << " bytes" << std::endl;
256 
257 #if 0
258  // Verification
259  if ((ret = ftdi_usb_get_strings(&ftdic, curdev->dev, manufacturer, 128, description, 128, serial, 128)) < 0)
260  {
261  std::cerr << "ftdi_usb_get_strings failed: " << ftdi_get_error_string(&ftdic) << "." << std::endl;
262  return -1;
263  }
264  std::cout << "Verify Device #" << i << std::endl;
265  std::cout << " Manufacturer: " << manufacturer << std::endl;
266  std::cout << " Description : " << description << std::endl;
267  std::cout << " Serial Id : " << serial << std::endl;
268  std::cout << std::endl;
269 #endif
270  std::cout << "Done." << std::endl;
271 //
272 // curdev = curdev->next;
273 // }
274 //
275 // ftdi_list_free(&devlist);
276  ftdi_deinit(&ftdic);
277  return 0;
278  }
279 
280 private:
281  int log_level;
282  bool serial_only;
288  const unsigned short vendor_id;
289 
293  const unsigned short product_id;
294 };
int log_level
Definition: writer.hpp:281
const unsigned short product_id
Definition: writer.hpp:293
bool use_first_device
Definition: writer.hpp:283
int write(const std::string &new_id_)
Definition: writer.hpp:81
int write(const std::string &old_id_, const std::string &new_id_)
Definition: writer.hpp:97
const unsigned short vendor_id
Definition: writer.hpp:288
FTDI_Writer()
Definition: writer.hpp:57
A Writer class.
Definition: writer.hpp:52
bool serial_only
Definition: writer.hpp:282
void reset_flags()
Definition: writer.hpp:69
int write(const std::string &old_id_, const std::string &new_id_, const std::string &manufacturer_, const std::string &product_)
Definition: writer.hpp:115


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