Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support embedded_io_async::ReadReady trait #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/

use embedded_io_async::{Read, Write};
use embedded_io_async::{Read, ReadReady, Write};
use heapless::Vec;
use rand_core::RngCore;

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

pub struct MqttClient<'a, T, const MAX_PROPERTIES: usize, R: RngCore>
where
T: Read + Write,
T: Read + ReadReady + Write,
{
raw: RawMqttClient<'a, T, MAX_PROPERTIES, R>,
}

impl<'a, T, const MAX_PROPERTIES: usize, R> MqttClient<'a, T, MAX_PROPERTIES, R>
where
T: Read + Write,
T: Read + ReadReady + Write,
R: RngCore,
{
pub fn new(
Expand Down Expand Up @@ -221,4 +221,9 @@ where
_ => Err(ReasonCode::ImplementationSpecificError),
}
}

/// Get whether a TCP connection reader is ready.
pub fn read_ready(&mut self) -> Result<bool, ReasonCode> {
self.raw.read_ready()
}
}
18 changes: 13 additions & 5 deletions src/client/raw_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use embedded_io_async::{Read, Write};
use embedded_io_async::{Read, ReadReady, Write};
use heapless::Vec;
use rand_core::RngCore;

Expand Down Expand Up @@ -38,7 +38,7 @@ pub enum Event<'a> {

pub struct RawMqttClient<'a, T, const MAX_PROPERTIES: usize, R: RngCore>
where
T: Read + Write,
T: Read + ReadReady + Write,
{
connection: Option<NetworkConnection<T>>,
buffer: &'a mut [u8],
Expand All @@ -50,7 +50,7 @@ where

impl<'a, T, const MAX_PROPERTIES: usize, R> RawMqttClient<'a, T, MAX_PROPERTIES, R>
where
T: Read + Write,
T: Read + ReadReady + Write,
R: RngCore,
{
pub fn new(
Expand Down Expand Up @@ -470,10 +470,18 @@ where
}
}
}

pub fn read_ready(&mut self) -> Result<bool, ReasonCode> {
if self.connection.is_none() {
return Err(ReasonCode::NetworkError);
}
let conn = self.connection.as_mut().unwrap();
conn.read_ready()
}
}

#[cfg(not(feature = "tls"))]
async fn receive_packet<'c, T: Read + Write>(
async fn receive_packet<'c, T: Read + ReadReady + Write>(
buffer: &mut [u8],
buffer_len: usize,
recv_buffer: &mut [u8],
Expand Down Expand Up @@ -544,7 +552,7 @@ async fn receive_packet<'c, T: Read + Write>(
}

#[cfg(feature = "tls")]
async fn receive_packet<'c, T: Read + Write>(
async fn receive_packet<'c, T: Read + ReadReady + Write>(
buffer: &mut [u8],
buffer_len: usize,
recv_buffer: &mut [u8],
Expand Down
13 changes: 10 additions & 3 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
*/

use crate::packet::v5::reason_codes::ReasonCode;
use embedded_io_async::{Read, Write};
use embedded_io_async::{Read, ReadReady, Write};

pub struct NetworkConnection<T>
where
T: Read + Write,
T: Read + ReadReady + Write,
{
io: T,
}

/// Network connection represents an established TCP connection.
impl<T> NetworkConnection<T>
where
T: Read + Write,
T: Read + ReadReady + Write,
{
/// Create a new network handle using the provided IO implementation.
pub fn new(io: T) -> Self {
Expand All @@ -59,4 +59,11 @@ where
.await
.map_err(|_| ReasonCode::NetworkError)
}

/// Get whether a TCP connection reader is ready.
pub fn read_ready(&mut self) -> Result<bool, ReasonCode> {
self.io.read_ready()
.map_err(|_| ReasonCode::NetworkError)
}

}
Loading