Skip to main content

datex_core/global/protocol_structures/
block_header.rs

1use super::serializable::Serializable;
2use crate::values::core_values::endpoint::Endpoint;
3use binrw::{BinRead, BinWrite};
4
5use modular_bitfield::{Specifier, bitfield, prelude::B43};
6use strum_macros::Display;
7
8// 4 bit
9#[derive(
10    Debug,
11    Display,
12    PartialEq,
13    Clone,
14    Copy,
15    Default,
16    Specifier,
17    serde::Serialize,
18    serde::Deserialize,
19)]
20#[bits = 4]
21pub enum BlockType {
22    #[default]
23    Request = 0,
24    Response = 1,
25    Hello = 2,
26    Trace = 3,
27    TraceBack = 4,
28}
29
30impl BlockType {
31    pub fn is_response(&self) -> bool {
32        core::matches!(self, BlockType::Response | BlockType::TraceBack)
33    }
34}
35
36// 21 bit + 43 bit = 64 bit
37/// has_side_effects: If set, the block can have side effects that change external state. Default is true
38/// has_only_data: If set, the block does only contain data and no executable instructions. Default is false
39#[bitfield]
40#[derive(BinWrite, BinRead, Clone, Copy, Debug, PartialEq)]
41#[bw(map = |&x| Self::into_bytes(x))]
42#[br(map = Self::from_bytes)]
43#[brw(little)]
44pub struct FlagsAndTimestamp {
45    /// The base type of the block, e.g. request, response, hello, ...
46    pub block_type: BlockType,
47    /// Indicates if the block can have side effects that change external state.
48    pub has_side_effects: bool,
49    /// Indicates if the block only contains data and no executable instructions.
50    pub has_only_data: bool,
51    /// Indicates if the block is the last block of a section.
52    pub is_end_of_section: bool,
53    /// Indicates if the block is the last block of a context. Any cached state of the context can be discarded after this block.
54    pub is_end_of_context: bool,
55    /// Indicates if the block has a lifetime field. If set, the block will be discarded after the lifetime expires.
56    pub has_lifetime: bool,
57    /// Indicates if the block has a represented_by field. If set, the block is represented by another endpoint.
58    pub has_represented_by: bool,
59    /// Indicates if the block has an initialization vector (IV) for encryption.
60    pub has_iv: bool,
61    /// Indicates if the block is compressed. If set, the block body is compressed and needs to be decompressed before processing.
62    pub is_compressed: bool,
63    /// Indicates if the block has a signature in the last subblock. If set, the block does not have a signature in the header, but in the last subblock of the current section.
64    pub is_signature_in_last_subblock: bool,
65    /// Indicates if the block has a skip_after_block field.
66    pub has_skip_after_block: bool,
67    /// Indicates if the block is a debug block. Debug blocks should be hidden e.g. in the Inspector
68    pub is_debug_block: bool,
69    #[allow(unused)]
70    unused_2: bool,
71    #[allow(unused)]
72    unused_3: bool,
73    #[allow(unused)]
74    unused_4: bool,
75    #[allow(unused)]
76    unused_5: bool,
77    #[allow(unused)]
78    unused_6: bool,
79    #[allow(unused)]
80    unused_7: bool,
81
82    pub creation_timestamp: B43,
83}
84
85mod flags_and_timestamp_serde {
86    use super::*;
87    use serde::{Deserialize, Deserializer, Serialize, Serializer};
88
89    #[derive(Serialize, Deserialize)]
90    struct FlagsHelper {
91        block_type: BlockType,
92        has_side_effects: bool,
93        has_only_data: bool,
94        is_end_of_section: bool,
95        is_end_of_context: bool,
96        has_lifetime: bool,
97        has_represented_by: bool,
98        has_iv: bool,
99        is_compressed: bool,
100        is_signature_in_last_subblock: bool,
101        creation_timestamp: u64,
102    }
103
104    impl Serialize for FlagsAndTimestamp {
105        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
106        where
107            S: Serializer,
108        {
109            let helper = FlagsHelper {
110                block_type: self.block_type(),
111                has_side_effects: self.has_side_effects(),
112                has_only_data: self.has_only_data(),
113                is_end_of_section: self.is_end_of_section(),
114                is_end_of_context: self.is_end_of_context(),
115                has_lifetime: self.has_lifetime(),
116                has_represented_by: self.has_represented_by(),
117                has_iv: self.has_iv(),
118                is_compressed: self.is_compressed(),
119                is_signature_in_last_subblock: self
120                    .is_signature_in_last_subblock(),
121                creation_timestamp: self.creation_timestamp(),
122            };
123            helper.serialize(serializer)
124        }
125    }
126
127    impl<'de> Deserialize<'de> for FlagsAndTimestamp {
128        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
129        where
130            D: Deserializer<'de>,
131        {
132            let helper = FlagsHelper::deserialize(deserializer)?;
133            Ok(FlagsAndTimestamp::new()
134                .with_block_type(helper.block_type)
135                .with_has_side_effects(helper.has_side_effects)
136                .with_has_only_data(helper.has_only_data)
137                .with_is_end_of_section(helper.is_end_of_section)
138                .with_is_end_of_context(helper.is_end_of_context)
139                .with_has_lifetime(helper.has_lifetime)
140                .with_has_represented_by(helper.has_represented_by)
141                .with_has_iv(helper.has_iv)
142                .with_is_compressed(helper.is_compressed)
143                .with_is_signature_in_last_subblock(
144                    helper.is_signature_in_last_subblock,
145                )
146                .with_creation_timestamp(helper.creation_timestamp))
147        }
148    }
149}
150
151impl Default for FlagsAndTimestamp {
152    fn default() -> Self {
153        FlagsAndTimestamp::new()
154            .with_block_type(BlockType::Request)
155            .with_has_side_effects(true)
156            .with_has_only_data(false)
157            .with_is_end_of_section(true)
158            .with_is_end_of_context(true)
159            .with_has_lifetime(false)
160            .with_has_represented_by(false)
161            .with_has_iv(false)
162            .with_is_compressed(false)
163            .with_is_signature_in_last_subblock(false)
164    }
165}
166
167// min: 16 byte
168// max 8 + 8 byte + 4 byte + 21 byte + 16 byte = 57 byte
169#[derive(
170    serde::Serialize,
171    serde::Deserialize,
172    Debug,
173    Clone,
174    Default,
175    BinWrite,
176    BinRead,
177    PartialEq,
178)]
179#[brw(little)]
180pub struct BlockHeader {
181    /// A unique id that defines the context in which this block lives
182    /// A context has a persistent state that can e.g. contain DATEX variables
183    pub context_id: u32,
184    /// A section is a collection of multiple sequential blocks inside the same context
185    /// (each with an incrementing block number)
186    /// When a new section starts, the block number is not reset but continues to increment
187    pub section_index: u16,
188    /// A unique number that identifies a block inside a block context
189    /// The context_id combined with the block_number define a unique block from a specific endpoint
190    /// the block id (endpoint, context_id, block_number) defines a globally unique block
191    /// Note: blocks ids are not completely unique, when the block_number or section_index overflows,
192    /// it starts from 0 again, leading to duplicate block ids after a while
193    pub block_number: u16,
194
195    pub flags_and_timestamp: FlagsAndTimestamp,
196
197    /// When set, this indicates that all blocks between [skip_after_block] and the current [block_number]
198    /// should be skipped and don't need to be awaited
199    /// TODO: improve this
200    #[brw(if(flags_and_timestamp.has_skip_after_block()))]
201    pub skip_after_block: Option<u16>,
202
203    #[brw(if(flags_and_timestamp.has_lifetime()))]
204    pub lifetime: Option<u32>,
205
206    #[brw(if(flags_and_timestamp.has_represented_by()))]
207    pub represented_by: Option<Endpoint>,
208
209    #[brw(if(flags_and_timestamp.has_iv()))]
210    pub iv: Option<[u8; 16]>,
211}
212
213impl Serializable for BlockHeader {}