Skip to content

Commit e96b549

Browse files
committed
Modified DeviceBootloader::Config to retain previous values
1 parent a130826 commit e96b549

File tree

4 files changed

+116
-44
lines changed

4 files changed

+116
-44
lines changed

include/depthai/device/DeviceBootloader.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ class DeviceBootloader {
7575
void setUsbMaxSpeed(UsbSpeed speed);
7676
/// Get maxUsbSpeed
7777
UsbSpeed getUsbMaxSpeed();
78+
79+
/// To JSON
80+
nlohmann::json toJson() const;
81+
82+
/// from JSON
83+
static Config fromJson(nlohmann::json);
84+
85+
private:
86+
nlohmann::json data;
7887
};
7988

8089
/// Bootloader version structure

src/device/DeviceBootloader.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,13 +1134,11 @@ std::tuple<bool, std::string> DeviceBootloader::flashConfigFile(const dai::Path&
11341134

11351135
DeviceBootloader::Config DeviceBootloader::readConfig(Memory memory, Type type) {
11361136
auto json = readConfigData(memory, type);
1137-
// Implicit parse from json to Config
1138-
return json;
1137+
return Config::fromJson(json);
11391138
}
11401139

11411140
std::tuple<bool, std::string> DeviceBootloader::flashConfig(const Config& config, Memory memory, Type type) {
1142-
// Implicit parse from Config to json
1143-
return flashConfigData(config, memory, type);
1141+
return flashConfigData(config.toJson(), memory, type);
11441142
}
11451143

11461144
// Boot memory
@@ -1403,11 +1401,13 @@ UsbSpeed DeviceBootloader::Config::getUsbMaxSpeed() {
14031401
}
14041402

14051403
void DeviceBootloader::Config::setMacAddress(std::string mac) {
1406-
std::array<uint8_t, 6> a;
1407-
int last = -1;
1408-
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);
1409-
if(rc != 6 || static_cast<long>(mac.size()) != last) {
1410-
throw std::invalid_argument("Invalid MAC address format " + mac);
1404+
std::array<uint8_t, 6> a = {0, 0, 0, 0, 0, 0};
1405+
if(mac != "") {
1406+
int last = -1;
1407+
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);
1408+
if(rc != 6 || static_cast<long>(mac.size()) != last) {
1409+
throw std::invalid_argument("Invalid MAC address format " + mac);
1410+
}
14111411
}
14121412

14131413
// Set the parsed mac address
@@ -1429,4 +1429,20 @@ std::string DeviceBootloader::Config::getMacAddress() {
14291429
return std::string(macStr.data());
14301430
}
14311431

1432+
nlohmann::json DeviceBootloader::Config::toJson() const {
1433+
// Get current config & add data (but don't override)
1434+
nlohmann::json configValues = *this;
1435+
auto dataCopy = data;
1436+
dataCopy.update(configValues);
1437+
return dataCopy;
1438+
}
1439+
1440+
DeviceBootloader::Config DeviceBootloader::Config::fromJson(nlohmann::json json) {
1441+
// Parse out json (implicitly) and
1442+
Config cfg = json;
1443+
// save json data as is (to retain unknown values)
1444+
cfg.data = json;
1445+
return cfg;
1446+
}
1447+
14321448
} // namespace dai

