Struct coap_lite::Packet

source ·
pub struct Packet {
    pub header: Header,
    pub payload: Vec<u8>,
    /* private fields */
}
Expand description

The CoAP packet.

Fields§

§header: Header§payload: Vec<u8>

Implementations§

source§

impl Packet

source

pub const MAX_SIZE: usize = 1_280usize

Maximum allowed packet size. By default limited to 1280 so that CoAP packets can be sent over TCP or UDP.

source

pub fn new() -> Packet

Creates a new packet.

Examples found in repository?
examples/bytes.rs (line 6)
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
fn main() {
    let mut request = Packet::new();
    request.header.message_id = 23839;
    request.header.code = MessageClass::Request(RequestType::Get);
    request.set_token(vec![0, 0, 57, 116]);
    request.add_option(CoapOption::UriHost, b"localhost".to_vec());
    request.add_option(CoapOption::UriPath, b"tv1".to_vec());
    assert_eq!(
        [
            0x44, 0x01, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0x39, 0x6C, 0x6F,
            0x63, 0x61, 0x6C, 0x68, 0x6F, 0x73, 0x74, 0x83, 0x74, 0x76, 0x31,
        ],
        request.to_bytes().unwrap()[..]
    );

    let response = Packet::from_bytes(&[
        0x64, 0x45, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0xFF, 0x48, 0x65,
        0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21,
    ])
    .unwrap();
    assert_eq!(23839, response.header.message_id);
    assert_eq!(
        MessageClass::Response(ResponseType::Content),
        response.header.code
    );
    assert_eq!(MessageType::Acknowledgement, response.header.get_type());
    assert_eq!([0, 0, 57, 116], response.get_token()[..]);
    assert_eq!(b"Hello World!", &response.payload[..]);
}
source

pub fn options(&self) -> Iter<'_, u16, LinkedList<Vec<u8>>>

Returns an iterator over the options of the packet.

source

pub fn set_token(&mut self, token: Vec<u8>)

Sets the token.

Examples found in repository?
examples/bytes.rs (line 9)
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
fn main() {
    let mut request = Packet::new();
    request.header.message_id = 23839;
    request.header.code = MessageClass::Request(RequestType::Get);
    request.set_token(vec![0, 0, 57, 116]);
    request.add_option(CoapOption::UriHost, b"localhost".to_vec());
    request.add_option(CoapOption::UriPath, b"tv1".to_vec());
    assert_eq!(
        [
            0x44, 0x01, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0x39, 0x6C, 0x6F,
            0x63, 0x61, 0x6C, 0x68, 0x6F, 0x73, 0x74, 0x83, 0x74, 0x76, 0x31,
        ],
        request.to_bytes().unwrap()[..]
    );

    let response = Packet::from_bytes(&[
        0x64, 0x45, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0xFF, 0x48, 0x65,
        0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21,
    ])
    .unwrap();
    assert_eq!(23839, response.header.message_id);
    assert_eq!(
        MessageClass::Response(ResponseType::Content),
        response.header.code
    );
    assert_eq!(MessageType::Acknowledgement, response.header.get_type());
    assert_eq!([0, 0, 57, 116], response.get_token()[..]);
    assert_eq!(b"Hello World!", &response.payload[..]);
}
source

pub fn get_token(&self) -> &[u8]

Returns the token.

Examples found in repository?
examples/bytes.rs (line 31)
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
fn main() {
    let mut request = Packet::new();
    request.header.message_id = 23839;
    request.header.code = MessageClass::Request(RequestType::Get);
    request.set_token(vec![0, 0, 57, 116]);
    request.add_option(CoapOption::UriHost, b"localhost".to_vec());
    request.add_option(CoapOption::UriPath, b"tv1".to_vec());
    assert_eq!(
        [
            0x44, 0x01, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0x39, 0x6C, 0x6F,
            0x63, 0x61, 0x6C, 0x68, 0x6F, 0x73, 0x74, 0x83, 0x74, 0x76, 0x31,
        ],
        request.to_bytes().unwrap()[..]
    );

    let response = Packet::from_bytes(&[
        0x64, 0x45, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0xFF, 0x48, 0x65,
        0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21,
    ])
    .unwrap();
    assert_eq!(23839, response.header.message_id);
    assert_eq!(
        MessageClass::Response(ResponseType::Content),
        response.header.code
    );
    assert_eq!(MessageType::Acknowledgement, response.header.get_type());
    assert_eq!([0, 0, 57, 116], response.get_token()[..]);
    assert_eq!(b"Hello World!", &response.payload[..]);
}
source

