From b40299538a73d25d096d8f58f1468cd7f647a3f9 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 7 Jan 2024 17:43:05 +0100 Subject: Add first iteration of send page, and fake-radio --- src/main.rs | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 9a4ae2b..0a427e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use anyhow::Context; use log::{debug, info, warn, error}; use std::sync::{Arc, Mutex}; +use tokio::net::UdpSocket; use tokio::sync::mpsc; use sqlx::{Connection, SqliteConnection}; use radio::{RadioManager, MAX_PACKET_LEN}; @@ -11,7 +12,8 @@ mod ui; struct AppState { conf : config::Config, - db : Mutex + db : Mutex, + transmit_queue : mpsc::Sender>, } type SharedState = Arc>; @@ -28,17 +30,37 @@ async fn main() -> std::io::Result<()> { let conf = config::Config::load().expect("Could not load config"); + let (radio_rx_queue, mut packet_receive) = mpsc::channel(16); + let (packet_send, mut radio_tx_queue) = mpsc::channel::>(16); + if conf.freq == 0 { - warn!("Frequency {0} is zero, disabling radio", conf.freq); + warn!("Frequency {0} is zero, disabling radio. Fake receiver udp 127.0.0.1:9073, sending to 9074", conf.freq); + let sock_r = Arc::new(UdpSocket::bind("127.0.0.1:9073").await?); + let sock_s = sock_r.clone(); + + // These two tasks behave like the radio, but use UDP instead of the RF channel. + tokio::spawn(async move { + let mut buf = [0; 1024]; + while let Ok((len, addr)) = sock_r.recv_from(&mut buf).await { + println!("{:?} bytes received from {:?}", len, addr); + let packet = buf[..len].to_vec(); + let rssi = 0f64; + radio_rx_queue.send((packet, rssi)).await.expect("Inject frame"); + } + }); + + tokio::spawn(async move { + while let Some(p) = radio_tx_queue.recv().await { + sock_s.send_to(&p, "127.0.0.1:9074").await.unwrap(); + } + }); } else if !(430000..=436380).contains(&conf.freq) { error!("Frequency {} kHz out of range (430MHz - 436.375MHz), skipping radio setup", conf.freq); } else { info!("Setting up radio"); - let (packet_tx, mut packet_receive) = mpsc::channel(16); - let (packet_send, packet_rx) = mpsc::channel(16); - let mut radio = RadioManager::new(packet_tx, packet_rx).expect("Could not initialize radio"); + let mut radio = RadioManager::new(radio_rx_queue, radio_tx_queue).expect("Could not initialize radio"); let channel = ((conf.freq - 430000) / 25) as u8; radio.set_channel(channel); @@ -82,7 +104,8 @@ async fn main() -> std::io::Result<()> { let shared_state = Arc::new(Mutex::new(AppState { conf, - db: Mutex::new(conn) + db: Mutex::new(conn), + transmit_queue: packet_send.clone(), })); let port = 3000; -- cgit v1.2.3