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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use toad_common::{AppendCopy, Array, Cursor, GetSize};
use toad_macros::rfc_7252_doc;

use crate::content_format::ContentFormat;
#[allow(unused_imports)]
use crate::TryIntoBytes;

/// 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, Eq, PartialOrd, Ord)]
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, Eq, Ord)]
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>, Options: OptionMap> 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.opt_refs().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 std::collections::BTreeMap;
///
/// 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();
///
/// // `toad_msg::alloc::Message` uses `Vec` as the backing structure for byte buffers
/// let msg = toad_msg::alloc::Message::try_from_bytes(packet.clone()).unwrap();
/// let mut opts_expected = BTreeMap::from([(OptNumber(12), vec![OptValue(content_format.iter().map(|u| *u).collect())])]);
///
/// let expected = toad_msg::alloc::Message {
///   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, Eq, Ord, 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>,
}

/// An error occurred during a call to [`Message::set`]
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SetOptionError<OV, OVs> {
  RepeatedTooManyTimes(OV),
  TooManyOptions(OptNumber, OVs),
}

impl<PayloadBytes: Array<Item = u8> + AppendCopy<u8>, Options: OptionMap>
  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::alloc::Message;
  /// use toad_msg::Id;
  ///
  /// 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: Default::default(),
  ///   #                     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() }
  }

  /// Insert a new value for a given option
  ///
  /// Errors when there cannot be any more options, or the option
  /// cannot be repeated any more (only applies to non-std environments)
  #[doc = rfc_7252_doc!("5.4.5")]
  pub fn add(&mut self,
             n: OptNumber,
             v: OptValue<Options::OptValue>)
             -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>> {
    match (self.remove(n).unwrap_or_default(), &mut self.opts) {
      | (vals, _) if vals.is_full() => Err(SetOptionError::RepeatedTooManyTimes(v)),
      | (vals, opts) if opts.is_full() => Err(SetOptionError::TooManyOptions(n, vals)),
      | (mut vals, opts) => {
        vals.push(v);
        opts.insert(n, vals).ok();
        Ok(())
      },
    }
  }

  /// Replace any / all existing values with a new one,
  /// yielding the previous value(s)
  pub fn set(
    &mut self,
    n: OptNumber,
    v: OptValue<Options::OptValue>)
    -> Result<Option<Options::OptValues>,
              SetOptionError<OptValue<Options::OptValue>, Options::OptValues>> {
    Ok(self.remove(n)).and_then(|old| self.add(n, v).map(|_| old))
  }

  /// Get the number of values for a given option
  pub fn count(&self, n: OptNumber) -> usize {
    self.get(n).map(|a| a.get_size()).unwrap_or(0)
  }

  /// Get the value(s) of an option by number
  ///
  /// This just invokes [`toad_common::Map::get`] on [`Message.opts`].
  pub fn get(&self, n: OptNumber) -> Option<&Options::OptValues> {
    self.opts.get(&n)
  }

  /// Remove all values for the option from this message,
  /// returning them if there were any.
  pub fn remove(&mut self, n: OptNumber) -> Option<Options::OptValues> {
    self.opts.remove(&n)
  }

  /// Update the value for the [Uri-Host](opt::known::no_repeat::HOST) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.1")]
  pub fn set_host<S>(
    &mut self,
    host: S)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>>
    where S: AsRef<str>
  {
    self.set(opt::known::no_repeat::HOST,
             host.as_ref().as_bytes().iter().copied().collect())
        .map(|_| ())
  }

  /// Update the value for the [Uri-Port](opt::known::no_repeat::PORT) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.1")]
  pub fn set_port(
    &mut self,
    port: u16)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>> {
    self.set(opt::known::no_repeat::PORT,
             port.to_be_bytes().into_iter().collect())
        .map(|_| ())
  }

  /// Update the value for the [Uri-Path](opt::known::no_repeat::PATH) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.1")]
  pub fn set_path<S>(
    &mut self,
    path: S)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>>
    where S: AsRef<str>
  {
    self.set(opt::known::no_repeat::PATH,
             path.as_ref().as_bytes().iter().copied().collect())
        .map(|_| ())
  }

  /// Insert a new value for the [Uri-Query](opt::known::repeat::QUERY) option,
  /// alongside any existing values.
  #[doc = rfc_7252_doc!("5.10.1")]
  pub fn add_query<S>(
    &mut self,
    query: S)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>>
    where S: AsRef<str>
  {
    self.add(opt::known::repeat::QUERY,
             query.as_ref().as_bytes().iter().copied().collect())
  }

  /// Update the value for the [Content-Format](opt::known::no_repeat::CONTENT_FORMAT) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.3")]
  pub fn set_content_format(
    &mut self,
    format: ContentFormat)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>> {
    self.set(opt::known::no_repeat::CONTENT_FORMAT,
             format.into_iter().collect())
        .map(|_| ())
  }

  /// Update the value for the [Accept](opt::known::no_repeat::ACCEPT) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.4")]
  pub fn set_accept(
    &mut self,
    format: ContentFormat)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>> {
    self.set(opt::known::no_repeat::ACCEPT, format.into_iter().collect())
        .map(|_| ())
  }

  /// Update the value for the [Size1](opt::known::no_repeat::SIZE1) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.9")]
  pub fn set_size1(
    &mut self,
    size_bytes: u64)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>> {
    self.set(opt::known::no_repeat::SIZE1,
             size_bytes.to_be_bytes().into_iter().collect())
        .map(|_| ())
  }

  /// Discard all values for [If-Match](opt::known::repeat::IF_MATCH), and replace them with
  /// an empty value.
  ///
  /// This signals that our request should only be processed if we're trying to update
  /// a resource that exists (e.g. this ensures PUT only updates and will never insert)
  #[doc = rfc_7252_doc!("5.10.8.1")]
  pub fn set_if_exists(
    &mut self)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>> {
    self.set(opt::known::repeat::IF_MATCH, Default::default())
        .map(|_| ())
  }

  /// Enable the [If-None-Match](opt::known::no_repeat::IF_NONE_MATCH) flag
  ///
  /// This signals that our request should only be processed if we're trying to insert
  /// a resource that does not exist (e.g. this ensures PUT only inserts and will never update)
  #[doc = rfc_7252_doc!("5.10.8.2")]
  pub fn set_if_not_exists(
    &mut self)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>> {
    self.set(opt::known::no_repeat::IF_NONE_MATCH, Default::default())
        .map(|_| ())
  }

  /// Update the value for the [Max-Age](opt::known::no_repeat::MAX_AGE) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.5")]
  pub fn set_max_age(
    &mut self,
    max_age_seconds: u32)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>> {
    self.set(opt::known::no_repeat::MAX_AGE,
             max_age_seconds.to_be_bytes().into_iter().collect())
        .map(|_| ())
  }

  /// Update the value for the [Proxy-Uri](opt::known::no_repeat::PROXY_URI) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.2")]
  pub fn set_proxy_uri<S>(
    &mut self,
    uri: S)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>>
    where S: AsRef<str>
  {
    self.set(opt::known::no_repeat::PROXY_URI,
             uri.as_ref().as_bytes().iter().copied().collect())
        .map(|_| ())
  }

  /// Update the value for the [Proxy-Scheme](opt::known::no_repeat::PROXY_SCHEME) option,
  /// discarding any existing values.
  #[doc = rfc_7252_doc!("5.10.2")]
  pub fn set_proxy_scheme<S>(
    &mut self,
    scheme: S)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>>
    where S: AsRef<str>
  {
    self.set(opt::known::no_repeat::PROXY_URI,
             scheme.as_ref().as_bytes().iter().copied().collect())
        .map(|_| ())
  }

  /// Insert a new value for the [If-Match](opt::known::repeat::IF_MATCH) option,
  /// alongside any existing values.
  #[doc = rfc_7252_doc!("5.10.8.1")]
  pub fn add_if_match<B>(
    &mut self,
    tag: B)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>>
    where B: AsRef<[u8]>
  {
    if let Some(others) = self.remove(opt::known::repeat::IF_MATCH) {
      others.into_iter()
            .filter(|v| v.0.get_size() > 0)
            .map(|v| self.add(opt::known::repeat::IF_MATCH, v))
            .collect::<Result<(), _>>()?;
    }

    self.add(opt::known::repeat::IF_MATCH,
             tag.as_ref().iter().copied().collect())
  }

  /// Insert a new value for the [Location-Path](opt::known::repeat::LOCATION_PATH) option,
  /// alongside any existing values.
  #[doc = rfc_7252_doc!("5.10.7")]
  pub fn add_location_path<S>(
    &mut self,
    path: S)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>>
    where S: AsRef<str>
  {
    self.add(opt::known::repeat::LOCATION_PATH,
             path.as_ref().as_bytes().iter().copied().collect())
  }

  /// Insert a new value for the [Location-Query](opt::known::repeat::LOCATION_QUERY) option,
  /// alongside any existing values.
  #[doc = rfc_7252_doc!("5.10.7")]
  pub fn add_location_query<S>(
    &mut self,
    query: S)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>>
    where S: AsRef<str>
  {
    self.add(opt::known::repeat::LOCATION_QUERY,
             query.as_ref().as_bytes().iter().copied().collect())
  }

  /// Insert a new value for the [ETag](opt::known::repeat::ETAG) option,
  /// alongside any existing values.
  #[doc = rfc_7252_doc!("5.10.7")]
  pub fn add_etag<B>(
    &mut self,
    tag: B)
    -> Result<(), SetOptionError<OptValue<Options::OptValue>, Options::OptValues>>
    where B: AsRef<[u8]>
  {
    self.add(opt::known::repeat::ETAG,
             tag.as_ref().iter().copied().collect())
  }
}

impl<Bytes: AsRef<[u8]>, PayloadBytes: Array<Item = u8> + AppendCopy<u8>, Options: OptionMap>
  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::alloc;

  #[test]
  fn parse_msg() {
    let (expect, msg) = crate::test_msg();
    assert_eq!(alloc::Message::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));
  }
}