Skip to content
Snippets Groups Projects
Commit 544924aa authored by Romain Porte's avatar Romain Porte
Browse files

Added ContentFormat support

parent 0af4e219
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ num = "0.1" ...@@ -18,6 +18,7 @@ num = "0.1"
rand = "0.3" rand = "0.3"
log = "0.3" log = "0.3"
threadpool = "1.3" threadpool = "1.3"
enum_primitive = "0.1.1"
[dev-dependencies] [dev-dependencies]
quickcheck = "0.2.27" quickcheck = "0.2.27"
...@@ -74,6 +74,8 @@ extern crate url; ...@@ -74,6 +74,8 @@ extern crate url;
extern crate num; extern crate num;
extern crate rand; extern crate rand;
extern crate threadpool; extern crate threadpool;
#[macro_use] extern crate enum_primitive;
#[cfg(test)] #[cfg(test)]
extern crate quickcheck; extern crate quickcheck;
......
...@@ -2,6 +2,8 @@ use bincode; ...@@ -2,6 +2,8 @@ use bincode;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::collections::LinkedList; use std::collections::LinkedList;
use num::FromPrimitive;
use message::header; use message::header;
macro_rules! u8_to_unsigned_be { macro_rules! u8_to_unsigned_be {
...@@ -32,6 +34,18 @@ pub enum CoAPOption { ...@@ -32,6 +34,18 @@ pub enum CoAPOption {
Size1, Size1,
} }
enum_from_primitive! {
#[derive(PartialEq, Eq, Debug)]
pub enum ContentFormat {
TextPlain = 0,
ApplicationLinkFormat = 40,
ApplicationXML = 41,
ApplicationOctetStream = 42,
ApplicationEXI = 47,
ApplicationJSON = 50,
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum PackageError { pub enum PackageError {
InvalidHeader, InvalidHeader,
...@@ -78,6 +92,15 @@ impl Packet { ...@@ -78,6 +92,15 @@ impl Packet {
self.options.insert(num, value); self.options.insert(num, value);
} }
pub fn set_content_format(&mut self, cf: ContentFormat) {
let content_format = cf as u16;
let msb = (content_format >> 8) as u8;
let lsb = (content_format & 0xFF) as u8;
let content_format: Vec<u8> = vec![msb, lsb];
self.add_option(CoAPOption::ContentFormat, content_format);
}
pub fn set_payload(&mut self, payload: Vec<u8>) { pub fn set_payload(&mut self, payload: Vec<u8>) {
self.payload = payload; self.payload = payload;
} }
...@@ -105,6 +128,20 @@ impl Packet { ...@@ -105,6 +128,20 @@ impl Packet {
} }
} }
pub fn get_content_format(&self) -> Option<ContentFormat> {
if let Some(list) = self.get_option(CoAPOption::ContentFormat) {
if let Some(vector) = list.front() {
let msb = vector[0] as u16;
let lsb = vector[1] as u16;
let number = (msb << 8) + lsb;
return ContentFormat::from_u16(number);
}
}
None
}
/// Decodes a byte slice and construct the equivalent Packet. /// Decodes a byte slice and construct the equivalent Packet.
pub fn from_bytes(buf: &[u8]) -> Result<Packet, ParseError> { pub fn from_bytes(buf: &[u8]) -> Result<Packet, ParseError> {
let header_result: bincode::DecodingResult<header::HeaderRaw> = bincode::decode(buf); let header_result: bincode::DecodingResult<header::HeaderRaw> = bincode::decode(buf);
...@@ -442,6 +479,19 @@ mod test { ...@@ -442,6 +479,19 @@ mod test {
0x6C, 0x6F]); 0x6C, 0x6F]);
} }
#[test]
fn test_encode_decode_content_format() {
let mut packet = Packet::new();
packet.set_content_format(ContentFormat::ApplicationJSON);
assert_eq!(ContentFormat::ApplicationJSON, packet.get_content_format().unwrap())
}
#[test]
fn test_decode_empty_content_format() {
let packet = Packet::new();
assert!(packet.get_content_format().is_none());
}
#[test] #[test]
fn test_malicious_packet() { fn test_malicious_packet() {
use rand; use rand;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment