flash.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 
00004 #include <boost/program_options.hpp>
00005 #include <crazyflie_cpp/Crazyflie.h>
00006 
00007 std::istream& operator>>(std::istream& in, Crazyflie::BootloaderTarget& target)
00008 {
00009   std::string token;
00010   in >> token;
00011   if (token == "stm32")
00012     target = Crazyflie::TargetSTM32;
00013   else if (token == "nrf51")
00014     target = Crazyflie::TargetNRF51;
00015   else throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
00016   return in;
00017 }
00018 
00019 enum Mode
00020 {
00021   FlashAndVerify,
00022   FlashOnly,
00023   VerifyOnly,
00024 };
00025 
00026 std::istream& operator>>(std::istream& in, Mode& mode)
00027 {
00028   std::string token;
00029   in >> token;
00030   if (token == "flashAndVerify")
00031     mode = FlashAndVerify;
00032   else if (token == "flashOnly")
00033     mode = FlashOnly;
00034   else if (token == "verifyOnly")
00035     mode = VerifyOnly;
00036   else throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
00037   return in;
00038 }
00039 
00040 int main(int argc, char **argv)
00041 {
00042 
00043   std::string fileName;
00044   std::string uri;
00045   std::string defaultUri("radio://0/0/2M");
00046   Crazyflie::BootloaderTarget target;
00047   Mode mode = FlashAndVerify;
00048 
00049   namespace po = boost::program_options;
00050 
00051   po::options_description desc("Allowed options");
00052   desc.add_options()
00053     ("help", "produce help message")
00054     ("target", po::value<Crazyflie::BootloaderTarget>(&target)->required(), "target {stm32,nrf51}")
00055     ("filename", po::value<std::string>(&fileName)->required(), "file to flash")
00056     ("uri", po::value<std::string>(&uri)->default_value(defaultUri), "unique ressource identifier")
00057     ("mode", po::value<Mode>(&mode)->default_value(mode), "mode {default=flashAndVerify, flashOnly, verifyOnly}")
00058   ;
00059 
00060   try
00061   {
00062     po::variables_map vm;
00063     po::store(po::parse_command_line(argc, argv, desc), vm);
00064     po::notify(vm);
00065 
00066     if (vm.count("help")) {
00067       std::cout << desc << "\n";
00068       return 0;
00069     }
00070   }
00071   catch(po::error& e)
00072   {
00073     std::cerr << e.what() << std::endl << std::endl;
00074     std::cerr << desc << std::endl;
00075     return 1;
00076   }
00077 
00078   try
00079   {
00080     bool success = true;
00081     if (uri != defaultUri) {
00082       // std::cout << "Reboot to Bootloader...";
00083       Crazyflie cf(uri);
00084       uint64_t address = cf.rebootToBootloader();
00085       // std::cout << "...Done" << std::endl;
00086 
00087       char addr[17];
00088       std::sprintf(addr, "%lx", address);
00089       defaultUri += "/" + std::string(addr);
00090     }
00091 
00092     std::ifstream stream(fileName.c_str(), std::ios::binary);
00093     std::vector<uint8_t> targetData((
00094       std::istreambuf_iterator<char>(stream)),
00095       (std::istreambuf_iterator<char>()));
00096 
00097     Crazyflie cf(defaultUri);
00098 
00099     if (mode == FlashAndVerify || mode == FlashOnly) {
00100       // std::cout << "Flashing " << targetData.size() / 1024 << " kB" << std::endl;
00101       cf.writeFlash(target, targetData);
00102     }
00103     if (mode == FlashAndVerify || mode == VerifyOnly) {
00104       // std::cout << "Reading " << targetData.size() / 1024 << " kB" << std::endl;
00105       std::vector<uint8_t> currentData;
00106       cf.readFlash(target, targetData.size(), currentData);
00107       std::ofstream dbg("data.bin", std::ios::binary);
00108       dbg.write((char*)currentData.data(), currentData.size());
00109       if (memcmp(targetData.data(), currentData.data(), targetData.size()) == 0) {
00110         // std::cout << "Verification successful!" << std::endl;
00111       } else {
00112         std::cout << "Verification NOT successful!" << std::endl;
00113         success = false;
00114       }
00115     }
00116 
00117     // std::cout << "Reboot to firmware" << std::endl;
00118     cf.reboot();
00119 
00120     if (success) {
00121       return 0;
00122     }
00123     return 1;
00124   }
00125   catch(std::exception& e)
00126   {
00127     std::cerr << e.what() << std::endl;
00128     return 1;
00129   }
00130 }


crazyflie_tools
Author(s): Wolfgang Hoenig
autogenerated on Sun Oct 8 2017 02:48:04