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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use std::ops::Range;

use enum_primitive_derive::Primitive;
use num_traits::FromPrimitive;

/// Every message starts with this header
pub const IDENT: [u8; 4] = [b'+', b'@', b'C', b'|'];
pub const MAX_ROUTE_LEN: usize = 1 << 9;

/* ---------------------------------------------------------------------------------------------- */
/*                                       RAW MESSAGE HEADER                                       */
/* ---------------------------------------------------------------------------------------------- */
///
/// # Message Delivery Protocol
///
/// - Each field is in **network byte order**.
///
/// ```plain
///
///     BYTE 0, 1, 2, 3: IDENTIFIER, always "+@C|"
///     BYTE [4, 8): b0
///         bit 29..32) message type
///             000: NOTI
///             001: REQ
///             010: REP
///             011: (reserved)
///             100: (reserved)
///             101: (reserved)
///             110: (reserved)
///             111: (reserved)
///         bit 20..29) n: length of route/method, max 512 byte allowed.
///         bit 0 ..20)
///             NOTI: (reserved)
///             REQ:
///                 bit 16..20) m: length of request id, max 16 byte allowed.
///             REP:
///                 bit 16..20) m: length of request id, max 16 byte allowed.
///                 bit 10..16) e: error code
///             
///     BYTE [8, 12): p: length of all payload length to read. Max 4GB allowed.
///
///     PAYLOAD:
///         NOTI: [0..n) route [n) 0 [n+1..p) payload
///         REQ: [0..m) request_id [m..m+n] route [m+n] 0 [n+m+1..p) payload
///         REP: [0..m] request id, [m..p) payload
///
/// ```
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct RawHead {
    /// Check if this is a valid message
    ident: [u8; 4],

    b0: u32,
    p: u32,
}

pub type RawHeadBuf = [u8; std::mem::size_of::<RawHead>()];

impl RawHead {
    fn is_le() -> bool {
        const ENDIAN_CHECK: *const i32 = &1 as *const i32;
        unsafe { *(ENDIAN_CHECK as *const u8) == 1 }
    }

    pub fn from_bytes(mut value: RawHeadBuf) -> Option<Self> {
        // Check identifier
        if value[0..4] != IDENT {
            return None;
        }

        // Rearranges frames received in network byte order to system byte order, if necessary.
        if Self::is_le() {
            value[4..8].reverse();
            value[8..12].reverse();
        }

        Some(unsafe { std::mem::transmute(value) })
    }

    pub fn to_bytes(&self) -> RawHeadBuf {
        let mut value: RawHeadBuf = unsafe { std::mem::transmute_copy(self) };

        // Rearranges frames received in network byte order to system byte order, if necessary.
        if Self::is_le() {
            value[4..8].reverse();
            value[8..12].reverse();
        }

        value
    }

    pub fn new(this: Head) -> Self {
        match this {
            Head::Noti(HNoti { route: n, all_b: p }) => Self {
                ident: IDENT,
                b0: 0 << 29 | (n as u32 & 0x1ff) << 20,
                p,
            },
            Head::Req(HReq {
                route: n,
                req_id: m,
                all_b: p,
            }) => Self {
                ident: IDENT,
                b0: 1 << 29 | (n as u32 & 0x1ff) << 20 | (m as u32 & 0xf) << 16,
                p,
            },
            Head::Rep(HRep {
                errc: e,
                req_id: m,
                all_b: p,
            }) => Self {
                ident: IDENT,
                b0: 2 << 29 | (m as u32 & 0xf) << 16 | (e as u32 & 0x3f) << 10,
                p,
            },
        }
    }

    pub fn parse(&self) -> Result<Head, ParseError> {
        let t = (self.b0 >> 29) & 0x7;
        let n = (self.b0 >> 20) & 0x1ff;
        let m = (self.b0 >> 16) & 0xf;
        let e = (self.b0 >> 10) & 0x3f;

        match t {
            0 => Ok(Head::Noti(HNoti {
                route: n as u16,
                all_b: self.p,
            })),
            1 => Ok(Head::Req(HReq {
                route: n as u16,
                req_id: m as u8,
                all_b: self.p,
            })),
            2 => Ok(Head::Rep(HRep {
                errc: RepCode::from_u8(e as u8).ok_or(ParseError::InvalidErrCode(e as u8))?,
                req_id: m as u8,
                all_b: self.p,
            })),
            ty => Err(ParseError::InvalidType(ty as u8)),
        }
    }
}

#[derive(thiserror::Error, Debug)]
pub enum ParseError {
    #[error("invalid message type: {0}")]
    InvalidType(u8),

