UsingWi‑Fias an example, theMAC layeris the "network access manager" that sits above the PHY/Baseband and decides how a device joins a network, gains access to the medium, exchanges frames, retries lost packets, and maintains connectivity.A simplified Wi‑Fi stack looks like:Application TCP/IP ↓ 802.11 MAC ↓ 802.11 PHY/Baseband ↓ RF ↓ AntennaThe MAC layer performs:ScanningAuthenticationAssociationSecurity key exchangeData transmissionRetransmissionRoamingPower savingLet's walk through what happens when an ESP32-S31 connects to a Wi‑Fi AP.Step 1: ScanningBefore connecting, the STA (station, e.g., ESP32) must discover available APs.Passive ScanThe STA simply listens.APs periodically send:Beacon Framecontaining:SSID Channel BSSID Security Mode Supported Rates CapabilitiesExample:SSID: HomeWiFi Channel: 6 Security: WPA2Active ScanThe STA sends:Probe RequestExample:Who has SSID "HomeWiFi"?AP replies:Probe ResponseAt this stage, the MAC is handling management frames.STA -------------------> Probe Request STA <------------------- Probe ResponseStep 2: AuthenticationHistorically, 802.11 defined an authentication procedure before association.Open System AuthenticationToday this is the most common.STA sends:Authentication Request `AP replies:Authentication Response (Status = Success)STA AP Authentication Request -----> <------- Authentication ResponseThis doesnotyet verify the Wi‑Fi password.It simply says:You may proceed to associate.Step 3: AssociationNow the station asks to join the AP.Association RequestContains:SSID Supported Data Rates Capabilities QoS Features HT/VHT/HE CapabilitiesExample:I support: 802.11n MCS0~MCS7 WPA2 QoSAssociation ResponseAP replies:Association ID (AID) Status CapabilitiesExample:AID = 0x0021 Status = SuccessAt this point:STA is associatedbut encrypted data is still unavailable.STA AP Assoc Req -------------> <------- Assoc RespStep 4: WPA/WPA2/WPA3 SecurityNow comes the real security process.For WPA2-PSK the famous:4-Way Handshakeoccurs.GoalBoth sides derive:PTK (Pairwise Transient Key)used to encrypt traffic.Message 1AP sends:ANonceMessage 2STA sends:SNonce MICMessage 3AP sends:Install Key GTKMessage 4STA acknowledges.AP STA M1 ANonce ------------> <---- M2 SNonce M3 GTK -------------> <---- M4 ACKAfter this:Encrypted traffic enabledNow IP traffic can start.Step 5: DHCPThis is above the MAC layer but happens immediately afterward.DHCP Discover DHCP Offer DHCP Request DHCP ACKNow the device obtains:IP Address Gateway DNSExample:192.168.1.105Step 6: Data TransmissionOnce connected, the Wi‑Fi MAC handles frame delivery.Example:TCP Packetbecomes:802.11 Data Framecontaining:MAC Header Payload FCSMAC AddressingEach device has:48-bit MAC AddressExample:ESP32: 84:FC:E6:12:34:56 AP: 18:E8:29:AA:BB:CCThe MAC layer inserts source and destination addresses into every frame.CSMA/CAWi‑Fi MAC must share the air with other devices.Unlike Ethernet:Can't detect collisions reliablySo Wi‑Fi uses:CSMA/CACarrier Sense Multiple Access with Collision Avoidance.Example:Device A wants to send.MAC first listens.Channel Busy?If yes:Wait Random Backoff RetryIf no:TransmitListen ↓ Busy? ↓ Wait ↓ Backoff ↓ TransmitThis logic is entirely MAC-level.ACK and RetransmissionAfter every unicast frame:Receiver sends ACK.STA ---------- Data ----------> <------ ACK -----------If no ACK arrives:Retransmit `The MAC maintains:Retry Counter Sequence Number Duplicate DetectionThis happens below TCP/IP.Power SaveFor battery devices, the MAC supports sleeping.AP periodically indicates:Buffered packets waiting.through beacon information.Station behavior:Sleep Wake Check Beacon Retrieve Data Sleep AgainThis is one reason Wi‑Fi IoT devices can save power.RoamingWhen signal quality drops:RSSI ↓the MAC scans for another AP.Example:AP1 -> AP2The MAC performs:Re-authentication Re-association Key Updateswhile trying to keep connectivity.Inside an ESP32-S31Very roughly:Application ↓ TCP/IP Stack ↓ Wi-Fi MAC ├─ Scanning ├─ Authentication ├─ Association ├─ Security ├─ ACK ├─ Retries ├─ Power Save └─ Roaming ↓ Wi-Fi Baseband ├─ OFDM ├─ Modulation ├─ Encoding └─ Synchronization ↓ RF ├─ 2.4 GHz LO ├─ PA ├─ LNA └─ AntennaA useful rule of thumbMACdecideswho can talk, when to talk, and who we're talking to.Basebandconverts packets into Wi‑Fi symbols and vice versa.RFconverts those symbols into actual 2.4 GHz radio waves.So when you see logs like:wifi: scan start wifi: authentication success wifi: association success wifi: connectedthose events are primarily being driven by theWi‑Fi MAC layer, while the baseband and RF are the engines that physically move the bits through the air.
UsingWi‑Fias an example, theMAC layeris the "network access manager" that sits above the PHY/Baseband and decides how a device joins a network, gains access to the medium, exchanges frames, retries lost packets, and maintains connectivity.A simplified Wi‑Fi stack looks like:Application TCP/IP ↓ 802.11 MAC ↓ 802.11 PHY/Baseband ↓ RF ↓ AntennaThe MAC layer performs:ScanningAuthenticationAssociationSecurity key exchangeData transmissionRetransmissionRoamingPower savingLet's walk through what happens when an ESP32-S31 connects to a Wi‑Fi AP.Step 1: ScanningBefore connecting, the STA (station, e.g., ESP32) must discover available APs.Passive ScanThe STA simply listens.APs periodically send:Beacon Framecontaining:SSID Channel BSSID Security Mode Supported Rates CapabilitiesExample:SSID: HomeWiFi Channel: 6 Security: WPA2Active ScanThe STA sends:Probe RequestExample:Who has SSID "HomeWiFi"?AP replies:Probe ResponseAt this stage, the MAC is handling management frames.STA -------------------> Probe Request STA <------------------- Probe ResponseStep 2: AuthenticationHistorically, 802.11 defined an authentication procedure before association.Open System AuthenticationToday this is the most common.STA sends:Authentication Request `AP replies:Authentication Response (Status = Success)STA AP Authentication Request -----> <------- Authentication ResponseThis doesnotyet verify the Wi‑Fi password.It simply says:You may proceed to associate.Step 3: AssociationNow the station asks to join the AP.Association RequestContains:SSID Supported Data Rates Capabilities QoS Features HT/VHT/HE CapabilitiesExample:I support: 802.11n MCS0~MCS7 WPA2 QoSAssociation ResponseAP replies:Association ID (AID) Status CapabilitiesExample:AID = 0x0021 Status = SuccessAt this point:STA is associatedbut encrypted data is still unavailable.STA AP Assoc Req -------------> <------- Assoc RespStep 4: WPA/WPA2/WPA3 SecurityNow comes the real security process.For WPA2-PSK the famous:4-Way Handshakeoccurs.GoalBoth sides derive:PTK (Pairwise Transient Key)used to encrypt traffic.Message 1AP sends:ANonceMessage 2STA sends:SNonce MICMessage 3AP sends:Install Key GTKMessage 4STA acknowledges.AP STA M1 ANonce ------------> <---- M2 SNonce M3 GTK -------------> <---- M4 ACKAfter this:Encrypted traffic enabledNow IP traffic can start.Step 5: DHCPThis is above the MAC layer but happens immediately afterward.DHCP Discover DHCP Offer DHCP Request DHCP ACKNow the device obtains:IP Address Gateway DNSExample:192.168.1.105Step 6: Data TransmissionOnce connected, the Wi‑Fi MAC handles frame delivery.Example:TCP Packetbecomes:802.11 Data Framecontaining:MAC Header Payload FCSMAC AddressingEach device has:48-bit MAC AddressExample:ESP32: 84:FC:E6:12:34:56 AP: 18:E8:29:AA:BB:CCThe MAC layer inserts source and destination addresses into every frame.CSMA/CAWi‑Fi MAC must share the air with other devices.Unlike Ethernet:Can't detect collisions reliablySo Wi‑Fi uses:CSMA/CACarrier Sense Multiple Access with Collision Avoidance.Example:Device A wants to send.MAC first listens.Channel Busy?If yes:Wait Random Backoff RetryIf no:TransmitListen ↓ Busy? ↓ Wait ↓ Backoff ↓ TransmitThis logic is entirely MAC-level.ACK and RetransmissionAfter every unicast frame:Receiver sends ACK.STA ---------- Data ----------> <------ ACK -----------If no ACK arrives:Retransmit `The MAC maintains:Retry Counter Sequence Number Duplicate DetectionThis happens below TCP/IP.Power SaveFor battery devices, the MAC supports sleeping.AP periodically indicates:Buffered packets waiting.through beacon information.Station behavior:Sleep Wake Check Beacon Retrieve Data Sleep AgainThis is one reason Wi‑Fi IoT devices can save power.RoamingWhen signal quality drops:RSSI ↓the MAC scans for another AP.Example:AP1 -> AP2The MAC performs:Re-authentication Re-association Key Updateswhile trying to keep connectivity.Inside an ESP32-S31Very roughly:Application ↓ TCP/IP Stack ↓ Wi-Fi MAC ├─ Scanning ├─ Authentication ├─ Association ├─ Security ├─ ACK ├─ Retries ├─ Power Save └─ Roaming ↓ Wi-Fi Baseband ├─ OFDM ├─ Modulation ├─ Encoding └─ Synchronization ↓ RF ├─ 2.4 GHz LO ├─ PA ├─ LNA └─ AntennaA useful rule of thumbMACdecideswho can talk, when to talk, and who we're talking to.Basebandconverts packets into Wi‑Fi symbols and vice versa.RFconverts those symbols into actual 2.4 GHz radio waves.So when you see logs like:wifi: scan start wifi: authentication success wifi: association success wifi: connectedthose events are primarily being driven by theWi‑Fi MAC layer, while the baseband and RF are the engines that physically move the bits through the air.