tests/src/bootloader_config_test.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,54 @@ TEST_CASE("Bootloader Config") {
4848
// std::cout << "Orig mac address: " << mac << " len: " << mac.length() << std::endl;
4949
// std::cout << "Get mac address: " << config.getMacAddress() << " len: " << config.getMacAddress().length() << std::endl;
5050
REQUIRE(config.getMacAddress() == mac);
51-
}
51+
}
52+
53+
TEST_CASE("Bootloader Config retain") {
54+
auto config = dai::DeviceBootloader::Config::fromJson({{"my_custom_value", "hi"}});
55+
56+
// By default IPv4 is 0.0.0.0 (invalid)
57+
REQUIRE("0.0.0.0" == config.getIPv4());
58+
59+
std::string ipv4 = "192.168.1.150";
60+
std::string ipv4Mask = "255.255.255.0";
61+
std::string ipv4Gateway = "192.168.1.1";
62+
63+
config.setStaticIPv4(ipv4, ipv4Mask, ipv4Gateway);
64+
65+
std::array<uint8_t, 4> ipv4InMemory = {192, 168, 1, 150};
66+
for(size_t i = 0; i < ipv4InMemory.size(); i++) {
67+
REQUIRE(ipv4InMemory[i] == reinterpret_cast<uint8_t*>(&config.network.ipv4)[i]);
68+
}
69+
70+
REQUIRE(ipv4 == config.getIPv4());
71+
REQUIRE(ipv4Mask == config.getIPv4Mask());
72+
REQUIRE(ipv4Gateway == config.getIPv4Gateway());
73+
REQUIRE(config.isStaticIPV4() == true);
74+
75+
std::string dns = "1.1.1.1";
76+
std::string dnsAlt = "8.8.8.8";
77+
78+
config.setDnsIPv4(dns);
79+
80+
REQUIRE(config.getDnsIPv4() == dns);
81+
REQUIRE(config.network.ipv4DnsAlt == 0);
82+
83+
config.setDnsIPv4(dns, dnsAlt);
84+
85+
REQUIRE(config.getDnsIPv4() == dns);
86+
REQUIRE(config.getDnsAltIPv4() == dnsAlt);
87+
88+
// MAC address
89+
std::string mac = "FF:AA:BB:CC:00:11";
90+
config.setMacAddress(mac);
91+
// std::cout << "Orig mac address: " << mac << " len: " << mac.length() << std::endl;
92+
// std::cout << "Get mac address: " << config.getMacAddress() << " len: " << config.getMacAddress().length() << std::endl;
93+
REQUIRE(config.getMacAddress() == mac);
94+
95+
// At the end check that value is retained
96+
REQUIRE(config.toJson()["my_custom_value"].get<std::string>() == "hi");
97+
98+
// Check roundtrip
99+
auto j1 = config.toJson();
100+
REQUIRE(config.fromJson(j1).toJson() == j1);
101+
}

tests/src/stability_stress_test.cpp

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// std
2+
#include <chrono>
23
#include <csignal>
34
#include <iostream>
45
#include <unordered_map>
5-
#include <chrono>
66

77
// Includes common necessary includes for development using depthai library
88
#include "depthai/depthai.hpp"
99

1010
// #define DEPTHAI_STABILITY_TEST_SCRIPT
1111

1212
#ifdef DEPTHAI_STABILITY_TEST_DEBUG
13-
#include <opencv2/opencv.hpp>
13+
#include <opencv2/opencv.hpp>
1414
#endif
1515

