Skip to content

Commit

Permalink
Merge pull request #5 from embassy-rs/update-deps
Browse files Browse the repository at this point in the history
Update to bt-hci main and adopt to new api
  • Loading branch information
lulf authored Mar 29, 2024
2 parents 3c00a81 + d8bf541 commit 20f75d3
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 73 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ jobs:
run: |
cd host
cargo check
cargo fmt --check
cargo clippy
- name: Test
run: |
cd host
cargo test -- --nocapture
- name: Build examples
run: for i in nrf-sdc; do pushd examples/$i; cargo build --release; popd; done;
run: for i in nrf-sdc; do pushd examples/$i; cargo fmt --check && cargo clippy && cargo build --release; popd; done;
6 changes: 3 additions & 3 deletions examples/nrf-sdc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ embassy-futures = { git = "https://github.com/embassy-rs/embassy.git", branch =
embassy-time = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-time-driver = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
embassy-embedded-hal = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
nrf-sdc = { git = "https://github.com/alexmoon/nrf-sdc.git", branch = "update-api" }
nrf-mpsl = { git = "https://github.com/alexmoon/nrf-sdc.git", branch = "update-api" }
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "serial-controller" }
nrf-sdc = { git = "https://github.com/alexmoon/nrf-sdc.git", branch = "main" }
nrf-mpsl = { git = "https://github.com/alexmoon/nrf-sdc.git", branch = "main" }
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "main" }

#embassy-executor = {path = "../../../embassy/embassy-executor"}
#embassy-nrf = {path = "../../../embassy/embassy-nrf"}
Expand Down
4 changes: 2 additions & 2 deletions examples/nrf-sdc/src/bin/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ async fn main(spawner: Spawner) {
unwrap!(L2capChannel::create(&adapter, &conn, 0x2349).await);
info!("New l2cap channel created, sending some data!");
for i in 0..10 {
let mut tx = [i; PAYLOAD_LEN];
let _ = unwrap!(ch1.send(&mut tx).await);
let tx = [i; PAYLOAD_LEN];
unwrap!(ch1.send(&tx).await);
}
info!("Sent data, waiting for them to be sent back");
let mut rx = [0; PAYLOAD_LEN];
Expand Down
4 changes: 2 additions & 2 deletions examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ async fn main(spawner: Spawner) {
info!("L2CAP data received, echoing");
Timer::after(Duration::from_secs(1)).await;
for i in 0..10 {
let mut tx = [i; PAYLOAD_LEN];
let _ = unwrap!(ch1.send(&mut tx).await);
let tx = [i; PAYLOAD_LEN];
unwrap!(ch1.send(&tx).await);
}
info!("L2CAP data echoed");

Expand Down
2 changes: 1 addition & 1 deletion examples/serial-hci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bt-hci = { version = "0.1.0", default-features = false, features = ["log"] }
trouble-host = { version = "0.1.0", path = "../../host", features = ["log"] }

[patch.crates-io]
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "serial-controller" }
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "main" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
#embassy-executor = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
#bt-hci = { path = "../../../bt-hci" }
8 changes: 4 additions & 4 deletions examples/serial-hci/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Use with any serial HCI
use bt_hci::driver::HciController;
use bt_hci::serial::SerialHciDriver;
use bt_hci::controller::ExternalController;
use bt_hci::transport::SerialTransport;
use embassy_futures::join::join3;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use log::*;
Expand Down Expand Up @@ -56,8 +56,8 @@ async fn main() {
let reader = embedded_io_adapters::tokio_1::FromTokio::new(reader);
let writer = embedded_io_adapters::tokio_1::FromTokio::new(writer);

let driver: SerialHciDriver<NoopRawMutex, _, _> = SerialHciDriver::new(reader, writer);
let controller: HciController<_, 10> = HciController::new(driver);
let driver: SerialTransport<NoopRawMutex, _, _> = SerialTransport::new(reader, writer);
let controller: ExternalController<_, 10> = ExternalController::new(driver);
static HOST_RESOURCES: StaticCell<HostResources<NoopRawMutex, 4, 32, 27>> = StaticCell::new();
let host_resources = HOST_RESOURCES.init(HostResources::new(PacketQos::None));

Expand Down
2 changes: 1 addition & 1 deletion host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ defmt = {version = "0.3", optional = true }
defmt = [ "dep:defmt" ]

[patch.crates-io]
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "serial-controller" }
bt-hci = { git = "https://github.com/alexmoon/bt-hci.git", branch = "main" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", branch = "main" }
6 changes: 3 additions & 3 deletions host/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ use bt_hci::cmd::le::{
};
use bt_hci::cmd::link_control::{Disconnect, DisconnectParams};
use bt_hci::cmd::{AsyncCmd, SyncCmd};
use bt_hci::controller::Controller;
use bt_hci::controller::{ControllerCmdAsync, ControllerCmdSync};
use bt_hci::data::{AclBroadcastFlag, AclPacket, AclPacketBoundary};
use bt_hci::event::le::LeEvent;
use bt_hci::event::Event;
use bt_hci::param::{BdAddr, ConnHandle, DisconnectReason, EventMask};
use bt_hci::{Controller, ControllerToHostPacket};
use bt_hci::{ControllerCmdAsync, ControllerCmdSync};
use bt_hci::ControllerToHostPacket;
use embassy_futures::select::{select, Either};
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::channel::Channel;
Expand Down Expand Up @@ -150,7 +151,6 @@ where
item.encode(&mut w)?;
}
let len = w.len();
drop(w);
LeSetAdvData::new(len as u8, data).exec(&self.controller).await?;
LeSetAdvEnable::new(true).exec(&self.controller).await?;
let conn = Connection::accept(self).await;
Expand Down
10 changes: 8 additions & 2 deletions host/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl<'d> AttributeData<'d> {
} else if offset == 1 {
w.write(*handle)?;
} else if offset == 2 {
w.write(handle.to_le_bytes()[1] as u8)?;
w.write(handle.to_le_bytes()[1])?;
}

let to_write = w.available().min(val.len());
Expand Down Expand Up @@ -257,6 +257,12 @@ impl<'d, const MAX: usize> InnerTable<'d, MAX> {
}
}

