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
use super::*;

const MANAGEMENT_FRAME_SIZE: usize = ManagementFrame::FRAGMENT_SEQUENCE_END;

pub struct ManagementFrameBuilder {
    bytes: [u8; MANAGEMENT_FRAME_SIZE],
}
impl ManagementFrameBuilder {
    #[must_use]
    pub fn new_blank() -> Self {
        let mut builder = Self {
            bytes: [0; MANAGEMENT_FRAME_SIZE],
        };

        builder.type_(FrameType::Management);

        builder
    }

    #[must_use]
    pub fn new_defaults() -> Self {
        Self::new_blank()
    }

    #[must_use]
    pub fn build(&self) -> ManagementFrame {
        ManagementFrame::new(self.bytes().to_vec())
    }
}
impl FrameBuilderTrait for ManagementFrameBuilder {
    fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    fn bytes_mut(&mut self) -> &mut [u8] {
        &mut self.bytes
    }
}

impl FragmentSequenceBuilderTrait for ManagementFrameBuilder {}
impl ManagementFrameBuilderTrait for ManagementFrameBuilder {}

pub trait ManagementFrameBuilderTrait: FrameBuilderTrait + FragmentSequenceBuilderTrait {
    fn addr2(&mut self, mac_address: MacAddress) {
        self.bytes_mut()[10..16].copy_from_slice(mac_address.as_bytes());
    }

    fn addr3(&mut self, mac_address: MacAddress) {
        self.bytes_mut()[16..22].copy_from_slice(mac_address.as_bytes());
    }

    /// Transmitter Address
    /// Who this packet came from wirelessly.
    fn transmitter_address(&mut self, mac_address: MacAddress) {
        self.addr2(mac_address)
    }

    /// Source Address
    /// Who the packet came from.
    fn source_address(&mut self, mac_address: MacAddress) {
        self.transmitter_address(mac_address)
    }

    fn bssid_address(&mut self, mac_address: MacAddress) {
        self.addr3(mac_address)
    }

    fn station_address(&mut self, _mac_address: MacAddress) {
        // noop
    }
}

#[test]
fn test_management_frame_builder() {
    let mut management_frame_builder = ManagementFrameBuilder::new_blank();

    management_frame_builder.version(FrameVersion::Standard);
    management_frame_builder.subtype(FrameSubtype::Management(ManagementSubtype::Beacon));
    management_frame_builder.ds_status(DSStatus::FromDSToSTA);

    management_frame_builder.more_fragments(false);
    management_frame_builder.retry(true);
    management_frame_builder.pwr_mgt(true);
    management_frame_builder.more_data(false);
    management_frame_builder.protected(false);
    management_frame_builder.order(false);

    management_frame_builder.source_address("11:22:33:44:55:66".parse().unwrap());
    management_frame_builder.destination_address("22:22:33:44:55:66".parse().unwrap());
    management_frame_builder.bssid_address("33:22:33:44:55:66".parse().unwrap());

    management_frame_builder.sequence_number(10);
    management_frame_builder.fragment_number(11);

    let management_frame = management_frame_builder.build();

    assert_eq!(
        management_frame.version(),
        FrameVersion::Standard,
        "version"
    );
    assert_eq!(
        management_frame.subtype(),
        FrameSubtype::Management(ManagementSubtype::Beacon),
        "subtype"
    );
    assert_eq!(
        management_frame.ds_status(),
        DSStatus::FromDSToSTA,
        "ds_status"
    );

    assert_eq!(management_frame.more_fragments(), false, "more_fragments");
    assert_eq!(management_frame.retry(), true, "retry");
    assert_eq!(management_frame.pwr_mgt(), true, "pwr_mgt");
    assert_eq!(management_frame.more_data(), false, "more_management");
    assert_eq!(management_frame.protected(), false, "protected");
    assert_eq!(management_frame.order(), false, "order");

    assert_eq!(
        management_frame.source_address().unwrap(),
        "11:22:33:44:55:66".parse().unwrap(),
        "source_address"
    );

    assert_eq!(
        management_frame
            .destination_address()
            .expect("destination_address"),
        "22:22:33:44:55:66".parse().unwrap(),
        "destination_address"
    );

    assert_eq!(
        management_frame.bssid_address().expect("bssid_address"),
        "33:22:33:44:55:66".parse().unwrap(),
        "bssid_address"
    );

    assert_eq!(management_frame.sequence_number(), 10);
    assert_eq!(management_frame.fragment_number(), 11);
}