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
// Copyright (C) 2015-2021 Swift Navigation Inc.
// Contact: https://support.swiftnav.com
//
// This source is subject to the license found in the file 'LICENSE' which must
// be be distributed together with this source. All other rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

//****************************************************************************
// Automatically generated from yaml/swiftnav/sbp/flash.yaml
// with generate.py. Please do not hand edit!
//****************************************************************************/
//! Messages for reading/writing the device's onboard flash memory. Many of
//! these messages target specific flash memory peripherals used in Swift
//! Navigation devices: the STM32 flash and the M25Pxx FPGA configuration
//! flash from Piksi 2.3.1.  This module does not apply to Piksi Multi.
pub use msg_flash_done::MsgFlashDone;
pub use msg_flash_erase::MsgFlashErase;
pub use msg_flash_program::MsgFlashProgram;
pub use msg_flash_read_req::MsgFlashReadReq;
pub use msg_flash_read_resp::MsgFlashReadResp;
pub use msg_m25_flash_write_status::MsgM25FlashWriteStatus;
pub use msg_stm_flash_lock_sector::MsgStmFlashLockSector;
pub use msg_stm_flash_unlock_sector::MsgStmFlashUnlockSector;
pub use msg_stm_unique_id_req::MsgStmUniqueIdReq;
pub use msg_stm_unique_id_resp::MsgStmUniqueIdResp;

pub mod msg_flash_done {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Flash response message (host <= device)
    ///
    /// This message defines success or failure codes for a variety of flash
    /// memory requests from the host to the device. Flash read and write
    /// messages, such as MSG_FLASH_READ_REQ, or MSG_FLASH_PROGRAM, may return
    /// this message on failure.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgFlashDone {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// Response flags
        #[cfg_attr(feature = "serde", serde(rename(serialize = "response")))]
        pub response: u8,
    }

    impl MsgFlashDone {
        /// Gets the [ResponseCode][self::ResponseCode] stored in the `response` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `ResponseCode` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `ResponseCode` were added.
        pub fn response_code(&self) -> Result<ResponseCode, u8> {
            get_bit_range!(self.response, u8, u8, 2, 0).try_into()
        }

        /// Set the bitrange corresponding to the [ResponseCode][ResponseCode] of the `response` bitfield.
        pub fn set_response_code(&mut self, response_code: ResponseCode) {
            set_bit_range!(&mut self.response, response_code, u8, u8, 2, 0);
        }
    }

    impl ConcreteMessage for MsgFlashDone {
        const MESSAGE_TYPE: u16 = 224;
        const MESSAGE_NAME: &'static str = "MSG_FLASH_DONE";
    }

    impl SbpMessage for MsgFlashDone {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgFlashDone {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgFlashDone(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgFlashDone {
        const MIN_LEN: usize = <u8 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.response)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.response, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgFlashDone {
                sender_id: None,
                response: WireFormat::parse_unchecked(buf),
            }
        }
    }

    /// Response code
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum ResponseCode {
        /// FLASH_OK
        FlashOk = 0,

        /// FLASH_INVALID_FLASH
        FlashInvalidFlash = 1,

        /// FLASH_INVALID_LEN
        FlashInvalidLen = 2,

        /// FLASH_INVALID_ADDR
        FlashInvalidAddr = 3,

        /// FLASH_INVALID_RANGE
        FlashInvalidRange = 4,

        /// FLASH_INVALID_SECTOR
        FlashInvalidSector = 5,
    }

    impl std::fmt::Display for ResponseCode {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                ResponseCode::FlashOk => f.write_str("FLASH_OK"),
                ResponseCode::FlashInvalidFlash => f.write_str("FLASH_INVALID_FLASH"),
                ResponseCode::FlashInvalidLen => f.write_str("FLASH_INVALID_LEN"),
                ResponseCode::FlashInvalidAddr => f.write_str("FLASH_INVALID_ADDR"),
                ResponseCode::FlashInvalidRange => f.write_str("FLASH_INVALID_RANGE"),
                ResponseCode::FlashInvalidSector => f.write_str("FLASH_INVALID_SECTOR"),
            }
        }
    }

    impl TryFrom<u8> for ResponseCode {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, Self::Error> {
            match i {
                0 => Ok(ResponseCode::FlashOk),
                1 => Ok(ResponseCode::FlashInvalidFlash),
                2 => Ok(ResponseCode::FlashInvalidLen),
                3 => Ok(ResponseCode::FlashInvalidAddr),
                4 => Ok(ResponseCode::FlashInvalidRange),
                5 => Ok(ResponseCode::FlashInvalidSector),
                i => Err(i),
            }
        }
    }
}