    #[error("invalid error code: {0}")]
    InvalidErrCode(u8),
}

/* ---------------------------------------------------------------------------------------------- */
/*                                          PARSED HEADER                                         */
/* ---------------------------------------------------------------------------------------------- */
#[derive(Clone, Copy, Debug, derive_more::From)]
pub enum Head {
    Noti(HNoti),
    Req(HReq),
    Rep(HRep),
}

/* --------------------------------------------- -- --------------------------------------------- */
#[derive(Clone, Copy, Debug)]
pub struct HNoti {
    pub route: u16,
    pub all_b: u32,
}

impl HNoti {
    pub fn n(&self) -> usize {
        self.route as usize
    }

    pub fn p(&self) -> usize {
        self.all_b as usize
    }

    pub fn route(&self) -> Range<usize> {
        0..self.n()
    }

    pub fn route_c(&self) -> Range<usize> {
        0..self.n() + 1
    }

    pub fn payload(&self) -> Range<usize> {
        self.n() + 1..self.p()
    }

    pub fn payload_c(&self) -> Range<usize> {
        self.n() + 1..self.p() + 1
    }
}

/* --------------------------------------------- -- --------------------------------------------- */
#[derive(Clone, Copy, Debug)]
pub struct HReq {
    pub route: u16,
    pub req_id: u8,
    pub all_b: u32,
}

impl HReq {
    pub fn n(&self) -> usize {
        self.route as usize
    }

    pub fn m(&self) -> usize {
        self.req_id as usize
    }

    pub fn p(&self) -> usize {
        self.all_b as usize
    }

    pub fn route(&self) -> Range<usize> {
        self.m()..self.m() + self.n()
    }

    pub fn route_c(&self) -> Range<usize> {
        self.m()..self.m() + self.n() + 1
    }

    pub fn payload(&self) -> Range<usize> {
        self.m() + self.n() + 1..self.p()
    }

    pub fn payload_c(&self) -> Range<usize> {
        self.m() + self.n() + 1..self.p() + 1
    }

    pub fn req_id(&self) -> Range<usize> {
        0..self.m()
    }
}

/* --------------------------------------------- -- --------------------------------------------- */
#[derive(Clone, Copy, Debug)]
pub struct HRep {
    pub errc: RepCode,
    pub req_id: u8,
    pub all_b: u32,
}

impl HRep {
    pub fn m(&self) -> usize {
        self.req_id as usize
    }

    pub fn p(&self) -> usize {
        self.all_b as usize
    }

    pub fn payload(&self) -> Range<usize> {
        self.m()..self.p()
    }

    pub fn payload_c(&self) -> Range<usize> {
        self.m()..self.p() + 1
    }

    pub fn req_id(&self) -> Range<usize> {
        0..self.m()
    }
}

/* ------------------------------------------- Helper ------------------------------------------- */
pub fn retrieve_req_id(val: &[u8]) -> u128 {
    debug_assert!(val.len() <= 16);
    let offset = 16 - val.len();
    let value: [u8; 16] = std::array::from_fn(|i| if i < offset { 0 } else { val[i - offset] });
    u128::from_be_bytes(value)
}

pub fn store_req_id<'a>(val: u128, buf: &'a mut [u8; 16]) -> &'a [u8] {
    let value = val.to_be_bytes();
    buf.copy_from_slice(&value);

    // remove leading zeros
    let pos = buf.iter().position(|x| *x != 0).unwrap_or(16);
    &buf[pos..]
}

#[test]
fn test_endian() {
    let val = 1u128;
    let buf = val.to_be_bytes();

    assert!(buf[0..15].iter().all(|x| *x == 0));

    let mut buf: [u8; 16] = [0; 16];
    assert!(store_req_id(val, &mut buf).len() == 1);
    assert!(store_req_id(val, &mut buf)[0] == 1);
}

/* ---------------------------------------------------------------------------------------------- */
/*                                           REPLY CODE                                           */
/* ---------------------------------------------------------------------------------------------- */
/// The response code for the request.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Primitive, PartialEq, Eq)]
pub enum RepCode {
    /// Successful.
    Okay = 0,

    /// Request is canceled by not handling reply structure.
    ///
    /// Payload likely to be empty.
    Aborted = 1,

    /// There was no route to handle the request.
    ///
    /// Payload may contain the route that was requested.
    NoRoute = 2,

    /// Unkown error.
    Unkown = 4,

    /// User returned error. Payload may contain user defined error context.
    UserError = 100,

    /// User error - invalid data format
    ParseFailed = 101,

    /// User error - invalid argument received
    InvalidArgument = 102,

    /// User error - invalid state
    InvalidState = 103,
}