Skip to content

Commit

Permalink
add more docs and fix camel case warning
Browse files Browse the repository at this point in the history
  • Loading branch information
adryzz committed Mar 28, 2024
1 parent b2f8aef commit 649103b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
2 changes: 1 addition & 1 deletion ctru-rs/examples/wifi-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {

fn print_network_info(ac: &Ac) -> ctru::Result<()> {
let connected = ac.get_wifi_status()?;
println!("Wi-Fi connected: {}", connected);
println!("Wi-Fi status: {:?}", connected);

// Some methods error out if the console isn't connected
if connected {
Expand Down
10 changes: 0 additions & 10 deletions ctru-rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ pub enum Error {
},
/// An error that doesn't fit into the other categories.
Other(String),
/// UTF-8 buffer to [String] conversion error
Utf8(std::string::FromUtf8Error),
}

impl Error {
Expand Down Expand Up @@ -139,12 +137,6 @@ impl From<ResultCode> for Error {
}
}

impl From<std::string::FromUtf8Error> for Error {
fn from(err: std::string::FromUtf8Error) -> Self {
Error::Utf8(err)
}
}

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand All @@ -165,7 +157,6 @@ impl fmt::Debug for Error {
.field("wanted", wanted)
.finish(),
Self::Other(err) => f.debug_tuple("Other").field(err).finish(),
Self::Utf8(e) => f.debug_tuple("Utf8").field(e).finish(),
}
}
}
Expand All @@ -190,7 +181,6 @@ impl fmt::Display for Error {
}
Self::BufferTooShort{provided, wanted} => write!(f, "the provided buffer's length is too short (length = {provided}) to hold the wanted data (size = {wanted})"),
Self::Other(err) => write!(f, "{err}"),
Self::Utf8(e) => write!(f, "UTF-8 conversion error: {e}")
}
}
}
Expand Down
74 changes: 59 additions & 15 deletions ctru-rs/src/services/ac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Ac {
}
}

/// Waits for an internet connection
/// Waits for an internet connection.
///
/// # Example
///
Expand Down Expand Up @@ -61,7 +61,7 @@ impl Ac {
}
}

