zenoh_protocol/network/
oam.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 crate::common::ZExtBody;
15
16pub type OamId = u16;
17
18pub mod flag {
19    pub const T: u8 = 1 << 5; // 0x20 Transport
20                              // pub const X: u8 = 1 << 6; // 0x40 Reserved
21    pub const Z: u8 = 1 << 7; // 0x80 Extensions    if Z==1 then an extension will follow
22}
23
24pub mod id {
25    use super::OamId;
26
27    pub const OAM_LINKSTATE: OamId = 0x0001;
28}
29
30/// ```text
31/// Flags:
32/// - E |: Encoding     The encoding of the extension
33/// - E/
34/// - Z: Extension      If Z==1 then at least one extension is present
35///
36///  7 6 5 4 3 2 1 0
37/// +-+-+-+-+-+-+-+-+
38/// |X|ENC|  OAM    |
39/// +-+-+-+---------+
40/// ~    id:z16     ~
41/// +---------------+
42/// %    length     % -- If ENC == Z64 || ENC == ZBuf (z32)
43/// +---------------+
44/// ~     [u8]      ~ -- If ENC == ZBuf
45/// +---------------+
46///
47/// Encoding:
48/// - 0b00: Unit
49/// - 0b01: Z64
50/// - 0b10: ZBuf
51/// - 0b11: Reserved
52/// ```
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct Oam {
55    pub id: OamId,
56    pub body: ZExtBody,
57    pub ext_qos: ext::QoSType,
58    pub ext_tstamp: Option<ext::TimestampType>,
59}
60
61pub mod ext {
62    use crate::{
63        common::{ZExtZ64, ZExtZBuf},
64        zextz64, zextzbuf,
65    };
66
67    pub type QoS = zextz64!(0x1, false);
68    pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
69
70    pub type Timestamp = zextzbuf!(0x2, false);
71    pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
72}
73
74impl Oam {
75    #[cfg(feature = "test")]
76    #[doc(hidden)]
77    pub fn rand() -> Self {
78        use rand::Rng;
79        let mut rng = rand::thread_rng();
80
81        let id: OamId = rng.gen();
82        let body = ZExtBody::rand();
83        let ext_qos = ext::QoSType::rand();
84        let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
85
86        Self {
87            id,
88            body,
89            ext_qos,
90            ext_tstamp,
91        }
92    }
93}