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

Commit 896a5f8e authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Generate ComandExpectations trait for nci packets" am: b724b574 am: 395f62bf

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/1957386

Change-Id: I5827b07cdd6c41fe584456ee5f60d413d3645582
parents 2ef7a9b8 395f62bf
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;
    }