Skip to content

Commit 47e95e9

Browse files
committed
check password before login
1 parent cd02d42 commit 47e95e9

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

src/net/bupt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ impl fmt::Debug for BuptAccount {
2929
}
3030
}
3131

32-
const CHECK_URL: &str = "http://connect.rom.miui.com/generate_204?cmd=redirect&arubalp=12345";
32+
pub const CHECK_URL: &str = "http://connect.rom.miui.com/generate_204?cmd=redirect&arubalp=12345";
3333

34-
enum BuptNetStatus {
34+
pub enum BuptNetStatus {
3535
Authenticated,
3636
NotAuthenticated(Option<String>),
3737
}
3838

39-
fn check(url: impl AsRef<str>) -> Result<BuptNetStatus> {
39+
pub fn check(url: impl AsRef<str>) -> Result<BuptNetStatus> {
4040
log::debug!("checking bupt network status with url: {}", url.as_ref());
4141
let connection = EspHttpConnection::new(&Configuration {
4242
follow_redirects_policy: FollowRedirectsPolicy::FollowNone,

src/net/mod.rs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,31 @@ fn connect_wifi_with_config(
7272
log::info!("Retrying...");
7373
}
7474
}
75-
7675
log::info!("Waiting for DHCP lease...");
7776
wifi.wait_netif_up()?;
7877

7978
let ip_info = wifi.wifi().sta_netif().get_ip_info()?;
8079
log::info!("Wifi DHCP info: {:?}", ip_info);
81-
8280
if let Some(account) = bupt_account {
81+
8382
for retry in 0..10 {
8483
match bupt::login(&account) {
8584
Ok(_) => break,
85+
8686
Err(e) => {
87-
log::warn!(
87+
if e.to_string().contains("incorrect password") {
88+
log::error!("Failed to login to BUPT-portal: {}, retrying with new password",
89+
e);
90+
bail!("Failed to login to BUPT-portal because of wrong password");
91+
//密码错误 重新配网
92+
93+
}
94+
else {
95+
log::warn!(
8896
"Failed to login to BUPT-portal: {}, will retry after 10 seconds...",
8997
e
90-
);
98+
);
99+
}
91100
}
92101
}
93102
delay.delay_ms(1000 * 10);
@@ -139,23 +148,51 @@ pub fn generate_random_mac() -> [u8; 6] {
139148
pub fn connect() -> Result<Box<EspWifi<'static>>> {
140149
set_target_level("wifi", log::LevelFilter::Warn)?;
141150
set_target_level("wifi_init", log::LevelFilter::Warn)?;
142-
143-
#[cfg(feature = "clean_nvs")]
144-
crate::nvs::remove::<NetConfig>()?;
145-
151+
152+
let nvs = crate::nvs::nvs();
146153
match crate::nvs::load::<NetConfig>()? {
147-
Some(config) => {
148-
log::info!("Loaded NetConfig: {:?}", &config);
149-
connect_wifi_with_config(
150-
config,
151-
Peripherals::take()?.modem,
152-
EspSystemEventLoop::take()?,
153-
)
154+
Some(config) => {//有配置
155+
match bupt::check(bupt::CHECK_URL) { //判断是否已经登陆过,如果已经登陆进入第一个块
156+
Ok(bupt::BuptNetStatus::Authenticated) => {
157+
let bupt_wifi = EspWifi::new(Peripherals::take()?.modem, EspSystemEventLoop::take()?.clone(), Some(nvs))?;//bupt—portal wifi配置?
158+
log::info!("BUPT-portal is already authenticated");
159+
Ok(Box::new(bupt_wifi))//返回bupt-portal实例
160+
}
161+
162+
_ => {//未登录进入第二个块
163+
log::info!("Loaded NetConfig: {:?}", &config);
164+
let wifi_result = connect_wifi_with_config(//用已经储存的用户名密码登录,如果密码修改,无法登录,需要重新进行配网
165+
config,
166+
Peripherals::take()?.modem,
167+
EspSystemEventLoop::take()?,
168+
);
169+
check_error_and_reconnect(wifi_result)
170+
171+
}
172+
}
154173
}
155-
None => {
174+
None => {//没有配置
156175
let p = provisioning::Provisioner::new()?;
157176
p.wait();
158177
Ok(p.wifi)
159178
}
160179
}
161180
}
181+
182+
fn check_error_and_reconnect(result: Result<Box<EspWifi<'static>>>) -> Result<Box<EspWifi<'static>>> {
183+
match result {
184+
Ok(wifi) => Ok(wifi),
185+
Err(e) => {
186+
if e.to_string().contains("Failed to login to BUPT-portal because of wrong password") {
187+
// 密码错误,读取到返回的错误消息中特定报错,当做没有配置,重新进行配网
188+
log::info!("Re-provisioning the network");
189+
let new_config = provisioning::Provisioner::new()?;
190+
new_config.wait();
191+
Ok(new_config.wifi)
192+
} else {
193+
// 其他错误,直接返回原始错误
194+
Err(e)
195+
}
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)