5 #include <boost/program_options.hpp> 6 #include <crazyflie_cpp/Crazyflie.h> 14 else if (token ==
"nrf51")
16 else throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
31 if (token ==
"flashAndVerify")
33 else if (token ==
"flashOnly")
35 else if (token ==
"verifyOnly")
37 else throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
49 virtual void info(
const std::string& msg)
52 std::cout << msg << std::endl;
61 int main(
int argc,
char **argv)
66 std::string defaultUri(
"radio://0/0/2M");
71 namespace po = boost::program_options;
73 po::options_description desc(
"Allowed options");
75 (
"help",
"produce help message")
76 (
"target", po::value<Crazyflie::BootloaderTarget>(&target)->required(),
"target {stm32,nrf51}")
77 (
"filename", po::value<std::string>(&fileName)->required(),
"file to flash")
78 (
"uri", po::value<std::string>(&uri)->default_value(defaultUri),
"unique ressource identifier")
79 (
"mode", po::value<Mode>(&mode)->default_value(mode),
"mode {default=flashAndVerify, flashOnly, verifyOnly}")
80 (
"verbose,v",
"verbose output")
86 po::store(po::parse_command_line(argc, argv, desc), vm);
89 if (vm.count(
"help")) {
90 std::cout << desc <<
"\n";
93 verbose = vm.count(
"verbose");
97 std::cerr << e.what() << std::endl << std::endl;
98 std::cerr << desc << std::endl;
108 if (uri != defaultUri) {
109 logger.
info(
"Reboot to Bootloader...");
111 logger.
info(
"...Done");
114 std::sprintf(addr,
"%" SCNx64, address);
115 defaultUri +=
"/" + std::string(addr);
118 std::ifstream stream(fileName.c_str(), std::ios::binary);
119 if (!stream.good()) {
120 std::cerr <<
"Couldn't open " << fileName <<
"!" << std::endl;
123 std::vector<uint8_t> targetData((
124 std::istreambuf_iterator<char>(stream)),
125 (std::istreambuf_iterator<char>()));
130 logger.
info(
"Flashing...");
134 logger.
info(
"Reading...");
135 std::vector<uint8_t> currentData;
136 cf.
readFlash(target, targetData.size(), currentData);
137 std::ofstream dbg(
"data.bin", std::ios::binary);
138 dbg.write((
char*)currentData.data(), currentData.size());
139 if (memcmp(targetData.data(), currentData.data(), targetData.size()) == 0) {
140 logger.
info(
"Verification successful!");
142 std::cerr <<
"Verification NOT successful!" << std::endl;
147 logger.
info(
"Reboot to firmware");
155 catch(std::exception& e)
157 std::cerr << e.what() << std::endl;
int main(int argc, char **argv)
void readFlash(BootloaderTarget target, size_t size, std::vector< uint8_t > &data)
void writeFlash(BootloaderTarget target, const std::vector< uint8_t > &data)
void rebootFromBootloader()
std::istream & operator>>(std::istream &in, Crazyflie::BootloaderTarget &target)
virtual void info(const std::string &msg)
uint64_t rebootToBootloader()