Skip to content

Commit

Permalink
Support for BLE and BTP
Browse files Browse the repository at this point in the history
Fix CI to install libdbus

Fix a few typos in doc comments

MAX_BTP_SESSIONS needs to be public

Clone for the callback

Compute MTU more precisely, also based on the reported GATT MTU
  • Loading branch information
ivmarkov committed Jun 13, 2024
1 parent 2076dc6 commit 2f2b29f
Show file tree
Hide file tree
Showing 17 changed files with 2,770 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci-tlv-tool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
toolchain: ${{ env.RUST_TOOLCHAIN }}
components: rustfmt, clippy, rust-src

- name: Install libdbus
run: sudo apt-get install -y libdbus-1-dev

- name: Checkout
uses: actions/checkout@v3

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
toolchain: ${{ env.RUST_TOOLCHAIN }}
components: rustfmt, clippy, rust-src

- name: Install libdbus
run: sudo apt-get install -y libdbus-1-dev

- name: Checkout
uses: actions/checkout@v3

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish-dry-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
toolchain: ${{ env.RUST_TOOLCHAIN }}
components: rustfmt, clippy, rust-src

- name: Install libdbus
run: sudo apt-get install -y libdbus-1-dev

- name: Checkout
uses: actions/checkout@v3

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
toolchain: ${{ env.RUST_TOOLCHAIN }}
components: rustfmt, clippy, rust-src

- name: Install libdbus
run: sudo apt-get install -y libdbus-1-dev

- name: Checkout
uses: actions/checkout@v3

Expand Down
56 changes: 41 additions & 15 deletions examples/onoff_light/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use core::borrow::Borrow;
use core::pin::pin;
use std::net::UdpSocket;
use std::sync::Arc;

use embassy_futures::select::{select, select4};

Expand All @@ -36,16 +37,24 @@ use rs_matter::data_model::subscriptions::Subscriptions;
use rs_matter::data_model::system_model::descriptor;
use rs_matter::error::Error;
use rs_matter::mdns::MdnsService;
use rs_matter::pairing::DiscoveryCapabilities;
use rs_matter::persist::Psm;
use rs_matter::respond::DefaultResponder;
use rs_matter::secure_channel::spake2p::VerifierData;
use rs_matter::transport::core::MATTER_SOCKET_BIND_ADDR;
use rs_matter::transport::network::{
btp::{Btp, BtpContext},
Address, ChainedNetwork,
};
use rs_matter::utils::buf::PooledBuffers;
use rs_matter::utils::select::Coalesce;
use rs_matter::utils::std_mutex::StdRawMutex;
use rs_matter::MATTER_PORT;

mod dev_att;

static BTP_CONTEXT: BtpContext<StdRawMutex> = BtpContext::<StdRawMutex>::new();

fn main() -> Result<(), Error> {
let thread = std::thread::Builder::new()
// Increase the stack size until the example can work without stack blowups.
Expand All @@ -55,7 +64,7 @@ fn main() -> Result<(), Error> {
// e.g., an opt-level of "0" will require a several times' larger stack.
//
// Optimizing/lowering `rs-matter` memory consumption is an ongoing topic.
.stack_size(95 * 1024)
.stack_size(950 * 1024)
.spawn(run)
.unwrap();

Expand Down Expand Up @@ -98,6 +107,15 @@ fn run() -> Result<(), Error> {
MATTER_PORT,
);

let dev_comm = CommissioningData {
// TODO: Hard-coded for now
verifier: VerifierData::new_with_pw(123456, *matter.borrow()),
discriminator: 250,
};

//let discovery_caps = DiscoveryCapabilities::new(true, false, false);
let discovery_caps = DiscoveryCapabilities::new(false, true, false);

matter.initialize_transport_buffers()?;

info!("Matter initialized");
Expand Down Expand Up @@ -141,23 +159,30 @@ fn run() -> Result<(), Error> {
}
});

//let btp = Btp::new_builtin(Arc::new(BtpContext::<StdRawMutex>::new()));
let btp = Btp::new_builtin(&BTP_CONTEXT);

