awdl_frame_parser/tlvs/sync_elect/channel_sequence_tlv/
channel.rs1use macro_bits::{bit, bitfield, serializable_enum};
2
3serializable_enum! {
4 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5 pub enum ChannelEncoding : u8 {
7 Simple => 0x00,
9
10 Legacy => 0x01,
12
13 OpClass => 0x03
15 }
16}
17impl ChannelEncoding {
18 #[inline]
19 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 pub enum ChannelBandwidth : u8{
43 #[default]
44 TwentyMHz => 0x01,
46
47 FourtyMHz => 0x02,
49
50 EightyMHz => 0x03
52 }
53}
54serializable_enum! {
55 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
56 pub enum Band : u8 {
59 #[default]
60 TwoPointFourGHz => 0x02,
62 FiveGHz => 0x01
64 }
65}
66bitfield! {
67 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
68 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)]
77pub enum Channel {
79 Simple { channel: u8 },
80 Legacy { flags: LegacyFlags, channel: u8 },
81 OpClass { channel: u8, opclass: u8 },
82}
83impl Channel {
84 #[inline]
85 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 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}