Skip to content

Commit

Permalink
feat(coap): auto-start CoAP server as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Jan 30, 2025
1 parent 1cdd055 commit fc9149f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions examples/coap-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@

use ariel_os::debug::log::info;

/// Run a CoAP stack without serving any actual resources.
#[ariel_os::task(autostart)]
async fn coap_run() {
use coap_handler_implementations::new_dispatcher;

let handler = new_dispatcher();
ariel_os::coap::coap_run(handler).await;
}

#[ariel_os::task(autostart)]
async fn run_client_operations() {
use coap_request::Stack;
Expand Down
4 changes: 4 additions & 0 deletions laze-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ modules:
# will have their own entry point when they exist.
depends:
- coap
env:
global:
FEATURES:
- ariel-os/coap-server

- name: coap-client
help: Support for CoAP client functionality.
Expand Down
13 changes: 12 additions & 1 deletion src/ariel-os-coap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ lakers = { version = "0.7.2", default-features = false }
ariel-os-debug.workspace = true
ariel-os-embassy = { workspace = true, features = ["net"] }
ariel-os-random = { workspace = true, features = ["csprng"] }
ariel-os-macros = { path = "../ariel-os-macros" }
static_cell = "2.1.0"

# FIXME: Should go out eventually
Expand All @@ -43,9 +44,19 @@ embedded-io-async = "0.6.1"
workspace = true

[features]
# Enables the coap_run function through which an own server is passed in. The
# crate setting this must call `ariel_os{_,::}coap::coap_run()`; if not set,
# that function is run with a default minimal server automatically (as is
# required by the implementation to have client functionality).
#
# At the feature level, this is a bit of a misnomer, could also be
# called "manual-server-start"; the name is chosen to align with
# laze's name for this (where coap-server makes more sense).
coap-server = []

## Enables an arbitrary set of features in dependencies where dependencies fail
## if no features are configured at all.
doc = ["embassy-net/proto-ipv6", "embassy-net/medium-ip"]
doc = ["embassy-net/proto-ipv6", "embassy-net/medium-ip", "coap-server"]

## Enables defmt logging of coapcore
defmt = ["coapcore/defmt"]
22 changes: 22 additions & 0 deletions src/ariel-os-coap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#![no_std]
#![deny(missing_docs)]
#![deny(clippy::pedantic)]
// for #[ariel_os_macros::task(autostart)]
#![feature(impl_trait_in_assoc_type, used_with_arg)]

// Moving work from https://github.com/embassy-rs/embassy/pull/2519 in here for the time being
mod udp_nal;
Expand Down Expand Up @@ -63,7 +65,12 @@ mod demo_setup {
/// # Panics
///
/// This can only be run once, as it sets up a system wide CoAP handler.
#[cfg(feature = "coap-server")]
pub async fn coap_run(handler: impl coap_handler::Handler + coap_handler::Reporting) -> ! {
coap_run_impl(handler).await
}

async fn coap_run_impl(handler: impl coap_handler::Handler + coap_handler::Reporting) -> ! {
static COAP: StaticCell<embedded_nal_coap::CoAPShared<CONCURRENT_REQUESTS>> = StaticCell::new();

let stack = ariel_os_embassy::net::network_stack().await.unwrap();
Expand Down Expand Up @@ -155,3 +162,18 @@ pub async fn coap_client(
.await // Not an actual await, just a convenient way to see which executor is running
.expect("CoAP client can currently only be used from the thread the network is bound to")
}

/// Auto-started CoAP server that serves two purposes:
///
/// * It provides the backend for the CoAP client operation (which leaves message sending to that
/// task).
/// * It runs any CoAP server components provided by the OS (none yet).
#[cfg(not(feature = "coap-server"))]
#[ariel_os_macros::task(autostart)]
async fn coap_run() {
use coap_handler_implementations::new_dispatcher;

// FIXME: Provide an "all system components" constructor in this crate.
let handler = new_dispatcher();
coap_run_impl(handler).await;
}
7 changes: 7 additions & 0 deletions src/ariel-os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ dns = ["ariel-os-embassy/dns"]
mdns = ["ariel-os-embassy/mdns"]
## Enables support for [CoAP](https://ariel-os.github.io/ariel-os/dev/docs/book/tooling/coap.html).
coap = ["dep:ariel-os-coap", "random"]
## Support for applications to set up own CoAP server handlers.
##
## When an application selects this, it needs to run `ariel_os::coap_run()`
## in a task; otherwise, other components (eg. system components that also
## run on the CoAP server, or the CoAP client that depends on the server
## loop to run) get stalled.
coap-server = ["coap", "ariel-os-coap/coap-server"]
## Selects static IP configuration.
network-config-static = ["ariel-os-embassy/network-config-static"]

Expand Down

0 comments on commit fc9149f

Please sign in to comment.