Skip to content

Commit

Permalink
Fix deadlock bug
Browse files Browse the repository at this point in the history
  • Loading branch information
360tetsu360 committed May 4, 2024
1 parent 9071e84 commit d36457a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 65 deletions.
4 changes: 2 additions & 2 deletions atom-skygaze/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ pub struct NetworkConfig {
pub psk: String,
}


pub async fn load_atomconf() -> std::io::Result<AtomConfig> {
let content = tokio::fs::read_to_string("/media/mmc/atom_config.toml").await?;
let atomconf: AtomConfig = toml::from_str(&content).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
let atomconf: AtomConfig = toml::from_str(&content)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
Ok(atomconf)
}

Expand Down
2 changes: 1 addition & 1 deletion atom-skygaze/src/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub unsafe fn start(

diff_list.push_back(diff);

if diff_list.len() == (app_state_tmp.fps / 5) as usize {
if diff_list.len() > (app_state_tmp.fps / 5) as usize {
let mut diff_buff = composite(&mut diff_list);
let integrated_diff = Mat::new_rows_cols_with_data_def(
img_height as i32,
Expand Down
10 changes: 6 additions & 4 deletions atom-skygaze/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::config::load_from_file;
use crate::config::save_to_file;
use crate::config::*;
use crate::detection::*;
use crate::download::download_file;
use crate::gpio::*;
Expand All @@ -9,7 +10,6 @@ use crate::osd::*;
use crate::record::*;
use crate::websocket::*;
use crate::webstream::*;
use crate::config::*;
use axum::routing::get;
use axum::Router;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -107,7 +107,7 @@ async fn main() {
ap_mode: false,
ssid: "".to_owned(),
psk: "".to_owned(),
}
},
};
save_atomconf(atomconf.clone()).await.unwrap();
atomconf
Expand Down Expand Up @@ -155,6 +155,7 @@ async fn main() {

if app_state.led_on {
if !app_state.detect {
drop(app_state);
if blue_on {
led_off(LEDType::Blue).unwrap();
led_on(LEDType::Orange).unwrap();
Expand All @@ -165,14 +166,15 @@ async fn main() {

blue_on = !blue_on;
} else {
drop(app_state);
led_on(LEDType::Blue).unwrap();
led_off(LEDType::Orange).unwrap();
}
} else {
drop(app_state);
led_off(LEDType::Blue).unwrap();
led_off(LEDType::Orange).unwrap();
}
drop(app_state);

std::thread::sleep(std::time::Duration::from_millis(500));
}
Expand Down Expand Up @@ -216,7 +218,7 @@ async fn main() {
thread::Builder::new()
.name("led_loop".to_string())
.spawn(move || {
start(app_state_common_instance3, detected_tx, logtx, flag4);
start(app_state_common_instance3, detected_tx, logtx, flag4);
})
.unwrap();
};
Expand Down
1 change: 0 additions & 1 deletion atom-skygaze/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ unsafe fn get_h264_stream(
return false;
}


while queue.len() > 76 {
queue.pop_front();
}
Expand Down
144 changes: 87 additions & 57 deletions atom-skygaze/src/websocket.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::config::save_to_file;
use crate::config::AtomConfig;
use crate::gpio::*;
use crate::system;
use crate::AppState;
use crate::config::AtomConfig;
use axum::extract::{
ws::{Message, WebSocket},
State, WebSocketUpgrade,
Expand Down Expand Up @@ -33,7 +33,9 @@ pub async fn handler(
ws: WebSocketUpgrade,
State((rx, app_state, atom_conf, log_rx, flag)): AppStateWs,
) -> impl IntoResponse {
ws.on_upgrade(move |socket: WebSocket| handle_socket(socket, rx, app_state, atom_conf, log_rx, flag))
ws.on_upgrade(move |socket: WebSocket| {
handle_socket(socket, rx, app_state, atom_conf, log_rx, flag)
})
}

pub async fn handle_socket(
Expand All @@ -45,115 +47,153 @@ pub async fn handle_socket(
flag: Arc<Mutex<bool>>,
) {
let (mut sender, mut receiver) = socket.split();
let app_state_json = serde_json::to_string(&(*app_state.lock().unwrap()).clone()).unwrap();
let app_state_message = Message::Text(format!(
"{{\"type\":\"appstate\",\"payload\":{}}}",
app_state_json
));

// To avoid deadlock.
let app_state_message = {
let app_state_tmp = match app_state.lock() {
Ok(guard) => guard,
Err(_) => {
_ = sender.close();
return;
}
};
let app_state_json = serde_json::to_string(&app_state_tmp.clone()).unwrap();
drop(app_state_tmp);
Message::Text(format!(
"{{\"type\":\"appstate\",\"payload\":{}}}",
app_state_json
))
};

if sender.send(app_state_message).await.is_err() {
return;
}

let atomconf_json = serde_json::to_string(&(*atom_conf.lock().unwrap()).clone()).unwrap();
let atomconf_message = Message::Text(format!(
"{{\"type\":\"atomconf\",\"payload\":{}}}",
atomconf_json
));
let atomconf_message = {
let atom_conf_tmp = match atom_conf.lock() {
Ok(guard) => guard,
Err(_) => {
_ = sender.close();
return;
}
};
let atomconf_json = serde_json::to_string(&atom_conf_tmp.clone()).unwrap();
drop(atom_conf_tmp);
Message::Text(format!(
"{{\"type\":\"atomconf\",\"payload\":{}}}",
atomconf_json
))
};

if sender.send(atomconf_message).await.is_err() {
return;
}

tokio::spawn(async move {
while let Some(Ok(msg)) = receiver.next().await {
let mut app_state_tmp = match app_state.lock() {
Ok(guard) => guard,
Err(_) => continue,
};
match msg {
Message::Text(texta) => {
let text: Vec<&str> = texta.split(',').collect();
match text[0] {
"mode" => {
if text.len() == 2 {
if text[1] == "day" {
app_state_tmp.night_mode = false;
drop(app_state_tmp);
unsafe {
IMP_ISP_Tuning_SetISPRunningMode(
IMPISPRunningMode_IMPISP_RUNNING_MODE_DAY,
);
}
app_state.lock().unwrap().night_mode = false;
} else if text[1] == "night" {
app_state_tmp.night_mode = true;
drop(app_state_tmp);
unsafe {
IMP_ISP_Tuning_SetISPRunningMode(
IMPISPRunningMode_IMPISP_RUNNING_MODE_NIGHT,
);
}
app_state.lock().unwrap().night_mode = true;
}
}
}
"ir" => {
if text.len() == 2 {
if text[1] == "on" {
app_state_tmp.ircut_on = true;
drop(app_state_tmp);
ircut_on().unwrap();
app_state.lock().unwrap().ircut_on = true;
} else if text[1] == "off" {
app_state_tmp.ircut_on = false;
drop(app_state_tmp);
ircut_off().unwrap();
app_state.lock().unwrap().ircut_on = false;
}
}
}
"led" => {
if text.len() == 2 {
if text[1] == "on" {
app_state.lock().unwrap().led_on = true;
app_state_tmp.led_on = true;
drop(app_state_tmp);
} else if text[1] == "off" {
app_state.lock().unwrap().led_on = false;
app_state_tmp.led_on = false;
drop(app_state_tmp);
}
}
}
"irled" => {
if text.len() == 2 {
if text[1] == "on" {
app_state_tmp.irled_on = true;
drop(app_state_tmp);
irled_on().unwrap();
app_state.lock().unwrap().irled_on = true;
} else if text[1] == "off" {
app_state_tmp.irled_on = false;
drop(app_state_tmp);
irled_off().unwrap();
app_state.lock().unwrap().irled_on = false;
}
}
}
"flip" => {
if text.len() == 3 {
if text[1] == "h" {
if text[2] == "on" {
app_state_tmp.flip.0 = true;
drop(app_state_tmp);
unsafe {
IMP_ISP_Tuning_SetISPHflip(
IMPISPTuningOpsMode_IMPISP_TUNING_OPS_MODE_ENABLE,
);
}
app_state.lock().unwrap().flip.0 = true;
} else if text[2] == "off" {
app_state_tmp.flip.0 = false;
drop(app_state_tmp);
unsafe {
IMP_ISP_Tuning_SetISPHflip(
IMPISPTuningOpsMode_IMPISP_TUNING_OPS_MODE_DISABLE,
);
}
app_state.lock().unwrap().flip.0 = false;
}
} else if text[1] == "v" {
if text[2] == "on" {
app_state_tmp.flip.1 = true;
drop(app_state_tmp);
unsafe {
IMP_ISP_Tuning_SetISPVflip(
IMPISPTuningOpsMode_IMPISP_TUNING_OPS_MODE_ENABLE,
);
}
app_state.lock().unwrap().flip.1 = true;
} else if text[2] == "off" {
app_state_tmp.flip.1 = false;
drop(app_state_tmp);
unsafe {
IMP_ISP_Tuning_SetISPVflip(
IMPISPTuningOpsMode_IMPISP_TUNING_OPS_MODE_DISABLE,
);
}
app_state.lock().unwrap().flip.1 = false;
}
}
}
Expand All @@ -162,7 +202,8 @@ pub async fn handle_socket(
if text.len() == 2 {
let fps = text[1].parse().unwrap();
if matches!(fps, 5 | 10 | 15 | 20 | 25) {
app_state.lock().unwrap().fps = fps;
app_state_tmp.fps = fps;
drop(app_state_tmp);
unsafe {
IMP_ISP_Tuning_SetSensorFPS(fps, 1);
}
Expand All @@ -175,20 +216,24 @@ pub async fn handle_socket(
unsafe {
match text[1] {
"sat" => {
app_state_tmp.saturation = v;
drop(app_state_tmp);
IMP_ISP_Tuning_SetSaturation(v);
app_state.lock().unwrap().saturation = v;
}
"brt" => {
app_state_tmp.brightness = v;
drop(app_state_tmp);
IMP_ISP_Tuning_SetBrightness(v);
app_state.lock().unwrap().brightness = v;
}
"cnt" => {
app_state_tmp.contrast = v;
drop(app_state_tmp);
IMP_ISP_Tuning_SetContrast(v);
app_state.lock().unwrap().contrast = v;
}
"shrp" => {
app_state_tmp.sharpness = v;
drop(app_state_tmp);
IMP_ISP_Tuning_SetSharpness(v);
app_state.lock().unwrap().sharpness = v;
}
_ => {}
}
Expand All @@ -198,47 +243,31 @@ pub async fn handle_socket(
"det" => {
if text.len() == 2 {
if text[1] == "on" {
app_state.lock().unwrap().detect = true;
app_state_tmp.detect = true;
drop(app_state_tmp);
} else if text[1] == "off" {
app_state.lock().unwrap().detect = false;
app_state_tmp.detect = false;
drop(app_state_tmp);
}
}
}
"tstmp" => {
if text.len() == 2 {
if text[1] == "on" {
app_state.lock().unwrap().timestamp = true;
app_state_tmp.timestamp = true;
drop(app_state_tmp);
} else if text[1] == "off" {
app_state.lock().unwrap().timestamp = false;
app_state_tmp.timestamp = false;
drop(app_state_tmp);
}
}
}
"save" => {
let app_state_clone = (*app_state.lock().unwrap()).clone();
let app_state_clone = app_state_tmp.clone();
drop(app_state_tmp);
tokio::spawn(save_to_file(app_state_clone));
}
"atomconf" => {
//if text.len() == 4 {
// let mut netconf = NetworkConfig {
// ap_mode: false,
// ssid: "".to_string(),
// psk: "".to_string(),
// };
//
// if text[1] == "on" {
// netconf.ap_mode = true;
// } else if text[1] == "off" {
// netconf.ap_mode = false;
// } else {
// return;
// }
//
// netconf.ssid = text[2].to_string();
// netconf.psk = text[3].to_string();
//
// //tokio::spawn(save_netconf(netconf));
//}
}
"atomconf" => {}
"reboot" => {
tokio::spawn(system::reboot(flag));
break;
Expand All @@ -247,7 +276,8 @@ pub async fn handle_socket(
}
}
Message::Binary(buffer) => {
app_state.lock().unwrap().mask = buffer;
app_state_tmp.mask = buffer;
drop(app_state_tmp);
}
Message::Close(_) => break,
_ => (),
Expand Down

0 comments on commit d36457a

Please sign in to comment.