Skip to content

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

denysvitali
Copy link

@denysvitali denysvitali commented Jul 19, 2025

Note

Feature was "vibe-coded" with Claude Sonnet 4. Tested and it works for me.

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • I have added necessary changes to user code to the Migration Guide.
  • My changes are in accordance to the esp-rs developer guidelines

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) or None

@Copilot Copilot AI review requested due to automatic review settings July 19, 2025 20:52
Copy link

@Copilot Copilot AI left a 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 the AccessPointInfo 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

Comment on lines +1861 to +1863
if cc_len >= 2 {
// Validate that we have at least 2 valid ASCII characters
let cc_slice = &cc_bytes[..cc_len.min(2)];
Copy link
Preview

Copilot AI Jul 19, 2025

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.

Suggested change
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.

// 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) };
Copy link
Preview

Copilot AI Jul 19, 2025

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.

Suggested change
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.

/// 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>,
Copy link
Contributor

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,
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants