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

Commit c919bb02 authored by Zach Johnson's avatar Zach Johnson Committed by Gerrit Code Review
Browse files

Merge changes I4496bf97,I6368fd2e

* changes:
  rusty-gd: some fixes to handle errors & get it somewhat working
  rusty-gd: sequence immediate message loop posts
parents 5463c52c 4b0a1c71
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -34,7 +34,12 @@ type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
  #[error("Packet parsing failed")]
  InvalidPacketError
  InvalidPacketError,
  #[error("{field} was {value:x}, which is not known")]
  ConstraintOutOfBounds {
    field: String,
    value: u64,
  },
}

pub trait Packet {
+2 −1
Original line number Diff line number Diff line
@@ -964,7 +964,8 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
    }

    if (!constrained_descendants.empty()) {
      s << "_ => panic!(\"unexpected value " << "\"),";
      s << "v => return Err(Error::ConstraintOutOfBounds{field: \"" << constraint_name
        << "\".to_string(), value: v as u64}),";
    }

    s << "};\n";
+9 −2
Original line number Diff line number Diff line
@@ -64,13 +64,20 @@ fn on_init_complete() {
}

fn on_event(data: &[u8]) {
    log::error!("got event: {:02x?}", data);
    let callbacks = CALLBACKS.lock().unwrap();
    callbacks.as_ref().unwrap().evt_tx.send(EventPacket::parse(data).unwrap()).unwrap();
    match EventPacket::parse(data) {
        Ok(p) => callbacks.as_ref().unwrap().evt_tx.send(p).unwrap(),
        Err(e) => log::error!("failure to parse event: {:?} data: {:02x?}", e, data),
    }
}

fn on_acl(data: &[u8]) {
    let callbacks = CALLBACKS.lock().unwrap();
    callbacks.as_ref().unwrap().acl_tx.send(AclPacket::parse(data).unwrap()).unwrap();
    match AclPacket::parse(data) {
        Ok(p) => callbacks.as_ref().unwrap().acl_tx.send(p).unwrap(),
        Err(e) => log::error!("failure to parse incoming ACL: {:?} data: {:02x?}", e, data),
    }
}

fn on_sco(_data: &[u8]) {}
+15 −7
Original line number Diff line number Diff line
@@ -62,16 +62,24 @@ pub fn hci_send_command(
    data: &[u8],
    callback: cxx::UniquePtr<ffi::u8SliceOnceCallback>,
) {
    let packet = CommandPacket::parse(data).unwrap();
    log::error!("sending command: {:02x?}", data);
    match CommandPacket::parse(data) {
        Ok(packet) => {
            let mut commands = hci.internal.commands.clone();
            hci.rt.spawn(async move {
                let resp = commands.send(packet).await.unwrap();
                callback.Run(&resp.to_bytes());
            });
        }
        Err(e) => panic!("could not parse command: {:?} {:02x?}", e, data),
    }
}

pub fn hci_send_acl(hci: &mut Hci, data: &[u8]) {
    hci.rt.block_on(hci.internal.acl_tx.send(AclPacket::parse(data).unwrap())).unwrap();
    match AclPacket::parse(data) {
        Ok(packet) => hci.rt.block_on(hci.internal.acl_tx.send(packet)).unwrap(),
        Err(e) => panic!("could not parse acl: {:?} {:02x?}", e, data),
    }
}

pub fn hci_register_event(hci: &mut Hci, event: u8) {
+27 −12
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ use std::convert::TryInto;
use std::sync::Arc;
use std::time::Duration;
use tokio::runtime::Runtime;
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};

#[cxx::bridge(namespace = bluetooth::shim::rust)]
mod ffi {
@@ -32,12 +33,21 @@ unsafe impl Send for ffi::OnceClosure {}

pub struct MessageLoopThread {
    rt: Arc<Runtime>,
    tx: UnboundedSender<cxx::UniquePtr<ffi::OnceClosure>>,
}

pub fn main_message_loop_thread_create() -> Box<MessageLoopThread> {
    assert!(init_flags::gd_rust_is_enabled());

    Box::new(MessageLoopThread { rt: crate::stack::RUNTIME.clone() })
    let rt = crate::stack::RUNTIME.clone();
    let (tx, mut rx) = unbounded_channel::<cxx::UniquePtr<ffi::OnceClosure>>();
    rt.spawn(async move {
        while let Some(c) = rx.recv().await {
            c.Run();
        }
    });

    Box::new(MessageLoopThread { rt, tx })
}

pub fn main_message_loop_thread_start(thread: &mut MessageLoopThread) -> i32 {
@@ -52,7 +62,11 @@ pub fn main_message_loop_thread_do_delayed(
    delay_ms: i64,
) {
    assert!(init_flags::gd_rust_is_enabled());

    if delay_ms == 0 {
        if thread.tx.send(closure).is_err() {
            log::error!("could not post task - shutting down?");
        }
    } else {
        thread.rt.spawn(async move {
            // NOTE: tokio's sleep can't wake up the system...
            // but hey, neither could the message loop from libchrome.
@@ -64,3 +78,4 @@ pub fn main_message_loop_thread_do_delayed(
            closure.Run();
        });
    }
}
Loading