Loading system/gd/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -507,6 +507,7 @@ rust_library { "libbytes", "libnum_traits", "libthiserror", "libbt_hci_custom_types", ], } Loading system/gd/packet/parser/fields/custom_field_fixed_size.cc +37 −0 Original line number Diff line number Diff line Loading @@ -31,6 +31,10 @@ std::string CustomFieldFixedSize::GetDataType() const { return type_name_; } std::string CustomFieldFixedSize::GetRustDataType() const { return type_name_; } int CustomFieldFixedSize::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const { if (!start_offset.empty()) { // Default to start if available. Loading Loading @@ -68,3 +72,36 @@ void CustomFieldFixedSize::GenStringRepresentation(std::ostream& s, std::string // We assume that custom fields will have a ToString() method s << accessor << ".ToString()"; } std::string CustomFieldFixedSize::GetRustParseDataType() const { return "[u8; " + std::to_string(GetSize().bytes()) + "]"; } void CustomFieldFixedSize::GenRustGetter(std::ostream& s, Size start_offset, Size end_offset) const { Size size = GetSize(); int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize()); if (num_leading_bits != 0) { ERROR(this) << "must be byte aligned"; } if (size.bits() % 8 != 0) { ERROR(this) << "size must be in full bytes"; } s << "let " << GetName() << " = bytes[" << start_offset.bytes() << ".."; s << start_offset.bytes() + size.bytes() << "].try_into().unwrap();"; } void CustomFieldFixedSize::GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const { Size size = GetSize(); int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize()); if (num_leading_bits != 0) { ERROR(this) << "must be byte aligned"; } if (size.bits() % 8 != 0) { ERROR(this) << "size must be in full bytes"; } s << "let " << GetName() << ": " << GetRustParseDataType() << " = self." << GetName() << ".into();"; s << "buffer[" << start_offset.bytes() << ".." << start_offset.bytes() + GetSize().bytes() << "].copy_from_slice(&" << GetName() << ");"; } system/gd/packet/parser/fields/custom_field_fixed_size.h +8 −0 Original line number Diff line number Diff line Loading @@ -43,5 +43,13 @@ class CustomFieldFixedSize : public ScalarField { virtual void GenStringRepresentation(std::ostream& s, std::string accessor) const override; void GenRustGetter(std::ostream& s, Size start_offset, Size end_offset) const override; void GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const override; virtual std::string GetRustDataType() const override; virtual std::string GetRustParseDataType() const override; std::string type_name_; }; system/gd/packet/parser/gen_rust.cc +0 −7 Original line number Diff line number Diff line Loading @@ -37,13 +37,6 @@ pub enum Error { InvalidPacketError } pub struct Address { pub addr: [u8; 6], } pub struct ClassOfDevice { pub cod: [u8; 3], } )"; } Loading system/gd/rust/hci/Android.bp +9 −0 Original line number Diff line number Diff line Loading @@ -18,8 +18,17 @@ rust_library { "libgddi", "liblog_rust", "libbt_common", "libbt_hci_custom_types", ], proc_macros: [ "libnum_derive", ], } rust_library { name: "libbt_hci_custom_types", defaults: ["gd_rust_defaults"], crate_name: "bt_hci_custom_types", srcs: ["custom_types/lib.rs"], edition: "2018", } Loading
system/gd/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -507,6 +507,7 @@ rust_library { "libbytes", "libnum_traits", "libthiserror", "libbt_hci_custom_types", ], } Loading
system/gd/packet/parser/fields/custom_field_fixed_size.cc +37 −0 Original line number Diff line number Diff line Loading @@ -31,6 +31,10 @@ std::string CustomFieldFixedSize::GetDataType() const { return type_name_; } std::string CustomFieldFixedSize::GetRustDataType() const { return type_name_; } int CustomFieldFixedSize::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const { if (!start_offset.empty()) { // Default to start if available. Loading Loading @@ -68,3 +72,36 @@ void CustomFieldFixedSize::GenStringRepresentation(std::ostream& s, std::string // We assume that custom fields will have a ToString() method s << accessor << ".ToString()"; } std::string CustomFieldFixedSize::GetRustParseDataType() const { return "[u8; " + std::to_string(GetSize().bytes()) + "]"; } void CustomFieldFixedSize::GenRustGetter(std::ostream& s, Size start_offset, Size end_offset) const { Size size = GetSize(); int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize()); if (num_leading_bits != 0) { ERROR(this) << "must be byte aligned"; } if (size.bits() % 8 != 0) { ERROR(this) << "size must be in full bytes"; } s << "let " << GetName() << " = bytes[" << start_offset.bytes() << ".."; s << start_offset.bytes() + size.bytes() << "].try_into().unwrap();"; } void CustomFieldFixedSize::GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const { Size size = GetSize(); int num_leading_bits = GetRustBitOffset(s, start_offset, end_offset, GetSize()); if (num_leading_bits != 0) { ERROR(this) << "must be byte aligned"; } if (size.bits() % 8 != 0) { ERROR(this) << "size must be in full bytes"; } s << "let " << GetName() << ": " << GetRustParseDataType() << " = self." << GetName() << ".into();"; s << "buffer[" << start_offset.bytes() << ".." << start_offset.bytes() + GetSize().bytes() << "].copy_from_slice(&" << GetName() << ");"; }
system/gd/packet/parser/fields/custom_field_fixed_size.h +8 −0 Original line number Diff line number Diff line Loading @@ -43,5 +43,13 @@ class CustomFieldFixedSize : public ScalarField { virtual void GenStringRepresentation(std::ostream& s, std::string accessor) const override; void GenRustGetter(std::ostream& s, Size start_offset, Size end_offset) const override; void GenRustWriter(std::ostream& s, Size start_offset, Size end_offset) const override; virtual std::string GetRustDataType() const override; virtual std::string GetRustParseDataType() const override; std::string type_name_; };
system/gd/packet/parser/gen_rust.cc +0 −7 Original line number Diff line number Diff line Loading @@ -37,13 +37,6 @@ pub enum Error { InvalidPacketError } pub struct Address { pub addr: [u8; 6], } pub struct ClassOfDevice { pub cod: [u8; 3], } )"; } Loading
system/gd/rust/hci/Android.bp +9 −0 Original line number Diff line number Diff line Loading @@ -18,8 +18,17 @@ rust_library { "libgddi", "liblog_rust", "libbt_common", "libbt_hci_custom_types", ], proc_macros: [ "libnum_derive", ], } rust_library { name: "libbt_hci_custom_types", defaults: ["gd_rust_defaults"], crate_name: "bt_hci_custom_types", srcs: ["custom_types/lib.rs"], edition: "2018", }