rustorb-rtps 0.3.0

DDSI-RTPS wire protocol (messages, discovery, writers and readers) for rustorb-dds
Documentation
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
//! The basic RTPS types and submessage elements
//! (DDSI-RTPS 2.5, 8.3.5, 9.3 and 9.4.2).

use std::fmt;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

use crate::error::DecodeError;
use crate::wire::{Endianness, Reader, Writer};

/// The 12-octet prefix shared by the GUIDs of all entities of one
/// participant (DDSI-RTPS 2.5, 9.3.1.1).
///
/// The first two octets must be the vendor id of the implementation
/// (9.3.1.5).
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct GuidPrefix(pub [u8; 12]);

impl GuidPrefix {
    /// `GUIDPREFIX_UNKNOWN`: all zeros.
    pub const UNKNOWN: GuidPrefix = GuidPrefix([0; 12]);

    /// Makes a prefix that is very likely unique in the domain: the vendor
    /// id in the first two octets (9.3.1.5), then ten octets mixed from
    /// the process id, the time, a counter and the standard library's
    /// random hash keys.
    pub fn generate(vendor_id: VendorId) -> GuidPrefix {
        use std::collections::hash_map::RandomState;
        use std::hash::{BuildHasher, Hasher};
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);

        let mut h = RandomState::new().build_hasher();
        h.write_u32(std::process::id());
        h.write_u64(COUNTER.fetch_add(1, Ordering::Relaxed));
        if let Ok(d) = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
            h.write_u128(d.as_nanos());
        }
        let a = h.finish();
        h.write_u64(a);
        let b = h.finish();
        let mut out = [0u8; 12];
        out[..2].copy_from_slice(&vendor_id.0);
        out[2..10].copy_from_slice(&a.to_be_bytes());
        out[10..].copy_from_slice(&b.to_be_bytes()[..2]);
        GuidPrefix(out)
    }

    pub(crate) fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        r.array().map(GuidPrefix)
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        w.bytes(&self.0);
    }
}

impl fmt::Debug for GuidPrefix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "GuidPrefix(")?;
        for (i, b) in self.0.iter().enumerate() {
            if i > 0 && i % 4 == 0 {
                f.write_str(".")?;
            }
            write!(f, "{b:02x}")?;
        }
        f.write_str(")")
    }
}

/// The identifier of an entity within its participant
/// (DDSI-RTPS 2.5, 9.3.1.2).
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct EntityId {
    /// The three-octet key. Chosen by the implementation unless the entity
    /// is a predefined one.
    pub entity_key: [u8; 3],
    /// The kind of entity; see [`entity_kind`].
    pub entity_kind: u8,
}

/// Values of [`EntityId::entity_kind`] (DDSI-RTPS 2.5, Table 9.1).
///
/// The two most significant bits say who defined the entity: `00` user,
/// `11` built-in, `01` vendor-specific (9.3.1.2).
pub mod entity_kind {
    /// Unknown, user-defined.
    pub const USER_UNKNOWN: u8 = 0x00;
    /// Writer with a key, user-defined.
    pub const USER_WRITER_WITH_KEY: u8 = 0x02;
    /// Writer without a key, user-defined.
    pub const USER_WRITER_NO_KEY: u8 = 0x03;
    /// Reader without a key, user-defined.
    pub const USER_READER_NO_KEY: u8 = 0x04;
    /// Reader with a key, user-defined.
    pub const USER_READER_WITH_KEY: u8 = 0x07;
    /// Writer group, user-defined.
    pub const USER_WRITER_GROUP: u8 = 0x08;
    /// Reader group, user-defined.
    pub const USER_READER_GROUP: u8 = 0x09;
    /// Unknown, built-in.
    pub const BUILTIN_UNKNOWN: u8 = 0xc0;
    /// Participant (always built-in).
    pub const BUILTIN_PARTICIPANT: u8 = 0xc1;
    /// Writer with a key, built-in.
    pub const BUILTIN_WRITER_WITH_KEY: u8 = 0xc2;
    /// Writer without a key, built-in.
    pub const BUILTIN_WRITER_NO_KEY: u8 = 0xc3;
    /// Reader without a key, built-in.
    pub const BUILTIN_READER_NO_KEY: u8 = 0xc4;
    /// Reader with a key, built-in.
    pub const BUILTIN_READER_WITH_KEY: u8 = 0xc7;
    /// Writer group, built-in.
    pub const BUILTIN_WRITER_GROUP: u8 = 0xc8;
    /// Reader group, built-in.
    pub const BUILTIN_READER_GROUP: u8 = 0xc9;
}

impl EntityId {
    const fn new(key: [u8; 3], kind: u8) -> Self {
        EntityId {
            entity_key: key,
            entity_kind: kind,
        }
    }

