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

Commit 150a8c45 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: drastically simplify command sending

include a trait during generation that knows how to
convert the event responses to the correct matching type

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: Icf96ce20c0b2311e94e5198c1cc4038f78683771
parent fdca3cac
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -74,6 +74,42 @@ bool generate_rust_source_one_file(
  out_file << "// @generated rust packets from " << input_file.filename().string() << "\n\n";

  generate_rust_packet_preamble(out_file);
  if (input_filename == "hci_packets") {
    out_file << "pub trait CommandExpectations { "
             << "type ResponseType;"
             << "fn _to_response_type(pkt: EventPacket) -> Self::ResponseType;"
             << "}";

    for (const auto& packet_def : decls.packet_defs_queue_) {
      auto packet = packet_def.second;
      if (!packet->HasAncestorNamed("Command")) {
        continue;
      }
      auto constraint = packet->parent_constraints_.find("op_code");
      if (constraint == packet->parent_constraints_.end()) {
        continue;
      }
      auto opcode = std::get<std::string>(constraint->second);
      for (const auto& other_packet_def : decls.packet_defs_queue_) {
        auto other_packet = other_packet_def.second;
        bool command_status = other_packet->HasAncestorNamed("CommandStatus");
        bool command_complete = other_packet->HasAncestorNamed("CommandComplete");
        if (!command_status && !command_complete) {
          continue;
        }
        auto other_constraint = other_packet->parent_constraints_.find("command_op_code");
        if (other_constraint == other_packet->parent_constraints_.end()) {
          continue;
        }

        auto other_opcode = std::get<std::string>(other_constraint->second);
        if (opcode == other_opcode) {
          packet->complement_ = other_packet;
          break;
        }
      }
    }
  }

  for (const auto& e : decls.type_defs_queue_) {
    if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) {
+11 −0
Original line number Diff line number Diff line
@@ -945,6 +945,17 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
}

void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
  if (complement_ != nullptr) {
    auto complement_root = complement_->GetRootDef();
    auto complement_root_accessor = util::CamelCaseToUnderScore(complement_root->name_);
    s << "impl CommandExpectations for " << name_ << "Packet {";
    s << " type ResponseType = " << complement_->name_ << "Packet;";
    s << " fn _to_response_type(pkt: EventPacket) -> Self::ResponseType { ";
    s << complement_->name_ << "Packet::new(pkt." << complement_root_accessor << ".clone())";
    s << " }";
    s << "}";
  }

  s << "impl " << name_ << "Packet {";
  if (parent_ == nullptr) {
    s << "pub fn parse(bytes: &[u8]) -> Result<Self> { ";
+11 −0
Original line number Diff line number Diff line
@@ -510,3 +510,14 @@ std::map<std::string, std::variant<int64_t, std::string>> ParentDef::GetAllConst
  }
  return res;
}

bool ParentDef::HasAncestorNamed(std::string name) const {
  auto parent = parent_;
  while (parent != nullptr) {
    if (parent->name_ == name) {
      return true;
    }
    parent = parent->parent_;
  }
  return false;
}
+4 −0
Original line number Diff line number Diff line
@@ -63,6 +63,8 @@ class ParentDef : public TypeDef {

  const ParentDef* GetRootDef() const;

  bool HasAncestorNamed(std::string name) const;

  std::map<std::string, std::variant<int64_t, std::string>> GetAllConstraints() const;

  std::vector<const ParentDef*> GetAncestors() const;
@@ -71,6 +73,8 @@ class ParentDef : public TypeDef {

  ParentDef* parent_{nullptr};

  ParentDef* complement_{nullptr};

  std::vector<ParentDef*> children_;

  std::map<std::string, std::variant<int64_t, std::string>> parent_constraints_;
+4 −4
Original line number Diff line number Diff line
@@ -58,8 +58,8 @@ impl HciLayerFacade for HciLayerFacadeService {
    ) {
        self.rt.block_on(
            self.hci_exports
                .enqueue_command_with_complete(hci::CommandPacket::parse(&data.take_payload()).unwrap()),
        );
                .send_raw(hci::CommandPacket::parse(&data.take_payload()).unwrap()),
        ).unwrap();
        sink.success(Empty::default());
    }

@@ -71,8 +71,8 @@ impl HciLayerFacade for HciLayerFacadeService {
    ) {
        self.rt.block_on(
            self.hci_exports
                .enqueue_command_with_complete(hci::CommandPacket::parse(&data.take_payload()).unwrap()),
        );
                .send_raw(hci::CommandPacket::parse(&data.take_payload()).unwrap()),
        ).unwrap();
        sink.success(Empty::default());
    }

Loading