From 649103b9ca3005856beeca6422e3dafb73775e14 Mon Sep 17 00:00:00 2001 From: Lena Date: Sat, 25 Nov 2023 12:18:17 +0100 Subject: [PATCH] add more docs and fix camel case warning --- ctru-rs/examples/wifi-info.rs | 2 +- ctru-rs/src/error.rs | 10 ----- ctru-rs/src/services/ac.rs | 74 ++++++++++++++++++++++++++++------- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/ctru-rs/examples/wifi-info.rs b/ctru-rs/examples/wifi-info.rs index 807ca19..176cd19 100644 --- a/ctru-rs/examples/wifi-info.rs +++ b/ctru-rs/examples/wifi-info.rs @@ -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 { diff --git a/ctru-rs/src/error.rs b/ctru-rs/src/error.rs index b409318..0be67b7 100644 --- a/ctru-rs/src/error.rs +++ b/ctru-rs/src/error.rs @@ -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 { @@ -139,12 +137,6 @@ impl From for Error { } } -impl From 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 { @@ -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(), } } } @@ -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}") } } } diff --git a/ctru-rs/src/services/ac.rs b/ctru-rs/src/services/ac.rs index 433ac47..0d4ba8c 100644 --- a/ctru-rs/src/services/ac.rs +++ b/ctru-rs/src/services/ac.rs @@ -32,7 +32,7 @@ impl Ac { } } - /// Waits for an internet connection + /// Waits for an internet connection. /// /// # Example /// @@ -61,7 +61,7 @@ impl Ac { } } - /// Returns whether the console is connected to Wi-Fi + /// Returns the current Wi-Fi connection status. /// /// # Example /// @@ -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 { + pub fn get_wifi_status(&self) -> crate::Result { 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 /// /// ``` @@ -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(()) /// # } @@ -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 /// @@ -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)?) } } @@ -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 /// @@ -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 /// @@ -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 /// @@ -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)?) } } @@ -282,7 +306,7 @@ impl Ac { /// # use std::error::Error; /// # fn main() -> Result<(), Box> { /// # - /// use ctru::services::ac::Ac; + /// use ctru::services::ac::{Ac, NetworkSlot}; /// /// let ac = Ac::new()?; /// @@ -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 @@ -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);