Skip to content

Conversation

agatti
Copy link
Contributor

@agatti agatti commented Feb 16, 2025

Summary

This commit introduces the ability to obtain a list of stations connected to the device when in soft-AP mode.

A new parameter ("stations") to pass to WLAN.status is supported, returning a tuple of (bssid, ipv4) entries, one per connected station. An empty tuple is returned if no stations are connected, and an exception is raised if an error occurred whilst building the python objects to return to the interpreter.

Documentation is also updated to cover the new parameter.

This fixes #5395.

Testing

I've tested this code as part of a project of mine on a NodeMCU with three stations connected to it, with the expected output being returned by calling interface.status("stations"). I doubt this can be put in the CI pipeline or how this can have a test of its own.

Trade-offs and Alternatives

This comes with a small footprint increase for the general firmware configuration, however if this is unacceptable maybe I can add a port define that disables AP support in MicroPython.

Also, no idea on how wifi_softap_get_station_info behaves with a large number of stations, all possible recoverable error conditions should be handled in this PR.

@dpgeorge
Copy link
Member

Thanks for this addition, it's good because a few other WLAN interfaces also support ap.status("stations"), namely cyw43 (stm32 and rp2), esp-hosted, ninaw10 and esp32.

Unfortunately those existing implementations don't really follow any standard about what they return! Some return a a list of 1-tuples of MAC, some return a list of IPs.

We need to standardise on something and document it. What you have here, namely a list of 2-tuples of MAC and IP, seems like a good choice. The thing with a tuple is that it could be extended in the future to add new entries, and that's backwards compatible.

But the first entry in the tuple should just be a bytes object of the MAC. You can easily hexlify this in Python via .hex(":").

@agatti agatti force-pushed the esp8266-ap-stations-list branch from f2bae29 to b9baf4a Compare February 27, 2025 01:48
@agatti
Copy link
Contributor Author

agatti commented Feb 27, 2025

We need to standardise on something and document it. What you have here, namely a list of 2-tuples of MAC and IP, seems like a good choice. The thing with a tuple is that it could be extended in the future to add new entries, and that's backwards compatible.

That makes sense, thanks for letting me know. I admit I haven't looked at what other ports did as I thought that different ports had different WiFi capabilities exposed to user code.

I'll look at other ports' code later today to see what information is available for connected stations, and see if a followup PR can be submitted to align the ports on that regard. Those changes, if made, will be put behind MICROPY_PREVIEW_VERSION_2 of course.

@agatti agatti force-pushed the esp8266-ap-stations-list branch from b9baf4a to 69d07a0 Compare February 27, 2025 10:39
Copy link

codecov bot commented Feb 27, 2025

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Project coverage is 98.52%. Comparing base (14ba32b) to head (69d07a0).

Files with missing lines Patch % Lines
py/runtime.c 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #16765      +/-   ##
==========================================
- Coverage   98.53%   98.52%   -0.02%     
==========================================
  Files         169      169              
  Lines       21852    21855       +3     
==========================================
  Hits        21532    21532              
- Misses        320      323       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@agatti agatti force-pushed the esp8266-ap-stations-list branch from 69d07a0 to 385c4c1 Compare February 28, 2025 00:49
@agatti agatti force-pushed the esp8266-ap-stations-list branch from 385c4c1 to cd53f29 Compare March 4, 2025 13:09
This commit introduces the ability to obtain a list of stations
connected to the device when in soft-AP mode.

A new parameter ("stations") to pass to WLAN.status is supported,
returning a tuple of (bssid, ipv4) entries, one per connected station.
An empty tuple is returned if no stations are connected, and an
exception is raised if an error occurred whilst building the python
objects to return to the interpreter.

Documentation is also updated to cover the new parameter.

This fixes micropython#5395.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for updating.

I tested on an esp8266 and it works (after having issues with the esp8266 disassociating the STA, I think due to power saving timing problems).

@dpgeorge dpgeorge force-pushed the esp8266-ap-stations-list branch from cd53f29 to 4d034f8 Compare March 5, 2025 05:34
@dpgeorge dpgeorge merged commit 4d034f8 into micropython:master Mar 5, 2025
8 checks passed
@agatti agatti deleted the esp8266-ap-stations-list branch April 10, 2025 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

List mac addresses connected to ESP8266 via AP_IF mode
2 participants