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
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
//! CoAP: Constrained Application Protocol
//!
//! # References
//!
//! - [RFC 7252: The Constrained Application Protocol (CoAP)][rfc]
//!
//! [rfc]: https://tools.ietf.org/html/rfc7252

use core::{fmt, marker::PhantomData, ops::Range, option::Option as CoreOption, str};

use as_slice::{AsMutSlice, AsSlice};
use byteorder::{ByteOrder, NetworkEndian as NE};
use cast::{u16, u8, usize};
use owning_slice::Truncate;

use crate::traits::{TryFrom, UncheckedIndex};

/// CoAP default UDP port
pub const PORT: u16 = 5683;

/* Message format */
const VER_T_TKL: usize = 0;
mod tkl {
    pub const MASK: u8 = (1 << SIZE) - 1;
    pub const OFFSET: u8 = 0;
    pub const SIZE: u8 = 4;
}

mod t {
    pub const MASK: u8 = (1 << SIZE) - 1;
    pub const OFFSET: u8 = super::tkl::OFFSET + super::tkl::SIZE;
    pub const SIZE: u8 = 2;
}

mod ver {
    pub const MASK: u8 = (1 << SIZE) - 1;
    pub const OFFSET: u8 = super::t::OFFSET + super::t::SIZE;
    pub const SIZE: u8 = 2;
}

const CODE: usize = 1;
const MESSAGE_ID: Range<usize> = 2..4;
const TOKEN_START: usize = MESSAGE_ID.end;

// Option header
mod length {
    pub const MASK: u8 = (1 << SIZE) - 1;
    pub const OFFSET: u8 = 0;
    pub const SIZE: u8 = 4;
}

mod delta {
    pub const MASK: u8 = (1 << SIZE) - 1;
    pub const OFFSET: u8 = super::length::OFFSET + super::length::SIZE;
    pub const SIZE: u8 = 4;
}

/// Size of a CoAP header
pub const HEADER_SIZE: u8 = MESSAGE_ID.end as u8;

/* Option parsing */
// This marks the end of the options
const PAYLOAD_MARKER: u8 = 0xff;

// The option delta and option length nibbles can't never be this value
const RESERVED: u8 = 0b1111;

// Offset to add to the option delta / length when they are larger than a nibble
const OFFSET8: u16 = 13;
const OFFSET16: u16 = 269;

// Option delta is an 8-bit unsigned integer
const DELTA8: u8 = 13;

// Option delta is a 16-bit unsigned integer
const DELTA16: u8 = 14;

// Option length is an 8-bit unsigned integer
const LENGTH8: u8 = 13;

// Option length is a 16-bit unsigned integer
const LENGTH16: u8 = 14;

/* Transmission parameters */
// const ACK_TIMEOUT: u16 = 2_000; // ms
// const ACK_RANDOM_FACTOR: f32 = 1.5;
// const MAX_RETRANSMIT: u8 = 4;
// const NSTART: u8 = 1;
// const DEFAULT_LEISURE: u16 = 5_000; // ms
// const PROBING_RATE: u8 = 1; // byte / second

/// CoAP (version 1) message
// NOTE Invariants
// - Options are always valid. For example, this means that the reserved bit pattern (0b1111)
//   doesn't appear in the Option Length nibble. It also means that options are not truncated so
//   they'll always be terminated by the `PAYLOAD_MARKER`, if there's a payload, or by the end of
//   the slice, if there's no payload.
pub struct Message<BUFFER, PAYLOAD = Set>
where
    BUFFER: AsSlice<Element = u8>,
    PAYLOAD: 'static,
{
    _payload: PhantomData<PAYLOAD>,
    buffer: BUFFER,
    // Position of the `PAYLOAD_MARKER`. Cached to avoid traversing the options (O(N) runtime) when
    // the payload is requested. `NO_PAYLOAD` indicates that there is no payload
    // When PAYLOAD is `Unset` this is used to track where the marker *would be*, but that spot in
    // the buffer is not yet set to `PAYLOAD_MARKER`
    marker: u16,
    /// Highest option number stored in the Options field
    number: u16,
}