    /// `ENTITYID_UNKNOWN`.
    pub const UNKNOWN: EntityId = EntityId::new([0, 0, 0], 0x00);
    /// `ENTITYID_PARTICIPANT` (Table 9.2).
    pub const PARTICIPANT: EntityId = EntityId::new([0, 0, 1], 0xc1);
    /// `ENTITYID_SEDP_BUILTIN_TOPICS_ANNOUNCER`.
    pub const SEDP_BUILTIN_TOPICS_ANNOUNCER: EntityId = EntityId::new([0, 0, 2], 0xc2);
    /// `ENTITYID_SEDP_BUILTIN_TOPICS_DETECTOR`.
    pub const SEDP_BUILTIN_TOPICS_DETECTOR: EntityId = EntityId::new([0, 0, 2], 0xc7);
    /// `ENTITYID_SEDP_BUILTIN_PUBLICATIONS_ANNOUNCER`.
    pub const SEDP_BUILTIN_PUBLICATIONS_ANNOUNCER: EntityId = EntityId::new([0, 0, 3], 0xc2);
    /// `ENTITYID_SEDP_BUILTIN_PUBLICATIONS_DETECTOR`.
    pub const SEDP_BUILTIN_PUBLICATIONS_DETECTOR: EntityId = EntityId::new([0, 0, 3], 0xc7);
    /// `ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_ANNOUNCER`.
    pub const SEDP_BUILTIN_SUBSCRIPTIONS_ANNOUNCER: EntityId = EntityId::new([0, 0, 4], 0xc2);
    /// `ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_DETECTOR`.
    pub const SEDP_BUILTIN_SUBSCRIPTIONS_DETECTOR: EntityId = EntityId::new([0, 0, 4], 0xc7);
    /// `ENTITYID_SPDP_BUILTIN_PARTICIPANT_ANNOUNCER`.
    pub const SPDP_BUILTIN_PARTICIPANT_ANNOUNCER: EntityId = EntityId::new([0, 1, 0], 0xc2);
    /// `ENTITYID_SPDP_BUILTIN_PARTICIPANT_DETECTOR`.
    pub const SPDP_BUILTIN_PARTICIPANT_DETECTOR: EntityId = EntityId::new([0, 1, 0], 0xc7);
    /// `ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER`.
    pub const P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER: EntityId = EntityId::new([0, 2, 0], 0xc2);
    /// `ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_READER`.
    pub const P2P_BUILTIN_PARTICIPANT_MESSAGE_READER: EntityId = EntityId::new([0, 2, 0], 0xc7);
    /// `ENTITYID_TL_SVC_REQ_WRITER` (DDS-XTypes 1.3 Table 61).
    pub const TL_SVC_REQ_WRITER: EntityId = EntityId::new([0, 3, 0], 0xc3);
    /// `ENTITYID_TL_SVC_REQ_READER`.
    pub const TL_SVC_REQ_READER: EntityId = EntityId::new([0, 3, 0], 0xc4);
    /// `ENTITYID_TL_SVC_REPLY_WRITER`.
    pub const TL_SVC_REPLY_WRITER: EntityId = EntityId::new([0, 3, 1], 0xc3);
    /// `ENTITYID_TL_SVC_REPLY_READER`.
    pub const TL_SVC_REPLY_READER: EntityId = EntityId::new([0, 3, 1], 0xc4);
    /// SEDPbuiltinPublicationsSecureWriter (DDS Security 1.2, Table 9).
    pub const SEDP_BUILTIN_PUBLICATIONS_SECURE_WRITER: EntityId = EntityId::new([0xff, 0, 3], 0xc2);
    /// SEDPbuiltinPublicationsSecureReader.
    pub const SEDP_BUILTIN_PUBLICATIONS_SECURE_READER: EntityId = EntityId::new([0xff, 0, 3], 0xc7);
    /// SEDPbuiltinSubscriptionsSecureWriter.
    pub const SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_WRITER: EntityId =
        EntityId::new([0xff, 0, 4], 0xc2);
    /// SEDPbuiltinSubscriptionsSecureReader.
    pub const SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_READER: EntityId =
        EntityId::new([0xff, 0, 4], 0xc7);
    /// BuiltinParticipantMessageSecureWriter.
    pub const P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_WRITER: EntityId =
        EntityId::new([0xff, 2, 0], 0xc2);
    /// BuiltinParticipantMessageSecureReader.
    pub const P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_READER: EntityId =
        EntityId::new([0xff, 2, 0], 0xc7);
    /// BuiltinParticipantStatelessMessageWriter.
    pub const P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER: EntityId = EntityId::new([0, 2, 1], 0xc3);
    /// BuiltinParticipantStatelessMessageReader.
    pub const P2P_BUILTIN_PARTICIPANT_STATELESS_READER: EntityId = EntityId::new([0, 2, 1], 0xc4);
    /// BuiltinParticipantVolatileMessageSecureWriter.
    pub const P2P_BUILTIN_PARTICIPANT_VOLATILE_SECURE_WRITER: EntityId =
        EntityId::new([0xff, 2, 2], 0xc3);
    /// BuiltinParticipantVolatileMessageSecureReader.
    pub const P2P_BUILTIN_PARTICIPANT_VOLATILE_SECURE_READER: EntityId =
        EntityId::new([0xff, 2, 2], 0xc4);
    /// SPDPbuiltinParticipantsSecureWriter.
    pub const SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_WRITER: EntityId =
        EntityId::new([0xff, 1, 1], 0xc2);
    /// SPDPbuiltinParticipantsSecureReader.
    pub const SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_READER: EntityId =
        EntityId::new([0xff, 1, 1], 0xc7);

