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
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
#[allow(dead_code)]
pub struct PacketType {
    pub version: u8, // 0x40 ( 2 bit value, can be a value between 1-4 depending on version mask, )
    pub timecode: bool, // 0x10
    pub storage: bool, // 0x08
    pub reply: bool, // 0x04
    pub query: bool, //0x02
    pub push: bool,  //0x01
}

impl PacketType {
    pub fn push(&mut self, push: bool) {
        self.push = push;
    }
}

const VERSION_MASK: u8 = 0xc0;
//const RESERVED: u8 = 0x20;
const TIMECODE: u8 = 0x10;
const STORAGE: u8 = 0x08;
const REPLY: u8 = 0x04;
const QUERY: u8 = 0x02;
const PUSH: u8 = 0x01;

impl Default for PacketType {
    fn default() -> Self {
        Self {
            version: 1,
            timecode: false,
            storage: false,
            reply: false,
            query: false,
            push: false,
        }
    }
}

impl From<u8> for PacketType {
    fn from(byte: u8) -> Self {
        let version = match byte & VERSION_MASK {
            0x00 => 0,
            0x40 => 1,
            0x80 => 2,
            0xc0 => 3,
            _ => 0,
        };
        let timecode = byte & TIMECODE == TIMECODE;
        let storage = byte & STORAGE == STORAGE;
        let reply = byte & REPLY == REPLY;
        let query = byte & QUERY == QUERY;
        let push = byte & PUSH == PUSH;

        PacketType {
            version,
            timecode,
            storage,
            reply,
            query,
            push,
        }
    }
}

impl Into<u8> for PacketType {
    fn into(self) -> u8 {
        let mut byte: u8 = 0;
        let v = match self.version {
            1 => self.version,
            2 => self.version,
            3 => self.version,
            4 => self.version,
            _ => 0,
        };
        byte |= v << 6;
        // Set the flag bits
        if self.timecode {
            byte |= TIMECODE
        };
        if self.storage {
            byte |= STORAGE
        };
        if self.reply {
            byte |= REPLY
        };
        if self.query {
            byte |= QUERY
        };
        if self.push {
            byte |= PUSH
        };

        byte
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_packet_type_parse() {
        let byte: u8 = 0b00010110;
        let packet_type = PacketType::from(byte);

        assert_eq!(
            packet_type,
            PacketType {
                version: 0,
                timecode: true,
                storage: false,
                reply: true,
                query: true,
                push: false,
            }
        );
    }

    #[test]
    fn test_packet_type_into_u8() {
        let packet_type = PacketType {
            version: 1,
            timecode: true,
            storage: false,
            reply: true,
            query: true,
            push: false,
        };

        let byte: u8 = packet_type.into();
        assert_eq!(byte, 0x56);
    }
}