diff options
| -rw-r--r-- | examples/rpi-pico-w/src/bin/tcp_server.rs | 11 | ||||
| -rw-r--r-- | src/control.rs | 50 | ||||
| -rw-r--r-- | src/lib.rs | 2 |
3 files changed, 42 insertions, 21 deletions
diff --git a/examples/rpi-pico-w/src/bin/tcp_server.rs b/examples/rpi-pico-w/src/bin/tcp_server.rs index 036f79308..9581602a7 100644 --- a/examples/rpi-pico-w/src/bin/tcp_server.rs +++ b/examples/rpi-pico-w/src/bin/tcp_server.rs | |||
| @@ -94,8 +94,15 @@ async fn main(spawner: Spawner) { | |||
| 94 | 94 | ||
| 95 | unwrap!(spawner.spawn(net_task(stack))); | 95 | unwrap!(spawner.spawn(net_task(stack))); |
| 96 | 96 | ||
| 97 | //control.join_open(env!("WIFI_NETWORK")).await; | 97 | loop { |
| 98 | control.join_wpa2(env!("WIFI_NETWORK"), env!("WIFI_PASSWORD")).await; | 98 | //control.join_open(env!("WIFI_NETWORK")).await; |
| 99 | match control.join_wpa2(env!("WIFI_NETWORK"), env!("WIFI_PASSWORD")).await { | ||
| 100 | Ok(_) => break, | ||
| 101 | Err(err) => { | ||
| 102 | info!("join failed with status={}", err.status); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 99 | 106 | ||
| 100 | // And now we can use it! | 107 | // And now we can use it! |
| 101 | 108 | ||
diff --git a/src/control.rs b/src/control.rs index e1ad06e6b..3d7d4dd38 100644 --- a/src/control.rs +++ b/src/control.rs | |||
| @@ -12,6 +12,11 @@ use crate::ioctl::{IoctlState, IoctlType}; | |||
| 12 | use crate::structs::*; | 12 | use crate::structs::*; |
| 13 | use crate::{countries, events, PowerManagementMode}; | 13 | use crate::{countries, events, PowerManagementMode}; |
| 14 | 14 | ||
| 15 | #[derive(Debug)] | ||
| 16 | pub struct Error { | ||
| 17 | pub status: u32, | ||
| 18 | } | ||
| 19 | |||
| 15 | pub struct Control<'a> { | 20 | pub struct Control<'a> { |
| 16 | state_ch: ch::StateRunner<'a>, | 21 | state_ch: ch::StateRunner<'a>, |
| 17 | events: &'a Events, | 22 | events: &'a Events, |
| @@ -145,7 +150,7 @@ impl<'a> Control<'a> { | |||
| 145 | self.ioctl_set_u32(86, 0, mode_num).await; | 150 | self.ioctl_set_u32(86, 0, mode_num).await; |
| 146 | } | 151 | } |
| 147 | 152 | ||
| 148 | pub async fn join_open(&mut self, ssid: &str) { | 153 | pub async fn join_open(&mut self, ssid: &str) -> Result<(), Error> { |
| 149 | self.set_iovar_u32("ampdu_ba_wsize", 8).await; | 154 | self.set_iovar_u32("ampdu_ba_wsize", 8).await; |
| 150 | 155 | ||
| 151 | self.ioctl_set_u32(134, 0, 0).await; // wsec = open | 156 | self.ioctl_set_u32(134, 0, 0).await; // wsec = open |
| @@ -159,10 +164,10 @@ impl<'a> Control<'a> { | |||
| 159 | }; | 164 | }; |
| 160 | i.ssid[..ssid.len()].copy_from_slice(ssid.as_bytes()); | 165 | i.ssid[..ssid.len()].copy_from_slice(ssid.as_bytes()); |
| 161 | 166 | ||
| 162 | self.wait_for_join(i).await; | 167 | self.wait_for_join(i).await |
| 163 | } | 168 | } |
| 164 | 169 | ||
| 165 | pub async fn join_wpa2(&mut self, ssid: &str, passphrase: &str) { | 170 | pub async fn join_wpa2(&mut self, ssid: &str, passphrase: &str) -> Result<(), Error> { |
| 166 | self.set_iovar_u32("ampdu_ba_wsize", 8).await; | 171 | self.set_iovar_u32("ampdu_ba_wsize", 8).await; |
| 167 | 172 | ||
| 168 | self.ioctl_set_u32(134, 0, 4).await; // wsec = wpa2 | 173 | self.ioctl_set_u32(134, 0, 4).await; // wsec = wpa2 |
| @@ -191,33 +196,42 @@ impl<'a> Control<'a> { | |||
| 191 | }; | 196 | }; |
| 192 | i.ssid[..ssid.len()].copy_from_slice(ssid.as_bytes()); | 197 | i.ssid[..ssid.len()].copy_from_slice(ssid.as_bytes()); |
| 193 | 198 | ||
| 194 | self.wait_for_join(i).await; | 199 | self.wait_for_join(i).await |
| 195 | } | 200 | } |
| 196 | 201 | ||
| 197 | async fn wait_for_join(&mut self, i: SsidInfo) { | 202 | async fn wait_for_join(&mut self, i: SsidInfo) -> Result<(), Error> { |
| 198 | self.events.mask.enable(&[Event::JOIN, Event::AUTH]); | 203 | self.events.mask.enable(&[Event::SET_SSID, Event::AUTH]); |
| 199 | let mut subscriber = self.events.queue.subscriber().unwrap(); | 204 | let mut subscriber = self.events.queue.subscriber().unwrap(); |
| 200 | // the actual join operation starts here | 205 | // the actual join operation starts here |
| 201 | // we make sure to enable events before so we don't miss any | 206 | // we make sure to enable events before so we don't miss any |
| 207 | |||
| 208 | // set_ssid | ||
| 202 | self.ioctl(IoctlType::Set, IOCTL_CMD_SET_SSID, 0, &mut i.to_bytes()) | 209 | self.ioctl(IoctlType::Set, IOCTL_CMD_SET_SSID, 0, &mut i.to_bytes()) |
| 203 | .await; | 210 | .await; |
| 204 | // set_ssid | ||
| 205 | 211 | ||
| 206 | loop { | 212 | // to complete the join, we wait for a SET_SSID event |
| 213 | // we also save the AUTH status for the user, it may be interesting | ||
| 214 | let mut auth_status = 0; | ||
| 215 | let status = loop { | ||
| 207 | let msg = subscriber.next_message_pure().await; | 216 | let msg = subscriber.next_message_pure().await; |
| 208 | if msg.header.event_type == Event::AUTH && msg.header.status != EStatus::SUCCESS { | 217 | if msg.header.event_type == Event::AUTH && msg.header.status != EStatus::SUCCESS { |
| 209 | // retry | 218 | auth_status = msg.header.status; |
| 210 | warn!("JOIN failed with status={}", msg.header.status); | 219 | } else if msg.header.event_type == Event::SET_SSID { |
| 211 | self.ioctl(IoctlType::Set, IOCTL_CMD_SET_SSID, 0, &mut i.to_bytes()) | 220 | // join operation ends with SET_SSID event |
| 212 | .await; | 221 | break msg.header.status; |
| 213 | } else if msg.header.event_type == Event::JOIN && msg.header.status == EStatus::SUCCESS { | ||
| 214 | // successful join | ||
| 215 | break; | ||
| 216 | } | 222 | } |
| 217 | } | 223 | }; |
| 224 | |||
| 218 | self.events.mask.disable_all(); | 225 | self.events.mask.disable_all(); |
| 219 | self.state_ch.set_link_state(LinkState::Up); | 226 | if status == EStatus::SUCCESS { |
| 220 | info!("JOINED"); | 227 | // successful join |
| 228 | self.state_ch.set_link_state(LinkState::Up); | ||
| 229 | info!("JOINED"); | ||
| 230 | Ok(()) | ||
| 231 | } else { | ||
| 232 | warn!("JOIN failed with status={} auth={}", status, auth_status); | ||
| 233 | Err(Error { status }) | ||
| 234 | } | ||
| 221 | } | 235 | } |
| 222 | 236 | ||
| 223 | pub async fn gpio_set(&mut self, gpio_n: u8, gpio_en: bool) { | 237 | pub async fn gpio_set(&mut self, gpio_n: u8, gpio_en: bool) { |
diff --git a/src/lib.rs b/src/lib.rs index d437a882e..4a9ada90f 100644 --- a/src/lib.rs +++ b/src/lib.rs | |||
| @@ -27,7 +27,7 @@ use ioctl::IoctlState; | |||
| 27 | 27 | ||
| 28 | use crate::bus::Bus; | 28 | use crate::bus::Bus; |
| 29 | pub use crate::bus::SpiBusCyw43; | 29 | pub use crate::bus::SpiBusCyw43; |
| 30 | pub use crate::control::Control; | 30 | pub use crate::control::{Control, Error as ControlError}; |
| 31 | pub use crate::runner::Runner; | 31 | pub use crate::runner::Runner; |
| 32 | pub use crate::structs::BssInfo; | 32 | pub use crate::structs::BssInfo; |
| 33 | 33 | ||