    /// Makes a user-defined entity id from a key and a kind.
    pub const fn user(entity_key: [u8; 3], entity_kind: u8) -> Self {
        EntityId::new(entity_key, entity_kind)
    }

    /// The four octets as they appear on the wire.
    pub const fn to_bytes(self) -> [u8; 4] {
        let k = self.entity_key;
        [k[0], k[1], k[2], self.entity_kind]
    }

    /// Builds an entity id from its four wire octets.
    pub const fn from_bytes(b: [u8; 4]) -> Self {
        EntityId::new([b[0], b[1], b[2]], b[3])
    }

    /// Whether the kind marks a built-in entity (top bits `11`).
    pub const fn is_builtin(self) -> bool {
        self.entity_kind & 0xc0 == 0xc0
    }

    /// Whether the kind marks a vendor-specific entity (top bits `01`).
    pub const fn is_vendor_specific(self) -> bool {
        self.entity_kind & 0xc0 == 0x40
    }

    pub(crate) fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        r.array().map(EntityId::from_bytes)
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        w.bytes(&self.to_bytes());
    }
}

impl fmt::Debug for EntityId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let [a, b, c, d] = self.to_bytes();
        write!(f, "EntityId({a:02x}{b:02x}{c:02x}.{d:02x})")
    }
}

/// A globally unique identifier of an entity (DDSI-RTPS 2.5, 9.3.1.5).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Guid {
    /// The participant part.
    pub prefix: GuidPrefix,
    /// The entity part.
    pub entity_id: EntityId,
}

impl Guid {
    /// `GUID_UNKNOWN`.
    pub const UNKNOWN: Guid = Guid {
        prefix: GuidPrefix::UNKNOWN,
        entity_id: EntityId::UNKNOWN,
    };

    /// Joins a prefix and an entity id.
    pub const fn new(prefix: GuidPrefix, entity_id: EntityId) -> Self {
        Guid { prefix, entity_id }
    }

    /// The 16 octets: the prefix followed by the entity id.
    pub fn to_bytes(self) -> [u8; 16] {
        let mut out = [0u8; 16];
        out[..12].copy_from_slice(&self.prefix.0);
        out[12..].copy_from_slice(&self.entity_id.to_bytes());
        out
    }

    /// From the 16 octets of [`to_bytes`](Self::to_bytes).
    pub fn from_bytes(b: [u8; 16]) -> Self {
        let mut prefix = [0u8; 12];
        prefix.copy_from_slice(&b[..12]);
        Guid::new(
            GuidPrefix(prefix),
            EntityId::from_bytes([b[12], b[13], b[14], b[15]]),
        )
    }
}

/// The vendor of an RTPS implementation (DDSI-RTPS 2.5, 8.3.3.1.3).
///
/// Values are assigned by the OMG.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct VendorId(pub [u8; 2]);

impl VendorId {
    /// `VENDORID_UNKNOWN`.
    pub const UNKNOWN: VendorId = VendorId([0, 0]);

    pub(crate) fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        r.array().map(VendorId)
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        w.bytes(&self.0);
    }
}

/// An RTPS protocol version (DDSI-RTPS 2.5, 9.4.2.4).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ProtocolVersion {
    /// Major version.
    pub major: u8,
    /// Minor version.
    pub minor: u8,
}

impl ProtocolVersion {
    /// 1.0
    pub const V1_0: ProtocolVersion = ProtocolVersion { major: 1, minor: 0 };
    /// 1.1
    pub const V1_1: ProtocolVersion = ProtocolVersion { major: 1, minor: 1 };
    /// 2.0
    pub const V2_0: ProtocolVersion = ProtocolVersion { major: 2, minor: 0 };
    /// 2.1
    pub const V2_1: ProtocolVersion = ProtocolVersion { major: 2, minor: 1 };
    /// 2.2
    pub const V2_2: ProtocolVersion = ProtocolVersion { major: 2, minor: 2 };
    /// 2.3
    pub const V2_3: ProtocolVersion = ProtocolVersion { major: 2, minor: 3 };
    /// 2.4
    pub const V2_4: ProtocolVersion = ProtocolVersion { major: 2, minor: 4 };
    /// 2.5
    pub const V2_5: ProtocolVersion = ProtocolVersion { major: 2, minor: 5 };
    /// `PROTOCOLVERSION`: the version this crate implements, 2.5.
    pub const CURRENT: ProtocolVersion = ProtocolVersion::V2_5;

