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

Commit 2871a6be authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Decouple bt_facade_helpers with bt_packets

What we really need is converting a packet into Bytes or Vec<u8>.
Implement From<SomePacket> for them. Then in facade/helpers, we can get
rid of packet dependency.

Test: compile on android and chromeos
Bug: 191711152
Tag: #gd-refactor
Change-Id: I34178167ff7f012c3dc596a96fefc57b258195fe
parent 00418c27
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -1079,6 +1079,22 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
  s << "fn to_vec(self) -> Vec<u8> { self.to_bytes().to_vec() }\n";
  s << "}";

  s << "impl From<" << name_ << "Packet"
    << "> for Bytes {\n";
  s << "fn from(packet: " << name_ << "Packet"
    << ") -> Self {\n";
  s << "packet.to_bytes()\n";
  s << "}\n";
  s << "}\n";

  s << "impl From<" << name_ << "Packet"
    << "> for Vec<u8> {\n";
  s << "fn from(packet: " << name_ << "Packet"
    << ") -> Self {\n";
  s << "packet.to_vec()\n";
  s << "}\n";
  s << "}\n";

  s << "impl " << name_ << "Packet {";
  if (parent_ == nullptr) {
    s << "pub fn parse(bytes: &[u8]) -> Result<Self> { ";
+0 −1
Original line number Diff line number Diff line
@@ -52,7 +52,6 @@ rust_library {
    edition: "2018",
    rustlibs: [
        "libbt_facade_proto",
        "libbt_packets",
        "libbytes",
        "libfutures",
        "libgrpcio",
+0 −1
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ edition = "2018"
[dependencies]
# GD bluetooth deps
bt_facade_proto = { path = "../../facade_proto" }
bt_packets = { path = "../../packets" }

# External deps
bytes = "*"
+5 −4
Original line number Diff line number Diff line
//! common facade & shim helpers

use bt_facade_proto::common::Data;
use bt_packets::hci::Packet;
use bytes::Bytes;
use futures::sink::SinkExt;
use grpcio::*;
use std::sync::Arc;
@@ -22,7 +22,7 @@ pub struct RxAdapter<T> {
    running: bool,
}

impl<T: 'static + Packet + Send> RxAdapter<T> {
impl<T: 'static + Into<Vec<u8>> + Into<Bytes> + Send> RxAdapter<T> {
    /// New, from an unwrapped receiver
    pub fn new(rx: Receiver<T>) -> Self {
        Self::from_arc(Arc::new(Mutex::new(rx)))
@@ -42,7 +42,7 @@ impl<T: 'static + Packet + Send> RxAdapter<T> {
        ctx.spawn(async move {
            while let Some(payload) = clone_rx.lock().await.recv().await {
                let mut data = Data::default();
                data.set_payload(payload.to_vec());
                data.set_payload(payload.into());
                if let Err(e) = sink.send((data, WriteFlags::default())).await {
                    log::error!("failure sending data: {:?}", e);
                }
@@ -62,7 +62,8 @@ impl<T: 'static + Packet + Send> RxAdapter<T> {
        let clone_rx = self.rx.clone();
        rt.spawn(async move {
            while let Some(payload) = clone_rx.lock().await.recv().await {
                runnable.run(&payload.to_bytes());
                let bytes: Bytes = payload.into();
                runnable.run(&bytes);
            }
        });
    }