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
use embedded_time::duration::Milliseconds;
use embedded_time::Clock;
use kwap_common::prelude::*;
use no_std_net::SocketAddr;
use crate::config::Config;
use crate::core::{Core, Error, Secure, What, When};
use crate::net::{Addrd, Socket};
use crate::platform::Platform;
#[cfg(feature = "std")]
use crate::platform::{Std, StdSecure};
use crate::req::{Req, ReqBuilder};
use crate::resp::Resp;
#[cfg(feature = "std")]
use crate::std::{secure, SecureUdpSocket};
#[derive(Clone, Debug)]
pub struct ClientConfig<Cfg: Platform> {
pub clock: Cfg::Clock,
pub sock: Cfg::Socket,
}
#[allow(missing_debug_implementations)]
pub struct Client<Cfg: Platform> {
core: Core<Cfg>,
}
pub trait ClientResultExt<T, Cfg: Platform> {
fn timeout_ok(self) -> Result<Option<T>, Error<Cfg>>;
}
impl<T, Cfg: Platform> ClientResultExt<T, Cfg> for Result<T, Error<Cfg>> {
fn timeout_ok(self) -> Result<Option<T>, Error<Cfg>> {
match self {
| Ok(t) => Ok(Some(t)),
| Err(Error { what: What::MessageNeverAcked,
.. }) => Ok(None),
| Err(e) => Err(e),
}
}
}
#[cfg(feature = "std")]
impl Client<StdSecure> {
pub fn try_new_secure(port: u16) -> secure::Result<Self> {
Client::<StdSecure>::try_new_secure_config(port, Config::default())
}
pub fn try_new_secure_config(port: u16, config: Config) -> secure::Result<Self> {
let clock = crate::std::Clock::new();
std::net::UdpSocket::bind(format!("0.0.0.0:{}", port)).map_err(secure::Error::from).bind(
SecureUdpSocket::try_new_client
).map(|sock|
Client::<StdSecure>::new_config(config, ClientConfig { clock, sock }))
}
}
#[cfg(feature = "std")]
impl Client<Std> {
pub fn new_std() -> Self {
Client::<Std>::new_std_config(Config::default())
}
pub fn new_std_config(config: Config) -> Self {
let clock = crate::std::Clock::new();
let sock = std::net::UdpSocket::bind("0.0.0.0:1111").unwrap();
Client::<Std>::new_config(config, ClientConfig { clock, sock })
}
}
impl<P: Platform> Client<P> {
pub fn new(ClientConfig { clock, sock }: ClientConfig<P>) -> Self {
Self { core: Core::new(clock, sock) }
}
pub fn new_config(config: Config, ClientConfig { clock, sock }: ClientConfig<P>) -> Self {
Self { core: Core::new_config(config, clock, sock) }
}
pub fn ping(&mut self, host: impl AsRef<str>, port: u16) -> Result<(), Error<P>> {
self.core
.ping(host, port)
.bind(|(id, addr)| nb::block!(self.core.poll_ping(id, addr)))
}
pub fn send(&mut self, req: Req<P>) -> Result<Resp<P>, Error<P>> {
self.core
.send_req(req, Secure::IfSupported)
.bind(|(token, addr)| nb::block!(self.core.poll_resp(token, addr)))
}
pub fn listen_multicast(clock: P::Clock, port: u16) -> Result<Addrd<Req<P>>, Error<P>> {
let addr = crate::multicast::all_coap_devices(port);
P::Socket::bind(addr).map_err(|e| When::None.what(What::SockError(e)))
.map(|sock| Self::new(ClientConfig { clock, sock }))
.bind(|mut client| loop {
let start = client.core.clock.try_now().unwrap();
let since_start = |clock: &P::Clock| {
let now = clock.try_now().unwrap();
Milliseconds::<u64>::try_from(now - start).unwrap()
};
match client.core.poll_req() {
| Err(nb::Error::Other(e)) => {
log::error!("{:?}", e);
break Err(e);
},
| Err(nb::Error::WouldBlock)
if since_start(&client.core.clock) > Milliseconds(1000u64) =>
{
log::error!("timeout");
break Err(When::None.what(What::Timeout));
},
| Err(nb::Error::WouldBlock) => (),
| Ok(x) => break Ok(x),
}
})
}
pub fn get(host: SocketAddr, path: impl AsRef<str>) -> ReqBuilder<P> {
ReqBuilder::get(host, path)
}
}