// Special value
const NO_PAYLOAD: u16 = 0;

/// Payload has been set
pub enum Set {}

/// Payload has not been set yet
pub enum Unset {}

impl<B, P> Message<B, P>
where
    B: AsSlice<Element = u8>,
{
    /* Getters */
    /// Returns the Version field of the header
    ///
    /// As per RFC 7252 this always returns 1
    pub fn get_version(&self) -> u8 {
        debug_assert_eq!(get!(self.header_()[VER_T_TKL], ver), 1);

        1
    }

    /// Returns the Type field of the header
    pub fn get_type(&self) -> Type {
        Type::from(get!(self.header_()[VER_T_TKL], t))
    }

    /// Returns the Token Length (TKL) field of the header
    ///
    /// As per RFC 7252 this always returns a value in the range `0..=8`
    pub fn get_token_length(&self) -> u8 {
        get!(self.header_()[VER_T_TKL], tkl)
    }

    /// Returns the Code field of the header
    pub fn get_code(&self) -> Code {
        Code(self.header_()[CODE])
    }

    /// Returns the Message ID field of the header
    pub fn get_message_id(&self) -> u16 {
        NE::read_u16(&self.header_()[MESSAGE_ID])
    }

    /// View into the Token field of the header
    pub fn token(&self) -> &[u8] {
        let start = TOKEN_START;
        let end = start + self.get_token_length() as usize;
        unsafe { self.as_slice().r(start..end) }
    }

    /// Returns the byte representation of this message
    pub fn as_bytes(&self) -> &[u8] {
        if typeid!(P == Unset) {
            unsafe { self.buffer.as_slice().rt(..usize(self.marker)) }
        } else {
            self.buffer.as_slice()
        }
    }

    /// Returns the length (header + data) of the CoAP message
    pub fn len(&self) -> u16 {
        u16(self.as_bytes().len()).unwrap()
    }

    /// Returns an iterator over the options of this message
    pub fn options(&self) -> Options<'_> {
        let end = if self.marker != NO_PAYLOAD {
            usize(self.marker)
        } else {
            self.as_slice().len()
        };

        Options {
            number: 0,
            ptr: unsafe { self.as_slice().r(usize(self.options_start())..end) },
        }
    }

    /* Private */
    fn as_slice(&self) -> &[u8] {
        self.buffer.as_slice()
    }

    fn header_(&self) -> &[u8; HEADER_SIZE as usize] {
        debug_assert!(self.as_slice().len() >= HEADER_SIZE as usize);

        unsafe { &*(self.as_slice().as_ptr() as *const _) }
    }

    /// Returns the index at which the options start
    fn options_start(&self) -> u8 {
        HEADER_SIZE + self.get_token_length()
    }

    unsafe fn unchecked(buffer: B) -> Self {
        Message {
            _payload: PhantomData,
            buffer,
            marker: NO_PAYLOAD,
            number: 0,
        }
    }
}

impl<B, P> Message<B, P>
where
    B: AsMutSlice<Element = u8>,
{
    /* Setters */
    /// Sets the Code field of the header
    pub fn set_code<C>(&mut self, code: C)
    where
        C: Into<Code>,
    {
        self.header_mut_()[CODE] = code.into().0;
    }

    /// Sets the Message ID field of the header
    pub fn set_message_id(&mut self, id: u16) {
        NE::write_u16(&mut self.header_mut_()[MESSAGE_ID], id)
    }

    /// Sets the Type field of the header
    pub fn set_type(&mut self, ty: Type) {
        let ty: u8 = ty.into();
        set!(self.header_mut_()[VER_T_TKL], t, ty);
    }

    /// Mutable view into the Token field
    pub fn token_mut(&mut self) -> &mut [u8] {
        let start = TOKEN_START;
        let end = start + self.get_token_length() as usize;
        unsafe { self.as_mut_slice().rm(start..end) }
    }

    /* Private */
    fn as_mut_slice(&mut self) -> &mut [u8] {
        self.buffer.as_mut_slice()
    }

    fn header_mut_(&mut self) -> &mut [u8; HEADER_SIZE as usize] {
        debug_assert!(self.as_slice().len() >= HEADER_SIZE as usize);

        unsafe { &mut *(self.as_mut_slice().as_mut_ptr() as *mut _) }
    }

    unsafe fn set_token_length(&mut self, tkl: u8) {
        debug_assert!(tkl <= 8);

        set!(self.as_mut_slice()[VER_T_TKL], tkl, tkl);
    }

    unsafe fn set_version(&mut self, ver: u8) {
        set!(self.as_mut_slice()[VER_T_TKL], ver, ver);
    }
}

