1use std::{fmt, hash::Hash, ops::RangeBounds};
2
3use speedy::{Context, Readable, Reader, Writable, Writer};
4use serde::{Deserialize, Serialize};
5use cdr_encoding_size::*;
6use mio_06::Token;
7use log::warn;
8use static_assertions as sa;
9
10use crate::dds::key::Key;
11
12#[derive(
14 Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Serialize, Deserialize, CdrEncodingSize,
15)]
16pub struct GuidPrefix {
17 pub(crate) bytes: [u8; 12],
18}
19
20impl GuidPrefix {
21 pub const UNKNOWN: Self = Self { bytes: [0x00; 12] };
22
23 pub fn new(prefix: &[u8]) -> Self {
24 let mut bytes: [u8; 12] = [0; 12];
25 for (ix, data) in prefix.iter().enumerate() {
26 if ix >= 12 {
27 break;
28 }
29 bytes[ix] = *data;
30 }
31 Self { bytes }
32 }
33
34 pub fn random_for_this_participant() -> Self {
35 let mut bytes: [u8; 12] = rand::random(); let my_vendor_id_bytes = crate::messages::vendor_id::VendorId::THIS_IMPLEMENTATION.as_bytes();
41 bytes[0] = my_vendor_id_bytes[0];
42 bytes[1] = my_vendor_id_bytes[1];
43
44 Self { bytes }
49 }
50
51 pub fn range(&self) -> impl RangeBounds<GUID> {
52 GUID::new(*self, EntityId::MIN)..=GUID::new(*self, EntityId::MAX)
53 }
54}
55
56impl AsRef<[u8]> for GuidPrefix {
57 fn as_ref(&self) -> &[u8] {
58 &self.bytes
59 }
60}
61
62impl fmt::Debug for GuidPrefix {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 for b in self.bytes.iter() {
66 write!(f, "{b:02x}")?;
67 }
68 Ok(())
69 }
70}
71
72impl Default for GuidPrefix {
73 fn default() -> Self {
74 Self::UNKNOWN
75 }
76}
77
78impl<'a, C: Context> Readable<'a, C> for GuidPrefix {
79 #[inline]
80 fn read_from<R: Reader<'a, C>>(reader: &mut R) -> Result<Self, C::Error> {
81 let mut guid_prefix = Self::default();
82 for i in 0..guid_prefix.bytes.len() {
83 guid_prefix.bytes[i] = reader.read_u8()?;
84 }
85 Ok(guid_prefix)
86 }
87
88 #[inline]
89 fn minimum_bytes_needed() -> usize {
90 std::mem::size_of::<Self>()
91 }
92}
93
94impl<C: Context> Writable<C> for GuidPrefix {
95 #[inline]
96 fn write_to<T: ?Sized + Writer<C>>(&self, writer: &mut T) -> Result<(), C::Error> {
97 for elem in &self.bytes {
98 writer.write_u8(*elem)?;
99 }
100 Ok(())
101 }
102}
103
104#[derive(
105 Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Serialize, Deserialize, CdrEncodingSize,
106)]
107pub struct EntityKind(u8);
108
109impl EntityKind {
110 pub const UNKNOWN_USER_DEFINED: Self = Self(0x00);
112 pub const WRITER_WITH_KEY_USER_DEFINED: Self = Self(0x02);
115 pub const WRITER_NO_KEY_USER_DEFINED: Self = Self(0x03);
116 pub const READER_NO_KEY_USER_DEFINED: Self = Self(0x04);
117 pub const READER_WITH_KEY_USER_DEFINED: Self = Self(0x07);
118 pub const WRITER_GROUP_USER_DEFINED: Self = Self(0x08);
119 pub const READER_GROUP_USER_DEFINED: Self = Self(0x09);
120
121 pub const UNKNOWN_BUILT_IN: Self = Self(0xC0);
122 pub const PARTICIPANT_BUILT_IN: Self = Self(0xC1);
123 pub const WRITER_WITH_KEY_BUILT_IN: Self = Self(0xC2);
124 pub const WRITER_NO_KEY_BUILT_IN: Self = Self(0xC3);
125 pub const READER_NO_KEY_BUILT_IN: Self = Self(0xC4);
126 pub const READER_WITH_KEY_BUILT_IN: Self = Self(0xC7);
127 pub const WRITER_GROUP_BUILT_IN: Self = Self(0xC8);
128 pub const READER_GROUP_BUILT_IN: Self = Self(0xC9);
129
130 pub const MIN: Self = Self(0x00);
131 pub const MAX: Self = Self(0xFF);
132
133 pub const POLL_TOKEN_BASE: usize = 0x40;
142 pub fn is_reader(&self) -> bool {
155 let e = self.0 & 0x0F;
156 e == 0x04 || e == 0x07 || e == 0x09
157 }
158
159 pub fn is_writer(&self) -> bool {
160 let e = self.0 & 0x0F;
161 e == 0x02 || e == 0x03 || e == 0x08
162 }
163
164 pub fn is_built_in(&self) -> bool {
165 (self.0 & 0xF0) == 0xC0
166 }
167
168 pub fn is_user_defined(&self) -> bool {
169 (self.0 & 0xF0) == 0x00
170 }
171}
172
173impl From<u8> for EntityKind {
174 fn from(b: u8) -> Self {
175 Self(b)
176 }
177}
178
179impl From<EntityKind> for u8 {
180 fn from(ek: EntityKind) -> Self {
181 ek.0
182 }
183}
184impl fmt::Debug for EntityKind {
185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186 match *self {
187 Self::UNKNOWN_USER_DEFINED => f.write_str("EntityKind::UNKNOWN_USER_DEFINED"),
188 Self::WRITER_WITH_KEY_USER_DEFINED => f.write_str("EntityKind::WRITER_WITH_KEY_USER_DEFINED"),
189 Self::WRITER_NO_KEY_USER_DEFINED => f.write_str("EntityKind::WRITER_NO_KEY_USER_DEFINED"),
190 Self::READER_NO_KEY_USER_DEFINED => f.write_str("EntityKind::READER_NO_KEY_USER_DEFINED"),
191 Self::READER_WITH_KEY_USER_DEFINED => f.write_str("EntityKind::READER_WITH_KEY_USER_DEFINED"),
192 Self::WRITER_GROUP_USER_DEFINED => f.write_str("EntityKind::WRITER_GROUP_USER_DEFINED"),
193 Self::READER_GROUP_USER_DEFINED => f.write_str("EntityKind::READER_GROUP_USER_DEFINED"),
194
195 Self::UNKNOWN_BUILT_IN => f.write_str("EntityKind::UNKNOWN_BUILT_IN"),
196 Self::PARTICIPANT_BUILT_IN => f.write_str("EntityKind::PARTICIPANT_BUILT_IN"),
197 Self::WRITER_WITH_KEY_BUILT_IN => f.write_str("EntityKind::WRITER_WITH_KEY_BUILT_IN"),
198 Self::WRITER_NO_KEY_BUILT_IN => f.write_str("EntityKind::WRITER_NO_KEY_BUILT_IN"),
199 Self::READER_NO_KEY_BUILT_IN => f.write_str("EntityKind::READER_NO_KEY_BUILT_IN"),
200 Self::READER_WITH_KEY_BUILT_IN => f.write_str("EntityKind::READER_WITH_KEY_BUILT_IN"),
201 Self::WRITER_GROUP_BUILT_IN => f.write_str("EntityKind::WRITER_GROUP_BUILT_IN"),
202 Self::READER_GROUP_BUILT_IN => f.write_str("EntityKind::READER_GROUP_BUILT_IN"),
203 _ => f.write_fmt(format_args!("EntityKind({:x?})", self.0)),
204 }
205 }
206}
207
208#[derive(
211 Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Serialize, Deserialize, CdrEncodingSize,
212)]
213pub struct EntityId {
214 pub entity_key: [u8; 3],
215 pub entity_kind: EntityKind,
216}
217
218sa::const_assert!(std::mem::size_of::<usize>() >= std::mem::size_of::<u32>());
221
222#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Ord, Eq)]
223pub enum TokenDecode {
224 Entity(EntityId),
225 AltEntity(EntityId),
226 FixedToken(Token),
227}
228
229impl EntityId {
230 pub const UNKNOWN: Self = Self {
231 entity_key: [0x00; 3],
232 entity_kind: EntityKind::UNKNOWN_USER_DEFINED,
233 };
234 pub const PARTICIPANT: Self = Self {
235 entity_key: [0x00, 0x00, 0x01],
236 entity_kind: EntityKind::PARTICIPANT_BUILT_IN,
237 };
238 pub const SEDP_BUILTIN_TOPIC_WRITER: Self = Self {
239 entity_key: [0x00, 0x00, 0x02],
240 entity_kind: EntityKind::WRITER_WITH_KEY_BUILT_IN,
241 };
242 pub const SEDP_BUILTIN_TOPIC_READER: Self = Self {
243 entity_key: [0x00, 0x00, 0x02],
244 entity_kind: EntityKind::READER_WITH_KEY_BUILT_IN,
245 };
246 pub const SEDP_BUILTIN_PUBLICATIONS_WRITER: Self = Self {
247 entity_key: [0x00, 0x00, 0x03],
248 entity_kind: EntityKind::WRITER_WITH_KEY_BUILT_IN,
249 };
250 pub const SEDP_BUILTIN_PUBLICATIONS_READER: Self = Self {
251 entity_key: [0x00, 0x00, 0x03],
252 entity_kind: EntityKind::READER_WITH_KEY_BUILT_IN,
253 };
254 pub const SEDP_BUILTIN_SUBSCRIPTIONS_WRITER: Self = Self {
255 entity_key: [0x00, 0x00, 0x04],
256 entity_kind: EntityKind::WRITER_WITH_KEY_BUILT_IN,
257 };
258 pub const SEDP_BUILTIN_SUBSCRIPTIONS_READER: Self = Self {
259 entity_key: [0x00, 0x00, 0x04],
260 entity_kind: EntityKind::READER_WITH_KEY_BUILT_IN,
261 };
262 pub const SPDP_BUILTIN_PARTICIPANT_WRITER: Self = Self {
263 entity_key: [0x00, 0x01, 0x00],
264 entity_kind: EntityKind::WRITER_WITH_KEY_BUILT_IN,
265 };
266 pub const SPDP_BUILTIN_PARTICIPANT_READER: Self = Self {
267 entity_key: [0x00, 0x01, 0x00],
268 entity_kind: EntityKind::READER_WITH_KEY_BUILT_IN,
269 };
270 pub const P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER: Self = Self {
271 entity_key: [0x00, 0x02, 0x00],
272 entity_kind: EntityKind::WRITER_WITH_KEY_BUILT_IN,
273 };
274 pub const P2P_BUILTIN_PARTICIPANT_MESSAGE_READER: Self = Self {
275 entity_key: [0x00, 0x02, 0x00],
276 entity_kind: EntityKind::READER_WITH_KEY_BUILT_IN,
277 };
278
279 pub const SEDP_BUILTIN_PUBLICATIONS_SECURE_WRITER: Self = Self {
284 entity_key: [0xff, 0x00, 0x03],
285 entity_kind: EntityKind::WRITER_WITH_KEY_BUILT_IN, };
287 pub const SEDP_BUILTIN_PUBLICATIONS_SECURE_READER: Self = Self {
288 entity_key: [0xff, 0x00, 0x03],
289 entity_kind: EntityKind::READER_WITH_KEY_BUILT_IN,
290 };
291 pub const SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_WRITER: Self = Self {
292 entity_key: [0xff, 0x00, 0x04],
293 entity_kind: EntityKind::WRITER_WITH_KEY_BUILT_IN,
294 };
295 pub const SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_READER: Self = Self {
296 entity_key: [0xff, 0x00, 0x04],
297 entity_kind: EntityKind::READER_WITH_KEY_BUILT_IN,
298 };
299 pub const P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_WRITER: Self = Self {
300 entity_key: [0xff, 0x02, 0x00],
301 entity_kind: EntityKind::WRITER_WITH_KEY_BUILT_IN,
302 };
303 pub const P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_READER: Self = Self {
304 entity_key: [0xff, 0x02, 0x00],
305 entity_kind: EntityKind::READER_WITH_KEY_BUILT_IN,
306 };
307 pub const P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER: Self = Self {
308 entity_key: [0x00, 0x02, 0x01],
309 entity_kind: EntityKind::WRITER_NO_KEY_BUILT_IN, };
311 pub const P2P_BUILTIN_PARTICIPANT_STATELESS_READER: Self = Self {
312 entity_key: [0x00, 0x02, 0x01],
313 entity_kind: EntityKind::READER_NO_KEY_BUILT_IN, };
315 pub const P2P_BUILTIN_PARTICIPANT_VOLATILE_SECURE_WRITER: Self = Self {
316 entity_key: [0xff, 0x02, 0x02],
317 entity_kind: EntityKind::WRITER_NO_KEY_BUILT_IN,
318 };
319 pub const P2P_BUILTIN_PARTICIPANT_VOLATILE_SECURE_READER: Self = Self {
320 entity_key: [0xff, 0x02, 0x02],
321 entity_kind: EntityKind::READER_NO_KEY_BUILT_IN,
322 };
323 pub const SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_WRITER: Self = Self {
324 entity_key: [0xff, 0x01, 0x01],
325 entity_kind: EntityKind::WRITER_WITH_KEY_BUILT_IN,
326 };
327 pub const SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_READER: Self = Self {
328 entity_key: [0xff, 0x01, 0x01],
329 entity_kind: EntityKind::READER_WITH_KEY_BUILT_IN,
330 };
331
332 pub const MIN: Self = Self {
333 entity_key: [0x00; 3],
334 entity_kind: EntityKind::MIN,
335 };
336 pub const MAX: Self = Self {
337 entity_key: [0xFF, 0xFF, 0xFF],
338 entity_kind: EntityKind::MAX,
339 };
340
341 pub fn new(entity_key: [u8; 3], entity_kind: EntityKind) -> Self {
342 Self {
343 entity_key,
344 entity_kind,
345 }
346 }
347
348 #[cfg(test)]
349 pub(crate) fn create_custom_entity_id(entity_key: [u8; 3], entity_kind: EntityKind) -> Self {
350 Self::new(entity_key, entity_kind)
351 }
352
353 fn as_usize(self) -> usize {
354 let u1 = u32::from(self.entity_key[0]);
357 let u2 = u32::from(self.entity_key[1]);
358 let u3 = u32::from(self.entity_key[2]);
359 let u4 = u32::from(self.entity_kind.0);
360
361 ((u1 << 24) | (u2 << 16) | (u3 << 8) | u4) as usize
365 }
366
367 fn from_usize(number: usize) -> Self {
369 let u4 = (number & 0xFF) as u8;
370 let u3 = ((number >> 8) & 0xFF) as u8;
371 let u2 = ((number >> 16) & 0xFF) as u8;
372 let u1 = ((number >> 24) & 0xFF) as u8;
373
374 let result = Self {
375 entity_key: [u1, u2, u3],
376 entity_kind: EntityKind::from(u4),
377 };
378
379 let kind_kind = u4 & (0xC0 | 0x10);
381 if kind_kind == 0xC0 || kind_kind == 0x00 {
382 } else {
384 warn!("EntityId::from_usize tried to decode 0x{number:x?}");
385 }
386
387 result
388 }
389
390 pub fn as_token(self) -> Token {
391 let u = self.as_usize();
392 assert_eq!(u & !0x20, u); Token(u)
394 }
395
396 pub fn as_alt_token(self) -> Token {
397 Token(self.as_usize() | 0x20) }
399
400 pub fn from_token(t: Token) -> TokenDecode {
401 match (t.0 & 0xF0) as u8 {
402 0x00 | 0xC0 => TokenDecode::Entity(Self::from_usize(t.0)),
403 0x20 | 0xE0 => TokenDecode::AltEntity(Self::from_usize(t.0 & !0x20)),
404 0x40 | 0x50 | 0x60 | 0x70 | 0x80 => TokenDecode::FixedToken(t),
405 _other => {
406 warn!("EntityId::from_token tried to decode 0x{:x?}", t.0);
407 TokenDecode::FixedToken(t)
408 }
409 }
410 }
411
412 pub fn kind(self) -> EntityKind {
413 self.entity_kind
414 }
415
416 pub fn set_kind(&mut self, entity_kind: EntityKind) {
417 self.entity_kind = entity_kind;
418 }
419
420 pub fn to_slice(self) -> [u8; 4] {
421 let mut slice = [0; 4];
422 slice[0] = self.entity_key[0];
423 slice[1] = self.entity_key[1];
424 slice[2] = self.entity_key[2];
425 slice[3] = self.entity_kind.0;
426
427 slice
428 }
429
430 pub fn from_slice(bytes: [u8; 4]) -> Self {
431 Self {
432 entity_key: [bytes[0], bytes[1], bytes[2]],
433 entity_kind: EntityKind::from(bytes[3]),
434 }
435 }
436}
437
438impl Default for EntityId {
439 fn default() -> Self {
440 Self::UNKNOWN
441 }
442}
443
444impl fmt::Debug for EntityId {
445 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
446 match *self {
447 Self::UNKNOWN => f.write_str("EntityId::UNKNOWN"),
448 Self::PARTICIPANT => f.write_str("EntityId::PARTICIPANT"),
449 Self::SEDP_BUILTIN_TOPIC_WRITER => f.write_str("EntityId::SEDP_BUILTIN_TOPIC_WRITER"),
450 Self::SEDP_BUILTIN_TOPIC_READER => f.write_str("EntityId::SEDP_BUILTIN_TOPIC_READER"),
451 Self::SEDP_BUILTIN_PUBLICATIONS_WRITER => {
452 f.write_str("EntityId::SEDP_BUILTIN_PUBLICATIONS_WRITER")
453 }
454 Self::SEDP_BUILTIN_PUBLICATIONS_READER => {
455 f.write_str("EntityId::SEDP_BUILTIN_PUBLICATIONS_READER")
456 }
457 Self::SEDP_BUILTIN_SUBSCRIPTIONS_WRITER => {
458 f.write_str("EntityId::SEDP_BUILTIN_SUBSCRIPTIONS_WRITER")
459 }
460 Self::SEDP_BUILTIN_SUBSCRIPTIONS_READER => {
461 f.write_str("EntityId::SEDP_BUILTIN_SUBSCRIPTIONS_READER")
462 }
463 Self::SPDP_BUILTIN_PARTICIPANT_WRITER => {
464 f.write_str("EntityId::SPDP_BUILTIN_PARTICIPANT_WRITER")
465 }
466 Self::SPDP_BUILTIN_PARTICIPANT_READER => {
467 f.write_str("EntityId::SPDP_BUILTIN_PARTICIPANT_READER")
468 }
469 Self::P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER => {
470 f.write_str("EntityId::P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER")
471 }
472 Self::P2P_BUILTIN_PARTICIPANT_MESSAGE_READER => {
473 f.write_str("EntityId::P2P_BUILTIN_PARTICIPANT_MESSAGE_READER")
474 }
475 _ => {
478 f.write_str("EntityId {")?;
479 self.entity_key.fmt(f)?;
480 f.write_str(" ")?;
481 self.entity_kind.fmt(f)?;
482 f.write_str("}")
483 }
484 }
485 }
486}
487
488impl<'a, C: Context> Readable<'a, C> for EntityId {
489 #[inline]
490 fn minimum_bytes_needed() -> usize {
491 4
492 }
493
494 #[inline]
495 fn read_from<R: Reader<'a, C>>(reader: &mut R) -> Result<Self, C::Error> {
496 let entity_key = [reader.read_u8()?, reader.read_u8()?, reader.read_u8()?];
497 let entity_kind = EntityKind(reader.read_u8()?);
498 Ok(Self {
499 entity_key,
500 entity_kind,
501 })
502 }
503}
504
505impl<C: Context> Writable<C> for EntityId {
506 #[inline]
507 fn write_to<T: ?Sized + Writer<C>>(&self, writer: &mut T) -> Result<(), C::Error> {
508 for elem in &self.entity_key {
509 writer.write_u8(*elem)?;
510 }
511 writer.write_u8(self.entity_kind.0)
512 }
513}
514
515#[derive(
531 Copy,
532 Clone,
533 Default,
534 PartialOrd,
535 PartialEq,
536 Ord,
537 Eq,
538 Readable,
539 Writable,
540 Hash,
541 Serialize,
542 Deserialize,
543 CdrEncodingSize,
544)]
545pub struct GUID {
546 pub prefix: GuidPrefix,
550 pub entity_id: EntityId,
551}
552
553impl GUID {
554 pub const GUID_UNKNOWN: Self = Self {
555 prefix: GuidPrefix::UNKNOWN,
556 entity_id: EntityId::UNKNOWN,
557 };
558
559 pub fn new(prefix: GuidPrefix, entity_id: EntityId) -> Self {
561 Self::new_with_prefix_and_id(prefix, entity_id)
562 }
563
564 pub fn from_bytes(bytes: [u8; 16]) -> Self {
565 let mut prefix = GuidPrefix { bytes: [0; 12] };
566 prefix.bytes.as_mut_slice().copy_from_slice(&bytes[0..12]);
567
568 let mut eid_bytes = [0; 4];
569 eid_bytes.as_mut_slice().copy_from_slice(&bytes[12..16]);
570
571 Self {
572 prefix,
573 entity_id: EntityId::from_slice(eid_bytes),
574 }
575 }
576
577 pub fn new_participant_guid() -> Self {
579 Self {
580 prefix: GuidPrefix::random_for_this_participant(),
581 entity_id: EntityId::PARTICIPANT,
582 }
583 }
584
585 pub fn dummy_test_guid(entity_kind: EntityKind) -> Self {
586 Self {
587 prefix: GuidPrefix::new(b"FakeTestGUID"),
588 entity_id: EntityId {
589 entity_key: [1, 2, 3],
590 entity_kind,
591 },
592 }
593 }
594
595 #[must_use]
597 pub fn from_prefix(self, entity_id: EntityId) -> Self {
598 Self {
599 prefix: self.prefix,
600 entity_id,
601 }
602 }
603
604 pub fn new_with_prefix_and_id(prefix: GuidPrefix, entity_id: EntityId) -> Self {
606 Self { prefix, entity_id }
607 }
608
609 pub fn as_usize(&self) -> usize {
610 self.entity_id.as_usize()
611 }
612
613 pub fn to_bytes(&self) -> [u8; 16] {
614 let mut bytes = [0; 16];
615 bytes.as_mut_slice()[0..12].copy_from_slice(self.prefix.as_ref());
616 bytes.as_mut_slice()[12..16].copy_from_slice(&self.entity_id.to_slice());
617 bytes
618 }
619}
620
621impl Key for GUID {}
622
623impl fmt::Debug for GUID {
624 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
625 f.write_fmt(format_args!(
626 "GUID {{{:?} {:?}}}",
627 self.prefix, self.entity_id
628 ))
629 }
630}
631
632#[cfg(test)]
633mod tests {
634 use speedy::Endianness;
635 use log::info;
636 use byteorder::BigEndian;
637
638 use super::*;
639
640 #[test]
641 fn serde_test() {
642 use crate::serialization::{from_bytes, to_vec};
643
644 let test_bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
645 let test_guid = GUID::from_bytes(test_bytes);
646 let ser = to_vec::<GUID, BigEndian>(&test_guid).unwrap();
647 assert_eq!(test_bytes.to_vec(), ser);
648
649 let (and_back, _byte_count) = from_bytes::<GUID, BigEndian>(&ser).unwrap();
650 assert_eq!(test_guid, and_back);
651 }
652
653 #[test]
670 fn keyhash_test() {
671 let test_bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
672 let test_guid = GUID::from_bytes(test_bytes);
673 let key_hash = test_guid.hash_key(false); assert_eq!(key_hash.to_vec(), test_bytes.to_vec());
675 }
676
677 #[test]
678 fn convert_entity_id_to_token_and_back() {
679 let e = EntityId::SPDP_BUILTIN_PARTICIPANT_WRITER;
680 let _t = Token(e.as_usize());
681 info!("{:?}", e.as_usize());
682 let entity = EntityId::from_usize(e.as_usize());
683 assert_eq!(e, entity);
684
685 let e2 = EntityId::P2P_BUILTIN_PARTICIPANT_MESSAGE_READER;
686 let entity2 = EntityId::from_usize(e2.as_usize());
687 assert_eq!(e2, entity2);
688
689 let e3 = EntityId::SEDP_BUILTIN_TOPIC_WRITER;
690 let entity3 = EntityId::from_usize(e3.as_usize());
691 assert_eq!(e3, entity3);
692
693 let e4 = EntityId::SEDP_BUILTIN_TOPIC_WRITER;
694 let entity4 = EntityId::from_usize(e4.as_usize());
695 assert_eq!(e4, entity4);
696
697 let e5 = EntityId::UNKNOWN;
698 let entity5 = EntityId::from_usize(e5.as_usize());
699 assert_eq!(e5, entity5);
700
701 let e6 = EntityId::create_custom_entity_id([12u8, 255u8, 0u8], EntityKind(254u8));
702 let entity6 = EntityId::from_usize(e6.as_usize());
703 assert_eq!(e6, entity6);
704 }
705
706 #[test]
707 fn minimum_bytes_needed() {
708 assert_eq!(
709 12,
710 <GuidPrefix as Readable<Endianness>>::minimum_bytes_needed()
711 );
712 }
713
714 serialization_test!( type = GuidPrefix,
715 {
716 guid_prefix_unknown,
717 GuidPrefix::UNKNOWN,
718 le = [0x00; 12],
719 be = [0x00; 12]
720 },
721 {
722 guid_prefix_default,
723 GuidPrefix::default(),
724 le = [0x00; 12],
725 be = [0x00; 12]
726 },
727 {
728 guid_prefix_endianness_insensitive,
729 GuidPrefix {
730 bytes: [0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
731 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB]
732 },
733 le = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
734 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB],
735 be = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
736 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB]
737 });
738
739 serialization_test!( type = EntityId,
740 {
741 entity_unknown,
742 EntityId::UNKNOWN,
743 le = [0x00, 0x00, 0x00, 0x00],
744 be = [0x00, 0x00, 0x00, 0x00]
745 },
746 {
747 entity_default,
748 EntityId::default(),
749 le = [0x00, 0x00, 0x00, 0x00],
750 be = [0x00, 0x00, 0x00, 0x00]
751 },
752 {
753 entity_participant,
754 EntityId::PARTICIPANT,
755 le = [0x00, 0x00, 0x01, 0xC1],
756 be = [0x00, 0x00, 0x01, 0xC1]
757 },
758 {
759 entity_sedp_builtin_topic_writer,
760 EntityId::SEDP_BUILTIN_TOPIC_WRITER,
761 le = [0x00, 0x00, 0x02, 0xC2],
762 be = [0x00, 0x00, 0x02, 0xC2]
763 },
764 {
765 entity_sedp_builtin_topic_reader,
766 EntityId::SEDP_BUILTIN_TOPIC_READER,
767 le = [0x00, 0x00, 0x02, 0xC7],
768 be = [0x00, 0x00, 0x02, 0xC7]
769 },
770 {
771 entity_sedp_builtin_publications_writer,
772 EntityId::SEDP_BUILTIN_PUBLICATIONS_WRITER,
773 le = [0x00, 0x00, 0x03, 0xC2],
774 be = [0x00, 0x00, 0x03, 0xC2]
775 },
776 {
777 entity_sedp_builtin_publications_reader,
778 EntityId::SEDP_BUILTIN_PUBLICATIONS_READER,
779 le = [0x00, 0x00, 0x03, 0xC7],
780 be = [0x00, 0x00, 0x03, 0xC7]
781 },
782 {
783 entity_sedp_builtin_subscriptions_writer,
784 EntityId::SEDP_BUILTIN_SUBSCRIPTIONS_WRITER,
785 le = [0x00, 0x00, 0x04, 0xC2],
786 be = [0x00, 0x00, 0x04, 0xC2]
787 },
788 {
789 entity_sedp_builtin_subscriptions_reader,
790 EntityId::SEDP_BUILTIN_SUBSCRIPTIONS_READER,
791 le = [0x00, 0x00, 0x04, 0xC7],
792 be = [0x00, 0x00, 0x04, 0xC7]
793 },
794 {
795 entity_spdp_builtin_participant_writer,
796 EntityId::SPDP_BUILTIN_PARTICIPANT_WRITER,
797 le = [0x00, 0x01, 0x00, 0xC2],
798 be = [0x00, 0x01, 0x00, 0xC2]
799 },
800 {
801 entity_spdp_builtin_participant_reader,
802 EntityId::SPDP_BUILTIN_PARTICIPANT_READER,
803 le = [0x00, 0x01, 0x00, 0xC7],
804 be = [0x00, 0x01, 0x00, 0xC7]
805 },
806 {
807 entity_p2p_builtin_participant_message_writer,
808 EntityId::P2P_BUILTIN_PARTICIPANT_MESSAGE_WRITER,
809 le = [0x00, 0x02, 0x00, 0xC2],
810 be = [0x00, 0x02, 0x00, 0xC2]
811 },
812 {
813 entity_p2p_builtin_participant_message_reader,
814 EntityId::P2P_BUILTIN_PARTICIPANT_MESSAGE_READER,
815 le = [0x00, 0x02, 0x00, 0xC7],
816 be = [0x00, 0x02, 0x00, 0xC7]
817 }
818 );
819
820 #[test]
821 fn guid_unknown_is_a_combination_of_unknown_members() {
822 assert_eq!(
823 GUID {
824 entity_id: EntityId::UNKNOWN,
825 prefix: GuidPrefix::UNKNOWN
826 },
827 GUID::GUID_UNKNOWN
828 );
829 }
830
831 serialization_test!( type = GUID,
832 {
833 guid_unknown,
834 GUID::GUID_UNKNOWN,
835 le = [0x00; 16],
836 be = [0x00; 16]
837 },
838 {
839 guid_default,
840 GUID::default(),
841 le = [0x00; 16],
842 be = [0x00; 16]
843 },
844 {
845 guid_entity_id_on_the_last_position,
846 GUID {
847 entity_id: EntityId::PARTICIPANT,
848 ..GUID::default()
849 },
850 le = [0x00, 0x00, 0x00, 0x00,
851 0x00, 0x00, 0x00, 0x00,
852 0x00, 0x00, 0x00, 0x00,
853 0x00, 0x00, 0x01, 0xC1],
854 be = [0x00, 0x00, 0x00, 0x00,
855 0x00, 0x00, 0x00, 0x00,
856 0x00, 0x00, 0x00, 0x00,
857 0x00, 0x00, 0x01, 0xC1]
858 }
859 );
860}