Skip to content

Commit 13c212a

Browse files
authored
Merge pull request #23 from Foundation-Devices/cooperation-session
more btp improvements
2 parents 1771065 + d6e7c5e commit 13c212a

File tree

7 files changed

+550
-342
lines changed

7 files changed

+550
-342
lines changed

Cargo.lock

Lines changed: 44 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

abstracted/src/abstracted/abstract_bluetooth.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,17 @@ pub trait AbstractBluetoothChannel {
2525
let cbor = envelope.to_cbor_data();
2626

2727
for chunk in chunk(&cbor) {
28-
self.send(chunk).await.expect("couldn't send");
28+
self.send(chunk).await?;
2929
}
3030

3131
Ok(())
3232
}
3333

3434
async fn receive_envelope(&self, timeout: Duration) -> Result<Envelope> {
3535
let mut unchunker = Dechunker::new();
36-
loop {
36+
while !unchunker.is_complete() {
3737
let bytes = self.receive(timeout).await?;
38-
println!("Received {} bytes over BLE", bytes.len());
3938
unchunker.receive(&bytes)?;
40-
41-
if unchunker.is_complete() {
42-
break;
43-
}
4439
}
4540

4641
let message = unchunker.data().expect("data is complete");

btp/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ edition = "2021"
55
homepage.workspace = true
66

77
[dependencies]
8-
consts = { git = "https://github.com/Foundation-Devices/prime-ble-firmware.git", features = ["dle"] }
8+
bytemuck = { version = "1", features = ["derive"] }
9+
consts = { git = "https://github.com/Foundation-Devices/prime-ble-firmware.git", features = [
10+
"dle",
11+
] }
912
rand = { workspace = true }
13+
thiserror = "2"

btp/src/chunk.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use consts::APP_MTU;
2+
3+
use crate::{Header, CHUNK_DATA_SIZE, HEADER_SIZE};
4+
5+
pub struct Chunker<'a> {
6+
data: &'a [u8],
7+
message_id: u16,
8+
current_index: u16,
9+
total_chunks: u16,
10+
}
11+
12+
impl<'a> Iterator for Chunker<'a> {
13+
type Item = [u8; APP_MTU];
14+
15+
fn next(&mut self) -> Option<Self::Item> {
16+
let start_idx = self.current_index as usize * CHUNK_DATA_SIZE;
17+
if start_idx >= self.data.len() {
18+
return None;
19+
}
20+
21+
let end_idx = (start_idx + CHUNK_DATA_SIZE).min(self.data.len());
22+
let chunk_data = &self.data[start_idx..end_idx];
23+
24+
let header = Header::new(
25+
self.message_id,
26+
self.current_index,
27+
self.total_chunks,
28+
chunk_data.len() as u8,
29+
);
30+
31+
let mut buffer = [0u8; APP_MTU];
32+
buffer[..HEADER_SIZE].copy_from_slice(header.as_bytes());
33+
buffer[HEADER_SIZE..HEADER_SIZE + chunk_data.len()].copy_from_slice(chunk_data);
34+
self.current_index += 1;
35+
36+
Some(buffer)
37+
}
38+
}
39+
40+
/// Splits data into chunks for transmission
41+
pub fn chunk(data: &[u8]) -> Chunker<'_> {
42+
let message_id = rand::Rng::random::<u16>(&mut rand::rng());
43+
let total_chunks = data.len().div_ceil(CHUNK_DATA_SIZE) as u16;
44+
45+
Chunker {
46+
data,
47+
message_id,
48+
current_index: 0,
49+
total_chunks,
50+
}
51+
}

0 commit comments

Comments
 (0)