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

Commit e601448e authored by Henri Chataing's avatar Henri Chataing Committed by Automerger Merge Worker
Browse files

Merge "RootCanal: Migrate packets::hci to the new PDL generator" am: 05d6487d

parents ae15d6d9 05d6487d
Loading
Loading
Loading
Loading
+3 −10
Original line number Diff line number Diff line
@@ -436,16 +436,9 @@ genrule {

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

// bt_vhci_forwarder in cuttlefish depends on this H4Packetizer implementation.
+5 −4
Original line number Diff line number Diff line
message(STATUS "Enabling bluetooth LMP module.")

# TODO(b/279494407): migrate hci packets to new generator
android_bluetooth_packet_gen(
    GENERATED hci_packets_rs INCLUDES packets SRC hci/hci_packets.pdl
    SOURCE_DIR ${ROOTCANAL_ROOT} LANG rust)
pdl_gen(
    NAME hci_packets_rs
    INPUT ${ROOTCANAL_ROOT}/packets/hci/hci_packets.pdl
    OUTPUT hci_packets.rs
    LANG rust)

pdl_gen(
    NAME lmp_packets_rs
+18 −19
Original line number Diff line number Diff line
@@ -14,8 +14,9 @@
//  limitations under the License.

use std::env;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::process::{Command, Stdio};

fn main() {
    install_generated_module(
@@ -48,35 +49,33 @@ fn install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &Pa
    }
}

fn generate_module(pdl_name: &PathBuf) {
fn generate_module(in_file: &PathBuf) {
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
    let out_file =
        File::create(out_dir.join(in_file.file_name().unwrap()).with_extension("rs")).unwrap();

    // Find the packetgen tool. Expecting it at CARGO_HOME/bin
    let packetgen = match env::var("CARGO_HOME") {
        Ok(dir) => PathBuf::from(dir).join("bin").join("bluetooth_packetgen"),
        Err(_) => PathBuf::from("bluetooth_packetgen"),
    // Find the pdl tool. Expecting it at CARGO_HOME/bin
    let pdl = match env::var("CARGO_HOME") {
        Ok(dir) => PathBuf::from(dir).join("bin").join("pdl"),
        Err(_) => PathBuf::from("pdl"),
    };

    if !Path::new(packetgen.as_os_str()).exists() {
        panic!(
            "bluetooth_packetgen not found in the current environment: {:?}",
            packetgen.as_os_str().to_str().unwrap()
        );
    if !Path::new(pdl.as_os_str()).exists() {
        panic!("pdl not found in the current environment: {:?}", pdl.as_os_str().to_str().unwrap());
    }

    println!("cargo:rerun-if-changed={}", pdl_name.display());
    let output = Command::new(packetgen.as_os_str().to_str().unwrap())
        .arg("--out=".to_owned() + out_dir.as_os_str().to_str().unwrap())
        .arg("--include=".to_owned() + pdl_name.parent().unwrap().as_os_str().to_str().unwrap())
        .arg("--rust")
        .arg(pdl_name)
    println!("cargo:rerun-if-changed={}", in_file.display());
    let output = Command::new(pdl.as_os_str().to_str().unwrap())
        .arg("--output-format")
        .arg("rust")
        .arg(in_file)
        .stdout(Stdio::from(out_file))
        .output()
        .unwrap();

    println!(
        "Status: {}, stdout: {}, stderr: {}",
        "Status: {}, stderr: {}",
        output.status,
        String::from_utf8_lossy(output.stdout.as_slice()),
        String::from_utf8_lossy(output.stderr.as_slice())
    );

+1 −1
Original line number Diff line number Diff line
@@ -32,4 +32,4 @@ macro_rules! impl_try_from {
}

impl_try_from!(lmp::LmpPacket);
impl_try_from!(hci::CommandPacket);
impl_try_from!(hci::Command);
+12 −13
Original line number Diff line number Diff line
@@ -20,17 +20,15 @@ pub struct LinkManagerOps {

impl LinkManagerOps {
    pub(crate) fn get_address(&self, handle: u16) -> Option<hci::Address> {
        let mut result = hci::EMPTY_ADDRESS;
        unsafe { (self.get_address)(self.user_pointer, handle, &mut result.bytes as *mut _) };
        if result == hci::EMPTY_ADDRESS {
            None
        } else {
            Some(result)
        }
        let mut result = [0; 6];
        unsafe { (self.get_address)(self.user_pointer, handle, &mut result as *mut _) };
        let addr = hci::Address::from(&result);
        (addr != hci::EMPTY_ADDRESS).then_some(addr)
    }

    pub(crate) fn get_handle(&self, addr: hci::Address) -> u16 {
        unsafe { (self.get_handle)(self.user_pointer, &addr.bytes as *const _) }
        let addr_bytes: [u8; 6] = addr.into();
        unsafe { (self.get_handle)(self.user_pointer, &addr_bytes as *const _) }
    }

    pub(crate) fn extended_features(&self, features_page: u8) -> u64 {
@@ -42,10 +40,11 @@ impl LinkManagerOps {
    }

    pub(crate) fn send_lmp_packet(&self, to: hci::Address, packet: &[u8]) {
        let to_bytes: [u8; 6] = to.into();
        unsafe {
            (self.send_lmp_packet)(
                self.user_pointer,
                &to.bytes as *const _,
                &to_bytes as *const _,
                packet.as_ptr(),
                packet.len(),
            )
@@ -75,7 +74,7 @@ pub unsafe extern "C" fn link_manager_add_link(
    peer: *const [u8; 6],
) -> bool {
    let lm = ManuallyDrop::new(Rc::from_raw(lm));
    lm.add_link(hci::Address { bytes: *peer }).is_ok()
    lm.add_link(hci::Address::from(&*peer)).is_ok()
}

/// Unregister a link with a peer inside the link manager
@@ -93,7 +92,7 @@ pub unsafe extern "C" fn link_manager_remove_link(
    peer: *const [u8; 6],
) -> bool {
    let lm = ManuallyDrop::new(Rc::from_raw(lm));
    lm.remove_link(hci::Address { bytes: *peer }).is_ok()
    lm.remove_link(hci::Address::from(&*peer)).is_ok()
}

/// Run the Link Manager procedures
@@ -127,7 +126,7 @@ pub unsafe extern "C" fn link_manager_ingest_hci(
    let lm = ManuallyDrop::new(Rc::from_raw(lm));
    let data = slice::from_raw_parts(data, len);

    if let Ok(packet) = hci::CommandPacket::parse(data) {
    if let Ok(packet) = hci::Command::parse(data) {
        lm.ingest_hci(packet).is_ok()
    } else {
        false
@@ -157,7 +156,7 @@ pub unsafe extern "C" fn link_manager_ingest_lmp(
    let data = slice::from_raw_parts(data, len);

    if let Ok(packet) = lmp::LmpPacket::parse(data) {
        lm.ingest_lmp(hci::Address { bytes: *from }, packet).is_ok()
        lm.ingest_lmp(hci::Address::from(&*from), packet).is_ok()
    } else {
        false
    }
Loading