pub mod msg_flash_erase {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Erase sector of device flash memory (host => device)
    ///
    /// The flash erase message from the host erases a sector of either the STM or
    /// M25 onboard flash memory. The device will reply with a MSG_FLASH_DONE
    /// message containing the return code - FLASH_OK (0) on success or
    /// FLASH_INVALID_FLASH (1) if the flash specified is invalid.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgFlashErase {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// Target flags
        #[cfg_attr(feature = "serde", serde(rename(serialize = "target")))]
        pub target: u8,
        /// Flash sector number to erase (0-11 for the STM, 0-15 for the M25)
        #[cfg_attr(feature = "serde", serde(rename(serialize = "sector_num")))]
        pub sector_num: u32,
    }

    impl MsgFlashErase {
        /// Gets the [FlashTargetToRead][self::FlashTargetToRead] stored in the `target` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `FlashTargetToRead` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `FlashTargetToRead` were added.
        pub fn flash_target_to_read(&self) -> Result<FlashTargetToRead, u8> {
            get_bit_range!(self.target, u8, u8, 0, 0).try_into()
        }

        /// Set the bitrange corresponding to the [FlashTargetToRead][FlashTargetToRead] of the `target` bitfield.
        pub fn set_flash_target_to_read(&mut self, flash_target_to_read: FlashTargetToRead) {
            set_bit_range!(&mut self.target, flash_target_to_read, u8, u8, 0, 0);
        }
    }

    impl ConcreteMessage for MsgFlashErase {
        const MESSAGE_TYPE: u16 = 226;
        const MESSAGE_NAME: &'static str = "MSG_FLASH_ERASE";
    }

    impl SbpMessage for MsgFlashErase {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgFlashErase {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgFlashErase(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgFlashErase {
        const MIN_LEN: usize = <u8 as WireFormat>::MIN_LEN + <u32 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.target) + WireFormat::len(&self.sector_num)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.target, buf);
            WireFormat::write(&self.sector_num, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgFlashErase {
                sender_id: None,
                target: WireFormat::parse_unchecked(buf),
                sector_num: WireFormat::parse_unchecked(buf),
            }
        }
    }

    /// Flash target to read
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum FlashTargetToRead {
        /// FLASH_STM
        FlashStm = 0,

        /// FLASH_M25
        FlashM25 = 1,
    }

    impl std::fmt::Display for FlashTargetToRead {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                FlashTargetToRead::FlashStm => f.write_str("FLASH_STM"),
                FlashTargetToRead::FlashM25 => f.write_str("FLASH_M25"),
            }
        }
    }

    impl TryFrom<u8> for FlashTargetToRead {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, Self::Error> {
            match i {
                0 => Ok(FlashTargetToRead::FlashStm),
                1 => Ok(FlashTargetToRead::FlashM25),
                i => Err(i),
            }
        }
    }
}

