diff options
Diffstat (limited to 'embassy-net-esp-hosted/src/control.rs')
| -rw-r--r-- | embassy-net-esp-hosted/src/control.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/embassy-net-esp-hosted/src/control.rs b/embassy-net-esp-hosted/src/control.rs index c86891bc3..b141bd6d2 100644 --- a/embassy-net-esp-hosted/src/control.rs +++ b/embassy-net-esp-hosted/src/control.rs | |||
| @@ -5,6 +5,7 @@ use heapless::String; | |||
| 5 | use crate::ioctl::Shared; | 5 | use crate::ioctl::Shared; |
| 6 | use crate::proto::{self, CtrlMsg}; | 6 | use crate::proto::{self, CtrlMsg}; |
| 7 | 7 | ||
| 8 | /// Errors reported by control. | ||
| 8 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] | 9 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 9 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 10 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 10 | pub enum Error { | 11 | pub enum Error { |
| @@ -13,30 +14,42 @@ pub enum Error { | |||
| 13 | Internal, | 14 | Internal, |
| 14 | } | 15 | } |
| 15 | 16 | ||
| 17 | /// Handle for managing the network and WiFI state. | ||
| 16 | pub struct Control<'a> { | 18 | pub struct Control<'a> { |
| 17 | state_ch: ch::StateRunner<'a>, | 19 | state_ch: ch::StateRunner<'a>, |
| 18 | shared: &'a Shared, | 20 | shared: &'a Shared, |
| 19 | } | 21 | } |
| 20 | 22 | ||
| 23 | /// WiFi mode. | ||
| 21 | #[allow(unused)] | 24 | #[allow(unused)] |
| 22 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] | 25 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 23 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 26 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 24 | enum WifiMode { | 27 | enum WifiMode { |
| 28 | /// No mode. | ||
| 25 | None = 0, | 29 | None = 0, |
| 30 | /// Client station. | ||
| 26 | Sta = 1, | 31 | Sta = 1, |
| 32 | /// Access point mode. | ||
| 27 | Ap = 2, | 33 | Ap = 2, |
| 34 | /// Repeater mode. | ||
| 28 | ApSta = 3, | 35 | ApSta = 3, |
| 29 | } | 36 | } |
| 30 | 37 | ||
| 31 | pub use proto::CtrlWifiSecProt as Security; | 38 | pub use proto::CtrlWifiSecProt as Security; |
| 32 | 39 | ||
| 40 | /// WiFi status. | ||
| 33 | #[derive(Clone, Debug)] | 41 | #[derive(Clone, Debug)] |
| 34 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 42 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 35 | pub struct Status { | 43 | pub struct Status { |
| 44 | /// Service Set Identifier. | ||
| 36 | pub ssid: String<32>, | 45 | pub ssid: String<32>, |
| 46 | /// Basic Service Set Identifier. | ||
| 37 | pub bssid: [u8; 6], | 47 | pub bssid: [u8; 6], |
| 48 | /// Received Signal Strength Indicator. | ||
| 38 | pub rssi: i32, | 49 | pub rssi: i32, |
| 50 | /// WiFi channel. | ||
| 39 | pub channel: u32, | 51 | pub channel: u32, |
| 52 | /// Security mode. | ||
| 40 | pub security: Security, | 53 | pub security: Security, |
| 41 | } | 54 | } |
| 42 | 55 | ||
| @@ -65,6 +78,7 @@ impl<'a> Control<'a> { | |||
| 65 | Self { state_ch, shared } | 78 | Self { state_ch, shared } |
| 66 | } | 79 | } |
| 67 | 80 | ||
| 81 | /// Initialize device. | ||
| 68 | pub async fn init(&mut self) -> Result<(), Error> { | 82 | pub async fn init(&mut self) -> Result<(), Error> { |
| 69 | debug!("wait for init event..."); | 83 | debug!("wait for init event..."); |
| 70 | self.shared.init_wait().await; | 84 | self.shared.init_wait().await; |
| @@ -82,6 +96,7 @@ impl<'a> Control<'a> { | |||
| 82 | Ok(()) | 96 | Ok(()) |
| 83 | } | 97 | } |
| 84 | 98 | ||
| 99 | /// Get the current status. | ||
| 85 | pub async fn get_status(&mut self) -> Result<Status, Error> { | 100 | pub async fn get_status(&mut self) -> Result<Status, Error> { |
| 86 | let req = proto::CtrlMsgReqGetApConfig {}; | 101 | let req = proto::CtrlMsgReqGetApConfig {}; |
| 87 | ioctl!(self, ReqGetApConfig, RespGetApConfig, req, resp); | 102 | ioctl!(self, ReqGetApConfig, RespGetApConfig, req, resp); |
| @@ -95,6 +110,7 @@ impl<'a> Control<'a> { | |||
| 95 | }) | 110 | }) |
| 96 | } | 111 | } |
| 97 | 112 | ||
| 113 | /// Connect to the network identified by ssid using the provided password. | ||
| 98 | pub async fn connect(&mut self, ssid: &str, password: &str) -> Result<(), Error> { | 114 | pub async fn connect(&mut self, ssid: &str, password: &str) -> Result<(), Error> { |
| 99 | let req = proto::CtrlMsgReqConnectAp { | 115 | let req = proto::CtrlMsgReqConnectAp { |
| 100 | ssid: unwrap!(String::try_from(ssid)), | 116 | ssid: unwrap!(String::try_from(ssid)), |
| @@ -108,6 +124,7 @@ impl<'a> Control<'a> { | |||
| 108 | Ok(()) | 124 | Ok(()) |
| 109 | } | 125 | } |
| 110 | 126 | ||
| 127 | /// Disconnect from any currently connected network. | ||
| 111 | pub async fn disconnect(&mut self) -> Result<(), Error> { | 128 | pub async fn disconnect(&mut self) -> Result<(), Error> { |
| 112 | let req = proto::CtrlMsgReqGetStatus {}; | 129 | let req = proto::CtrlMsgReqGetStatus {}; |
| 113 | ioctl!(self, ReqDisconnectAp, RespDisconnectAp, req, resp); | 130 | ioctl!(self, ReqDisconnectAp, RespDisconnectAp, req, resp); |