impl<B> Message<B, Set>
where
    B: AsSlice<Element = u8>,
{
    /* Public constructors */
    /// Parses bytes into a CoAP message
    pub fn parse(bytes: B) -> Result<Self, B> {
        let len = bytes.as_slice().len();

        if len < usize(HEADER_SIZE) {
            // smaller than header
            return Err(bytes);
        }

        let m = unsafe { Message::<B, Set>::unchecked(bytes) };
        let tkl = m.get_token_length();
        let bytes = m.buffer;

        let opts_start = HEADER_SIZE + tkl;
        if len < usize(opts_start) {
            // smaller than header + token
            return Err(bytes);
        }

        // Scans the slice for options
        //
        // Returns the highest option number and the index of the PAYLOAD_MARKER
        fn scan(bytes: &[u8]) -> Result<(u16, CoreOption<u16>), ()> {
            let len = bytes.len();
            let mut cursor = 0;
            let mut number = 0;

            let marker = loop {
                let head = *match bytes.as_slice().get(usize(cursor)) {
                    Some(b) => b,
                    // end of packet -- no payload marker was found
                    None => break None,
                };

                if head == PAYLOAD_MARKER {
                    // end of options
                    break Some(cursor);
                }
                cursor += 1;

                let delta4 = get!(head, delta);
                let len4 = get!(head, length);

                if delta4 == DELTA8 {
                    let byte = *bytes.as_slice().get(usize(cursor)).ok_or(())?;
                    cursor += 1;

                    number += u16(byte) + OFFSET8;
                } else if delta4 == DELTA16 {
                    if len < usize(cursor) + 1 {
                        return Err(());
                    }

                    let halfword = unsafe {
                        NE::read_u16(
                            &*(bytes.as_slice().as_ptr().add(usize(cursor)) as *const [u8; 2]),
                        )
                    };
                    cursor += 2;

                    number += halfword + OFFSET16;
                } else if delta4 == RESERVED {
                    return Err(());
                } else {
                    number += u16(delta4);
                }

                if len4 == LENGTH8 {
                    let byte = *bytes.as_slice().get(usize(cursor)).ok_or(())?;
                    cursor += 1;

                    cursor += u16(byte) + OFFSET8;
                } else if len4 == LENGTH16 {
                    if len < usize(cursor) + 1 {
                        return Err(());
                    }

                    let halfword = unsafe {
                        NE::read_u16(
                            &*(bytes.as_slice().as_ptr().add(usize(cursor)) as *const [u8; 2]),
                        )
                    };
                    cursor += 2;

                    cursor += halfword + OFFSET16;
                } else if len4 == RESERVED {
                    return Err(());
                } else {
                    cursor += u16(len4);
                }
            };

            Ok((number, marker))
        }

        if let Ok((number, marker)) = unsafe { scan(bytes.as_slice().rf(usize(opts_start)..)) } {
            Ok(Message {
                _payload: PhantomData,
                buffer: bytes,
                number,
                marker: marker.map(|m| m + u16(opts_start)).unwrap_or(NO_PAYLOAD),
            })
        } else {
            Err(bytes)
        }
    }

    /// View into the payload
    pub fn payload(&self) -> &[u8] {
        if self.marker == NO_PAYLOAD {
            &[]
        } else {
            unsafe { &self.as_slice().rf(usize(self.marker + 1)..) }
        }
    }
}

impl<B> Message<B, Set>
where
    B: AsMutSlice<Element = u8>,
{
    /// Mutable view into the payload
    pub fn payload_mut(&mut self) -> &mut [u8] {
        if self.marker == NO_PAYLOAD {
            &mut []
        } else {
            let start = self.marker + 1;
            unsafe { self.as_mut_slice().rfm(usize(start)..) }
        }
    }
}

