std_embedded_nal_minicoapserver/
std_embedded_nal_minicoapserver.rs

1use embedded_nal::UdpFullStack;
2
3use log::info;
4
5// This function works on *any* UdpFullStack, including embedded ones -- only main() is what makes
6// this use POSIX sockets.
7//
8// There *is* still a bit of a std dependency in that the code can't know when the next request is
9// here, so right now it's using std::thread::sleep to stay idle, but that's not realted to how
10// coap-message us used here.
11fn run<S>(stack: &mut S)
12where
13    S: UdpFullStack,
14{
15    let mut sock = stack.socket().expect("Can't create a socket");
16
17    let log = Some(coap_message_demos::log::Log::start_once());
18
19    let mut handler = coap_message_demos::full_application_tree(log);
20
21    stack.bind(&mut sock, 5683).expect("Can't bind to port");
22    info!("Server is ready.");
23
24    loop {
25        match embedded_nal_minimal_coapserver::poll(stack, &mut sock, &mut handler) {
26            Err(embedded_nal::nb::Error::WouldBlock) => {
27                // See <https://github.com/rust-embedded-community/embedded-nal/issues/47>
28                std::thread::sleep(std::time::Duration::from_millis(50));
29            }
30            e => e.expect("UDP error during send/receive"),
31        }
32    }
33}
34
35fn main() {
36    let mut stack = std_embedded_nal::Stack::default();
37
38    run(&mut stack);
39}