zenoh_protocol/zenoh/
put.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;
17use zenoh_buffers::ZBuf;
18
19use crate::{common::ZExtUnknown, core::Encoding};
20
21/// # Put message
22///
23/// ```text
24/// Flags:
25/// - T: Timestamp      If T==1 then the timestamp if present
26/// - E: Encoding       If E==1 then the encoding is present
27/// - Z: Extension      If Z==1 then at least one extension is present
28///
29///   7 6 5 4 3 2 1 0
30///  +-+-+-+-+-+-+-+-+
31///  |Z|E|T|   PUT   |
32///  +-+-+-+---------+
33///  ~ ts: <u8;z16>  ~  if T==1
34///  +---------------+
35///  ~   encoding    ~  if E==1
36///  +---------------+
37///  ~  [put_exts]   ~  if Z==1
38///  +---------------+
39///  ~ pl: <u8;z32>  ~  -- Payload
40///  +---------------+
41/// ```
42pub mod flag {
43    pub const T: u8 = 1 << 5; // 0x20 Timestamp     if T==0 then the timestamp if present
44    pub const E: u8 = 1 << 6; // 0x40 Encoding      if E==1 then the encoding is present
45    pub const Z: u8 = 1 << 7; // 0x80 Extensions    if Z==1 then an extension will follow
46}
47
48#[derive(Debug, Default, Clone, PartialEq, Eq)]
49pub struct Put {
50    pub timestamp: Option<Timestamp>,
51    pub encoding: Encoding,
52    pub ext_sinfo: Option<ext::SourceInfoType>,
53    pub ext_attachment: Option<ext::AttachmentType>,
54    #[cfg(feature = "shared-memory")]
55    pub ext_shm: Option<ext::ShmType>,
56    pub ext_unknown: Vec<ZExtUnknown>,
57    pub payload: ZBuf,
58}
59
60pub mod ext {
61    #[cfg(feature = "shared-memory")]
62    use crate::{common::ZExtUnit, zextunit};
63    use crate::{common::ZExtZBuf, zextzbuf};
64
65    /// # SourceInfo extension
66    /// Used to carry additional information about the source of data
67    pub type SourceInfo = zextzbuf!(0x1, false);
68    pub type SourceInfoType = crate::zenoh::ext::SourceInfoType<{ SourceInfo::ID }>;
69
70    /// # Shared Memory extension
71    /// Used to carry additional information about the shared-memory layout of data
72    #[cfg(feature = "shared-memory")]
73    pub type Shm = zextunit!(0x2, true);
74    #[cfg(feature = "shared-memory")]
75    pub type ShmType = crate::zenoh::ext::ShmType<{ Shm::ID }>;
76
77    /// # User attachment
78    pub type Attachment = zextzbuf!(0x3, false);
79    pub type AttachmentType = crate::zenoh::ext::AttachmentType<{ Attachment::ID }>;
80}
81
82impl Put {
83    #[cfg(feature = "test")]
84    #[doc(hidden)]
85    pub fn rand() -> Self {
86        use rand::Rng;
87
88        use crate::{common::iext, core::ZenohIdProto};
89        let mut rng = rand::thread_rng();
90
91        let timestamp = rng.gen_bool(0.5).then_some({
92            let time = uhlc::NTP64(rng.gen());
93            let id = uhlc::ID::try_from(ZenohIdProto::rand().to_le_bytes()).unwrap();
94            Timestamp::new(time, id)
95        });
96        let encoding = Encoding::rand();
97        let ext_sinfo = rng.gen_bool(0.5).then_some(ext::SourceInfoType::rand());
98        #[cfg(feature = "shared-memory")]
99        let ext_shm = rng.gen_bool(0.5).then_some(ext::ShmType::rand());
100        let ext_attachment = rng.gen_bool(0.5).then_some(ext::AttachmentType::rand());
101        let mut ext_unknown = Vec::new();
102        for _ in 0..rng.gen_range(0..4) {
103            ext_unknown.push(ZExtUnknown::rand2(
104                iext::mid(ext::Attachment::ID) + 1,
105                false,
106            ));
107        }
108        let payload = ZBuf::rand(rng.gen_range(1..=64));
109
110        Self {
111            timestamp,
112            encoding,
113            ext_sinfo,
114            #[cfg(feature = "shared-memory")]
115            ext_shm,
116            ext_attachment,
117            ext_unknown,
118            payload,
119        }
120    }
121}