From 9e9ef21a3df27192b1fe5098e7e9dccadfbe1f4f Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 11 Jan 2024 19:27:57 -0800 Subject: [PATCH] [Yunq] POC for moving yunq parsing to a library. --- lib/yunq/CMakeLists.txt | 1 + lib/yunq/message_view.cpp | 19 +++++++ lib/yunq/message_view.h | 40 ++++++++++++++ sys/denali/lib/denali/denali.yunq.cpp | 20 ++++--- .../lib/victoriafalls/victoriafalls.yunq.cpp | 28 +++++----- .../lib/voyageurs/voyageurs.yunq.cpp | 4 +- .../lib/yellowstone/yellowstone.yunq.cpp | 55 ++++++++++--------- yunq/message.cpp.jinja | 17 ++---- 8 files changed, 125 insertions(+), 59 deletions(-) create mode 100644 lib/yunq/message_view.cpp create mode 100644 lib/yunq/message_view.h diff --git a/lib/yunq/CMakeLists.txt b/lib/yunq/CMakeLists.txt index dea897d..5fd7a31 100644 --- a/lib/yunq/CMakeLists.txt +++ b/lib/yunq/CMakeLists.txt @@ -1,4 +1,5 @@ set(yunq_files + message_view.cpp serialize.cpp ) diff --git a/lib/yunq/message_view.cpp b/lib/yunq/message_view.cpp new file mode 100644 index 0000000..6567f73 --- /dev/null +++ b/lib/yunq/message_view.cpp @@ -0,0 +1,19 @@ +#include "message_view.h" + +namespace yunq { + +template <> +glcr::ErrorOr MessageView::ReadField(uint64_t field_index) { + return buffer_.At(field_offset(field_index)); +} + +template <> +glcr::ErrorOr MessageView::ReadField( + uint64_t field_index) { + ExtensionPointer ptr = + buffer_.At(field_offset(field_index)); + + return buffer_.StringAt(offset_ + ptr.offset, ptr.length); +} + +} // namespace yunq diff --git a/lib/yunq/message_view.h b/lib/yunq/message_view.h new file mode 100644 index 0000000..ca68acf --- /dev/null +++ b/lib/yunq/message_view.h @@ -0,0 +1,40 @@ +#pragma once + +#include +#include + +namespace yunq { + +const uint64_t kHeaderSize = 24; // 4x uint32, 1x uint64 + +struct ExtensionPointer { + uint32_t offset; + uint32_t length; +}; + +class MessageView { + public: + MessageView(const glcr::ByteBuffer& buffer, uint64_t offset) + : buffer_(buffer), offset_(offset) {} + + // TODO: Implement glcr::StatusOr + template + glcr::ErrorOr ReadField(uint64_t field_index); + + private: + const glcr::ByteBuffer& buffer_; + uint64_t offset_; + + uint64_t field_offset(uint64_t field_index) { + return offset_ + kHeaderSize + (8 * field_index); + } +}; + +template <> +glcr::ErrorOr MessageView::ReadField(uint64_t field_index); + +template <> +glcr::ErrorOr MessageView::ReadField( + uint64_t field_index); + +} // namespace yunq diff --git a/sys/denali/lib/denali/denali.yunq.cpp b/sys/denali/lib/denali/denali.yunq.cpp index 0f6646b..eaf9f6c 100644 --- a/sys/denali/lib/denali/denali.yunq.cpp +++ b/sys/denali/lib/denali/denali.yunq.cpp @@ -1,6 +1,7 @@ // Generated file -- DO NOT MODIFY. #include "denali.yunq.h" +#include #include @@ -26,12 +27,14 @@ glcr::Status ReadRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t glcr::Status ReadRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); + + yunq::MessageView view(bytes, offset); // Parse device_id. - set_device_id(bytes.At(offset + header_size + (8 * 0))); + ASSIGN_OR_RETURN(device_id_, view.ReadField(0)); // Parse lba. - set_lba(bytes.At(offset + header_size + (8 * 1))); + ASSIGN_OR_RETURN(lba_, view.ReadField(1)); // Parse size. - set_size(bytes.At(offset + header_size + (8 * 2))); + ASSIGN_OR_RETURN(size_, view.ReadField(2)); return glcr::Status::Ok(); } @@ -80,8 +83,10 @@ glcr::Status ReadManyRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint glcr::Status ReadManyRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); + + yunq::MessageView view(bytes, offset); // Parse device_id. - set_device_id(bytes.At(offset + header_size + (8 * 0))); + ASSIGN_OR_RETURN(device_id_, view.ReadField(0)); // Parse lba. auto lba_pointer = bytes.At(offset + header_size + (8 * 1)); @@ -201,12 +206,13 @@ glcr::Status ReadResponse::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_ glcr::Status ReadResponse::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); + + yunq::MessageView view(bytes, offset); // Parse device_id. - set_device_id(bytes.At(offset + header_size + (8 * 0))); + ASSIGN_OR_RETURN(device_id_, view.ReadField(0)); // Parse size. - set_size(bytes.At(offset + header_size + (8 * 1))); + ASSIGN_OR_RETURN(size_, view.ReadField(1)); // Parse memory. - // Skip Cap. return glcr::Status::Ok(); } diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp index 44c50b0..46f8899 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.cpp @@ -1,6 +1,7 @@ // Generated file -- DO NOT MODIFY. #include "victoriafalls.yunq.h" +#include #include @@ -26,10 +27,10 @@ glcr::Status OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint glcr::Status OpenFileRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); - // Parse path. - auto path_pointer = bytes.At(offset + header_size + (8 * 0)); - set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length)); + yunq::MessageView view(bytes, offset); + // Parse path. + ASSIGN_OR_RETURN(path_, view.ReadField(0)); return glcr::Status::Ok(); } @@ -95,14 +96,13 @@ glcr::Status OpenFileResponse::ParseFromBytes(const glcr::ByteBuffer& bytes, uin glcr::Status OpenFileResponse::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); - // Parse path. - auto path_pointer = bytes.At(offset + header_size + (8 * 0)); - set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length)); + yunq::MessageView view(bytes, offset); + // Parse path. + ASSIGN_OR_RETURN(path_, view.ReadField(0)); // Parse size. - set_size(bytes.At(offset + header_size + (8 * 1))); + ASSIGN_OR_RETURN(size_, view.ReadField(1)); // Parse memory. - // Skip Cap. return glcr::Status::Ok(); } @@ -171,10 +171,10 @@ glcr::Status GetDirectoryRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, glcr::Status GetDirectoryRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); - // Parse path. - auto path_pointer = bytes.At(offset + header_size + (8 * 0)); - set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length)); + yunq::MessageView view(bytes, offset); + // Parse path. + ASSIGN_OR_RETURN(path_, view.ReadField(0)); return glcr::Status::Ok(); } @@ -233,10 +233,10 @@ glcr::Status Directory::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t o glcr::Status Directory::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); - // Parse filenames. - auto filenames_pointer = bytes.At(offset + header_size + (8 * 0)); - set_filenames(bytes.StringAt(offset + filenames_pointer.offset, filenames_pointer.length)); + yunq::MessageView view(bytes, offset); + // Parse filenames. + ASSIGN_OR_RETURN(filenames_, view.ReadField(0)); return glcr::Status::Ok(); } diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp index 3adfc57..563b0f6 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.cpp @@ -1,6 +1,7 @@ // Generated file -- DO NOT MODIFY. #include "voyageurs.yunq.h" +#include #include @@ -33,8 +34,9 @@ glcr::Status KeyboardListener::ParseFromBytes(const glcr::ByteBuffer& bytes, uin glcr::Status KeyboardListener::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); + + yunq::MessageView view(bytes, offset); // Parse port_capability. - // Skip Cap. return glcr::Status::Ok(); } diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp index 6b78856..3a5dc65 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.cpp @@ -1,6 +1,7 @@ // Generated file -- DO NOT MODIFY. #include "yellowstone.yunq.h" +#include #include @@ -35,12 +36,11 @@ glcr::Status RegisterEndpointRequest::ParseFromBytes(const glcr::ByteBuffer& byt glcr::Status RegisterEndpointRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); - // Parse endpoint_name. - auto endpoint_name_pointer = bytes.At(offset + header_size + (8 * 0)); - set_endpoint_name(bytes.StringAt(offset + endpoint_name_pointer.offset, endpoint_name_pointer.length)); + yunq::MessageView view(bytes, offset); + // Parse endpoint_name. + ASSIGN_OR_RETURN(endpoint_name_, view.ReadField(0)); // Parse endpoint_capability. - // Skip Cap. return glcr::Status::Ok(); } @@ -105,10 +105,10 @@ glcr::Status GetEndpointRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, u glcr::Status GetEndpointRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); - // Parse endpoint_name. - auto endpoint_name_pointer = bytes.At(offset + header_size + (8 * 0)); - set_endpoint_name(bytes.StringAt(offset + endpoint_name_pointer.offset, endpoint_name_pointer.length)); + yunq::MessageView view(bytes, offset); + // Parse endpoint_name. + ASSIGN_OR_RETURN(endpoint_name_, view.ReadField(0)); return glcr::Status::Ok(); } @@ -174,8 +174,9 @@ glcr::Status Endpoint::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t of glcr::Status Endpoint::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); + + yunq::MessageView view(bytes, offset); // Parse endpoint. - // Skip Cap. return glcr::Status::Ok(); } @@ -225,10 +226,11 @@ glcr::Status AhciInfo::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t of glcr::Status AhciInfo::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); + + yunq::MessageView view(bytes, offset); // Parse ahci_region. - // Skip Cap. // Parse region_length. - set_region_length(bytes.At(offset + header_size + (8 * 1))); + ASSIGN_OR_RETURN(region_length_, view.ReadField(1)); return glcr::Status::Ok(); } @@ -275,30 +277,32 @@ glcr::Status FramebufferInfo::ParseFromBytes(const glcr::ByteBuffer& bytes, uint glcr::Status FramebufferInfo::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); + + yunq::MessageView view(bytes, offset); // Parse address_phys. - set_address_phys(bytes.At(offset + header_size + (8 * 0))); + ASSIGN_OR_RETURN(address_phys_, view.ReadField(0)); // Parse width. - set_width(bytes.At(offset + header_size + (8 * 1))); + ASSIGN_OR_RETURN(width_, view.ReadField(1)); // Parse height. - set_height(bytes.At(offset + header_size + (8 * 2))); + ASSIGN_OR_RETURN(height_, view.ReadField(2)); // Parse pitch. - set_pitch(bytes.At(offset + header_size + (8 * 3))); + ASSIGN_OR_RETURN(pitch_, view.ReadField(3)); // Parse bpp. - set_bpp(bytes.At(offset + header_size + (8 * 4))); + ASSIGN_OR_RETURN(bpp_, view.ReadField(4)); // Parse memory_model. - set_memory_model(bytes.At(offset + header_size + (8 * 5))); + ASSIGN_OR_RETURN(memory_model_, view.ReadField(5)); // Parse red_mask_size. - set_red_mask_size(bytes.At(offset + header_size + (8 * 6))); + ASSIGN_OR_RETURN(red_mask_size_, view.ReadField(6)); // Parse red_mask_shift. - set_red_mask_shift(bytes.At(offset + header_size + (8 * 7))); + ASSIGN_OR_RETURN(red_mask_shift_, view.ReadField(7)); // Parse green_mask_size. - set_green_mask_size(bytes.At(offset + header_size + (8 * 8))); + ASSIGN_OR_RETURN(green_mask_size_, view.ReadField(8)); // Parse green_mask_shift. - set_green_mask_shift(bytes.At(offset + header_size + (8 * 9))); + ASSIGN_OR_RETURN(green_mask_shift_, view.ReadField(9)); // Parse blue_mask_size. - set_blue_mask_size(bytes.At(offset + header_size + (8 * 10))); + ASSIGN_OR_RETURN(blue_mask_size_, view.ReadField(10)); // Parse blue_mask_shift. - set_blue_mask_shift(bytes.At(offset + header_size + (8 * 11))); + ASSIGN_OR_RETURN(blue_mask_shift_, view.ReadField(11)); return glcr::Status::Ok(); } @@ -390,12 +394,13 @@ glcr::Status DenaliInfo::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t glcr::Status DenaliInfo::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); + + yunq::MessageView view(bytes, offset); // Parse denali_endpoint. - // Skip Cap. // Parse device_id. - set_device_id(bytes.At(offset + header_size + (8 * 1))); + ASSIGN_OR_RETURN(device_id_, view.ReadField(1)); // Parse lba_offset. - set_lba_offset(bytes.At(offset + header_size + (8 * 2))); + ASSIGN_OR_RETURN(lba_offset_, view.ReadField(2)); return glcr::Status::Ok(); } diff --git a/yunq/message.cpp.jinja b/yunq/message.cpp.jinja index 30f5556..42da094 100644 --- a/yunq/message.cpp.jinja +++ b/yunq/message.cpp.jinja @@ -1,6 +1,7 @@ // Generated file -- DO NOT MODIFY. #include "{{file}}.h" +#include #include {% if package != None %} @@ -48,21 +49,13 @@ glcr::Status {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uin glcr::Status {{message.name}}::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { RETURN_ERROR(yunq::CheckHeader(bytes, offset)); + yunq::MessageView view(bytes, offset); + {%- for field in message.fields %} // Parse {{field.name}}. {%- if not field.repeated %} -{%- if field.type == Type.U64 %} - set_{{field.name}}(bytes.At(offset + header_size + (8 * {{loop.index0}}))); -{%- elif field.type == Type.I64 %} - set_{{field.name}}(bytes.At(offset + header_size + (8 * {{loop.index0}}))); -{%- elif field.type == Type.STRING %} - auto {{field.name}}_pointer = bytes.At(offset + header_size + (8 * {{loop.index0}})); - - set_{{field.name}}(bytes.StringAt(offset + {{field.name}}_pointer.offset, {{field.name}}_pointer.length)); -{%- elif field.type == Type.CAPABILITY %} - // Skip Cap. -{%- else %} - // TODO: Unimplemented parsing {{field.type}} +{%- if field.type != Type.CAPABILITY %} + ASSIGN_OR_RETURN({{field.name}}_, view.ReadField<{{field.cpp_type()}}>({{loop.index0}})); {%- endif %} {%- else %} auto {{field.name}}_pointer = bytes.At(offset + header_size + (8 * {{loop.index0}}));