Skip to content

Conversation

TheMutta
Copy link

Purpose

This branch adds the ability to Depthai to communicate with Gate via USB through XLink

Specification

All the API calls in the DepthaiGate class use USB communication if TCP/IP is not available
None / not applicable

Dependencies & Potential Impact

All the dependencies are already integrated in XLink

Deployment Plan

The changes don't affect the normal functioning via TCP/IP and don't break existing features, they can be rolled out easily.

Testing & Validation

The branch was tested by using all the API calls, which all completed successfully.

cursor[bot]

This comment was marked as outdated.

@TheMutta
Copy link
Author

bugbot run

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: USB Fallback Missing Error Handling

The createSession() USB fallback implementation in DeviceGate.cpp is missing an error check after reading the USB response header. Unlike other USB methods in the file, it does not verify request.RequestNum == RESPONSE_ERROR. This allows the code to proceed with memory allocation and JSON parsing using potentially invalid request.RequestSize or error data, which can lead to memory corruption, crashes, or incorrect behavior.

src/device/DeviceGate.cpp#L237-L244

XLinkPlatformGateRead(&request, sizeof(request));
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());

Fix in CursorFix in Web


Bug: USB Firmware Upload Fails JSON Binary Handling

The USB communication path incorrectly embeds the firmware package (a std::vector<uint8_t>) directly into a JSON object. Unlike the HTTP path which uses multipart form data for binary uploads, JSON does not natively support raw binary data. This format mismatch will prevent the server from correctly processing the firmware package.

src/device/DeviceGate.cpp#L260-L266

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());

Fix in CursorFix in Web


Comment bugbot run to trigger another review on this PR
Was this report helpful? Give feedback by reacting with 👍 or 👎

@TheMutta TheMutta changed the base branch from v3_develop to develop July 18, 2025 10:41
return false;
}

char *respBuffer = new char[request.RequestSize + 1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets use C++ here - eg a vector /w reserve or resize and pass its "data()" to XLink to not have to manually new & delete

Copy link
Collaborator

@moratom moratom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @TheMutta!

I'm thinking if I'd be safer here to define an interface (pure virtual class) and then do two implementations, so we have compile-time safety to a degree that we implemented all current (and potential future) calls to gate.
What do you think?

XLink
GIT_REPOSITORY https://github.com/luxonis/XLink.git
GIT_TAG 87785828fabdb1718760bb0a044405d5bbfbb3a2
GIT_TAG d46c049e30d2a56f7bbec2e25e7e2fa5c09ec6d0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we cross link the XLink PR in this PRs description? Thanks!

throw std::invalid_argument("Gate only supports RVC3 and RVC4 platforms");
}
this->platform = deviceInfo.platform;
this->platform =deviceInfo.platform;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace change

struct USBRequest_t {
uint16_t RequestNum;
uint32_t RequestSize;
}__attribute__((packed));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this cross platform?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My 2c, can just skip packed by making both uint32_t or swapping them around

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely cross platform

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be removed

request.RequestSize = 0;
XLinkGateWrite(&request, sizeof(USBRequest_t));

XLinkGateRead(&request, sizeof(request));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue with Gate being in an "wrong state / timeout"
Pass a timeout explicitly here, instead of using a default.
Align the timeout, with Gate impl.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pin down "who" retries in this case when Gate isn't yet available

@moratom moratom added the testable PR is ready to be tested label Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testable PR is ready to be tested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants