Skip to content

Commit

Permalink
Simplify STA connect/disconnect logic, removing the broken supplicant…
Browse files Browse the repository at this point in the history
… way of doing it, as it never really worked out. This also means we will no longer persist network configs on the device, and will no longer use the active on startup field (#68)
  • Loading branch information
MathiasKoch authored Sep 22, 2023
1 parent 7517cdf commit 3b8f1a0
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 658 deletions.
25 changes: 5 additions & 20 deletions ublox-short-range/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use crate::{
wifi::{
connection::{NetworkState, WiFiState, WifiConnection},
network::{WifiMode, WifiNetwork},
supplicant::Supplicant,
SocketMap,
},
UbloxWifiBuffers, UbloxWifiIngress, UbloxWifiUrcChannel,
Expand Down Expand Up @@ -170,7 +169,7 @@ where
pub(crate) socket_map: SocketMap,
pub(crate) udp_listener: UdpListener<2, N>,
urc_subscription: UrcSubscription<'sub, EdmEvent, URC_CAPACITY, URC_SUBSCRIBERS>,
urc_channel: &'buf AtUrcCh,
_urc_channel: &'buf AtUrcCh,
}

impl<'buf, 'sub, W, RST, const INGRESS_BUF_SIZE: usize, const N: usize, const L: usize>
Expand Down Expand Up @@ -215,8 +214,8 @@ where
AtUrcCh: AtatUrcChannel<EdmEvent, URC_CAPACITY, URC_SUBSCRIBERS>,
RST: OutputPin,
{
pub fn new(client: AtCl, urc_channel: &'buf AtUrcCh, config: Config<RST>) -> Self {
let urc_subscription = urc_channel.subscribe().unwrap();
pub fn new(client: AtCl, _urc_channel: &'buf AtUrcCh, config: Config<RST>) -> Self {
let urc_subscription = _urc_channel.subscribe().unwrap();
UbloxClient {
module_started: false,
initialized: false,
Expand All @@ -231,7 +230,7 @@ where
socket_map: SocketMap::default(),
udp_listener: UdpListener::new(),
urc_subscription,
urc_channel,
_urc_channel,
}
}
}
Expand Down Expand Up @@ -337,7 +336,6 @@ where
}

self.initialized = true;
self.supplicant::<10>()?.init()?;

Ok(())
}
Expand Down Expand Up @@ -624,7 +622,6 @@ where
mode: WifiMode::Station,
},
WiFiState::Connected,
255,
).activate()
);
}
Expand Down Expand Up @@ -860,20 +857,8 @@ where
}
}