pub mod msg_flash_program {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Program flash addresses
    ///
    /// The flash program message programs a set of addresses of either the STM or
    /// M25 flash. The device replies with either a MSG_FLASH_DONE message
    /// containing the return code FLASH_OK (0) on success, or FLASH_INVALID_LEN
    /// (2) if the maximum write size is exceeded. Note that the sector-containing
    /// addresses must be erased before addresses can be programmed.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgFlashProgram {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// Target flags
        #[cfg_attr(feature = "serde", serde(rename(serialize = "target")))]
        pub target: u8,
        /// Starting address offset to program
        #[cfg_attr(feature = "serde", serde(rename(serialize = "addr_start")))]
        pub addr_start: [u8; 3],
        /// Length of set of addresses to program, counting up from starting address
        #[cfg_attr(feature = "serde", serde(rename(serialize = "addr_len")))]
        pub addr_len: u8,
        /// Data to program addresses with, with length N=addr_len
        #[cfg_attr(feature = "serde", serde(rename(serialize = "data")))]
        pub data: Vec<u8>,
    }

    impl MsgFlashProgram {
        /// Gets the [FlashTargetToRead][self::FlashTargetToRead] stored in the `target` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `FlashTargetToRead` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `FlashTargetToRead` were added.
        pub fn flash_target_to_read(&self) -> Result<FlashTargetToRead, u8> {
            get_bit_range!(self.target, u8, u8, 0, 0).try_into()
        }

        /// Set the bitrange corresponding to the [FlashTargetToRead][FlashTargetToRead] of the `target` bitfield.
        pub fn set_flash_target_to_read(&mut self, flash_target_to_read: FlashTargetToRead) {
            set_bit_range!(&mut self.target, flash_target_to_read, u8, u8, 0, 0);
        }
    }

    impl ConcreteMessage for MsgFlashProgram {
        const MESSAGE_TYPE: u16 = 230;
        const MESSAGE_NAME: &'static str = "MSG_FLASH_PROGRAM";
    }

    impl SbpMessage for MsgFlashProgram {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgFlashProgram {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgFlashProgram(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgFlashProgram {
        const MIN_LEN: usize = <u8 as WireFormat>::MIN_LEN
            + <[u8; 3] as WireFormat>::MIN_LEN
            + <u8 as WireFormat>::MIN_LEN
            + <Vec<u8> as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.target)
                + WireFormat::len(&self.addr_start)
                + WireFormat::len(&self.addr_len)
                + WireFormat::len(&self.data)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.target, buf);
            WireFormat::write(&self.addr_start, buf);
            WireFormat::write(&self.addr_len, buf);
            WireFormat::write(&self.data, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgFlashProgram {
                sender_id: None,
                target: WireFormat::parse_unchecked(buf),
                addr_start: WireFormat::parse_unchecked(buf),
                addr_len: WireFormat::parse_unchecked(buf),
                data: WireFormat::parse_unchecked(buf),
            }
        }
    }

    /// Flash target to read
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum FlashTargetToRead {
        /// FLASH_STM
        FlashStm = 0,

        /// FLASH_M25
        FlashM25 = 1,
    }

    impl std::fmt::Display for FlashTargetToRead {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                FlashTargetToRead::FlashStm => f.write_str("FLASH_STM"),
                FlashTargetToRead::FlashM25 => f.write_str("FLASH_M25"),
            }
        }
    }

    impl TryFrom<u8> for FlashTargetToRead {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, Self::Error> {
            match i {
                0 => Ok(FlashTargetToRead::FlashStm),
                1 => Ok(FlashTargetToRead::FlashM25),
                i => Err(i),
            }
        }
    }
}

