78 int main(
int argc,
char **argv)
80 const unsigned short vendor_id = 0x0403;
81 const unsigned short product_id = 0x6001;
86 CmdLine cmd_line(
"This is used to write a new serial id string to the ftdi device.",
' ',
"0.1");
88 "o",
"old",
"Identify the device via the old serial id (if there are multiple attached) ['unspecified'].",
false,
89 "unspecified",
"string");
92 cmd_line.
add(old_arg);
93 cmd_line.
add(new_arg);
94 cmd_line.
parse(argc, argv);
95 bool using_old_id =
false;
97 if (old_arg.
getValue() !=
"unspecified")
103 std::string new_manufacturer(
"Yujin Robot");
104 std::string new_product(
"iClebo Kobuki");
109 std::cout <<
"Input Information" << std::endl;
112 std::cout <<
" Old id: unused (first device found.)" << std::endl;
116 std::cout <<
" Old id: " << old_id << std::endl;
118 std::cout <<
" New id: " << new_id << std::endl;
123 struct ftdi_context ftdi;
124 if (ftdi_init(&ftdi) < 0)
126 std::cerr <<
"ftdi_init failed" << std::endl;
131 if (ftdi_usb_open(&ftdi, vendor_id, product_id) < 0)
133 std::cerr <<
"Couldn't find/open an ftdi device." << std::endl;
139 if (ftdi_usb_open_desc(&ftdi, vendor_id, product_id, NULL, old_id.c_str()) < 0)
141 std::cerr <<
"Couldn't open the device with serial id string: " << old_id << std::endl;
148 std::cout <<
"Eeprom Binary" << std::endl;
150 unsigned char eeprom_binary[512];
151 int result = ftdi_read_eeprom(&ftdi,eeprom_binary);
152 int size = FTDI_DEFAULT_EEPROM_SIZE;
157 std::cerr <<
"Error: Could not read the eeprom from the requested device." << std::endl;
160 std::cout <<
" Read binary [" << size <<
" bytes]." << std::endl;
161 std::cout <<
" Saving binary ['eeprom.backup']" << std::endl;
162 FILE *fp = fopen (
"eeprom.backup",
"wb");
163 fwrite (&eeprom_binary, 1, size, fp);
166 std::cout <<
"Decoding into eeprom structure." << std::endl;
168 if ( ftdi_eeprom_decode(&eeprom, eeprom_binary, size) != 0 ) {
169 std::cerr <<
"Error: Could not write raw binary eeprom into the eeprom structure." << std::endl;
172 std::cout <<
" Vendor Id : " << eeprom.vendor_id << std::endl;
173 std::cout <<
" Product Id : " << eeprom.product_id << std::endl;
174 std::cout <<
" Self Powered: " << eeprom.self_powered << std::endl;
175 std::cout <<
" Remote Wake : " << eeprom.remote_wakeup << std::endl;
176 std::cout <<
" In Iso : " << eeprom.in_is_isochronous << std::endl;
177 std::cout <<
" Out Iso : " << eeprom.out_is_isochronous << std::endl;
178 std::cout <<
" Suspend : " << eeprom.suspend_pull_downs << std::endl;
179 std::cout <<
" Use Serial : " << eeprom.use_serial << std::endl;
180 std::cout <<
" Change Usb : " << eeprom.change_usb_version << std::endl;
181 std::cout <<
" Usb Version : " << eeprom.usb_version << std::endl;
182 std::cout <<
" Max Power : " << eeprom.max_power << std::endl;
183 std::cout <<
" Manufacturer: " << eeprom.manufacturer << std::endl;
184 std::cout <<
" Product : " << eeprom.product << std::endl;
185 std::cout <<
" Serial Id : " << eeprom.serial << std::endl;
186 if ( eeprom.chip_type == TYPE_R ) {
187 std::cout <<
" Chip Type : TYPE_R" << std::endl;
189 std::cout <<
" Chip Type : " << eeprom.chip_type << std::endl;
195 eeprom.chip_type = TYPE_R;
199 new_id = new_id +
"_" + std::string(eeprom.serial);
204 std::cout <<
" Size : " << eeprom.size << std::endl;
205 std::cout <<
" New serial : " << new_id <<
"." << std::endl;
206 std::cout <<
" New manufact. : " << new_manufacturer <<
"." << std::endl;
207 std::cout <<
" New product : " << new_product <<
"." << std::endl;
208 std::cout <<
" New chip type : TYPE_R." << std::endl;
210 eeprom.serial = (
char*)malloc(new_id.size() + 1);
211 std::strcpy(eeprom.serial, new_id.c_str());
212 free(eeprom.manufacturer);
213 eeprom.manufacturer = (
char*)malloc(new_manufacturer.size() + 1);
214 std::strcpy(eeprom.manufacturer, new_manufacturer.c_str());
215 free(eeprom.product);
216 eeprom.product = (
char*)malloc(new_product.size() + 1);
217 std::strcpy(eeprom.product, new_product.c_str());
219 std::cout <<
"Building new eeprom binary." << std::endl;
220 int eeprom_binary_length = ftdi_eeprom_build(&eeprom, eeprom_binary);
221 if (eeprom_binary_length == -1)
223 std::cerr <<
"Eeprom binary exceeded 128 bytes, reduce the size of your strings." << std::endl;
226 else if (eeprom_binary_length == -2)
228 std::cerr <<
"Eeprom structure not valid." << std::endl;
236 std::cout <<
" Flashing binary." << std::endl;
237 result = ftdi_write_eeprom(&ftdi, eeprom_binary);
240 std::cerr <<
" Could not rewrite the eeprom." << std::endl;
243 std::cout <<
" Flashed " << result <<
" bytes" << std::endl;
245 std::cout <<
"Done." << std::endl;
int main(int argc, char **argv)
void parse(int argc, char **argv)