Skip to content

Commit

Permalink
add responsiveness with threading
Browse files Browse the repository at this point in the history
  • Loading branch information
indexds committed Dec 27, 2024
1 parent f884d59 commit d66ac8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/http/wg_routes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::{Arc, Mutex};
use std::thread;

use anyhow::Error;
use esp_idf_svc::http::server::{EspHttpServer, Method};
Expand All @@ -18,9 +19,6 @@ pub fn set_routes(
let nvs_set = Arc::clone(&nvs);
let wifi_check = Arc::clone(&wifi);

let nvs_wg = Arc::clone(&nvs);
let wifi_sync = Arc::clone(&wifi);

move |mut request| {
let wifi_check = wifi_check.lock().unwrap();

Expand Down Expand Up @@ -53,8 +51,13 @@ pub fn set_routes(

drop(nvs_set);

wireguard::sync_sntp(Arc::clone(&wifi_sync))?;
wireguard::start_wg_tunnel(Arc::clone(&nvs_wg))?;
let wifi = Arc::clone(&wifi);
let nvs = Arc::clone(&nvs);

thread::spawn(move || {
_ = wireguard::sync_sntp(Arc::clone(&wifi));
_ = wireguard::start_wg_tunnel(Arc::clone(&nvs));
});

let connection = request.connection();

Expand All @@ -66,7 +69,9 @@ pub fn set_routes(

// Handler to disconnect from the wireguard peer
http_server.fn_handler("/disconnect-wg", Method::Post, move |mut request| {
wireguard::end_wg_tunnel()?;
thread::spawn(|| {
_ = wireguard::end_wg_tunnel();
});

let connection = request.connection();

Expand All @@ -76,6 +81,7 @@ pub fn set_routes(
})?;

// Handler to get current wireguard status (connected/disconnected)
// TODO! FIX THIS CALLBACK
http_server.fn_handler("/wg-status", Method::Get, move |mut request| {
let connection = request.connection();

Expand Down
22 changes: 16 additions & 6 deletions src/http/wifi_routes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::{Arc, Mutex};
use std::thread;

use anyhow::Error;
use esp_idf_svc::http::server::{EspHttpServer, Method};
Expand All @@ -16,6 +17,7 @@ pub fn set_routes(
// Handler to disconnect from wifi
http_server.fn_handler("/disconnect-wifi", Method::Get, {
let wifi = Arc::clone(&wifi);

move |mut request| {
wifi::disconnect(Arc::clone(&wifi))?;

Expand All @@ -29,10 +31,10 @@ pub fn set_routes(

// Handler to connect to wifi
http_server.fn_handler("/connect-wifi", Method::Post, {
let nvs = Arc::clone(&nvs);
let nvs_set = Arc::clone(&nvs);
let wifi = Arc::clone(&wifi);

move |mut request| {
let mut nvs_set = nvs.lock().unwrap();
let mut body = Vec::new();
let mut buffer = [0_u8; 128];

Expand All @@ -44,18 +46,24 @@ pub fn set_routes(
}
}

let form_data = String::from_utf8(body)?;
let wifi_conf: NvsWifi = serde_urlencoded::from_str(form_data.as_str())?;
let wifi_conf: NvsWifi = serde_urlencoded::from_str(String::from_utf8(body)?.as_str())?;

let mut nvs_set = nvs_set.lock().unwrap();

NvsWifi::set_field(&mut nvs_set, NvsKeys::STA_SSID, wifi_conf.sta_ssid.clean_string().as_str())?;
NvsWifi::set_field(&mut nvs_set, NvsKeys::STA_PASSWD, wifi_conf.sta_passwd.clean_string().as_str())?;
NvsWifi::set_field(&mut nvs_set, NvsKeys::STA_AUTH, wifi_conf.sta_auth.clean_string().as_str())?;

drop(nvs_set);

wifi::set_configuration(Arc::clone(&nvs), Arc::clone(&wifi))?;
wifi::connect(Arc::clone(&wifi))?;

let nvs_thread = Arc::clone(&nvs);
let wifi = Arc::clone(&wifi);

thread::spawn(move || {
_ = wifi::set_configuration(Arc::clone(&nvs_thread), Arc::clone(&wifi));
_ = wifi::connect(Arc::clone(&wifi));
});
let connection = request.connection();

connection.initiate_response(204, Some("OK"), &[("Content-Type", "text/html")])?;
Expand All @@ -67,6 +75,7 @@ pub fn set_routes(
// Handler to get available wifis
http_server.fn_handler("/wifi", Method::Get, {
let wifi = Arc::clone(&wifi);

move |request| {
let mut wifi = wifi.lock().unwrap();

Expand Down Expand Up @@ -165,6 +174,7 @@ pub fn set_routes(
// Handler to get current wifi status (connected/disconnected)
http_server.fn_handler("/wifi-status", Method::Get, {
let wifi = Arc::clone(&wifi);

move |mut request| {
let wifi = wifi.lock().unwrap();

Expand Down

0 comments on commit d66ac8c

Please sign in to comment.