Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7620c00d authored by Zach Johnson's avatar Zach Johnson Committed by Gerrit Code Review
Browse files

Merge changes Iec2fb8b7,If7eabc0b,I759d041c

* changes:
  rusty-gd: make sure to notify HCI shim on stack start
  rusty-gd: handle padding in sizes
  rusty-gd: give special treament to unconstrained only-children
parents b06dca96 644470e6
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -159,9 +159,6 @@ bool generate_rust_source_one_file(
  }

  for (const auto& packet_def : decls.packet_defs_queue_) {
    if (packet_def.second->name_.rfind("LeGetVendorCapabilitiesComplete", 0) == 0) {
      continue;
    }
    packet_def.second->GenRustDef(out_file);
    out_file << "\n\n";
  }
+17 −27
Original line number Diff line number Diff line
@@ -746,9 +746,6 @@ void PacketDef::GenRustChildEnums(std::ostream& s) const {
    s << "#[derive(Debug)] ";
    s << "enum " << name_ << "DataChild {";
    for (const auto& child : children_) {
      if (child->name_.rfind("LeGetVendorCapabilitiesComplete", 0) == 0) {
        continue;
      }
      s << child->name_ << "(Arc<" << child->name_ << "Data>),";
    }
    if (payload) {
@@ -761,9 +758,6 @@ void PacketDef::GenRustChildEnums(std::ostream& s) const {
    s << "fn get_total_size(&self) -> usize {";
    s << "match self {";
    for (const auto& child : children_) {
      if (child->name_.rfind("LeGetVendorCapabilitiesComplete", 0) == 0) {
        continue;
      }
      s << name_ << "DataChild::" << child->name_ << "(value) => value.get_total_size(),";
    }
    if (payload) {
@@ -777,9 +771,6 @@ void PacketDef::GenRustChildEnums(std::ostream& s) const {
    s << "#[derive(Debug)] ";
    s << "pub enum " << name_ << "Child {";
    for (const auto& child : children_) {
      if (child->name_.rfind("LeGetVendorCapabilitiesComplete", 0) == 0) {
        continue;
      }
      s << child->name_ << "(" << child->name_ << "Packet),";
    }
    if (payload) {
@@ -889,12 +880,11 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
  s << "}";

  // parse function
  if (parent_constraints_.empty() && !children_.empty() && parent_ != nullptr) {
  if (parent_constraints_.empty() && children_.size() > 1 && parent_ != nullptr) {
    auto constraint = FindConstraintField();
    auto constraint_field = GetParamList().GetField(constraint);
    auto constraint_type = constraint_field->GetRustDataType();
      s << "fn parse(bytes: &[u8], " << constraint << ": " << constraint_type
          << ") -> Result<Self> {";
    s << "fn parse(bytes: &[u8], " << constraint << ": " << constraint_type << ") -> Result<Self> {";
  } else {
    s << "fn parse(bytes: &[u8]) -> Result<Self> {";
  }
@@ -928,13 +918,10 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
  auto constraint_name = FindConstraintField();
  auto constrained_descendants = FindDescendantsWithConstraint(constraint_name);

  if (!children_.empty()) {
  if (children_.size() > 1) {
    s << "let child = match " << constraint_name << " {";

    for (const auto& desc : constrained_descendants) {
      if (desc.first->name_.rfind("LeGetVendorCapabilitiesComplete", 0) == 0) {
        continue;
      }
      auto desc_path = FindPathToDescendant(desc.first->name_);
      std::reverse(desc_path.begin(), desc_path.end());
      auto constraint_field = GetParamList().GetField(constraint_name);
@@ -970,6 +957,15 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
    }

    s << "};\n";
  } else if (children_.size() == 1) {
    auto child = children_.at(0);
    s << "let child = match " << child->name_ << "Data::parse(&bytes[..]) {";
    s << " Ok(c) if " << child->name_ << "Data::conforms(&bytes[..]) => {";
    s << name_ << "DataChild::" << child->name_ << "(Arc::new(c))";
    s << " },";
    s << " Err(Error::InvalidLengthError { .. }) => " << name_ << "DataChild::None,";
    s << " _ => return Err(Error::InvalidPacketError),";
    s << "};";
  } else if (fields_.HasPayload()) {
    s << "let child = if payload.len() > 0 {";
    s << name_ << "DataChild::Payload(Bytes::from(payload))";
@@ -1010,9 +1006,6 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
  if (HasChildEnums()) {
    s << "match &self.child {";
    for (const auto& child : children_) {
      if (child->name_.rfind("LeGetVendorCapabilitiesComplete", 0) == 0) {
        continue;
      }
      s << name_ << "DataChild::" << child->name_ << "(value) => value.write_to(buffer),";
    }
    if (fields_.HasPayload()) {
@@ -1040,7 +1033,7 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
}

void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
  if (complement_ != nullptr && complement_->name_.rfind("LeGetVendorCapabilitiesComplete", 0) != 0) {
  if (complement_ != nullptr) {
    auto complement_root = complement_->GetRootDef();
    auto complement_root_accessor = util::CamelCaseToUnderScore(complement_root->name_);
    s << "impl CommandExpectations for " << name_ << "Packet {";
@@ -1076,9 +1069,6 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
    s << " pub fn specialize(&self) -> " << name_ << "Child {";
    s << " match &self." << util::CamelCaseToUnderScore(name_) << ".child {";
    for (const auto& child : children_) {
      if (child->name_.rfind("LeGetVendorCapabilitiesComplete", 0) == 0) {
        continue;
      }
      s << name_ << "DataChild::" << child->name_ << "(_) => " << name_ << "Child::" << child->name_ << "("
        << child->name_ << "Packet::new(self." << root_accessor << ".clone())),";
    }
@@ -1152,7 +1142,7 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
}

void PacketDef::GenRustBuilderStructImpls(std::ostream& s) const {
  if (complement_ != nullptr && complement_->name_.rfind("LeGetVendorCapabilitiesComplete", 0) != 0) {
  if (complement_ != nullptr) {
    auto complement_root = complement_->GetRootDef();
    auto complement_root_accessor = util::CamelCaseToUnderScore(complement_root->name_);
    s << "impl CommandExpectations for " << name_ << "Builder {";
+32 −14
Original line number Diff line number Diff line
@@ -650,23 +650,41 @@ void ParentDef::GenSizeRetVal(std::ostream& s) const {
  auto fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
  });
  s << "let ret = 0;";
  for (int i = 0; i < fields.size(); i++) {
    size += fields[i]->GetSize().bits();
  }
    auto field = fields[i];
    bool is_padding = field->GetFieldType() == PaddingField::kFieldType;
    bool is_vector = field->GetFieldType() == VectorField::kFieldType;
    if (is_padding || is_vector) {
      if (size > 0) {
        if (size % 8 != 0) {
          ERROR() << "size is not a multiple of 8!\n";
        }
  s << size / 8;
        s << "let ret = ret + " << size / 8 << ";";
        size = 0;
      }

  fields = fields_.GetFieldsWithTypes({
      VectorField::kFieldType,
  });
  for (int i = 0; i < fields.size(); i++) {
    const VectorField* vector = (VectorField*)fields[i];
      if (is_vector) {
        const VectorField* vector = (VectorField*)field;
        if (vector->element_size_.empty() || vector->element_size_.has_dynamic()) {
      s << " + self." << vector->GetName() << ".iter().fold(0, |acc, x| acc + x.get_total_size())";
          s << "let ret = ret + self." << vector->GetName() << ".iter().fold(0, |acc, x| acc + x.get_total_size());";
        } else {
          s << "let ret = ret + (self." << vector->GetName() << ".len() * ((" << vector->element_size_ << ") / 8));";
        }
      } else {
      s << " + (self." << vector->GetName() << ".len() * ((" << vector->element_size_ << ") / 8))";
        auto padded = field->GetSize().bytes();
        s << "let ret = if ret < " << padded << " { " << padded << " } else { ret };";
      }
    } else {
      size += field->GetSize().bits();
    }
  }
  if (size > 0) {
    if (size % 8 != 0) {
      ERROR() << "size is not a multiple of 8!\n";
    }
    s << "let ret = ret + " << size / 8 << ";";
  }

  s << "ret";
}
+1 −1
Original line number Diff line number Diff line
@@ -224,7 +224,7 @@ async fn dispatch(
                                }
                            },
                            None if code == EventCode::NumberOfCompletedPackets =>{},
                            None => panic!("Unhandled subevent {:?}", code),
                            None => panic!("Unhandled event {:?}", code),
                        }
                    },
                }
+1 −0
Original line number Diff line number Diff line
@@ -81,6 +81,7 @@ void Stack::StartEverything() {
      rust_controller_ = new ::rust::Box<rust::Controller>(
          rust::get_controller(**rust_stack_));
    }
    bluetooth::shim::hci_on_reset_complete();
    return;
  }