-
Notifications
You must be signed in to change notification settings - Fork 308
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eee10e0
feat(esp-wifi): add Country Code
denysvitali 16c4d03
fix: fmt, changelog
denysvitali 9f5a9b9
fix: cast
denysvitali 96e0192
chore: cargo xtask fmt-packages
denysvitali abd22f7
chore: remove example
denysvitali 2e582ee
fix: use Option<[u8;2]> for country code
denysvitali c7a4286
fix: return Some
denysvitali 9c812fe
Use an opaque type
bugadani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,7 +4,10 @@ pub mod event; | |||||||||||||
mod internal; | ||||||||||||||
pub(crate) mod os_adapter; | ||||||||||||||
pub(crate) mod state; | ||||||||||||||
use alloc::{collections::vec_deque::VecDeque, string::String}; | ||||||||||||||
use alloc::{ | ||||||||||||||
collections::vec_deque::VecDeque, | ||||||||||||||
string::{String, ToString}, | ||||||||||||||
}; | ||||||||||||||
use core::{ | ||||||||||||||
fmt::Debug, | ||||||||||||||
marker::PhantomData, | ||||||||||||||
|
@@ -241,6 +244,11 @@ pub struct AccessPointInfo { | |||||||||||||
|
||||||||||||||
/// The authentication method used by the access point. | ||||||||||||||
pub auth_method: Option<AuthMethod>, | ||||||||||||||
|
||||||||||||||
/// 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>, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Configuration for a Wi-Fi access point. | ||||||||||||||
|
@@ -1839,6 +1847,30 @@ fn convert_ap_info(record: &include::wifi_ap_record_t) -> AccessPointInfo { | |||||||||||||
let mut ssid = String::new(); | ||||||||||||||
ssid.push_str(ssid_ref); | ||||||||||||||
|
||||||||||||||
// Extract country code from ESP-IDF structure | ||||||||||||||
let country_code = { | ||||||||||||||
let cc_bytes = | ||||||||||||||
unsafe { core::slice::from_raw_parts(record.country.cc.as_ptr(), 3) }; | ||||||||||||||
|
||||||||||||||
// Find the null terminator or end of array | ||||||||||||||
let cc_len = cc_bytes | ||||||||||||||
.iter() | ||||||||||||||
.position(|&b| b == 0) | ||||||||||||||
.unwrap_or(cc_bytes.len()); | ||||||||||||||
|
||||||||||||||
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 commentThe 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
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||
if cc_slice.iter().all(|&b| b.is_ascii_uppercase()) { | ||||||||||||||
core::str::from_utf8(cc_slice).ok().map(|s| s.to_string()) | ||||||||||||||
} else { | ||||||||||||||
None | ||||||||||||||
} | ||||||||||||||
} else { | ||||||||||||||
None | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
AccessPointInfo { | ||||||||||||||
ssid, | ||||||||||||||
bssid: record.bssid, | ||||||||||||||
|
@@ -1851,6 +1883,7 @@ fn convert_ap_info(record: &include::wifi_ap_record_t) -> AccessPointInfo { | |||||||||||||
}, | ||||||||||||||
signal_strength: record.rssi, | ||||||||||||||
auth_method: Some(AuthMethod::from_raw(record.authmode)), | ||||||||||||||
country_code, | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! WiFi scanning example with country code display | ||
//! | ||
//! This example demonstrates how to scan for WiFi access points and display | ||
//! their country codes, which are now available directly from ESP-IDF. | ||
|
||
//% FEATURES: esp-wifi esp-wifi/wifi esp-hal/unstable | ||
//% CHIPS: esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c6 | ||
|
||
#![no_std] | ||
#![no_main] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::{collections::BTreeSet, string::String, vec::Vec}; | ||
use esp_backtrace as _; | ||
use esp_hal::{clock::CpuClock, main, rng::Rng, timer::timg::TimerGroup}; | ||
use esp_println::println; | ||
use esp_wifi::{init, wifi::WifiMode}; | ||
|
||
esp_bootloader_esp_idf::esp_app_desc!(); | ||
|
||
#[main] | ||
fn main() -> ! { | ||
esp_println::logger::init_logger_from_env(); | ||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); | ||
let peripherals = esp_hal::init(config); | ||
|
||
esp_alloc::heap_allocator!(size: 72 * 1024); | ||
|
||
let timg0 = TimerGroup::new(peripherals.TIMG0); | ||
let esp_wifi_ctrl = init(timg0.timer0, Rng::new(peripherals.RNG)).unwrap(); | ||
|
||
let (mut controller, _interfaces) = | ||
esp_wifi::wifi::new(&esp_wifi_ctrl, peripherals.WIFI).unwrap(); | ||
|
||
controller.set_mode(WifiMode::Sta).unwrap(); | ||
controller.start().unwrap(); | ||
|
||
println!("Starting WiFi scan with country code display..."); | ||
|
||
// Perform WiFi scan | ||
let scan_results = controller.scan_n(20).unwrap(); | ||
|
||
println!("Found {} access points:\n", scan_results.len()); | ||
println!("{:<25} | {:<8} | {:<5} | {:<8}", "SSID", "Channel", "RSSI", "Country"); | ||
println!("{:-<50}", ""); | ||
|
||
// Display scan results with country codes | ||
for ap in &scan_results { | ||
let country_display = ap.country_code.as_deref().unwrap_or("Unknown"); | ||
println!( | ||
"{:<25} | {:<8} | {:<5} | {:<8}", | ||
if ap.ssid.len() > 24 { &ap.ssid[..24] } else { &ap.ssid }, | ||
ap.channel, | ||
bugadani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ap.signal_strength, | ||
country_display | ||
); | ||
} | ||
|
||
// Show unique country codes found | ||
let unique_countries: Vec<String> = { | ||
let mut countries: BTreeSet<String> = BTreeSet::new(); | ||
for ap in &scan_results { | ||
if let Some(ref country) = ap.country_code { | ||
countries.insert(country.clone()); | ||
} | ||
} | ||
countries.into_iter().collect() | ||
}; | ||
|
||
println!("\nUnique country codes detected: {:?}", unique_countries); | ||
println!("Total APs with country info: {}", | ||
scan_results.iter().filter(|ap| ap.country_code.is_some()).count()); | ||
|
||
loop { | ||
let mut delay = esp_hal::delay::Delay::new(); | ||
delay.delay_millis(1000u32); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.