Skip to content

Commit

Permalink
Merge branch 'set-ip-pr'
Browse files Browse the repository at this point in the history
  • Loading branch information
torkleyy committed Mar 25, 2024
2 parents 38a4add + b363f30 commit 883e2fe
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 20 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license = "MIT OR Apache-2.0"
readme = "README.md"
links = "esp_idf_svc"
build = "build.rs"
documentation = "https://esp-rs.github.io/esp-idf-svc/"
documentation = "https://docs.esp-rs.org/esp-idf-svc/"
rust-version = "1.75"

[features]
Expand Down Expand Up @@ -48,6 +48,9 @@ esp-idf-hal = { version = "0.43", default-features = false }
embassy-time-driver = { version = "0.1", optional = true, features = ["tick-hz-1_000_000"] }
embassy-futures = "0.1"

[patch.crates-io]
embedded-svc = { git = "https://github.com/esp-rs/embedded-svc.git" }

[build-dependencies]
embuild = "0.31.3"

Expand Down
16 changes: 15 additions & 1 deletion examples/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,20 @@ fn main() -> anyhow::Result<()> {

connect_wifi(&mut wifi)?;

// Create HTTP(S) client
// Create HTTP client
//
// Note: To send a request to an HTTPS server, you can do:
//
// ```
// use esp_idf_svc::http::client::{Configuration as HttpConfiguration, EspHttpConnection};
//
// let config = &HttpConfiguration {
// crt_bundle_attach: Some(esp_idf_svc::sys::esp_crt_bundle_attach),
// ..Default::default()
// };
//
// let mut client = HttpClient::wrap(EspHttpConnection::new(&config)?);
// ```
let mut client = HttpClient::wrap(EspHttpConnection::new(&Default::default())?);

// GET
Expand Down Expand Up @@ -173,6 +186,7 @@ fn connect_wifi(wifi: &mut BlockingWifi<EspWifi<'static>>) -> anyhow::Result<()>
auth_method: AuthMethod::WPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
..Default::default()
});

wifi.set_configuration(&wifi_configuration)?;
Expand Down
1 change: 1 addition & 0 deletions examples/http_ws_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
auth_method: AuthMethod::WPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
..Default::default()
});

wifi.set_configuration(&wifi_configuration)?;
Expand Down
1 change: 1 addition & 0 deletions examples/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn connect_wifi(wifi: &mut BlockingWifi<EspWifi<'static>>) -> anyhow::Result<()>
auth_method: AuthMethod::WPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
..Default::default()
});

wifi.set_configuration(&wifi_configuration)?;
Expand Down
1 change: 1 addition & 0 deletions examples/wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn connect_wifi(wifi: &mut BlockingWifi<EspWifi<'static>>) -> anyhow::Result<()>
auth_method: AuthMethod::WPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
..Default::default()
});

wifi.set_configuration(&wifi_configuration)?;
Expand Down
1 change: 1 addition & 0 deletions examples/wifi_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async fn connect_wifi(wifi: &mut AsyncWifi<EspWifi<'static>>) -> anyhow::Result<
auth_method: AuthMethod::WPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
..Default::default()
});

wifi.set_configuration(&wifi_configuration)?;
Expand Down
1 change: 1 addition & 0 deletions examples/wifi_async_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async fn connect_wifi() -> anyhow::Result<AsyncWifi<EspWifi<'static>>> {
auth_method: AuthMethod::WPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
..Default::default()
});

wifi.set_configuration(&wifi_configuration)?;
Expand Down
1 change: 1 addition & 0 deletions examples/wifi_static_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn configure_wifi(wifi: WifiDriver) -> anyhow::Result<EspWifi> {
auth_method: AuthMethod::WPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
..Default::default()
});
wifi.set_configuration(&wifi_configuration)?;

Expand Down
1 change: 1 addition & 0 deletions examples/wps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn main() -> anyhow::Result<()> {
auth_method: AuthMethod::WPA2Personal,
password: credentials[1].passphrase.clone(),
channel: None,
..Default::default()
});
wifi.set_configuration(&wifi_configuration)?;
}
Expand Down
1 change: 1 addition & 0 deletions examples/wps_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async fn connect_wps(wifi: &mut AsyncWifi<EspWifi<'static>>) -> anyhow::Result<(
auth_method: AuthMethod::WPA2Personal,
password: credentials[1].passphrase.clone(),
channel: None,
..Default::default()
});
wifi.set_configuration(&wifi_configuration)?;
}
Expand Down
14 changes: 14 additions & 0 deletions src/netif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,20 @@ impl EspNetif {
})
}

pub fn set_ip_info(&mut self, ip_info: ipv4::IpInfo) -> Result<(), EspError> {
let mut raw_ip_info: Newtype<esp_netif_ip_info_t> = ip_info.clone().into();

unsafe { esp!(esp_netif_set_ip_info(self.0, &mut raw_ip_info.0)) }?;
if let Some(dns) = ip_info.dns {
self.set_dns(dns);
}
if let Some(dns) = ip_info.secondary_dns {
self.set_secondary_dns(dns);
}

Ok(())
}

pub fn get_key(&self) -> heapless::String<32> {
unsafe { from_cstr_ptr(esp_netif_get_ifkey(self.0)) }
.try_into()
Expand Down
9 changes: 4 additions & 5 deletions src/nvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,14 @@ impl NvsEncrypted {
None
};

let c_key_partition = c_key_partition
.map(|p| p.as_ptr())
.unwrap_or(core::ptr::null());

let keys_partition_ptr = unsafe {
esp_partition_find_first(
esp_partition_type_t_ESP_PARTITION_TYPE_DATA,
esp_partition_subtype_t_ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS,
c_key_partition,
match c_key_partition {
Some(ref v) => v.as_ptr(),
None => core::ptr::null(),
},
)
};

Expand Down
23 changes: 16 additions & 7 deletions src/ota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,19 +240,28 @@ impl EspOta {
}

pub fn get_boot_slot(&self) -> Result<Slot, EspError> {
self.get_slot(unsafe { esp_ota_get_boot_partition().as_ref().unwrap() })
if let Some(partition) = unsafe { esp_ota_get_boot_partition().as_ref() } {
self.get_slot(partition)
} else {
Err(EspError::from_infallible::<ESP_ERR_NOT_FOUND>())
}
}

pub fn get_running_slot(&self) -> Result<Slot, EspError> {
self.get_slot(unsafe { esp_ota_get_running_partition().as_ref().unwrap() })
if let Some(partition) = unsafe { esp_ota_get_running_partition().as_ref() } {
self.get_slot(partition)
} else {
Err(EspError::from_infallible::<ESP_ERR_NOT_FOUND>())
}
}

pub fn get_update_slot(&self) -> Result<Slot, EspError> {
self.get_slot(unsafe {
esp_ota_get_next_update_partition(ptr::null())
.as_ref()
.unwrap()
})
if let Some(partition) = unsafe { esp_ota_get_next_update_partition(ptr::null()).as_ref() }
{
self.get_slot(partition)
} else {
Err(EspError::from_infallible::<ESP_ERR_NOT_FOUND>())
}
}

pub fn get_last_invalid_slot(&self) -> Result<Option<Slot>, EspError> {
Expand Down
100 changes: 94 additions & 6 deletions src/wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,10 @@ impl TryFrom<&ClientConfiguration> for Newtype<wifi_sta_config_t> {
let mut result = wifi_sta_config_t {
ssid: [0; 32],
password: [0; 64],
scan_method: wifi_scan_method_t_WIFI_ALL_CHANNEL_SCAN,
bssid_set: conf.bssid.is_some(),
bssid,
channel: conf.channel.unwrap_or(0u8),
listen_interval: 0,
sort_method: wifi_sort_method_t_WIFI_CONNECT_AP_BY_SIGNAL,
threshold: wifi_scan_threshold_t {
rssi: -127,
authmode: Newtype::<wifi_auth_mode_t>::from(conf.auth_method).0,
Expand Down Expand Up @@ -1799,6 +1797,14 @@ impl<'d> NetifStatus for EspWifi<'d> {
#[repr(transparent)]
pub struct WpsCredentialsRef(wifi_event_sta_wps_er_success_t__bindgen_ty_1);

#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct ApStaConnectedRef(wifi_event_ap_staconnected_t);

#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct ApStaDisconnectedRef(wifi_event_ap_stadisconnected_t);

#[cfg(not(any(
esp_idf_version_major = "4",
all(
Expand Down Expand Up @@ -1895,6 +1901,76 @@ impl TryFrom<&WpsCredentialsRef> for WpsCredentials {
}
}

impl ApStaConnectedRef {
/// MAC address of the station connected to ESP32 soft-AP
pub fn mac(&self) -> [u8; 6] {
self.0.mac
}

/// the aid that ESP32 soft-AP gives to the station connected to
pub fn aid(&self) -> u8 {
self.0.aid
}

/// flag to identify mesh child
pub fn is_mesh_child(&self) -> bool {
self.0.is_mesh_child
}
}

impl fmt::Debug for ApStaConnectedRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ApStaConnectedRef")
.field("mac", &self.mac())
.field("aid", &self.aid())
.field("is_mesh_child", &self.is_mesh_child())
.finish()
}
}

impl ApStaDisconnectedRef {
/// MAC address of the station disconnects to ESP32 soft-AP
pub fn mac(&self) -> [u8; 6] {
self.0.mac
}

/// the aid that ESP32 soft-AP gave to the station disconnects to
pub fn aid(&self) -> u8 {
self.0.aid
}

/// flag to identify mesh child
pub fn is_mesh_child(&self) -> bool {
self.0.is_mesh_child
}

/// reason of disconnection
#[cfg(not(any(
esp_idf_version_major = "4",
all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
)))]
pub fn reason(&self) -> u8 {
self.0.reason
}
}

