async_coap/send_desc/observe.rs
1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16use super::*;
17
18/// Send descriptor created by [`CoapRequest::observe`] used for sending CoAP GET requests that
19/// observe changing resources.
20///
21/// This send descriptor can yield multiple results, so it should be used with
22/// [`LocalEndpointExt::send_as_stream`], [`RemoteEndpointExt::send_as_stream`],
23/// and/or [`RemoteEndpointExt::send_to_as_stream`].
24#[derive(Debug)]
25pub struct SendObserve<IC> {
26 phantom: PhantomData<IC>,
27}
28
29impl<IC> SendDescUnicast for SendObserve<IC> {}
30
31impl<IC> Default for SendObserve<IC> {
32 fn default() -> Self {
33 Self::new()
34 }
35}
36
37impl<IC> SendObserve<IC> {
38 pub(crate) fn new() -> Self {
39 Self {
40 phantom: PhantomData,
41 }
42 }
43
44 /// Returns a nonconfirmable version of this send descriptor.
45 #[inline(always)]
46 pub fn nonconfirmable(self) -> Nonconfirmable<SendObserve<IC>> {
47 Default::default()
48 }
49
50 /// Returns a multicast version of this send descriptor.
51 #[inline(always)]
52 pub fn multicast(self) -> Multicast<SendObserve<IC>> {
53 Default::default()
54 }
55}
56
57impl<IC: InboundContext> SendDesc<IC, ()> for SendObserve<IC> {
58 fn delay_to_restart(&self) -> Option<Duration> {
59 // TODO: Derive this value from the `MaxAge` option on the response.
60 Some(Duration::from_secs(60))
61 }
62
63 fn write_options(
64 &self,
65 msg: &mut dyn OptionInsert,
66 socket_addr: &IC::SocketAddr,
67 start: Bound<OptionNumber>,
68 end: Bound<OptionNumber>,
69 ) -> Result<(), Error> {
70 write_options!((msg, socket_addr, start, end) {
71 OBSERVE => Some(OBSERVE_REGISTER),
72 })
73 }
74
75 fn write_payload(
76 &self,
77 msg: &mut dyn MessageWrite,
78 _socket_addr: &IC::SocketAddr,
79 ) -> Result<(), Error> {
80 msg.set_msg_code(MsgCode::MethodGet);
81 Ok(())
82 }
83
84 fn handler(&mut self, context: Result<&IC, Error>) -> Result<ResponseStatus<()>, Error> {
85 context?;
86 Ok(ResponseStatus::Continue)
87 }
88}