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

Commit 683c38cd authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: simplify some packet usages

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: Ie591460ed642a46215cfe8b0ddefcb509f21db89
parent be24b415
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
//! Implementation of the HAl that talks to BT controller over Android's HIDL
use crate::internal::{Hal, RawHalExports};
use bt_packets::hci;
use bt_packets::hci::{AclPacket, CommandPacket, EventPacket};
use gddi::{module, provides};
use std::sync::Arc;
use std::sync::Mutex;
@@ -53,8 +53,8 @@ mod ffi {

struct Callbacks {
    init_tx: UnboundedSender<()>,
    evt_tx: UnboundedSender<hci::EventPacket>,
    acl_tx: UnboundedSender<hci::AclPacket>,
    evt_tx: UnboundedSender<EventPacket>,
    acl_tx: UnboundedSender<AclPacket>,
}

lazy_static! {
@@ -72,7 +72,7 @@ fn on_event(data: &[u8]) {
        .as_ref()
        .unwrap()
        .evt_tx
        .send(hci::EventPacket::parse(data).unwrap())
        .send(EventPacket::parse(data).unwrap())
        .unwrap();
}

@@ -82,15 +82,15 @@ fn on_acl(data: &[u8]) {
        .as_ref()
        .unwrap()
        .acl_tx
        .send(hci::AclPacket::parse(data).unwrap())
        .send(AclPacket::parse(data).unwrap())
        .unwrap();
}

fn on_sco(_data: &[u8]) {}

async fn dispatch_outgoing(
    mut cmd_rx: UnboundedReceiver<hci::CommandPacket>,
    mut acl_rx: UnboundedReceiver<hci::AclPacket>,
    mut cmd_rx: UnboundedReceiver<CommandPacket>,
    mut acl_rx: UnboundedReceiver<AclPacket>,
) {
    loop {
        select! {
+14 −14
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ pub mod snoop;
#[cfg(target_os = "android")]
mod hidl_hal;

use bt_packets::hci;
use bt_packets::hci::{AclPacket, CommandPacket, EventPacket};
use gddi::{module, Stoppable};
use std::sync::Arc;
use thiserror::Error;
@@ -47,17 +47,17 @@ const H4_HEADER_SIZE: usize = 1;
#[derive(Clone, Stoppable)]
pub struct HalExports {
    /// Transmit end of a channel used to send HCI commands
    pub cmd_tx: Sender<hci::CommandPacket>,
    pub cmd_tx: Sender<CommandPacket>,
    /// Receive end of a channel used to receive HCI events
    pub evt_rx: Arc<Mutex<Receiver<hci::EventPacket>>>,
    pub evt_rx: Arc<Mutex<Receiver<EventPacket>>>,
    /// Transmit end of a channel used to send ACL data
    pub acl_tx: Sender<hci::AclPacket>,
    pub acl_tx: Sender<AclPacket>,
    /// Receive end of a channel used to receive ACL data
    pub acl_rx: Arc<Mutex<Receiver<hci::AclPacket>>>,
    pub acl_rx: Arc<Mutex<Receiver<AclPacket>>>,
}

mod internal {
    use bt_packets::hci;
    use bt_packets::hci::{AclPacket, CommandPacket, EventPacket};
    use gddi::Stoppable;
    use std::sync::Arc;
    use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
@@ -65,17 +65,17 @@ mod internal {

    #[derive(Clone, Stoppable)]
    pub struct RawHalExports {
        pub cmd_tx: UnboundedSender<hci::CommandPacket>,
        pub evt_rx: Arc<Mutex<UnboundedReceiver<hci::EventPacket>>>,
        pub acl_tx: UnboundedSender<hci::AclPacket>,
        pub acl_rx: Arc<Mutex<UnboundedReceiver<hci::AclPacket>>>,
        pub cmd_tx: UnboundedSender<CommandPacket>,
        pub evt_rx: Arc<Mutex<UnboundedReceiver<EventPacket>>>,
        pub acl_tx: UnboundedSender<AclPacket>,
        pub acl_rx: Arc<Mutex<UnboundedReceiver<AclPacket>>>,
    }

    pub struct Hal {
        pub cmd_rx: UnboundedReceiver<hci::CommandPacket>,
        pub evt_tx: UnboundedSender<hci::EventPacket>,
        pub acl_rx: UnboundedReceiver<hci::AclPacket>,
        pub acl_tx: UnboundedSender<hci::AclPacket>,
        pub cmd_rx: UnboundedReceiver<CommandPacket>,
        pub evt_tx: UnboundedSender<EventPacket>,
        pub acl_rx: UnboundedReceiver<AclPacket>,
        pub acl_tx: UnboundedSender<AclPacket>,
    }

    impl Hal {
+12 −8
Original line number Diff line number Diff line
@@ -4,9 +4,10 @@

use crate::internal::{Hal, RawHalExports};
use crate::{Result, H4_HEADER_SIZE};
use bt_packets::hci;
use bt_packets::hci::{AclPacket, CommandPacket, EventPacket};
use bytes::{BufMut, Bytes, BytesMut};
use gddi::{module, provides, Stoppable};
use num_derive::{FromPrimitive, ToPrimitive};
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;
use std::sync::Arc;
@@ -15,7 +16,6 @@ use tokio::net::TcpStream;
use tokio::runtime::Runtime;
use tokio::select;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use num_derive::{FromPrimitive, ToPrimitive};

#[derive(FromPrimitive, ToPrimitive)]
enum HciPacketType {
@@ -76,8 +76,8 @@ impl RootcanalConfig {

/// Send HCI events received from the HAL to the HCI layer
async fn dispatch_incoming<R>(
    evt_tx: UnboundedSender<hci::EventPacket>,
    acl_tx: UnboundedSender<hci::AclPacket>,
    evt_tx: UnboundedSender<EventPacket>,
    acl_tx: UnboundedSender<AclPacket>,
    reader: R,
) -> Result<()>
where
@@ -96,7 +96,9 @@ where
            payload.resize(len, 0);
            reader.read_exact(&mut payload).await?;
            buffer.unsplit(payload);
            evt_tx.send(hci::EventPacket::parse(&buffer.freeze()).unwrap()).unwrap();
            evt_tx
                .send(EventPacket::parse(&buffer.freeze()).unwrap())
                .unwrap();
        } else if buffer[0] == HciPacketType::Acl as u8 {
            buffer.resize(HciPacketHeaderSize::Acl as usize, 0);
            reader.read_exact(&mut buffer).await?;
@@ -105,15 +107,17 @@ where
            payload.resize(len, 0);
            reader.read_exact(&mut payload).await?;
            buffer.unsplit(payload);
            acl_tx.send(hci::AclPacket::parse(&buffer.freeze()).unwrap()).unwrap();
            acl_tx
                .send(AclPacket::parse(&buffer.freeze()).unwrap())
                .unwrap();
        }
    }
}

/// Send commands received from the HCI later to rootcanal
async fn dispatch_outgoing<W>(
    mut cmd_rx: UnboundedReceiver<hci::CommandPacket>,
    mut acl_rx: UnboundedReceiver<hci::AclPacket>,
    mut cmd_rx: UnboundedReceiver<CommandPacket>,
    mut acl_rx: UnboundedReceiver<AclPacket>,
    mut writer: W,
) -> Result<()>
where
+11 −26
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
use crate::internal::RawHalExports;
use crate::HalExports;
use bt_common::sys_prop;
use bt_packets::hci::{AclPacket, CommandPacket, EventPacket};
use bytes::{BufMut, Bytes, BytesMut};
use gddi::{module, provides, Stoppable};
use log::error;
@@ -15,7 +16,6 @@ use tokio::runtime::Runtime;
use tokio::select;
use tokio::sync::mpsc::{channel, UnboundedReceiver};
use tokio::sync::Mutex;
use bt_packets::hci;

/// The different modes snoop logging can be in
#[derive(Clone)]
@@ -59,9 +59,7 @@ impl SnoopConfig {
            path: "/data/misc/bluetooth/logs/btsnoop_hci.log".to_string(),
            max_packets_per_file: sys_prop::get_u32("persist.bluetooth.btsnoopsize")
                .unwrap_or(0xFFFF),
            mode: get_configured_snoop_mode()
                .parse()
                .unwrap_or(SnoopMode::Disabled),
            mode: get_configured_snoop_mode().parse().unwrap_or(SnoopMode::Disabled),
        }
    }

@@ -103,10 +101,10 @@ async fn provide_snooped_hal(
    hal_exports: RawHalExports,
    rt: Arc<Runtime>,
) -> HalExports {
    let (cmd_down_tx, mut cmd_down_rx) = channel::<hci::CommandPacket>(10);
    let (evt_up_tx, evt_up_rx) = channel::<hci::EventPacket>(10);
    let (acl_down_tx, mut acl_down_rx) = channel::<hci::AclPacket>(10);
    let (acl_up_tx, acl_up_rx) = channel::<hci::AclPacket>(10);
    let (cmd_down_tx, mut cmd_down_rx) = channel::<CommandPacket>(10);
    let (evt_up_tx, evt_up_rx) = channel::<EventPacket>(10);
    let (acl_down_tx, mut acl_down_rx) = channel::<AclPacket>(10);
    let (acl_up_tx, acl_up_rx) = channel::<AclPacket>(10);

    rt.spawn(async move {
        let mut logger = SnoopLogger::new(config).await;
@@ -178,16 +176,10 @@ impl SnoopLogger {
        remove_file(config.path.clone() + ".last").await.ok();
        if let SnoopMode::Disabled = config.mode {
            remove_file(config.path.clone() + ".filtered").await.ok();
            remove_file(config.path.clone() + ".filtered.last")
                .await
                .ok();
            remove_file(config.path.clone() + ".filtered.last").await.ok();
        }

        let mut ret = Self {
            config,
            file: None,
            packets: 0,
        };
        let mut ret = Self { config, file: None, packets: 0 };
        ret.open_next_file().await;

        ret
@@ -207,10 +199,7 @@ impl SnoopLogger {
        }

        let timestamp: u64 = u64::try_from(
            SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap()
                .as_micros(),
            SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_micros(),
        )
        .unwrap()
            + SNOOP_EPOCH_DELTA;
@@ -255,12 +244,8 @@ impl SnoopLogger {
    async fn open_next_file(&mut self) {
        self.close_file().await;

        rename(&self.config.path, self.config.path.clone() + ".last")
            .await
            .ok();
        let mut file = File::create(&self.config.path)
            .await
            .expect("could not open snoop log");
        rename(&self.config.path, self.config.path.clone() + ".last").await.ok();
        let mut file = File::create(&self.config.path).await.expect("could not open snoop log");
        file.write_all(b"btsnoop\x00\x00\x00\x00\x01\x00\x00\x03\xea")
            .await
            .expect("could not write snoop header");