impl<'d, M: RawMutex, const MAX: usize> Default for AttributeTable<'d, M, MAX> {
fn default() -> Self {
Self::new()
}
}

impl<'d, M: RawMutex, const MAX: usize> AttributeTable<'d, M, MAX> {
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -374,7 +380,7 @@ impl<'r, 'd, M: RawMutex, const MAX: usize> ServiceBuilder<'r, 'd, M, MAX> {
handle: 0,
last_handle_in_group: 0,
data: AttributeData::Declaration {
props: props,
props,
handle: next,
uuid,
},
Expand Down
6 changes: 3 additions & 3 deletions host/src/attribute_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl<'c, 'd, M: RawMutex, const MAX: usize> AttributeServer<'c, 'd, M, MAX> {
if att.handle == handle {
if att.data.writable() {
err = att.data.write(0, data);
if let Ok(_) = &err {
if err.is_ok() {
if let AttributeData::Cccd {
notifications,
indications,
Expand Down Expand Up @@ -326,8 +326,8 @@ impl<'c, 'd, M: RawMutex, const MAX: usize> AttributeServer<'c, 'd, M, MAX> {
}
}

fn error_response<'m>(
mut w: WriteCursor<'m>,
fn error_response(
mut w: WriteCursor<'_>,
opcode: u8,
handle: u16,
code: AttErrorCode,
Expand Down
10 changes: 5 additions & 5 deletions host/src/channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{
task::{Context, Poll},
};

use bt_hci::{param::ConnHandle, Controller};
use bt_hci::{controller::Controller, param::ConnHandle};
use embassy_sync::{
blocking_mutex::{raw::RawMutex, Mutex},
channel::{Channel, DynamicReceiver},
Expand Down Expand Up @@ -393,7 +393,7 @@ impl<'d, M: RawMutex, const CHANNELS: usize, const L2CAP_TXQ: usize, const L2CAP
_ => {}
}
}
return Err(Error::NotFound);
Err(Error::NotFound)
})?;
Ok((conn, signal))
}
Expand All @@ -410,7 +410,7 @@ impl<'d, M: RawMutex, const CHANNELS: usize, const L2CAP_TXQ: usize, const L2CAP
_ => {}
}
}
return Err(Error::NotFound);
Err(Error::NotFound)
})
}

