1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#![cfg(feature = "l2cap")]
use crate::{sys, Result};
/// A Bluetooth LE L2CAP Connection-oriented Channel (CoC)
#[derive(Debug)]
pub struct L2capChannel {
pub(crate) reader: sys::l2cap_channel::L2capChannelReader,
pub(crate) writer: sys::l2cap_channel::L2capChannelWriter,
}
/// Reader half of a L2CAP Connection-oriented Channel (CoC)
#[derive(Debug)]
pub struct L2capChannelReader {
reader: sys::l2cap_channel::L2capChannelReader,
}
/// Writerhalf of a L2CAP Connection-oriented Channel (CoC)
#[derive(Debug)]
pub struct L2capChannelWriter {
writer: sys::l2cap_channel::L2capChannelWriter,
}
impl L2capChannel {
/// Read a packet from the L2CAP channel.
///
/// The packet is written to the start of `buf`, and the packet length is returned.
#[inline]
pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.reader.read(buf).await
}
/// Write a packet to the L2CAP channel.
#[inline]
pub async fn write(&mut self, packet: &[u8]) -> Result<()> {
self.writer.write(packet).await
}
/// Close the L2CAP channel.
///
/// This closes the entire channel, in both directions (reading and writing).
///
/// The channel is automatically closed when `L2capChannel` is dropped, so
/// you don't need to call this explicitly.
#[inline]
pub async fn close(&mut self) -> Result<()> {
self.writer.close().await
}
/// Split the channel into read and write halves.
#[inline]
pub fn split(self) -> (L2capChannelReader, L2capChannelWriter) {
let Self { reader, writer } = self;
(L2capChannelReader { reader }, L2capChannelWriter { writer })
}
}
impl L2capChannelReader {
/// Read a packet from the L2CAP channel.
///
/// The packet is written to the start of `buf`, and the packet length is returned.
#[inline]
pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.reader.read(buf).await
}
/// Try reading a packet from the L2CAP channel.
///
/// The packet is written to the start of `buf`, and the packet length is returned.
///
/// If no packet is immediately available for reading, this returns an error with kind `NotReady`.
#[inline]
pub fn try_read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.reader.try_read(buf)
}
/// Close the L2CAP channel.
///
/// This closes the entire channel, not just the read half.
///
/// The channel is automatically closed when both the `L2capChannelWriter`
/// and `L2capChannelReader` are dropped, so you don't need to call this explicitly.
#[inline]
pub async fn close(&mut self) -> Result<()> {
self.reader.close().await
}
}
impl L2capChannelWriter {
/// Write a packet to the L2CAP channel.
///
/// If the buffer is full, this will wait until there's buffer space for the packet.
#[inline]
pub async fn write(&mut self, packet: &[u8]) -> Result<()> {
self.writer.write(packet).await
}
/// Try writing a packet to the L2CAP channel.
///
/// If there's no buffer space, this returns an error with kind `NotReady`.
#[inline]
pub fn try_write(&mut self, packet: &[u8]) -> Result<()> {
self.writer.try_write(packet)
}
/// Close the L2CAP channel.
///
/// This closes the entire channel, not just the write half.
///
/// The channel is automatically closed when both the `L2capChannelWriter`
/// and `L2capChannelReader` are dropped, so you don't need to call this explicitly.
#[inline]
pub async fn close(&mut self) -> Result<()> {
self.writer.close().await
}
}