    pub(crate) fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        let [major, minor] = r.array()?;
        Ok(ProtocolVersion { major, minor })
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        w.bytes(&[self.major, self.minor]);
    }
}

/// A 64-bit signed sequence number (DDSI-RTPS 2.5, 8.3.5.4 and 9.3.2.10).
///
/// On the wire it is `{ long high; unsigned long low; }` and the value is
/// `high * 2^32 + low`. Sequence numbers start at 1.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SequenceNumber(pub i64);

impl SequenceNumber {
    /// `SEQUENCENUMBER_UNKNOWN`: high = -1, low = 0.
    pub const UNKNOWN: SequenceNumber = SequenceNumber(-(1 << 32));

    pub(crate) fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        let high = r.i32()?;
        let low = r.u32()?;
        Ok(SequenceNumber(((high as i64) << 32) | low as i64))
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        w.i32((self.0 >> 32) as i32);
        w.u32(self.0 as u32);
    }
}

/// The identity of a sample: its writer and its sequence number
/// (`SampleIdentity`, DDS-RPC 1.0, 7.5.1.1.1). A reply carries that of
/// its request in-line, as `PID_RELATED_SAMPLE_IDENTITY` (7.8.2).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SampleIdentity {
    /// `writer_guid`.
    pub writer_guid: Guid,
    /// `sequence_number`.
    pub sequence_number: SequenceNumber,
}

impl SampleIdentity {
    /// `RELATED_SAMPLE_IDENTITY_INVALID`: `GUID_UNKNOWN` and
    /// `SEQUENCENUMBER_UNKNOWN`.
    pub const INVALID: SampleIdentity = SampleIdentity {
        writer_guid: Guid::UNKNOWN,
        sequence_number: SequenceNumber::UNKNOWN,
    };

    /// The CDR serialization (24 octets) in `endianness`.
    pub fn to_bytes(&self, endianness: Endianness) -> Vec<u8> {
        let mut out = Vec::with_capacity(24);
        let mut w = Writer::new(&mut out, endianness);
        w.bytes(&self.writer_guid.to_bytes());
        self.sequence_number.write(&mut w);
        out
    }

    /// Reads the CDR serialization in `endianness`.
    pub fn from_bytes(bytes: &[u8], endianness: Endianness) -> Result<Self, DecodeError> {
        let mut r = Reader::new(bytes, endianness);
        let prefix = GuidPrefix::read(&mut r)?;
        let entity_id = EntityId::read(&mut r)?;
        Ok(SampleIdentity {
            writer_guid: Guid::new(prefix, entity_id),
            sequence_number: SequenceNumber::read(&mut r)?,
        })
    }
}

/// A 32-bit fragment number, starting at 1 (DDSI-RTPS 2.5, 8.3.5.6).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FragmentNumber(pub u32);

/// The bitmap shared by [`SequenceNumberSet`] and [`FragmentNumberSet`]
/// (DDSI-RTPS 2.5, 9.4.2.6): up to 256 bits, the first bit is the most
/// significant bit of the first 32-bit word.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
struct Bitmap {
    num_bits: u32,
    words: [u32; 8],
}

impl Bitmap {
    const MAX_BITS: u32 = 256;

    fn new(num_bits: u32) -> Option<Self> {
        (num_bits <= Self::MAX_BITS).then_some(Bitmap {
            num_bits,
            words: [0; 8],
        })
    }

    fn get(&self, offset: u32) -> bool {
        offset < self.num_bits
            && self.words[(offset / 32) as usize] & (1 << (31 - offset % 32)) != 0
    }

    fn set(&mut self, offset: u32) -> bool {
        if offset >= self.num_bits {
            return false;
        }
        self.words[(offset / 32) as usize] |= 1 << (31 - offset % 32);
        true
    }

    fn ones(&self) -> impl Iterator<Item = u32> + '_ {
        (0..self.num_bits).filter(|&i| self.get(i))
    }

    fn word_count(&self) -> usize {
        self.num_bits.div_ceil(32) as usize
    }

    /// Reads `numBits` and the words. Bits past `numBits` are cleared, as
    /// they carry no meaning.
    fn read(r: &mut Reader<'_>, invalid: DecodeError) -> Result<Self, DecodeError> {
        let num_bits = r.u32()?;
        let mut bitmap = Bitmap::new(num_bits).ok_or(invalid)?;
        for i in 0..bitmap.word_count() {
            bitmap.words[i] = r.u32()?;
        }
        let rem = num_bits % 32;
        if rem != 0 {
            let last = bitmap.word_count() - 1;
            bitmap.words[last] &= !(u32::MAX >> rem);
        }
        Ok(bitmap)
    }

    fn write(&self, w: &mut Writer<'_>) {
        w.u32(self.num_bits);
        for word in &self.words[..self.word_count()] {
            w.u32(*word);
        }
    }
}

