Skip to content

Commit

Permalink
docs(examples/coap): Split out minimal server example
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Nov 21, 2024
1 parent efe3212 commit 7d74086
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

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

28 changes: 28 additions & 0 deletions examples/coap-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "coap-server"
version = "0.1.0"
authors = ["Christian Amsüss <[email protected]>"]
license.workspace = true
edition.workspace = true
publish = false

[lints]
workspace = true

[dependencies]
embassy-net = { workspace = true, features = ["udp"] }
embassy-sync = { workspace = true }
heapless = { workspace = true }
ariel-os = { path = "../../src/ariel-os", features = [
"override-network-config",
"coap",
] }
ariel-os-boards = { path = "../../src/ariel-os-boards" }
coap-message = "0.3.2"
coap-message-demos = { version = "0.4.0", default-features = false }
coap-handler = "0.2.0"
coap-handler-implementations = "0.5.0"

static-alloc = { version = "0.2.5", features = ["polyfill"] }

ariel-os-coap = { workspace = true }
45 changes: 45 additions & 0 deletions examples/coap-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# coap server demo

## About

This application starts a minimal CoAP server.

The server offers a single resource, `/hello`, which returns a friendly message.

The default policy allows access to the resource,
but clients can cryptographically verify that they are talking to the right server using its public key.
Both the policy and the key are currently hard-coded;
making the former configurable and the latter dynamic is work in progress.

## Running

* Run on any board with networking, eg. `laze build -b particle-xenon run`.
* [Set up networking](../README.md).
* Run `aiocoap-client`
to list the resources of the device:

```sh
$ pipx install 'aiocoap[all]'
$ aiocoap-client coap://10.42.0.61/.well-known/core --credentials client.diag
# application/link-format content was re-formatted
</hello>
```

If you prefer not to install the CoAP client, you can
replace any call to `aiocoap-client` with `pipx run --spec 'aiocoap[all]' aiocoap-client` instead.

The output tells you there is a `/hello` resource, so read that next:

```sh
$ aiocoap-client coap://10.42.0.61/hello --credentials client.diag
Hello from Ariel OS
```

The argument `--credentials client.diag` tells the client to establish a secure connection.
Without the argument, the requests come through just as well,
but the client has no assurance on the server's identity.

## Further references

There is a [chapter in the book](https://ariel-os.github.io/ariel-os/dev/docs/book/tooling/coap.html)
that describes more concepts and background.
1 change: 1 addition & 0 deletions examples/coap-server/client.cosekey
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{1: 2, -1: 1, -4: h'fb13adeb6518cee5f88417660841142e830a81fe334380a953406a1305e8706b'}
13 changes: 13 additions & 0 deletions examples/coap-server/client.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"coap://10.42.0.61/*": {
"edhoc-oscore": {
"suite": 2,
"method": 3,
"own_cred_style": "by-value",
/ From the server's point of view, this is unauthenticated; we could use any key pair, as long as we send it by value. /
"own_cred": {14: {2: "", 8: {1: {1: 2, 2: h'2b', -1: 1, -2: h'ac75e9ece3e50bfc8ed60399889522405c47bf16df96660a41298cb4307f7eb6', -3: h'6e5de611388a4b8a8211334ac7d37ecb52a387d257e6db3c2a93df21ff3affc8'}}}},
"private_key_file": "client.cosekey",
"peer_cred": {14: {2: "", 8: {1: {1: 2, 2: h'0a', -1: 1, -2: h'bbc34960526ea4d32e940cad2a234148ddc21791a12afbcbac93622046dd44f0', -3: h'4519e257236b2a0ce2023f0931f1f386ca7afda64fcde0108c224c51eabf6072'}}}},
}
},
}
16 changes: 16 additions & 0 deletions examples/coap-server/laze.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apps:
- name: coap-server
env:
global:
CARGO_ENV:
- CONFIG_ISR_STACKSIZE=16384
selects:
- ?release
- network
- random
conflicts:
# see https://github.com/ariel-os/ariel-os/issues/418
- thumbv6m-none-eabi
# no xtensa / riscv gcc on CI
- xtensa
- riscv
37 changes: 37 additions & 0 deletions examples/coap-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![no_main]
#![no_std]
#![feature(impl_trait_in_assoc_type)]
#![feature(used_with_arg)]

// Due to mismatches between CoAP components, coapcore currently needs an allocator. This example
// provides the one that can be made most easily.
extern crate alloc;
use static_alloc::Bump;
#[global_allocator]
static A: Bump<[u8; 1 << 16]> = Bump::uninit();

#[ariel_os::task(autostart)]
async fn coap_run() {
use coap_handler_implementations::{
new_dispatcher, HandlerBuilder, ReportingHandlerBuilder, SimpleRendered,
};

let handler = new_dispatcher()
// We offer a single resource: /hello, which response just with a text string.
.at(&["hello"], SimpleRendered("Hello from Ariel OS"))
.with_wkc();

ariel_os::coap::coap_run(handler).await;
}

// So far, this is necessary boiler plate; see ../../README.md#networking for details
#[ariel_os::config(network)]
fn network_config() -> embassy_net::Config {
use embassy_net::Ipv4Address;

embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 {
address: embassy_net::Ipv4Cidr::new(Ipv4Address::new(10, 42, 0, 61), 24),
dns_servers: heapless::Vec::new(),
gateway: Some(Ipv4Address::new(10, 42, 0, 1)),
})
}
1 change: 1 addition & 0 deletions examples/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ subdirs:
- blinky
- core-sizes
- coap
- coap-server
- device-metadata
- embassy-http-server
- embassy-usb-keyboard
Expand Down

0 comments on commit 7d74086

Please sign in to comment.