[tlvs] add Start/End/AdjustTlv methods for staged writing - #12250
Conversation
Summary of ChangesHello @abtink, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the handling of Type-Length-Value (TLV) structures within messages by introducing a new set of static methods for staged writing. This change streamlines the process of appending variable-length TLVs, eliminating the need for manual length calculation and back-patching. The new methods improve code clarity, reduce potential errors, and offer performance optimizations for large TLVs, ultimately making TLV serialization more efficient and maintainable across the codebase. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces new helper functions (StartTlv, AdjustTlv, EndTlv) within the Tlv class to streamline the process of appending TLVs to messages, particularly when the TLV's final length is unknown during initial creation. These functions manage the creation of placeholder TLV headers, dynamically promote standard TLVs to extended TLVs if their content exceeds the base length limit (with AdjustTlv offering an optional optimization to avoid large memory copies), and finalize the TLV by writing the correct length. Existing code in discover_scanner.cpp, link_metrics.cpp, mle.cpp, and network_diagnostic.cpp has been refactored to utilize this new pattern, replacing manual TLV header management and length calculations. Additionally, comprehensive unit tests have been added in test_tlv.cpp to validate the functionality of these new TLV appending methods.
Library files
|
a02e547 to
571423f
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #12250 +/- ##
==========================================
- Coverage 75.91% 72.90% -3.02%
==========================================
Files 681 680 -1
Lines 95588 99198 +3610
==========================================
- Hits 72564 72317 -247
- Misses 23024 26881 +3857
🚀 New features to boost your workflow:
|
This commit introduces a new set of static methods to simplify writing TLVs with variable lengths to a `Message`. The new mechanism consists of three methods: - `Tlv::StartTlv()`: Appends a placeholder TLV header and returns a `Bookmark`. - `Tlv::AdjustTlv()`: Optionally promotes the TLV to an extended TLV if the length grows beyond the standard TLV limit. This is an optimization to avoid large copies within a message. - `Tlv::EndTlv()`: Calculates the final length and updates the TLV header, promoting to an extended TLV if necessary. This new set replaces the common but cumbersome pattern of manually saving the start offset, appending data, and then back-patching the length field. The existing code is updated to use this new, simpler, and more robust mechanism. This commit also adds unit tests to validate the new functionality.
571423f to
d4cd40b
Compare
This commit introduces a new set of static methods to simplify writing TLVs with variable lengths to a
Message.The new mechanism consists of three methods:
Tlv::StartTlv(): Appends a placeholder TLV header and returns aBookmark.Tlv::AdjustTlv(): Optionally promotes the TLV to an extended TLV if the length grows beyond the standard TLV limit. This is an optimization to avoid large copies within a message.Tlv::EndTlv(): Calculates the final length and updates the TLV header, promoting to an extended TLV if necessary.This new set replaces the common but cumbersome pattern of manually saving the start offset, appending data, and then back-patching the length field.
The existing code is updated to use this new, simpler, and more robust mechanism.
This commit also adds unit tests to validate the new functionality.