let mut bluetooth = pin!(btp.run("MT", &dev_det, &dev_comm));

// NOTE:
// When using a custom UDP stack (e.g. for `no_std` environments), replace with a UDP socket bind for your custom UDP stack
// The returned socket should be splittable into two halves, where each half implements `UdpSend` and `UdpReceive` respectively
let socket = async_io::Async::<UdpSocket>::bind(MATTER_SOCKET_BIND_ADDR)?;
//let udp = async_io::Async::<UdpSocket>::bind(MATTER_SOCKET_BIND_ADDR)?;

// Run the Matter and mDNS transports
// let network = ChainedNetwork::new(
// Address::is_btp,
// &btp,
// &udp);

// Run the Matter transport
let mut transport = pin!(matter.run(
&socket,
&socket,
Some((
CommissioningData {
// TODO: Hard-coded for now
verifier: VerifierData::new_with_pw(123456, *matter.borrow()),
discriminator: 250,
},
Default::default(),
)),
// network.clone(),
// network,
&btp,
&btp,
// &udp,
// &udp,
Some((dev_comm, discovery_caps)),
));

// NOTE:
Expand All @@ -168,14 +193,15 @@ fn run() -> Result<(), Error> {
// Combine all async tasks in a single one
let all = select4(
&mut transport,
&mut mdns,
&mut bluetooth,
//&mut mdns,
&mut persist,
select(&mut respond, &mut device).coalesce(),
);

// NOTE:
// Replace with a different executor for e.g. `no_std` environments
futures_lite::future::block_on(all.coalesce())
futures_lite::future::block_on(async_compat::Compat::new(all.coalesce()))
}

const NODE: Node<'static> = Node {
Expand Down
6 changes: 5 additions & 1 deletion rs-matter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ x509-cert = { version = "0.2", default-features = false, features = ["pem"], opt
# STD
rand = { version = "0.8", optional = true, default-features = false, features = ["std", "std_rng"] }
async-io = { version = "2", optional = true, default-features = false }
async-compat = { version = "0.2", optional = true, default-features = false }

[target.'cfg(target_os = "macos")'.dependencies]
astro-dnssd = { version = "0.3" }

[target.'cfg(target_os = "linux")'.dependencies]
zeroconf = { version = "0.12", optional = true }
bluer = { version = "0.17", features = ["bluetoothd"] }
tokio = { version = "1" }
tokio-stream = { version = "0.1" }

[dev-dependencies]
env_logger = "0.11"
Expand All @@ -82,7 +86,7 @@ futures-lite = "1"
[[example]]
name = "onoff_light"
path = "../examples/onoff_light/src/main.rs"
required-features = ["std", "async-io"]
required-features = ["std", "async-io", "async-compat"]

# [[example]]
# name = "speaker"
Expand Down
15 changes: 15 additions & 0 deletions rs-matter/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,21 @@ impl From<ccm::aead::Error> for Error {
}
}

#[cfg(all(feature = "std", target_os = "linux", not(feature = "backtrace")))]
impl From<bluer::Error> for Error {
fn from(e: bluer::Error) -> Self {
::log::error!("Error in BTP: {e}");
Self::new(ErrorCode::BtpError)
}
}

#[cfg(all(feature = "std", target_os = "linux", feature = "backtrace"))]
impl From<bluer::Error> for Error {
fn from(e: bluer::Error) -> Self {
Self::new_with_details(ErrorCode::BtpError, Box::new(e))
}
}

#[cfg(feature = "std")]
impl From<std::time::SystemTimeError> for Error {
fn from(_e: std::time::SystemTimeError) -> Self {
Expand Down
1 change: 1 addition & 0 deletions rs-matter/src/transport/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use embassy_futures::select::{select, Either};

use crate::error::{Error, ErrorCode};

pub mod btp;
pub mod udp;

// Maximum UDP RX packet size per Matter spec
Expand Down
Loading

0 comments on commit 2f2b29f

Please sign in to comment.