pub mod msg_flash_read_req {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Read STM or M25 flash address request (host => device)
    ///
    /// The flash read message reads a set of addresses of either the STM or M25
    /// onboard flash. The device replies with a MSG_FLASH_READ_RESP message
    /// containing either the read data on success or a MSG_FLASH_DONE message
    /// containing the return code FLASH_INVALID_LEN (2) if the maximum read size
    /// is exceeded or FLASH_INVALID_ADDR (3) if the address is outside of the
    /// allowed range.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgFlashReadReq {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// Target flags
        #[cfg_attr(feature = "serde", serde(rename(serialize = "target")))]
        pub target: u8,
        /// Starting address offset to read from
        #[cfg_attr(feature = "serde", serde(rename(serialize = "addr_start")))]
        pub addr_start: [u8; 3],
        /// Length of set of addresses to read, counting up from starting address
        #[cfg_attr(feature = "serde", serde(rename(serialize = "addr_len")))]
        pub addr_len: u8,
    }

    impl MsgFlashReadReq {
        /// Gets the [FlashTargetToRead][self::FlashTargetToRead] stored in the `target` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `FlashTargetToRead` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `FlashTargetToRead` were added.
        pub fn flash_target_to_read(&self) -> Result<FlashTargetToRead, u8> {
            get_bit_range!(self.target, u8, u8, 0, 0).try_into()
        }

        /// Set the bitrange corresponding to the [FlashTargetToRead][FlashTargetToRead] of the `target` bitfield.
        pub fn set_flash_target_to_read(&mut self, flash_target_to_read: FlashTargetToRead) {
            set_bit_range!(&mut self.target, flash_target_to_read, u8, u8, 0, 0);
        }
    }

    impl ConcreteMessage for MsgFlashReadReq {
        const MESSAGE_TYPE: u16 = 231;
        const MESSAGE_NAME: &'static str = "MSG_FLASH_READ_REQ";
    }

    impl SbpMessage for MsgFlashReadReq {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgFlashReadReq {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgFlashReadReq(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgFlashReadReq {
        const MIN_LEN: usize = <u8 as WireFormat>::MIN_LEN
            + <[u8; 3] as WireFormat>::MIN_LEN
            + <u8 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.target)
                + WireFormat::len(&self.addr_start)
                + WireFormat::len(&self.addr_len)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.target, buf);
            WireFormat::write(&self.addr_start, buf);
            WireFormat::write(&self.addr_len, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgFlashReadReq {
                sender_id: None,
                target: WireFormat::parse_unchecked(buf),
                addr_start: WireFormat::parse_unchecked(buf),
                addr_len: WireFormat::parse_unchecked(buf),
            }
        }
    }

    /// Flash target to read
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum FlashTargetToRead {
        /// FLASH_STM
        FlashStm = 0,

        /// FLASH_M25
        FlashM25 = 1,
    }

    impl std::fmt::Display for FlashTargetToRead {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                FlashTargetToRead::FlashStm => f.write_str("FLASH_STM"),
                FlashTargetToRead::FlashM25 => f.write_str("FLASH_M25"),
            }
        }
    }

    impl TryFrom<u8> for FlashTargetToRead {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, Self::Error> {
            match i {
                0 => Ok(FlashTargetToRead::FlashStm),
                1 => Ok(FlashTargetToRead::FlashM25),
                i => Err(i),
            }
        }
    }
}