impl<B> Message<B, Unset>
where
    B: AsMutSlice<Element = u8>,
{
    /* Constructors */
    /// Transforms the given buffer into a CoAP message
    ///
    /// This constructor sets the following header fields
    ///
    /// - Version = 1
    /// - Token Length = token_length
    ///
    /// NOTE The CoAP message will span the whole buffer.
    ///
    /// # Panics
    ///
    /// This constructor panics if
    ///
    /// - `token_length` is NOT in the range `0..=8`.
    /// - The buffer is not large enough to contain the CoAP header
    pub fn new(buffer: B, token_length: u8) -> Self {
        assert!(token_length <= 8);
        let len = buffer.as_slice().len();
        let marker = u16(HEADER_SIZE + token_length);
        assert!(len >= usize(marker));

        unsafe {
            let mut m = Message::unchecked(buffer);
            m.set_version(1);
            m.set_token_length(token_length);
            m.marker = marker;
            m
        }
    }

    /// Adds an option to this message
    ///
    /// *HEADS UP* This method will cause the first bytes of the payload to be lost
    ///
    /// # Panics
    ///
    /// This method panics
    ///
    /// - if `number` is smaller than the highest option number already contained in the message
    /// - if there's no space in the message to add the option
    pub fn add_option(&mut self, number: OptionNumber, value: &[u8]) {
        /// Number of bytes required to encode `x`
        fn nbytes(x: u16) -> u16 {
            if x < OFFSET8 {
                0 // 0.5 actually; this fits in a nibble
            } else if x < OFFSET16 {
                1
            } else {
                2
            }
        }

        // we can only add options that have an equal or a higher option number
        let nr: u16 = number.into();
        let delta = nr.checked_sub(self.number).unwrap();

        let len = u16(value.len()).unwrap();
        let sz = 1 + nbytes(delta) + nbytes(len) + len;

        let start = usize(self.marker);
        let mut cursor = start + 1;

        // update the cached highest number
        self.number = nr;

        // move the payload marker
        self.marker += sz;
        let end = usize(self.marker);

        // fill in the delta
        if delta < OFFSET8 {
            set!(self.as_mut_slice()[start], delta, u8(delta).unwrap());
        } else if delta < OFFSET16 {
            set!(self.as_mut_slice()[start], delta, DELTA8);
            self.as_mut_slice()[cursor] = u8(delta - OFFSET8).unwrap();
            cursor += 1;
        } else {
            set!(self.as_mut_slice()[start], delta, DELTA16);
            NE::write_u16(
                &mut self.as_mut_slice()[cursor..cursor + 2],
                delta - OFFSET16,
            );
            cursor += 2;
        }

        // fill in the length
        if len < OFFSET8 {
            set!(self.as_mut_slice()[start], length, u8(len).unwrap());
        } else if len < OFFSET16 {
            set!(self.as_mut_slice()[start], length, LENGTH8);
            self.as_mut_slice()[cursor] = u8(len - OFFSET8).unwrap();
            cursor += 1;
        } else {
            set!(self.as_mut_slice()[start], length, LENGTH16);
            NE::write_u16(&mut self.as_mut_slice()[cursor..cursor + 2], len - OFFSET16);
            cursor += 2;
        }

        // fill in the value
        self.as_mut_slice()[cursor..end].copy_from_slice(value);
    }

    /// Removes all the options this message has
    pub fn clear_options(&mut self) {
        self.number = 0;
        self.marker = u16(self.options_start());
    }
}

