poe_set_ip.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 #include <string>
4 
5 #include "depthai/depthai.hpp"
7 
8 // for string delimiter
9 std::vector<int> split(std::string s, std::string delimiter) {
10  size_t pos_start = 0, pos_end, delim_len = delimiter.length();
11  std::string token;
12  std::vector<int> res;
13 
14  while((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
15  token = s.substr(pos_start, pos_end - pos_start);
16  pos_start = pos_end + delim_len;
17  res.push_back(stoi(token));
18  }
19  res.push_back(stoi(s.substr(pos_start)));
20  return res;
21 }
22 
23 std::string checkStr(std::string str) {
24  std::vector<int> v = split(str, ".");
25  if(v.size() != 4) {
26  std::cout << "Entered value " << str << " doesn't contain 3 dots. Value has to be in the following format: '255.255.255.255'" << std::endl;
27  exit(0);
28  }
29  for(auto i : v) {
30  if(i < 0 || 255 < i) {
31  std::cout << "Entered values can't be above 255!" << std::endl;
32  exit(0);
33  }
34  }
35  return str;
36 }
37 
38 int main(int argc, char** argv) {
39  bool found = false;
42  if(!found) {
43  std::cout << "No device found to flash. Exiting." << std::endl;
44  return -1;
45  }
46 
47  std::cout << "Found device with name: " << info.getMxId() << std::endl;
48  std::cout << "-------------------------------------" << std::endl;
49  std::cout << "\"1\" to set a static IPv4 address" << std::endl;
50  std::cout << "\"2\" to set a dynamic IPv4 address" << std::endl;
51  std::cout << "\"3\" to clear the config" << std::endl;
52  auto key = std::cin.get();
53  std::cout << "-------------------------------------" << std::endl;
54 
55  bool success = false;
56  std::string error;
57  dai::DeviceBootloader bl(info, true);
58  if(key == '1' || key == '2') {
59  std::string ip, mask, gateway, in;
60 
61  std::cout << "Enter IPv4: ";
62  std::cin >> ip;
63  checkStr(ip);
64 
65  std::cout << "Enter IPv4 Mask: ";
66  std::cin >> mask;
67  checkStr(mask);
68 
69  std::cout << "Enter IPv4 Gateway: ";
70  std::cin >> gateway;
71  checkStr(gateway);
72 
73  std::string mode = "static";
74  if(key == '2') mode = "dynamic";
75  std::cout << "Flashing " << mode << " IPv4 " << ip << ", mask " << mask << ", gateway " << gateway << " to the POE device. Enter 'y' to confirm. ";
76  std::cin >> in;
77  if(in != "y") {
78  std::cout << "Flashing aborted.";
79  return 0;
80  }
81  auto conf = dai::DeviceBootloader::Config();
82  if(key == '1') {
83  conf.setStaticIPv4(ip, mask, gateway);
84  } else {
85  conf.setDynamicIPv4(ip, mask, gateway);
86  }
87 
88  std::tie(success, error) = bl.flashConfig(conf);
89  } else if(key == '3') {
90  std::tie(success, error) = bl.flashConfigClear();
91  } else {
92  std::cout << "Entered value should either be '1', '2' or '3'!";
93  return 0;
94  }
95 
96  if(success) {
97  std::cout << "Flashing successful." << std::endl;
98  } else {
99  std::cout << "Flashing failed: " << error << std::endl;
100  }
101 }
dai::DeviceBootloader
Definition: DeviceBootloader.hpp:29
dai::DeviceInfo
Definition: XLinkConnection.hpp:27
main
int main(int argc, char **argv)
Definition: poe_set_ip.cpp:38
dai::logger::info
void info(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:78
split
std::vector< int > split(std::string s, std::string delimiter)
Definition: poe_set_ip.cpp:9
dai::DeviceBootloader::Config
Definition: DeviceBootloader.hpp:39
dai::DeviceBootloader::flashConfigClear
std::tuple< bool, std::string > flashConfigClear(Memory memory=Memory::AUTO, Type type=Type::AUTO)
Definition: DeviceBootloader.cpp:1290
depthai.hpp
XLinkConnection.hpp
dai::logger::error
void error(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:90
checkStr
std::string checkStr(std::string str)
Definition: poe_set_ip.cpp:23
dai::DeviceBootloader::getFirstAvailableDevice
static std::tuple< bool, DeviceInfo > getFirstAvailableDevice()
Definition: DeviceBootloader.cpp:48
dai::DeviceBootloader::flashConfig
std::tuple< bool, std::string > flashConfig(const Config &config, Memory memory=Memory::AUTO, Type type=Type::AUTO)
Definition: DeviceBootloader.cpp:1359


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