std_embedded_nal_minicoaptcpserver/
std_embedded_nal_minicoaptcpserver.rs

1use log::info;
2
3// This function works on *any* TCP stack (plus some custom stuff), including embedded ones -- only main() is what makes
4// this use POSIX sockets.
5//
6// There *is* still a bit of a std dependency in that the code can't know when the next request is
7// here, so right now it's using std::thread::sleep to stay idle, but that's not realted to how
8// coap-message us used here.
9fn run<S>(stack: &mut S)
10where
11    S: embedded_nal::TcpFullStack + embedded_nal_tcpextensions::TcpExactStack,
12{
13    let mut sock = stack.socket().expect("Can't create a socket");
14
15    let log = Some(coap_message_demos::log::Log::start_once());
16
17    let mut handler = coap_message_demos::full_application_tree(log);
18
19    stack.bind(&mut sock, 5683).expect("Can't bind to port");
20    info!("Server is ready.");
21
22    let mut pool = embedded_nal_minimal_coaptcpserver::ServerPool::<S, 4, 1152>::new(sock);
23
24    loop {
25        pool.poll(stack, &mut handler)
26            .expect("Actual error in polling (accepting?)");
27        // See <https://github.com/rust-embedded-community/embedded-nal/issues/47>
28        std::thread::sleep(std::time::Duration::from_millis(50));
29    }
30}
31
32fn main() {
33    let mut stack = std_embedded_nal::Stack::default();
34
35    // If we used any other than the default stack (with its embedded-nal-tcpextensions feature)
36    // let mut stack = embedded_nal_tcpextensions::BufferedStack::<_, 2000>::new(stack);
37
38    run(&mut stack);
39}