From e96b549c4c0e64dfe745201826c45120358dbe0f Mon Sep 17 00:00:00 2001 From: Martin Peterlin Date: Fri, 16 Sep 2022 20:55:25 +0200 Subject: [PATCH] Modified DeviceBootloader::Config to retain previous values --- include/depthai/device/DeviceBootloader.hpp | 9 +++ src/device/DeviceBootloader.cpp | 34 ++++++++--- tests/src/bootloader_config_test.cpp | 52 ++++++++++++++++- tests/src/stability_stress_test.cpp | 65 ++++++++++----------- 4 files changed, 116 insertions(+), 44 deletions(-) diff --git a/include/depthai/device/DeviceBootloader.hpp b/include/depthai/device/DeviceBootloader.hpp index 6f3a2d140..81f142847 100644 --- a/include/depthai/device/DeviceBootloader.hpp +++ b/include/depthai/device/DeviceBootloader.hpp @@ -75,6 +75,15 @@ class DeviceBootloader { void setUsbMaxSpeed(UsbSpeed speed); /// Get maxUsbSpeed UsbSpeed getUsbMaxSpeed(); + + /// To JSON + nlohmann::json toJson() const; + + /// from JSON + static Config fromJson(nlohmann::json); + + private: + nlohmann::json data; }; /// Bootloader version structure diff --git a/src/device/DeviceBootloader.cpp b/src/device/DeviceBootloader.cpp index 8c2d2c2f0..915154f1a 100644 --- a/src/device/DeviceBootloader.cpp +++ b/src/device/DeviceBootloader.cpp @@ -1134,13 +1134,11 @@ std::tuple DeviceBootloader::flashConfigFile(const dai::Path& DeviceBootloader::Config DeviceBootloader::readConfig(Memory memory, Type type) { auto json = readConfigData(memory, type); - // Implicit parse from json to Config - return json; + return Config::fromJson(json); } std::tuple DeviceBootloader::flashConfig(const Config& config, Memory memory, Type type) { - // Implicit parse from Config to json - return flashConfigData(config, memory, type); + return flashConfigData(config.toJson(), memory, type); } // Boot memory @@ -1403,11 +1401,13 @@ UsbSpeed DeviceBootloader::Config::getUsbMaxSpeed() { } void DeviceBootloader::Config::setMacAddress(std::string mac) { - std::array a; - int last = -1; - int rc = std::sscanf(mac.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx%n", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &last); - if(rc != 6 || static_cast(mac.size()) != last) { - throw std::invalid_argument("Invalid MAC address format " + mac); + std::array a = {0, 0, 0, 0, 0, 0}; + if(mac != "") { + int last = -1; + int rc = std::sscanf(mac.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx%n", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &last); + if(rc != 6 || static_cast(mac.size()) != last) { + throw std::invalid_argument("Invalid MAC address format " + mac); + } } // Set the parsed mac address @@ -1429,4 +1429,20 @@ std::string DeviceBootloader::Config::getMacAddress() { return std::string(macStr.data()); } +nlohmann::json DeviceBootloader::Config::toJson() const { + // Get current config & add data (but don't override) + nlohmann::json configValues = *this; + auto dataCopy = data; + dataCopy.update(configValues); + return dataCopy; +} + +DeviceBootloader::Config DeviceBootloader::Config::fromJson(nlohmann::json json) { + // Parse out json (implicitly) and + Config cfg = json; + // save json data as is (to retain unknown values) + cfg.data = json; + return cfg; +} + } // namespace dai diff --git a/tests/src/bootloader_config_test.cpp b/tests/src/bootloader_config_test.cpp index f9707e569..014a82416 100644 --- a/tests/src/bootloader_config_test.cpp +++ b/tests/src/bootloader_config_test.cpp @@ -48,4 +48,54 @@ TEST_CASE("Bootloader Config") { // std::cout << "Orig mac address: " << mac << " len: " << mac.length() << std::endl; // std::cout << "Get mac address: " << config.getMacAddress() << " len: " << config.getMacAddress().length() << std::endl; REQUIRE(config.getMacAddress() == mac); -} \ No newline at end of file +} + +TEST_CASE("Bootloader Config retain") { + auto config = dai::DeviceBootloader::Config::fromJson({{"my_custom_value", "hi"}}); + + // By default IPv4 is 0.0.0.0 (invalid) + REQUIRE("0.0.0.0" == config.getIPv4()); + + std::string ipv4 = "192.168.1.150"; + std::string ipv4Mask = "255.255.255.0"; + std::string ipv4Gateway = "192.168.1.1"; + + config.setStaticIPv4(ipv4, ipv4Mask, ipv4Gateway); + + std::array ipv4InMemory = {192, 168, 1, 150}; + for(size_t i = 0; i < ipv4InMemory.size(); i++) { + REQUIRE(ipv4InMemory[i] == reinterpret_cast(&config.network.ipv4)[i]); + } + + REQUIRE(ipv4 == config.getIPv4()); + REQUIRE(ipv4Mask == config.getIPv4Mask()); + REQUIRE(ipv4Gateway == config.getIPv4Gateway()); + REQUIRE(config.isStaticIPV4() == true); + + std::string dns = "1.1.1.1"; + std::string dnsAlt = "8.8.8.8"; + + config.setDnsIPv4(dns); + + REQUIRE(config.getDnsIPv4() == dns); + REQUIRE(config.network.ipv4DnsAlt == 0); + + config.setDnsIPv4(dns, dnsAlt); + + REQUIRE(config.getDnsIPv4() == dns); + REQUIRE(config.getDnsAltIPv4() == dnsAlt); + + // MAC address + std::string mac = "FF:AA:BB:CC:00:11"; + config.setMacAddress(mac); + // std::cout << "Orig mac address: " << mac << " len: " << mac.length() << std::endl; + // std::cout << "Get mac address: " << config.getMacAddress() << " len: " << config.getMacAddress().length() << std::endl; + REQUIRE(config.getMacAddress() == mac); + + // At the end check that value is retained + REQUIRE(config.toJson()["my_custom_value"].get() == "hi"); + + // Check roundtrip + auto j1 = config.toJson(); + REQUIRE(config.fromJson(j1).toJson() == j1); +} diff --git a/tests/src/stability_stress_test.cpp b/tests/src/stability_stress_test.cpp index 9c5706e22..5d6379000 100644 --- a/tests/src/stability_stress_test.cpp +++ b/tests/src/stability_stress_test.cpp @@ -1,8 +1,8 @@ // std +#include #include #include #include -#include // Includes common necessary includes for development using depthai library #include "depthai/depthai.hpp" @@ -10,7 +10,7 @@ // #define DEPTHAI_STABILITY_TEST_SCRIPT #ifdef DEPTHAI_STABILITY_TEST_DEBUG -#include + #include #endif static const std::vector labelMap = { @@ -42,7 +42,7 @@ int main(int argc, char** argv) { // nnPath = std::string(argv[1]); // } - seconds TEST_TIMEOUT{24*60*60}; + seconds TEST_TIMEOUT{24 * 60 * 60}; if(argc > 1) { TEST_TIMEOUT = seconds{stoi(argv[1])}; } @@ -62,13 +62,13 @@ int main(int argc, char** argv) { auto edgeDetectorLeft = pipeline.create(); auto edgeDetectorRight = pipeline.create(); auto edgeDetectorRgb = pipeline.create(); - #ifdef DEPTHAI_STABILITY_TEST_SCRIPT +#ifdef DEPTHAI_STABILITY_TEST_SCRIPT auto script1 = pipeline.create(); auto script2 = pipeline.create(); auto script3 = pipeline.create(); auto script4 = pipeline.create(); auto scriptBurn = pipeline.create(); - #endif +#endif // TODO(themarpe) - enable specific parts separatelly, to control load // auto featureTrackerLeft = pipeline.create(); @@ -84,10 +84,10 @@ int main(int argc, char** argv) { auto xoutEdgeLeft = pipeline.create(); auto xoutEdgeRight = pipeline.create(); auto xoutEdgeRgb = pipeline.create(); - #ifdef DEPTHAI_STABILITY_TEST_SCRIPT +#ifdef DEPTHAI_STABILITY_TEST_SCRIPT auto scriptOut = pipeline.create(); auto scriptOut2 = pipeline.create(); - #endif +#endif // auto xoutTrackedFeaturesLeft = pipeline.create(); // auto xoutTrackedFeaturesRight = pipeline.create(); @@ -105,14 +105,13 @@ int main(int argc, char** argv) { xoutEdgeLeft->setStreamName(edgeLeftStr); xoutEdgeRight->setStreamName(edgeRightStr); xoutEdgeRgb->setStreamName(edgeRgbStr); - #ifdef DEPTHAI_STABILITY_TEST_SCRIPT +#ifdef DEPTHAI_STABILITY_TEST_SCRIPT scriptOut->setStreamName("script"); scriptOut2->setStreamName("script2"); - #endif +#endif // xoutTrackedFeaturesLeft->setStreamName("trackedFeaturesLeft"); // xoutTrackedFeaturesRight->setStreamName("trackedFeaturesRight"); - // Properties camRgb->setBoardSocket(dai::CameraBoardSocket::RGB); camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_4_K); @@ -152,7 +151,7 @@ int main(int argc, char** argv) { edgeDetectorRgb->setMaxOutputFrameSize(8294400); - #ifdef DEPTHAI_STABILITY_TEST_SCRIPT +#ifdef DEPTHAI_STABILITY_TEST_SCRIPT std::string source1 = R"( import time import json @@ -219,7 +218,7 @@ int main(int argc, char** argv) { time.sleep(0.001) )"); scriptBurn->setProcessor(dai::ProcessorType::LEON_MSS); - #endif +#endif // // By default the least mount of resources are allocated // // increasing it improves performance when optical flow is enabled @@ -254,12 +253,12 @@ int main(int argc, char** argv) { edgeDetectorLeft->outputImage.link(xoutEdgeLeft->input); edgeDetectorRight->outputImage.link(xoutEdgeRight->input); - #ifdef DEPTHAI_STABILITY_TEST_SCRIPT +#ifdef DEPTHAI_STABILITY_TEST_SCRIPT script1->outputs["out"].link(script2->inputs["in"]); script2->outputs["out"].link(scriptOut->input); script3->outputs["out"].link(script4->inputs["in"]); script4->outputs["out"].link(scriptOut2->input); - #endif +#endif // monoLeft->out.link(featureTrackerLeft->inputImage); // featureTrackerLeft->outputFeatures.link(xoutTrackedFeaturesLeft->input); @@ -284,10 +283,10 @@ int main(int argc, char** argv) { auto edgeRightQueue = device.getOutputQueue(edgeRightStr, 8, false); auto edgeRgbQueue = device.getOutputQueue(edgeRgbStr, 8, false); - #ifdef DEPTHAI_STABILITY_TEST_SCRIPT +#ifdef DEPTHAI_STABILITY_TEST_SCRIPT auto scriptQueue = device.getOutputQueue("script", 8, false); auto script2Queue = device.getOutputQueue("script2", 8, false); - #endif +#endif // auto outputFeaturesLeftQueue = device.getOutputQueue("trackedFeaturesLeft", 8, false); // auto outputFeaturesRightQueue = device.getOutputQueue("trackedFeaturesRight", 8, false); @@ -299,11 +298,10 @@ int main(int argc, char** argv) { auto color = cv::Scalar(255, 255, 255); #endif - mutex countersMtx; unordered_map counters; - thread countingThread([&countersMtx, &counters, &device, TEST_TIMEOUT](){ + thread countingThread([&countersMtx, &counters, &device, TEST_TIMEOUT]() { // Initial delay this_thread::sleep_for(5s); @@ -314,9 +312,10 @@ int main(int argc, char** argv) { unique_lock l(countersMtx); bool failed = counters.size() == 0; - cout << "[" << duration_cast(steady_clock::now() - timeoutStopwatch).count() << "s] " << "FPS: "; - for(const auto& kv : counters){ - if(kv.second == 0){ + cout << "[" << duration_cast(steady_clock::now() - timeoutStopwatch).count() << "s] " + << "FPS: "; + for(const auto& kv : counters) { + if(kv.second == 0) { failed = true; } @@ -333,7 +332,7 @@ int main(int argc, char** argv) { fiveFpsCounter = steady_clock::now(); } - if(steady_clock::now() - timeoutStopwatch > TEST_TIMEOUT){ + if(steady_clock::now() - timeoutStopwatch > TEST_TIMEOUT) { alive = false; break; } @@ -357,10 +356,10 @@ int main(int argc, char** argv) { auto edgeLefts = edgeLeftQueue->tryGetAll(); auto edgeRights = edgeRightQueue->tryGetAll(); - #ifdef DEPTHAI_STABILITY_TEST_SCRIPT +#ifdef DEPTHAI_STABILITY_TEST_SCRIPT auto script = scriptQueue->tryGetAll(); auto script2 = script2Queue->tryGetAll(); - #endif +#endif // auto edgeRgbs = edgeRgbQueue->getAll(); @@ -378,15 +377,15 @@ int main(int argc, char** argv) { counters["edgeLefts"] += edgeLefts.size(); counters["edgeRights"] += edgeRights.size(); - #ifdef DEPTHAI_STABILITY_TEST_SCRIPT +#ifdef DEPTHAI_STABILITY_TEST_SCRIPT counters["script"] += script.size(); counters["script2"] += script2.size(); - #endif +#endif // counters["trackedLefts"] += trackedLefts.size(); // counters["trackedRights"] += trackedRigths.size(); } - #ifdef DEPTHAI_STABILITY_TEST_DEBUG +#ifdef DEPTHAI_STABILITY_TEST_DEBUG /// DISPLAY & OPENCV Section for(const auto& edgeLeft : edgeLefts) { @@ -402,7 +401,6 @@ int main(int argc, char** argv) { // cv::imshow(edgeRgbStr, edgeRgbFrame); // } - cv::Mat frame = imgFrame->getCvFrame(); cv::Mat depthFrame = depth->getFrame(); // depthFrame values are in millimeters @@ -475,18 +473,18 @@ int main(int argc, char** argv) { cv::imshow("depth", depthFrameColor); cv::imshow("rgb", frame); - #ifdef DEPTHAI_STABILITY_TEST_SCRIPT + #ifdef DEPTHAI_STABILITY_TEST_SCRIPT for(auto json : script) { if(json != nullptr) { - std::cout << "Script: " << std::string((const char*) json->getData().data(), json->getData().size()) << std::endl; + std::cout << "Script: " << std::string((const char*)json->getData().data(), json->getData().size()) << std::endl; } } for(auto json : script2) { if(json != nullptr) { - std::cout << "Script2: " << std::string((const char*) json->getData().data(), json->getData().size()) << std::endl; + std::cout << "Script2: " << std::string((const char*)json->getData().data(), json->getData().size()) << std::endl; } } - #endif + #endif int key = cv::waitKey(1); if(key == 'q' || key == 'Q') { @@ -494,8 +492,7 @@ int main(int argc, char** argv) { break; } - #endif - +#endif } // Clean up the counting thread