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
00056
00057
00058
00059
00060 #include <ftdi.h>
00061 #include <cstring>
00062 #include <ecl/command_line.hpp>
00063
00064
00065
00066
00067
00068 using ecl::CmdLine;
00069 using ecl::UnlabeledValueArg;
00070 using ecl::ValueArg;
00071 using ecl::SwitchArg;
00072 using std::string;
00073
00074
00075
00076
00077
00078 int main(int argc, char **argv)
00079 {
00080 const unsigned short vendor_id = 0x0403;
00081 const unsigned short product_id = 0x6001;
00082
00083
00084
00085
00086 CmdLine cmd_line("This is used to write a new serial id string to the ftdi device.", ' ', "0.1");
00087 ValueArg<std::string> old_arg(
00088 "o", "old", "Identify the device via the old serial id (if there are multiple attached) ['unspecified'].", false,
00089 "unspecified", "string");
00090 UnlabeledValueArg<std::string> new_arg("new_id", "New serial id used to identify the device [kobuki].", false,
00091 "kobuki", "string");
00092 cmd_line.add(old_arg);
00093 cmd_line.add(new_arg);
00094 cmd_line.parse(argc, argv);
00095 bool using_old_id = false;
00096 string old_id;
00097 if (old_arg.getValue() != "unspecified")
00098 {
00099 using_old_id = true;
00100 old_id = old_arg.getValue();
00101 }
00102 string new_id = new_arg.getValue();
00103 std::string new_manufacturer("Yujin Robot");
00104 std::string new_product("iClebo Kobuki");
00105
00106
00107
00108
00109 std::cout << "Input Information" << std::endl;
00110 if (!using_old_id)
00111 {
00112 std::cout << " Old id: unused (first device found.)" << std::endl;
00113 }
00114 else
00115 {
00116 std::cout << " Old id: " << old_id << std::endl;
00117 }
00118 std::cout << " New id: " << new_id << std::endl;
00119
00120
00121
00122
00123 struct ftdi_context ftdi;
00124 if (ftdi_init(&ftdi) < 0)
00125 {
00126 std::cerr << "ftdi_init failed" << std::endl;
00127 return EXIT_FAILURE;
00128 }
00129 if (!using_old_id)
00130 {
00131 if (ftdi_usb_open(&ftdi, vendor_id, product_id) < 0)
00132 {
00133 std::cerr << "Couldn't find/open an ftdi device." << std::endl;
00134 return EXIT_FAILURE;
00135 }
00136 }
00137 else
00138 {
00139 if (ftdi_usb_open_desc(&ftdi, vendor_id, product_id, NULL, old_id.c_str()) < 0)
00140 {
00141 std::cerr << "Couldn't open the device with serial id string: " << old_id << std::endl;
00142 return EXIT_FAILURE;
00143 }
00144 }
00145
00146
00147
00148 std::cout << "Eeprom Binary" << std::endl;
00149 ftdi_eeprom eeprom;
00150 unsigned char eeprom_binary[512];
00151 int result = ftdi_read_eeprom(&ftdi,eeprom_binary);
00152 int size = FTDI_DEFAULT_EEPROM_SIZE;
00153
00154
00155 if (size < 0)
00156 {
00157 std::cerr << "Error: Could not read the eeprom from the requested device." << std::endl;
00158 return EXIT_FAILURE;
00159 }
00160 std::cout << " Read binary [" << size << " bytes]." << std::endl;
00161 std::cout << " Saving binary ['eeprom.backup']" << std::endl;
00162 FILE *fp = fopen ("eeprom.backup", "wb");
00163 fwrite (&eeprom_binary, 1, size, fp);
00164 fclose (fp);
00165
00166 std::cout << "Decoding into eeprom structure." << std::endl;
00167
00168 if ( ftdi_eeprom_decode(&eeprom, eeprom_binary, size) != 0 ) {
00169 std::cerr << "Error: Could not write raw binary eeprom into the eeprom structure." << std::endl;
00170 return EXIT_FAILURE;
00171 }
00172 std::cout << " Vendor Id : " << eeprom.vendor_id << std::endl;
00173 std::cout << " Product Id : " << eeprom.product_id << std::endl;
00174 std::cout << " Self Powered: " << eeprom.self_powered << std::endl;
00175 std::cout << " Remote Wake : " << eeprom.remote_wakeup << std::endl;
00176 std::cout << " In Iso : " << eeprom.in_is_isochronous << std::endl;
00177 std::cout << " Out Iso : " << eeprom.out_is_isochronous << std::endl;
00178 std::cout << " Suspend : " << eeprom.suspend_pull_downs << std::endl;
00179 std::cout << " Use Serial : " << eeprom.use_serial << std::endl;
00180 std::cout << " Change Usb : " << eeprom.change_usb_version << std::endl;
00181 std::cout << " Usb Version : " << eeprom.usb_version << std::endl;
00182 std::cout << " Max Power : " << eeprom.max_power << std::endl;
00183 std::cout << " Manufacturer: " << eeprom.manufacturer << std::endl;
00184 std::cout << " Product : " << eeprom.product << std::endl;
00185 std::cout << " Serial Id : " << eeprom.serial << std::endl;
00186 if ( eeprom.chip_type == TYPE_R ) {
00187 std::cout << " Chip Type : TYPE_R" << std::endl;
00188 } else {
00189 std::cout << " Chip Type : " << eeprom.chip_type << std::endl;
00190 }
00191
00192
00193
00194
00195 eeprom.chip_type = TYPE_R;
00196
00197
00198
00199 new_id = new_id + "_" + std::string(eeprom.serial);
00200
00201
00202
00203 eeprom.size = size;
00204 std::cout << " Size : " << eeprom.size << std::endl;
00205 std::cout << " New serial : " << new_id << "." << std::endl;
00206 std::cout << " New manufact. : " << new_manufacturer << "." << std::endl;
00207 std::cout << " New product : " << new_product << "." << std::endl;
00208 std::cout << " New chip type : TYPE_R." << std::endl;
00209 free(eeprom.serial);
00210 eeprom.serial = (char*)malloc(new_id.size() + 1);
00211 std::strcpy(eeprom.serial, new_id.c_str());
00212 free(eeprom.manufacturer);
00213 eeprom.manufacturer = (char*)malloc(new_manufacturer.size() + 1);
00214 std::strcpy(eeprom.manufacturer, new_manufacturer.c_str());
00215 free(eeprom.product);
00216 eeprom.product = (char*)malloc(new_product.size() + 1);
00217 std::strcpy(eeprom.product, new_product.c_str());
00218
00219 std::cout << "Building new eeprom binary." << std::endl;
00220 int eeprom_binary_length = ftdi_eeprom_build(&eeprom, eeprom_binary);
00221 if (eeprom_binary_length == -1)
00222 {
00223 std::cerr << "Eeprom binary exceeded 128 bytes, reduce the size of your strings." << std::endl;
00224 return EXIT_FAILURE;
00225 }
00226 else if (eeprom_binary_length == -2)
00227 {
00228 std::cerr << "Eeprom structure not valid." << std::endl;
00229 return EXIT_FAILURE;
00230 }
00231
00232
00233
00234
00235
00236 std::cout << " Flashing binary." << std::endl;
00237 result = ftdi_write_eeprom(&ftdi, eeprom_binary);
00238 if (result < 0)
00239 {
00240 std::cerr << " Could not rewrite the eeprom." << std::endl;
00241 return EXIT_FAILURE;
00242 } else {
00243 std::cout << " Flashed " << result << " bytes" << std::endl;
00244 }
00245 std::cout << "Done." << std::endl;
00246
00247 return 0;
00248 }
00249