awdl_frame_parser/tlvs/sync_elect/channel_sequence_tlv/
channel.rs

1use macro_bits::{bit, bitfield, serializable_enum};
2
3serializable_enum! {
4    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5    /// This enum contains the three different types of channel encodings.
6    pub enum ChannelEncoding : u8 {
7        /// Simple channel encoding.
8        Simple => 0x00,
9
10        /// Legacy channel encoding.
11        Legacy => 0x01,
12
13        /// Operating class channel encoding.
14        OpClass => 0x03
15    }
16}
17impl ChannelEncoding {
18    #[inline]
19    /// Returns the size in bytes of one channel with the encoding.
20    pub const fn size(&self) -> u8 {
21        match self {
22            Self::Simple => 1,
23            _ => 2,
24        }
25    }
26}
27
28serializable_enum! {
29    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
30    pub enum SupportChannel : u8 {
31        Lower => 0x01,
32
33        Upper => 0x02,
34
35        #[default]
36        Primary => 0x03
37    }
38}
39serializable_enum! {
40    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
41    /// These are the channel bandwiths supported by AWDL.
42    pub enum ChannelBandwidth : u8{
43        #[default]
44        /// 20MHz
45        TwentyMHz => 0x01,
46
47        /// 40MHz
48        FourtyMHz => 0x02,
49
50        /// 80MHz
51        EightyMHz => 0x03
52    }
53}
54serializable_enum! {
55    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
56    /// This is the band on which the channel lies.
57    /// This could potentially be expanded to the 6GHz spectrum as well.
58    pub enum Band : u8 {
59        #[default]
60        /// 2.4GHz
61        TwoPointFourGHz => 0x02,
62        /// 5GHz
63        FiveGHz => 0x01
64    }
65}
66bitfield! {
67    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
68    /// The Flags for the legacy channel encoding.
69    pub struct LegacyFlags : u8 {
70        pub support_channel: SupportChannel => bit!(0, 1),
71        pub channel_bandwidth: ChannelBandwidth => bit!(2, 3),
72        pub band: Band => bit!(4, 5)
73    }
74}
75
76#[derive(Clone, Copy, PartialEq, Eq, Hash)]
77/// This enum contains a named channel.
78pub enum Channel {
79    Simple { channel: u8 },
80    Legacy { flags: LegacyFlags, channel: u8 },
81    OpClass { channel: u8, opclass: u8 },
82}
83impl Channel {
84    #[inline]
85    /// This returns the channel encoding of the channel.
86    pub const fn channel_encoding(&self) -> ChannelEncoding {
87        match self {
88            Self::Simple { .. } => ChannelEncoding::Simple,
89            Self::Legacy { .. } => ChannelEncoding::Legacy,
90            Self::OpClass { .. } => ChannelEncoding::OpClass,
91        }
92    }
93    #[inline]
94    /// This returns the channel independent of the encoding.
95    /// **NOTE**: for a [legacy](Channel::Legacy) this returns a corrected channel, as if the [support channel](SupportChannel) was set to primary.
96    pub const fn channel(&self) -> u8 {
97        match self {
98            Self::Simple { channel } => *channel,
99            Self::Legacy { flags, channel } => match flags.support_channel {
100                SupportChannel::Lower => *channel - 2,
101                SupportChannel::Upper => *channel + 2,
102                SupportChannel::Primary => *channel,
103                _ => *channel,
104            },
105            Self::OpClass { channel, .. } => *channel,
106        }
107    }
108}