/// Returns whether the console is connected to Wi-Fi
/// Returns the current Wi-Fi connection status.
///
/// # Example
///
Expand All @@ -74,23 +74,32 @@ impl Ac {
///
/// let ac = Ac::new()?;
///
/// println!("Wi-Fi connected: {}", ac.get_wifi_status()?);
/// println!("Wi-Fi status: {:?}", ac.get_wifi_status()?);
/// #
/// # Ok(())
/// # }
/// ```
#[doc(alias = "ACU_GetWifiStatus")]
pub fn get_wifi_status(&self) -> crate::Result<bool> {
pub fn get_wifi_status(&self) -> crate::Result<NetworkStatus> {
unsafe {
let mut ret = 0u32;
ResultCode(ctru_sys::ACU_GetStatus(&mut ret))?;

Ok(ret == 3)
Ok(
match ret {
0 => NetworkStatus::None,
1 => NetworkStatus::Idle,
2 => NetworkStatus::LANConnected,
3 => NetworkStatus::WANConnected,
_ => Err(crate::Error::Other(format!("Unknown value {}", ret)))
}
)
}
}

/// Returns whether the console is connected to Wi-Fi
/// Returns the [`SecurityMode`] of the currently connected network, or error if the console isn't connected to any network.
///
/// You can check if the console is connected to a network using [`Ac::get_wifi_status()`].
/// # Example
///
/// ```
Expand All @@ -102,6 +111,10 @@ impl Ac {
///
/// let ac = Ac::new()?;
///
/// if ac.get_wifi_status()? == NetworkStatus::WANConnected {
/// println!("Network security: {:?}", ac.get_wifi_security()?);
/// }
///
/// #
/// # Ok(())
/// # }
Expand All @@ -113,13 +126,27 @@ impl Ac {
ResultCode(ctru_sys::ACU_GetSecurityMode(&mut ret))?;
// fix this, for some reason the bindings have the type as a struct and not enum
// and so i can't impl TryFrom automatically
Ok(std::mem::transmute(ret))
Ok(match ret {
0 => SecurityMode::Open,

1 => SecurityMode::WEP40Bit,
2 => SecurityMode::WEP104Bit,
3 => SecurityMode::WEP128Bit,

4 => SecurityMode::WPA_TKIP,
5 => SecurityMode::WPA2_TKIP,

6 => SecurityMode::WPA_AES,
7 => SecurityMode::WPA2_AES,

_ => Err(crate::Error::Other(format!("Unknown value {}", ret)))
})
}
}

/// Returns the SSID of the Wi-Fi network the console is connected to, or error if the console isn't connected to any network.
///
/// You can check if the console is connected to a network using [`Self::get_wifi_status()`]
/// You can check if the console is connected to a network using [`Ac::get_wifi_status()`].
///
/// # Example
///
Expand All @@ -145,7 +172,6 @@ impl Ac {
// we don't really need space for the terminator
let mut vec = vec![0u8; len as usize];
ResultCode(ctru_sys::ACU_GetSSID(vec.as_mut_ptr()))?;
// how do i handle this error?
Ok(String::from_utf8(vec)?)
}
}
Expand Down Expand Up @@ -181,7 +207,7 @@ impl Ac {

/// Returns the connected network's proxy port, if present.
///
/// You can check if the console is using a proxy with [`Self::get_proxy_enabled()`]
/// You can check if the console is using a proxy with [`Ac::get_proxy_enabled()`]
///
/// # Example
///
Expand Down Expand Up @@ -211,7 +237,7 @@ impl Ac {

/// Returns the connected network's proxy username, if present.
///
/// You can check if the console is using a proxy with [`Self::get_proxy_enabled()`]
/// You can check if the console is using a proxy with [`Ac::get_proxy_enabled()`]
///
/// # Example
///
Expand All @@ -236,14 +262,13 @@ impl Ac {
let mut vec = vec![0u8; 0x20];
ResultCode(ctru_sys::ACU_GetProxyUserName(vec.as_mut_ptr()))?;

// how do i handle this error?
Ok(String::from_utf8(vec)?)
}
}

/// Returns the connected network's proxy password, if present.
///
/// You can check if the console is using a proxy with [`Self::get_proxy_enabled()`]
/// You can check if the console is using a proxy with [`Ac::get_proxy_enabled()`]
///
/// # Example
///
Expand All @@ -267,7 +292,6 @@ impl Ac {
let mut vec = vec![0u8; 0x20];
ResultCode(ctru_sys::ACU_GetProxyPassword(vec.as_mut_ptr()))?;

// how do i handle this error?
Ok(String::from_utf8(vec)?)
}
}
Expand All @@ -282,7 +306,7 @@ impl Ac {
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// #
/// use ctru::services::ac::Ac;
/// use ctru::services::ac::{Ac, NetworkSlot};
///
/// let ac = Ac::new()?;
///
Expand Down Expand Up @@ -311,6 +335,7 @@ impl Drop for Ac {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
#[non_exhaustive]
#[allow(non_camel_case_types)]
/// Represents all the supported Wi-Fi security modes.
pub enum SecurityMode {
/// No authentication
Expand All @@ -333,10 +358,29 @@ pub enum SecurityMode {

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u32)]
/// Represents a network slot, like in the System Settings
pub enum NetworkSlot {
/// The first network slot
First = 0,
/// The second network slot
Second = 1,
/// The third network slot
Third = 2,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
#[non_exhaustive]
/// Represents the current Wi-Fi status
pub enum NetworkStatus {
/// Wi-Fi turned off
None = 0,
/// Not connected
Idle = 1,
/// Connected, only LAN.
LANConnected = 2,
/// Connected to the Internet.
WANConnected = 3
}

from_impl!(SecurityMode, ctru_sys::acSecurityMode);

0 comments on commit 649103b

Please sign in to comment.