pub fn set_option(&mut self, tp: CoapOption, value: LinkedList<Vec<u8>>)

Sets an option’s values.

source

pub fn set_options_as<T: OptionValueType>( &mut self, tp: CoapOption, value: LinkedList<T> )

Sets an option’s values using a structured option value format.

source

pub fn get_option(&self, tp: CoapOption) -> Option<&LinkedList<Vec<u8>>>

Returns an option’s values.

source

pub fn get_options_as<T: OptionValueType>( &self, tp: CoapOption ) -> Option<LinkedList<Result<T, IncompatibleOptionValueFormat>>>

Returns an option’s values all decoded using the specified structured option value format.

source

pub fn get_first_option(&self, tp: CoapOption) -> Option<&Vec<u8>>

Returns an option’s first value as a convenience when only one is expected.

source

pub fn get_first_option_as<T: OptionValueType>( &self, tp: CoapOption ) -> Option<Result<T, IncompatibleOptionValueFormat>>

Returns an option’s first value as a convenience when only one is expected.

source

pub fn add_option(&mut self, tp: CoapOption, value: Vec<u8>)

Adds an option value.

Examples found in repository?
examples/bytes.rs (line 10)
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
fn main() {
    let mut request = Packet::new();
    request.header.message_id = 23839;
    request.header.code = MessageClass::Request(RequestType::Get);
    request.set_token(vec![0, 0, 57, 116]);
    request.add_option(CoapOption::UriHost, b"localhost".to_vec());
    request.add_option(CoapOption::UriPath, b"tv1".to_vec());
    assert_eq!(
        [
            0x44, 0x01, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0x39, 0x6C, 0x6F,
            0x63, 0x61, 0x6C, 0x68, 0x6F, 0x73, 0x74, 0x83, 0x74, 0x76, 0x31,
        ],
        request.to_bytes().unwrap()[..]
    );

    let response = Packet::from_bytes(&[
        0x64, 0x45, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0xFF, 0x48, 0x65,
        0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21,
    ])
    .unwrap();
    assert_eq!(23839, response.header.message_id);
    assert_eq!(
        MessageClass::Response(ResponseType::Content),
        response.header.code
    );
    assert_eq!(MessageType::Acknowledgement, response.header.get_type());
    assert_eq!([0, 0, 57, 116], response.get_token()[..]);
    assert_eq!(b"Hello World!", &response.payload[..]);
}
source

pub fn add_option_as<T: OptionValueType>(&mut self, tp: CoapOption, value: T)

Adds an option value using a structured option value format.

source

pub fn clear_option(&mut self, tp: CoapOption)

Removes an option.

source

pub fn set_content_format(&mut self, cf: ContentFormat)

Sets the content-format.

source

pub fn get_content_format(&self) -> Option<ContentFormat>

Returns the content-format.

source

pub fn set_observe_value(&mut self, value: u32)

Sets the value of the observe option.

source

pub fn get_observe_value( &self ) -> Option<Result<u32, IncompatibleOptionValueFormat>>

Returns the value of the observe option.

source

pub fn from_bytes(buf: &[u8]) -> Result<Packet, MessageError>

Decodes a byte slice and constructs the equivalent packet.

