Skip to content

Commit 45620cf

Browse files
author
haijd
committed
Support embedded_io_async::ReadReady trait
1 parent b4c7df8 commit 45620cf

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/client/client.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25-
use embedded_io_async::{Read, Write};
25+
use embedded_io_async::{Read, ReadReady, Write};
2626
use heapless::Vec;
2727
use rand_core::RngCore;
2828

@@ -34,14 +34,14 @@ use super::raw_client::{Event, RawMqttClient};
3434

3535
pub struct MqttClient<'a, T, const MAX_PROPERTIES: usize, R: RngCore>
3636
where
37-
T: Read + Write,
37+
T: Read + ReadReady + Write,
3838
{
3939
raw: RawMqttClient<'a, T, MAX_PROPERTIES, R>,
4040
}
4141

4242
impl<'a, T, const MAX_PROPERTIES: usize, R> MqttClient<'a, T, MAX_PROPERTIES, R>
4343
where
44-
T: Read + Write,
44+
T: Read + ReadReady + Write,
4545
R: RngCore,
4646
{
4747
pub fn new(
@@ -221,4 +221,9 @@ where
221221
_ => Err(ReasonCode::ImplementationSpecificError),
222222
}
223223
}
224+
225+
/// Get whether a TCP connection reader is ready.
226+
pub fn read_ready(&mut self) -> Result<bool, ReasonCode> {
227+
self.raw.read_ready()
228+
}
224229
}

src/client/raw_client.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use embedded_io_async::{Read, Write};
1+
use embedded_io_async::{Read, ReadReady, Write};
22
use heapless::Vec;
33
use rand_core::RngCore;
44

@@ -38,7 +38,7 @@ pub enum Event<'a> {
3838

3939
pub struct RawMqttClient<'a, T, const MAX_PROPERTIES: usize, R: RngCore>
4040
where
41-
T: Read + Write,
41+
T: Read + ReadReady + Write,
4242
{
4343
connection: Option<NetworkConnection<T>>,
4444
buffer: &'a mut [u8],
@@ -50,7 +50,7 @@ where
5050

5151
impl<'a, T, const MAX_PROPERTIES: usize, R> RawMqttClient<'a, T, MAX_PROPERTIES, R>
5252
where
53-
T: Read + Write,
53+
T: Read + ReadReady + Write,
5454
R: RngCore,
5555
{
5656
pub fn new(
@@ -470,10 +470,18 @@ where
470470
}
471471
}
472472
}
473+
474+
pub fn read_ready(&mut self) -> Result<bool, ReasonCode> {
475+
if self.connection.is_none() {
476+
return Err(ReasonCode::NetworkError);
477+
}
478+
let conn = self.connection.as_mut().unwrap();
479+
conn.read_ready()
480+
}
473481
}
474482

475483
#[cfg(not(feature = "tls"))]
476-
async fn receive_packet<'c, T: Read + Write>(
484+
async fn receive_packet<'c, T: Read + ReadReady + Write>(
477485
buffer: &mut [u8],
478486
buffer_len: usize,
479487
recv_buffer: &mut [u8],
@@ -544,7 +552,7 @@ async fn receive_packet<'c, T: Read + Write>(
544552
}
545553

546554
#[cfg(feature = "tls")]
547-
async fn receive_packet<'c, T: Read + Write>(
555+
async fn receive_packet<'c, T: Read + ReadReady + Write>(
548556
buffer: &mut [u8],
549557
buffer_len: usize,
550558
recv_buffer: &mut [u8],

src/network/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@
2323
*/
2424

2525
use crate::packet::v5::reason_codes::ReasonCode;
26-
use embedded_io_async::{Read, Write};
26+
use embedded_io_async::{Read, ReadReady, Write};
2727

2828
pub struct NetworkConnection<T>
2929
where
30-
T: Read + Write,
30+
T: Read + ReadReady + Write,
3131
{
3232
io: T,
3333
}
3434

3535
/// Network connection represents an established TCP connection.
3636
impl<T> NetworkConnection<T>
3737
where
38-
T: Read + Write,
38+
T: Read + ReadReady + Write,
3939
{
4040
/// Create a new network handle using the provided IO implementation.
4141
pub fn new(io: T) -> Self {
@@ -59,4 +59,11 @@ where
5959
.await
6060
.map_err(|_| ReasonCode::NetworkError)
6161
}
62+
63+
/// Get whether a TCP connection reader is ready.
64+
pub fn read_ready(&mut self) -> Result<bool, ReasonCode> {
65+
self.io.read_ready()
66+
.map_err(|_| ReasonCode::NetworkError)
67+
}
68+
6269
}

0 commit comments

Comments
 (0)