/// A set of up to 256 sequence numbers starting at `base`
/// (DDSI-RTPS 2.5, 8.3.5.5 and 9.4.2.6).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct SequenceNumberSet {
    base: SequenceNumber,
    bitmap: Bitmap,
}

impl SequenceNumberSet {
    /// An empty set covering `base .. base + num_bits`.
    ///
    /// Returns `None` if `base` is less than 1 or `num_bits` is more than
    /// 256.
    pub fn new(base: SequenceNumber, num_bits: u32) -> Option<Self> {
        if base.0 < 1 {
            return None;
        }
        Some(SequenceNumberSet {
            base,
            bitmap: Bitmap::new(num_bits)?,
        })
    }

    /// The first sequence number the set can hold (`bitmapBase`).
    pub fn base(&self) -> SequenceNumber {
        self.base
    }

    /// How many sequence numbers, from `base`, the set covers (`numBits`).
    pub fn num_bits(&self) -> u32 {
        self.bitmap.num_bits
    }

    /// Adds `sn`. Returns false, and leaves the set as it is, when `sn` is
    /// outside `base .. base + num_bits`.
    pub fn insert(&mut self, sn: SequenceNumber) -> bool {
        match sn.0.checked_sub(self.base.0) {
            Some(d) if (0..self.bitmap.num_bits as i64).contains(&d) => self.bitmap.set(d as u32),
            _ => false,
        }
    }

    /// Whether `sn` is in the set.
    pub fn contains(&self, sn: SequenceNumber) -> bool {
        match sn.0.checked_sub(self.base.0) {
            Some(d) if (0..self.bitmap.num_bits as i64).contains(&d) => self.bitmap.get(d as u32),
            _ => false,
        }
    }

    /// The sequence numbers in the set, in increasing order.
    pub fn iter(&self) -> impl Iterator<Item = SequenceNumber> + '_ {
        self.bitmap
            .ones()
            .map(move |d| SequenceNumber(self.base.0 + d as i64))
    }

    pub(crate) fn read(r: &mut Reader<'_>, id: u8) -> Result<Self, DecodeError> {
        let invalid = DecodeError::InvalidSubmessage {
            id,
            reason: "invalid SequenceNumberSet",
        };
        let base = SequenceNumber::read(r)?;
        if base.0 < 1 {
            return Err(invalid);
        }
        let bitmap = Bitmap::read(r, invalid)?;
        Ok(SequenceNumberSet { base, bitmap })
    }

    /// Like `read`, but also takes an empty set with base 0 and returns
    /// it as an empty set with base 1. Base 0 is invalid (8.3.5.5), but
    /// some implementations send it in an ACKNACK to say that nothing was
    /// received yet; "everything before 0" and "everything before 1"
    /// acknowledge the same nothing.
    pub(crate) fn read_allowing_empty_zero(
        r: &mut Reader<'_>,
        id: u8,
    ) -> Result<Self, DecodeError> {
        let invalid = DecodeError::InvalidSubmessage {
            id,
            reason: "invalid SequenceNumberSet",
        };
        let base = SequenceNumber::read(r)?;
        let bitmap = Bitmap::read(r, invalid.clone())?;
        if base.0 >= 1 {
            Ok(SequenceNumberSet { base, bitmap })
        } else if base.0 == 0 && bitmap.num_bits == 0 {
            Ok(SequenceNumberSet {
                base: SequenceNumber(1),
                bitmap,
            })
        } else {
            Err(invalid)
        }
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        self.base.write(w);
        self.bitmap.write(w);
    }
}

/// A set of up to 256 fragment numbers starting at `base`
/// (DDSI-RTPS 2.5, 8.3.5.7 and 9.4.2.8).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FragmentNumberSet {
    base: FragmentNumber,
    bitmap: Bitmap,
}

impl FragmentNumberSet {
    /// An empty set covering `base .. base + num_bits`.
    ///
    /// Returns `None` if `base` is 0 or `num_bits` is more than 256.
    pub fn new(base: FragmentNumber, num_bits: u32) -> Option<Self> {
        if base.0 < 1 {
            return None;
        }
        Some(FragmentNumberSet {
            base,
            bitmap: Bitmap::new(num_bits)?,
        })
    }

    /// The first fragment number the set can hold (`bitmapBase`).
    pub fn base(&self) -> FragmentNumber {
        self.base
    }

    /// How many fragment numbers, from `base`, the set covers (`numBits`).
    pub fn num_bits(&self) -> u32 {
        self.bitmap.num_bits
    }

    /// Adds `n`. Returns false when `n` is outside the range of the set.
    pub fn insert(&mut self, n: FragmentNumber) -> bool {
        match n.0.checked_sub(self.base.0) {
            Some(d) => self.bitmap.set(d),
            None => false,
        }
    }

    /// Whether `n` is in the set.
    pub fn contains(&self, n: FragmentNumber) -> bool {
        match n.0.checked_sub(self.base.0) {
            Some(d) => self.bitmap.get(d),
            None => false,
        }
    }