1616
static const std::vector<std::string> labelMap = {
@@ -42,7 +42,7 @@ int main(int argc, char** argv) {
4242
// nnPath = std::string(argv[1]);
4343
// }
4444

45-
seconds TEST_TIMEOUT{24*60*60};
45+
seconds TEST_TIMEOUT{24 * 60 * 60};
4646
if(argc > 1) {
4747
TEST_TIMEOUT = seconds{stoi(argv[1])};
4848
}
@@ -62,13 +62,13 @@ int main(int argc, char** argv) {
6262
auto edgeDetectorLeft = pipeline.create<dai::node::EdgeDetector>();
6363
auto edgeDetectorRight = pipeline.create<dai::node::EdgeDetector>();
6464
auto edgeDetectorRgb = pipeline.create<dai::node::EdgeDetector>();
65-
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
65+
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
6666
auto script1 = pipeline.create<dai::node::Script>();
6767
auto script2 = pipeline.create<dai::node::Script>();
6868
auto script3 = pipeline.create<dai::node::Script>();
6969
auto script4 = pipeline.create<dai::node::Script>();
7070
auto scriptBurn = pipeline.create<dai::node::Script>();
71-
#endif
71+
#endif
7272

7373
// TODO(themarpe) - enable specific parts separatelly, to control load
7474
// auto featureTrackerLeft = pipeline.create<dai::node::FeatureTracker>();
@@ -84,10 +84,10 @@ int main(int argc, char** argv) {
8484
auto xoutEdgeLeft = pipeline.create<dai::node::XLinkOut>();
8585
auto xoutEdgeRight = pipeline.create<dai::node::XLinkOut>();
8686
auto xoutEdgeRgb = pipeline.create<dai::node::XLinkOut>();
87-
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
87+
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
8888
auto scriptOut = pipeline.create<dai::node::XLinkOut>();
8989
auto scriptOut2 = pipeline.create<dai::node::XLinkOut>();
90-
#endif
90+
#endif
9191
// auto xoutTrackedFeaturesLeft = pipeline.create<dai::node::XLinkOut>();
9292
// auto xoutTrackedFeaturesRight = pipeline.create<dai::node::XLinkOut>();
9393

@@ -105,14 +105,13 @@ int main(int argc, char** argv) {
105105
xoutEdgeLeft->setStreamName(edgeLeftStr);
106106
xoutEdgeRight->setStreamName(edgeRightStr);
107107
xoutEdgeRgb->setStreamName(edgeRgbStr);
108-
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
108+
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
109109
scriptOut->setStreamName("script");
110110
scriptOut2->setStreamName("script2");
111-
#endif
111+
#endif
112112
// xoutTrackedFeaturesLeft->setStreamName("trackedFeaturesLeft");
113113
// xoutTrackedFeaturesRight->setStreamName("trackedFeaturesRight");
114114

115-
116115
// Properties
117116
camRgb->setBoardSocket(dai::CameraBoardSocket::RGB);
118117
camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_4_K);
@@ -152,7 +151,7 @@ int main(int argc, char** argv) {
152151

153152
edgeDetectorRgb->setMaxOutputFrameSize(8294400);
154153

155-
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
154+
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
156155
std::string source1 = R"(
157156
import time
158157
import json
@@ -219,7 +218,7 @@ int main(int argc, char** argv) {
219218
time.sleep(0.001)
220219
)");
221220
scriptBurn->setProcessor(dai::ProcessorType::LEON_MSS);
222-
#endif
221+
#endif
223222

224223
// // By default the least mount of resources are allocated
225224
// // increasing it improves performance when optical flow is enabled
@@ -254,12 +253,12 @@ int main(int argc, char** argv) {
254253
edgeDetectorLeft->outputImage.link(xoutEdgeLeft->input);
255254
edgeDetectorRight->outputImage.link(xoutEdgeRight->input);
256255

257-
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
256+
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
258257
script1->outputs["out"].link(script2->inputs["in"]);
259258
script2->outputs["out"].link(scriptOut->input);
260259
script3->outputs["out"].link(script4->inputs["in"]);
261260
script4->outputs["out"].link(scriptOut2->input);
262-
#endif
261+
#endif
263262

264263
// monoLeft->out.link(featureTrackerLeft->inputImage);
265264
// featureTrackerLeft->outputFeatures.link(xoutTrackedFeaturesLeft->input);
@@ -284,10 +283,10 @@ int main(int argc, char** argv) {
284283
auto edgeRightQueue = device.getOutputQueue(edgeRightStr, 8, false);
285284
auto edgeRgbQueue = device.getOutputQueue(edgeRgbStr, 8, false);
286285

287-
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
286+
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
288287
auto scriptQueue = device.getOutputQueue("script", 8, false);
289288
auto script2Queue = device.getOutputQueue("script2", 8, false);
290-
#endif
289+
#endif
291290

292291
// auto outputFeaturesLeftQueue = device.getOutputQueue("trackedFeaturesLeft", 8, false);
293292
// auto outputFeaturesRightQueue = device.getOutputQueue("trackedFeaturesRight", 8, false);
@@ -299,11 +298,10 @@ int main(int argc, char** argv) {
299298
auto color = cv::Scalar(255, 255, 255);
300299
#endif
301300

302-
303301
mutex countersMtx;
304302
unordered_map<std::string, int> counters;
305303

306-
thread countingThread([&countersMtx, &counters, &device, TEST_TIMEOUT](){
304+
thread countingThread([&countersMtx, &counters, &device, TEST_TIMEOUT]() {
307305
// Initial delay
308306
this_thread::sleep_for(5s);
309307

@@ -314,9 +312,10 @@ int main(int argc, char** argv) {
314312
unique_lock<mutex> l(countersMtx);
315313

316314
bool failed = counters.size() == 0;
317-
cout << "[" << duration_cast<seconds>(steady_clock::now() - timeoutStopwatch).count() << "s] " << "FPS: ";
318-
for(const auto& kv : counters){
319-
if(kv.second == 0){
315+
cout << "[" << duration_cast<seconds>(steady_clock::now() - timeoutStopwatch).count() << "s] "
316+
<< "FPS: ";
317+
for(const auto& kv : counters) {
318+
if(kv.second == 0) {
320319
failed = true;
321320
}
322321

@@ -333,7 +332,7 @@ int main(int argc, char** argv) {
333332
fiveFpsCounter = steady_clock::now();
334333
}
335334

336-
if(steady_clock::now() - timeoutStopwatch > TEST_TIMEOUT){
335+
if(steady_clock::now() - timeoutStopwatch > TEST_TIMEOUT) {
337336
alive = false;
338337
break;
339338
}
@@ -357,10 +356,10 @@ int main(int argc, char** argv) {
357356
auto edgeLefts = edgeLeftQueue->tryGetAll<dai::ImgFrame>();
358357
auto edgeRights = edgeRightQueue->tryGetAll<dai::ImgFrame>();
359358

360-
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
359+
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
361360
auto script = scriptQueue->tryGetAll<dai::Buffer>();
362361
auto script2 = script2Queue->tryGetAll<dai::Buffer>();
363-
#endif
362+
#endif
364363

365364
// auto edgeRgbs = edgeRgbQueue->getAll<dai::ImgFrame>();
366365

@@ -378,15 +377,15 @@ int main(int argc, char** argv) {
378377
counters["edgeLefts"] += edgeLefts.size();
379378
counters["edgeRights"] += edgeRights.size();
380379

381-
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
380+
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
382381
counters["script"] += script.size();
383382
counters["script2"] += script2.size();
384-
#endif
383+
#endif
385384
// counters["trackedLefts"] += trackedLefts.size();
386385
// counters["trackedRights"] += trackedRigths.size();
387386
}
388387

389-
#ifdef DEPTHAI_STABILITY_TEST_DEBUG
388+
#ifdef DEPTHAI_STABILITY_TEST_DEBUG
390389

391390
/// DISPLAY & OPENCV Section
392391
for(const auto& edgeLeft : edgeLefts) {
@@ -402,7 +401,6 @@ int main(int argc, char** argv) {
402401
// cv::imshow(edgeRgbStr, edgeRgbFrame);
403402
// }
404403

405-
406404
cv::Mat frame = imgFrame->getCvFrame();
407405
cv::Mat depthFrame = depth->getFrame(); // depthFrame values are in millimeters
408406

@@ -475,27 +473,26 @@ int main(int argc, char** argv) {
475473
cv::imshow("depth", depthFrameColor);
476474
cv::imshow("rgb", frame);
477475

478-
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
476+
#ifdef DEPTHAI_STABILITY_TEST_SCRIPT
479477
for(auto json : script) {
480478
if(json != nullptr) {
481-
std::cout << "Script: " << std::string((const char*) json->getData().data(), json->getData().size()) << std::endl;
479+
std::cout << "Script: " << std::string((const char*)json->getData().data(), json->getData().size()) << std::endl;
482480
}
483481
}
484482
for(auto json : script2) {
485483
if(json != nullptr) {
486-
std::cout << "Script2: " << std::string((const char*) json->getData().data(), json->getData().size()) << std::endl;
484+
std::cout << "Script2: " << std::string((const char*)json->getData().data(), json->getData().size()) << std::endl;
487485
}
488486
}
489-
#endif
487+
#endif
490488

491489
int key = cv::waitKey(1);
492490
if(key == 'q' || key == 'Q') {
493491
alive = false;
494492
break;
495493
}
496494

497-
#endif
498-
495+
#endif
499496
}
500497

501498
// Clean up the counting thread

0 commit comments

Comments
 (0)