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
// There are four ranges:
// First Interindustry   0b000x_xxxx
// Further Interindustry 0b01xx_xxxx
// Reserved              0b001x_xxxx
// Proprietary           0b1xxx_xxxx
//
// For the interindustry ranges, class contains:
// - chaining (continues/last)
// - secure messaging indication (none, two standard, proprietary)
// - logical channel number

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Class {
    cla: u8,
    range: Range,
    // secure_messaging: SecureMessaging,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SecureMessaging {
    None = 0,
    Proprietary = 1,
    Standard = 2,
    Authenticated = 3,
    Unknown,
}

impl SecureMessaging {
    pub fn none(&self) -> bool {
        *self == SecureMessaging::None
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Chain {
    LastOrOnly,
    NotTheLast,
    Unknown,
}

impl Chain {
    #[inline]
    pub fn last_or_only(&self) -> bool {
        *self == Chain::LastOrOnly
    }

    #[inline]
    pub fn not_the_last(&self) -> bool {
        *self == Chain::NotTheLast
    }
}

impl Class {
    #[inline]
    pub fn into_inner(self) -> u8 {
        self.cla
    }

    #[inline]
    pub fn range(&self) -> Range {
        self.range
    }

    #[inline]
    pub fn secure_messaging(&self) -> SecureMessaging {
        match self.range {
            Range::Interindustry(which) => match which {
                Interindustry::First => {
                    match (self.cla >> 2) & 0b11 {
                        0b00 => SecureMessaging::None,
                        0b01 => SecureMessaging::Proprietary,
                        0b10 => SecureMessaging::Standard,
                        0b11 => SecureMessaging::Authenticated,
                        _ => unreachable!(),
                    }
                },
                Interindustry::Further => {
                    match (self.cla >> 5)  != 0 {
                        true => SecureMessaging::Standard,
                        false => SecureMessaging::None,
                    }
                }
                Interindustry::Reserved => SecureMessaging::Unknown,
            }
            _ => SecureMessaging::Unknown,
        }
    }

    #[inline]
    pub fn chain(&self) -> Chain {
        if self.cla & (1 << 4) != 0 {
            Chain::NotTheLast
        } else {
            Chain::LastOrOnly
        }
    }

    #[inline]
    pub fn channel(&self) -> Option<u8> {
        Some(match self.range() {
            Range::Interindustry(Interindustry::First) => {
                self.cla & 0b11
            }
            Range::Interindustry(Interindustry::Further) => {
                4 + self.cla & 0b111
            }
            _ => return None
        })
    }


}

impl core::convert::TryFrom<u8> for Class {
    type Error = InvalidClass;

    #[inline]
    fn try_from(cla: u8) -> Result<Self, Self::Error> {
        let range = Range::try_from(cla)?;
        Ok(Self { cla, range })
    }
}

// impl core::ops::Deref for Class {
//     type Target = u8;
//     fn deref(&self) -> &Self::Target {
//         &self.cla
//     }
// }

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Range {
    Interindustry(Interindustry),
    Proprietary,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Interindustry {
    First,
    Further,
    Reserved,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct InvalidClass {}

impl core::convert::TryFrom<u8> for Range {
    type Error = InvalidClass;

    #[inline]
    fn try_from(cla: u8) -> Result<Self, Self::Error> {
        if cla == 0xff {
            return Err(InvalidClass {})
        }

        let range = match cla >> 5 {
            0b000 => Range::Interindustry(Interindustry::First),
            0b010 | 0b011 => Range::Interindustry(Interindustry::Further),
            0b001 => Range::Interindustry(Interindustry::Reserved),
            _ => Range::Proprietary,
        };

        Ok(range)
    }
}