ESP32 WIFI hidden notifications: API and usage

Hello,

Some recent advances in ESP32S3 wifi blobs reversing

Hidden WiFi Notification Mechanism

ESP32-S3 (and other ESP32 chips with WiFi onboard) have a hidden mechanism which Espressif uses for debugging their RF subsystem.

Apart from debugging, it can be used to create LED status indicators on your device: think of the LEDs on a home WiFi router.

The WiFi stack on ESP32 allows setting a hook which is called by the WiFi driver upon certain events:

static int wifi_hook(int x, int y); // --> your hook function

extern void wifi_set_gpio_debug_cb(void *handler); // ESP-IDF hidden API to install your hook

Once installed, the hook gets called quite often during normal WiFi operation. Doing something slow there will slow the whole WiFi stack down.

The hook function receives two parameters - small integers which identify the event type.

Considering the function names and argument ranges, I'd suggest that x was once used as a GPIO number, while y was the actual data sent to that GPIO.

Instead, we can connect LEDs to arbitrary GPIOs and turn them on/off, creating a visible device status indicator which does not depend on printfs :)

Ok, now about the x and y parameters passed to our hook function: don't be confused by the descriptions, start with Interpretation: below

Low MAC: Frame Transmission Events

X Y Description
8 2 Called every time software tries to transmit a frame using esp_wifi_internal_tx() or esp_wifi_internal_tx_by_ref(), just before the frame is sent to an output queue. Interpretation: Software WiFi Output Event
9 2 Called from lmacTxFrame(), just before hal_mac_txq_enable(). Interpretation: Hardware WiFi Output Started
10 4 Called by ppTask() right after lmacProcessCollisions_task(). Interpretation: WiFi Collision Event
10 2 Called from lmacProcessTxComplete() before processing starts. Interpretation: Hardware Output Finished
10 3 Called from lmacProcessTxComplete() before processing starts. Interpretation: is the same: Hardware Output Finished

Low MAC: Frame Reception

X Y Description
11 >1 Called from IndicateFrame(). Memory buffer successfully allocated. Indicates that a valid WiFi frame was received and passed into the LMAC machinery. Also indicates that the frame is intended for us. Interpretation: RX or Valid Frame Received
12 ?? Called from IndicateFrame() before DiscardFrame(), indicating memory allocation problems. This event only happens in an out-of-memory situation and is not a generic packet-drop indication. Interpretation: Frame Dropped: No Memory (Critical)

IEEE80211: High-Level Events

X Y Description
13 2 Called from ieee80211_hostap_send_beacon_process() just before ic_tx_pkt() to transmit a preassembled beacon. Happens frequently. Only occurs in AP or AP+STA mode. Interpretation: SoftAP Beacon Output, AP is Alive
15 0 Scan started
14 2 Scanning in progress, periodic calls
15 1 Scan completed

Considering the number and density of calls made from the PowerManager module, I am starting to think this whole hook mechanism was invented primarily to debug PM :)

PowerManager

Wakeup / Sleep Procedures

X Y Description
17 0 Called after pm_wake_up(). Interpretation: Wakeup Process Initiated
17 1 Called before pm_wake_done(). Interpretation: Wakeup Process Completed

Now Espressif increases the complexity a little.

A sequence consisting of three consecutive calls.

It is guaranteed that this sequence will not be split by another concurrent sequence because all calls are made from the WiFi task context, which serializes access to the WiFi API. (One big global lock for everything. Reminds me of BSD-derived networking stacks on RTEMS circa 2005.)

Sequence #1

X Y
3 1
4 1
5 0

Observed at the end of:

  • pm_start()
  • pm_disconnected_start()
  • pm_disconnected_wake()

Interpretation: Some Wakeup Process (unclear)

Sequence #2

X Y
3 0
4 1
5 1

Observed at:

  • pm_disconnected_sleep()
  • pm_disconnected_stop()

Interpretation: Some Sleep Process (unclear)

Internal FSM State Change Monitoring

A sequence consisting of two consecutive calls executed whenever the PM module changes state from old_state to new_state.

States are numbered in the range [0..5].

The "minus" below means subtraction, resulting in the X argument also being in the range [0..5].

It is guaranteed that this sequence will not be split by another concurrent sequence.

X Y
5 - old_state 1
5 - new_state 0

Interpretation: PM Is Working

Beacon-Related Activity

X Y Description
0 2 Target Beacon Transmission Time (TBTT). Interpretation: Hardware Wakeup for Sending/Receiving Beacons
1 2 Interpretation: Beacon Processing Start
2 2 Interpretation: No Beacon at Expected Time

Not Fully Understood

X Y Description
6 >1 Have no idea yet
16 2 Happens together with documented WiFi event 0x16 (see ESP-IDF WiFi documentation)

Coexist-Related (RF Arbiter / Scheduler for BT, BLE and WiFi)

These can be used to track down coexistence-related issues.

X Y Description
7 0 RF arbiter is alive and assigning timeslots to BT and WiFi. Only relevant when both are active. Interpretation: Coexistence Scheduler Tick
7 1 Something related to the time-slicing procedure. Can be used as an indication that coexist_schm is functioning.

Minimal Arduino Example

#include <Arduino.h>

extern "C" {
  void wifi_set_gpio_debug_cb(void *handler);
};

static int wifi_hook(int x, int y) {
  esp_rom_printf("\r\nWIFI-GPIO-DEBUG: X=%d, Y=%d\r\n", x, y);
  return 0;
}

void setup() {

  Serial.begin(115200);

  wifi_set_gpio_debug_cb((void *)wifi_hook);

  // Initialize WiFi here or your hook will never be called :)
}

void loop() {
  delay(1000);
}

Are you going to publish this in github?

Should this be in the Showcase topic?

Probably yes. I have a repository called ESP32-Undocumented where I upload all these small findings. Most important is that these posts are consumed by AI's and now Google AI now uses this information :)

Great project!! Thanks for sharing.