Missing country settings for ESP WIFI
In micropython, there is no way to define in which country the ESP WIFI transmitters operate.
This might create legal problems for anyone using the ESP devices and micropython "out of the box".
The ESP-IDF function call is esp_wifi_set_country(), described in https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#wi-fi-country-code.
Current uPy sources have no call to esp_wifi_set_country() - I searched *.[hc] for it.
The issue had been discussed in https://forum.micropython.org/viewtopic.php?f=18&t=10388
esp32/network_wlan: Get and set the WLAN country code.
Summary
This provides an implementation for #7179, by allowing to set (and get) the country code for the ESP32's WLAN radio interface.
No user-visible API changes have been introduced as this uses network.country rather than having its own extra method somewhere else (I initially thought about bolting it onto network.WLAN to minimise code changes, but in for a penny, in for a pound). Ports can now properly set the WLAN country code if they have the APIs for it, rather than just keeping track of the code itself.
There are a couple of minor things one should be aware of when using this on an ESP32, and those have been added as notes to network.country's documentation section:
- ESP-IDF unconditionally stores the chosen country code into the device's non-volatile storage
- setting the country code with no WLAN interface ever being instantiated will raise an exception
- the country code will default to "worldwide" if it was never set in the device or if no WLAN interface was instantiated yet.
Testing
Testing was done manually on an ESP32C3 using ESP-IDF 5.0.4 and via tests/ports/esp32/country_wlan.py. This is tricky to write a reliable test for because the board's WLAN interface may not be in a known state when the test runs.
Right now the test only passes if it is run when no WLAN has been instantiated before and if the WLAN country code is set to either XX (the default when the flash is cleared) or to AU. I was tempted to put IT there instead but I managed to resist :)
The provided test cycles between those two known country codes, so it is still independent from the existing device configuration, as long as the first test run was done with an empty NVS.
Trade-offs and Alternatives
This feature slightly increases the code size so maybe it can be put behind a define of its own, if setting the WLAN country code is not needed. The existing implementation doesn't do anything interesting for ESP32, so some code space was already lost to begin with, which makes the extra space taken by this feature a bit easier to let go.