-
Notifications
You must be signed in to change notification settings - Fork 4
Increase SD card speed (and file transfer) by roughly 10x for Tanmatsu and Konsool #4
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
cavac
wants to merge
9
commits into
badgeteam:main
Choose a base branch
from
cavac:main
base: main
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3a1db4c
Increase queue size for better buffering (slow SD cards...)
cavac fe11901
Use streaming CRC instead of rereading everything. Prevents timeouts on
cavac 509a755
Add support for protocol negotiation (for backwards compatibility), add
cavac b3021e9
BF
cavac ca0ebad
Speed up SD card operations by a factor of 10 on Tanmatsu and Konsool
cavac f370093
Cleanup
cavac a9c3040
Cleanup
cavac 176e590
Merge branch 'badgeteam:main' into main
cavac 7b14dcd
Tweaks to badglelink V2, update badgelink.proto to match
cavac 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # BadgeLink Protocol Changes | ||
|
|
||
| ## Protocol Version Negotiation (Version 2) | ||
|
|
||
| This update adds protocol version negotiation to BadgeLink. The previous protocol without version negotiation is considered version 1. | ||
|
|
||
| ### New Message Types | ||
|
|
||
| #### VersionReq (Request tag 7) | ||
|
|
||
| Sent by the client to negotiate the protocol version. | ||
|
|
||
| | Field | Tag | Type | Description | | ||
| |-------|-----|------|-------------| | ||
| | client_version | 1 | uint32 | Highest protocol version supported by the client | | ||
|
|
||
| #### VersionResp (Response tag 6) | ||
|
|
||
| Sent by the server in response to a VersionReq. | ||
|
|
||
| | Field | Tag | Type | Description | | ||
| |-------|-----|------|-------------| | ||
| | server_version | 1 | uint32 | Highest protocol version supported by the server | | ||
| | negotiated_version | 2 | uint32 | Protocol version to use for this session | | ||
|
|
||
| ### Negotiation Algorithm | ||
|
|
||
| The server calculates the negotiated version as: | ||
| ``` | ||
| negotiated_version = min(client_version, server_version) | ||
| ``` | ||
|
|
||
| The server stores this negotiated version and uses it for the remainder of the session. | ||
|
|
||
| **Important**: The server resets the negotiated version to 1 when a sync packet is received. This ensures each new connection starts fresh with v1 behavior until version negotiation occurs. | ||
|
|
||
| ### Backwards Compatibility | ||
|
|
||
| - **Old server (v1) + New client**: The server responds with `StatusNotSupported`. The client should fall back to version 1 behavior. | ||
| - **New server (v2) + Old client**: The client never sends a VersionReq. The server defaults to version 1. | ||
| - **New server + New client**: Full version negotiation occurs. | ||
|
|
||
| ### Client Implementation | ||
|
|
||
| Recommended flow for new clients: | ||
|
|
||
| ``` | ||
| 1. After sync, send VersionReq with client_version = 2 | ||
| 2. If response is StatusNotSupported: | ||
| - Server is version 1, use legacy behavior | ||
| 3. If response is VersionResp: | ||
| - Use negotiated_version for session behavior | ||
| - server_version indicates what features the server supports | ||
| ``` | ||
|
|
||
| ### Server API | ||
|
|
||
| The server exposes `badgelink_get_protocol_version()` which returns the currently negotiated protocol version (defaults to 1 if no VersionReq was received). | ||
|
|
||
| --- | ||
|
|
||
| ## Streaming CRC for Downloads (Version 2) | ||
|
|
||
| Version 2 changes how file downloads work to improve performance for large files. | ||
|
|
||
| ### Version 1 Behavior (Legacy) | ||
|
|
||
| 1. Client sends download request | ||
| 2. Server reads **entire file** to calculate CRC32 | ||
| 3. Server responds with `size` and `crc32` | ||
| 4. Client requests chunks with `XferContinue` | ||
| 5. Server sends `download_chunk` responses | ||
| 6. Client sends `XferFinish` | ||
| 7. Server responds with `StatusOk` | ||
|
|
||
| **Problem**: For large files, the server must read the entire file before sending the first byte. This causes significant delays. | ||
|
|
||
| ### Version 2 Behavior (Streaming CRC) | ||
|
|
||
| 1. Client sends download request | ||
| 2. Server uses `stat()` to get file size (no file read) | ||
| 3. Server responds with `size` and `crc32 = 0` | ||
| 4. Client requests chunks with `XferContinue` | ||
| 5. Server sends `download_chunk` responses, computing CRC incrementally | ||
| 6. Client sends `XferFinish` | ||
| 7. Server responds with `FsActionResp` or `AppfsActionResp` containing the final `crc32` | ||
|
|
||
| **Benefit**: Download starts immediately without reading the entire file first. | ||
|
|
||
| ### Response Differences | ||
|
|
||
| | Event | Version 1 | Version 2 | | ||
| |-------|-----------|-----------| | ||
| | Download start | `crc32` = actual CRC | `crc32` = 0 | | ||
| | XferFinish (FS) | `StatusOk` | `FsActionResp` with `crc32` | | ||
| | XferFinish (AppFS) | `StatusOk` | `AppfsActionResp` with `crc32` | | ||
|
|
||
| --- | ||
|
|
||
| ## Python Client Updates | ||
|
|
||
| The Python client (`badgelink.py`) has been updated to support protocol version 2. | ||
|
|
||
| ### New Features | ||
|
|
||
| - **Automatic version negotiation**: The client automatically negotiates the protocol version with the server on connection. | ||
| - **Streaming CRC for downloads**: Downloads use streaming CRC verification for v2 servers. | ||
|
|
||
| ### Command Line Options | ||
|
|
||
| ``` | ||
| --version1 Force protocol version 1 (legacy mode, skip version negotiation) | ||
| ``` | ||
|
|
||
| Use `--version1` when you need to connect using the legacy protocol, for example when testing v1 compatibility or connecting to a known v1 server. | ||
|
|
||
| ### Example Usage | ||
|
|
||
| ```bash | ||
| # Normal usage (auto-negotiates version) | ||
| ./badgelink.sh fs download /sd/file.bin local_file.bin | ||
|
|
||
| # Force version 1 protocol | ||
| ./badgelink.sh --version1 fs download /sd/file.bin local_file.bin | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Revision History | ||
|
|
||
| ### 2025-12-16: Field Type Change (uint16 to uint32) | ||
|
|
||
| The version fields in `VersionReq` and `VersionResp` were changed from `uint16` to `uint32`. | ||
|
|
||
| **Reason**: Protocol Buffers (proto3) does not have a native `uint16` type. The smallest unsigned integer type available is `uint32`. While nanopb can generate C code with `uint16_t` fields using options, this creates inconsistency between the C implementation and other language bindings (Python, etc.) which use `uint32`. | ||
|
|
||
| **Changes made**: | ||
|
|
||
| | File | Change | | ||
| |------|--------| | ||
| | `badgelink.proto` | Added `VersionReq` and `VersionResp` messages with `uint32` fields | | ||
| | `badgelink.pb.h` | Changed struct fields from `uint16_t` to `uint32_t` | | ||
| | `tools/libraries/badgelink_pb2.py` | Regenerated from updated proto file | | ||
| | `tools/badgelink.py` | Simplified to use generated protobuf classes instead of manual encoding | | ||
|
|
||
| **Wire compatibility**: This change is wire-compatible. Protobuf varints encode small values (like version numbers 1, 2, etc.) identically regardless of whether the field is declared as `uint16` or `uint32`. Existing implementations will continue to work. |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.