pub fn supplicant<const M: usize>(&mut self) -> Result<Supplicant<AtCl, M>, Error> {
// TODO: better solution
if !self.initialized {
return Err(Error::Uninitialized);
}

Ok(Supplicant {
client: &mut self.client,
wifi_connection: &mut self.wifi_connection,
active_on_startup: &mut self.wifi_config_active_on_startup,
})
}
/// Is the module attached to a WiFi and ready to open sockets
pub fn connected_to_network(&self) -> Result<(), Error> {
pub(crate) fn connected_to_network(&self) -> Result<(), Error> {
if let Some(ref con) = self.wifi_connection {
if !self.initialized {
Err(Error::Uninitialized)
Expand Down
79 changes: 73 additions & 6 deletions ublox-short-range/src/command/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,81 @@ use super::{NoResponse, OnOff};
/// This command is used to configure up to 10 different Wi-Fi networks. After configuring a network, it must be
/// activated (Wi-Fi Station Configuration Action +UWSCA) before use.
/// If more than one configuration has active on start up parameter enabled, the behaviour is undefined.
#[derive(Clone, AtatCmd)]
#[at_cmd("+UWSC", NoResponse, timeout_ms = 1000)]
pub struct SetWifiStationConfig {
#[derive(Clone)]
// #[at_cmd("+UWSC", NoResponse, timeout_ms = 1000)]
pub struct SetWifiStationConfig<'a> {
/// Wi-Fi configuration id. 0-9
#[at_arg(position = 0)]
// #[at_arg(position = 0)]
pub config_id: u8,
#[at_arg(position = 1)]
pub config_param: WifiStationConfig,
// #[at_arg(position = 1)]
pub config_param: WifiStationConfig<'a>,
}

// FIXME:
#[automatically_derived]
impl<'a> atat::AtatLen for SetWifiStationConfig<'a> {
const LEN: usize =
<WifiStationConfig<'a> as atat::AtatLen>::LEN + <u8 as atat::AtatLen>::LEN + 1usize;
}
const ATAT_SETWIFISTATIONCONFIG_LEN: usize =
<WifiStationConfig<'_> as atat::AtatLen>::LEN + <u8 as atat::AtatLen>::LEN + 1usize;
#[automatically_derived]
impl<'a> atat::AtatCmd<{ ATAT_SETWIFISTATIONCONFIG_LEN + 12usize }> for SetWifiStationConfig<'a> {
type Response = NoResponse;
const MAX_TIMEOUT_MS: u32 = 1000u32;
#[inline]
fn as_bytes(&self) -> atat::heapless::Vec<u8, { ATAT_SETWIFISTATIONCONFIG_LEN + 12usize }> {
match atat::serde_at::to_vec(
self,
"+UWSC",
atat::serde_at::SerializeOptions {
value_sep: true,
cmd_prefix: "AT",
termination: "\r\n",
quote_escape_strings: true,
},
) {
Ok(s) => s,
Err(_) => panic!("Failed to serialize command"),
}
}
#[inline]
fn parse(
&self,
res: Result<&[u8], atat::InternalError>,
) -> core::result::Result<Self::Response, atat::Error> {
match res {
Ok(resp) => {
atat::serde_at::from_slice::<NoResponse>(resp).map_err(|e| atat::Error::Parse)
}
Err(e) => Err(e.into()),
}
}
}
#[automatically_derived]
impl<'a> atat::serde_at::serde::Serialize for SetWifiStationConfig<'a> {
#[inline]
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: atat::serde_at::serde::Serializer,
{
let mut serde_state = atat::serde_at::serde::Serializer::serialize_struct(
serializer,
"SetWifiStationConfig",
2usize,
)?;
atat::serde_at::serde::ser::SerializeStruct::serialize_field(
&mut serde_state,
"config_id",
&self.config_id,
)?;
atat::serde_at::serde::ser::SerializeStruct::serialize_field(
&mut serde_state,
"config_param",
&self.config_param,
)?;
atat::serde_at::serde::ser::SerializeStruct::end(serde_state)
}
}

/// 7.1 Wi-Fi station configuration +UWSC
Expand Down
30 changes: 15 additions & 15 deletions ublox-short-range/src/command/wifi/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub enum WifiStationConfigParameter {
}

#[derive(Clone, PartialEq, AtatEnum)]
pub enum WifiStationConfig {
pub enum WifiStationConfig<'a> {
/// <param_val1> decides if the station is active on start up.
/// - Off (default): Inactive
/// - On: active
Expand All @@ -127,7 +127,7 @@ pub enum WifiStationConfig {
/// SSID - <param_val1> is the Service Set Identifier. The factory default
/// value is an empty string ("").
#[at_arg(value = 2)]
SSID(String<64>),
SSID(#[at_arg(len = 64)] &'a str),
/// Authentication - <param_val> is the authentication type.
/// - 1 (default): Open
/// - 2: WPA/WPA2 PSK
Expand All @@ -146,11 +146,11 @@ pub enum WifiStationConfig {
/// Authentication " is supported.
#[at_arg(value = 6)]
WEPKeys(
String<13>,
Option<String<13>>,
Option<String<13>>,
Option<String<13>>,
Option<String<13>>,
#[at_arg(len = 13)] &'a str,
#[at_arg(len = 13)] Option<&'a str>,
#[at_arg(len = 13)] Option<&'a str>,
#[at_arg(len = 13)] Option<&'a str>,
#[at_arg(len = 13)] Option<&'a str>,
),
/// Active Key - <param_val1> is the WEP active TX key (factory default 0
/// means that Open authentication with WEP encryption is disabled). Range
Expand All @@ -160,38 +160,38 @@ pub enum WifiStationConfig {
/// PSK/Passphrase - <param_val1> is the PSK (32 HEX values) or Passphrase
/// (8-63 ASCII characters as a string) for WPA/WPA2 PSK.
#[at_arg(value = 8)]
WpaPskOrPassphrase(String<64>),
WpaPskOrPassphrase(#[at_arg(len = 64)] &'a str),
/// Password - <param_val1> is the password for LEAP and PEAP; string with a
/// maximum length of 31.
#[at_arg(value = 9)]
EAPPassword(String<31>),
EAPPassword(#[at_arg(len = 31)] &'a str),
/// User name - <param_val1> is the public user name for LEAP and PEAP;
/// string with a maximum length of 31.
#[at_arg(value = 10)]
UserName(String<31>),
UserName(#[at_arg(len = 31)] &'a str),
/// Domain name - <param_val1> is the public domain name for LEAP and PEAP;
/// string with a maximum length of 63. The domain name is an optional
/// parameter.
#[at_arg(value = 11)]
DomainName(String<63>),
DomainName(#[at_arg(len = 63)] &'a str),
/// Client certificate name - <param_val1> is the internal client
/// certificate name for EAP-TLS as defined in the SSL/TLS certificates and
/// private keys manager +USECMNG command; string with a maximum length of
/// 32. Supported software versions 4.0.0 onwards
#[at_arg(value = 12)]
ClientCertificateName(String<32>),
ClientCertificateName(#[at_arg(len = 32)] &'a str),
/// Client private key - <param_val1> is the internal client private key
/// name for EAP- TLS as defined in the SSL/TLS certificates and private
/// keys manager +USECMNG command; string with a maximum length of 32.
/// Supported software versions 4.0.0 onwards
#[at_arg(value = 13)]
ClientPrivateKey(String<32>),
ClientPrivateKey(#[at_arg(len = 32)] &'a str),
/// CA certificate name - <param_val1> is the internal CA certificate name
/// for EAP- TLS as defined in the SSL/TLS certificates and private keys
/// manager +USECMNG command; string with a maximum length of 32. Supported
/// software versions 5.0.0 onwards
#[at_arg(value = 14)]
CACertificateName(String<32>),
CACertificateName(#[at_arg(len = 32)] &'a str),
/// Validate CA certificate. The default value is On; Setting this value to
/// Off means no CA Certificate validation has been done. For example
/// at+uwsc=0,15,0 would mean that the server CA Certificate is not
Expand Down Expand Up @@ -426,7 +426,7 @@ pub enum WifiStationAction {
Deactivate = 4,
}

#[derive(Debug, Clone, PartialEq, AtatEnum)]
#[derive(Debug, Clone, PartialEq, AtatEnum, defmt::Format)]
#[repr(u8)]
pub enum OperationMode {
Infrastructure = 1,
Expand Down
1 change: 0 additions & 1 deletion ublox-short-range/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub enum Error {
Unimplemented,
SocketMemory,
SocketMapMemory,
Supplicant,
Timeout,
ShadowStoreBug,
_Unknown,
Expand Down
1 change: 0 additions & 1 deletion ublox-short-range/src/wifi/ap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ where
mode: WifiMode::AccessPoint,
},
WiFiState::NotConnected,
ap_config_id as u8,
)
.activate(),
);
Expand Down
6 changes: 2 additions & 4 deletions ublox-short-range/src/wifi/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,22 @@ pub enum NetworkState {
}

// Fold into wifi connectivity
#[derive(defmt::Format)]
pub struct WifiConnection {
/// Keeps track of connection state on module
pub wifi_state: WiFiState,
pub network_state: NetworkState,
pub network: WifiNetwork,
/// Numbre from 0-9. 255 used for unknown
pub config_id: u8,
/// Keeps track of activation of the config by driver
pub activated: bool,
}

impl WifiConnection {
pub(crate) fn new(network: WifiNetwork, wifi_state: WiFiState, config_id: u8) -> Self {
pub(crate) fn new(network: WifiNetwork, wifi_state: WiFiState) -> Self {
WifiConnection {
wifi_state,
network_state: NetworkState::Unattached,
network,
config_id,
activated: false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion ublox-short-range/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod connection;
pub mod dns;
pub mod network;
pub mod options;
pub mod supplicant;
pub mod sta;
pub mod tls;

pub mod peer_builder;
Expand Down
5 changes: 3 additions & 2 deletions ublox-short-range/src/wifi/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use heapless::String;

use core::convert::TryFrom;

#[derive(PartialEq, Debug)]
#[derive(PartialEq, Debug, defmt::Format)]
pub enum WifiMode {
Station,
AccessPoint,
}

#[derive(Debug)]
#[derive(Debug, defmt::Format)]
pub struct WifiNetwork {
#[defmt(Debug2Format)]
pub bssid: Bytes<20>,
pub op_mode: OperationMode,
pub ssid: String<64>,
Expand Down
5 changes: 4 additions & 1 deletion ublox-short-range/src/wifi/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ impl HotspotOptions {
}
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Default, Serialize, Deserialize, defmt::Format)]
pub struct ConnectionOptions {
pub ssid: String<64>,
pub password: Option<String<64>>,

#[defmt(Debug2Format)]
pub ip: Option<Ipv4Addr>,
#[defmt(Debug2Format)]
pub subnet: Option<Ipv4Addr>,
#[defmt(Debug2Format)]
pub gateway: Option<Ipv4Addr>,
}

Expand Down
Loading

0 comments on commit 3b8f1a0

Please sign in to comment.