pub mod msg_flash_read_resp {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Read STM or M25 flash address response (host <= device)
    ///
    /// The flash read message reads a set of addresses of either the STM or M25
    /// onboard flash. The device replies with a MSG_FLASH_READ_RESP message
    /// containing either the read data on success or a MSG_FLASH_DONE message
    /// containing the return code FLASH_INVALID_LEN (2) if the maximum read size
    /// is exceeded or FLASH_INVALID_ADDR (3) if the address is outside of the
    /// allowed range.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgFlashReadResp {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// Target flags
        #[cfg_attr(feature = "serde", serde(rename(serialize = "target")))]
        pub target: u8,
        /// Starting address offset to read from
        #[cfg_attr(feature = "serde", serde(rename(serialize = "addr_start")))]
        pub addr_start: [u8; 3],
        /// Length of set of addresses to read, counting up from starting address
        #[cfg_attr(feature = "serde", serde(rename(serialize = "addr_len")))]
        pub addr_len: u8,
    }

    impl MsgFlashReadResp {
        /// Gets the [FlashTargetToRead][self::FlashTargetToRead] stored in the `target` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `FlashTargetToRead` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `FlashTargetToRead` were added.
        pub fn flash_target_to_read(&self) -> Result<FlashTargetToRead, u8> {
            get_bit_range!(self.target, u8, u8, 0, 0).try_into()
        }

        /// Set the bitrange corresponding to the [FlashTargetToRead][FlashTargetToRead] of the `target` bitfield.
        pub fn set_flash_target_to_read(&mut self, flash_target_to_read: FlashTargetToRead) {
            set_bit_range!(&mut self.target, flash_target_to_read, u8, u8, 0, 0);
        }
    }

    impl ConcreteMessage for MsgFlashReadResp {
        const MESSAGE_TYPE: u16 = 225;
        const MESSAGE_NAME: &'static str = "MSG_FLASH_READ_RESP";
    }

    impl SbpMessage for MsgFlashReadResp {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgFlashReadResp {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgFlashReadResp(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgFlashReadResp {
        const MIN_LEN: usize = <u8 as WireFormat>::MIN_LEN
            + <[u8; 3] as WireFormat>::MIN_LEN
            + <u8 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.target)
                + WireFormat::len(&self.addr_start)
                + WireFormat::len(&self.addr_len)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.target, buf);
            WireFormat::write(&self.addr_start, buf);
            WireFormat::write(&self.addr_len, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgFlashReadResp {
                sender_id: None,
                target: WireFormat::parse_unchecked(buf),
                addr_start: WireFormat::parse_unchecked(buf),
                addr_len: WireFormat::parse_unchecked(buf),
            }
        }
    }

    /// Flash target to read
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum FlashTargetToRead {
        /// FLASH_STM
        FlashStm = 0,

        /// FLASH_M25
        FlashM25 = 1,
    }

    impl std::fmt::Display for FlashTargetToRead {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                FlashTargetToRead::FlashStm => f.write_str("FLASH_STM"),
                FlashTargetToRead::FlashM25 => f.write_str("FLASH_M25"),
            }
        }
    }

    impl TryFrom<u8> for FlashTargetToRead {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, Self::Error> {
            match i {
                0 => Ok(FlashTargetToRead::FlashStm),
                1 => Ok(FlashTargetToRead::FlashM25),
                i => Err(i),
            }
        }
    }
}

pub mod msg_m25_flash_write_status {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Write M25 flash status register (host => device)
    ///
    /// The flash status message writes to the 8-bit M25 flash status register.
    /// The device replies with a MSG_FLASH_DONE message.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgM25FlashWriteStatus {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// Byte to write to the M25 flash status register
        #[cfg_attr(feature = "serde", serde(rename(serialize = "status")))]
        pub status: [u8; 1],
    }

    impl ConcreteMessage for MsgM25FlashWriteStatus {
        const MESSAGE_TYPE: u16 = 243;
        const MESSAGE_NAME: &'static str = "MSG_M25_FLASH_WRITE_STATUS";
    }

    impl SbpMessage for MsgM25FlashWriteStatus {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgM25FlashWriteStatus {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgM25FlashWriteStatus(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgM25FlashWriteStatus {
        const MIN_LEN: usize = <[u8; 1] as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.status)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.status, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgM25FlashWriteStatus {
                sender_id: None,
                status: WireFormat::parse_unchecked(buf),
            }
        }
    }
}

pub mod msg_stm_flash_lock_sector {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Lock sector of STM flash memory (host => device)
    ///
    /// The flash lock message locks a sector of the STM flash memory. The device
    /// replies with a MSG_FLASH_DONE message.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgStmFlashLockSector {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// Flash sector number to lock
        #[cfg_attr(feature = "serde", serde(rename(serialize = "sector")))]
        pub sector: u32,
    }

    impl ConcreteMessage for MsgStmFlashLockSector {
        const MESSAGE_TYPE: u16 = 227;
        const MESSAGE_NAME: &'static str = "MSG_STM_FLASH_LOCK_SECTOR";
    }

    impl SbpMessage for MsgStmFlashLockSector {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgStmFlashLockSector {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgStmFlashLockSector(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgStmFlashLockSector {
        const MIN_LEN: usize = <u32 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.sector)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.sector, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgStmFlashLockSector {
                sender_id: None,
                sector: WireFormat::parse_unchecked(buf),
            }
        }
    }
}

