Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ jobs:
run: cargo build --no-default-features --features embassy,defmt
- name: Build | Examples
run: cargo build --examples --features log
- name: Build | Examples - defmt
run: export DEFMT_LOG=trace; cargo check --examples --features std,defmt
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,47 +50,47 @@ async-compat = "0.2" # For the `mqtt_client` example

[[example]]
name = "captive_portal"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "dhcp_client"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "dhcp_server"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "http_client"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "http_server"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "mdns_responder"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "mdns_service_responder"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "ws_client"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "ws_server"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "nal_std"
required-features = ["std", "log"]
required-features = ["std"]

[[example]]
name = "mqtt_client"
required-features = ["std", "embedded-svc", "log"]
required-features = ["std", "embedded-svc"]

[workspace]
members = [
Expand Down
16 changes: 13 additions & 3 deletions edge-captive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ pub fn reply(
let buf = Buf(buf, 0);

let message = domain::base::Message::from_octets(request)?;
debug!("Processing message with header: {:?}", message.header());
debug!(
"Processing message with header: {:?}",
debug2format!(message.header())
);

let mut responseb = domain::base::MessageBuilder::from_target(buf)?;

Expand All @@ -106,10 +109,17 @@ pub fn reply(
Ttl::from_duration_lossy(ttl),
A::from_octets(ip[0], ip[1], ip[2], ip[3]),
);
debug!("Answering {:?} with {:?}", question, record);
debug!(
"Answering {:?} with {:?}",
debug2format!(question),
debug2format!(record)
);
answerb.push(record)?;
} else {
debug!("Question {:?} is not of type A, not answering", question);
debug!(
"Question {:?} is not of type A, not answering",
debug2format!(question)
);
}
}

Expand Down
23 changes: 23 additions & 0 deletions edge-http/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@ where
}
}

#[cfg(feature = "defmt")]
impl<E> defmt::Format for Error<E>
where
E: defmt::Format,
{
fn format(&self, f: defmt::Formatter<'_>) {
match self {
Self::InvalidHeaders => defmt::write!(f, "Invalid HTTP headers or status line"),
Self::InvalidBody => defmt::write!(f, "Invalid HTTP body"),
Self::TooManyHeaders => defmt::write!(f, "Too many HTTP headers"),
Self::TooLongHeaders => defmt::write!(f, "HTTP headers section is too long"),
Self::TooLongBody => defmt::write!(f, "HTTP body is too long"),
Self::IncompleteHeaders => defmt::write!(f, "HTTP headers section is incomplete"),
Self::IncompleteBody => defmt::write!(f, "HTTP body is incomplete"),
Self::InvalidState => defmt::write!(f, "Connection is not in requested state"),
Self::HeadersMismatchError(e) => defmt::write!(f, "Headers mismatch: {}", e),
Self::WsUpgradeError(e) => defmt::write!(f, "WebSocket upgrade error: {}", e),
Self::ConnectionClosed => defmt::write!(f, "Connection closed"),
Self::Io(e) => defmt::write!(f, "{}", e),
}
}
}

#[cfg(feature = "std")]
impl<E> std::error::Error for Error<E> where E: std::error::Error {}

Expand Down
58 changes: 47 additions & 11 deletions edge-http/src/io/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ where
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum HandlerError<T, E> {
Io(T),
Connection(Error<T>),
Expand Down Expand Up @@ -466,22 +467,26 @@ pub async fn handle_connection<H, T, const N: usize>(
T: Read + Write + Readable + TcpSplit + TcpShutdown,
{
let close = loop {
debug!("Handler task {}: Waiting for a new request", task_id);
debug!(
"Handler task {}: Waiting for a new request",
display2format!(task_id)
);

if let Some(keepalive_timeout_ms) = keepalive_timeout_ms {
let wait_data = with_timeout(keepalive_timeout_ms, io.readable()).await;
match wait_data {
Err(WithTimeoutError::Timeout) => {
info!(
"Handler task {}: Closing connection due to inactivity",
task_id
display2format!(task_id)
);
break true;
}
Err(e) => {
warn!(
"Handler task {}: Error when handling request: {:?}",
task_id, e
display2format!(task_id),
debug2format!(e)
);
break true;
}
Expand All @@ -493,25 +498,32 @@ pub async fn handle_connection<H, T, const N: usize>(

match result {
Err(HandlerError::Connection(Error::ConnectionClosed)) => {
debug!("Handler task {}: Connection closed", task_id);
debug!(
"Handler task {}: Connection closed",
display2format!(task_id)
);
break false;
}
Err(e) => {
warn!(
"Handler task {}: Error when handling request: {:?}",
task_id, e
display2format!(task_id),
debug2format!(e)
);
break true;
}
Ok(needs_close) => {
if needs_close {
debug!(
"Handler task {}: Request complete; closing connection",
task_id
display2format!(task_id)
);
break true;
} else {
debug!("Handler task {}: Request complete", task_id);
debug!(
"Handler task {}: Request complete",
display2format!(task_id)
);
}
}
}
Expand All @@ -521,7 +533,8 @@ pub async fn handle_connection<H, T, const N: usize>(
if let Err(e) = io.close(Close::Both).await {
warn!(
"Handler task {}: Error when closing the socket: {:?}",
task_id, e
display2format!(task_id),
debug2format!(e)
);
}
} else {
Expand Down Expand Up @@ -557,6 +570,20 @@ where
}
}

#[cfg(feature = "defmt")]
impl<C, E> defmt::Format for HandleRequestError<C, E>
where
C: defmt::Format,
E: defmt::Format,
{
fn format(&self, f: defmt::Formatter<'_>) {
match self {
Self::Connection(e) => defmt::write!(f, "Connection error: {}", e),
Self::Handler(e) => defmt::write!(f, "Handler error: {}", e),
}
}
}

impl<C, E> embedded_io_async::Error for HandleRequestError<C, E>
where
C: Debug + embedded_io_async::Error,
Expand Down Expand Up @@ -690,15 +717,21 @@ impl<const P: usize, const B: usize, const N: usize> Server<P, B, N> {
unwrap!(tasks
.push(async move {
loop {
debug!("Handler task {}: Waiting for connection", task_id);
debug!(
"Handler task {}: Waiting for connection",
display2format!(task_id)
);

let io = {
let _guard = mutex.lock().await;

acceptor.accept().await.map_err(Error::Io)?.1
};

debug!("Handler task {}: Got connection request", task_id);
debug!(
"Handler task {}: Got connection request",
display2format!(task_id)
);

handle_connection::<_, _, N>(
io,
Expand All @@ -715,7 +748,10 @@ impl<const P: usize, const B: usize, const N: usize> Server<P, B, N> {

let (result, _) = embassy_futures::select::select_slice(&mut tasks).await;

warn!("Server processing loop quit abruptly: {:?}", result);
warn!(
"Server processing loop quit abruptly: {:?}",
debug2format!(result)
);

result
}
Expand Down
Loading
Loading