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