diff options
| -rw-r--r-- | embassy-net-esp-hosted/src/control.rs | 17 | ||||
| -rw-r--r-- | embassy-net-esp-hosted/src/lib.rs | 9 | ||||
| -rw-r--r-- | embassy-net-esp-hosted/src/proto.rs | 2 |
3 files changed, 28 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); |
diff --git a/embassy-net-esp-hosted/src/lib.rs b/embassy-net-esp-hosted/src/lib.rs index d61eaef3a..ce7f39dc1 100644 --- a/embassy-net-esp-hosted/src/lib.rs +++ b/embassy-net-esp-hosted/src/lib.rs | |||
| @@ -97,12 +97,14 @@ enum InterfaceType { | |||
| 97 | const MAX_SPI_BUFFER_SIZE: usize = 1600; | 97 | const MAX_SPI_BUFFER_SIZE: usize = 1600; |
| 98 | const HEARTBEAT_MAX_GAP: Duration = Duration::from_secs(20); | 98 | const HEARTBEAT_MAX_GAP: Duration = Duration::from_secs(20); |
| 99 | 99 | ||
| 100 | /// Shared driver state. | ||
| 100 | pub struct State { | 101 | pub struct State { |
| 101 | shared: Shared, | 102 | shared: Shared, |
| 102 | ch: ch::State<MTU, 4, 4>, | 103 | ch: ch::State<MTU, 4, 4>, |
| 103 | } | 104 | } |
| 104 | 105 | ||
| 105 | impl State { | 106 | impl State { |
| 107 | /// Shared state for the | ||
| 106 | pub fn new() -> Self { | 108 | pub fn new() -> Self { |
| 107 | Self { | 109 | Self { |
| 108 | shared: Shared::new(), | 110 | shared: Shared::new(), |
| @@ -111,8 +113,13 @@ impl State { | |||
| 111 | } | 113 | } |
| 112 | } | 114 | } |
| 113 | 115 | ||
| 116 | /// Type alias for network driver. | ||
| 114 | pub type NetDriver<'a> = ch::Device<'a, MTU>; | 117 | pub type NetDriver<'a> = ch::Device<'a, MTU>; |
| 115 | 118 | ||
| 119 | /// Create a new esp-hosted driver using the provided state, SPI peripheral and pins. | ||
| 120 | /// | ||
| 121 | /// Returns a device handle for interfacing with embassy-net, a control handle for | ||
| 122 | /// interacting with the driver, and a runner for communicating with the WiFi device. | ||
| 116 | pub async fn new<'a, SPI, IN, OUT>( | 123 | pub async fn new<'a, SPI, IN, OUT>( |
| 117 | state: &'a mut State, | 124 | state: &'a mut State, |
| 118 | spi: SPI, | 125 | spi: SPI, |
| @@ -144,6 +151,7 @@ where | |||
| 144 | (device, Control::new(state_ch, &state.shared), runner) | 151 | (device, Control::new(state_ch, &state.shared), runner) |
| 145 | } | 152 | } |
| 146 | 153 | ||
| 154 | /// Runner for communicating with the WiFi device. | ||
| 147 | pub struct Runner<'a, SPI, IN, OUT> { | 155 | pub struct Runner<'a, SPI, IN, OUT> { |
| 148 | ch: ch::Runner<'a, MTU>, | 156 | ch: ch::Runner<'a, MTU>, |
| 149 | state_ch: ch::StateRunner<'a>, | 157 | state_ch: ch::StateRunner<'a>, |
| @@ -166,6 +174,7 @@ where | |||
| 166 | { | 174 | { |
| 167 | async fn init(&mut self) {} | 175 | async fn init(&mut self) {} |
| 168 | 176 | ||
| 177 | /// Run the packet processing. | ||
| 169 | pub async fn run(mut self) -> ! { | 178 | pub async fn run(mut self) -> ! { |
| 170 | debug!("resetting..."); | 179 | debug!("resetting..."); |
| 171 | self.reset.set_low().unwrap(); | 180 | self.reset.set_low().unwrap(); |
diff --git a/embassy-net-esp-hosted/src/proto.rs b/embassy-net-esp-hosted/src/proto.rs index 8ceb1579d..b42ff62f1 100644 --- a/embassy-net-esp-hosted/src/proto.rs +++ b/embassy-net-esp-hosted/src/proto.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | #![allow(missing_docs)] | ||
| 2 | |||
| 1 | use heapless::{String, Vec}; | 3 | use heapless::{String, Vec}; |
| 2 | 4 | ||
| 3 | /// internal supporting structures for CtrlMsg | 5 | /// internal supporting structures for CtrlMsg |