Expand All @@ -421,7 +421,7 @@ impl<'d, M: RawMutex, const CHANNELS: usize, const L2CAP_TXQ: usize, const L2CAP
match storage {
ChannelState::Connected(s) if cid == s.cid => {
if credits <= s.peer_credits as usize {
s.peer_credits = s.peer_credits - credits as u16;
s.peer_credits -= credits as u16;
return Poll::Ready(Ok(()));
} else {
state.credit_wakers[idx].register(cx.waker());
Expand All @@ -431,7 +431,7 @@ impl<'d, M: RawMutex, const CHANNELS: usize, const L2CAP_TXQ: usize, const L2CAP
_ => {}
}
}
return Poll::Ready(Err(Error::NotFound));
Poll::Ready(Err(Error::NotFound))
})
}
}
Expand Down
32 changes: 13 additions & 19 deletions host/src/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,10 @@ impl<M: RawMutex, const CONNS: usize> ConnectionManager<M, CONNS> {
self.state.lock(|state| {
let mut state = state.borrow_mut();
for storage in state.connections.iter_mut() {
match storage {
ConnectionState::Disconnected => {
*storage = ConnectionState::Connecting(handle, info);
state.waker.wake();
return Ok(());
}
_ => {}
if let ConnectionState::Disconnected = storage {
*storage = ConnectionState::Connecting(handle, info);
state.waker.wake();
return Ok(());
}
}
Err(Error::NotFound)
Expand All @@ -71,21 +68,18 @@ impl<M: RawMutex, const CONNS: usize> ConnectionManager<M, CONNS> {
self.state.lock(|state| {
let mut state = state.borrow_mut();
for storage in state.connections.iter_mut() {
match storage {
ConnectionState::Connecting(handle, info) => {
if let Some(peer) = peer {
if info.peer_address == peer {
let handle = handle.clone();
*storage = ConnectionState::Connected(handle.clone(), info.clone());
return Poll::Ready(handle);
}
} else {
let handle = handle.clone();
*storage = ConnectionState::Connected(handle.clone(), info.clone());
if let ConnectionState::Connecting(handle, info) = storage {
if let Some(peer) = peer {
if info.peer_address == peer {
let handle = *handle;
*storage = ConnectionState::Connected(handle, *info);
return Poll::Ready(handle);
}
} else {
let handle = *handle;
*storage = ConnectionState::Connected(handle, *info);
return Poll::Ready(handle);
}
_ => {}
}
}
state.waker.register(cx.waker());
Expand Down
17 changes: 4 additions & 13 deletions host/src/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::cursor::WriteCursor;
use crate::packet_pool::{AllocId, DynamicPacketPool};
use crate::pdu::Pdu;
use crate::{AdapterError, Error};
use bt_hci::controller::Controller;
use bt_hci::param::ConnHandle;
use bt_hci::Controller;
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::channel::DynamicReceiver;

Expand Down Expand Up @@ -45,11 +45,8 @@ impl<'reference, 'values, 'resources, M: RawMutex, T: Controller, const MAX: usi
data.write(mtu)?;

header.write(data.len() as u16)?;
header.write(4 as u16)?;
header.write(4_u16)?;
let len = header.len() + data.len();
drop(header);
drop(data);
drop(w);
self.tx.send(handle, Pdu::new(response, len).as_ref()).await?;
}
_ => match self.server.process(handle, att, data.write_buf()) {
Expand All @@ -58,11 +55,8 @@ impl<'reference, 'values, 'resources, M: RawMutex, T: Controller, const MAX: usi
data.commit(written)?;
data.truncate(mtu as usize);
header.write(written as u16)?;
header.write(4 as u16)?;
header.write(4_u16)?;
let len = header.len() + data.len();
drop(header);
drop(data);
drop(w);
self.tx.send(handle, Pdu::new(response, len).as_ref()).await?;
}
Ok(None) => {
Expand Down Expand Up @@ -112,11 +106,8 @@ impl<'reference, 'values, 'resources, M: RawMutex, T: Controller, const MAX: usi
data.append(value)?;

header.write(data.len() as u16)?;
header.write(4 as u16)?;
header.write(4_u16)?;
let total = header.len() + data.len();
drop(header);
drop(data);
drop(w);
self.tx.send(conn, Pdu::new(packet, total).as_ref()).await?;
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions host/src/l2cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::cursor::{ReadCursor, WriteCursor};
use crate::packet_pool::{AllocId, DynamicPacketPool};
use crate::pdu::Pdu;
use crate::{AdapterError, Error};
use bt_hci::controller::Controller;
use bt_hci::data::AclPacket;
use bt_hci::param::ConnHandle;
use bt_hci::Controller;
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::channel::DynamicReceiver;

Expand Down Expand Up @@ -41,7 +41,7 @@ impl<'d> L2capPacket<'d> {
let mut w = WriteCursor::new(dest);
w.write(self.payload.len() as u16)?;
w.write(self.channel)?;
w.append(&self.payload[..])?;
w.append(self.payload)?;
Ok(w.len())
}
}
Expand Down Expand Up @@ -78,7 +78,7 @@ impl<'a, 'd, T: Controller, const MTU: usize> L2capChannel<'a, 'd, T, MTU> {
let len = {
let mut w = WriteCursor::new(packet.as_mut());
w.write(2 + first.len() as u16)?;
w.write(self.peer_cid as u16)?;
w.write(self.peer_cid)?;
let len = buf.len() as u16;
w.write(len)?;
w.append(first)?;
Expand All @@ -97,7 +97,7 @@ impl<'a, 'd, T: Controller, const MTU: usize> L2capChannel<'a, 'd, T, MTU> {
let len = {
let mut w = WriteCursor::new(packet.as_mut());
w.write(chunk.len() as u16)?;
w.write(self.peer_cid as u16)?;
w.write(self.peer_cid)?;
w.append(chunk)?;
w.len()
};
Expand All @@ -124,7 +124,7 @@ impl<'a, 'd, T: Controller, const MTU: usize> L2capChannel<'a, 'd, T, MTU> {
pub async fn receive(&mut self, buf: &mut [u8]) -> Result<usize, AdapterError<T::Error>> {
let mut n_received = 1;
let packet = self.receive_pdu().await?;
let mut r = ReadCursor::new(&packet.as_ref());
let mut r = ReadCursor::new(packet.as_ref());
let remaining: u16 = r.read()?;
let data = r.remaining();

Expand Down
8 changes: 4 additions & 4 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ impl From<FromHciBytesError> for Error {
}
}

impl<E> From<bt_hci::CmdError<E>> for AdapterError<E> {
fn from(error: bt_hci::CmdError<E>) -> Self {
impl<E> From<bt_hci::controller::CmdError<E>> for AdapterError<E> {
fn from(error: bt_hci::controller::CmdError<E>) -> Self {
match error {
bt_hci::CmdError::Param(p) => Self::Adapter(Error::HciEncode(p)),
bt_hci::CmdError::Controller(p) => Self::Controller(p),
bt_hci::controller::CmdError::Hci(p) => Self::Adapter(Error::HciEncode(p)),
bt_hci::controller::CmdError::Io(p) => Self::Controller(p),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions host/src/packet_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ impl<M: RawMutex, const MTU: usize, const N: usize, const CLIENTS: usize> Packet
return None;
}

return state.alloc(id).map(|p_ref| Packet {
state.alloc(id).map(|p_ref| Packet {
client: id,
p_ref: Some(p_ref),
pool: self,
});
})
})
}

Expand Down
4 changes: 2 additions & 2 deletions host/src/types/l2cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Encode for SignalCode {

impl Decode for SignalCode {
fn decode(src: &[u8]) -> Result<Self, Error> {
Ok(src[0].try_into()?)
src[0].try_into()
}
}

Expand Down Expand Up @@ -276,7 +276,7 @@ impl Encode for LeCreditConnResultCode {

impl Decode for LeCreditConnResultCode {
fn decode(src: &[u8]) -> Result<Self, Error> {
Ok(u16::from_le_bytes([src[0], src[1]]).try_into()?)
u16::from_le_bytes([src[0], src[1]]).try_into()
}
}

Expand Down
Loading

0 comments on commit 20f75d3

Please sign in to comment.