diff options
| author | kbleeke <[email protected]> | 2023-03-01 19:03:46 +0100 |
|---|---|---|
| committer | kbleeke <[email protected]> | 2023-03-19 17:48:41 +0100 |
| commit | 1b410d6f3f08f12f2bd250a8b76f217291f4df26 (patch) | |
| tree | 658af49eb2c392b106f75a47fd0edc5367235080 /src | |
| parent | e33b99e9ec9902d6f93582530fd9cfe38953ce69 (diff) | |
add event handling to join
Diffstat (limited to 'src')
| -rw-r--r-- | src/events.rs | 14 | ||||
| -rw-r--r-- | src/lib.rs | 42 |
2 files changed, 49 insertions, 7 deletions
diff --git a/src/events.rs b/src/events.rs index a828eec98..9e6bb9625 100644 --- a/src/events.rs +++ b/src/events.rs | |||
| @@ -3,6 +3,9 @@ | |||
| 3 | 3 | ||
| 4 | use core::num; | 4 | use core::num; |
| 5 | 5 | ||
| 6 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 7 | use embassy_sync::pubsub::{PubSubChannel, Publisher, Subscriber}; | ||
| 8 | |||
| 6 | #[derive(Clone, Copy, PartialEq, Eq, num_enum::FromPrimitive)] | 9 | #[derive(Clone, Copy, PartialEq, Eq, num_enum::FromPrimitive)] |
| 7 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 10 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 8 | #[repr(u8)] | 11 | #[repr(u8)] |
| @@ -280,3 +283,14 @@ pub enum Event { | |||
| 280 | /// highest val + 1 for range checking | 283 | /// highest val + 1 for range checking |
| 281 | LAST = 190, | 284 | LAST = 190, |
| 282 | } | 285 | } |
| 286 | |||
| 287 | pub type EventQueue = PubSubChannel<CriticalSectionRawMutex, EventStatus, 2, 1, 1>; | ||
| 288 | pub type EventPublisher<'a> = Publisher<'a, CriticalSectionRawMutex, EventStatus, 2, 1, 1>; | ||
| 289 | pub type EventSubscriber<'a> = Subscriber<'a, CriticalSectionRawMutex, EventStatus, 2, 1, 1>; | ||
| 290 | |||
| 291 | #[derive(Clone, Copy)] | ||
| 292 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 293 | pub struct EventStatus { | ||
| 294 | pub event_type: Event, | ||
| 295 | pub status: u32, | ||
| 296 | } | ||
diff --git a/src/lib.rs b/src/lib.rs index 5733506ac..c58ac8e7d 100644 --- a/src/lib.rs +++ b/src/lib.rs | |||
| @@ -19,13 +19,15 @@ use core::slice; | |||
| 19 | use ch::driver::LinkState; | 19 | use ch::driver::LinkState; |
| 20 | use embassy_futures::yield_now; | 20 | use embassy_futures::yield_now; |
| 21 | use embassy_net_driver_channel as ch; | 21 | use embassy_net_driver_channel as ch; |
| 22 | use embassy_sync::pubsub::PubSubBehavior; | ||
| 22 | use embassy_time::{block_for, Duration, Timer}; | 23 | use embassy_time::{block_for, Duration, Timer}; |
| 23 | use embedded_hal_1::digital::OutputPin; | 24 | use embedded_hal_1::digital::OutputPin; |
| 24 | use embedded_hal_async::spi::{SpiBusRead, SpiBusWrite, SpiDevice}; | 25 | use embedded_hal_async::spi::{SpiBusRead, SpiBusWrite, SpiDevice}; |
| 26 | use events::EventQueue; | ||
| 25 | 27 | ||
| 26 | use crate::bus::Bus; | 28 | use crate::bus::Bus; |
| 27 | use crate::consts::*; | 29 | use crate::consts::*; |
| 28 | use crate::events::Event; | 30 | use crate::events::{Event, EventStatus}; |
| 29 | use crate::structs::*; | 31 | use crate::structs::*; |
| 30 | 32 | ||
| 31 | const MTU: usize = 1514; | 33 | const MTU: usize = 1514; |
| @@ -127,6 +129,7 @@ enum IoctlState { | |||
| 127 | pub struct State { | 129 | pub struct State { |
| 128 | ioctl_state: Cell<IoctlState>, | 130 | ioctl_state: Cell<IoctlState>, |
| 129 | ch: ch::State<MTU, 4, 4>, | 131 | ch: ch::State<MTU, 4, 4>, |
| 132 | events: EventQueue, | ||
| 130 | } | 133 | } |
| 131 | 134 | ||
| 132 | impl State { | 135 | impl State { |
| @@ -134,12 +137,14 @@ impl State { | |||
| 134 | Self { | 137 | Self { |
| 135 | ioctl_state: Cell::new(IoctlState::Idle), | 138 | ioctl_state: Cell::new(IoctlState::Idle), |
| 136 | ch: ch::State::new(), | 139 | ch: ch::State::new(), |
| 140 | events: EventQueue::new(), | ||
| 137 | } | 141 | } |
| 138 | } | 142 | } |
| 139 | } | 143 | } |
| 140 | 144 | ||
| 141 | pub struct Control<'a> { | 145 | pub struct Control<'a> { |
| 142 | state_ch: ch::StateRunner<'a>, | 146 | state_ch: ch::StateRunner<'a>, |
| 147 | event_sub: &'a EventQueue, | ||
| 143 | ioctl_state: &'a Cell<IoctlState>, | 148 | ioctl_state: &'a Cell<IoctlState>, |
| 144 | } | 149 | } |
| 145 | 150 | ||
| @@ -313,6 +318,7 @@ impl<'a> Control<'a> { | |||
| 313 | evts.unset(Event::PROBREQ_MSG_RX); | 318 | evts.unset(Event::PROBREQ_MSG_RX); |
| 314 | evts.unset(Event::PROBRESP_MSG); | 319 | evts.unset(Event::PROBRESP_MSG); |
| 315 | evts.unset(Event::PROBRESP_MSG); | 320 | evts.unset(Event::PROBRESP_MSG); |
| 321 | evts.unset(Event::ROAM); | ||
| 316 | 322 | ||
| 317 | self.set_iovar("bsscfg:event_msgs", &evts.to_bytes()).await; | 323 | self.set_iovar("bsscfg:event_msgs", &evts.to_bytes()).await; |
| 318 | 324 | ||
| @@ -393,8 +399,22 @@ impl<'a> Control<'a> { | |||
| 393 | ssid: [0; 32], | 399 | ssid: [0; 32], |
| 394 | }; | 400 | }; |
| 395 | i.ssid[..ssid.len()].copy_from_slice(ssid.as_bytes()); | 401 | i.ssid[..ssid.len()].copy_from_slice(ssid.as_bytes()); |
| 402 | |||
| 403 | let mut subscriber = self.event_sub.subscriber().unwrap(); | ||
| 396 | self.ioctl(IoctlType::Set, 26, 0, &mut i.to_bytes()).await; // set_ssid | 404 | self.ioctl(IoctlType::Set, 26, 0, &mut i.to_bytes()).await; // set_ssid |
| 397 | 405 | ||
| 406 | loop { | ||
| 407 | let msg = subscriber.next_message_pure().await; | ||
| 408 | if msg.event_type == Event::AUTH && msg.status != 0 { | ||
| 409 | // retry | ||
| 410 | defmt::warn!("JOIN failed with status={}", msg.status); | ||
| 411 | self.ioctl(IoctlType::Set, 26, 0, &mut i.to_bytes()).await; | ||
| 412 | } else if msg.event_type == Event::JOIN && msg.status == 0 { | ||
| 413 | // successful join | ||
| 414 | break; | ||
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 398 | info!("JOINED"); | 418 | info!("JOINED"); |
| 399 | } | 419 | } |
| 400 | 420 | ||
| @@ -489,6 +509,8 @@ pub struct Runner<'a, PWR, SPI> { | |||
| 489 | sdpcm_seq: u8, | 509 | sdpcm_seq: u8, |
| 490 | sdpcm_seq_max: u8, | 510 | sdpcm_seq_max: u8, |
| 491 | 511 | ||
| 512 | events: &'a EventQueue, | ||
| 513 | |||
| 492 | #[cfg(feature = "firmware-logs")] | 514 | #[cfg(feature = "firmware-logs")] |
| 493 | log: LogState, | 515 | log: LogState, |
| 494 | } | 516 | } |
| @@ -526,6 +548,8 @@ where | |||
| 526 | sdpcm_seq: 0, | 548 | sdpcm_seq: 0, |
| 527 | sdpcm_seq_max: 1, | 549 | sdpcm_seq_max: 1, |
| 528 | 550 | ||
| 551 | events: &state.events, | ||
| 552 | |||
| 529 | #[cfg(feature = "firmware-logs")] | 553 | #[cfg(feature = "firmware-logs")] |
| 530 | log: LogState { | 554 | log: LogState { |
| 531 | addr: 0, | 555 | addr: 0, |
| @@ -541,6 +565,7 @@ where | |||
| 541 | device, | 565 | device, |
| 542 | Control { | 566 | Control { |
| 543 | state_ch, | 567 | state_ch, |
| 568 | event_sub: &&state.events, | ||
| 544 | ioctl_state: &state.ioctl_state, | 569 | ioctl_state: &state.ioctl_state, |
| 545 | }, | 570 | }, |
| 546 | runner, | 571 | runner, |
| @@ -883,13 +908,16 @@ where | |||
| 883 | return; | 908 | return; |
| 884 | } | 909 | } |
| 885 | 910 | ||
| 911 | let evt_type = events::Event::from(event_packet.msg.event_type as u8); | ||
| 886 | let evt_data = &bcd_packet[EventMessage::SIZE..][..event_packet.msg.datalen as usize]; | 912 | let evt_data = &bcd_packet[EventMessage::SIZE..][..event_packet.msg.datalen as usize]; |
| 887 | debug!( | 913 | debug!("=== EVENT {}: {} {:02x}", evt_type, event_packet.msg, evt_data); |
| 888 | "=== EVENT {}: {} {:02x}", | 914 | |
| 889 | events::Event::from(event_packet.msg.event_type as u8), | 915 | if evt_type == events::Event::AUTH || evt_type == events::Event::JOIN { |
| 890 | event_packet.msg, | 916 | self.events.publish_immediate(EventStatus { |
| 891 | evt_data | 917 | status: event_packet.msg.status, |
| 892 | ); | 918 | event_type: evt_type, |
| 919 | }); | ||
| 920 | } | ||
| 893 | } | 921 | } |
| 894 | CHANNEL_TYPE_DATA => { | 922 | CHANNEL_TYPE_DATA => { |
| 895 | let bcd_header = BcdHeader::from_bytes(&payload[..BcdHeader::SIZE].try_into().unwrap()); | 923 | let bcd_header = BcdHeader::from_bytes(&payload[..BcdHeader::SIZE].try_into().unwrap()); |
