Skip to content

Commit b7d6cc4

Browse files
committed
Update example wifi-info
1 parent c26de0a commit b7d6cc4

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

ctru-rs/examples/wifi-info.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
//!
33
//! This example prints out all the info about the console's network, like SSID, security, proxy info...
44
5-
use ctru::{prelude::*, services::ac::Ac};
5+
use ctru::{
6+
prelude::*,
7+
services::ac::{Ac, NetworkStatus},
8+
};
9+
use std::error::Error;
610

711
fn main() {
812
let gfx = Gfx::new().expect("Couldn't obtain GFX controller");
913
let mut hid = Hid::new().expect("Couldn't obtain HID controller");
1014
let apt = Apt::new().expect("Couldn't obtain APT controller");
1115

16+
ctru::set_panic_hook(true);
17+
1218
let _console = Console::new(gfx.top_screen.borrow_mut());
1319

14-
let mut ac = Ac::new().expect("Couldn't get an AC handle");
20+
let ac = Ac::new().expect("Couldn't get an AC handle");
1521

1622
print_network_info(&ac).expect("Error while gathering network info");
1723
println!("Press START to exit.");
@@ -27,21 +33,32 @@ fn main() {
2733
}
2834
}
2935

30-
fn print_network_info(ac: &Ac) -> ctru::Result<()> {
31-
let connected = ac.get_wifi_status()?;
32-
println!("Wi-Fi status: {:?}", connected);
36+
fn print_network_info(ac: &Ac) -> Result<(), Box<dyn Error>> {
37+
let status = ac.wifi_status()?;
38+
println!("Wi-Fi status: {:?}", status);
3339

3440
// Some methods error out if the console isn't connected
35-
if connected {
36-
println!("Wi-Fi SSID: {}", ac.get_wifi_ssid()?);
37-
println!("Wi-Fi security: {:?}", ac.get_wifi_security()?);
38-
let proxied = ac.get_proxy_enabled()?;
41+
if matches!(
42+
status,
43+
NetworkStatus::WANConnected | NetworkStatus::LANConnected
44+
) {
45+
println!("Wi-Fi SSID: {}", String::from_utf8(ac.wifi_ssid()?)?);
46+
println!("Wi-Fi security: {:?}", ac.wifi_security()?);
47+
let proxied = ac.proxy_enabled()?;
3948
println!("Proxy enabled: {}", proxied);
4049
if proxied {
41-
println!("Proxy port: {}", ac.get_proxy_port()?);
42-
println!("Proxy username: {}", ac.get_proxy_username()?);
43-
println!("Proxy password: {}", ac.get_proxy_password()?);
50+
println!("Proxy port: {}", ac.proxy_port()?);
51+
println!(
52+
"Proxy username: {}",
53+
String::from_utf8(ac.proxy_username()?)?
54+
);
55+
println!(
56+
"Proxy password: {}",
57+
String::from_utf8(ac.proxy_password()?)?
58+
);
4459
}
60+
} else {
61+
println!("Not connected to any network.")
4562
}
4663

4764
Ok(())

0 commit comments

Comments
 (0)