    /// The fragment numbers in the set, in increasing order.
    pub fn iter(&self) -> impl Iterator<Item = FragmentNumber> + '_ {
        self.bitmap
            .ones()
            .map(move |d| FragmentNumber(self.base.0 + d))
    }

    pub(crate) fn read(r: &mut Reader<'_>, id: u8) -> Result<Self, DecodeError> {
        let invalid = DecodeError::InvalidSubmessage {
            id,
            reason: "invalid FragmentNumberSet",
        };
        let base = FragmentNumber(r.u32()?);
        if base.0 < 1 {
            return Err(invalid);
        }
        let bitmap = Bitmap::read(r, invalid)?;
        Ok(FragmentNumberSet { base, bitmap })
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        w.u32(self.base.0);
        self.bitmap.write(w);
    }
}

/// A point in time in NTP format: `seconds + fraction / 2^32`
/// (DDSI-RTPS 2.5, 9.3.2.1 and 9.3.2.2).
///
/// The IDL in 9.3.2.1 makes `seconds` an `unsigned long`, and the special
/// values use `0xffffffff`, so it is held as `u32` here. (The drawing in
/// 9.4.2.9 labels it `long`; the bits are the same.)
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Time {
    /// Whole seconds.
    pub seconds: u32,
    /// Fraction of a second in units of 2^-32 s.
    pub fraction: u32,
}

impl Time {
    /// `TIME_ZERO`.
    pub const ZERO: Time = Time {
        seconds: 0,
        fraction: 0,
    };
    /// `TIME_INVALID`.
    pub const INVALID: Time = Time {
        seconds: 0xffff_ffff,
        fraction: 0xffff_ffff,
    };
    /// `TIME_INFINITE`.
    pub const INFINITE: Time = Time {
        seconds: 0xffff_ffff,
        fraction: 0xffff_fffe,
    };

    /// The current time, counted from the UNIX epoch.
    ///
    /// DDSI-RTPS does not fix the origin of `Time_t`; the UNIX epoch is
    /// assumed here and is to be checked against other implementations
    /// (rustorb-dds#8).
    pub fn now() -> Time {
        let d = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default();
        Time {
            seconds: d.as_secs() as u32,
            fraction: ((u64::from(d.subsec_nanos()) << 32) / 1_000_000_000) as u32,
        }
    }

    pub(crate) fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        Ok(Time {
            seconds: r.u32()?,
            fraction: r.u32()?,
        })
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        w.u32(self.seconds);
        w.u32(self.fraction);
    }
}

/// A counter used to detect duplicate submessages
/// (DDSI-RTPS 2.5, 8.3.5.10 and 9.4.2.13).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Count(pub i32);

/// A 64-bit count of changes, used in GAP
/// (DDSI-RTPS 2.5, 8.3.5.11 and 9.3.2.11).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ChangeCount(pub i64);

impl ChangeCount {
    pub(crate) fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        SequenceNumber::read(r).map(|s| ChangeCount(s.0))
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        SequenceNumber(self.0).write(w);
    }
}

/// Four octets that stand for a group of entities
/// (DDSI-RTPS 2.5, 9.3.2.5).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct GroupDigest(pub [u8; 4]);

/// A transport address (DDSI-RTPS 2.5, 9.3.2.4).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Locator {
    /// The transport; see the `KIND_*` constants.
    pub kind: i32,
    /// The port.
    pub port: u32,
    /// The address. IPv4 addresses use the last four octets.
    pub address: [u8; 16],
}

impl Locator {
    /// `LOCATOR_KIND_INVALID`.
    pub const KIND_INVALID: i32 = -1;
    /// `LOCATOR_KIND_RESERVED`.
    pub const KIND_RESERVED: i32 = 0;
    /// `LOCATOR_KIND_UDPv4`.
    pub const KIND_UDPV4: i32 = 1;
    /// `LOCATOR_KIND_UDPv6`.
    pub const KIND_UDPV6: i32 = 2;
    /// The shared memory transport of rustorb-dds (a vendor kind, "RD"
    /// and 1): the address holds the host and the segment, the port the
    /// traffic (see the module `shm`).
    pub const KIND_SHM: i32 = 0x5244_0001;
    /// `LOCATOR_PORT_INVALID`.
    pub const PORT_INVALID: u32 = 0;
    /// `LOCATOR_ADDRESS_INVALID`.
    pub const ADDRESS_INVALID: [u8; 16] = [0; 16];
    /// `LOCATOR_INVALID`.
    pub const INVALID: Locator = Locator {
        kind: Locator::KIND_INVALID,
        port: Locator::PORT_INVALID,
        address: Locator::ADDRESS_INVALID,
    };

    /// The size of a locator on the wire.
    pub(crate) const WIRE_SIZE: usize = 24;

    /// A UDPv4 locator.
    pub fn udpv4(addr: SocketAddrV4) -> Self {
        let mut address = [0u8; 16];
        address[12..].copy_from_slice(&addr.ip().octets());
        Locator {
            kind: Locator::KIND_UDPV4,
            port: addr.port() as u32,
            address,
        }
    }

