aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkbleeke <[email protected]>2023-03-01 19:03:46 +0100
committerkbleeke <[email protected]>2023-03-19 17:48:41 +0100
commit1b410d6f3f08f12f2bd250a8b76f217291f4df26 (patch)
tree658af49eb2c392b106f75a47fd0edc5367235080 /src
parente33b99e9ec9902d6f93582530fd9cfe38953ce69 (diff)
add event handling to join
Diffstat (limited to 'src')
-rw-r--r--src/events.rs14
-rw-r--r--src/lib.rs42
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
4use core::num; 4use core::num;
5 5
6use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
7use 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
287pub type EventQueue = PubSubChannel<CriticalSectionRawMutex, EventStatus, 2, 1, 1>;
288pub type EventPublisher<'a> = Publisher<'a, CriticalSectionRawMutex, EventStatus, 2, 1, 1>;
289pub type EventSubscriber<'a> = Subscriber<'a, CriticalSectionRawMutex, EventStatus, 2, 1, 1>;
290
291#[derive(Clone, Copy)]
292#[cfg_attr(feature = "defmt", derive(defmt::Format))]
293pub 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;
19use ch::driver::LinkState; 19use ch::driver::LinkState;
20use embassy_futures::yield_now; 20use embassy_futures::yield_now;
21use embassy_net_driver_channel as ch; 21use embassy_net_driver_channel as ch;
22use embassy_sync::pubsub::PubSubBehavior;
22use embassy_time::{block_for, Duration, Timer}; 23use embassy_time::{block_for, Duration, Timer};
23use embedded_hal_1::digital::OutputPin; 24use embedded_hal_1::digital::OutputPin;
24use embedded_hal_async::spi::{SpiBusRead, SpiBusWrite, SpiDevice}; 25use embedded_hal_async::spi::{SpiBusRead, SpiBusWrite, SpiDevice};
26use events::EventQueue;
25 27
26use crate::bus::Bus; 28use crate::bus::Bus;
27use crate::consts::*; 29use crate::consts::*;
28use crate::events::Event; 30use crate::events::{Event, EventStatus};
29use crate::structs::*; 31use crate::structs::*;
30 32
31const MTU: usize = 1514; 33const MTU: usize = 1514;
@@ -127,6 +129,7 @@ enum IoctlState {
127pub struct State { 129pub 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
132impl State { 135impl 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
141pub struct Control<'a> { 145pub 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());