zenoh_protocol/zenoh/
del.rs

1//
2// Copyright (c) 2022 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14use alloc::vec::Vec;
15
16use uhlc::Timestamp;
17
18use crate::common::ZExtUnknown;
19
20/// # Put message
21///
22/// ```text
23/// Flags:
24/// - T: Timestamp      If T==1 then the timestamp if present
25/// - X: Reserved
26/// - Z: Extension      If Z==1 then at least one extension is present
27///
28///   7 6 5 4 3 2 1 0
29///  +-+-+-+-+-+-+-+-+
30///  |Z|X|T|   DEL   |
31///  +-+-+-+---------+
32///  ~ ts: <u8;z16>  ~  if T==1
33///  +---------------+
34///  ~  [del_exts]   ~  if Z==1
35///  +---------------+
36/// ```
37pub mod flag {
38    pub const T: u8 = 1 << 5; // 0x20 Timestamp     if T==0 then the timestamp if present
39                              // pub const X: u8 = 1 << 6; // 0x40 Reserved
40    pub const Z: u8 = 1 << 7; // 0x80 Extensions    if Z==1 then an extension will follow
41}
42
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct Del {
45    pub timestamp: Option<Timestamp>,
46    pub ext_sinfo: Option<ext::SourceInfoType>,
47    pub ext_attachment: Option<ext::AttachmentType>,
48    pub ext_unknown: Vec<ZExtUnknown>,
49}
50
51pub mod ext {
52    use crate::{common::ZExtZBuf, zextzbuf};
53
54    /// # SourceInfo extension
55    /// Used to carry additional information about the source of data
56    pub type SourceInfo = zextzbuf!(0x1, false);
57    pub type SourceInfoType = crate::zenoh::ext::SourceInfoType<{ SourceInfo::ID }>;
58
59    /// # User attachment
60    pub type Attachment = zextzbuf!(0x2, false);
61    pub type AttachmentType = crate::zenoh::ext::AttachmentType<{ Attachment::ID }>;
62}
63
64impl Del {
65    #[cfg(feature = "test")]
66    pub fn rand() -> Self {
67        use rand::Rng;
68
69        use crate::{common::iext, core::ZenohIdProto};
70        let mut rng = rand::thread_rng();
71
72        let timestamp = rng.gen_bool(0.5).then_some({
73            let time = uhlc::NTP64(rng.gen());
74            let id = uhlc::ID::try_from(ZenohIdProto::rand().to_le_bytes()).unwrap();
75            Timestamp::new(time, id)
76        });
77        let ext_sinfo = rng.gen_bool(0.5).then_some(ext::SourceInfoType::rand());
78        let ext_attachment = rng.gen_bool(0.5).then_some(ext::AttachmentType::rand());
79        let mut ext_unknown = Vec::new();
80        for _ in 0..rng.gen_range(0..4) {
81            ext_unknown.push(ZExtUnknown::rand2(
82                iext::mid(ext::Attachment::ID) + 1,
83                false,
84            ));
85        }
86
87        Self {
88            timestamp,
89            ext_sinfo,
90            ext_attachment,
91            ext_unknown,
92        }
93    }
94}