coap/lib.rs
1//! Implementation of the [CoAP Protocol][spec].
2//!
3//! This library provides both a client interface (`CoAPClient`)
4//! and a server interface (`CoAPServer`).
5//!
6//! Features:
7//! - CoAP core protocol [RFC 7252](https://tools.ietf.org/rfc/rfc7252.txt)
8//! - CoAP Observe option [RFC 7641](https://tools.ietf.org/rfc/rfc7641.txt)
9//! - *Too Many Requests* Response Code [RFC 8516](https://tools.ietf.org/html/rfc8516)
10//! - Block-Wise Transfers [RFC 7959](https://tools.ietf.org/html/rfc7959)
11//! - DTLS support via [webrtc-rs](https://github.com/webrtc-rs/webrtc)
12//! - Option to provide custom transports for client and server
13//! - Client can perform multiple concurrent requests, like observing and sending requests using
14//! the same underlying socket
15//!
16//!
17//! # Installation
18//!
19//! First add this to your `Cargo.toml`:
20//!
21//! ```toml
22//! [dependencies]
23//! coap = "0.23"
24//! coap-lite = "0.13.3"
25//! tokio = {version = "^1.32", features = ["full"]}
26//! ```
27//!
28//! Then, add this to your crate root:
29//!
30//! ```
31//! extern crate coap;
32//! ```
33//!
34//! # Example
35//!
36//! ## Server:
37//! ```no_run
38//! extern crate coap;
39//!
40//! use coap_lite::{RequestType as Method, CoapRequest};
41//! use coap::Server;
42//! use tokio::runtime::Runtime;
43//! use std::net::SocketAddr;
44//! fn main() {
45//! let addr = "127.0.0.1:5683";
46//! Runtime::new().unwrap().block_on(async move {
47//! let mut server = Server::new_udp(addr).unwrap();
48//! println!("Server up on {}", addr);
49//!
50//! server.run(|mut request: Box<CoapRequest<SocketAddr>>| async {
51//! match request.get_method() {
52//! &Method::Get => println!("request by get {}", request.get_path()),
53//! &Method::Post => println!("request by post {}", String::from_utf8(request.message.payload.clone()).unwrap()),
54//! &Method::Put => println!("request by put {}", String::from_utf8(request.message.payload.clone()).unwrap()),
55//! _ => println!("request by other method"),
56//! };
57
58//! match request.response {
59//! Some(ref mut message) => {
60//! message.message.payload = b"OK".to_vec();
61
62//! },
63//! _ => {}
64//! };
65//! return request
66//! }).await.unwrap();
67//! });
68//! }
69//! ```
70//!
71//! ## Client:
72//! ```no_run
73//! extern crate coap;
74//!
75//! use coap_lite::{RequestType as Method, CoapRequest};
76//! use coap::{UdpCoAPClient};
77//! use tokio::main;
78//! #[tokio::main]
79//! async fn main() {
80//! let url = "coap://127.0.0.1:5683/Rust";
81//! println!("Client request: {}", url);
82//!
83//! let response = UdpCoAPClient::get(url).await.unwrap();
84//! println!("Server reply: {}", String::from_utf8(response.message.payload).unwrap());
85//! }
86
87#[macro_use]
88extern crate alloc;
89
90#[cfg(test)]
91extern crate quickcheck;
92
93pub use self::client::UdpCoAPClient;
94pub use self::observer::Observer;
95pub use self::server::Server;
96pub mod client;
97#[cfg(feature = "dtls")]
98pub mod dtls;
99mod observer;
100pub mod request;
101pub mod server;