pub mod msg_stm_flash_unlock_sector {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Unlock sector of STM flash memory (host => device)
    ///
    /// The flash unlock message unlocks a sector of the STM flash memory. The
    /// device replies with a MSG_FLASH_DONE message.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgStmFlashUnlockSector {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// Flash sector number to unlock
        #[cfg_attr(feature = "serde", serde(rename(serialize = "sector")))]
        pub sector: u32,
    }

    impl ConcreteMessage for MsgStmFlashUnlockSector {
        const MESSAGE_TYPE: u16 = 228;
        const MESSAGE_NAME: &'static str = "MSG_STM_FLASH_UNLOCK_SECTOR";
    }

    impl SbpMessage for MsgStmFlashUnlockSector {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgStmFlashUnlockSector {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgStmFlashUnlockSector(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgStmFlashUnlockSector {
        const MIN_LEN: usize = <u32 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.sector)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.sector, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgStmFlashUnlockSector {
                sender_id: None,
                sector: WireFormat::parse_unchecked(buf),
            }
        }
    }
}

pub mod msg_stm_unique_id_req {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Read device's hard-coded unique ID request (host => device)

    ///
    /// This message reads the device's hard-coded unique ID. The host requests
    /// the ID by sending a MSG_STM_UNIQUE_ID_REQ. The device responds with a
    /// MSG_STM_UNIQUE_ID_RESP with the 12-byte unique ID in the payload.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgStmUniqueIdReq {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
    }

    impl ConcreteMessage for MsgStmUniqueIdReq {
        const MESSAGE_TYPE: u16 = 232;
        const MESSAGE_NAME: &'static str = "MSG_STM_UNIQUE_ID_REQ";
    }

    impl SbpMessage for MsgStmUniqueIdReq {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgStmUniqueIdReq {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgStmUniqueIdReq(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgStmUniqueIdReq {
        const MIN_LEN: usize = 0;
        fn len(&self) -> usize {
            0
        }
        fn write<B: BufMut>(&self, _buf: &mut B) {}
        fn parse_unchecked<B: Buf>(_buf: &mut B) -> Self {
            MsgStmUniqueIdReq { sender_id: None }
        }
    }
}

pub mod msg_stm_unique_id_resp {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::lib::*;

    /// Read device's hard-coded unique ID response (host <= device)

    ///
    /// This message reads the device's hard-coded unique ID. The host requests
    /// the ID by sending a MSG_STM_UNIQUE_ID_REQ. The device responds with a
    /// MSG_STM_UNIQUE_ID_RESP with the 12-byte unique ID in the payload.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgStmUniqueIdResp {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// Device unique ID
        #[cfg_attr(feature = "serde", serde(rename(serialize = "stm_id")))]
        pub stm_id: [u8; 12],
    }

    impl ConcreteMessage for MsgStmUniqueIdResp {
        const MESSAGE_TYPE: u16 = 229;
        const MESSAGE_NAME: &'static str = "MSG_STM_UNIQUE_ID_RESP";
    }

    impl SbpMessage for MsgStmUniqueIdResp {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgStmUniqueIdResp {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgStmUniqueIdResp(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgStmUniqueIdResp {
        const MIN_LEN: usize = <[u8; 12] as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.stm_id)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.stm_id, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgStmUniqueIdResp {
                sender_id: None,
                stm_id: WireFormat::parse_unchecked(buf),
            }
        }
    }
}