flash_bootloader.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <string>
3 
4 #include "XLink/XLink.h"
5 #include "depthai/depthai.hpp"
7 
8 static const char* ProtocolToStr(XLinkProtocol_t val) {
9  switch(val) {
10  case X_LINK_USB_VSC:
11  return "X_LINK_USB_VSC";
12  case X_LINK_USB_CDC:
13  return "X_LINK_USB_CDC";
14  case X_LINK_PCIE:
15  return "X_LINK_PCIE";
16  case X_LINK_IPC:
17  return "X_LINK_IPC";
18  case X_LINK_TCP_IP:
19  return "X_LINK_TCP_IP";
20  case X_LINK_NMB_OF_PROTOCOLS:
21  return "X_LINK_NMB_OF_PROTOCOLS";
22  case X_LINK_ANY_PROTOCOL:
23  return "X_LINK_ANY_PROTOCOL";
24  default:
25  return "INVALID_ENUM_VALUE";
26  break;
27  }
28 }
29 
30 int main(int argc, char** argv) {
31  using namespace std::chrono;
32 
34  if(argc > 1) {
35  std::string blTypeStr(argv[1]);
36  if(blTypeStr == "usb") {
37  blType = dai::DeviceBootloader::Type::USB;
38  } else if(blTypeStr == "network") {
39  blType = dai::DeviceBootloader::Type::NETWORK;
40  } else {
41  std::cout << "Specify either 'usb' or 'network' bootloader type\n";
42  return 0;
43  }
44  }
45 
48  if(deviceInfos.empty()) {
49  std::cout << "No device found to flash. Exiting." << std::endl;
50  return -1;
51  } else {
52  for(int i = 0; i < deviceInfos.size(); i++) {
53  const auto& devInfo = deviceInfos[i];
54  std::cout << "[" << i << "] " << devInfo.getMxId() << "[" << ProtocolToStr(devInfo.protocol) << "]";
55  if(devInfo.state == X_LINK_BOOTLOADER) {
56  dai::DeviceBootloader bl(devInfo);
57  std::cout << " current bootloader: " << bl.getVersion();
58  }
59  std::cout << std::endl;
60  }
61  int selected = 0;
62  std::cout << "Which DepthAI device to flash bootloader for [0.." << deviceInfos.size() - 1 << "]\n";
63  std::cin >> selected;
64  info = deviceInfos[selected];
65  }
66 
67  bool hasBootloader = (info.state == X_LINK_BOOTLOADER);
68  if(hasBootloader) {
69  std::cout << "Warning! Flashing bootloader can potentially soft brick your device and should be done with caution." << std::endl;
70  std::cout << "Do not unplug your device while the bootloader is flashing." << std::endl;
71  std::cout << "Type 'y' and press enter to proceed, otherwise exits: ";
72  std::cin.ignore();
73  if(std::cin.get() != 'y') {
74  std::cout << "Prompt declined, exiting..." << std::endl;
75  return -1;
76  }
77  }
78 
79  // Open DeviceBootloader and allow flashing bootloader
80  std::cout << "Booting latest bootloader first, will take a tad longer..." << std::endl;
81  dai::DeviceBootloader bl(info, true);
82  auto currentBlType = bl.getType();
83 
84  if(blType == dai::DeviceBootloader::Type::AUTO) {
85  blType = currentBlType;
86  }
87 
88  // Check if bootloader type is the same, if already booted by bootloader (not in USB recovery mode)
89  if(currentBlType != blType && hasBootloader) {
90  std::cout << "Are you sure you want to flash '" << blType << "' bootloader over current '" << currentBlType << "' bootloader?" << std::endl;
91  std::cout << "Type 'y' and press enter to proceed, otherwise exits: ";
92  std::cin.ignore();
93  if(std::cin.get() != 'y') {
94  std::cout << "Prompt declined, exiting..." << std::endl;
95  return -1;
96  }
97  }
98 
99  // Create a progress callback lambda
100  auto progress = [](float p) { std::cout << "Flashing Progress..." << p * 100 << "%" << std::endl; };
101 
102  std::cout << "Flashing " << blType << " bootloader..." << std::endl;
103  auto t1 = steady_clock::now();
104  bool success = false;
105  std::string message;
106  std::tie(success, message) = bl.flashBootloader(dai::DeviceBootloader::Memory::FLASH, blType, progress);
107  if(success) {
108  std::cout << "Flashing successful. Took " << duration_cast<milliseconds>(steady_clock::now() - t1).count() << "ms" << std::endl;
109  } else {
110  std::cout << "Flashing failed: " << message << std::endl;
111  }
112  return 0;
113 }
dai::DeviceBootloader
Definition: DeviceBootloader.hpp:29
DAI_SPAN_NAMESPACE_NAME::get
constexpr auto get(span< E, S > s) -> decltype(s[N])
Definition: span.hpp:491
dai::DeviceInfo
Definition: XLinkConnection.hpp:27
dai::logger::info
void info(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:78
main
int main(int argc, char **argv)
Definition: flash_bootloader.cpp:30
dai::DeviceBootloader::getType
Type getType() const
Definition: DeviceBootloader.cpp:638
dai::bootloader::Type
Type
Definition: Type.hpp:11
depthai.hpp
dai::DeviceBootloader::getVersion
Version getVersion() const
Definition: DeviceBootloader.cpp:599
dai::bootloader::Type::AUTO
@ AUTO
XLinkConnection.hpp
dai::DeviceBootloader::flashBootloader
std::tuple< bool, std::string > flashBootloader(std::function< void(float)> progressCallback, const dai::Path &path={})
Definition: DeviceBootloader.cpp:854
ProtocolToStr
static const char * ProtocolToStr(XLinkProtocol_t val)
Definition: flash_bootloader.cpp:8
dai::DeviceBootloader::getAllAvailableDevices
static std::vector< DeviceInfo > getAllAvailableDevices()
Definition: DeviceBootloader.cpp:63


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19