impl fmt::Debug for ApStaDisconnectedRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut ds = f.debug_struct("ApStaDisconnectedRef");
ds.field("mac", &self.mac())
.field("aid", &self.aid())
.field("is_mesh_child", &self.is_mesh_child());

#[cfg(not(any(
esp_idf_version_major = "4",
all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
)))]
ds.field("reason", &self.reason());

ds.finish()
}
}

#[derive(Copy, Clone, Debug)]
pub enum WifiEvent<'a> {
Ready,
Expand All @@ -1916,8 +1992,8 @@ pub enum WifiEvent<'a> {

ApStarted,
ApStopped,
ApStaConnected,
ApStaDisconnected,
ApStaConnected(&'a ApStaConnectedRef),
ApStaDisconnected(&'a ApStaDisconnectedRef),
ApProbeRequestReceived,

FtmReport,
Expand Down Expand Up @@ -1982,8 +2058,20 @@ impl<'a> EspEventDeserializer for WifiEvent<'a> {
wifi_event_t_WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP => WifiEvent::StaWpsPbcOverlap,
wifi_event_t_WIFI_EVENT_AP_START => WifiEvent::ApStarted,
wifi_event_t_WIFI_EVENT_AP_STOP => WifiEvent::ApStopped,
wifi_event_t_WIFI_EVENT_AP_STACONNECTED => WifiEvent::ApStaConnected,
wifi_event_t_WIFI_EVENT_AP_STADISCONNECTED => WifiEvent::ApStaDisconnected,
wifi_event_t_WIFI_EVENT_AP_STACONNECTED => {
let payload = unsafe {
(data.payload.unwrap() as *const _ as *const wifi_event_ap_staconnected_t)
.as_ref()
};
WifiEvent::ApStaConnected(unsafe { core::mem::transmute(payload.unwrap()) })
}
wifi_event_t_WIFI_EVENT_AP_STADISCONNECTED => {
let payload = unsafe {
(data.payload.unwrap() as *const _ as *const wifi_event_ap_stadisconnected_t)
.as_ref()
};
WifiEvent::ApStaDisconnected(unsafe { core::mem::transmute(payload.unwrap()) })
}
wifi_event_t_WIFI_EVENT_AP_PROBEREQRECVED => WifiEvent::ApProbeRequestReceived,
wifi_event_t_WIFI_EVENT_FTM_REPORT => WifiEvent::FtmReport,
wifi_event_t_WIFI_EVENT_STA_BSS_RSSI_LOW => WifiEvent::StaBssRssiLow,
Expand Down
4 changes: 4 additions & 0 deletions src/ws/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ pub struct EspWebSocketClientConfig<'a> {
pub disable_pingpong_discon: bool,
pub use_global_ca_store: bool,
pub skip_cert_common_name_check: bool,
#[cfg(not(esp_idf_version_major = "4"))]
pub crt_bundle_attach: Option<unsafe extern "C" fn(conf: *mut ffi::c_void) -> esp_err_t>,
pub keep_alive_idle: Option<time::Duration>,
pub keep_alive_interval: Option<time::Duration>,
pub keep_alive_count: Option<u16>,
Expand Down Expand Up @@ -233,6 +235,8 @@ impl<'a> TryFrom<&'a EspWebSocketClientConfig<'a>> for (esp_websocket_client_con

use_global_ca_store: conf.use_global_ca_store,
skip_cert_common_name_check: conf.skip_cert_common_name_check,
#[cfg(not(esp_idf_version_major = "4"))]
crt_bundle_attach: conf.crt_bundle_attach,

ping_interval_sec: conf.ping_interval_sec.as_secs() as _,

Expand Down

0 comments on commit 883e2fe

Please sign in to comment.