-
Notifications
You must be signed in to change notification settings - Fork 158
Depthai USB Support #1390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheMutta
wants to merge
39
commits into
develop
Choose a base branch
from
oak4-usb-support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Depthai USB Support #1390
Changes from 16 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
2cd0570
Changed XLink commit.
e470ea6
Added new protocol handling in log collection
7a82389
Updated XLink to support USB EP discovery
74f5a3a
Updated XLink
ef65dd0
Updated xlink
c71236a
Reset example
461e015
Updated XLink
b539e7b
Updated xlink and added test for Gate Write
8a03d04
Added debug message
6784c4f
Testing request via USB.
1f362b0
Finalised GATE via USB.
fa9efd6
Null terminated all respBuffers
ff1ca69
Added error handler for create session
0bd8b42
Updated XLink
29abe4d
Updated to use wrappers of Gate functions.
27526a6
Removed the packed tag for device gate.
5acfb3f
Added Impl inside DepthaiGate to split USB and HTTP request + ran cla…
e0801fb
Removed depthai-shared
7159156
Added USB tests for RVC4
52a92ba
Fixed GateImpl destructors and removed unnecessary functions
a08d62b
Updated XLink with new Gate Discovery features.
8495aa5
Merge remote-tracking branch 'origin/develop' into oak4-usb-support
f2750d6
Updated XLink+
e69b2c9
DeviceGate: bugfixing for USB.
c80eb9d
XLink: Updated XLink for USB EPs
825dbf3
Merge branch 'develop' into oak4-usb-support
e0de70b
DeviceGate: Added serious error handling and ran clangformat.
94187c3
XLink: updated XLink with new fixes for USB EP.
748ed44
XLink: updated to merge between usb and usb_ep
596c5d7
XLink: updated with newer usb_ep
3ccb575
XLink: updated with server-side on usb_host
36e4581
DeviceGate: Updated with new gate updates.
d9ab27b
USB: Testing OS added.
dd41350
Update tests/run_tests.py
TheMutta 9eb8f18
Update tests/run_tests.py
TheMutta 5402984
Updated XLink and added usb-testing OS build in the CI.
4f37bf7
Merge branch 'develop' into oak4-usb-support
TheMutta 3e95126
Updated XLink.
18a04b1
Merge branch 'oak4-usb-support' of github.com:luxonis/depthai-core in…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,4 +31,4 @@ int main() { | |
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,4 +30,4 @@ int main() { | |
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule depthai-shared
added at
9762f3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| #include "device/DeviceGate.hpp" | ||
|
|
||
| #include <XLink/XLinkPublicDefines.h> | ||
| #include <XLink/XLink.h> | ||
|
|
||
| // std | ||
| #include <fstream> | ||
|
|
@@ -27,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 { | ||
| uint32_t RequestNum; | ||
| uint32_t RequestSize; | ||
| }; | ||
|
|
||
|
|
||
| class DeviceGate::Impl { | ||
| public: | ||
| Impl() = default; | ||
|
|
@@ -44,7 +66,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 +78,33 @@ DeviceGate::DeviceGate(const DeviceInfo& deviceInfo) : deviceInfo(deviceInfo) { | |
| pimpl->cli = std::make_unique<httplib::Client>(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); | ||
|
|
||
| } | ||
|
|
||
| bool DeviceGate::isOkay() { | ||
| if(auto res = pimpl->cli->Get("/api/v1/status")) { | ||
| return nlohmann::json::parse(res->body)["status"].get<bool>(); | ||
| } else { | ||
| USBRequest_t request; | ||
| request.RequestNum = IS_OKAY; | ||
| request.RequestSize = 0; | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
|
|
||
| XLinkGateRead(&request, sizeof(request)); | ||
| if(request.RequestNum == RESPONSE_ERROR) { | ||
| return false; | ||
| } | ||
|
|
||
| char *respBuffer = new char[request.RequestSize + 1]; | ||
|
||
| XLinkGateRead(respBuffer, request.RequestSize); | ||
| respBuffer[request.RequestSize]= '\0'; | ||
|
|
||
| bool result = nlohmann::json::parse(respBuffer)["status"].get<bool>(); | ||
| delete[] respBuffer; | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -70,6 +113,26 @@ Version DeviceGate::getVersion() { | |
| if(res && res->status == 200) { | ||
| auto versionStr = nlohmann::json::parse(res->body)["version_gate"].get<std::string>(); | ||
| return Version{versionStr}; | ||
| } else { | ||
| USBRequest_t request; | ||
| request.RequestNum = GET_VERSION; | ||
| request.RequestSize = 0; | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
|
|
||
| XLinkGateRead(&request, sizeof(request)); | ||
| if(request.RequestNum == RESPONSE_ERROR) { | ||
| return Version{0, 0, 0}; | ||
| } | ||
|
|
||
| char *respBuffer = new char[request.RequestSize + 1]; | ||
| XLinkGateRead(respBuffer, request.RequestSize); | ||
| respBuffer[request.RequestSize]= '\0'; | ||
|
|
||
| auto result = nlohmann::json::parse(respBuffer)["version_gate"].get<std::string>(); | ||
| delete[] respBuffer; | ||
|
|
||
| return Version{result}; | ||
|
|
||
| } | ||
| return Version{0, 0, 0}; | ||
| } | ||
|
|
@@ -83,6 +146,28 @@ 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; | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
|
|
||
| XLinkGateRead(&request, sizeof(request)); | ||
| if(request.RequestNum == RESPONSE_ERROR) { | ||
| return {}; | ||
| } | ||
|
|
||
| char *respBuffer = new char[request.RequestSize + 1]; | ||
| XLinkGateRead(respBuffer, request.RequestSize); | ||
| respBuffer[request.RequestSize]= '\0'; | ||
| 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 {}; | ||
| } | ||
|
|
@@ -144,7 +229,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(); | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
| XLinkGateWrite((void*)createSessionBody.dump().c_str(), createSessionBody.dump().size()); | ||
|
|
||
| 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]; | ||
| XLinkGateRead(respBuffer, request.RequestSize); | ||
| respBuffer[request.RequestSize]= '\0'; | ||
| 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<bool>(); | ||
|
|
||
| if(!fwpExists) { | ||
| std::vector<uint8_t> 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::uint8_t>(std::istreambuf_iterator<char>(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(); | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
| XLinkGateWrite((void*)uploadFwpBody.dump().c_str(), uploadFwpBody.dump().size()); | ||
|
|
||
| XLinkGateRead(&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; | ||
| } | ||
|
|
@@ -159,7 +291,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(); | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
| XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); | ||
|
|
||
| XLinkGateRead(&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; | ||
|
|
@@ -188,7 +332,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(); | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
| XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); | ||
|
|
||
| XLinkGateRead(&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; | ||
|
|
@@ -214,7 +370,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(); | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
| XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); | ||
|
|
||
| XLinkGateRead(&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; | ||
| } | ||
|
|
@@ -234,7 +402,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(); | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
| XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); | ||
|
|
||
| XLinkGateRead(&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; | ||
| } | ||
|
|
@@ -273,7 +453,43 @@ 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(); | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
| XLinkGateWrite((void*)sessionId.c_str(), sessionId.size()); | ||
|
|
||
| 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]; | ||
| XLinkGateRead(respBuffer, request.RequestSize); | ||
| respBuffer[request.RequestSize]= '\0'; | ||
| 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; | ||
| } | ||
|
|
@@ -293,8 +509,29 @@ std::optional<std::vector<uint8_t>> 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(); | ||
| XLinkGateWrite(&request, sizeof(USBRequest_t)); | ||
| XLinkGateWrite((void*)fileUrl.c_str(), fileUrl.size()); | ||
|
|
||
| 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]; | ||
| XLinkGateRead(respBuffer, request.RequestSize); | ||
| respBuffer[request.RequestSize]= '\0'; | ||
| auto resp = nlohmann::json::parse(respBuffer); | ||
| delete[] respBuffer; | ||
|
|
||
| filename = resp["filename"].get<std::string>(); | ||
| std::vector<uint8_t> fileData(resp["data"].get<std::vector<uint8_t>>()); | ||
|
|
||
| spdlog::debug("File download successful. Filename: {}", filename); | ||
| return fileData; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be removed