Examples found in repository?
examples/server.rs (line 11)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let socket = UdpSocket::bind("127.0.0.1:5683").unwrap();
    let mut buf = [0; 100];
    let (size, src) = socket.recv_from(&mut buf).expect("Didn't receive data");

    println!("Payload {:x?}", &buf[..size]);

    let packet = Packet::from_bytes(&buf[..size]).unwrap();
    let request = CoapRequest::from_packet(packet, src);

    let method = request.get_method().clone();
    let path = request.get_path();

    println!("Received CoAP request '{:?} {}' from {}", method, path, src);

    let mut response = request.response.unwrap();
    response.message.payload = b"OK".to_vec();

    let packet = response.message.to_bytes().unwrap();
    socket
        .send_to(&packet[..], &src)
        .expect("Could not send the data");
}
More examples
Hide additional examples
examples/bytes.rs (lines 20-23)
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
fn main() {
    let mut request = Packet::new();
    request.header.message_id = 23839;
    request.header.code = MessageClass::Request(RequestType::Get);
    request.set_token(vec![0, 0, 57, 116]);
    request.add_option(CoapOption::UriHost, b"localhost".to_vec());
    request.add_option(CoapOption::UriPath, b"tv1".to_vec());
    assert_eq!(
        [
            0x44, 0x01, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0x39, 0x6C, 0x6F,
            0x63, 0x61, 0x6C, 0x68, 0x6F, 0x73, 0x74, 0x83, 0x74, 0x76, 0x31,
        ],
        request.to_bytes().unwrap()[..]
    );

    let response = Packet::from_bytes(&[
        0x64, 0x45, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0xFF, 0x48, 0x65,
        0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21,
    ])
    .unwrap();
    assert_eq!(23839, response.header.message_id);
    assert_eq!(
        MessageClass::Response(ResponseType::Content),
        response.header.code
    );
    assert_eq!(MessageType::Acknowledgement, response.header.get_type());
    assert_eq!([0, 0, 57, 116], response.get_token()[..]);
    assert_eq!(b"Hello World!", &response.payload[..]);
}
source

pub fn to_bytes(&self) -> Result<Vec<u8>, MessageError>

Returns a vector of bytes representing the Packet.

Examples found in repository?
examples/client.rs (line 12)
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
    let mut request: CoapRequest<SocketAddr> = CoapRequest::new();

    request.set_method(Method::Get);
    request.set_path("/test");

    let socket = UdpSocket::bind("127.0.0.1:0").unwrap();

    let packet = request.message.to_bytes().unwrap();
    socket
        .send_to(&packet[..], "127.0.0.1:5683")
        .expect("Could not send the data");
}
More examples
Hide additional examples
examples/server.rs (line 22)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fn main() {
    let socket = UdpSocket::bind("127.0.0.1:5683").unwrap();
    let mut buf = [0; 100];
    let (size, src) = socket.recv_from(&mut buf).expect("Didn't receive data");

    println!("Payload {:x?}", &buf[..size]);

    let packet = Packet::from_bytes(&buf[..size]).unwrap();
    let request = CoapRequest::from_packet(packet, src);

    let method = request.get_method().clone();
    let path = request.get_path();

    println!("Received CoAP request '{:?} {}' from {}", method, path, src);

    let mut response = request.response.unwrap();
    response.message.payload = b"OK".to_vec();

    let packet = response.message.to_bytes().unwrap();
    socket
        .send_to(&packet[..], &src)
        .expect("Could not send the data");
}
examples/bytes.rs (line 17)
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
fn main() {
    let mut request = Packet::new();
    request.header.message_id = 23839;
    request.header.code = MessageClass::Request(RequestType::Get);
    request.set_token(vec![0, 0, 57, 116]);
    request.add_option(CoapOption::UriHost, b"localhost".to_vec());
    request.add_option(CoapOption::UriPath, b"tv1".to_vec());
    assert_eq!(
        [
            0x44, 0x01, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0x39, 0x6C, 0x6F,
            0x63, 0x61, 0x6C, 0x68, 0x6F, 0x73, 0x74, 0x83, 0x74, 0x76, 0x31,
        ],
        request.to_bytes().unwrap()[..]
    );

    let response = Packet::from_bytes(&[
        0x64, 0x45, 0x5D, 0x1F, 0x00, 0x00, 0x39, 0x74, 0xFF, 0x48, 0x65,
        0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21,
    ])
    .unwrap();
    assert_eq!(23839, response.header.message_id);
    assert_eq!(
        MessageClass::Response(ResponseType::Content),
        response.header.code
    );
    assert_eq!(MessageType::Acknowledgement, response.header.get_type());
    assert_eq!([0, 0, 57, 116], response.get_token()[..]);
    assert_eq!(b"Hello World!", &response.payload[..]);
}

Trait Implementations§

source§

impl Clone for Packet

source§

fn clone(&self) -> Packet

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Packet

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Packet

source§

fn default() -> Packet

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.