1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! Foundation Layer. Handles Publication, Config, etc.

use crate::bytes::ToFromBytesEndian;
use crate::foundation::element::ElementsComposition;
use crate::mesh::CompanyID;
use crate::upper::AppPayload;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::convert::TryFrom;

pub mod element;
pub mod health;
pub mod model;
pub mod publication;
pub mod state;
// LITTLE ENDIAN

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
pub struct StatusCodeConversationError(());
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
#[repr(u8)]
pub enum StatusCode {
    Ok = 0x00,
}
impl StatusCode {
    pub const fn byte_len() -> usize {
        1
    }
}
impl From<StatusCode> for u8 {
    fn from(code: StatusCode) -> Self {
        code as u8
    }
}
impl TryFrom<u8> for StatusCode {
    type Error = StatusCodeConversationError;

    fn try_from(_value: u8) -> Result<Self, Self::Error> {
        unimplemented!()
    }
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Hash, Debug)]
pub struct FoundationStateError(());

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub struct ProductID(pub u16);
impl ProductID {
    pub const fn byte_len() -> usize {
        2
    }
}

impl ToFromBytesEndian for ProductID {
    type AsBytesType = [u8; 2];

    #[must_use]
    fn to_bytes_le(&self) -> Self::AsBytesType {
        (self.0).to_bytes_le()
    }

    #[must_use]
    fn to_bytes_be(&self) -> Self::AsBytesType {
        (self.0).to_bytes_be()
    }

    #[must_use]
    fn from_bytes_le(bytes: &[u8]) -> Option<Self> {
        Some(ProductID(u16::from_bytes_le(bytes)?))
    }

    #[must_use]
    fn from_bytes_be(bytes: &[u8]) -> Option<Self> {
        Some(ProductID(u16::from_bytes_be(bytes)?))
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub struct VersionID(pub u16);
impl VersionID {
    pub const fn byte_len() -> usize {
        2
    }
}
impl ToFromBytesEndian for VersionID {
    type AsBytesType = [u8; 2];

    #[must_use]
    fn to_bytes_le(&self) -> Self::AsBytesType {
        (self.0).to_bytes_le()
    }

    #[must_use]
    fn to_bytes_be(&self) -> Self::AsBytesType {
        (self.0).to_bytes_be()
    }

    #[must_use]
    fn from_bytes_le(bytes: &[u8]) -> Option<Self> {
        Some(VersionID(u16::from_bytes_le(bytes)?))
    }

    #[must_use]
    fn from_bytes_be(bytes: &[u8]) -> Option<Self> {
        Some(VersionID(u16::from_bytes_be(bytes)?))
    }
}

/// Minimum number of replay protection list entries
pub struct Crpl(u16);
impl Crpl {
    pub const fn byte_len() -> usize {
        2
    }
}
pub enum FeatureFlags {
    Relay = 0b0001,
    Proxy = 0b0010,
    Friend = 0b0100,
    LowPower = 0b1000,
}
impl From<FeatureFlags> for u16 {
    fn from(f: FeatureFlags) -> Self {
        f as u16
    }
}
impl ToFromBytesEndian for Features {
    type AsBytesType = [u8; 2];

    #[must_use]
    fn to_bytes_le(&self) -> Self::AsBytesType {
        (self.0).to_bytes_le()
    }

    #[must_use]
    fn to_bytes_be(&self) -> Self::AsBytesType {
        (self.0).to_bytes_be()
    }

    #[must_use]
    fn from_bytes_le(bytes: &[u8]) -> Option<Self> {
        Some(Features(u16::from_bytes_le(bytes)?))
    }

    #[must_use]
    fn from_bytes_be(bytes: &[u8]) -> Option<Self> {
        Some(Features(u16::from_bytes_be(bytes)?))
    }
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub struct Features(u16);
impl Features {
    pub const fn byte_len() -> usize {
        2
    }
    pub fn set(&mut self, feature: FeatureFlags) {
        self.0 |= u16::from(feature)
    }
    pub fn clear(&mut self, feature: FeatureFlags) {
        self.0 |= !u16::from(feature)
    }
    #[must_use]
    pub fn get(&self, feature: FeatureFlags) -> bool {
        self.0 & u16::from(feature) != 0
    }
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub struct CRPL(pub u16);
impl CRPL {
    pub const fn byte_len() -> usize {
        2
    }
}
impl ToFromBytesEndian for CRPL {
    type AsBytesType = [u8; 2];

    #[must_use]
    fn to_bytes_le(&self) -> Self::AsBytesType {
        (self.0).to_bytes_le()
    }

    #[must_use]
    fn to_bytes_be(&self) -> Self::AsBytesType {
        (self.0).to_bytes_be()
    }

    #[must_use]
    fn from_bytes_le(bytes: &[u8]) -> Option<Self> {
        Some(CRPL(u16::from_bytes_le(bytes)?))
    }

    #[must_use]
    fn from_bytes_be(bytes: &[u8]) -> Option<Self> {
        Some(CRPL(u16::from_bytes_be(bytes)?))
    }
}
#[derive(Clone, Ord, PartialOrd, PartialEq, Debug, Hash, Eq)]
pub struct CompositionDataPage0 {
    cid: CompanyID,
    pid: ProductID,
    vid: VersionID,
    crpl: CRPL,
    features: Features,
    elements: ElementsComposition,
}
impl CompositionDataPage0 {
    pub fn byte_len(&self) -> usize {
        CompanyID::byte_len()
            + ProductID::byte_len()
            + VersionID::byte_len()
            + CRPL::byte_len()
            + Features::byte_len()
            + self.elements.byte_len()
    }
    pub const fn min_byte_len() -> usize {
        CompanyID::byte_len()
            + ProductID::byte_len()
            + VersionID::byte_len()
            + CRPL::byte_len()
            + Features::byte_len()
    }
    pub fn try_unpack_from(&self, _data: &[u8]) {
        unimplemented!()
    }
    pub fn pack_into(&self, buf: &mut [u8]) {
        assert!(buf.len() >= self.byte_len());
        let buf = &mut buf[..self.byte_len()];
        buf[0..2].copy_from_slice(&self.cid.to_bytes_le());
        buf[2..4].copy_from_slice(&self.pid.to_bytes_le());
        buf[4..6].copy_from_slice(&self.vid.to_bytes_le());
        buf[6..8].copy_from_slice(&self.crpl.to_bytes_le());
        buf[8..10].copy_from_slice(&self.features.to_bytes_le());
        self.elements.pack_into(&mut buf[10..]);
    }
    pub fn as_app_payload(&self) -> AppPayload<Box<[u8]>> {
        let mut buf = Vec::with_capacity(self.byte_len()).into_boxed_slice();
        self.pack_into(buf.as_mut());
        AppPayload::new(buf)
    }
}