1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
// Copyright 2019 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // //! An experimental, asynchronous implementation of the Constrained Application Protocol (CoAP). //! //! This library provides a flexible, [asynchronous](https://rust-lang-nursery.github.io/futures-rs/) //! interface for using and serving CoAP resources. You can either use the [included datagram-based //! back-end](datagram) or you can write your own back-end by implementing [`LocalEndpoint`]. //! //! By implementing [datagram::AsyncDatagramSocket], you can use the [provided datagram-based //! back-end](datagram) with whatever datagram-based network layer you might want, be it UDP, //! DTLS, or even SMS. An implementation for Rust's standard [`std::net::UdpSocket`] //! ([`AllowStdUdpSocket`]) is included. A [Tokio](https://tokio.rs)-based implementation is //! forthcoming. //! //! ## Design //! //! Async-coap works differently than other CoAP libraries, making heavy use of combinators and //! [Futures v0.3]. //! //! [Futures v0.3]: https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.18/futures/ //! //! ### Simple Unicast //! //! For the most part, CoAP was designed for the typical [RESTful] paradigm of sending requests and //! receiving responses. //! In typical CoAP libraries, you have a message type that you would create, populate with your request, //! and then pass to a method to send that request somewhere. Once the response had been received, the result //! would be returned as a single message. Simple. Straightforward. //! //! [RESTful]: https://en.wikipedia.org/wiki/Representational_state_transfer //! //! This similarly straightforward to do with *async-coap*: //! //! ``` //! # #![feature(async_await)] //! # use std::sync::Arc; //! # use futures::{prelude::*,executor::LocalPool,task::LocalSpawnExt}; //! # use async_coap::prelude::*; //! # use async_coap::datagram::{DatagramLocalEndpoint, AllowStdUdpSocket, LoopbackSocket}; //! # use async_coap::null::NullLocalEndpoint; //! # let socket = AllowStdUdpSocket::bind("[::]:0").expect("UDP bind failed"); //! # let local_endpoint = Arc::new(DatagramLocalEndpoint::new(socket)); //! # let mut pool = LocalPool::new(); //! # pool.spawner().spawn_local(local_endpoint.clone().receive_loop_arc(null_receiver!()).map(|_|unreachable!())); //! # let future = async move { //! // Create a `remote_endpoint` instance representing the destination //! // identified by the URI "coap://coap.me/test". This tends to be //! // more convenient than using `local_endpoint` directly. //! let mut remote_endpoint = local_endpoint //! .remote_endpoint_from_uri(uri!("coap://coap.me/test")) //! .expect("Remote endpoint lookup failed"); //! //! // Create a future representing for our request. //! let future = remote_endpoint.send(CoapRequest::get().emit_any_response()); //! //! // Send the request and await the response. //! let response = future.await.expect("CoAP request failed"); //! //! // Print out the response message to standard output. //! println!("Got response: {}", response); //! # }; //! # pool.run_until(future); //! ``` //! //! ### Block2 Reconstruction //! //! However, there are cases where a single request can result in multiple //! responses (i.e. [Multicast] and [Observing]), as well as cases where a single "logical" //! request/response can be spread out across many smaller requests and responses //! (i.e. [Block transfer]). //! //! [Multicast]: https://tools.ietf.org/html/rfc7252#section-8 //! [Observing]: https://tools.ietf.org/html/rfc7641 //! [Block transfer]: https://tools.ietf.org/html/rfc7959 //! //! Let's take Block2 transfers, for example. Many libraries support Block2 transfers, //! often by implementing message reconstruction under-the-hood, which can be very //! convenient. The *async-coap* way to do it is similarly convenient: //! //! ``` //! # #![feature(async_await)] //! # use std::sync::Arc; //! # use futures::{prelude::*,executor::LocalPool,task::LocalSpawnExt}; //! # use async_coap::prelude::*; //! # use async_coap::datagram::{DatagramLocalEndpoint, AllowStdUdpSocket, LoopbackSocket}; //! # use async_coap::null::NullLocalEndpoint; //! # let socket = AllowStdUdpSocket::bind("[::]:0").expect("UDP bind failed"); //! # let local_endpoint = Arc::new(DatagramLocalEndpoint::new(socket)); //! # let mut pool = LocalPool::new(); //! # pool.spawner().spawn_local(local_endpoint.clone().receive_loop_arc(null_receiver!()).map(|_|unreachable!())); //! # let future = async move { //! # let mut remote_endpoint = local_endpoint //! # .remote_endpoint_from_uri(uri!("coap://coap.me/test")) //! # .expect("Remote endpoint lookup failed"); //! // We can change the path on the above remote_endpoint using //! // the `clone_using_rel_ref` method: //! let mut remote_endpoint = remote_endpoint.clone_using_rel_ref(rel_ref!("/large")); //! //! // Create a send descriptor that will reconstruct the block2 parts //! // and return the reconstituted message. //! let send_descriptor = CoapRequest::get() //! .block2(None) //! .emit_successful_collected_response(); //! //! let response = remote_endpoint //! .send(send_descriptor) //! .await //! .expect("CoAP request failed"); //! //! // Print out the response message to standard output. //! println!("Reconstructed response: {}", response); //! # }; //! # pool.run_until(future); //! ``` //! //! ### Inspection //! //! The problem with how this is implemented by other CoAP libraries is that it is difficult //! or impossible to implement things like progress meters. However, with *async-coap*, we //! can add some feedback to the above example very easily using [`inspect`]: //! //! [`inspect`]: send_desc::SendDescExt::inspect //! //! ``` //! # #![feature(async_await)] //! # use std::sync::Arc; //! # use futures::{prelude::*,executor::LocalPool,task::LocalSpawnExt}; //! # use async_coap::prelude::*; //! # use async_coap::datagram::{DatagramLocalEndpoint, AllowStdUdpSocket, LoopbackSocket}; //! # use async_coap::null::NullLocalEndpoint; //! # use async_coap::message::MessageDisplay; //! # let socket = AllowStdUdpSocket::bind("[::]:0").expect("UDP bind failed"); //! # let local_endpoint = Arc::new(DatagramLocalEndpoint::new(socket)); //! # let mut pool = LocalPool::new(); //! # pool.spawner().spawn_local(local_endpoint.clone().receive_loop_arc(null_receiver!()).map(|_|unreachable!())); //! # let future = async move { //! # let mut remote_endpoint = local_endpoint //! # .remote_endpoint_from_uri(uri!("coap://coap.me/test")) //! # .expect("Remote endpoint lookup failed"); //! # let mut remote_endpoint = remote_endpoint.clone_using_rel_ref(rel_ref!("/large")); //! // Create a send descriptor that will reconstruct the block2 parts //! // and return the reconstituted message, printing out each individual //! // message as we go. //! let send_descriptor = CoapRequest::get() //! .block2(None) //! .emit_successful_collected_response() //! .inspect(|context| { //! println!("inspect: Got {}", MessageDisplay(context.message())); //! }); //! //! let response = remote_endpoint //! .send(send_descriptor) //! .await //! .expect("CoAP request failed"); //! //! // Print out the response message to standard output. //! println!("Reconstructed response: {}", response); //! # }; //! # pool.run_until(future); //! ``` //! //! ### Multiple Responses //! //! That's all good and well, but what about requests that generate multiple responses, like //! multicast requests? For that we use a different //! send method: [`send_as_stream`]. Instead of returning a [`Future`], it returns a [`Stream`]. //! This allows us to collect all of the responses: //! //! ```no_run //! # #![feature(async_await)] //! # use std::sync::Arc; //! # use futures::{prelude::*,executor::LocalPool,task::LocalSpawnExt}; //! # use async_coap::prelude::*; //! # use async_coap::datagram::{DatagramLocalEndpoint, AllowStdUdpSocket, LoopbackSocket}; //! # use async_coap::null::NullLocalEndpoint; //! # use async_coap::message::MessageDisplay; //! # use async_coap::Error; //! # use futures_timer::TryFutureExt; //! # use std::time::Duration; //! # let socket = AllowStdUdpSocket::bind("[::]:0").expect("UDP bind failed"); //! # let local_endpoint = Arc::new(DatagramLocalEndpoint::new(socket)); //! # let mut pool = LocalPool::new(); //! # pool.spawner().spawn_local(local_endpoint.clone().receive_loop_arc(null_receiver!()).map(|_|unreachable!())); //! # let future = async move { //! let mut remote_endpoint = local_endpoint //! .remote_endpoint_from_uri(uri!("coap://[FF02::FD]/.well-known/core")) //! .expect("Remote endpoint lookup failed"); //! //! // Don't let the remote_endpoint include //! // a `Uri-Host` host option. //! remote_endpoint.remove_host_option(); //! //! let send_descriptor = CoapRequest::get() //! .multicast() //! .accept(ContentFormat::APPLICATION_LINK_FORMAT) //! .emit_successful_response() //! .include_socket_addr(); //! //! let mut stream = remote_endpoint.send_as_stream(send_descriptor); //! //! while let Some((msg, socket_addr)) //! = stream.next().await.transpose().expect("Error on get") //! { //! println!("From {} got {}", socket_addr, msg); //! } //! # }; //! # pool.run_until(future); //! ``` //! //! [`send_as_stream`]: RemoteEndpointExt::send_as_stream //! [`Future`]: std::future::Future //! [`Stream`]: futures-preview::stream::Stream //! //! ## Future Work //! //! This library is currently in the experimental stage, so there are a lot of additional features //! and mechanisms that aren't yet implemented. Here is a short list: //! //! * Support for "effortless" serving of [observable resources][Observing] //! * Support for [Block1][Block transfer] transfers. //! * Improved support for [observing][Observing] remote resources. //! * Make serving resources easier-to-use. //! * [OSCORE](https://tools.ietf.org/html/draft-ietf-core-object-security) support. //! * Support for supplying alternate [transmission parameters](https://tools.ietf.org/html/rfc7252#section-4.8). //! * Support for burst transmissions for nonconfirmable and multicast requests. //! * Make sending asynchronous responses easier. //! //! ### Support for deeply embedded devices //! //! To the extent possible, the API is designed //! to minimize the amount of memory allocation. While it does currently require the `alloc` crate, //! that requirement will (hopefully) become optional once the [Generic Associated Types][GAT] //! feature lands, without significantly influencing how the API works. This will allow for the //! same API to be used for deeply embedded, resource-constrained devices as would be used for //! other types of non-resource-constrained devices. //! //! [GAT]: https://github.com/rust-lang/rust/issues/44265 //! [`AllowStdUdpSocket`]: crate::datagram::AllowStdUdpSocket //! //! ## Full Example //! //! ``` //! # #![feature(async_await)] //! # //! use std::sync::Arc; //! use futures::{prelude::*,executor::LocalPool,task::LocalSpawnExt}; //! use async_coap::prelude::*; //! use async_coap::datagram::{DatagramLocalEndpoint,AllowStdUdpSocket}; //! //! // Create our asynchronous socket. In this case, it is just an //! // (inefficient) wrapper around the standard rust `UdpSocket`, //! // but that is quite adequate in this case. //! let socket = AllowStdUdpSocket::bind("[::]:0").expect("UDP bind failed"); //! //! // Create a new local endpoint from the socket we just created, //! // wrapping it in a `Arc<>` to ensure it can live long enough. //! let local_endpoint = Arc::new(DatagramLocalEndpoint::new(socket)); //! //! // Create a local execution pool for running our local endpoint. //! let mut pool = LocalPool::new(); //! //! // Quick aside: The `Local` in `LocalPool` is completely unrelated //! // to the `Local` in `LocalEndpoint`: a `LocalEndpoint` refers to //! // the local side of a CoAP connection. A `LocalPool` is just a //! // single-threaded execution pool. A `LocalEndpoint` will run //! // just fine on a `ThreadedPool`. //! //! // Add our local endpoint to the pool, so that it //! // can receive packets. //! pool.spawner().spawn_local(local_endpoint //! .clone() //! .receive_loop_arc(null_receiver!()) //! .map(|err| panic!("Receive loop terminated: {}", err)) //! ); //! //! // Create a remote endpoint instance to represent the //! // device we wish to interact with. //! let remote_endpoint = local_endpoint //! .remote_endpoint_from_uri(uri!("coap://coap.me")) //! .unwrap(); // Will only fail if the URI scheme or authority is unrecognizable //! //! // Create a future that sends a request to a specific path //! // on the remote endpoint, collecting any blocks in the response //! // and returning `Ok(OwnedImmutableMessage)` upon success. //! let future_result = remote_endpoint.send_to( //! rel_ref!("large"), //! CoapRequest::get() // This is a CoAP GET request //! .accept(ContentFormat::TEXT_PLAIN_UTF8) // We only want plaintext //! .block2(Some(Default::default())) // Enable block2 processing //! .emit_successful_collected_response() // Collect all blocks into a single message //! ); //! //! // Wait until we get the result of our request. //! let result = pool.run_until(future_result); //! //! println!("result: {:?}", result); //! ``` //! //! Additional examples can be found in the [module documentation for send descriptors][send-desc] //! and the [documentation for `LocalEndpoint`][LocalEndpoint]. //! //! [send-desc]: send_desc/index.html #![feature(async_await)] #![cfg_attr(not(feature = "std"), no_std)] #![warn(rust_2018_idioms)] #![warn(missing_debug_implementations)] #![warn(clippy::all)] #![warn(missing_docs)] #[macro_use] extern crate log; pub mod arc_guard; use arc_guard::*; #[doc(hidden)] pub use async_coap_uri; pub mod uri { //! A limited subset of items from the URI-handling [`async-coap-uri`] crate. //! //! See the [`async-coap-uri` crate documentation][`async-coap-uri`] for more details. //! //! [`async-coap-uri`]: ../async_coap_uri/index.html pub use async_coap_uri::escape; pub use async_coap_uri::{rel_ref, uri, uri_ref}; pub use async_coap_uri::{RelRef, Uri, UriRef}; pub use async_coap_uri::{RelRefBuf, UriBuf, UriRefBuf}; pub use async_coap_uri::{AnyUriRef, UriDisplay, UriType}; pub use async_coap_uri::{ParseError, ResolveError}; pub use async_coap_uri::UriRawComponents; #[doc(hidden)] pub(super) use async_coap_uri::prelude; #[doc(hidden)] pub use async_coap_uri::{assert_rel_ref_literal, assert_uri_literal, assert_uri_ref_literal}; } pub mod message; pub mod option; pub mod send_desc; use send_desc::*; mod response_status; pub use response_status::ResponseStatus; mod content_format; pub use content_format::ContentFormat; mod socketaddr; pub use socketaddr::SocketAddrExt; pub use socketaddr::ToSocketAddrs; mod block; pub use block::*; mod trans_params; pub use trans_params::*; mod local_endpoint; pub use local_endpoint::*; mod remote_endpoint; pub use remote_endpoint::*; mod send_as_stream; pub use send_as_stream::*; mod receive_as_stream; pub use receive_as_stream::*; mod inbound_context; pub use inbound_context::*; pub mod consts; #[doc(hidden)] pub use consts::*; mod error; pub use error::*; mod util; use util::*; pub mod link_format; #[doc(hidden)] pub use link_format::*; pub mod datagram; pub mod null; mod etag; pub use etag::ETag; use futures::future::BoxFuture; use message::MessageRead; use message::MessageWrite; #[doc(hidden)] pub mod prelude { pub use super::uri::prelude::*; pub use super::LocalEndpoint; pub use super::LocalEndpointExt; pub use super::null_receiver; pub use super::RemoteEndpoint; pub use super::RemoteEndpointExt; pub use super::send_desc::CoapRequest; pub use super::send_desc::SendDescExt; pub use super::send_desc::SendDescMulticast; pub use super::send_desc::SendDescUnicast; pub use super::ContentFormat; pub use super::ResponseStatus; pub use super::message::MsgCode; pub use super::message::MsgCodeClass; pub use super::message::MsgId; pub use super::message::MsgToken; pub use super::message::MsgType; pub use super::option; pub use option::OptionInsert; pub use option::OptionInsertExt; pub use option::OptionIterator; pub use option::OptionIteratorExt; pub use option::OptionKey; pub use option::OptionNumber; pub use super::SocketAddrExt; } use futures::prelude::*; use prelude::*;