|
1 | 1 | #![no_std] |
2 | | -/// MQTT Request/response Handling |
3 | | -/// |
4 | | -/// # Overview |
5 | | -/// This library is intended to be an easy way to handle inbound requests automatically. |
6 | | -/// |
7 | | -/// Handler functions can be associated with the library to be automatically called whenever a |
8 | | -/// specified request is received, and the handler will automatically be invoked when the request |
9 | | -/// is received. |
10 | | -/// |
11 | | -/// ## Limitations |
12 | | -/// * The `poll()` function has a somewhat odd signature (using a function to provide the `Context` |
13 | | -/// and call the handler) due to required compatibility with RTIC and unlocked resources. |
14 | | -/// |
15 | | -/// * Handlers may only be closures that do not capture any local resources. Instead, move local |
16 | | -/// captures into the `Context`, which will be provided to the handler in the function call. |
17 | | -/// |
| 2 | +//! MQTT Request/response Handling |
| 3 | +//! |
| 4 | +//! # Overview |
| 5 | +//! This library is intended to be an easy way to handle inbound requests automatically. |
| 6 | +//! |
| 7 | +//! Handler functions can be associated with the library to be automatically called whenever a |
| 8 | +//! specified request is received, and the handler will automatically be invoked with the request |
| 9 | +//! data. |
| 10 | +//! |
| 11 | +//! ## Limitations |
| 12 | +//! * The `poll()` function has a somewhat odd signature (using a function to provide the `Context` |
| 13 | +//! and call the handler) due to required compatibility with RTIC and unlocked resources. |
| 14 | +//! |
| 15 | +//! * Handlers may only be closures that do not capture any local resources. Instead, move local |
| 16 | +//! captures into the `Context`, which will be provided to the handler in the function call. |
| 17 | +//! |
| 18 | +//! ## Example |
| 19 | +//! ```no_run |
| 20 | +//! # use embedded_nal::TcpClientStack; |
| 21 | +//! type Error = minireq::Error< |
| 22 | +//! // Your network stack error type |
| 23 | +//! # <std_embedded_nal::Stack as TcpClientStack>::Error |
| 24 | +//! >; |
| 25 | +//! |
| 26 | +//! struct Context {} |
| 27 | +//! |
| 28 | +//! #[derive(serde::Serialize, serde::Deserialize)] |
| 29 | +//! struct Request { |
| 30 | +//! data: u32, |
| 31 | +//! } |
| 32 | +//! |
| 33 | +//! // Handler function for processing an incoming request. |
| 34 | +//! pub fn handler( |
| 35 | +//! context: &mut Context, |
| 36 | +//! cmd: &str, |
| 37 | +//! data: &[u8] |
| 38 | +//! ) -> Result<minireq::Response<128>, Error> { |
| 39 | +//! // Deserialize the request. |
| 40 | +//! let mut request: Request = serde_json_core::from_slice(data)?.0; |
| 41 | +//! |
| 42 | +//! request.data = request.data.wrapping_add(1); |
| 43 | +//! |
| 44 | +//! Ok(minireq::Response::data(request)) |
| 45 | +//! } |
| 46 | +//! |
| 47 | +//! // Construct the client |
| 48 | +//! let mut client: minireq::Minireq<Context, _, _, 128, 1> = minireq::Minireq::new( |
| 49 | +//! // Constructor arguments |
| 50 | +//! # std_embedded_nal::Stack::default(), |
| 51 | +//! # "test", |
| 52 | +//! # "minireq", |
| 53 | +//! # "127.0.0.1".parse().unwrap(), |
| 54 | +//! # std_embedded_time::StandardClock::default(), |
| 55 | +//! ) |
| 56 | +//! .unwrap(); |
| 57 | +//! |
| 58 | +//! // Whenever the `/test` command is received, call the associated handler. |
| 59 | +//! // You may add as many handlers as you would like. |
| 60 | +//! client.register("/test", handler).unwrap(); |
| 61 | +//! |
| 62 | +//! // ... |
| 63 | +//! |
| 64 | +//! loop { |
| 65 | +//! // In your main execution loop, continually poll the client to process incoming requests. |
| 66 | +//! client.poll(|handler, command, data| { |
| 67 | +//! let mut context = Context {}; |
| 68 | +//! handler(&mut context, command, data) |
| 69 | +//! }).unwrap(); |
| 70 | +//! } |
| 71 | +//! ``` |
| 72 | +//! |
18 | 73 | use core::fmt::Write; |
19 | 74 |
|
20 | 75 | use minimq::{ |
|
0 commit comments