1 2 3 4 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
use toad_common::{AppendCopy, Array, Cursor, GetSize};
use toad_macros::rfc_7252_doc;
/// Message Code
pub mod code;
/// Message parsing errors
pub mod parse_error;
/// Message ID
pub mod id;
/// Message Options
pub mod opt;
/// Message Type
pub mod ty;
/// Message Token
pub mod token;
/// Message Version
pub mod ver;
pub use code::*;
pub use id::*;
pub use opt::*;
pub use parse_error::*;
pub use token::*;
pub use ty::*;
pub use ver::*;
use crate::from_bytes::TryConsumeBytes;
use crate::TryFromBytes;
#[doc = rfc_7252_doc!("5.5")]
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct Payload<C>(pub C);
/// Struct representing the first byte of a message.
///
/// ```text
/// CoAP version
/// |
/// | Message type (request, response, empty)
/// | |
/// | | Length of token, in bytes. (4-bit integer)
/// | | |
/// vv vv vvvv
/// 01 00 0000
/// ```
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub(crate) struct Byte1 {
pub(crate) ver: Version,
pub(crate) ty: Type,
pub(crate) tkl: u8,
}
impl TryFrom<u8> for Byte1 {
type Error = MessageParseError;
fn try_from(b: u8) -> Result<Self, Self::Error> {
let ver = b >> 6; // bits 0 & 1
let ty = b >> 4 & 0b11; // bits 2 & 3
let tkl = b & 0b1111u8; // last 4 bits
Ok(Byte1 { ver: Version(ver),
ty: Type::try_from(ty)?,
tkl })
}
}
impl<PayloadBytes: Array<Item = u8>,
OptionValue: Array<Item = u8> + AppendCopy<u8>,
Options: Array<Item = Opt<OptionValue>>> GetSize for Message<PayloadBytes, Options>
{
fn get_size(&self) -> usize {
let header_size = 4;
let payload_marker_size = 1;
let payload_size = self.payload.0.get_size();
let token_size = self.token.0.len();
let opts_size: usize = self.opts.iter().map(|o| o.get_size()).sum();
header_size + payload_marker_size + payload_size + token_size + opts_size
}
fn max_size(&self) -> Option<usize> {
None
}
fn is_full(&self) -> bool {
false
}
}
/// # `Message` struct
/// Low-level representation of a message that has been parsed from the raw binary format.
///
/// Note that `Message` is generic over 3 [`Array`]s:
/// - `PayloadC`: the byte buffer used to store the message's [`Payload`]
/// - `OptC`: byte buffer used to store [`Opt`]ion values ([`OptValue`])
/// - `Opts`: collection of [`Opt`]ions in the message
///
/// Messages support both serializing to bytes and from bytes, by using the provided [`TryFromBytes`] and [`TryIntoBytes`] traits.
///
/// <details>
/// <summary><b>RFC7252 - CoAP Messaging Model</b></summary>
#[doc = concat!("\n#", rfc_7252_doc!("2.1"))]
/// </details>
/// <details>
/// <summary><b>RFC7252 - CoAP Message Binary Format</b></summary>
#[doc = concat!("\n#", rfc_7252_doc!("3"))]
/// </details>
///
/// ```
/// use toad_msg::TryFromBytes;
/// use toad_msg::*;
/// # // version token len code (2.05 Content)
/// # // | | /
/// # // | type | / message ID
/// # // | | | | |
/// # // vv vv vvvv vvvvvvvv vvvvvvvvvvvvvvvv
/// # let header: [u8; 4] = 0b_01_00_0001_01000101_0000000000000001u32.to_be_bytes();
/// # let token: [u8; 1] = [254u8];
/// # let content_format: &[u8] = b"application/json";
/// # let options: [&[u8]; 2] = [&[0b_1100_1101u8, 0b00000011u8], content_format];
/// # let payload: [&[u8]; 2] = [&[0b_11111111u8], b"hello, world!"];
/// let packet: Vec<u8> = /* bytes! */
/// # [header.as_ref(), token.as_ref(), options.concat().as_ref(), payload.concat().as_ref()].concat();
///
/// // `VecMessage` uses `Vec` as the backing structure for byte buffers
/// let msg = VecMessage::try_from_bytes(packet.clone()).unwrap();
/// # let opt = Opt {
/// # delta: OptDelta(12),
/// # value: OptValue(content_format.iter().map(|u| *u).collect()),
/// # };
/// let mut opts_expected = /* create expected options */
/// # Vec::new();
/// # opts_expected.push(opt);
///
/// let expected = VecMessage {
/// id: Id(1),
/// ty: Type::Con,
/// ver: Version(1),
/// token: Token(tinyvec::array_vec!([u8; 8] => 254)),
/// opts: opts_expected,
/// code: Code {class: 2, detail: 5},
/// payload: Payload(b"hello, world!".to_vec()),
/// };
///
/// assert_eq!(msg, expected);
/// ```
#[derive(Clone, PartialEq, PartialOrd, Debug)]
pub struct Message<PayloadBytes, Options> {
/// see [`Id`] for details
pub id: Id,
/// see [`Type`] for details
pub ty: Type,
/// see [`Version`] for details
pub ver: Version,
/// see [`Token`] for details
pub token: Token,
/// see [`Code`] for details
pub code: Code,
/// see [`opt::Opt`] for details
pub opts: Options,
/// see [`Payload`]
pub payload: Payload<PayloadBytes>,
}
impl<PayloadBytes: Array<Item = u8> + AppendCopy<u8>,
OptionValue: Array<Item = u8> + AppendCopy<u8> + 'static,
Options: Array<Item = Opt<OptionValue>>> Message<PayloadBytes, Options>
{
/// Create a new message that ACKs this one.
///
/// This needs an [`Id`] to assign to the newly created message.
///
/// ```
/// // we are a server
///
/// use std::net::SocketAddr;
///
/// use toad_msg::{Id, VecMessage as Message};
///
/// fn server_get_request() -> Option<(SocketAddr, Message)> {
/// // Servery sockety things...
/// # use std::net::{Ipv4Addr, ToSocketAddrs};
/// # use toad_msg::{Type, Code, Token, Version, Payload};
/// # let addr = (Ipv4Addr::new(0, 0, 0, 0), 1234);
/// # let addr = addr.to_socket_addrs().unwrap().next().unwrap();
/// # let msg = Message { code: Code::new(0, 0),
/// # id: Id(1),
/// # ty: Type::Con,
/// # ver: Version(1),
/// # token: Token(tinyvec::array_vec!([u8; 8] => 254)),
/// # opts: vec![],
/// # payload: Payload(vec![]) };
/// # Some((addr, msg))
/// }
///
/// fn server_send_msg(addr: SocketAddr, msg: Message) -> Result<(), ()> {
/// // Message sendy bits...
/// # Ok(())
/// }
///
/// let (addr, req) = server_get_request().unwrap();
/// let ack_id = Id(req.id.0 + 1);
/// let ack = req.ack(ack_id);
///
/// server_send_msg(addr, ack).unwrap();
/// ```
pub fn ack(&self, id: Id) -> Self {
Self { id,
token: self.token,
ver: Default::default(),
ty: Type::Ack,
code: Code::new(0, 0),
payload: Payload(Default::default()),
opts: Default::default() }
}
}
impl<Bytes: AsRef<[u8]>,
PayloadBytes: Array<Item = u8> + AppendCopy<u8>,
OptionValue: Array<Item = u8> + AppendCopy<u8>,
Options: Array<Item = Opt<OptionValue>>> TryFromBytes<Bytes>
for Message<PayloadBytes, Options>
{
type Error = MessageParseError;
fn try_from_bytes(bytes: Bytes) -> Result<Self, Self::Error> {
let mut bytes = Cursor::new(bytes);
let Byte1 { tkl, ty, ver } = bytes.next()
.ok_or_else(|| MessageParseError::eof())?
.try_into()?;
if tkl > 8 {
return Err(Self::Error::InvalidTokenLength(tkl));
}
let code: Code = bytes.next().ok_or_else(|| MessageParseError::eof())?.into();
let id: Id = Id::try_consume_bytes(&mut bytes)?;
let token = bytes.take_exact(tkl as usize)
.ok_or_else(|| MessageParseError::eof())?;
let token = tinyvec::ArrayVec::<[u8; 8]>::try_from(token).expect("tkl was checked to be <= 8");
let token = Token(token);
let opts = Options::try_consume_bytes(&mut bytes).map_err(Self::Error::OptParseError)?;
let mut payload = PayloadBytes::reserve(bytes.remaining());
payload.append_copy(bytes.take_until_end());
let payload = Payload(payload);
Ok(Message { id,
ty,
ver,
code,
token,
opts,
payload })
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::VecMessage;
#[test]
fn parse_msg() {
let (expect, msg) = crate::test_msg();
assert_eq!(VecMessage::try_from_bytes(&msg).unwrap(), expect)
}
#[test]
fn parse_byte1() {
let byte = 0b_01_10_0011u8;
let byte = Byte1::try_from(byte).unwrap();
assert_eq!(byte,
Byte1 { ver: Version(1),
ty: Type::Ack,
tkl: 3 })
}
#[test]
fn parse_id() {
let mut id_bytes = Cursor::new(34u16.to_be_bytes());
let id = Id::try_consume_bytes(&mut id_bytes).unwrap();
assert_eq!(id, Id(34));
}
}