aboutsummaryrefslogtreecommitdiff
path: root/cyw43/src/control.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cyw43/src/control.rs')
-rw-r--r--cyw43/src/control.rs93
1 files changed, 83 insertions, 10 deletions
diff --git a/cyw43/src/control.rs b/cyw43/src/control.rs
index 311fcb08c..26d50d311 100644
--- a/cyw43/src/control.rs
+++ b/cyw43/src/control.rs
@@ -3,7 +3,7 @@ use core::iter::zip;
3 3
4use embassy_net_driver_channel as ch; 4use embassy_net_driver_channel as ch;
5use embassy_net_driver_channel::driver::{HardwareAddress, LinkState}; 5use embassy_net_driver_channel::driver::{HardwareAddress, LinkState};
6use embassy_time::Timer; 6use embassy_time::{Duration, Timer};
7 7
8use crate::consts::*; 8use crate::consts::*;
9use crate::events::{Event, EventSubscriber, Events}; 9use crate::events::{Event, EventSubscriber, Events};
@@ -35,6 +35,45 @@ 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))]
40pub enum ScanType {
41 Active {
42 /// Period of time to wait on each channel when active scanning.
43 dwell_time: Option<Duration>,
44 },
45 Passive {
46 /// Period of time to wait on each channel when passive scanning.
47 dwell_time: Option<Duration>,
48 },
49}
50
51#[derive(Clone)]
52#[cfg_attr(feature = "defmt", derive(defmt::Format))]
53pub struct ScanOptions {
54 pub ssid: Option<heapless::String<32>>,
55 /// If set to `None`, all APs will be returned. If set to `Some`, only APs
56 /// with the specified BSSID will be returned.
57 pub bssid: Option<[u8; 6]>,
58 /// Number of probes to send on each channel.
59 pub nprobes: Option<u16>,
60 /// Time to spend waiting on the home channel.
61 pub home_time: Option<Duration>,
62 pub scan_type: ScanType,
63}
64
65impl Default for ScanOptions {
66 fn default() -> Self {
67 Self {
68 ssid: None,
69 bssid: None,
70 nprobes: None,
71 home_time: None,
72 scan_type: ScanType::Passive { dwell_time: None },
73 }
74 }
75}
76
38impl<'a> Control<'a> { 77impl<'a> Control<'a> {
39 pub(crate) fn new(state_ch: ch::StateRunner<'a>, event_sub: &'a Events, ioctl_state: &'a IoctlState) -> Self { 78 pub(crate) fn new(state_ch: ch::StateRunner<'a>, event_sub: &'a Events, ioctl_state: &'a IoctlState) -> Self {
40 Self { 79 Self {
@@ -471,22 +510,56 @@ impl<'a> Control<'a> {
471 /// # Note 510 /// # Note
472 /// Device events are currently implemented using a bounded queue. 511 /// Device events are currently implemented using a bounded queue.
473 /// To not miss any events, you should make sure to always await the stream. 512 /// To not miss any events, you should make sure to always await the stream.
474 pub async fn scan(&mut self) -> Scanner<'_> { 513 pub async fn scan(&mut self, scan_opts: ScanOptions) -> Scanner<'_> {
514 const SCANTYPE_ACTIVE: u8 = 0;
475 const SCANTYPE_PASSIVE: u8 = 1; 515 const SCANTYPE_PASSIVE: u8 = 1;
476 516
517 let mut active_time = !0;
518 let mut passive_time = !0;
519
520 let scan_type = match scan_opts.scan_type {
521 ScanType::Active { dwell_time: None } => SCANTYPE_ACTIVE,
522 ScanType::Active {
523 dwell_time: Some(dwell_time),
524 } => {
525 active_time = dwell_time.as_millis() as u32;
526 if active_time == !0 {
527 active_time = !0 - 1;
528 }
529 SCANTYPE_ACTIVE
530 }
531 ScanType::Passive { dwell_time: None } => SCANTYPE_PASSIVE,
532 ScanType::Passive {
533 dwell_time: Some(dwell_time),
534 } => {
535 passive_time = dwell_time.as_millis() as u32;
536 if passive_time == !0 {
537 passive_time = !0 - 1;
538 }
539 SCANTYPE_PASSIVE
540 }
541 };
542
477 let scan_params = ScanParams { 543 let scan_params = ScanParams {
478 version: 1, 544 version: 1,
479 action: 1, 545 action: 1,
480 sync_id: 1, 546 sync_id: 1,
481 ssid_len: 0, 547 ssid_len: scan_opts.ssid.as_ref().map(|e| e.as_bytes().len() as u32).unwrap_or(0),
482 ssid: [0; 32], 548 ssid: scan_opts
483 bssid: [0xff; 6], 549 .ssid
550 .map(|e| {
551 let mut ssid = [0; 32];
552 ssid[..e.as_bytes().len()].copy_from_slice(e.as_bytes());
553 ssid
554 })
555 .unwrap_or([0; 32]),
556 bssid: scan_opts.bssid.unwrap_or([0xff; 6]),
484 bss_type: 2, 557 bss_type: 2,
485 scan_type: SCANTYPE_PASSIVE, 558 scan_type,
486 nprobes: !0, 559 nprobes: scan_opts.nprobes.unwrap_or(!0).into(),
487 active_time: !0, 560 active_time,
488 passive_time: !0, 561 passive_time,
489 home_time: !0, 562 home_time: scan_opts.home_time.map(|e| e.as_millis() as u32).unwrap_or(!0),
490 channel_num: 0, 563 channel_num: 0,
491 channel_list: [0; 1], 564 channel_list: [0; 1],
492 }; 565 };