pub fn full_application_tree(
main_log: Option<&'static Log>,
) -> impl Handler + ReportingExpand description
Build a handler that contains all the demo applications
Note that no log::Log is created because that needs to be global, created-only-once and available as early in the application start-up as possible as per the log crate’s design; you’d have to pass one in if you want to view the log through CoAP.
The resources /message/info and /message/warn for creating log messages will be available independently thereof as long as the with-log feature is active.
Examples found in repository?
examples/std_embedded_nal_minicoapserver.rs (line 19)
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}More examples
examples/std_embedded_nal_minicoaptcpserver.rs (line 17)
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}examples/std_embedded_nal_coap.rs (line 17)
6async fn run<S>(stack: &mut S)
7where
8 S: embedded_nal_async::UdpStack,
9{
10 let mut sock = stack
11 .bind_multiple(core::net::SocketAddr::new("::".parse().unwrap(), 5683))
12 .await
13 .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 info!("Server is ready.");
20
21 let coap = embedded_nal_coap::CoAPShared::<3>::new();
22 let (client, server) = coap.split();
23
24 // going with an embassy_futures join instead of an async_std::task::spawn b/c CoAPShared is not
25 // Sync, and async_std expects to work in multiple threads
26 embassy_futures::join::join(
27 async {
28 use rand::SeedableRng;
29 server
30 .run(
31 &mut sock,
32 &mut handler,
33 &mut rand::rngs::StdRng::from_entropy(),
34 )
35 .await
36 .expect("UDP error")
37 },
38 run_client_operations(client),
39 )
40 .await;
41}examples/coaplite.rs (line 10)
7fn main() {
8 let log = Some(coap_message_demos::log::Log::start_once());
9
10 let mut handler = coap_message_demos::full_application_tree(log);
11
12 let socket = UdpSocket::bind("localhost:5683").unwrap();
13 let mut buf = [0; 1280];
14
15 loop {
16 let (size, src) = socket.recv_from(&mut buf).expect("Didn't receive data");
17
18 let packet = Packet::from_bytes(&buf[..size]).unwrap();
19 let request = CoapRequest::from_packet(packet, src);
20
21 let extracted = handler.extract_request_data(&request.message);
22
23 let mut response = request.response.unwrap();
24 match extracted {
25 Ok(extracted) => {
26 if let Err(e2) = handler.build_response(&mut response.message, extracted) {
27 response.message.payload = Default::default();
28 response.message.clear_all_options();
29 e2.render(&mut response.message).unwrap();
30 }
31 }
32 Err(e) => {
33 e.render(&mut response.message).unwrap();
34 }
35 }
36
37 let packet = response.message.to_bytes().unwrap();
38 socket
39 .send_to(&packet[..], &src)
40 .expect("Could not send the data");
41 }
42}examples/coap_crate.rs (line 26)
8fn main() {
9 let addr = "localhost:5683";
10
11 let log = coap_message_demos::log::Log::start_once();
12
13 Runtime::new().unwrap().block_on(async move {
14 let server = Server::new_udp(addr).unwrap();
15 println!("Server up on {}", addr);
16 info!("Server up on {}", addr);
17
18 use coap_handler::Handler;
19
20 // Unlike in coap 0.11, the run closure is now Fn, so we can't have an exclusive reference
21 // to a handler but need to fan out. To avoid the need for a mutex, we just use a handler
22 // factory.
23 //
24 // Note that this may have "funny" effects w/rt the CBOR editable items, which are shared
25 // only across handlers spawned from a single full_application_tree call.
26 let handler = || coap_message_demos::full_application_tree(Some(log));
27
28 server
29 .run(
30 move |mut request: Box<coap_lite::CoapRequest<std::net::SocketAddr>>| {
31 let mut handler = handler();
32
33 // We can just unwrap because the tokio based surver unwinds crashing handlers.
34 // We could still choose to rather render the errors, because a) it'd provide
35 // concrete information to the client and b) it sends a response instead of
36 // leaving the client hanging -- see the coap-lite example for how that is
37 // done.
38
39 let extracted = handler.extract_request_data(&request.message).unwrap();
40
41 if let Some(r) = request.response.as_mut() {
42 handler.build_response(&mut r.message, extracted).unwrap();
43 }
44
45 async { request }
46 },
47 )
48 .await
49 .unwrap();
50 });
51}