impl<B> Message<B, Unset>
where
    B: AsMutSlice<Element = u8> + Truncate<u16>,
{
    /// Fills the payload with the given data and adjusts the length of the CoAP message
    // TODO return a `Result`
    pub fn set_payload(mut self, data: &[u8]) -> Message<B> {
        if !data.is_empty() {
            let mut start = self.marker;

            // add `PAYLOAD_MARKER`
            self.buffer.as_mut_slice()[usize(start)] = PAYLOAD_MARKER;
            start += 1;

            // now add the payload
            let end = start + u16(data.len()).unwrap();
            self.buffer.as_mut_slice()[usize(start)..usize(end)].copy_from_slice(data);

            // finally, resize the buffer
            self.buffer.truncate(end);

            Message {
                _payload: PhantomData,
                buffer: self.buffer,
                marker: self.marker,
                number: self.number,
            }
        } else {
            self.no_payload()
        }
    }

    /// Finishing constructing this message by leaving the payload empty and truncating the message
    pub fn no_payload(mut self) -> Message<B> {
        let len = self.marker;
        self.marker = NO_PAYLOAD;

        self.buffer.truncate(len);

        Message {
            _payload: PhantomData,
            buffer: self.buffer,
            marker: self.marker,
            number: self.number,
        }
    }
}

impl<B, P> fmt::Debug for Message<B, P>
where
    B: AsSlice<Element = u8>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Adapter to format the `Options` iterator as a map
        struct Options<'a, B, P>(&'a Message<B, P>)
        where
            B: AsSlice<Element = u8>,
            P: 'static;
        impl<'a, B, P> fmt::Debug for Options<'a, B, P>
        where
            B: AsSlice<Element = u8>,
        {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let mut m = f.debug_map();
                for opt in self.0.options() {
                    if let Ok(s) = str::from_utf8(opt.value()) {
                        m.entry(&opt.number(), &s);
                    } else {
                        m.entry(&opt.number(), &opt.value());
                    }
                }
                m.finish()
            }
        }

        struct Prefix<'a, T>(&'a str, T)
        where
            T: fmt::Debug;

        impl<'a, T> fmt::Debug for Prefix<'a, T>
        where
            T: fmt::Debug,
        {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "{}{:?}", self.0, self.1)
            }
        }

        let mut s = f.debug_struct("coap::Message");
        s.field("version", &self.get_version())
            .field("type", &self.get_type());

        let code = self.get_code();
        if let Ok(method) = Method::try_from(code) {
            s.field("code", &Prefix("Method::", method));
        } else if let Ok(resp) = Response::try_from(code) {
            s.field("code", &Prefix("Response::", resp));
        } else {
            s.field("code", &code);
        }

        s.field("message_id", &self.get_message_id());

        if self.token().len() != 0 {
            s.field("token", &self.token());
        }

        if self.options().count() != 0 {
            s.field("options", &Options(self));
        }

        // if typeid!(P == Set) {
        //     if self.marker != NO_PAYLOAD {
        //         let payload = unsafe { &self.as_slice().rf(usize(self.marker + 1)..) };

        //         if let Ok(p) = str::from_utf8(payload) {
        //             s.field("payload", &p);
        //         } else {
        //             s.field("payload", &payload);
        //         }
        //     }
        // }

        s.finish()
    }
}

/// A CoAP option
pub struct Option<'a> {
    number: u16,
    value: &'a [u8],
}

impl<'a> Option<'a> {
    /// Returns the number of this option
    pub fn number(&self) -> OptionNumber {
        self.number.into()
    }

    /// Returns the value of this option
    pub fn value(&self) -> &'a [u8] {
        self.value
    }
}

/// Iterator over the options of a CoAP message
pub struct Options<'a> {
    /// Number of the previous option
    number: u16,
    ptr: &'a [u8],
}

// Helper
struct PtrReader<'a>(&'a [u8]);

impl<'a> PtrReader<'a> {
    fn try_read_u8(&mut self) -> ::core::option::Option<u8> {
        if self.0.len() > 0 {
            Some(unsafe { self.read_u8() })
        } else {
            None
        }
    }

    unsafe fn read_u8(&mut self) -> u8 {
        let byte = *self.0.gu(0);
        self.0 = self.0.rf(1..);
        byte
    }

    unsafe fn read_u16(&mut self) -> u16 {
        let halfword = NE::read_u16(self.0.rt(..2));
        self.0 = self.0.rf(2..);
        halfword
    }
}

impl<'a> Iterator for Options<'a> {
    type Item = Option<'a>;

