aboutsummaryrefslogtreecommitdiff
path: root/cyw43
diff options
context:
space:
mode:
authorumgefahren <[email protected]>2024-01-19 23:49:49 +0100
committerDario Nieuwenhuis <[email protected]>2024-01-19 23:51:57 +0100
commit6ca43030db125bd440c8e7383a4fc9c93bea7a4e (patch)
tree3f6ff1bfe7a3e7f4a4947f3918456da18f6fe7e6 /cyw43
parenta2eb46e9e458abec477a118ddbec12ae3f9f0900 (diff)
feat: Extended the Scan API
Diffstat (limited to 'cyw43')
-rw-r--r--cyw43/Cargo.toml4
-rw-r--r--cyw43/src/control.rs93
-rw-r--r--cyw43/src/events.rs4
3 files changed, 88 insertions, 13 deletions
diff --git a/cyw43/Cargo.toml b/cyw43/Cargo.toml
index c857f7378..64c38ea7a 100644
--- a/cyw43/Cargo.toml
+++ b/cyw43/Cargo.toml
@@ -10,7 +10,7 @@ repository = "https://github.com/embassy-rs/embassy"
10documentation = "https://docs.embassy.dev/cyw43" 10documentation = "https://docs.embassy.dev/cyw43"
11 11
12[features] 12[features]
13defmt = ["dep:defmt"] 13defmt = ["dep:defmt", "heapless/defmt-03"]
14log = ["dep:log"] 14log = ["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
32embedded-hal-1 = { package = "embedded-hal", version = "1.0" } 32embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
33num_enum = { version = "0.5.7", default-features = false } 33num_enum = { version = "0.5.7", default-features = false }
34 34
35heapless = "0.8.0"
36
35[package.metadata.embassy_docs] 37[package.metadata.embassy_docs]
36src_base = "https://github.com/embassy-rs/embassy/blob/cyw43-v$VERSION/cyw43/src/" 38src_base = "https://github.com/embassy-rs/embassy/blob/cyw43-v$VERSION/cyw43/src/"
37src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/cyw43/src/" 39src_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..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 };
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)]
315pub enum Payload { 315pub 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
322pub struct Message { 322pub struct Message {
323 pub header: Status, 323 pub header: Status,