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

Commit 944d52c0 authored by Henri Chataing's avatar Henri Chataing
Browse files

RootCanal: Migrate the pdl cxx generator to the new backend

Test: m root-canal
Bug: 284055183
Change-Id: I1e0b3a590ee5f8f072ea8ed3205546a805cfa173
parent 6ccf2639
Loading
Loading
Loading
Loading
+2 −8
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ cc_binary {
        "libcrypto",
        "libcutils",
        "libhidlbase",
        "libjsoncpp",
        "liblog",
        "libprotobuf-cpp-full",
        "libutils",
@@ -52,9 +51,6 @@ cc_binary {
    cflags: [
        "-fvisibility=hidden",
    ],
    generated_headers: [
        "libbt_init_flags_bridge_header",
    ],
    whole_static_libs: [
        "libbt-rootcanal",
    ],
@@ -64,6 +60,7 @@ cc_binary {
        "libscriptedbeaconpayload-protos-lite",
    ],
    include_dirs: [
        "packages/modules/Bluetooth/tools/rootcanal/include",
        "packages/modules/Bluetooth/system",
        "packages/modules/Bluetooth/system/gd",
        "packages/modules/Bluetooth/system/internal_include",
@@ -93,14 +90,10 @@ cc_library_shared {
        "libcrypto",
        "libcutils",
        "libhidlbase",
        "libjsoncpp",
        "liblog",
        "libprotobuf-cpp-full",
        "libutils",
    ],
    generated_headers: [
        "libbt_init_flags_bridge_header",
    ],
    whole_static_libs: [
        "libbt-rootcanal",
    ],
@@ -110,6 +103,7 @@ cc_library_shared {
        "libscriptedbeaconpayload-protos-lite",
    ],
    include_dirs: [
        "packages/modules/Bluetooth/tools/rootcanal/include",
        "packages/modules/Bluetooth/system",
        "packages/modules/Bluetooth/system/gd",
        "packages/modules/Bluetooth/system/internal_include",
+35 −15
Original line number Diff line number Diff line
@@ -47,13 +47,12 @@ cc_defaults {
        "-fvisibility=hidden",
    ],
    include_dirs: [
        "packages/modules/Bluetooth/tools/rootcanal/include",
        "packages/modules/Bluetooth/system/gd",
    ],
    local_include_dirs: [
        "include",
    ],
    header_libs: [
        "libbase_headers",
        "pdl_cxx_packet_runtime",
    ],
    generated_headers: [
        "rootcanal_bredr_bb_packets_cxx_gen",
@@ -81,7 +80,6 @@ cc_library_static {
    host_supported: true,
    proprietary: true,
    srcs: [
        ":BluetoothPacketSources",
        ":lib_sources",
        "model/controller/acl_connection.cc",
        "model/controller/acl_connection_handler.cc",
@@ -113,6 +111,9 @@ cc_library_static {
        "net/posix/posix_async_socket_connector.cc",
        "net/posix/posix_async_socket_server.cc",
    ],
    export_header_lib_headers: [
        "pdl_cxx_packet_runtime",
    ],
    export_include_dirs: [
        ".",
        "include",
@@ -149,7 +150,6 @@ cc_library_host_shared {
        address: false,
    },
    srcs: [
        ":BluetoothPacketSources",
        ":lib_sources",
        "model/controller/acl_connection.cc",
        "model/controller/acl_connection_handler.cc",
@@ -415,23 +415,36 @@ cc_binary_host {
genrule {
    name: "rootcanal_hci_packets_cxx_gen",
    tools: [
        "bluetooth_packetgen",
    ],
    cmd: "$(location bluetooth_packetgen) --include=packages/modules/Bluetooth/tools/rootcanal/packets --out=$(genDir) $(in)",
        ":pdlc",
        ":pdl_cxx_generator",
    ],
    cmd: "set -o pipefail;" +
        " $(location :pdlc) $(in) |" +
        " $(location :pdl_cxx_generator)" +
        " --namespace bluetooth::hci" +
        " --include-header hci/address.h" +
        " --output $(out)",
    srcs: [
        "packets/hci/hci_packets.pdl",
    ],
    out: [
        "hci/hci_packets.h",
        "packets/hci_packets.h",
    ],
}

genrule {
    name: "rootcanal_link_layer_packets_cxx_gen",
    tools: [
        "bluetooth_packetgen",
    ],
    cmd: "$(location bluetooth_packetgen) --root_namespace=model --include=packages/modules/Bluetooth/tools/rootcanal --out=$(genDir) $(in)",
        ":pdlc",
        ":pdl_cxx_generator",
    ],
    cmd: "set -o pipefail;" +
        " $(location :pdlc) $(in) |" +
        " $(location :pdl_cxx_generator)" +
        " --namespace model::packets" +
        " --include-header hci/address.h" +
        " --using-namespace bluetooth::hci" +
        " --output $(out)",
    srcs: [
        "packets/link_layer_packets.pdl",
    ],
@@ -443,9 +456,16 @@ genrule {
genrule {
    name: "rootcanal_bredr_bb_packets_cxx_gen",
    tools: [
        "bluetooth_packetgen",
    ],
    cmd: "$(location bluetooth_packetgen) --root_namespace=bredr_bb --include=packages/modules/Bluetooth/tools/rootcanal/packets --out=$(genDir) $(in)",
        ":pdlc",
        ":pdl_cxx_generator",
    ],
    cmd: "set -o pipefail;" +
        " $(location :pdlc) $(in) |" +
        " $(location :pdl_cxx_generator)" +
        " --namespace bredr_bb" +
        " --include-header hci/address.h" +
        " --using-namespace bluetooth::hci" +
        " --output $(out)",
    srcs: [
        "packets/bredr_bb.pdl",
    ],
+52 −117
Original line number Diff line number Diff line
@@ -28,7 +28,8 @@ android_license(
function(pdl_gen)
  # Parse arguments.
  set(options)
  set(oneValueArgs NAME INPUT OUTPUT LANG)
  set(oneValueArgs NAME INPUT OUTPUT LANG NAMESPACE)
  set(multiValueArgs USING INCLUDE)
  cmake_parse_arguments(pdl "${options}" "${oneValueArgs}"
                        "${multiValueArgs}" ${ARGN})

@@ -66,102 +67,62 @@ function(pdl_gen)
        VERBATIM
        DEPENDS pdlc ${pdl_INPUT_ABSOLUTE})
  endif()
endfunction()

# ~~~
# ! foobar : Compile .pdl files to C++/Rust!
#
# This function allows you to compile a set of .pdl files
# to rust or C++. It will generate a set of headers and
# sources.
#
# :GENERATED:  The set of sources that are generated.
# Add these sources to the library you are building
# :SRC:        The set of .pdl files to be compiled.
# :INCLUDES:   Include directory used when generating sources.
# :LANG:       Optional parameter indicating which language to use,
# either rust or c++, defaults to c++
# :NAMESPACE:  Root name space to use for the generated c++ classes.
# :OUTPUT_DIR: Optional paramater of the directory where the generated
# sources will be placed, defaults to CMAKE_CURRENT_BINARY_DIR/gens
# :SOURCE_DIR: Root directory where sources can be found,
# defaults to CMAKE_CURRENT_SOURCE_DIR
# ~~~
function(android_bluetooth_packet_gen)
  # Parse arguments
  set(options)
  set(oneValueArgs OUTPUT_DIR GENERATED SOURCE_DIR INCLUDES NAMESPACE LANG)
  set(multiValueArgs SRC)
  cmake_parse_arguments(packet_gen "${options}" "${oneValueArgs}"
                        "${multiValueArgs}" ${ARGN})

  if(NOT packet_gen_OUTPUT_DIR)
    set(packet_gen_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/packet_gen)
  if(pdl_LANG STREQUAL "c++")
    if(NOT pdl_NAMESPACE)
        message(FATAL_ERROR "Error: namespace not specified")
    endif()

  if(packet_gen_NAMESPACE)
    set(packet_gen_NAMESPACE "--root_namespace=${packet_gen_NAMESPACE}")
  endif()

  if(NOT packet_gen_SOURCE_DIR)
    set(packet_gen_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  endif()

  if(NOT packet_gen_SRC)
    message(
      FATAL_ERROR
        "Error: android_packet_gen_compile() called without any .yy files")
    return()
  endif()

  set(SUFFIX_GEN "h")

  if(packet_gen_LANG STREQUAL "rust")
    set(SUFFIX_GEN "rs")
    set(packet_gen_LANG "--rust")
  endif()

  # Configure packet_gen
  android_compile_for_host(
    bluetooth_packetgen ${ANDROID_QEMU2_TOP_DIR}/android/bluetooth/packet_gen
    bluetooth_packetgen_EXECUTABLE)

  set(BLUE_GEN "")
  file(MAKE_DIRECTORY ${packet_gen_OUTPUT_DIR})
    foreach(namespace ${pdl_USING})
        list(APPEND pdl_FLAGS --using-namespace)
        list(APPEND pdl_FLAGS ${namespace})
    endforeach()
    foreach(header ${pdl_INCLUDE})
        list(APPEND pdl_FLAGS --include-header)
        list(APPEND pdl_FLAGS ${header})
    endforeach()

  foreach(FIL ${packet_gen_SRC})
    get_filename_component(
      ABS_FIL ${packet_gen_SOURCE_DIR}/${packet_gen_INCLUDES}/${FIL} ABSOLUTE)
    get_filename_component(FIL_WE ${FIL} NAME_WE)
    get_filename_component(FIL_DIR ${FIL} DIRECTORY)
    set(FIL_GEN "${packet_gen_OUTPUT_DIR}/${FIL_DIR}/${FIL_WE}.${SUFFIX_GEN}")
    add_custom_command(
      OUTPUT "${FIL_GEN}"
        OUTPUT "${pdl_OUTPUT_ABSOLUTE}.json"
        COMMAND
        ${bluetooth_packetgen_EXECUTABLE} ${packet_gen_NAMESPACE}
        "--include=${packet_gen_INCLUDES}" "--out=${packet_gen_OUTPUT_DIR}"
        ${packet_gen_INCLUDES}/${FIL} ${packet_gen_LANG}
      COMMENT "Creating bluetooth packet headers from ${ABS_FIL}"
      WORKING_DIRECTORY ${packet_gen_SOURCE_DIR}
        ${pdlc_EXECUTABLE}
                --output-format json
                "${pdl_INPUT_ABSOLUTE}"
                    > "${pdl_OUTPUT_ABSOLUTE}.json"
        COMMENT "Analyzing ${pdl_INPUT}"
        VERBATIM
      DEPENDS ${bluetooth_packetgen_EXECUTABLE} ${ABS_FIL})
    list(APPEND BLUE_GEN ${FIL_GEN})
    set_source_files_properties(${FIL_GEN} PROPERTIES GENERATED TRUE)
  endforeach()
        DEPENDS pdlc ${pdl_INPUT_ABSOLUTE})

  # Make the library available
  if(packet_gen_GENERATED)
    set(${packet_gen_GENERATED} "${BLUE_GEN}" PARENT_SCOPE)
    add_custom_command(
        OUTPUT "${pdl_OUTPUT_ABSOLUTE}"
        COMMAND
        ${PDL_ROOT}/scripts/generate_cxx_backend.py
                --input "${pdl_OUTPUT_ABSOLUTE}.json"
                --output "${pdl_OUTPUT_ABSOLUTE}"
                --namespace ${pdl_NAMESPACE}
                ${pdl_FLAGS}
        COMMENT "Generating c++ header from ${pdl_INPUT}"
        VERBATIM
        DEPENDS pdlc ${pdl_OUTPUT_ABSOLUTE}.json)
  endif()
endfunction()

android_bluetooth_packet_gen(
  GENERATED BluetoothGeneratedPackets_h INCLUDES tools/rootcanal/packets
  SRC hci/hci_packets.pdl SOURCE_DIR ${BT_ROOT}/..)

android_bluetooth_packet_gen(
  GENERATED RootCanalGeneratedPackets_h INCLUDES tools/rootcanal NAMESPACE model
  SRC packets/link_layer_packets.pdl SOURCE_DIR ${BT_ROOT}/..)
pdl_gen(
  NAME BluetoothGeneratedPackets_h
  INPUT ${ROOTCANAL_ROOT}/packets/hci/hci_packets.pdl
  OUTPUT packets/hci_packets.h
  LANG c++
  NAMESPACE "bluetooth::hci"
  INCLUDE "hci/address.h")

pdl_gen(
  NAME RootCanalGeneratedPackets_h
  INPUT ${ROOTCANAL_ROOT}/packets/link_layer_packets.pdl
  OUTPUT packets/link_layer_packets.h
  LANG c++
  NAMESPACE model::packets
  INCLUDE "hci/address.h"
  USING "bluetooth::hci")

android_add_library(
  TARGET libscriptedbeaconpayload-protos-lite LICENSE Apache-2.0
@@ -219,30 +180,6 @@ target_include_directories(
    ${ROOTCANAL_ROOT}/include
    ${AOSP_ROOT}/external/fmtlib/include)

android_add_library(
  TARGET BluetoothPacketSources
  LICENSE Apache-2.0
  SOURCE_DIR ${BT_ROOT}/gd/packet
  SRC bit_inserter.cc
      byte_inserter.cc
      byte_observer.cc
      fragmenting_inserter.cc
      iterator.cc
      packet_view.cc
      raw_builder.cc
      view.cc
  DEPS android-emu-base android-emu-base-headers librootcanal_log)

target_include_directories(
  BluetoothPacketSources PUBLIC
    ${ROOTCANAL_ROOT}
    ${ROOTCANAL_ROOT}/include
    ${BT_ROOT}/gd
    ${BT_ROOT})

target_compile_options(BluetoothPacketSources
                       PUBLIC -Wno-inconsistent-missing-override)

android_add_library(
  TARGET libbt-rootcanal
  LICENSE Apache-2.0
@@ -286,7 +223,6 @@ android_add_library(
  DEPS android-emu-base
       android-emu-base-headers
       android-emu-base-logging
       BluetoothPacketSources
       crypto
       librootcanal_config
       libscriptedbeaconpayload-protos-lite)
@@ -299,9 +235,8 @@ target_include_directories(
  libbt-rootcanal
  PUBLIC ${ROOTCANAL_ROOT}/include
         ${ROOTCANAL_ROOT}
         ${BT_ROOT}/gd
         ${BT_ROOT}
         ${CMAKE_CURRENT_BINARY_DIR}/packet_gen
         ${PDL_ROOT}/scripts
         ${CMAKE_CURRENT_BINARY_DIR}/pdl_gen
         ${CMAKE_CURRENT_BINARY_DIR}/config)

target_compile_options(libbt-rootcanal
+15 −7
Original line number Diff line number Diff line
@@ -16,19 +16,20 @@

#pragma once

#include <packet_runtime.h>

#include <array>
#include <cstring>
#include <initializer_list>
#include <optional>
#include <ostream>
#include <string>

#include "packet/custom_field_fixed_size_interface.h"
#include <vector>

namespace bluetooth {
namespace hci {

class Address final : public packet::CustomFieldFixedSizeInterface<Address> {
class Address final : public pdl::packet::Builder {
 public:
  static constexpr size_t kLength = 6;

@@ -43,10 +44,6 @@ class Address final : public packet::CustomFieldFixedSizeInterface<Address> {
  Address(const uint8_t (&address)[kLength]);
  Address(std::initializer_list<uint8_t> l);

  // CustomFieldFixedSizeInterface methods
  inline uint8_t* data() override { return address.data(); }
  inline const uint8_t* data() const override { return address.data(); }

  // storage::Serializable methods
  std::string ToString() const;
  static std::optional<Address> FromString(const std::string& from);
@@ -59,6 +56,17 @@ class Address final : public packet::CustomFieldFixedSizeInterface<Address> {
  bool operator!=(const Address& rhs) const { return !(*this == rhs); }

  bool IsEmpty() const { return *this == kEmpty; }
  uint8_t* data() { return address.data(); }
  uint8_t const* data() const { return address.data(); }

  // Packet parser interface.
  static bool Parse(pdl::packet::slice& input, Address* output);

  // Packet builder interface.
  size_t GetSize() const override { return kLength; }
  void Serialize(std::vector<uint8_t>& output) const override {
    output.insert(output.end(), address.begin(), address.end());
  }

  // Converts |string| to Address and places it in |to|. If |from| does
  // not represent a Bluetooth address, |to| is not modified and this function
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@

#include "crypto/crypto.h"
#include "hci/address.h"
#include "hci/hci_packets.h"
#include "packets/hci_packets.h"

namespace bluetooth {
namespace hci {
Loading