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

Commit 7104087b authored by Henri Chataing's avatar Henri Chataing
Browse files

RootCanal: Migrate to :pdl for the generation of rust lmp backend

Bug: 279169336
Test: atest liblmp_tests
Change-Id: Iab1c8974876a060169296b57e370da21c3846caa
parent 5eb26a74
Loading
Loading
Loading
Loading
+48 −1
Original line number Original line Diff line number Diff line
@@ -8,6 +8,9 @@ corrosion_import_crate(


corrosion_set_env_vars(generate-canonical-tests CARGO_HOME=${Rust_CARGO_HOME})
corrosion_set_env_vars(generate-canonical-tests CARGO_HOME=${Rust_CARGO_HOME})
corrosion_set_env_vars(pdl CARGO_HOME=${Rust_CARGO_HOME})
corrosion_set_env_vars(pdl CARGO_HOME=${Rust_CARGO_HOME})
corrosion_set_hostbuild(pdl)

get_property(pdl_EXECUTABLE TARGET pdl PROPERTY IMPORTED_LOCATION_RELEASE)


# These tests depend on the tempfile crate which was not imported because
# These tests depend on the tempfile crate which was not imported because
# the crate remove_dir_all does not have a compatible version.
# the crate remove_dir_all does not have a compatible version.
@@ -15,12 +18,56 @@ set_tests_properties(cargo-test_pdl PROPERTIES DISABLED True)
set_tests_properties(cargo-test_generate-canonical-tests PROPERTIES DISABLED True)
set_tests_properties(cargo-test_generate-canonical-tests PROPERTIES DISABLED True)


android_license(
android_license(
    TARGET "pdl"
    TARGET pdl
    LIBNAME None
    LIBNAME None
    SPDX None
    SPDX None
    LICENSE None
    LICENSE None
    LOCAL None)
    LOCAL None)


# Generate the Rust/C++ backend for a .pdl specification file.
function(pdl_gen)
  # Parse arguments.
  set(options)
  set(oneValueArgs NAME INPUT OUTPUT LANG)
  cmake_parse_arguments(pdl "${options}" "${oneValueArgs}"
                        "${multiValueArgs}" ${ARGN})

  if(NOT pdl_NAME)
    message(FATAL_ERROR "Error: name not specified")
  endif()

  if(NOT pdl_INPUT)
    message(FATAL_ERROR "Error: output file not specified")
  endif()

  if(NOT pdl_OUTPUT)
    message(FATAL_ERROR "Error: output file not specified")
  endif()

  set(pdl_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/pdl_gen)
  set(pdl_OUTPUT "${pdl_OUTPUT_DIR}/${pdl_OUTPUT}")

  get_filename_component(pdl_INPUT_ABSOLUTE ${pdl_INPUT} ABSOLUTE)
  get_filename_component(pdl_OUTPUT_ABSOLUTE ${pdl_OUTPUT} ABSOLUTE)
  get_filename_component(pdl_OUTPUT_DIR ${pdl_OUTPUT_ABSOLUTE} DIRECTORY)
  set(${pdl_NAME} "${pdl_OUTPUT_ABSOLUTE}" PARENT_SCOPE)

  file(MAKE_DIRECTORY ${pdl_OUTPUT_DIR})

  if(pdl_LANG STREQUAL "rust")
    add_custom_command(
        OUTPUT "${pdl_OUTPUT_ABSOLUTE}"
        COMMAND
        ${pdl_EXECUTABLE}
                --output-format rust
                "${pdl_INPUT_ABSOLUTE}"
                    > "${pdl_OUTPUT_ABSOLUTE}"
        COMMENT "Generating rust module from ${pdl_INPUT}"
        VERBATIM
        DEPENDS pdl ${pdl_INPUT_ABSOLUTE})
  endif()
endfunction()

# ~~~
# ~~~
# ! foobar : Compile .pdl files to C++/Rust!
# ! foobar : Compile .pdl files to C++/Rust!
#
#
+3 −10
Original line number Original line Diff line number Diff line
@@ -34,16 +34,9 @@ rust_ffi {


genrule {
genrule {
    name: "LmpGeneratedPackets_rust",
    name: "LmpGeneratedPackets_rust",
    tools: [
    defaults: ["pdl_rust_generator_defaults"],
        "bluetooth_packetgen",
    srcs: ["lmp_packets.pdl"],
    ],
    out: ["lmp_packets.rs"],
    cmd: "$(location bluetooth_packetgen) --include=packages/modules/Bluetooth/tools/rootcanal/lmp  --out=$(genDir) $(in) --rust",
    srcs: [
        "lmp_packets.pdl",
    ],
    out: [
        "lmp_packets.rs",
    ],
}
}


rust_test_host {
rust_test_host {
+15 −10
Original line number Original line Diff line number Diff line
message(STATUS "Enabling bluetooth LMP module.")
message(STATUS "Enabling bluetooth LMP module.")


# TODO(b/279494407): migrate hci packets to new generator
android_bluetooth_packet_gen(
android_bluetooth_packet_gen(
  GENERATED HciGeneratedPackets_rs INCLUDES gd SRC hci/hci_packets.pdl
    GENERATED hci_packets_rs INCLUDES packets SRC hci/hci_packets.pdl
  SOURCE_DIR ${BT_ROOT} LANG rust)

android_bluetooth_packet_gen(
  GENERATED LmpGeneratedPackets_rs INCLUDES lmp SRC lmp_packets.pdl
    SOURCE_DIR ${ROOTCANAL_ROOT} LANG rust)
    SOURCE_DIR ${ROOTCANAL_ROOT} LANG rust)

pdl_gen(
    NAME lmp_packets_rs
    INPUT ${ROOTCANAL_ROOT}/lmp/lmp_packets.pdl
    OUTPUT lmp_packets.rs
    LANG rust)

# This will automatically register all the tests as well.
# This will automatically register all the tests as well.
corrosion_import_crate(MANIFEST_PATH ${ROOTCANAL_ROOT}/lmp/Cargo.toml FLAGS
corrosion_import_crate(MANIFEST_PATH ${ROOTCANAL_ROOT}/lmp/Cargo.toml FLAGS
                       --offline --verbose --verbose)
                       --offline --verbose --verbose)
corrosion_set_env_vars(lmp
corrosion_set_env_vars(lmp
  LMP_PACKETS_PREBUILT=${LmpGeneratedPackets_rs}
  LMP_PACKETS_PREBUILT=${lmp_packets_rs}
  HCI_PACKETS_PREBUILT=${HciGeneratedPackets_rs}
  HCI_PACKETS_PREBUILT=${hci_packets_rs}
  CARGO_HOME=${Rust_CARGO_HOME})
  CARGO_HOME=${Rust_CARGO_HOME})

android_license(TARGET "lmp" LIBNAME None SPDX None LICENSE None LOCAL None)
android_license(TARGET "lmp" LIBNAME None SPDX None LICENSE None LOCAL None)


add_custom_target(lmp_rust_packets DEPENDS ${LmpGeneratedPackets_rs}
add_custom_target(lmp_rust_packets DEPENDS ${lmp_packets_rs}
                                           ${HciGeneratedPackets_rs})
                                           ${hci_packets_rs})


# Make sure we have the rust packets generated before we build them.
# Make sure we have the rust packets generated before we build them.
add_dependencies(cargo-build_lmp lmp_rust_packets)
add_dependencies(cargo-build_lmp lmp_rust_packets)


# Enable lmp in rootcanal
# Enable lmp in rootcanal.
target_link_libraries(libbt-rootcanal PUBLIC lmp)
target_link_libraries(libbt-rootcanal PUBLIC lmp)
target_include_directories(libbt-rootcanal
target_include_directories(libbt-rootcanal
    PUBLIC ${ROOTCANAL_ROOT}/lmp/include)
    PUBLIC ${ROOTCANAL_ROOT}/lmp/include)
+17 −17
Original line number Original line Diff line number Diff line
@@ -95,23 +95,23 @@ enum ExtendedOpcode : 8 {
  SAM_SWITCH = 37,
  SAM_SWITCH = 37,
}
}


packet Packet {
packet LmpPacket {
  transaction_id: 1,
  transaction_id: 1,
  opcode: Opcode,
  opcode: Opcode,
  _payload_,
  _payload_,
}
}


packet ExtendedPacket : Packet(opcode = ESCAPED) {
packet ExtendedPacket : LmpPacket(opcode = ESCAPED) {
  extended_opcode: ExtendedOpcode,
  extended_opcode: ExtendedOpcode,
  _payload_,
  _payload_,
}
}


packet Accepted : Packet(opcode = ACCEPTED) {
packet Accepted : LmpPacket(opcode = ACCEPTED) {
  accepted_opcode: Opcode,
  accepted_opcode: Opcode,
  _fixed_ = 0 : 1,
  _fixed_ = 0 : 1,
}
}


packet NotAccepted : Packet(opcode = NOT_ACCEPTED) {
packet NotAccepted : LmpPacket(opcode = NOT_ACCEPTED) {
  not_accepted_opcode: Opcode,
  not_accepted_opcode: Opcode,
  _fixed_ = 0 : 1,
  _fixed_ = 0 : 1,
  error_code: 8,
  error_code: 8,
@@ -138,33 +138,33 @@ packet IoCapabilityRes : ExtendedPacket(extended_opcode = IO_CAPABILITY_RES) {
  authentication_requirement: 8,
  authentication_requirement: 8,
}
}


packet EncapsulatedHeader : Packet(opcode = ENCAPSULATED_HEADER) {
packet EncapsulatedHeader : LmpPacket(opcode = ENCAPSULATED_HEADER) {
  major_type: 8,
  major_type: 8,
  minor_type: 8,
  minor_type: 8,
  payload_length: 8,
  payload_length: 8,
}
}


packet EncapsulatedPayload : Packet(opcode = ENCAPSULATED_PAYLOAD) {
packet EncapsulatedPayload : LmpPacket(opcode = ENCAPSULATED_PAYLOAD) {
  data: 8[16],
  data: 8[16],
}
}


packet SimplePairingConfirm : Packet(opcode = SIMPLE_PAIRING_CONFIRM) {
packet SimplePairingConfirm : LmpPacket(opcode = SIMPLE_PAIRING_CONFIRM) {
  commitment_value: 8[16],
  commitment_value: 8[16],
}
}


packet SimplePairingNumber : Packet(opcode = SIMPLE_PAIRING_NUMBER) {
packet SimplePairingNumber : LmpPacket(opcode = SIMPLE_PAIRING_NUMBER) {
  nonce: 8[16],
  nonce: 8[16],
}
}


packet DhkeyCheck : Packet(opcode = DHKEY_CHECK) {
packet DhkeyCheck : LmpPacket(opcode = DHKEY_CHECK) {
  confirmation_value: 8[16],
  confirmation_value: 8[16],
}
}


packet AuRand : Packet(opcode = AU_RAND) {
packet AuRand : LmpPacket(opcode = AU_RAND) {
  random_number: 8[16],
  random_number: 8[16],
}
}


packet Sres : Packet(opcode = SRES) {
packet Sres : LmpPacket(opcode = SRES) {
  authentication_rsp: 8[4],
  authentication_rsp: 8[4],
}
}


@@ -176,27 +176,27 @@ packet KeypressNotification: ExtendedPacket(extended_opcode = KEYPRESS_NOTIFICAT
  notification_type: 8,
  notification_type: 8,
}
}


packet InRand : Packet(opcode = IN_RAND) {
packet InRand : LmpPacket(opcode = IN_RAND) {
  random_number: 8[16],
  random_number: 8[16],
}
}


packet CombKey : Packet(opcode = COMB_KEY) {
packet CombKey : LmpPacket(opcode = COMB_KEY) {
  random_number: 8[16],
  random_number: 8[16],
}
}


packet EncryptionModeReq : Packet(opcode = ENCRYPTION_MODE_REQ) {
packet EncryptionModeReq : LmpPacket(opcode = ENCRYPTION_MODE_REQ) {
  encryption_mode: 8,
  encryption_mode: 8,
}
}


packet EncryptionKeySizeReq : Packet(opcode = ENCRYPTION_KEY_SIZE_REQ) {
packet EncryptionKeySizeReq : LmpPacket(opcode = ENCRYPTION_KEY_SIZE_REQ) {
  key_size: 8,
  key_size: 8,
}
}


packet StartEncryptionReq : Packet(opcode = START_ENCRYPTION_REQ) {
packet StartEncryptionReq : LmpPacket(opcode = START_ENCRYPTION_REQ) {
  random_number: 8[16]
  random_number: 8[16]
}
}


packet StopEncryptionReq : Packet(opcode = STOP_ENCRYPTION_REQ) {}
packet StopEncryptionReq : LmpPacket(opcode = STOP_ENCRYPTION_REQ) {}


packet FeaturesReqExt : ExtendedPacket(extended_opcode = FEATURES_REQ) {
packet FeaturesReqExt : ExtendedPacket(extended_opcode = FEATURES_REQ) {
  features_page: 8,
  features_page: 8,
+1 −1
Original line number Original line Diff line number Diff line
@@ -31,5 +31,5 @@ macro_rules! impl_try_from {
    };
    };
}
}


impl_try_from!(lmp::PacketPacket);
impl_try_from!(lmp::LmpPacket);
impl_try_from!(hci::CommandPacket);
impl_try_from!(hci::CommandPacket);
Loading