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

Commit 0131ce5d authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-packets: generate sample enums and make this work end to end

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: I9467b6871b3072a64d06bcd8b79887bc6e85c010
parent 6aea6071
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -448,6 +448,28 @@ genrule {
    ],
}

genrule {
    name: "BluetoothGeneratedPackets_rust",
    tools: [
        "bluetooth_packetgen",
    ],
    cmd: "$(location bluetooth_packetgen) --include=packages/modules/Bluetooth/system/gd --out=$(genDir) $(in) --rust",
    srcs: [
        "hci/hci_packets.pdl",
    ],
    out: [
        "hci/hci_packets.rs",
    ],
}

rust_library {
    name: "libbt_packets",
    crate_name: "bt_packets",
    srcs: ["rust/packets/lib.rs", ":BluetoothGeneratedPackets_rust"],
    edition: "2018",
    host_supported: true,
}

// Generates binary schema data to be bundled and source file generated
genrule {
    name: "BluetoothGeneratedDumpsysBinarySchema_bfbs",
+2 −2
Original line number Diff line number Diff line
@@ -4622,7 +4622,7 @@ packet VendorSpecificEvent : EventPacket (event_code = VENDOR_SPECIFIC) {
  _payload_,
}

enum qualityReportId : 8 {
enum QualityReportId : 8 {
  MONITOR_MODE = 0x01,
  APPROACH_LSTO = 0x02,
  A2DP_AUDIO_CHOPPY = 0x03,
@@ -4634,7 +4634,7 @@ enum qualityReportId : 8 {
}

packet BqrEvent : VendorSpecificEvent (subevent_code = BQR_EVENT) {
  quality_report_id : qualityReportId,
  quality_report_id : QualityReportId,
  _payload_,
}

+8 −0
Original line number Diff line number Diff line
@@ -59,3 +59,11 @@ void EnumGen::GenLogging(std::ostream& stream) {
  stream << "  return os << " << e_.name_ << "Text(param);";
  stream << "}\n";
}

void EnumGen::GenRustDef(std::ostream& stream) {
  stream << "pub enum " << e_.name_ << " {";
  for (const auto& pair : e_.constants_) {
    stream << util::ConstantCaseToCamelCase(pair.second) << " = 0x" << std::hex << pair.first << std::dec << ",";
  }
  stream << "}";
}
+2 −0
Original line number Diff line number Diff line
@@ -31,5 +31,7 @@ class EnumGen {

  void GenLogging(std::ostream& stream);

  void GenRustDef(std::ostream& stream);

  EnumDef e_;
};
+11 −2
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@
#include "declarations.h"

bool generate_rust_source_one_file(
    __attribute__((unused)) const Declarations& decls,
    const Declarations& decls,
    const std::filesystem::path& input_file,
    const std::filesystem::path& include_dir,
    const std::filesystem::path& out_dir,
@@ -43,7 +43,16 @@ bool generate_rust_source_one_file(
    return false;
  }

  out_file << "// @generated rust packets from " << input_file.filename().string();
  out_file << "// @generated rust packets from " << input_file.filename().string() << "\n\n";

  for (const auto& e : decls.type_defs_queue_) {
    if (e.second->GetDefinitionType() == TypeDef::Type::ENUM) {
      const auto* enum_def = dynamic_cast<const EnumDef*>(e.second);
      EnumGen gen(*enum_def);
      gen.GenRustDef(out_file);
      out_file << "\n\n";
    }
  }

  out_file.close();
  return true;
Loading