From 2cd0570cfba2b5bdd9dbb390841873cba3ec61e0 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Tue, 1 Jul 2025 18:09:27 +0200 Subject: [PATCH 01/35] Changed XLink commit. --- cmake/depthaiDependencies.cmake | 2 +- shared/depthai-shared | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 160000 shared/depthai-shared diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index 4aa7551de7..dc67109443 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG 87785828fabdb1718760bb0a044405d5bbfbb3a2 + GIT_TAG 1958f31ab14dff84b0edba35db524ebecd00b1f8 ) FetchContent_MakeAvailable( diff --git a/shared/depthai-shared b/shared/depthai-shared new file mode 160000 index 0000000000..9762f334f3 --- /dev/null +++ b/shared/depthai-shared @@ -0,0 +1 @@ +Subproject commit 9762f334f39fc841a5bbea9b1114bbac933710e0 From e470ea6c6af3b6847cefbcb1d58e46da9f6f173a Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Tue, 1 Jul 2025 18:14:43 +0200 Subject: [PATCH 02/35] Added new protocol handling in log collection --- src/utility/LogCollection.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utility/LogCollection.cpp b/src/utility/LogCollection.cpp index 90f319fac1..92f01e8347 100644 --- a/src/utility/LogCollection.cpp +++ b/src/utility/LogCollection.cpp @@ -59,6 +59,8 @@ std::string protocolToString(XLinkProtocol_t protocol) { return "X_LINK_LOCAL_SHDMEM"; case X_LINK_TCP_IP_OR_LOCAL_SHDMEM: return "X_LINK_TCP_IP_OR_LOCAL_SHDMEM"; + case X_LINK_USB_EP: + return "X_LINK_USB_EP"; case X_LINK_NMB_OF_PROTOCOLS: return "X_LINK_NMB_OF_PROTOCOLS"; case X_LINK_ANY_PROTOCOL: From 7a8238946b5a5dec074ade68b4752c741d2ce8f9 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Thu, 10 Jul 2025 12:48:59 +0200 Subject: [PATCH 03/35] Updated XLink to support USB EP discovery --- cmake/depthaiDependencies.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index dc67109443..31970b7510 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG 1958f31ab14dff84b0edba35db524ebecd00b1f8 + GIT_TAG 1f1ade046e25424958937a23cedecb165fe0fafa ) FetchContent_MakeAvailable( From 74f5a3a5b6fe63daab0e78361ef30955d9a043ab Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Thu, 10 Jul 2025 15:58:01 +0200 Subject: [PATCH 04/35] Updated XLink --- cmake/depthaiDependencies.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index 31970b7510..03b4c73c3d 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG 1f1ade046e25424958937a23cedecb165fe0fafa + GIT_TAG 0e2e7d11f0f128c1363b1ac208fb09a363afb9d8 ) FetchContent_MakeAvailable( From ef65dd0779d01f459708dd368ddb6318aaba4960 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Thu, 10 Jul 2025 16:12:04 +0200 Subject: [PATCH 05/35] Updated xlink --- cmake/depthaiDependencies.cmake | 2 +- examples/cpp/Camera/camera_still_max_res.cpp | 42 ++++++++++---------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index 03b4c73c3d..16c423f467 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG 0e2e7d11f0f128c1363b1ac208fb09a363afb9d8 + GIT_TAG 8423b90465d9f91d8c3fd108bb8c18a7b27d8a3e ) FetchContent_MakeAvailable( diff --git a/examples/cpp/Camera/camera_still_max_res.cpp b/examples/cpp/Camera/camera_still_max_res.cpp index 250e919a09..ea6cf078b1 100644 --- a/examples/cpp/Camera/camera_still_max_res.cpp +++ b/examples/cpp/Camera/camera_still_max_res.cpp @@ -51,26 +51,28 @@ int main() { if(imgHd == nullptr) continue; cv::Mat frame = imgHd->getCvFrame(); - cv::imshow("video", frame); - - int key = cv::waitKey(1); - if(key == 'q') { - break; - } - if(key == 'c') { - // Send a trigger message to the Script node - qTrigger->send(std::make_shared()); - } - - if(highestResQ->has()) { - auto highresImg = highestResQ->get(); - if(highresImg != nullptr) { - cv::Mat frame = highresImg->getCvFrame(); - // Save the full image - cv::imwrite("full_image.png", frame); - } - } + + std::cout << "Got frame" << std::endl; +// cv::imshow("video", frame); + +// int key = cv::waitKey(1); +// if(key == 'q') { +// break; +// } +// if(key == 'c') { +// // Send a trigger message to the Script node +// qTrigger->send(std::make_shared()); +// } + +// if(highestResQ->has()) { +// auto highresImg = highestResQ->get(); +// if(highresImg != nullptr) { +// cv::Mat frame = highresImg->getCvFrame(); +// // Save the full image +// cv::imwrite("full_image.png", frame); +// } +// } } return 0; -} \ No newline at end of file +} From c71236a0de99727661d9cf7bf6f89c9b43840848 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Fri, 11 Jul 2025 09:08:32 +0200 Subject: [PATCH 06/35] Reset example --- examples/cpp/Camera/camera_still_max_res.cpp | 42 ++++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/examples/cpp/Camera/camera_still_max_res.cpp b/examples/cpp/Camera/camera_still_max_res.cpp index ea6cf078b1..250e919a09 100644 --- a/examples/cpp/Camera/camera_still_max_res.cpp +++ b/examples/cpp/Camera/camera_still_max_res.cpp @@ -51,28 +51,26 @@ int main() { if(imgHd == nullptr) continue; cv::Mat frame = imgHd->getCvFrame(); - - std::cout << "Got frame" << std::endl; -// cv::imshow("video", frame); - -// int key = cv::waitKey(1); -// if(key == 'q') { -// break; -// } -// if(key == 'c') { -// // Send a trigger message to the Script node -// qTrigger->send(std::make_shared()); -// } - -// if(highestResQ->has()) { -// auto highresImg = highestResQ->get(); -// if(highresImg != nullptr) { -// cv::Mat frame = highresImg->getCvFrame(); -// // Save the full image -// cv::imwrite("full_image.png", frame); -// } -// } + cv::imshow("video", frame); + + int key = cv::waitKey(1); + if(key == 'q') { + break; + } + if(key == 'c') { + // Send a trigger message to the Script node + qTrigger->send(std::make_shared()); + } + + if(highestResQ->has()) { + auto highresImg = highestResQ->get(); + if(highresImg != nullptr) { + cv::Mat frame = highresImg->getCvFrame(); + // Save the full image + cv::imwrite("full_image.png", frame); + } + } } return 0; -} +} \ No newline at end of file From 461e015af57618d23287091781a59716509921d2 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Fri, 11 Jul 2025 10:06:58 +0200 Subject: [PATCH 07/35] Updated XLink --- cmake/depthaiDependencies.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index 16c423f467..06749a67e3 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG 8423b90465d9f91d8c3fd108bb8c18a7b27d8a3e + GIT_TAG fdc7ca6699431bab598e366057274c9e215967c0 ) FetchContent_MakeAvailable( From b539e7b08e4bb4cae49793b2c229f6b1e2e875d0 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 16 Jul 2025 11:56:47 +0200 Subject: [PATCH 08/35] Updated xlink and added test for Gate Write --- cmake/depthaiDependencies.cmake | 2 +- examples/cpp/Camera/camera_max_resolution.cpp | 12 +++++++----- examples/cpp/Camera/camera_output.cpp | 11 ++++++----- include/depthai/device/DeviceGate.hpp | 3 +++ include/depthai/xlink/XLinkStream.hpp | 2 ++ src/device/DeviceGate.cpp | 17 ++++++++++++++++- src/xlink/XLinkStream.cpp | 17 +++++++++++++++++ 7 files changed, 52 insertions(+), 12 deletions(-) diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index 06749a67e3..85d2ddb09c 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG fdc7ca6699431bab598e366057274c9e215967c0 + GIT_TAG e519b8698d81722b05b389551e1038006198ef77 ) FetchContent_MakeAvailable( diff --git a/examples/cpp/Camera/camera_max_resolution.cpp b/examples/cpp/Camera/camera_max_resolution.cpp index 5be761ebbf..a4879ecbe2 100644 --- a/examples/cpp/Camera/camera_max_resolution.cpp +++ b/examples/cpp/Camera/camera_max_resolution.cpp @@ -23,12 +23,14 @@ int main() { auto videoIn = videoQueue->get(); if(videoIn == nullptr) continue; - cv::imshow("video", videoIn->getCvFrame()); + videoIn->getCvFrame(); +std::cout << "Got frame" << std::endl; + //cv::imshow("video", videoIn->getCvFrame()); - if(cv::waitKey(1) == 'q') { - break; - } + //if(cv::waitKey(1) == 'q') { + // break; + //} } return 0; -} \ No newline at end of file +} diff --git a/examples/cpp/Camera/camera_output.cpp b/examples/cpp/Camera/camera_output.cpp index cdaeac32d9..2101c45c51 100644 --- a/examples/cpp/Camera/camera_output.cpp +++ b/examples/cpp/Camera/camera_output.cpp @@ -13,7 +13,8 @@ int main() { // Create nodes auto cam = pipeline.create()->build(); - auto videoQueue = cam->requestOutput(std::make_pair(640, 400))->createOutputQueue(); + auto videoQueue = cam->requestOutput(std::make_pair(4096, 2160))->createOutputQueue(); + //auto videoQueue = cam->requestOutput(std::make_pair(2048, 1556))->createOutputQueue(); // Start pipeline pipeline.start(); @@ -21,13 +22,13 @@ int main() { while(true) { auto videoIn = videoQueue->get(); if(videoIn == nullptr) continue; - - cv::imshow("video", videoIn->getCvFrame()); + videoIn->getCvFrame(); +/* cv::imshow("video", videoIn->getCvFrame()); if(cv::waitKey(1) == 'q') { break; - } + }*/ } return 0; -} \ No newline at end of file +} diff --git a/include/depthai/device/DeviceGate.hpp b/include/depthai/device/DeviceGate.hpp index 7e75c97b04..6a2257faf4 100644 --- a/include/depthai/device/DeviceGate.hpp +++ b/include/depthai/device/DeviceGate.hpp @@ -14,6 +14,7 @@ #include "depthai/device/Version.hpp" #include "depthai/utility/Pimpl.hpp" #include "depthai/xlink/XLinkConnection.hpp" +#include "depthai/xlink/XLinkStream.hpp" namespace dai { /** @@ -65,6 +66,8 @@ class DeviceGate { std::atomic_bool sessionCreated{false}; XLinkPlatform_t platform; + std::shared_ptr gateConnection; + std::shared_ptr gateStream; std::string version; // pimpl class Impl; diff --git a/include/depthai/xlink/XLinkStream.hpp b/include/depthai/xlink/XLinkStream.hpp index 95be5e004e..7658118055 100644 --- a/include/depthai/xlink/XLinkStream.hpp +++ b/include/depthai/xlink/XLinkStream.hpp @@ -92,12 +92,14 @@ class XLinkStream { void write(long fd); void write(long fd, span data); void write(const void* data, std::size_t size); + void writeGate(span data); std::vector read(); std::vector read(XLinkTimespec& timestampReceived); void read(std::vector& data); void read(std::vector& data, long& fd); void read(std::vector& data, XLinkTimespec& timestampReceived); void read(std::vector& data, long& fd, XLinkTimespec& timestampReceived); + void readGate(std::vector& data); // split write helper void writeSplit(const void* data, std::size_t size, std::size_t split); void writeSplit(const std::vector& data, std::size_t split); diff --git a/src/device/DeviceGate.cpp b/src/device/DeviceGate.cpp index 7698736e52..944549b409 100644 --- a/src/device/DeviceGate.cpp +++ b/src/device/DeviceGate.cpp @@ -44,7 +44,7 @@ DeviceGate::DeviceGate(const DeviceInfo& deviceInfo) : deviceInfo(deviceInfo) { if(deviceInfo.platform != X_LINK_RVC3 && deviceInfo.platform != X_LINK_RVC4) { throw std::invalid_argument("Gate only supports RVC3 and RVC4 platforms"); } - this->platform = deviceInfo.platform; + this->platform =deviceInfo.platform; if(deviceInfo.platform == X_LINK_RVC3) { version = DEPTHAI_DEVICE_RVC3_VERSION; } else if(deviceInfo.platform == X_LINK_RVC4) { @@ -56,12 +56,27 @@ DeviceGate::DeviceGate(const DeviceInfo& deviceInfo) : deviceInfo(deviceInfo) { pimpl->cli = std::make_unique(deviceInfo.name, DEFAULT_PORT); pimpl->cli->set_read_timeout(60); // 60 seconds timeout to allow for compressing the crash dumps without async // pimpl->cli->set_connection_timeout(2); + + gateConnection = std::make_shared(deviceInfo, X_LINK_GATE); + gateStream = std::make_shared(gateConnection, "XLink Gate Stream", 32 * 1024 * 1024); + } bool DeviceGate::isOkay() { if(auto res = pimpl->cli->Get("/api/v1/status")) { return nlohmann::json::parse(res->body)["status"].get(); } + + struct request_t { + uint16_t RequestNum; + }__attribute__((packed)); + request_t request; + request.RequestNum = 1; + + span data; + memcpy((char*)data.data(), &request, sizeof(request)); + gateStream->writeGate(data); + return false; } diff --git a/src/xlink/XLinkStream.cpp b/src/xlink/XLinkStream.cpp index 13a0321c7e..44d95c425e 100644 --- a/src/xlink/XLinkStream.cpp +++ b/src/xlink/XLinkStream.cpp @@ -113,6 +113,14 @@ void XLinkStream::write(long fd, span data) { } } +void XLinkStream::writeGate(span data) { + auto status = XLinkGateWriteData(streamId, data.data(), static_cast(data.size())); + if(status != X_LINK_SUCCESS) { + throw XLinkWriteError(status, streamName); + } +} + + void XLinkStream::read(std::vector& data) { XLinkTimespec timestampReceived; read(data, timestampReceived); @@ -158,6 +166,15 @@ std::vector XLinkStream::read(XLinkTimespec& timestampReceived) { return data; } +void XLinkStream::readGate(std::vector& data) { + StreamPacketDesc packet; + const auto status = XLinkReadMoveData(streamId, &packet); + if(status != X_LINK_SUCCESS) { + throw XLinkReadError(status, streamName); + } + data = std::vector(packet.data, packet.data + packet.length); +} + StreamPacketDesc XLinkStream::readMove() { StreamPacketDesc packet; const auto status = XLinkReadMoveData(streamId, &packet); From 8a03d047c79a8ab470333a768b5002e86d6fadbf Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Wed, 16 Jul 2025 17:19:20 +0200 Subject: [PATCH 09/35] Added debug message --- cmake/depthaiDependencies.cmake | 2 +- less | 1 + src/device/DeviceGate.cpp | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 less diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index 85d2ddb09c..6b2b93cc01 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG e519b8698d81722b05b389551e1038006198ef77 + GIT_TAG 4126554f5854b8215ef5833af9ca85fd39816d0c ) FetchContent_MakeAvailable( diff --git a/less b/less new file mode 100644 index 0000000000..19a589a9a6 --- /dev/null +++ b/less @@ -0,0 +1 @@ +gmake: *** [Makefile:6195: cmake_check_build_system] Interrupt diff --git a/src/device/DeviceGate.cpp b/src/device/DeviceGate.cpp index 944549b409..786e0efdef 100644 --- a/src/device/DeviceGate.cpp +++ b/src/device/DeviceGate.cpp @@ -59,6 +59,7 @@ DeviceGate::DeviceGate(const DeviceInfo& deviceInfo) : deviceInfo(deviceInfo) { gateConnection = std::make_shared(deviceInfo, X_LINK_GATE); gateStream = std::make_shared(gateConnection, "XLink Gate Stream", 32 * 1024 * 1024); + spdlog::debug("Connected to gate stream"); } From 6784c4f04415cd3a31a74bd9906d0471e2f66c0d Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Thu, 17 Jul 2025 09:13:49 +0200 Subject: [PATCH 10/35] Testing request via USB. --- cmake/depthaiDependencies.cmake | 2 +- include/depthai/xlink/XLinkStream.hpp | 2 -- src/device/DeviceGate.cpp | 28 +++++++++++++-------------- src/xlink/XLinkStream.cpp | 17 ---------------- 4 files changed, 15 insertions(+), 34 deletions(-) diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index 6b2b93cc01..c4fccc0e77 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG 4126554f5854b8215ef5833af9ca85fd39816d0c + GIT_TAG d46c049e30d2a56f7bbec2e25e7e2fa5c09ec6d0 ) FetchContent_MakeAvailable( diff --git a/include/depthai/xlink/XLinkStream.hpp b/include/depthai/xlink/XLinkStream.hpp index 7658118055..95be5e004e 100644 --- a/include/depthai/xlink/XLinkStream.hpp +++ b/include/depthai/xlink/XLinkStream.hpp @@ -92,14 +92,12 @@ class XLinkStream { void write(long fd); void write(long fd, span data); void write(const void* data, std::size_t size); - void writeGate(span data); std::vector read(); std::vector read(XLinkTimespec& timestampReceived); void read(std::vector& data); void read(std::vector& data, long& fd); void read(std::vector& data, XLinkTimespec& timestampReceived); void read(std::vector& data, long& fd, XLinkTimespec& timestampReceived); - void readGate(std::vector& data); // split write helper void writeSplit(const void* data, std::size_t size, std::size_t split); void writeSplit(const std::vector& data, std::size_t split); diff --git a/src/device/DeviceGate.cpp b/src/device/DeviceGate.cpp index 786e0efdef..20ab069092 100644 --- a/src/device/DeviceGate.cpp +++ b/src/device/DeviceGate.cpp @@ -1,6 +1,7 @@ #include "device/DeviceGate.hpp" #include +#include // std #include @@ -56,10 +57,6 @@ DeviceGate::DeviceGate(const DeviceInfo& deviceInfo) : deviceInfo(deviceInfo) { pimpl->cli = std::make_unique(deviceInfo.name, DEFAULT_PORT); pimpl->cli->set_read_timeout(60); // 60 seconds timeout to allow for compressing the crash dumps without async // pimpl->cli->set_connection_timeout(2); - - gateConnection = std::make_shared(deviceInfo, X_LINK_GATE); - gateStream = std::make_shared(gateConnection, "XLink Gate Stream", 32 * 1024 * 1024); - spdlog::debug("Connected to gate stream"); } @@ -68,16 +65,6 @@ bool DeviceGate::isOkay() { return nlohmann::json::parse(res->body)["status"].get(); } - struct request_t { - uint16_t RequestNum; - }__attribute__((packed)); - request_t request; - request.RequestNum = 1; - - span data; - memcpy((char*)data.data(), &request, sizeof(request)); - gateStream->writeGate(data); - return false; } @@ -166,6 +153,19 @@ bool DeviceGate::createSession(bool exclusive) { } bool DeviceGate::startSession() { + struct request_t { + uint16_t RequestNum; + uint32_t RequestSize; + }__attribute__((packed)); + request_t request; + request.RequestNum = 1; + request.RequestSize = 0; + + XLinkPlatformGateWrite(&request, sizeof(request)); + XLinkPlatformGateRead(&request, sizeof(request)); + int requestNum = request.RequestNum; + spdlog::warn("Request num: {}", requestNum); + std::string url = fmt::format("{}/{}/start", sessionsEndpoint, sessionId); if(auto res = pimpl->cli->Post(url.c_str())) { if(res->status != 200) { diff --git a/src/xlink/XLinkStream.cpp b/src/xlink/XLinkStream.cpp index 44d95c425e..13a0321c7e 100644 --- a/src/xlink/XLinkStream.cpp +++ b/src/xlink/XLinkStream.cpp @@ -113,14 +113,6 @@ void XLinkStream::write(long fd, span data) { } } -void XLinkStream::writeGate(span data) { - auto status = XLinkGateWriteData(streamId, data.data(), static_cast(data.size())); - if(status != X_LINK_SUCCESS) { - throw XLinkWriteError(status, streamName); - } -} - - void XLinkStream::read(std::vector& data) { XLinkTimespec timestampReceived; read(data, timestampReceived); @@ -166,15 +158,6 @@ std::vector XLinkStream::read(XLinkTimespec& timestampReceived) { return data; } -void XLinkStream::readGate(std::vector& data) { - StreamPacketDesc packet; - const auto status = XLinkReadMoveData(streamId, &packet); - if(status != X_LINK_SUCCESS) { - throw XLinkReadError(status, streamName); - } - data = std::vector(packet.data, packet.data + packet.length); -} - StreamPacketDesc XLinkStream::readMove() { StreamPacketDesc packet; const auto status = XLinkReadMoveData(streamId, &packet); From 1f362b0760b7614ce56e71ed5711009882c6b77c Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Fri, 18 Jul 2025 09:19:40 +0200 Subject: [PATCH 11/35] Finalised GATE via USB. --- src/device/DeviceGate.cpp | 253 ++++++++++++++++++++++++++++++++++---- 1 file changed, 232 insertions(+), 21 deletions(-) diff --git a/src/device/DeviceGate.cpp b/src/device/DeviceGate.cpp index 20ab069092..098c37c202 100644 --- a/src/device/DeviceGate.cpp +++ b/src/device/DeviceGate.cpp @@ -28,6 +28,27 @@ const std::string API_ROOT{"/api/v1"}; const auto sessionsEndpoint = API_ROOT + "/sessions"; const int DEFAULT_PORT{11492}; +enum USBRequestID_t { + RESPONSE_OK, + RESPONSE_ERROR, + CREATE_SESSION, + START_SESSION, + UPLOAD_FWP, + STOP_SESSION, + DESTROY_SESSION, + DELETE_SESSION, + IS_OKAY, + GET_VERSION, + GET_STATE, + GET_FILE, +}; + +struct USBRequest_t { + uint16_t RequestNum; + uint32_t RequestSize; +}__attribute__((packed)); + + class DeviceGate::Impl { public: Impl() = default; @@ -63,6 +84,24 @@ DeviceGate::DeviceGate(const DeviceInfo& deviceInfo) : deviceInfo(deviceInfo) { bool DeviceGate::isOkay() { if(auto res = pimpl->cli->Get("/api/v1/status")) { return nlohmann::json::parse(res->body)["status"].get(); + } else { + USBRequest_t request; + request.RequestNum = IS_OKAY; + request.RequestSize = 0; + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + return false; + } + + char *respBuffer = new char[request.RequestSize]; + XLinkPlatformGateRead(respBuffer, request.RequestSize); + + bool result = nlohmann::json::parse(respBuffer)["status"].get(); + delete[] respBuffer; + + return result; } return false; @@ -73,6 +112,25 @@ Version DeviceGate::getVersion() { if(res && res->status == 200) { auto versionStr = nlohmann::json::parse(res->body)["version_gate"].get(); return Version{versionStr}; + } else { + USBRequest_t request; + request.RequestNum = GET_VERSION; + request.RequestSize = 0; + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + return Version{0, 0, 0}; + } + + char *respBuffer = new char[request.RequestSize]; + XLinkPlatformGateRead(respBuffer, request.RequestSize); + + auto result = nlohmann::json::parse(respBuffer)["version_gate"].get(); + delete[] respBuffer; + + return Version{result}; + } return Version{0, 0, 0}; } @@ -86,6 +144,27 @@ DeviceGate::VersionInfo DeviceGate::getAllVersion() { info.gate = result.value("version_gate", ""); info.os = result.value("version_os", ""); return info; + } else { + USBRequest_t request; + request.RequestNum = GET_VERSION; + request.RequestSize = 0; + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + return {}; + } + + char *respBuffer = new char[request.RequestSize]; + XLinkPlatformGateRead(respBuffer, request.RequestSize); + auto result = nlohmann::json::parse(respBuffer); + delete[] respBuffer; + + VersionInfo info; + info.gate = result.value("version_gate", ""); + info.os = result.value("version_os", ""); + return info; + } return {}; } @@ -147,25 +226,54 @@ bool DeviceGate::createSession(bool exclusive) { sessionCreated = true; return true; } else { - spdlog::warn("DeviceGate createSession not successful - got no response"); + USBRequest_t request; + request.RequestNum = CREATE_SESSION; + request.RequestSize = createSessionBody.dump().size(); + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkPlatformGateWrite((void*)createSessionBody.dump().c_str(), createSessionBody.dump().size()); + + XLinkPlatformGateRead(&request, sizeof(request)); + char *respBuffer = new char[request.RequestSize]; + XLinkPlatformGateRead(respBuffer, request.RequestSize); + auto resp = nlohmann::json::parse(respBuffer); + delete[] respBuffer; + spdlog::debug("DeviceGate createSession response: {}", resp.dump()); + + // Retrieve sessionId + sessionId = resp["id"]; + bool fwpExists = resp["fwp_exists"].get(); + + if(!fwpExists) { + std::vector package; + std::string path; + if(!path.empty()) { + std::ifstream fwStream(path, std::ios::binary); + if(!fwStream.is_open()) throw std::runtime_error(fmt::format("Cannot flash bootloader, binary at path: {} doesn't exist", path)); + package = std::vector(std::istreambuf_iterator(fwStream), {}); + } else { + package = platform == X_LINK_RVC3 ? Resources::getInstance().getDeviceRVC3Fwp() : Resources::getInstance().getDeviceRVC4Fwp(); + } + + nlohmann::json uploadFwpBody = {{"sessionId", sessionId}, + {"file", package}}; + request.RequestNum = UPLOAD_FWP; + request.RequestSize = uploadFwpBody.dump().size(); + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkPlatformGateWrite((void*)uploadFwpBody.dump().c_str(), uploadFwpBody.dump().size()); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + spdlog::warn("DeviceGate upload fwp not successful - got no response"); + return false; + } + } + sessionCreated = true; + return true; } return false; } bool DeviceGate::startSession() { - struct request_t { - uint16_t RequestNum; - uint32_t RequestSize; - }__attribute__((packed)); - request_t request; - request.RequestNum = 1; - request.RequestSize = 0; - - XLinkPlatformGateWrite(&request, sizeof(request)); - XLinkPlatformGateRead(&request, sizeof(request)); - int requestNum = request.RequestNum; - spdlog::warn("Request num: {}", requestNum); - std::string url = fmt::format("{}/{}/start", sessionsEndpoint, sessionId); if(auto res = pimpl->cli->Post(url.c_str())) { if(res->status != 200) { @@ -175,7 +283,19 @@ bool DeviceGate::startSession() { spdlog::debug("DeviceGate start fwp successful"); return true; } else { - spdlog::debug("DeviceGate start fwp not successful - got no response"); + USBRequest_t request; + request.RequestNum = START_SESSION; + request.RequestSize = sessionId.size(); + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + spdlog::debug("DeviceGate start fwp not successful - got no response"); + return false; + } + spdlog::debug("DeviceGate start fwp successful"); + return true; } return false; @@ -204,7 +324,19 @@ bool DeviceGate::stopSession() { spdlog::debug("DeviceGate stopSession successful"); return true; } else { - spdlog::error("DeviceGate stopSession not successful - got no response"); + USBRequest_t request; + request.RequestNum = STOP_SESSION; + request.RequestSize = sessionId.size(); + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + spdlog::error("DeviceGate stopSession not successful - got no response"); + return false; + } + spdlog::debug("DeviceGate stopSession successful"); + return true; } return false; @@ -230,7 +362,19 @@ bool DeviceGate::destroySession() { spdlog::debug("DeviceGate destroySession successful"); return true; } else { - spdlog::error("DeviceGate destroySession not successful - got no response"); + USBRequest_t request; + request.RequestNum = DESTROY_SESSION; + request.RequestSize = sessionId.size(); + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + spdlog::error("DeviceGate destroySession not successful - got no response"); + return false; + } + spdlog::debug("DeviceGate destroySession successful"); + return true; } return false; } @@ -250,7 +394,19 @@ bool DeviceGate::deleteSession() { spdlog::debug("DeviceGate deleteSession successful"); return true; } else { - spdlog::error("DeviceGate deleteSession not successful - got no response"); + USBRequest_t request; + request.RequestNum = DELETE_SESSION; + request.RequestSize = sessionId.size(); + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + spdlog::error("DeviceGate deleteSession not successful - got no response"); + return false; + } + spdlog::debug("DeviceGate deleteSession successful"); + return true; } return false; } @@ -289,7 +445,42 @@ DeviceGate::SessionState DeviceGate::getState() { } return sessionState; } else { - spdlog::warn("DeviceGate getState not successful - got no response"); + USBRequest_t request; + request.RequestNum = GET_STATE; + request.RequestSize = sessionId.size(); + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + spdlog::warn("DeviceGate getState not successful - got no response"); + return SessionState::ERROR_STATE; + } + + char *respBuffer = new char[request.RequestSize]; + XLinkPlatformGateRead(respBuffer, request.RequestSize); + auto resp = nlohmann::json::parse(respBuffer); + delete[] respBuffer; + spdlog::trace("DeviceGate getState response: {}", resp.dump()); + + std::string sessionStateStr = resp["state"]; + if(sessionStateStr == "CREATED") { + sessionState = SessionState::CREATED; + } else if(sessionStateStr == "RUNNING") { + sessionState = SessionState::RUNNING; + } else if(sessionStateStr == "STOPPED") { + sessionState = SessionState::STOPPED; + } else if(sessionStateStr == "STOPPING") { + sessionState = SessionState::STOPPING; + } else if(sessionStateStr == "CRASHED") { + sessionState = SessionState::CRASHED; + } else if(sessionStateStr == "DESTROYED") { + sessionState = SessionState::DESTROYED; + } else { + spdlog::warn("DeviceGate getState not successful - unknown session state: {}", sessionStateStr); + sessionState = SessionState::ERROR_STATE; + } + return sessionState; } return SessionState::ERROR_STATE; } @@ -309,8 +500,28 @@ std::optional> DeviceGate::getFile(const std::string& fileU return std::nullopt; } } else { - spdlog::warn("File download not successful - got no response"); - return std::nullopt; + USBRequest_t request; + request.RequestNum = GET_FILE; + request.RequestSize = fileUrl.size(); + XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkPlatformGateWrite((void*)fileUrl.c_str(), fileUrl.size()); + + XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + spdlog::warn("File download not successful - got no response"); + return std::nullopt; + } + + char *respBuffer = new char[request.RequestSize]; + XLinkPlatformGateRead(respBuffer, request.RequestSize); + auto resp = nlohmann::json::parse(respBuffer); + delete[] respBuffer; + + filename = resp["filename"].get(); + std::vector fileData(resp["data"]); + + spdlog::debug("File download successful. Filename: {}", filename); + return fileData; } } From fa9efd675b8eef0c3c84722449ad3b8309309ab3 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Fri, 18 Jul 2025 11:14:04 +0200 Subject: [PATCH 12/35] Null terminated all respBuffers --- examples/cpp/Camera/camera_max_resolution.cpp | 10 ++++------ examples/cpp/Camera/camera_output.cpp | 9 ++++----- less | 1 - src/device/DeviceGate.cpp | 20 ++++++++++++------- 4 files changed, 21 insertions(+), 19 deletions(-) delete mode 100644 less diff --git a/examples/cpp/Camera/camera_max_resolution.cpp b/examples/cpp/Camera/camera_max_resolution.cpp index a4879ecbe2..86eb996f82 100644 --- a/examples/cpp/Camera/camera_max_resolution.cpp +++ b/examples/cpp/Camera/camera_max_resolution.cpp @@ -23,13 +23,11 @@ int main() { auto videoIn = videoQueue->get(); if(videoIn == nullptr) continue; - videoIn->getCvFrame(); -std::cout << "Got frame" << std::endl; - //cv::imshow("video", videoIn->getCvFrame()); + cv::imshow("video", videoIn->getCvFrame()); - //if(cv::waitKey(1) == 'q') { - // break; - //} + if(cv::waitKey(1) == 'q') { + break; + } } return 0; diff --git a/examples/cpp/Camera/camera_output.cpp b/examples/cpp/Camera/camera_output.cpp index 2101c45c51..e12e8adb77 100644 --- a/examples/cpp/Camera/camera_output.cpp +++ b/examples/cpp/Camera/camera_output.cpp @@ -13,8 +13,7 @@ int main() { // Create nodes auto cam = pipeline.create()->build(); - auto videoQueue = cam->requestOutput(std::make_pair(4096, 2160))->createOutputQueue(); - //auto videoQueue = cam->requestOutput(std::make_pair(2048, 1556))->createOutputQueue(); + auto videoQueue = cam->requestOutput(std::make_pair(640, 400))->createOutputQueue(); // Start pipeline pipeline.start(); @@ -22,12 +21,12 @@ int main() { while(true) { auto videoIn = videoQueue->get(); if(videoIn == nullptr) continue; - videoIn->getCvFrame(); -/* cv::imshow("video", videoIn->getCvFrame()); + + cv::imshow("video", videoIn->getCvFrame()); if(cv::waitKey(1) == 'q') { break; - }*/ + } } return 0; diff --git a/less b/less deleted file mode 100644 index 19a589a9a6..0000000000 --- a/less +++ /dev/null @@ -1 +0,0 @@ -gmake: *** [Makefile:6195: cmake_check_build_system] Interrupt diff --git a/src/device/DeviceGate.cpp b/src/device/DeviceGate.cpp index 098c37c202..74809f813a 100644 --- a/src/device/DeviceGate.cpp +++ b/src/device/DeviceGate.cpp @@ -95,8 +95,9 @@ bool DeviceGate::isOkay() { return false; } - char *respBuffer = new char[request.RequestSize]; + char *respBuffer = new char[request.RequestSize + 1]; XLinkPlatformGateRead(respBuffer, request.RequestSize); + respBuffer[request.RequestSize]= '\0'; bool result = nlohmann::json::parse(respBuffer)["status"].get(); delete[] respBuffer; @@ -123,8 +124,9 @@ Version DeviceGate::getVersion() { return Version{0, 0, 0}; } - char *respBuffer = new char[request.RequestSize]; + char *respBuffer = new char[request.RequestSize + 1]; XLinkPlatformGateRead(respBuffer, request.RequestSize); + respBuffer[request.RequestSize]= '\0'; auto result = nlohmann::json::parse(respBuffer)["version_gate"].get(); delete[] respBuffer; @@ -155,8 +157,9 @@ DeviceGate::VersionInfo DeviceGate::getAllVersion() { return {}; } - char *respBuffer = new char[request.RequestSize]; + char *respBuffer = new char[request.RequestSize + 1]; XLinkPlatformGateRead(respBuffer, request.RequestSize); + respBuffer[request.RequestSize]= '\0'; auto result = nlohmann::json::parse(respBuffer); delete[] respBuffer; @@ -233,8 +236,9 @@ bool DeviceGate::createSession(bool exclusive) { XLinkPlatformGateWrite((void*)createSessionBody.dump().c_str(), createSessionBody.dump().size()); XLinkPlatformGateRead(&request, sizeof(request)); - char *respBuffer = new char[request.RequestSize]; + char *respBuffer = new char[request.RequestSize + 1]; XLinkPlatformGateRead(respBuffer, request.RequestSize); + respBuffer[request.RequestSize]= '\0'; auto resp = nlohmann::json::parse(respBuffer); delete[] respBuffer; spdlog::debug("DeviceGate createSession response: {}", resp.dump()); @@ -457,8 +461,9 @@ DeviceGate::SessionState DeviceGate::getState() { return SessionState::ERROR_STATE; } - char *respBuffer = new char[request.RequestSize]; + char *respBuffer = new char[request.RequestSize + 1]; XLinkPlatformGateRead(respBuffer, request.RequestSize); + respBuffer[request.RequestSize]= '\0'; auto resp = nlohmann::json::parse(respBuffer); delete[] respBuffer; spdlog::trace("DeviceGate getState response: {}", resp.dump()); @@ -512,13 +517,14 @@ std::optional> DeviceGate::getFile(const std::string& fileU return std::nullopt; } - char *respBuffer = new char[request.RequestSize]; + char *respBuffer = new char[request.RequestSize + 1]; XLinkPlatformGateRead(respBuffer, request.RequestSize); + respBuffer[request.RequestSize]= '\0'; auto resp = nlohmann::json::parse(respBuffer); delete[] respBuffer; filename = resp["filename"].get(); - std::vector fileData(resp["data"]); + std::vector fileData(resp["data"].get>()); spdlog::debug("File download successful. Filename: {}", filename); return fileData; From ff1ca698fe9aef6e176b406a8ac2c9f3dea38ac3 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Fri, 18 Jul 2025 11:59:54 +0200 Subject: [PATCH 13/35] Added error handler for create session --- src/device/DeviceGate.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/device/DeviceGate.cpp b/src/device/DeviceGate.cpp index 74809f813a..8df1bdec65 100644 --- a/src/device/DeviceGate.cpp +++ b/src/device/DeviceGate.cpp @@ -236,6 +236,10 @@ bool DeviceGate::createSession(bool exclusive) { XLinkPlatformGateWrite((void*)createSessionBody.dump().c_str(), createSessionBody.dump().size()); XLinkPlatformGateRead(&request, sizeof(request)); + if(request.RequestNum == RESPONSE_ERROR) { + spdlog::warn("DeviceGate createSession not successful - got no response"); + return false; + } char *respBuffer = new char[request.RequestSize + 1]; XLinkPlatformGateRead(respBuffer, request.RequestSize); respBuffer[request.RequestSize]= '\0'; From 0bd8b4207e28a4c81e1683eec93e866ef5291c7a Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Tue, 5 Aug 2025 11:48:39 +0200 Subject: [PATCH 14/35] Updated XLink --- cmake/depthaiDependencies.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index c4fccc0e77..737ec0b61b 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG d46c049e30d2a56f7bbec2e25e7e2fa5c09ec6d0 + GIT_TAG 58c5cb8b56a3e814a3c27a937c6e3a1456b36555 ) FetchContent_MakeAvailable( From 29abe4d81db2271f2f1ac66f4199d379c169ada8 Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Tue, 5 Aug 2025 12:12:25 +0200 Subject: [PATCH 15/35] Updated to use wrappers of Gate functions. --- cmake/depthaiDependencies.cmake | 2 +- src/device/DeviceGate.cpp | 74 ++++++++++++++++----------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/cmake/depthaiDependencies.cmake b/cmake/depthaiDependencies.cmake index 737ec0b61b..4bade5ead8 100644 --- a/cmake/depthaiDependencies.cmake +++ b/cmake/depthaiDependencies.cmake @@ -176,7 +176,7 @@ else() FetchContent_Declare( XLink GIT_REPOSITORY https://github.com/luxonis/XLink.git - GIT_TAG 58c5cb8b56a3e814a3c27a937c6e3a1456b36555 + GIT_TAG 674b295ce8a260c8817ee3396a1403d8697ce226 ) FetchContent_MakeAvailable( diff --git a/src/device/DeviceGate.cpp b/src/device/DeviceGate.cpp index 8df1bdec65..ca0498e529 100644 --- a/src/device/DeviceGate.cpp +++ b/src/device/DeviceGate.cpp @@ -1,7 +1,7 @@ #include "device/DeviceGate.hpp" #include -#include +#include // std #include @@ -88,15 +88,15 @@ bool DeviceGate::isOkay() { USBRequest_t request; request.RequestNum = IS_OKAY; request.RequestSize = 0; - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { return false; } char *respBuffer = new char[request.RequestSize + 1]; - XLinkPlatformGateRead(respBuffer, request.RequestSize); + XLinkGateRead(respBuffer, request.RequestSize); respBuffer[request.RequestSize]= '\0'; bool result = nlohmann::json::parse(respBuffer)["status"].get(); @@ -117,15 +117,15 @@ Version DeviceGate::getVersion() { USBRequest_t request; request.RequestNum = GET_VERSION; request.RequestSize = 0; - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { return Version{0, 0, 0}; } char *respBuffer = new char[request.RequestSize + 1]; - XLinkPlatformGateRead(respBuffer, request.RequestSize); + XLinkGateRead(respBuffer, request.RequestSize); respBuffer[request.RequestSize]= '\0'; auto result = nlohmann::json::parse(respBuffer)["version_gate"].get(); @@ -150,15 +150,15 @@ DeviceGate::VersionInfo DeviceGate::getAllVersion() { USBRequest_t request; request.RequestNum = GET_VERSION; request.RequestSize = 0; - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { return {}; } char *respBuffer = new char[request.RequestSize + 1]; - XLinkPlatformGateRead(respBuffer, request.RequestSize); + XLinkGateRead(respBuffer, request.RequestSize); respBuffer[request.RequestSize]= '\0'; auto result = nlohmann::json::parse(respBuffer); delete[] respBuffer; @@ -232,16 +232,16 @@ bool DeviceGate::createSession(bool exclusive) { USBRequest_t request; request.RequestNum = CREATE_SESSION; request.RequestSize = createSessionBody.dump().size(); - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateWrite((void*)createSessionBody.dump().c_str(), createSessionBody.dump().size()); + XLinkGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite((void*)createSessionBody.dump().c_str(), createSessionBody.dump().size()); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { spdlog::warn("DeviceGate createSession not successful - got no response"); return false; } char *respBuffer = new char[request.RequestSize + 1]; - XLinkPlatformGateRead(respBuffer, request.RequestSize); + XLinkGateRead(respBuffer, request.RequestSize); respBuffer[request.RequestSize]= '\0'; auto resp = nlohmann::json::parse(respBuffer); delete[] respBuffer; @@ -266,10 +266,10 @@ bool DeviceGate::createSession(bool exclusive) { {"file", package}}; request.RequestNum = UPLOAD_FWP; request.RequestSize = uploadFwpBody.dump().size(); - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateWrite((void*)uploadFwpBody.dump().c_str(), uploadFwpBody.dump().size()); + XLinkGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite((void*)uploadFwpBody.dump().c_str(), uploadFwpBody.dump().size()); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { spdlog::warn("DeviceGate upload fwp not successful - got no response"); return false; @@ -294,10 +294,10 @@ bool DeviceGate::startSession() { USBRequest_t request; request.RequestNum = START_SESSION; request.RequestSize = sessionId.size(); - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + XLinkGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { spdlog::debug("DeviceGate start fwp not successful - got no response"); return false; @@ -335,10 +335,10 @@ bool DeviceGate::stopSession() { USBRequest_t request; request.RequestNum = STOP_SESSION; request.RequestSize = sessionId.size(); - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + XLinkGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { spdlog::error("DeviceGate stopSession not successful - got no response"); return false; @@ -373,10 +373,10 @@ bool DeviceGate::destroySession() { USBRequest_t request; request.RequestNum = DESTROY_SESSION; request.RequestSize = sessionId.size(); - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + XLinkGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { spdlog::error("DeviceGate destroySession not successful - got no response"); return false; @@ -405,10 +405,10 @@ bool DeviceGate::deleteSession() { USBRequest_t request; request.RequestNum = DELETE_SESSION; request.RequestSize = sessionId.size(); - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + XLinkGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { spdlog::error("DeviceGate deleteSession not successful - got no response"); return false; @@ -456,17 +456,17 @@ DeviceGate::SessionState DeviceGate::getState() { USBRequest_t request; request.RequestNum = GET_STATE; request.RequestSize = sessionId.size(); - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateWrite((void*)sessionId.c_str(), sessionId.size()); + XLinkGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { spdlog::warn("DeviceGate getState not successful - got no response"); return SessionState::ERROR_STATE; } char *respBuffer = new char[request.RequestSize + 1]; - XLinkPlatformGateRead(respBuffer, request.RequestSize); + XLinkGateRead(respBuffer, request.RequestSize); respBuffer[request.RequestSize]= '\0'; auto resp = nlohmann::json::parse(respBuffer); delete[] respBuffer; @@ -512,17 +512,17 @@ std::optional> DeviceGate::getFile(const std::string& fileU USBRequest_t request; request.RequestNum = GET_FILE; request.RequestSize = fileUrl.size(); - XLinkPlatformGateWrite(&request, sizeof(USBRequest_t)); - XLinkPlatformGateWrite((void*)fileUrl.c_str(), fileUrl.size()); + XLinkGateWrite(&request, sizeof(USBRequest_t)); + XLinkGateWrite((void*)fileUrl.c_str(), fileUrl.size()); - XLinkPlatformGateRead(&request, sizeof(request)); + XLinkGateRead(&request, sizeof(request)); if(request.RequestNum == RESPONSE_ERROR) { spdlog::warn("File download not successful - got no response"); return std::nullopt; } char *respBuffer = new char[request.RequestSize + 1]; - XLinkPlatformGateRead(respBuffer, request.RequestSize); + XLinkGateRead(respBuffer, request.RequestSize); respBuffer[request.RequestSize]= '\0'; auto resp = nlohmann::json::parse(respBuffer); delete[] respBuffer; From 27526a673b4d396c94f6a45daa5ae8a4f651c6aa Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Tue, 5 Aug 2025 16:38:13 +0200 Subject: [PATCH 16/35] Removed the packed tag for device gate. --- src/device/DeviceGate.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/device/DeviceGate.cpp b/src/device/DeviceGate.cpp index ca0498e529..7dca1a00e1 100644 --- a/src/device/DeviceGate.cpp +++ b/src/device/DeviceGate.cpp @@ -44,9 +44,9 @@ enum USBRequestID_t { }; struct USBRequest_t { - uint16_t RequestNum; + uint32_t RequestNum; uint32_t RequestSize; -}__attribute__((packed)); +}; class DeviceGate::Impl { From 5acfb3f347cb1d5dd40a9bbcf95a87a72c040a4e Mon Sep 17 00:00:00 2001 From: Filippo Mutta Date: Tue, 2 Sep 2025 17:46:21 +0200 Subject: [PATCH 17/35] Added Impl inside DepthaiGate to split USB and HTTP request + ran clangformat --- include/depthai/device/DeviceGate.hpp | 65 +- include/depthai/pipeline/MessageQueue.hpp | 4 +- include/depthai/pipeline/Node.hpp | 4 +- include/depthai/utility/ImageManipImpl.hpp | 36 +- include/depthai/utility/LockingQueue.hpp | 4 +- include/depthai/utility/Memory.hpp | 2 +- include/depthai/utility/MemoryWrappers.hpp | 2 +- .../depthai/utility/NlohmannJsonCompat.hpp | 4 +- include/depthai/utility/Path.hpp | 4 +- src/device/DeviceGate.cpp | 755 ++++++++++-------- src/pipeline/Node.cpp | 4 +- src/pipeline/node/DetectionNetwork.cpp | 7 +- src/utility/LogCollection.cpp | 4 +- src/utility/MemoryWrappers.cpp | 2 +- src/utility/Platform.cpp | 2 +- 15 files changed, 502 insertions(+), 397 deletions(-) diff --git a/include/depthai/device/DeviceGate.hpp b/include/depthai/device/DeviceGate.hpp index 6a2257faf4..d3bfd81cba 100644 --- a/include/depthai/device/DeviceGate.hpp +++ b/include/depthai/device/DeviceGate.hpp @@ -56,6 +56,68 @@ class DeviceGate { private: // private + class GateImpl { + public: + virtual ~GateImpl(); + virtual bool isOkay() = 0; + virtual bool createSession(std::string version, bool exclusive, XLinkPlatform_t platform, std::string& sessionId, std::atomic_bool& sessionCreated) = 0; + virtual bool startSession(std::string sessionId) = 0; + virtual bool stopSession(std::string sessionId) = 0; + virtual bool deleteSession(std::string sessionId) = 0; + virtual bool destroySession(std::string sessionId) = 0; + virtual SessionState getState(std::string sessionId) = 0; + virtual std::optional waitForSessionEnd() = 0; + virtual std::optional getCrashDump() = 0; + virtual Version getVersion() = 0; + virtual VersionInfo getAllVersion() = 0; + virtual bool isBootedNonExclusive() = 0; + virtual std::optional> getFile(const std::string& fileUrl, std::string& filename) = 0; + }; + + class USBImpl : public GateImpl { + public: + ~USBImpl(); + bool isOkay() override; + bool createSession(std::string version, bool exclusive, XLinkPlatform_t platform, std::string& sessionId, std::atomic_bool& sessionCreated) override; + bool startSession(std::string sessionId) override; + bool stopSession(std::string sessionId) override; + bool deleteSession(std::string sessionId) override; + bool destroySession(std::string sessionId) override; + SessionState getState(std::string sessionId) override; + std::optional waitForSessionEnd() override; + std::optional getCrashDump() override; + Version getVersion() override; + VersionInfo getAllVersion() override; + bool isBootedNonExclusive() override; + std::optional> getFile(const std::string& fileUrl, std::string& filename) override; + }; + + class HTTPImpl : public GateImpl { + public: + HTTPImpl(DeviceInfo deviceInfo); + ~HTTPImpl(); + bool isOkay() override; + bool createSession(std::string version, bool exclusive, XLinkPlatform_t platform, std::string& sessionId, std::atomic_bool& sessionCreated) override; + bool startSession(std::string sessionId) override; + bool stopSession(std::string sessionId) override; + bool deleteSession(std::string sessionId) override; + bool destroySession(std::string sessionId) override; + SessionState getState(std::string sessionId) override; + std::optional waitForSessionEnd() override; + std::optional getCrashDump() override; + Version getVersion() override; + VersionInfo getAllVersion() override; + bool isBootedNonExclusive() override; + std::optional> getFile(const std::string& fileUrl, std::string& filename) override; + + private: + // pimpl + class Impl; + Pimpl pimpl; + }; + + std::shared_ptr impl; + DeviceInfo deviceInfo; std::thread stateMonitoringThread; @@ -69,9 +131,6 @@ class DeviceGate { std::shared_ptr gateConnection; std::shared_ptr gateStream; std::string version; - // pimpl - class Impl; - Pimpl pimpl; std::string sessionId; }; diff --git a/include/depthai/pipeline/MessageQueue.hpp b/include/depthai/pipeline/MessageQueue.hpp index 3e624aee1c..53761e4c90 100644 --- a/include/depthai/pipeline/MessageQueue.hpp +++ b/include/depthai/pipeline/MessageQueue.hpp @@ -43,13 +43,13 @@ class MessageQueue : public std::enable_shared_from_this { explicit MessageQueue(std::string name, unsigned int maxSize = 16, bool blocking = true); MessageQueue(const MessageQueue& c) - : enable_shared_from_this(c), queue(c.queue), name(c.name), callbacks(c.callbacks), uniqueCallbackId(c.uniqueCallbackId){}; + : enable_shared_from_this(c), queue(c.queue), name(c.name), callbacks(c.callbacks), uniqueCallbackId(c.uniqueCallbackId) {}; MessageQueue(MessageQueue&& m) noexcept : enable_shared_from_this(m), queue(std::move(m.queue)), name(std::move(m.name)), callbacks(std::move(m.callbacks)), - uniqueCallbackId(m.uniqueCallbackId){}; + uniqueCallbackId(m.uniqueCallbackId) {}; MessageQueue& operator=(const MessageQueue& c) { queue = c.queue; diff --git a/include/depthai/pipeline/Node.hpp b/include/depthai/pipeline/Node.hpp index 7faa5c1767..022ec73073 100644 --- a/include/depthai/pipeline/Node.hpp +++ b/include/depthai/pipeline/Node.hpp @@ -65,7 +65,9 @@ class Node : public std::enable_shared_from_this { static constexpr auto DEFAULT_NAME = ""; #define DEFAULT_TYPES \ { \ - { DatatypeEnum::Buffer, true } \ + { \ + DatatypeEnum::Buffer, true \ + } \ } static constexpr auto DEFAULT_BLOCKING = true; static constexpr auto DEFAULT_QUEUE_SIZE = 3; diff --git a/include/depthai/utility/ImageManipImpl.hpp b/include/depthai/utility/ImageManipImpl.hpp index 9657698947..3ff234d987 100644 --- a/include/depthai/utility/ImageManipImpl.hpp +++ b/include/depthai/utility/ImageManipImpl.hpp @@ -382,8 +382,7 @@ class ColorChange { template