Skip to main content

Crate pamoja_coap

Crate pamoja_coap 

Source
Expand description

CoAP transport for the pamoja SDK.

CoapTransport implements the core [Transport] and [Receive] traits on top of the pure-Rust [coap_lite] message codec and a UDP socket, so an application can talk to constrained RESTful devices through the same protocol-agnostic surface it uses for every other transport.

CoAP is connectionless: connect binds a local UDP socket and points it at the server, then spawns a background task that decodes inbound datagrams. A send is a CoAP PUT to a resource path, and a subscribe registers an RFC 7641 observe on a resource so the server’s notifications are forwarded to an internal queue that recv drains.

Delivery follows the configured Reliability: Reliability::Confirmable messages are acknowledged with retransmission (at-least-once), while Reliability::NonConfirmable messages are fire-and-forget (at-most-once), which suits the cheapest, most power-constrained devices. A confirmable request the server resets, or answers with a 4.xx or 5.xx code, fails rather than counting as delivered, and a notification is delivered under the path its observation named, since a notification carries only the registration’s token.

CoapServer is the other end: a gateway that takes the readings nodes send it and holds the resources they observe, through the same traits.

§Examples

use pamoja_core::{Receive, Transport};
use pamoja_coap::{CoapConfig, CoapTransport};

let mut transport = CoapTransport::new(CoapConfig::new("localhost", 5683));
transport.connect().await?;
transport.subscribe("sensors/temperature").await?;
transport.send("actuators/valve", b"open").await?;

if let Some(message) = transport.recv().await? {
    println!("{}: {} bytes", message.topic, message.payload.len());
}

Structs§

CoapConfig
Connection settings for a CoapTransport.
CoapPublisher
A handle that sets the state of a server’s resources from anywhere, while another task waits on the server’s recv.
CoapServer
A CoAP server that implements the core [Transport] and [Receive] traits.
CoapTransport
A CoAP client that implements the core [Transport] trait.
Message
A message that arrived on a subscribed topic.

Enums§

Reliability
The delivery guarantee applied to published and subscribed messages.