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

Commit 536f9e54 authored by Alisher Alikhodjaev's avatar Alisher Alikhodjaev
Browse files

Generate ComandExpectations trait for nci packets

For some reason only generates implementations for CommandResponse

Bug: 197333653
Test: build ok
Change-Id: Ibfce5159da439cca509985f2f6d4c6c0f8d208b1
parent 673f938b
Loading
Loading
Loading
Loading
+53 −0
Original line number Diff line number Diff line
@@ -136,6 +136,59 @@ bool generate_rust_source_one_file(
      }
    }

    if (opcode_index != nullptr && opcode != nullptr) {
      opcode_index->try_from_enum_ = opcode;
    }
  } else if (input_filename == "nci_packets") {
    out_file << "type EventPacket = NciPacket;"
             << "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");
      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_response = other_packet->HasAncestorNamed("Response");
        bool command_notification = other_packet->HasAncestorNamed("Notification");
        if (!command_response && !command_notification) {
          continue;
        }
        auto other_constraint = other_packet->parent_constraints_.find("cmd_op");
        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;
        }
      }
    }

    EnumDef* opcode = nullptr;
    EnumDef* opcode_index = nullptr;
    for (const auto& e : decls.type_defs_queue_) {
      if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) {
        auto* enum_def = static_cast<EnumDef*>(e.second);
        if (enum_def->name_ == "Opcode") {
          opcode = enum_def;
        } else if (enum_def->name_ == "OpCodeIndex") {
          opcode_index = enum_def;
        }
      }
    }

    if (opcode_index != nullptr && opcode != nullptr) {
      opcode_index->try_from_enum_ = opcode;
    }