    fn next(&mut self) -> CoreOption<Option<'a>> {
        let mut ptr = PtrReader(self.ptr);

        let head = ptr.try_read_u8()?;
        if head == PAYLOAD_MARKER {
            None
        } else {
            let delta4 = get!(head, delta);
            let len4 = get!(head, length);

            // Sanity check `Message` invariants
            debug_assert!(delta4 != RESERVED);
            debug_assert!(len4 != RESERVED);

            self.number += if delta4 == DELTA8 {
                u16(unsafe { ptr.read_u8() }) + OFFSET8
            } else if delta4 == DELTA16 {
                unsafe { ptr.read_u16() + OFFSET16 }
            } else {
                u16(delta4)
            };

            let len = if len4 == LENGTH8 {
                u16(unsafe { ptr.read_u8() }) + OFFSET8
            } else if len4 == LENGTH16 {
                unsafe { ptr.read_u16() + OFFSET16 }
            } else {
                u16(len4)
            };

            // move pointer by `len` for the next iteration
            let value = unsafe { ptr.0.rt(..usize(len)) };
            self.ptr = unsafe { ptr.0.rf(usize(len)..) };

            Some(Option {
                number: self.number,
                value: value,
            })
        }
    }
}

/// CoAP Type
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Type {
    /// Confirmable message
    Confirmable,
    /// Non-confirmable message
    NonConfirmable,
    /// Acknowledgement message
    Acknowledgement,
    /// Reset message
    Reset,
}

impl Type {
    fn from(nibble: u8) -> Self {
        match nibble & 0b11 {
            0b00 => Type::Confirmable,
            0b01 => Type::NonConfirmable,
            0b10 => Type::Acknowledgement,
            0b11 => Type::Reset,
            _ => unreachable!(),
        }
    }
}

impl Into<u8> for Type {
    fn into(self) -> u8 {
        match self {
            Type::Confirmable => 0,
            Type::NonConfirmable => 1,
            Type::Acknowledgement => 2,
            Type::Reset => 3,
        }
    }
}

/// CoAP Code
#[derive(Clone, Copy, PartialEq)]
pub struct Code(u8);

mod detail {
    pub const MASK: u8 = (1 << SIZE) - 1;
    pub const OFFSET: u8 = 0;
    pub const SIZE: u8 = 5;
}

mod class {
    pub const MASK: u8 = (1 << SIZE) - 1;
    pub const OFFSET: u8 = 5;
    pub const SIZE: u8 = 3;
}

impl Code {
    /// Empty message
    pub const EMPTY: Self = Code(0b000_00000);

    /// Returns the class of this code
    pub fn class(&self) -> u8 {
        get!(self.0, class)
    }

    /// Returns the detail of this code
    pub fn detail(&self) -> u8 {
        get!(self.0, detail)
    }

    /// Checks if this is a request code
    pub fn is_request(&self) -> bool {
        self.class() == 0 && self.detail() != 0
    }

    /// Checks if this is a reponse code
    pub fn is_response(&self) -> bool {
        match self.class() {
            2..=5 => true,
            _ => false,
        }
    }

    /* Private */
    fn from_parts(class: u8, detail: u8) -> Self {
        let mut code = 0;
        set!(code, class, class);
        set!(code, detail, detail);

        Code(code)
    }
}

impl fmt::Debug for Code {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Code(0b{:03b}_{:05b})", self.class(), self.detail())
    }
}

impl fmt::Display for Code {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{:02}", self.class(), self.detail())
    }
}

code!(
    /// CoAP Method Codes
    #[derive(Clone, Copy, Debug, PartialEq)]
    pub enum Method {
        /// GET
        Get = (0, 1),
        /// POST
        Post = (0, 2),
        /// PUT
        Put = (0, 3),
        /// DELETE
        Delete = (0, 4),
    }
);

