-
Notifications
You must be signed in to change notification settings - Fork 307
feat(esp-wifi): add Country Code #3837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for extracting and returning country codes from WiFi access point scan results. The country code is obtained from beacon frames when available and exposed as an optional 2-character ISO country code string.
- Adds a new
country_code
field to theAccessPointInfo
struct - Implements country code extraction logic in the
convert_ap_info
function with validation for ASCII uppercase characters - Updates the changelog to document the new feature
Reviewed Changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
esp-wifi/src/wifi/mod.rs | Adds country_code field to AccessPointInfo struct and implements extraction logic from ESP-IDF wifi_ap_record_t |
esp-wifi/CHANGELOG.md | Documents the new country code feature in the changelog |
if cc_len >= 2 { | ||
// Validate that we have at least 2 valid ASCII characters | ||
let cc_slice = &cc_bytes[..cc_len.min(2)]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number 2 is used multiple times for country code length validation. Consider defining a constant like const COUNTRY_CODE_LENGTH: usize = 2;
to improve maintainability and make the intent clearer.
if cc_len >= 2 { | |
// Validate that we have at least 2 valid ASCII characters | |
let cc_slice = &cc_bytes[..cc_len.min(2)]; | |
if cc_len >= COUNTRY_CODE_LENGTH { | |
// Validate that we have at least COUNTRY_CODE_LENGTH valid ASCII characters | |
let cc_slice = &cc_bytes[..cc_len.min(COUNTRY_CODE_LENGTH)]; |
Copilot uses AI. Check for mistakes.
esp-wifi/src/wifi/mod.rs
Outdated
// Extract country code from ESP-IDF structure | ||
let country_code = { | ||
let cc_bytes = | ||
unsafe { core::slice::from_raw_parts(record.country.cc.as_ptr() as *const u8, 3) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number 3 for the country code array size should be defined as a constant to improve code maintainability and reduce the risk of inconsistencies if the underlying structure changes.
unsafe { core::slice::from_raw_parts(record.country.cc.as_ptr() as *const u8, 3) }; | |
unsafe { core::slice::from_raw_parts(record.country.cc.as_ptr() as *const u8, COUNTRY_CODE_SIZE) }; |
Copilot uses AI. Check for mistakes.
esp-wifi/src/wifi/mod.rs
Outdated
/// The country code of the access point (if available from beacon frames). | ||
/// This is a 2-character ISO country code (e.g., "US", "DE", "JP"). | ||
#[cfg_attr(feature = "defmt", defmt(Debug2Format))] | ||
pub country_code: Option<String>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasting 12 bytes AND a heap allocation for something so rarely needed is not the greatest choice. Let's use an opaque type that wraps [u8; 2]
instead, and has an as_str() -> &str
method.
Also, this lets us avoid defmt(Debug2Format)
.
} else { | ||
&ap.ssid | ||
}, | ||
ap.channel, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example is really not necessary. If you think it is, then it can be part of the documentation.
I'd refrain from teaching BTreeSet. For the number of APs we usually work with, it's better to directly push into a vector, after verifying that it doesn't contain the value.
Note
Feature was "vibe-coded" with Claude Sonnet 4. Tested and it works for me.
Submission Checklist 📝
cargo xtask fmt-packages
command to ensure that all changed code is formatted correctly.CHANGELOG.md
in the proper section.Extra:
Pull Request Details 📖
Description
This PR adds support to return the country code of the access points scanned by esp-wifi.
Testing
Tested by using the feature - most of the APs around me either return a country code (e.g:
US
) orNone