Skip to content

Commit

Permalink
feat(esp-wifi): Implement serde:{Deserialize, Serialize} for wifi s…
Browse files Browse the repository at this point in the history
…tructs (#2346)

* feat(esp-wifi): Implement `serde:{Deserialize, Serialize}` for wifi structs

* Update changelog

* Add missing serde feature for dependencies that supports it.
  • Loading branch information
AnthonyGrondin authored Oct 16, 2024
1 parent 67bc37f commit 060aa72
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions esp-wifi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added `serde` support through the `serde` feature (#2346)

### Changed

### Fixed
Expand Down
7 changes: 5 additions & 2 deletions esp-wifi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ heapless = { version = "0.8.0", default-features = false, features = [
] }
num-derive = { version = "0.4.2" }
num-traits = { version = "0.2.19", default-features = false }
no-std-net = { version = "0.6.0", optional = true }
esp-wifi-sys = { version = "0.6.0" }
embassy-sync = { version = "0.6.0", optional = true }
embassy-net-driver = { version = "0.2.0", optional = true }
Expand All @@ -45,6 +44,7 @@ bt-hci = { version = "0.1.1", optional = true }
esp-config = { version = "0.1.0", path = "../esp-config" }

xtensa-lx-rt = { version = "0.17.1", path = "../xtensa-lx-rt", optional = true }
serde = { version = "1.0.210", default-features = false, features = ["derive"], optional = true }

[build-dependencies]
esp-build = { version = "0.1.0", path = "../esp-build" }
Expand Down Expand Up @@ -126,7 +126,7 @@ smoltcp = ["dep:smoltcp"]
utils = ["smoltcp"]

## Enable WiFi support
wifi = ["dep:enumset", "dep:no-std-net"]
wifi = ["dep:enumset"]

## Enable BLE support
ble = ["esp-hal/bluetooth"]
Expand Down Expand Up @@ -182,6 +182,9 @@ sniffer = ["wifi"]
# Don't include `strchr` - not shown in docs
have-strchr = []

# Implement serde Serialize / Deserialize
serde = ["dep:serde", "enumset?/serde", "heapless/serde"]

[package.metadata.docs.rs]
features = [
"esp32c3",
Expand Down
27 changes: 26 additions & 1 deletion esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub(crate) use os_adapter::*;
#[cfg(feature = "sniffer")]
use portable_atomic::AtomicBool;
use portable_atomic::{AtomicUsize, Ordering};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "smoltcp")]
use smoltcp::phy::{Device, DeviceCapabilities, RxToken, TxToken};
pub use state::*;
Expand Down Expand Up @@ -142,6 +144,7 @@ use crate::{

#[derive(EnumSetType, Debug, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Default)]
pub enum AuthMethod {
None,
Expand All @@ -158,6 +161,7 @@ pub enum AuthMethod {

#[derive(EnumSetType, Debug, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Default)]
pub enum Protocol {
P802D11B,
Expand All @@ -171,6 +175,7 @@ pub enum Protocol {

#[derive(EnumSetType, Debug, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Default)]
pub enum SecondaryChannel {
// TODO: Need to extend that for 5GHz
Expand All @@ -182,6 +187,7 @@ pub enum SecondaryChannel {

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct AccessPointInfo {
pub ssid: heapless::String<32>,
pub bssid: [u8; 6],
Expand All @@ -195,6 +201,7 @@ pub struct AccessPointInfo {

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct AccessPointConfiguration {
pub ssid: heapless::String<32>,
pub ssid_hidden: bool,
Expand Down Expand Up @@ -224,6 +231,7 @@ impl Default for AccessPointConfiguration {

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct ClientConfiguration {
pub ssid: heapless::String<32>,
pub bssid: Option<[u8; 6]>,
Expand Down Expand Up @@ -258,6 +266,7 @@ impl Default for ClientConfiguration {

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct EapFastConfig {
pub fast_provisioning: u8,
pub fast_max_pac_list_len: u8,
Expand All @@ -266,6 +275,7 @@ pub struct EapFastConfig {

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum TtlsPhase2Method {
Eap,
Mschapv2,
Expand Down Expand Up @@ -298,6 +308,7 @@ impl TtlsPhase2Method {

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct EapClientConfiguration {
pub ssid: heapless::String<32>,
pub bssid: Option<[u8; 6]>,
Expand Down Expand Up @@ -363,6 +374,7 @@ impl Default for EapClientConfiguration {

#[derive(EnumSetType, Debug, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum Capability {
Client,
AccessPoint,
Expand All @@ -371,6 +383,7 @@ pub enum Capability {

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Default)]
#[allow(clippy::large_enum_variant)]
pub enum Configuration {
Expand All @@ -379,6 +392,7 @@ pub enum Configuration {
Client(ClientConfiguration),
AccessPoint(AccessPointConfiguration),
Mixed(ClientConfiguration, AccessPointConfiguration),
#[cfg_attr(feature = "serde", serde(skip))]
EapClient(EapClientConfiguration),
}

Expand Down Expand Up @@ -471,12 +485,15 @@ impl Configuration {
}

pub mod ipv4 {
pub use core::net::Ipv4Addr;
use core::{fmt::Display, str::FromStr};

pub use no_std_net::*;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Mask(pub u8);

impl FromStr for Mask {
Expand Down Expand Up @@ -536,6 +553,7 @@ pub mod ipv4 {

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Subnet {
#[cfg_attr(feature = "defmt", defmt(Debug2Format))]
pub gateway: Ipv4Addr,
Expand Down Expand Up @@ -571,6 +589,7 @@ pub mod ipv4 {

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct ClientSettings {
#[cfg_attr(feature = "defmt", defmt(Debug2Format))]
pub ip: Ipv4Addr,
Expand All @@ -597,12 +616,14 @@ pub mod ipv4 {

#[derive(Default, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct DHCPClientSettings {
pub hostname: Option<heapless::String<30>>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum ClientConfiguration {
DHCP(DHCPClientSettings),
Fixed(ClientSettings),
Expand Down Expand Up @@ -635,6 +656,7 @@ pub mod ipv4 {

#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct RouterConfiguration {
pub subnet: Subnet,
pub dhcp_enabled: bool,
Expand All @@ -660,6 +682,7 @@ pub mod ipv4 {

#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum Configuration {
Client(ClientConfiguration),
Router(RouterConfiguration),
Expand All @@ -673,6 +696,7 @@ pub mod ipv4 {

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct IpInfo {
#[cfg_attr(feature = "defmt", defmt(Debug2Format))]
pub ip: Ipv4Addr,
Expand Down Expand Up @@ -723,6 +747,7 @@ impl AuthMethodExt for AuthMethod {
/// Wifi Mode (Sta and/or Ap)
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum WifiMode {
Sta,
Ap,
Expand Down

0 comments on commit 060aa72

Please sign in to comment.