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
impl Packet
sourcepub const MAX_SIZE: usize = 1_280usize
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.
sourcepub fn new() -> Packet
pub fn new() -> Packet
Creates a new packet.
Examples found in repository?
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[..]);
}sourcepub fn options(&self) -> Iter<'_, u16, LinkedList<Vec<u8>>>
pub fn options(&self) -> Iter<'_, u16, LinkedList<Vec<u8>>>
Returns an iterator over the options of the packet.
sourcepub fn set_token(&mut self, token: Vec<u8>)
pub fn set_token(&mut self, token: Vec<u8>)
Sets the token.
Examples found in repository?
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[..]);
}sourcepub fn get_token(&self) -> &[u8] ⓘ
pub fn get_token(&self) -> &[u8] ⓘ
Returns the token.
Examples found in repository?
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[..]);
}sourcepub fn set_option(&mut self, tp: CoapOption, value: LinkedList<Vec<u8>>)
pub fn set_option(&mut self, tp: CoapOption, value: LinkedList<Vec<u8>>)
Sets an option’s values.
sourcepub fn set_options_as<T: OptionValueType>(
&mut self,
tp: CoapOption,
value: LinkedList<T>
)
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.
sourcepub fn get_option(&self, tp: CoapOption) -> Option<&LinkedList<Vec<u8>>>
pub fn get_option(&self, tp: CoapOption) -> Option<&LinkedList<Vec<u8>>>
Returns an option’s values.
sourcepub fn get_options_as<T: OptionValueType>(
&self,
tp: CoapOption
) -> Option<LinkedList<Result<T, IncompatibleOptionValueFormat>>>
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.
sourcepub fn get_first_option(&self, tp: CoapOption) -> Option<&Vec<u8>>
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.
sourcepub fn get_first_option_as<T: OptionValueType>(
&self,
tp: CoapOption
) -> Option<Result<T, IncompatibleOptionValueFormat>>
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.
sourcepub fn add_option(&mut self, tp: CoapOption, value: Vec<u8>)
pub fn add_option(&mut self, tp: CoapOption, value: Vec<u8>)
Adds an option value.
Examples found in repository?
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[..]);
}sourcepub fn add_option_as<T: OptionValueType>(&mut self, tp: CoapOption, value: T)
pub fn add_option_as<T: OptionValueType>(&mut self, tp: CoapOption, value: T)
Adds an option value using a structured option value format.
sourcepub fn clear_option(&mut self, tp: CoapOption)
pub fn clear_option(&mut self, tp: CoapOption)
Removes an option.
sourcepub fn set_content_format(&mut self, cf: ContentFormat)
pub fn set_content_format(&mut self, cf: ContentFormat)
Sets the content-format.
sourcepub fn get_content_format(&self) -> Option<ContentFormat>
pub fn get_content_format(&self) -> Option<ContentFormat>
Returns the content-format.
sourcepub fn set_observe_value(&mut self, value: u32)
pub fn set_observe_value(&mut self, value: u32)
Sets the value of the observe option.
sourcepub fn get_observe_value(
&self
) -> Option<Result<u32, IncompatibleOptionValueFormat>>
pub fn get_observe_value( &self ) -> Option<Result<u32, IncompatibleOptionValueFormat>>
Returns the value of the observe option.
sourcepub fn from_bytes(buf: &[u8]) -> Result<Packet, MessageError>
pub fn from_bytes(buf: &[u8]) -> Result<Packet, MessageError>
Decodes a byte slice and constructs the equivalent packet.
Examples found in repository?
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
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[..]);
}sourcepub fn to_bytes(&self) -> Result<Vec<u8>, MessageError>
pub fn to_bytes(&self) -> Result<Vec<u8>, MessageError>
Returns a vector of bytes representing the Packet.
Examples found in repository?
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
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");
}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[..]);
}