From 029233ce0da7c321cf1697d0d238bed3a00a87c9 Mon Sep 17 00:00:00 2001 From: Erol444 Date: Fri, 19 Nov 2021 12:32:47 +0100 Subject: [PATCH] Added poe_set_ip example --- examples/CMakeLists.txt | 1 + examples/bootloader/poe_set_ip.cpp | 97 ++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 examples/bootloader/poe_set_ip.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5007f9c0c..8ff9e2597 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -143,6 +143,7 @@ hunter_private_data( dai_add_example(bootloader_version bootloader/bootloader_version.cpp ON) dai_add_example(flash_bootloader bootloader/flash_bootloader.cpp OFF) dai_add_example(bootloader_config bootloader/bootloader_config.cpp OFF) +dai_add_example(poe_set_ip bootloader/poe_set_ip.cpp OFF) # calibration dai_add_example(calibration_flash calibration/calibration_flash.cpp OFF) diff --git a/examples/bootloader/poe_set_ip.cpp b/examples/bootloader/poe_set_ip.cpp new file mode 100644 index 000000000..ef2309366 --- /dev/null +++ b/examples/bootloader/poe_set_ip.cpp @@ -0,0 +1,97 @@ +#include +#include +#include + +#include "depthai/depthai.hpp" +#include "depthai/xlink/XLinkConnection.hpp" + +// for string delimiter +std::vector split (std::string s, std::string delimiter) { + size_t pos_start = 0, pos_end, delim_len = delimiter.length(); + std::string token; + std::vector res; + + while ((pos_end = s.find (delimiter, pos_start)) != std::string::npos) { + token = s.substr (pos_start, pos_end - pos_start); + pos_start = pos_end + delim_len; + res.push_back (stoi(token)); + } + res.push_back(stoi(s.substr(pos_start))); + return res; +} + +std::string checkStr(std::string str) { + std::vector v = split (str, "."); + if(v.size() != 4) { + std::cout << "Entered value " << str << " doesn't contain 3 dots. Value has to be in the following format: '255.255.255.255'" << std::endl; + exit(0); + } + for (auto i : v) { + if(i < 0 || 255 < i) { + std::cout << "Entered values can't be above 255!" << std::endl; + exit(0); + } + } + return str; +} + +int main(int argc, char** argv) { + bool found = false; + dai::DeviceInfo info; + std::tie(found, info) = dai::DeviceBootloader::getFirstAvailableDevice(); + if(!found) { + std::cout << "No device found to flash. Exiting." << std::endl; + return -1; + } + + std::cout << "Found device with name: " << info.getMxId() << std::endl; + std::cout << "-------------------------------------" << std::endl; + std::cout << "\"1\" to set a static IPv4 address" << std::endl; + std::cout << "\"2\" to set a dynamic IPv4 address" << std::endl; + std::cout << "\"3\" to clear the config" << std::endl; + auto key = std::cin.get(); + std::cout << "-------------------------------------" << std::endl; + + bool success = false; + std::string error; + dai::DeviceBootloader bl(info, true); + if(key == '1' || key == '2') { + std::string ip, mask, gateway, in; + + std::cout << "Enter IPv4: "; + std::cin >> ip; + checkStr(ip); + + std::cout << "Enter IPv4 Mask: "; + std::cin >> mask; + checkStr(mask); + + std::cout << "Enter IPv4 Gateway: "; + std::cin >> gateway; + checkStr(gateway); + + std::string mode = "static"; + if(key == '2') mode = "dynamic"; + std::cout << "Flashing " << mode << " IPv4 " << ip << ", mask " << mask << ", gateway " << gateway << " to the POE device. Enter 'y' to confirm. "; + std::cin >> in; + if(in != "y") { + std::cout << "Flashing aborted."; + return 0; + } + auto conf = dai::DeviceBootloader::Config(); + if (key == '1') conf.setStaticIPv4(ip, mask, gateway); + else conf.setDynamicIPv4(ip, mask, gateway); + + std::tie(success, error) = bl.flashConfig(conf); + } + else if(key == '3') { + std::tie(success, error) = bl.flashConfigClear(); + } + else { + std::cout << "Entered value should either be '1', '2' or '3'!"; + return 0; + } + + if(success) std::cout << "Flashing successful." << std::endl; + else std::cout << "Flashing failed: " << error << std::endl; +} \ No newline at end of file