Browse Source

add more docs and fix camel case warning

pull/140/head
Lena 1 year ago committed by Lena
parent
commit
649103b9ca
No known key found for this signature in database
GPG Key ID: 5A940B96C2DA3683
  1. 2
      ctru-rs/examples/wifi-info.rs
  2. 10
      ctru-rs/src/error.rs
  3. 74
      ctru-rs/src/services/ac.rs

2
ctru-rs/examples/wifi-info.rs

@ -29,7 +29,7 @@ fn main() {
fn print_network_info(ac: &Ac) -> ctru::Result<()> { fn print_network_info(ac: &Ac) -> ctru::Result<()> {
let connected = ac.get_wifi_status()?; 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 // Some methods error out if the console isn't connected
if connected { if connected {

10
ctru-rs/src/error.rs

@ -96,8 +96,6 @@ pub enum Error {
}, },
/// An error that doesn't fit into the other categories. /// An error that doesn't fit into the other categories.
Other(String), Other(String),
/// UTF-8 buffer to [String] conversion error
Utf8(std::string::FromUtf8Error),
} }
impl Error { impl Error {
@ -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 { impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
@ -165,7 +157,6 @@ impl fmt::Debug for Error {
.field("wanted", wanted) .field("wanted", wanted)
.finish(), .finish(),
Self::Other(err) => f.debug_tuple("Other").field(err).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::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::Other(err) => write!(f, "{err}"),
Self::Utf8(e) => write!(f, "UTF-8 conversion error: {e}")
} }
} }
} }

74
ctru-rs/src/services/ac.rs

@ -32,7 +32,7 @@ impl Ac {
} }
} }
/// Waits for an internet connection /// Waits for an internet connection.
/// ///
/// # Example /// # Example
/// ///
@ -61,7 +61,7 @@ impl Ac {
} }
} }
/// Returns whether the console is connected to Wi-Fi /// Returns the current Wi-Fi connection status.
/// ///
/// # Example /// # Example
/// ///
@ -74,23 +74,32 @@ impl Ac {
/// ///
/// let ac = Ac::new()?; /// let ac = Ac::new()?;
/// ///
/// println!("Wi-Fi connected: {}", ac.get_wifi_status()?); /// println!("Wi-Fi status: {:?}", ac.get_wifi_status()?);
/// # /// #
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
#[doc(alias = "ACU_GetWifiStatus")] #[doc(alias = "ACU_GetWifiStatus")]
pub fn get_wifi_status(&self) -> crate::Result<bool> { pub fn get_wifi_status(&self) -> crate::Result<NetworkStatus> {
unsafe { unsafe {
let mut ret = 0u32; let mut ret = 0u32;
ResultCode(ctru_sys::ACU_GetStatus(&mut ret))?; 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 /// # Example
/// ///
/// ``` /// ```
@ -102,6 +111,10 @@ impl Ac {
/// ///
/// let ac = Ac::new()?; /// let ac = Ac::new()?;
/// ///
/// if ac.get_wifi_status()? == NetworkStatus::WANConnected {
/// println!("Network security: {:?}", ac.get_wifi_security()?);
/// }
///
/// # /// #
/// # Ok(()) /// # Ok(())
/// # } /// # }
@ -113,13 +126,27 @@ impl Ac {
ResultCode(ctru_sys::ACU_GetSecurityMode(&mut ret))?; ResultCode(ctru_sys::ACU_GetSecurityMode(&mut ret))?;
// fix this, for some reason the bindings have the type as a struct and not enum // fix this, for some reason the bindings have the type as a struct and not enum
// and so i can't impl TryFrom automatically // 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. /// 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 /// # Example
/// ///
@ -145,7 +172,6 @@ impl Ac {
// we don't really need space for the terminator // we don't really need space for the terminator
let mut vec = vec![0u8; len as usize]; let mut vec = vec![0u8; len as usize];
ResultCode(ctru_sys::ACU_GetSSID(vec.as_mut_ptr()))?; ResultCode(ctru_sys::ACU_GetSSID(vec.as_mut_ptr()))?;
// how do i handle this error?
Ok(String::from_utf8(vec)?) Ok(String::from_utf8(vec)?)
} }
} }
@ -181,7 +207,7 @@ impl Ac {
/// Returns the connected network's proxy port, if present. /// 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 /// # Example
/// ///
@ -211,7 +237,7 @@ impl Ac {
/// Returns the connected network's proxy username, if present. /// 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 /// # Example
/// ///
@ -236,14 +262,13 @@ impl Ac {
let mut vec = vec![0u8; 0x20]; let mut vec = vec![0u8; 0x20];
ResultCode(ctru_sys::ACU_GetProxyUserName(vec.as_mut_ptr()))?; ResultCode(ctru_sys::ACU_GetProxyUserName(vec.as_mut_ptr()))?;
// how do i handle this error?
Ok(String::from_utf8(vec)?) Ok(String::from_utf8(vec)?)
} }
} }
/// Returns the connected network's proxy password, if present. /// 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 /// # Example
/// ///
@ -267,7 +292,6 @@ impl Ac {
let mut vec = vec![0u8; 0x20]; let mut vec = vec![0u8; 0x20];
ResultCode(ctru_sys::ACU_GetProxyPassword(vec.as_mut_ptr()))?; ResultCode(ctru_sys::ACU_GetProxyPassword(vec.as_mut_ptr()))?;
// how do i handle this error?
Ok(String::from_utf8(vec)?) Ok(String::from_utf8(vec)?)
} }
} }
@ -282,7 +306,7 @@ impl Ac {
/// # use std::error::Error; /// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> { /// # fn main() -> Result<(), Box<dyn Error>> {
/// # /// #
/// use ctru::services::ac::Ac; /// use ctru::services::ac::{Ac, NetworkSlot};
/// ///
/// let ac = Ac::new()?; /// let ac = Ac::new()?;
/// ///
@ -311,6 +335,7 @@ impl Drop for Ac {
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)] #[repr(u32)]
#[non_exhaustive] #[non_exhaustive]
#[allow(non_camel_case_types)]
/// Represents all the supported Wi-Fi security modes. /// Represents all the supported Wi-Fi security modes.
pub enum SecurityMode { pub enum SecurityMode {
/// No authentication /// No authentication
@ -333,10 +358,29 @@ pub enum SecurityMode {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u32)] #[repr(u32)]
/// Represents a network slot, like in the System Settings
pub enum NetworkSlot { pub enum NetworkSlot {
/// The first network slot
First = 0, First = 0,
/// The second network slot
Second = 1, Second = 1,
/// The third network slot
Third = 2, 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); from_impl!(SecurityMode, ctru_sys::acSecurityMode);

Loading…
Cancel
Save