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

Commit 296b2518 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: reorganize HAL a bit

remove mpsc:: prefixes, and hide internals so it's not exported as part
of the public api

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: I7393adfcdd52f3afa5051012ba0724155db87407
parent 205e4aa1
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
//! Implementation of the HAl that talks to BT controller over Android's HIDL
use crate::{Hal, HalExports};
use crate::internal::Hal;
use crate::HalExports;
use bt_packet::{HciCommand, HciEvent, RawPacket};
use bytes::Bytes;
use gddi::{module, provides};
@@ -7,7 +8,7 @@ use std::sync::Arc;
use std::sync::Mutex;
use tokio::runtime::Runtime;
use tokio::select;
use tokio::sync::mpsc;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};

module! {
    hidl_hal_module,
@@ -19,7 +20,7 @@ module! {
#[provides]
async fn provide_hidl_hal(rt: Arc<Runtime>) -> HalExports {
    let (hal_exports, hal) = Hal::new();
    let (init_tx, mut init_rx) = mpsc::unbounded_channel();
    let (init_tx, mut init_rx) = unbounded_channel();
    *CALLBACKS.lock().unwrap() = Some(Callbacks {
        init_tx,
        evt_tx: hal.evt_tx,
@@ -53,9 +54,9 @@ mod ffi {
}

struct Callbacks {
    init_tx: mpsc::UnboundedSender<()>,
    evt_tx: mpsc::UnboundedSender<HciEvent>,
    acl_tx: mpsc::UnboundedSender<RawPacket>,
    init_tx: UnboundedSender<()>,
    evt_tx: UnboundedSender<HciEvent>,
    acl_tx: UnboundedSender<RawPacket>,
}

lazy_static! {
@@ -90,8 +91,8 @@ fn on_acl(data: &[u8]) {
fn on_sco(_data: &[u8]) {}

async fn dispatch_outgoing(
    mut cmd_rx: mpsc::UnboundedReceiver<HciCommand>,
    mut acl_rx: mpsc::UnboundedReceiver<RawPacket>,
    mut cmd_rx: UnboundedReceiver<HciCommand>,
    mut acl_rx: UnboundedReceiver<RawPacket>,
) {
    loop {
        select! {
+39 −38
Original line number Diff line number Diff line
@@ -16,7 +16,8 @@ use bt_packet::{HciCommand, HciEvent, RawPacket};
use gddi::{module, Stoppable};
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::{mpsc, Mutex};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::Mutex;

#[cfg(target_os = "android")]
module! {
@@ -44,37 +45,36 @@ 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: mpsc::UnboundedSender<HciCommand>,
    pub cmd_tx: UnboundedSender<HciCommand>,
    /// Receive end of a channel used to receive HCI events
    pub evt_rx: Arc<Mutex<mpsc::UnboundedReceiver<HciEvent>>>,
    pub evt_rx: Arc<Mutex<UnboundedReceiver<HciEvent>>>,
    /// Transmit end of a channel used to send ACL data
    pub acl_tx: mpsc::UnboundedSender<RawPacket>,
    pub acl_tx: UnboundedSender<RawPacket>,
    /// Receive end of a channel used to receive ACL data
    pub acl_rx: Arc<Mutex<mpsc::UnboundedReceiver<RawPacket>>>,
    pub acl_rx: Arc<Mutex<UnboundedReceiver<RawPacket>>>,
}

/// HCI HAL
/// Receive HCI commands, send HCI events
mod internal {
    use bt_packet::{HciCommand, HciEvent, RawPacket};
    use std::sync::Arc;
    use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
    use tokio::sync::Mutex;

    pub struct Hal {
    /// Receive end of a channel used to receive HCI commands
    pub cmd_rx: mpsc::UnboundedReceiver<HciCommand>,
    /// Transmit end of a channel used to send HCI events
    pub evt_tx: mpsc::UnboundedSender<HciEvent>,
    /// Receive end of a channel used to send ACL data
    pub acl_rx: mpsc::UnboundedReceiver<RawPacket>,
    /// Transmit end of a channel used to receive ACL data
    pub acl_tx: mpsc::UnboundedSender<RawPacket>,
        pub cmd_rx: UnboundedReceiver<HciCommand>,
        pub evt_tx: UnboundedSender<HciEvent>,
        pub acl_rx: UnboundedReceiver<RawPacket>,
        pub acl_tx: UnboundedSender<RawPacket>,
    }

    impl Hal {
    /// Create a new Hal instance
    pub fn new() -> (HalExports, Self) {
        let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
        let (evt_tx, evt_rx) = mpsc::unbounded_channel();
        let (acl_down_tx, acl_down_rx) = mpsc::unbounded_channel();
        let (acl_up_tx, acl_up_rx) = mpsc::unbounded_channel();
        pub fn new() -> (super::HalExports, Self) {
            let (cmd_tx, cmd_rx) = unbounded_channel();
            let (evt_tx, evt_rx) = unbounded_channel();
            let (acl_down_tx, acl_down_rx) = unbounded_channel();
            let (acl_up_tx, acl_up_rx) = unbounded_channel();
            (
            HalExports {
                super::HalExports {
                    cmd_tx,
                    evt_rx: Arc::new(Mutex::new(evt_rx)),
                    acl_tx: acl_down_tx,
@@ -89,6 +89,7 @@ impl Hal {
            )
        }
    }
}

/// Result type
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
+7 −6
Original line number Diff line number Diff line
@@ -2,7 +2,8 @@
//! This connects to "rootcanal" which provides a simulated
//! Bluetooth chip as well as a simulated environment.

use crate::{Hal, HalExports, Result, H4_HEADER_SIZE};
use crate::internal::Hal;
use crate::{HalExports, Result, H4_HEADER_SIZE};
use bt_packet::{HciCommand, HciEvent, HciPacketHeaderSize, HciPacketType, RawPacket};
use bytes::{BufMut, Bytes, BytesMut};
use gddi::{module, provides, Stoppable};
@@ -13,7 +14,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpStream;
use tokio::runtime::Runtime;
use tokio::select;
use tokio::sync::mpsc;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};

module! {
    rootcanal_hal_module,
@@ -59,8 +60,8 @@ impl RootcanalConfig {

/// Send HCI events received from the HAL to the HCI layer
async fn dispatch_incoming<R>(
    evt_tx: mpsc::UnboundedSender<HciEvent>,
    acl_tx: mpsc::UnboundedSender<RawPacket>,
    evt_tx: UnboundedSender<HciEvent>,
    acl_tx: UnboundedSender<RawPacket>,
    reader: R,
) -> Result<()>
where
@@ -95,8 +96,8 @@ where

/// Send commands received from the HCI later to rootcanal
async fn dispatch_outgoing<W>(
    mut cmd_rx: mpsc::UnboundedReceiver<HciCommand>,
    mut acl_rx: mpsc::UnboundedReceiver<RawPacket>,
    mut cmd_rx: UnboundedReceiver<HciCommand>,
    mut acl_rx: UnboundedReceiver<RawPacket>,
    mut writer: W,
) -> Result<()>
where