diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-01-20 01:40:30 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-01-20 01:40:30 +0000 |
| commit | da3884ff688236c2e3930556d6d4cc11e5880870 (patch) | |
| tree | 9c86190b2537e169b6b54d7b7b7466b2f5783158 | |
| parent | 67159d80bb861633a952f40c20c60db42f5ebb18 (diff) | |
| parent | 24968629ec810b844b819e8f84baab2a9349ed2f (diff) | |
Merge pull request #2282 from umgefahren/extend-cyw43-bss-info
feat: Extended the Scan API
| -rw-r--r-- | cyw43/Cargo.toml | 4 | ||||
| -rw-r--r-- | cyw43/src/control.rs | 89 | ||||
| -rw-r--r-- | cyw43/src/events.rs | 4 | ||||
| -rw-r--r-- | examples/rp/src/bin/wifi_scan.rs | 2 |
4 files changed, 85 insertions, 14 deletions
diff --git a/cyw43/Cargo.toml b/cyw43/Cargo.toml index c857f7378..f279739e4 100644 --- a/cyw43/Cargo.toml +++ b/cyw43/Cargo.toml | |||
| @@ -10,7 +10,7 @@ repository = "https://github.com/embassy-rs/embassy" | |||
| 10 | documentation = "https://docs.embassy.dev/cyw43" | 10 | documentation = "https://docs.embassy.dev/cyw43" |
| 11 | 11 | ||
| 12 | [features] | 12 | [features] |
| 13 | defmt = ["dep:defmt"] | 13 | defmt = ["dep:defmt", "heapless/defmt-03", "embassy-time/defmt"] |
| 14 | log = ["dep:log"] | 14 | log = ["dep:log"] |
| 15 | 15 | ||
| 16 | # Fetch console logs from the WiFi firmware and forward them to `log` or `defmt`. | 16 | # Fetch console logs from the WiFi firmware and forward them to `log` or `defmt`. |
| @@ -32,6 +32,8 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa | |||
| 32 | embedded-hal-1 = { package = "embedded-hal", version = "1.0" } | 32 | embedded-hal-1 = { package = "embedded-hal", version = "1.0" } |
| 33 | num_enum = { version = "0.5.7", default-features = false } | 33 | num_enum = { version = "0.5.7", default-features = false } |
| 34 | 34 | ||
| 35 | heapless = "0.8.0" | ||
| 36 | |||
| 35 | [package.metadata.embassy_docs] | 37 | [package.metadata.embassy_docs] |
| 36 | src_base = "https://github.com/embassy-rs/embassy/blob/cyw43-v$VERSION/cyw43/src/" | 38 | src_base = "https://github.com/embassy-rs/embassy/blob/cyw43-v$VERSION/cyw43/src/" |
| 37 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/cyw43/src/" | 39 | src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/cyw43/src/" |
diff --git a/cyw43/src/control.rs b/cyw43/src/control.rs index 311fcb08c..135b8c245 100644 --- a/cyw43/src/control.rs +++ b/cyw43/src/control.rs | |||
| @@ -3,7 +3,7 @@ use core::iter::zip; | |||
| 3 | 3 | ||
| 4 | use embassy_net_driver_channel as ch; | 4 | use embassy_net_driver_channel as ch; |
| 5 | use embassy_net_driver_channel::driver::{HardwareAddress, LinkState}; | 5 | use embassy_net_driver_channel::driver::{HardwareAddress, LinkState}; |
| 6 | use embassy_time::Timer; | 6 | use embassy_time::{Duration, Timer}; |
| 7 | 7 | ||
| 8 | use crate::consts::*; | 8 | use crate::consts::*; |
| 9 | use crate::events::{Event, EventSubscriber, Events}; | 9 | use crate::events::{Event, EventSubscriber, Events}; |
| @@ -35,6 +35,43 @@ pub struct Control<'a> { | |||
| 35 | ioctl_state: &'a IoctlState, | 35 | ioctl_state: &'a IoctlState, |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | #[derive(Copy, Clone)] | ||
| 39 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 40 | pub enum ScanType { | ||
| 41 | Active, | ||
| 42 | Passive, | ||
| 43 | } | ||
| 44 | |||
| 45 | #[derive(Clone)] | ||
| 46 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 47 | pub struct ScanOptions { | ||
| 48 | pub ssid: Option<heapless::String<32>>, | ||
| 49 | /// If set to `None`, all APs will be returned. If set to `Some`, only APs | ||
| 50 | /// with the specified BSSID will be returned. | ||
| 51 | pub bssid: Option<[u8; 6]>, | ||
| 52 | /// Number of probes to send on each channel. | ||
| 53 | pub nprobes: Option<u16>, | ||
| 54 | /// Time to spend waiting on the home channel. | ||
| 55 | pub home_time: Option<Duration>, | ||
| 56 | /// Scan type: active or passive. | ||
| 57 | pub scan_type: ScanType, | ||
| 58 | /// Period of time to wait on each channel when passive scanning. | ||
| 59 | pub dwell_time: Option<Duration>, | ||
| 60 | } | ||
| 61 | |||
| 62 | impl Default for ScanOptions { | ||
| 63 | fn default() -> Self { | ||
| 64 | Self { | ||
| 65 | ssid: None, | ||
| 66 | bssid: None, | ||
| 67 | nprobes: None, | ||
| 68 | home_time: None, | ||
| 69 | scan_type: ScanType::Passive, | ||
| 70 | dwell_time: None, | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 38 | impl<'a> Control<'a> { | 75 | impl<'a> Control<'a> { |
| 39 | pub(crate) fn new(state_ch: ch::StateRunner<'a>, event_sub: &'a Events, ioctl_state: &'a IoctlState) -> Self { | 76 | pub(crate) fn new(state_ch: ch::StateRunner<'a>, event_sub: &'a Events, ioctl_state: &'a IoctlState) -> Self { |
| 40 | Self { | 77 | Self { |
| @@ -471,22 +508,54 @@ impl<'a> Control<'a> { | |||
| 471 | /// # Note | 508 | /// # Note |
| 472 | /// Device events are currently implemented using a bounded queue. | 509 | /// Device events are currently implemented using a bounded queue. |
| 473 | /// To not miss any events, you should make sure to always await the stream. | 510 | /// To not miss any events, you should make sure to always await the stream. |
| 474 | pub async fn scan(&mut self) -> Scanner<'_> { | 511 | pub async fn scan(&mut self, scan_opts: ScanOptions) -> Scanner<'_> { |
| 512 | const SCANTYPE_ACTIVE: u8 = 0; | ||
| 475 | const SCANTYPE_PASSIVE: u8 = 1; | 513 | const SCANTYPE_PASSIVE: u8 = 1; |
| 476 | 514 | ||
| 515 | let dwell_time = match scan_opts.dwell_time { | ||
| 516 | None => !0, | ||
| 517 | Some(t) => { | ||
| 518 | let mut t = t.as_millis() as u32; | ||
| 519 | if t == !0 { | ||
| 520 | t = !0 - 1; | ||
| 521 | } | ||
| 522 | t | ||
| 523 | } | ||
| 524 | }; | ||
| 525 | |||
| 526 | let mut active_time = !0; | ||
| 527 | let mut passive_time = !0; | ||
| 528 | let scan_type = match scan_opts.scan_type { | ||
| 529 | ScanType::Active => { | ||
| 530 | active_time = dwell_time; | ||
| 531 | SCANTYPE_ACTIVE | ||
| 532 | } | ||
| 533 | ScanType::Passive => { | ||
| 534 | passive_time = dwell_time; | ||
| 535 | SCANTYPE_PASSIVE | ||
| 536 | } | ||
| 537 | }; | ||
| 538 | |||
| 477 | let scan_params = ScanParams { | 539 | let scan_params = ScanParams { |
| 478 | version: 1, | 540 | version: 1, |
| 479 | action: 1, | 541 | action: 1, |
| 480 | sync_id: 1, | 542 | sync_id: 1, |
| 481 | ssid_len: 0, | 543 | ssid_len: scan_opts.ssid.as_ref().map(|e| e.as_bytes().len() as u32).unwrap_or(0), |
| 482 | ssid: [0; 32], | 544 | ssid: scan_opts |
| 483 | bssid: [0xff; 6], | 545 | .ssid |
| 546 | .map(|e| { | ||
| 547 | let mut ssid = [0; 32]; | ||
| 548 | ssid[..e.as_bytes().len()].copy_from_slice(e.as_bytes()); | ||
| 549 | ssid | ||
| 550 | }) | ||
| 551 | .unwrap_or([0; 32]), | ||
| 552 | bssid: scan_opts.bssid.unwrap_or([0xff; 6]), | ||
| 484 | bss_type: 2, | 553 | bss_type: 2, |
| 485 | scan_type: SCANTYPE_PASSIVE, | 554 | scan_type, |
| 486 | nprobes: !0, | 555 | nprobes: scan_opts.nprobes.unwrap_or(!0).into(), |
| 487 | active_time: !0, | 556 | active_time, |
| 488 | passive_time: !0, | 557 | passive_time, |
| 489 | home_time: !0, | 558 | home_time: scan_opts.home_time.map(|e| e.as_millis() as u32).unwrap_or(!0), |
| 490 | channel_num: 0, | 559 | channel_num: 0, |
| 491 | channel_list: [0; 1], | 560 | channel_list: [0; 1], |
| 492 | }; | 561 | }; |
diff --git a/cyw43/src/events.rs b/cyw43/src/events.rs index a94c49a0c..44bfa98e9 100644 --- a/cyw43/src/events.rs +++ b/cyw43/src/events.rs | |||
| @@ -311,13 +311,13 @@ pub struct Status { | |||
| 311 | pub status: u32, | 311 | pub status: u32, |
| 312 | } | 312 | } |
| 313 | 313 | ||
| 314 | #[derive(Clone, Copy)] | 314 | #[derive(Copy, Clone)] |
| 315 | pub enum Payload { | 315 | pub enum Payload { |
| 316 | None, | 316 | None, |
| 317 | BssInfo(BssInfo), | 317 | BssInfo(BssInfo), |
| 318 | } | 318 | } |
| 319 | 319 | ||
| 320 | #[derive(Clone, Copy)] | 320 | #[derive(Copy, Clone)] |
| 321 | 321 | ||
| 322 | pub struct Message { | 322 | pub struct Message { |
| 323 | pub header: Status, | 323 | pub header: Status, |
diff --git a/examples/rp/src/bin/wifi_scan.rs b/examples/rp/src/bin/wifi_scan.rs index 45bb5b76c..e678209dd 100644 --- a/examples/rp/src/bin/wifi_scan.rs +++ b/examples/rp/src/bin/wifi_scan.rs | |||
| @@ -65,7 +65,7 @@ async fn main(spawner: Spawner) { | |||
| 65 | .set_power_management(cyw43::PowerManagementMode::PowerSave) | 65 | .set_power_management(cyw43::PowerManagementMode::PowerSave) |
| 66 | .await; | 66 | .await; |
| 67 | 67 | ||
| 68 | let mut scanner = control.scan().await; | 68 | let mut scanner = control.scan(Default::default()).await; |
| 69 | while let Some(bss) = scanner.next().await { | 69 | while let Some(bss) = scanner.next().await { |
| 70 | if let Ok(ssid_str) = str::from_utf8(&bss.ssid) { | 70 | if let Ok(ssid_str) = str::from_utf8(&bss.ssid) { |
| 71 | info!("scanned {} == {:x}", ssid_str, bss.bssid); | 71 | info!("scanned {} == {:x}", ssid_str, bss.bssid); |
