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
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};
#[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("<") {
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
}
}
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
}
#[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)
}
#[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)
}
}