async_coap/send_desc/
ping.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 for sending a CoAP ping.
19#[derive(Debug)]
20pub struct Ping;
21
22impl Ping {
23    /// Creates a new instance of `Ping`.
24    #[inline]
25    pub fn new() -> Ping {
26        Ping
27    }
28}
29
30impl Default for Ping {
31    #[inline]
32    fn default() -> Self {
33        Ping
34    }
35}
36
37impl<IC: InboundContext> SendDesc<IC> for Ping {
38    fn write_options(
39        &self,
40        _msg: &mut dyn OptionInsert,
41        _socket_addr: &IC::SocketAddr,
42        _start: Bound<OptionNumber>,
43        _end: Bound<OptionNumber>,
44    ) -> Result<(), Error> {
45        Ok(())
46    }
47
48    fn write_payload(
49        &self,
50        msg: &mut dyn MessageWrite,
51        _socket_addr: &IC::SocketAddr,
52    ) -> Result<(), Error> {
53        msg.set_msg_code(MsgCode::Empty);
54        msg.set_msg_type(MsgType::Con);
55        msg.set_msg_token(MsgToken::EMPTY);
56        Ok(())
57    }
58
59    fn handler(&mut self, context: Result<&IC, Error>) -> Result<ResponseStatus<()>, Error> {
60        let context = context?;
61        if context.message().msg_type() == MsgType::Res {
62            Ok(ResponseStatus::Done(()))
63        } else {
64            Err(Error::BadResponse)
65        }
66    }
67}