    /// A UDPv6 locator.
    pub fn udpv6(addr: SocketAddrV6) -> Self {
        Locator {
            kind: Locator::KIND_UDPV6,
            port: addr.port() as u32,
            address: addr.ip().octets(),
        }
    }

    /// The UDP socket address, if this is a UDPv4 or UDPv6 locator whose
    /// port fits in 16 bits.
    pub fn to_socket_addr(&self) -> Option<SocketAddr> {
        let port = u16::try_from(self.port).ok()?;
        match self.kind {
            Locator::KIND_UDPV4 => {
                let a = &self.address;
                let ip = Ipv4Addr::new(a[12], a[13], a[14], a[15]);
                Some(SocketAddr::V4(SocketAddrV4::new(ip, port)))
            }
            Locator::KIND_UDPV6 => Some(SocketAddr::V6(SocketAddrV6::new(
                Ipv6Addr::from(self.address),
                port,
                0,
                0,
            ))),
            _ => None,
        }
    }

    pub(crate) fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        Ok(Locator {
            kind: r.i32()?,
            port: r.u32()?,
            address: r.array()?,
        })
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        w.i32(self.kind);
        w.u32(self.port);
        w.bytes(&self.address);
    }
}

/// The compact UDPv4 locator of INFO_REPLY_IP4
/// (DDSI-RTPS 2.5, 9.4.3.1).
///
/// The specification gives both fields as `unsigned long` and does not say
/// how `address` maps to dotted notation. [`LocatorUdpV4::ip`] reads it
/// with `a` as the most significant octet; this needs to be checked
/// against other implementations (rustorb-dds#8).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct LocatorUdpV4 {
    /// The IPv4 address as a 32-bit number.
    pub address: u32,
    /// The port.
    pub port: u32,
}

impl LocatorUdpV4 {
    /// The address as an [`Ipv4Addr`], `a` being the most significant
    /// octet of `address`.
    pub fn ip(&self) -> Ipv4Addr {
        Ipv4Addr::from(self.address)
    }

    pub(crate) fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        Ok(LocatorUdpV4 {
            address: r.u32()?,
            port: r.u32()?,
        })
    }

    pub(crate) fn write(&self, w: &mut Writer<'_>) {
        w.u32(self.address);
        w.u32(self.port);
    }
}

/// A length of time in NTP format: `seconds + fraction / 2^32`
/// (DDSI-RTPS 2.5, 9.3.2.1 and 9.3.2.3).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Duration {
    /// Whole seconds.
    pub seconds: i32,
    /// Fraction of a second in units of 2^-32 s.
    pub fraction: u32,
}

impl Duration {
    /// `DURATION_ZERO`.
    pub const ZERO: Duration = Duration {
        seconds: 0,
        fraction: 0,
    };
    /// `DURATION_INFINITE`.
    pub const INFINITE: Duration = Duration {
        seconds: 0x7fff_ffff,
        fraction: 0xffff_ffff,
    };

    /// Whole seconds.
    pub const fn from_secs(seconds: i32) -> Duration {
        Duration {
            seconds,
            fraction: 0,
        }
    }

    /// Converts to a [`std::time::Duration`]. Negative values give zero;
    /// [`Duration::INFINITE`] gives `None`.
    pub fn to_std(self) -> Option<std::time::Duration> {
        if self == Duration::INFINITE {
            return None;
        }
        if self.seconds < 0 {
            return Some(std::time::Duration::ZERO);
        }
        let nanos = (u64::from(self.fraction) * 1_000_000_000) >> 32;
        Some(std::time::Duration::new(self.seconds as u64, nanos as u32))
    }

    /// Converts from a [`std::time::Duration`], saturating at the largest
    /// finite value.
    pub fn from_std(d: std::time::Duration) -> Duration {
        let Ok(seconds) = i32::try_from(d.as_secs()) else {
            return Duration {
                seconds: i32::MAX,
                fraction: 0xffff_fffe,
            };
        };
        Duration {
            seconds,
            fraction: ((u64::from(d.subsec_nanos()) << 32) / 1_000_000_000) as u32,
        }
    }
}

/// The built-in endpoints a participant has (DDSI-RTPS 2.5, 9.3.2.12).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct BuiltinEndpointSet(pub u32);