code!(
    /// CoAP Response Codes
    #[derive(Clone, Copy, Debug, PartialEq)]
    pub enum Response {
        // Success
        /// Created
        Created = (2, 1),
        /// Deleted
        Deleted = (2, 2),
        /// Valid
        Valid = (2, 3),
        /// Changed
        Changed = (2, 4),
        /// Content
        Content = (2, 5),

        // Client error
        /// Bad Request
        BadRequest = (4, 0),
        /// Unauthorized
        Unauthorized = (4, 1),
        /// Bad Option
        BadOption = (4, 2),
        /// Forbidden
        Forbidden = (4, 3),
        /// Not Found
        NotFound = (4, 4),
        /// Method Not Allowed
        MethodNotAllowed = (4, 5),
        /// Not Acceptable
        NotAcceptable = (4, 6),
        /// Precondition Failed
        PreconditionFailed = (4, 12),
        /// Request Entity Too Large
        RequestEntityTooLarge = (4, 13),
        /// Unsupported Content-Format
        UnsupportedContentFormat = (4, 15),

        // Server error
        /// Internal Server Error
        InternalServerError = (5, 0),
        /// Not Implemented
        NotImplemented = (5, 1),
        /// Bad Gateway
        BadGateway = (5, 2),
        /// Service Unavailable
        ServiceUnavailable = (5, 3),
        /// Gateway Timeout
        GatewayTimeout = (5, 4),
        /// Proxying Not Supported
        ProxyingNotSupported = (5, 5),
    }
);

full_range!(
    u16,
    /// CoAP Option Numbers
    #[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
    pub enum OptionNumber {
        /// Reserved
        Reserved0 = 0,
        /// If-Match
        IfMatch = 1,
        /// Uri-Host
        UriHost = 3,
        /// ETag
        ETag = 4,
        /// If-None-Patch
        IfNoneMatch = 5,
        /// Uri-Port
        UriPort = 7,
        /// Location-Path
        LocationPath = 8,
        /// Uri-Path
        UriPath = 11,
        /// Content-Format
        ContentFormat = 12,
        /// Max-Age
        MaxAge = 14,
        /// Uri-Query
        UriQuery = 15,
        /// Accept
        Accept = 17,
        /// Location-Query
        LocationQuery = 20,
        /// Proxy-Uri
        ProxyUri = 35,
        /// Proxy-Scheme
        ProxyScheme = 39,
        /// Size1
        Size1 = 60,
        /// Reserved
        Reserved1 = 128,
        /// Reserved
        Reserved2 = 132,
        /// Reserved
        Reserved3 = 136,
        /// Reserved
        Reserved4 = 140,
    }
);

impl OptionNumber {
    /// Is this a critical option?
    pub fn is_critical(&self) -> bool {
        // odd option numbers are critical
        u16::from(*self) % 2 == 1
    }

    /// Is this an elective option?
    pub fn is_elective(&self) -> bool {
        // even option numbers are elective
        !self.is_critical()
    }

    /// Is this option UnSafe to forward?
    pub fn is_unsafe(&self) -> bool {
        u16::from(*self) & 2 == 1
    }
}

full_range!(
    u16,
    /// CoAP Content-Formats
    pub enum ContentFormat {
        /// text/plain; charset=utf-8
        TextPlain = 0,
        /// application/link-format
        ApplicationLinkFormat = 40,
        /// application/xml
        ApplicationXml = 41,
        /// application/octet-stream
        ApplicationOctetStream = 42,
        /// application/exi
        ApplicationExi = 47,
        /// application/json
        ApplicationJson = 50,
    }
);

#[cfg(test)]
mod tests {
    use cast::usize;
    use rand::{self, Rng, RngCore};

    use crate::coap;

    const URI_HOST: &[u8] = b"www.example.org";
    const URI_PORT: &[u8] = &[22, 51]; // 5683

    #[test]
    fn new() {
        const SZ: usize = super::TOKEN_START;

        let mut chunk = [0; SZ];
        let buf = &mut chunk[..];

        let coap = coap::Message::new(buf, 0);
        assert_eq!(usize(coap.len()), SZ);
        let m = coap.no_payload();
        assert_eq!(m.payload(), &[]);
        assert_eq!(usize(m.len()), SZ);
    }

