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
//! This module demonstrates the use of [coap_handler_implementations::SimpleCBORHandler]
//!
//! It is only present when built with `alloc`.

use alloc::string::{String, ToString};
use alloc::{vec, vec::Vec};
use coap_handler_implementations::{
    new_dispatcher, HandlerBuilder, SimpleCBORHandler, SimpleCBORWrapper,
};
use coap_handler::Handler;
use coap_numbers;
use core::default::Default;
use serde::{Deserialize, Serialize};

/// A demo struct with several members, all of which implement serde traits.
///
/// Using SimpleCBORHandler, a CoAP GETter and SETter are implemented. To avoid being boring, both
/// do some checks: The setter rejects attempts to write HTML, and the getter refuses to answer if
/// the "hidden" property is true.
#[derive(Serialize, Deserialize, Clone)]
pub struct MyCBOR {
    hidden: bool,
    number: usize,
    label: String,
    list: Vec<usize>,
}

impl Default for MyCBOR {
    fn default() -> Self {
        Self {
            hidden: false,
            number: 32,
            label: "Hello".to_string(),
            list: vec![1, 2, 3],
        }
    }
}

impl SimpleCBORHandler for MyCBOR {
    type Get = MyCBOR;
    type Put = MyCBOR;
    type Post = MyCBOR;

    fn get(&mut self) -> Result<MyCBOR, u8> {
        if self.hidden {
            return Err(coap_numbers::code::FORBIDDEN);
        }
        Ok(self.clone())
    }

    fn put(&mut self, new: &MyCBOR) -> u8 {
        if new.label.contains("<") {
            // No HTML injection please ;-)
            return coap_numbers::code::BAD_REQUEST;
        }
        *self = new.clone();
        coap_numbers::code::CHANGED
    }

    fn post(&mut self, _: &MyCBOR) -> u8 {
        coap_numbers::code::METHOD_NOT_ALLOWED
    }
}

/// Build a handler that gives access to a single [MyCBOR] object
///
/// This can be built with alloc alone (and even that is just needed because it's used inside the
/// MyCBOR type), and thus has no external synchronization -- the MyCBOR is owned by the handler,
/// and can only be accessed through the CoAP interface. For an alternative, see
/// [double_cbor_with_access]().
pub fn single_cbor_tree() -> impl Handler {
    let cbor: MyCBOR = Default::default();

    let handler = new_dispatcher()
        .at(
            &[".well-known", "core"],
            coap_handler_implementations::TypedStaticResponse {
                payload: b"</cbor>;ct=60",
                contentformat: &[40],
            },
        )
        .at(&["cbor"], SimpleCBORWrapper::new(cbor));

    handler
}

/// Build a handler that gives access to a single [MyCBOR] object on two paths, and to the rest of
/// the application
///
/// As the MyCBOR object is now stored in an Arc and referenced through a Mutex, it can be in two
/// places in the tree at the same time, and even be accessed by the application at the same time.
#[cfg(feature = "std")]
pub fn double_cbor_with_access() -> (impl Handler, std::sync::Arc<std::sync::Mutex<MyCBOR>>) {
    let cbor: std::sync::Arc<std::sync::Mutex<MyCBOR>> = Default::default();

    let handler = new_dispatcher()
        .at(
            &[".well-known", "core"],
            coap_handler_implementations::TypedStaticResponse {
                payload: b"</cbor/1>;ct=60,</cbor/2>;ct=60",
                contentformat: &[40],
            },
        )
        .at(
            &["cbor", "1"],
            SimpleCBORWrapper::new(TryingThroughMutex(cbor.clone())),
        )
        .at(
            &["cbor", "2"],
            SimpleCBORWrapper::new(TryingThroughMutex(cbor.clone())),
        );

    (handler, cbor)
}

/// Helper struct that accesses a SimpleCBORHandler intrough an `Arc<Mutex<_>>`, thus allowing easiy
/// simultaneous access.
///
/// When implementing SimpelCBORHandler, it does not wait for the lock, but rather fails with a
/// 5.03 Service Unavailable that usually prompts the client to retry. Note that this will not
/// happen ever if the items are just accessed through different paths on the same handler, and
/// neither will be if they are only ever locked outside the CoAP server's main loop. (And even
/// then, unless they're locked for long, it's very unlikely to be hit by chance).
///
/// TBD: Send a "Max-Age: 0" option along to indicate that the client can try again right away
/// rather than wait the usual 60 seconds.
///
/// TBD: This may be a nice addition to the [coap_handler] crate in general (but needs the
/// introduction of a `std` feature there).
#[cfg(feature = "std")]
pub struct TryingThroughMutex<T: SimpleCBORHandler>(pub std::sync::Arc<std::sync::Mutex<T>>);

#[cfg(feature = "std")]
impl<T: SimpleCBORHandler> SimpleCBORHandler for TryingThroughMutex<T> {
    type Get = T::Get;
    type Put = T::Put;
    type Post = T::Post;

    fn get(&mut self) -> Result<Self::Get, u8> {
        Ok(self
            .0
            .try_lock()
            .map_err(|_| coap_numbers::code::SERVICE_UNAVAILABLE)?
            .get()?)
    }

    fn put(&mut self, new: &Self::Put) -> u8 {
        self.0
            .try_lock()
            .map(|mut s| s.put(new))
            .unwrap_or(coap_numbers::code::SERVICE_UNAVAILABLE)
    }

    fn post(&mut self, request: &Self::Post) -> u8 {
        self.0
            .try_lock()
            .map(|mut s| s.post(request))
            .unwrap_or(coap_numbers::code::SERVICE_UNAVAILABLE)
    }
}