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

Commit e6129f94 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: simplify hci a little bit

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: I0356c2102cf760c091a92ae0ab8ee7917eea03b6
parent 683c38cd
Loading
Loading
Loading
Loading
+22 −18
Original line number Diff line number Diff line
@@ -7,8 +7,8 @@ pub mod error;
pub mod facade;

use bt_hal::HalExports;
use bt_packets::hci;
use bt_packets::hci::EventChild::{CommandStatus,CommandComplete};
use bt_packets::hci::EventChild::{CommandComplete, CommandStatus};
use bt_packets::hci::{AclPacket, CommandPacket, EventCode, EventPacket, OpCode};
use error::Result;
use gddi::{module, provides, Stoppable};
use std::collections::HashMap;
@@ -53,30 +53,30 @@ async fn provide_hci(hal_exports: HalExports, rt: Arc<Runtime>) -> HciExports {
/// to the command is received
#[derive(Debug)]
struct Command {
    cmd: hci::CommandPacket,
    fut: oneshot::Sender<hci::EventPacket>,
    cmd: CommandPacket,
    fut: oneshot::Sender<EventPacket>,
}

#[derive(Debug)]
struct PendingCommand {
    opcode: hci::OpCode,
    fut: oneshot::Sender<hci::EventPacket>,
    opcode: OpCode,
    fut: oneshot::Sender<EventPacket>,
}

/// HCI interface
#[derive(Clone, Stoppable)]
pub struct HciExports {
    cmd_tx: Sender<Command>,
    evt_handlers: Arc<Mutex<HashMap<hci::EventCode, Sender<hci::EventPacket>>>>,
    evt_handlers: Arc<Mutex<HashMap<EventCode, Sender<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>>>,
}

impl HciExports {
    async fn send(&mut self, cmd: hci::CommandPacket) -> Result<hci::EventPacket> {
        let (tx, rx) = oneshot::channel::<hci::EventPacket>();
    async fn send(&mut self, cmd: CommandPacket) -> Result<EventPacket> {
        let (tx, rx) = oneshot::channel::<EventPacket>();
        self.cmd_tx.send(Command { cmd, fut: tx }).await?;
        let event = rx.await?;
        Ok(event)
@@ -84,26 +84,30 @@ impl HciExports {

    /// Enqueue an HCI command expecting a command complete
    /// response from the controller
    pub async fn enqueue_command_with_complete(&mut self, cmd: hci::CommandPacket) -> hci::EventPacket {
    pub async fn enqueue_command_with_complete(&mut self, cmd: CommandPacket) -> EventPacket {
        self.send(cmd).await.unwrap()
    }

    /// Enqueue an HCI command expecting a status response
    /// from the controller
    pub async fn enqueue_command_with_status(&mut self, cmd: hci::CommandPacket) -> hci::EventPacket {
    pub async fn enqueue_command_with_status(&mut self, cmd: CommandPacket) -> EventPacket {
        self.send(cmd).await.unwrap()
    }

    /// Indicate interest in specific HCI events
    pub async fn register_event_handler(&mut self, evt_code: hci::EventCode, sender: Sender<hci::EventPacket>) {
    pub async fn register_event_handler(
        &mut self,
        evt_code: EventCode,
        sender: Sender<EventPacket>,
    ) {
        self.evt_handlers.lock().await.insert(evt_code, sender);
    }
}

async fn dispatch(
    evt_handlers: Arc<Mutex<HashMap<hci::EventCode, Sender<hci::EventPacket>>>>,
    evt_rx: Arc<Mutex<Receiver<hci::EventPacket>>>,
    cmd_tx: Sender<hci::CommandPacket>,
    evt_handlers: Arc<Mutex<HashMap<EventCode, Sender<EventPacket>>>>,
    evt_rx: Arc<Mutex<Receiver<EventPacket>>>,
    cmd_tx: Sender<CommandPacket>,
    mut cmd_rx: Receiver<Command>,
) {
    let mut pending_cmds: Vec<PendingCommand> = Vec::new();
@@ -142,7 +146,7 @@ async fn dispatch(
    }
}

async fn consume(evt_rx: &Arc<Mutex<Receiver<hci::EventPacket>>>) -> Option<hci::EventPacket> {
async fn consume(evt_rx: &Arc<Mutex<Receiver<EventPacket>>>) -> Option<EventPacket> {
    evt_rx.lock().await.recv().await
}