    #[test]
    fn new_payload() {
        const BYTE: u8 = 42;
        const SZ: usize = super::TOKEN_START + 1 /* PAYLOAD_MARKER */ + 1 /* BYTE */;

        let mut chunk = [0; SZ];
        let buf = &mut chunk[..];

        let coap = coap::Message::new(buf, 0);
        let m = coap.set_payload(&[BYTE]);
        assert_eq!(m.payload(), &[BYTE]);
        assert_eq!(usize(m.len()), SZ);
    }

    #[test]
    fn options() {
        // NOTE start with randomized array to make sure we set *everything* correctly
        let mut buf = [0; 128];
        rand::thread_rng().fill_bytes(&mut buf);

        let mut coap = coap::Message::new(&mut buf[..], rand::thread_rng().gen::<u8>() % 9);

        coap.add_option(coap::OptionNumber::UriHost, URI_HOST);

        {
            let host = coap.options().next().unwrap();

            assert_eq!(host.number(), coap::OptionNumber::UriHost);
            assert_eq!(host.value(), URI_HOST);
        }

        coap.add_option(coap::OptionNumber::UriPort, URI_PORT);

        {
            let host = coap.options().nth(0).unwrap();
            let port = coap.options().nth(1).unwrap();

            assert_eq!(host.number(), coap::OptionNumber::UriHost);
            assert_eq!(host.value(), URI_HOST);

            assert_eq!(port.number(), coap::OptionNumber::UriPort);
            assert_eq!(port.value(), URI_PORT);
        }

        coap.clear_options();

        assert!(coap.options().next().is_none());
    }

    #[test]
    fn parse() {
        const TYPE: coap::Type = coap::Type::Confirmable;
        const CODE: coap::Code = coap::Code(0b000_00001);
        const MID: u16 = 0xabcd;

        let mut rng = rand::thread_rng();
        let tkl = rng.gen::<u8>() % 9;
        let mut buf = [0; 8];
        rng.fill_bytes(&mut buf[..usize(tkl)]);
        let token = &buf[..usize(tkl)];

        // NOTE start with randomized array to make sure we set *everything* correctly
        let mut buf = [0; 128];
        rng.fill_bytes(&mut buf);

        let mut coap = coap::Message::new(&mut buf[..], tkl);

        coap.set_type(TYPE);
        coap.set_code(CODE);
        coap.set_message_id(MID);
        coap.token_mut().copy_from_slice(token);

        // zero options
        {
            let m = coap::Message::parse(coap.as_bytes()).unwrap();

            assert_eq!(m.get_version(), 1);
            assert_eq!(m.get_type(), TYPE);
            assert_eq!(m.get_token_length(), tkl);
            assert_eq!(m.get_code(), CODE);
            assert_eq!(m.get_message_id(), MID);
            assert_eq!(m.token(), token);
        }

        coap.add_option(coap::OptionNumber::UriHost, URI_HOST);

        // one option
        {
            let m = coap::Message::parse(coap.as_bytes()).unwrap();

            assert_eq!(m.get_version(), 1);
            assert_eq!(m.get_type(), TYPE);
            assert_eq!(m.get_token_length(), tkl);
            assert_eq!(m.get_code(), CODE);
            assert_eq!(m.get_message_id(), MID);
            assert_eq!(m.token(), token);

            let host = m.options().next().unwrap();

            assert_eq!(host.number(), coap::OptionNumber::UriHost);
            assert_eq!(host.value(), URI_HOST);
        }

        coap.add_option(coap::OptionNumber::UriPort, URI_PORT);

        // two options
        {
            let m = coap::Message::parse(coap.as_bytes()).unwrap();

            assert_eq!(m.get_version(), 1);
            assert_eq!(m.get_type(), TYPE);
            assert_eq!(m.get_token_length(), tkl);
            assert_eq!(m.get_code(), CODE);
            assert_eq!(m.get_message_id(), MID);
            assert_eq!(m.token(), token);

            let host = coap.options().nth(0).unwrap();
            let port = coap.options().nth(1).unwrap();

            assert_eq!(host.number(), coap::OptionNumber::UriHost);
            assert_eq!(host.value(), URI_HOST);

            assert_eq!(port.number(), coap::OptionNumber::UriPort);
            assert_eq!(port.value(), URI_PORT);
        }
    }
}