impl BuiltinEndpointSet {
    /// `DISC_BUILTIN_ENDPOINT_PARTICIPANT_ANNOUNCER`.
    pub const PARTICIPANT_ANNOUNCER: u32 = 1 << 0;
    /// `DISC_BUILTIN_ENDPOINT_PARTICIPANT_DETECTOR`.
    pub const PARTICIPANT_DETECTOR: u32 = 1 << 1;
    /// `DISC_BUILTIN_ENDPOINT_PUBLICATIONS_ANNOUNCER`.
    pub const PUBLICATIONS_ANNOUNCER: u32 = 1 << 2;
    /// `DISC_BUILTIN_ENDPOINT_PUBLICATIONS_DETECTOR`.
    pub const PUBLICATIONS_DETECTOR: u32 = 1 << 3;
    /// `DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_ANNOUNCER`.
    pub const SUBSCRIPTIONS_ANNOUNCER: u32 = 1 << 4;
    /// `DISC_BUILTIN_ENDPOINT_SUBSCRIPTIONS_DETECTOR`.
    pub const SUBSCRIPTIONS_DETECTOR: u32 = 1 << 5;
    /// `BUILTIN_ENDPOINT_PARTICIPANT_MESSAGE_DATA_WRITER`.
    pub const PARTICIPANT_MESSAGE_DATA_WRITER: u32 = 1 << 10;
    /// `BUILTIN_ENDPOINT_PARTICIPANT_MESSAGE_DATA_READER`.
    pub const PARTICIPANT_MESSAGE_DATA_READER: u32 = 1 << 11;
    /// TypeLookupServiceRequestDataWriter (DDS-XTypes 1.3 Table 62).
    pub const TYPE_LOOKUP_REQUEST_WRITER: u32 = 1 << 12;
    /// TypeLookupServiceRequestDataReader.
    pub const TYPE_LOOKUP_REQUEST_READER: u32 = 1 << 13;
    /// TypeLookupServiceReplyDataWriter.
    pub const TYPE_LOOKUP_REPLY_WRITER: u32 = 1 << 14;
    /// TypeLookupServiceReplyDataReader.
    pub const TYPE_LOOKUP_REPLY_READER: u32 = 1 << 15;
    /// `DISC_BUILTIN_ENDPOINT_TOPICS_ANNOUNCER`.
    /// SEDPbuiltinPublicationsSecureWriter (DDS Security 1.2, Table 11).
    pub const PUBLICATIONS_SECURE_WRITER: u32 = 1 << 16;
    /// SEDPbuiltinPublicationsSecureReader.
    pub const PUBLICATIONS_SECURE_READER: u32 = 1 << 17;
    /// SEDPbuiltinSubscriptionsSecureWriter.
    pub const SUBSCRIPTIONS_SECURE_WRITER: u32 = 1 << 18;
    /// SEDPbuiltinSubscriptionsSecureReader.
    pub const SUBSCRIPTIONS_SECURE_READER: u32 = 1 << 19;
    /// BuiltinParticipantMessageSecureWriter.
    pub const PARTICIPANT_MESSAGE_SECURE_WRITER: u32 = 1 << 20;
    /// BuiltinParticipantMessageSecureReader.
    pub const PARTICIPANT_MESSAGE_SECURE_READER: u32 = 1 << 21;
    /// BuiltinParticipantStatelessMessageWriter.
    pub const PARTICIPANT_STATELESS_MESSAGE_WRITER: u32 = 1 << 22;
    /// BuiltinParticipantStatelessMessageReader.
    pub const PARTICIPANT_STATELESS_MESSAGE_READER: u32 = 1 << 23;
    /// BuiltinParticipantVolatileMessageSecureWriter.
    pub const PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER: u32 = 1 << 24;
    /// BuiltinParticipantVolatileMessageSecureReader.
    pub const PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER: u32 = 1 << 25;
    /// SPDPbuiltinParticipantsSecureWriter.
    pub const PARTICIPANT_SECURE_WRITER: u32 = 1 << 26;
    /// SPDPbuiltinParticipantsSecureReader.
    pub const PARTICIPANT_SECURE_READER: u32 = 1 << 27;
    /// TOPICS_ANNOUNCER.
    pub const TOPICS_ANNOUNCER: u32 = 1 << 28;
    /// `DISC_BUILTIN_ENDPOINT_TOPICS_DETECTOR`.
    pub const TOPICS_DETECTOR: u32 = 1 << 29;

    /// Whether all the given bits are set.
    pub const fn contains(self, bits: u32) -> bool {
        self.0 & bits == bits
    }
}

#[cfg(test)]
mod sample_identity_tests {
    use super::*;

    #[test]
    fn sample_identity_round_trip() {
        let id = SampleIdentity {
            writer_guid: Guid::new(GuidPrefix([1; 12]), EntityId::PARTICIPANT),
            sequence_number: SequenceNumber((2 << 32) | 3),
        };
        let le = id.to_bytes(Endianness::Little);
        assert_eq!(&le[16..], &[2, 0, 0, 0, 3, 0, 0, 0]);
        assert_eq!(
            SampleIdentity::from_bytes(&le, Endianness::Little).unwrap(),
            id
        );
        let be = id.to_bytes(Endianness::Big);
        assert_eq!(&be[16..], &[0, 0, 0, 2, 0, 0, 0, 3]);
        assert_eq!(
            SampleIdentity::from_bytes(&be, Endianness::Big).unwrap(),
            id
        );
        assert!(SampleIdentity::from_bytes(&be[..20], Endianness::Big).is_err());
    }
}